manifestation: clicking past end of a long, wrapping line containing
non-ASCII would cause the cursor to disappear rather than position past
end of screen line. Hitting enter would then throw an assertion with the
following stack trace:

  Error: text.lua:381: bad argument #2 to 'sub' (number expected, got nil)
  stack traceback:
    [love "boot.lua"]:345: in function <[love "boot.lua"]:341>
    [C]: in function 'sub'
    text.lua:381: in function 'insert_return'
    text.lua:179: in function 'keychord_pressed'
    main.lua:495: in function 'keychord_pressed'
    keychord.lua:10: in function <keychord.lua:5>
    app.lua:34: in function <app.lua:25>
    [C]: in function 'xpcall'

cause: the click caused a call to Text.to_pos_on_line whose result was
not on a UTF-8 character boundary.

fix: make to_pos_on_line utf8-aware.
This commit is contained in:
Kartik K. Agaram 2022-06-13 17:23:21 -07:00
parent 9b0577f79e
commit e20935ad7a
3 changed files with 46 additions and 4 deletions

View File

@ -86,6 +86,8 @@ Last_resize_time = nil
-- blinking cursor
Cursor_time = 0
Quit = false
end -- App.initialize_globals
function App.initialize(arg)
@ -101,6 +103,7 @@ function App.initialize(arg)
end
initialize_font_settings(20)
--? Line_width = 80
if #arg > 0 then
Filename = arg[1]
@ -122,6 +125,8 @@ function App.initialize(arg)
jit.off()
jit.flush()
end
Quit = true
end -- App.initialize
function initialize_window_geometry(geometry_spec)
@ -258,6 +263,8 @@ function App.draw()
if Search_term then
Text.draw_search_bar()
end
--? if Quit then os.exit(1) end
end
function App.update(dt)
@ -319,6 +326,7 @@ function App.mousereleased(x,y, button)
if line.mode == 'text' then
if Text.in_line(line_index,line, x,y) then
Cursor1 = {line=line_index, pos=Text.to_pos_on_line(line, x, y)}
--? print(Cursor1.line, Cursor1.pos)
if Mousepress_shift then
if Old_selection1.line == nil then
Selection1 = Old_cursor1

View File

@ -12,7 +12,7 @@ require 'text_tests'
-- y coordinate drawn until in px
-- position of start of final screen line drawn
function Text.draw(line, line_width, line_index)
--? print('text.draw')
--? print('text.draw', line_index)
love.graphics.setColor(0,0,0)
-- wrap long lines
local x = 25
@ -380,6 +380,9 @@ end
function Text.insert_return()
local byte_offset = utf8.offset(Lines[Cursor1.line].data, Cursor1.pos)
--? print(Cursor1.line, Cursor1.pos, #Lines[Cursor1.line].data)
--? print(Lines[Cursor1.line].data)
assert(byte_offset)
table.insert(Lines, Cursor1.line+1, {mode='text', data=string.sub(Lines[Cursor1.line].data, byte_offset)})
Lines[Cursor1.line].data = string.sub(Lines[Cursor1.line].data, 1, byte_offset-1)
Lines[Cursor1.line].fragments = nil
@ -678,6 +681,7 @@ end
-- convert mx,my in pixels to schema-1 coordinates
function Text.to_pos_on_line(line, mx, my)
--? print('Text.to_pos_on_line', mx, my, 'width', Line_width)
if line.fragments == nil then
Text.compute_fragments(line, Line_width)
end
@ -685,15 +689,20 @@ function Text.to_pos_on_line(line, mx, my)
-- duplicate some logic from Text.draw
local y = line.y
for screen_line_index,screen_line_starting_pos in ipairs(line.screen_line_starting_pos) do
--? print('iter', y, screen_line_index, screen_line_starting_pos, string.sub(line.data, screen_line_starting_pos))
local nexty = y + Line_height
if my < nexty then
-- On all wrapped screen lines but the final one, clicks past end of
-- line position cursor on final character of screen line.
-- (The final screen line positions past end of screen line as always.)
if mx > Line_width and screen_line_index < #line.screen_line_starting_pos then
--? print('past end of non-final line; return')
return line.screen_line_starting_pos[screen_line_index+1]
end
local s = string.sub(line.data, screen_line_starting_pos)
local screen_line_starting_byte_offset = utf8.offset(line.data, screen_line_starting_pos)
assert(screen_line_starting_byte_offset)
local s = string.sub(line.data, screen_line_starting_byte_offset)
--? print('return', mx, Text.nearest_cursor_pos(s, mx), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx) - 1)
return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx) - 1
end
y = nexty
@ -872,13 +881,13 @@ function Text.populate_screen_line_starting_pos(line_index)
local pos = 1
for _, f in ipairs(line.fragments) do
local frag, frag_text = f.data, f.text
--? print(x, frag)
-- render fragment
local frag_width = App.width(frag_text)
--? print(x, pos, frag, frag_width)
if x + frag_width > Line_width then
x = 25
--? print(' ', #line.screen_line_starting_pos, line.data)
table.insert(line.screen_line_starting_pos, pos)
--? print('new screen line:', #line.screen_line_starting_pos, pos)
end
x = x + frag_width
local frag_len = utf8.len(frag)

View File

@ -127,6 +127,31 @@ function test_draw_wrapping_text_containing_non_ascii()
App.screen.check(y, 'm ad', 'F - test_draw_wrapping_text_containing_non_ascii/screen:3')
end
function test_click_on_wrapping_line_containing_non_ascii()
io.write('\ntest_click_on_wrapping_line_containing_non_ascii')
-- display a wrapping line containing non-ASCII
App.screen.init{width=80, height=80}
-- 12345678901234
Lines = load_array{'madam Im adam'} -- notice the non-ASCII apostrophe
Line_width = 75
Cursor1 = {line=1, pos=1}
Screen_top1 = {line=1, pos=1}
Screen_bottom1 = {}
App.draw()
local y = Margin_top
App.screen.check(y, 'madam ', 'F - test_click_on_wrapping_line_containing_non_ascii/baseline/screen:1')
y = y + Line_height
App.screen.check(y, 'Im ada', 'F - test_click_on_wrapping_line_containing_non_ascii/baseline/screen:2')
y = y + Line_height
App.screen.check(y, 'm', 'F - test_click_on_wrapping_line_containing_non_ascii/baseline/screen:3')
y = y + Line_height
-- click past the end of it
App.draw()
App.run_after_mouse_click(App.screen.width-2,y-2, '1')
-- cursor moves to end of line
check_eq(Cursor1.pos, 15, 'F - test_click_on_wrapping_line_containing_non_ascii/cursor') -- one more than the number of UTF-8 code-points
end
function test_edit_wrapping_text()
io.write('\ntest_edit_wrapping_text')
App.screen.init{width=50, height=60}