bugfix: UTF-8 in compute_fragments

This commit is contained in:
Kartik K. Agaram 2022-06-14 07:30:47 -07:00
parent 5b6171cf02
commit 9b0577f79e
3 changed files with 27 additions and 5 deletions

View File

@ -194,6 +194,8 @@ function App.draw()
Button_handlers = {}
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle('fill', 0, 0, App.screen.width-1, App.screen.height-1)
--? love.graphics.setColor(0, 1, 0)
--? love.graphics.line(Line_width,0, Line_width,App.screen.height)
love.graphics.setColor(0, 0, 0)
-- some hysteresis while resizing

View File

@ -110,17 +110,19 @@ function Text.compute_fragments(line, line_width)
--? print(frag, x, frag_width, line_width)
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local b = Text.nearest_pos_less_than(frag, line_width - x)
assert(b > 0) -- avoid infinite loop when window is too narrow
--? print('space for '..tostring(b)..' graphemes')
local frag1 = string.sub(frag, 1, b)
local bpos = Text.nearest_pos_less_than(frag, line_width - x)
assert(bpos > 0) -- avoid infinite loop when window is too narrow
local boffset = utf8.offset(frag, bpos+1) -- byte _after_ bpos
assert(boffset)
--? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset)..' bytes')
local frag1 = string.sub(frag, 1, boffset-1)
local frag1_text = App.newText(love.graphics.getFont(), frag1)
local frag1_width = App.width(frag1_text)
--? print(frag, x, frag1_width, line_width)
assert(x + frag1_width <= line_width)
--? print('inserting '..frag1..' of width '..tostring(frag1_width)..'px')
table.insert(line.fragments, {data=frag1, text=frag1_text})
frag = string.sub(frag, b+1)
frag = string.sub(frag, boffset)
frag_text = App.newText(love.graphics.getFont(), frag)
frag_width = App.width(frag_text)
end

View File

@ -109,6 +109,24 @@ function test_draw_text_wrapping_within_word()
App.screen.check(y, 'jk', 'F - test_draw_text_wrapping_within_word/screen:3')
end
function test_draw_wrapping_text_containing_non_ascii()
-- draw a long line containing non-ASCII
io.write('\ntest_draw_wrapping_text_containing_non_ascii')
App.screen.init{width=60, height=60}
Lines = load_array{'madam Im adam', 'xyz'} -- notice the non-ASCII apostrophe
Line_width = App.screen.width
Cursor1 = {line=1, pos=1}
Screen_top1 = {line=1, pos=1}
Screen_bottom1 = {}
App.draw()
local y = Margin_top
App.screen.check(y, 'mada', 'F - test_draw_wrapping_text_containing_non_ascii/screen:1')
y = y + Line_height
App.screen.check(y, 'm I', 'F - test_draw_wrapping_text_containing_non_ascii/screen:2')
y = y + Line_height
App.screen.check(y, 'm ad', 'F - test_draw_wrapping_text_containing_non_ascii/screen:3')
end
function test_edit_wrapping_text()
io.write('\ntest_edit_wrapping_text')
App.screen.init{width=50, height=60}