Merge text0

This commit is contained in:
Kartik K. Agaram 2024-01-15 02:33:22 -08:00
commit 83881938f5
4 changed files with 29 additions and 34 deletions

View File

@ -269,8 +269,3 @@ for handler_name in pairs(love.handlers) do
end
end
end
function warning_message()
assert(type(Current_app) == 'table')
return Current_app.message
end

View File

@ -13,7 +13,7 @@ function Text.draw_search_bar(State)
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
App.color(Text_color)
App.screen.print(State.search_term, 25,y-5)
Text.draw_cursor(State, 25+App.width(State.search_term),y-5, Cursor_color)
Text.draw_cursor(State, 25+State.font:getWidth(State.search_term),y-5, Cursor_color)
end
function Text.search_next(State)

View File

@ -57,11 +57,11 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
lo_px = 0
else
local before = line.data:sub(pos_offset, lo_offset-1)
lo_px = App.width(before)
lo_px = State.font:getWidth(before)
end
local s = line.data:sub(lo_offset, hi_offset-1)
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
love.graphics.rectangle('fill', x+lo_px,y, State.font:getWidth(s),State.line_height)
App.color(Text_color)
return lo_px
end

View File

@ -46,18 +46,18 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor)
else
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
if hide_cursor then
Text.pretend_draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
else
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
end
elseif pos + frag_len == State.cursor1.pos then
-- Show cursor at end of line.
-- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
-- It seems useful to see a cursor whether your eye is on the left or right margin.
if hide_cursor then
Text.pretend_draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
else
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
end
end
end
@ -111,14 +111,14 @@ function Text.populate_screen_line_starting_pos(State, line_index)
local pos = 1
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_width = App.width(frag)
local frag_width = State.font:getWidth(frag)
--? print('-- frag:', frag, pos, x, frag_width, State.width)
while x + frag_width > State.width do
--? print('frag:', frag, pos, x, frag_width, State.width)
if x < 0.8 * State.width then
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.width - x)
local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
if x == 0 and bpos == 0 then
assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
end
@ -128,7 +128,7 @@ function Text.populate_screen_line_starting_pos(State, line_index)
--? if bpos > 0 then
--? print('after chop:', frag)
--? end
frag_width = App.width(frag)
frag_width = State.font:getWidth(frag)
end
--? print('screen line:', pos)
table.insert(line_cache.screen_line_starting_pos, pos)
@ -423,7 +423,7 @@ function Text.up(State)
screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
end
else
-- move up one screen line in current line
@ -431,7 +431,7 @@ function Text.up(State)
local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
--? print('cursor pos is now '..tostring(State.cursor1.pos))
end
if Text.lt1(State.cursor1, State.screen_top1) then
@ -452,7 +452,7 @@ function Text.down(State)
if State.cursor1.line < #State.lines then
local new_cursor_line = State.cursor1.line+1
State.cursor1.line = new_cursor_line
State.cursor1.pos = Text.nearest_cursor_pos(State.lines[State.cursor1.line].data, State.cursor_x, State.left)
State.cursor1.pos = Text.nearest_cursor_pos(State.font, State.lines[State.cursor1.line].data, State.cursor_x, State.left)
--? print(State.cursor1.pos)
end
if State.cursor1.line > State.screen_bottom1.line then
@ -471,7 +471,7 @@ function Text.down(State)
--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
--? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
if scroll_down then
--? print('scroll up preserving cursor')
@ -685,8 +685,8 @@ function Text.to_pos_on_line(State, line_index, mx, my)
return line_cache.screen_line_starting_pos[screen_line_index+1]-1
end
local s = string.sub(line.data, screen_line_starting_byte_offset)
--? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1
--? print('return', mx, Text.nearest_cursor_pos(State.font, s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1)
return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
end
y = nexty
end
@ -706,7 +706,7 @@ function Text.screen_line_width(State, line_index, i)
else
screen_line = string.sub(line.data, start_pos)
end
return App.width(screen_line)
return State.font:getWidth(screen_line)
end
function Text.screen_line_index(screen_line_starting_pos, pos)
@ -720,12 +720,12 @@ end
-- convert x pixel coordinate to pos
-- oblivious to wrapping
-- result: 1 to len+1
function Text.nearest_cursor_pos(line, x, left)
function Text.nearest_cursor_pos(font, line, x, left)
if x < left then
return 1
end
local len = utf8.len(line)
local max_x = left+Text.x(line, len+1)
local max_x = left+Text.x(font, line, len+1)
if x > max_x then
return len+1
end
@ -737,8 +737,8 @@ function Text.nearest_cursor_pos(line, x, left)
return leftpos
end
local curr = math.floor((leftpos+rightpos)/2)
local currxmin = left+Text.x(line, curr)
local currxmax = left+Text.x(line, curr+1)
local currxmin = left+Text.x(font, line, curr)
local currxmax = left+Text.x(font, line, curr+1)
--? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
if currxmin <= x and x < currxmax then
if x-currxmin < currxmax-x then
@ -762,18 +762,18 @@ end
-- return the nearest index of line (in utf8 code points) which lies entirely
-- within x pixels of the left margin
-- result: 0 to len+1
function Text.nearest_pos_less_than(line, x)
function Text.nearest_pos_less_than(font, line, x)
--? print('', '-- nearest_pos_less_than', line, x)
local len = utf8.len(line)
local max_x = Text.x_after(line, len)
local max_x = Text.x_after(font, line, len)
if x > max_x then
return len+1
end
local left, right = 0, len+1
while true do
local curr = math.floor((left+right)/2)
local currxmin = Text.x_after(line, curr+1)
local currxmax = Text.x_after(line, curr+2)
local currxmin = Text.x_after(font, line, curr+1)
local currxmax = Text.x_after(font, line, curr+2)
--? print('', x, left, right, curr, currxmin, currxmax)
if currxmin <= x and x < currxmax then
return curr
@ -790,18 +790,18 @@ function Text.nearest_pos_less_than(line, x)
assert(false, 'failed to map x pixel to pos')
end
function Text.x_after(s, pos)
function Text.x_after(font, s, pos)
local len = utf8.len(s)
local offset = Text.offset(s, math.min(pos+1, len+1))
local s_before = s:sub(1, offset-1)
--? print('^'..s_before..'$')
return App.width(s_before)
return font:getWidth(s_before)
end
function Text.x(s, pos)
function Text.x(font, s, pos)
local offset = Text.offset(s, pos)
local s_before = s:sub(1, offset-1)
return App.width(s_before)
return font:getWidth(s_before)
end
function Text.to2(State, loc1)