From 4ab901c92e11f47828bc7f5f16d8d2250812d53f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 14:48:59 -0700 Subject: [PATCH 01/11] get rid of to_text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've been misunderstanding what Text objects are. They can render a lot of text with a given line height, word wrap, colors in various places. And I've been creating one for every word :facepalm: Unwinding this will take some time. This is just a first baby step for ad hoc text objects. Turns out I don't need to convert to Text to get something's rendered width, just the Font can do that. Thanks to the LÖVE Discord for educating me: https://discord.com/channels/329400828920070144/330089431379869708/1091535487333826580 --- app.lua | 10 +++++++++- commands.lua | 29 ++++++++++++----------------- help.lua | 2 +- log_browser.lua | 13 ++++++------- run.lua | 8 ++------ source.lua | 8 -------- source_text.lua | 3 +-- 7 files changed, 31 insertions(+), 42 deletions(-) diff --git a/app.lua b/app.lua index 59dff31..816c9af 100644 --- a/app.lua +++ b/app.lua @@ -219,6 +219,9 @@ function App.newText(font, s) end function App.width(text) + if type(text) == 'string' then + return love.graphics.getFont():getWidth(text) + end return text.text:getWidth() end @@ -425,7 +428,12 @@ function App.disable_tests() App.screen.print = love.graphics.print App.newText = love.graphics.newText App.screen.draw = love.graphics.draw - App.width = function(text) return text:getWidth() end + App.width = function(text) + if type(text) == 'string' then + return love.graphics.getFont():getWidth(text) + end + return text:getWidth() + end if Current_app == nil or Current_app == 'run' then App.open_for_reading = function(filename) return io.open(filename, 'r') end App.open_for_writing = function(filename) return io.open(filename, 'w') end diff --git a/commands.lua b/commands.lua index b1ac5ab..512992d 100644 --- a/commands.lua +++ b/commands.lua @@ -43,21 +43,19 @@ function source.draw_menu_bar() end function add_hotkey_to_menu(s) - local s_text = to_text(s) - local width = App.width(s_text) + local width = App.width(s) if Menu_cursor > App.screen.width - 30 then return end App.color(Menu_command_color) - App.screen.draw(s_text, Menu_cursor,5) + App.screen.print(s, Menu_cursor,5) Menu_cursor = Menu_cursor + width + 30 end function source.draw_file_navigator() App.color(Menu_command_color) - local filter_text = to_text(File_navigation.filter) - App.screen.draw(filter_text, 5, 5) - draw_cursor(5 + App.width(filter_text), 5) + App.screen.print(File_navigation.filter, 5, 5) + draw_cursor(5 + App.width(File_navigation.filter), 5) if File_navigation.num_lines == nil then File_navigation.num_lines = source.num_lines_for_file_navigator(File_navigation.candidates) end @@ -97,7 +95,7 @@ function source.num_lines_for_file_navigator(candidates) local result = 1 local x = 5 for i,filename in ipairs(candidates) do - local width = App.width(to_text(filename)) + local width = App.width(filename) if x + width > App.screen.width - 5 then result = result+1 x = 5 + width @@ -109,8 +107,7 @@ function source.num_lines_for_file_navigator(candidates) end function add_file_to_menu(x,y, s, cursor_highlight) - local s_text = to_text(s) - local width = App.width(s_text) + local width = App.width(s) if x + width > App.screen.width - 5 then y = y + Editor_state.line_height x = 5 @@ -125,7 +122,7 @@ function add_file_to_menu(x,y, s, cursor_highlight) end }) App.color(Menu_command_color) - App.screen.draw(s_text, x,y) + App.screen.print(s, x,y) x = x + width + 30 return x,y end @@ -189,8 +186,7 @@ function log_render.file_navigator_state(o, x,y, w) local x2,y2 = 0,0 local width = 0 for i,filename in ipairs(o.files) do - local filename_text = to_text(filename) - width = App.width(filename_text) + width = App.width(filename) if x2 + width > App.screen.width - 5 then y2 = y2 + Editor_state.line_height x2 = 0 @@ -207,8 +203,7 @@ function log_render.file_navigator_state(o, x,y, w) local x3,y3 = 0,y -- x3 is relative, y3 is absolute local width = 0 for i,filename in ipairs(o.files) do - local filename_text = to_text(filename) - width = App.width(filename_text) + width = App.width(filename) if x3 + width > App.screen.width - 5 then y3 = y3 + Editor_state.line_height x3 = 0 @@ -219,7 +214,7 @@ function log_render.file_navigator_state(o, x,y, w) end if x3 >= menu_xmin and x3 + width < menu_xmax then App.color(Menu_command_color) - App.screen.draw(filename_text, x + x3-menu_xmin, y3) + App.screen.print(filename, x + x3-menu_xmin, y3) end x3 = x3 + width + 30 end @@ -246,7 +241,7 @@ end function file_coord(index) local y,x = Menu_status_bar_height, 5 for i,filename in ipairs(File_navigation.candidates) do - local width = App.width(to_text(filename)) + local width = App.width(filename) if x + width > App.screen.width - 5 then y = y + Editor_state.line_height x = 5 @@ -264,7 +259,7 @@ function file_index(fy, fx, fwidth) local y,x = Menu_status_bar_height, 5 local best_guess, best_guess_x, best_guess_width for i,filename in ipairs(File_navigation.candidates) do - local width = App.width(to_text(filename)) + local width = App.width(filename) if x + width > App.screen.width - 5 then y = y + Editor_state.line_height x = 5 diff --git a/help.lua b/help.lua index 145692f..d91be3c 100644 --- a/help.lua +++ b/help.lua @@ -147,5 +147,5 @@ function current_shape(State, shape) end function bullet_indent() - return App.width(to_text('* ')) + return App.width('* ') end diff --git a/log_browser.lua b/log_browser.lua index 46d84c0..b128ab7 100644 --- a/log_browser.lua +++ b/log_browser.lua @@ -98,21 +98,20 @@ function log_browser.draw(State) local xright = render_stack_right_margin(State, line_index, line, y) if line.section_name then App.color(Section_border_color) - local section_text = to_text(line.section_name) if line.section_begin then local sectiony = y+Section_border_padding_vertical love.graphics.line(xleft,sectiony, xleft,y+State.line_height) love.graphics.line(xright,sectiony, xright,y+State.line_height) love.graphics.line(xleft,sectiony, xleft+50-2,sectiony) - love.graphics.draw(section_text, xleft+50,y) - love.graphics.line(xleft+50+App.width(section_text)+2,sectiony, xright,sectiony) + love.graphics.print(line.section_name, xleft+50,y) + love.graphics.line(xleft+50+App.width(line.section_name)+2,sectiony, xright,sectiony) else assert(line.section_end) local sectiony = y+State.line_height-Section_border_padding_vertical love.graphics.line(xleft,y, xleft,sectiony) love.graphics.line(xright,y, xright,sectiony) love.graphics.line(xleft,sectiony, xleft+50-2,sectiony) - love.graphics.draw(section_text, xleft+50,y) - love.graphics.line(xleft+50+App.width(section_text)+2,sectiony, xright,sectiony) + love.graphics.print(line.section_name, xleft+50,y) + love.graphics.line(xleft+50+App.width(line.section_name)+2,sectiony, xright,sectiony) end else if type(line.data) == 'string' then @@ -148,7 +147,7 @@ function render_stack_left_margin(State, line_index, line, y) love.graphics.print(line.section_stack[i].name, x+State.font_height+5, y+5, --[[vertically]] math.pi/2) end if y > App.screen.height-log_browser.height(State, line_index) then - love.graphics.print(line.section_stack[i].name, x+State.font_height+5, App.screen.height-App.width(to_text(line.section_stack[i].name))-5, --[[vertically]] math.pi/2) + love.graphics.print(line.section_stack[i].name, x+State.font_height+5, App.screen.height-App.width(line.section_stack[i].name)-5, --[[vertically]] math.pi/2) end end return log_browser.left_margin(State, line) @@ -163,7 +162,7 @@ function render_stack_right_margin(State, line_index, line, y) love.graphics.print(line.section_stack[i].name, x, y+5, --[[vertically]] math.pi/2) end if y > App.screen.height-log_browser.height(State, line_index) then - love.graphics.print(line.section_stack[i].name, x, App.screen.height-App.width(to_text(line.section_stack[i].name))-5, --[[vertically]] math.pi/2) + love.graphics.print(line.section_stack[i].name, x, App.screen.height-App.width(line.section_stack[i].name)-5, --[[vertically]] math.pi/2) end end return log_browser.right_margin(State, line) diff --git a/run.lua b/run.lua index 049362e..6da1c59 100644 --- a/run.lua +++ b/run.lua @@ -197,10 +197,6 @@ function run.key_release(key, scancode) return edit.key_release(Editor_state, key, scancode) end --- use this sparingly -function to_text(s) - if Text_cache[s] == nil then - Text_cache[s] = App.newText(love.graphics.getFont(), s) - end - return Text_cache[s] +function width(s) + return love.graphics.getFont():getWidth(s) end diff --git a/source.lua b/source.lua index 2413eb0..bc2b1c5 100644 --- a/source.lua +++ b/source.lua @@ -418,11 +418,3 @@ function source.key_release(key, scancode) return log_browser.keychord_press(Log_browser_state, chordkey, scancode) end end - --- use this sparingly -function to_text(s) - if Text_cache[s] == nil then - Text_cache[s] = App.newText(love.graphics.getFont(), s) - end - return Text_cache[s] -end diff --git a/source_text.lua b/source_text.lua index 4d13748..2aa27bc 100644 --- a/source_text.lua +++ b/source_text.lua @@ -45,8 +45,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) if starts_with(trimmed_word, '[[') and ends_with(trimmed_word, ']]') then local filename = trimmed_word:gsub('^..(.*)..$', '%1') if source.link_exists(State, filename) then - local filename_text = App.newText(love.graphics.getFont(), filename) - button(State, 'link', {x=x+App.width(to_text('[[')), y=y, w=App.width(filename_text), h=State.line_height, color={1,1,1}, + button(State, 'link', {x=x+App.width('[['), y=y, w=App.width(filename), h=State.line_height, color={1,1,1}, icon = icon.hyperlink_decoration, onpress1 = function() source.switch_to_file(filename) From 876d6298b40fc8b00bf559d4ec2d909ab1e6bc80 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 16:29:17 -0700 Subject: [PATCH 02/11] App.width can no longer take a Text In the process I discovered the horrible fact that Text.x allocates a new Text. And it gets called (just once, thank goodness) on every single frame. --- app.lua | 11 +---------- drawing.lua | 9 ++++----- edit.lua | 7 ------- run.lua | 4 +--- search.lua | 5 +---- select.lua | 7 ++----- source.lua | 4 +--- source_edit.lua | 7 ------- source_select.lua | 7 ++----- source_text.lua | 19 ++++++++----------- text.lua | 19 ++++++++----------- 11 files changed, 28 insertions(+), 71 deletions(-) diff --git a/app.lua b/app.lua index 816c9af..48ab363 100644 --- a/app.lua +++ b/app.lua @@ -219,10 +219,7 @@ function App.newText(font, s) end function App.width(text) - if type(text) == 'string' then - return love.graphics.getFont():getWidth(text) - end - return text.text:getWidth() + return love.graphics.getFont():getWidth(text) end function App.screen.draw(obj, x,y) @@ -428,12 +425,6 @@ function App.disable_tests() App.screen.print = love.graphics.print App.newText = love.graphics.newText App.screen.draw = love.graphics.draw - App.width = function(text) - if type(text) == 'string' then - return love.graphics.getFont():getWidth(text) - end - return text:getWidth() - end if Current_app == nil or Current_app == 'run' then App.open_for_reading = function(filename) return io.open(filename, 'r') end App.open_for_writing = function(filename) return io.open(filename, 'w') end diff --git a/drawing.lua b/drawing.lua index 99193c6..5af25fb 100644 --- a/drawing.lua +++ b/drawing.lua @@ -60,14 +60,13 @@ function Drawing.draw(State, line_index, y) if State.current_drawing_mode == 'name' and i == line.pending.target_point then -- create a faint red box for the name App.color(Current_name_background_color) - local name_text - -- TODO: avoid computing name width on every repaint + local name_width if p.name == '' then - name_text = State.em + name_width = App.width('m') else - name_text = App.newText(love.graphics.getFont(), p.name) + name_width = App.width(p.name) end - love.graphics.rectangle('fill', x,y, App.width(name_text), State.line_height) + love.graphics.rectangle('fill', x,y, name_width, State.line_height) end end end diff --git a/edit.lua b/edit.lua index 3cea87f..407d8fb 100644 --- a/edit.lua +++ b/edit.lua @@ -103,7 +103,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c -- search search_term = nil, - search_text = nil, search_backup = nil, -- stuff to restore when cancelling search } return result @@ -324,7 +323,6 @@ function edit.text_input(State, t) --? print('text input', t) if State.search_term then State.search_term = State.search_term..t - State.search_text = nil Text.search_next(State) elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then local before = snapshot(State, State.lines.current_drawing_index) @@ -355,20 +353,17 @@ function edit.keychord_press(State, chord, key) for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll if chord == 'escape' then State.search_term = nil - State.search_text = nil State.cursor1 = State.search_backup.cursor State.screen_top1 = State.search_backup.screen_top State.search_backup = nil Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks elseif chord == 'return' then State.search_term = nil - State.search_text = nil State.search_backup = nil elseif chord == 'backspace' then local len = utf8.len(State.search_term) local byte_offset = Text.offset(State.search_term, len) State.search_term = string.sub(State.search_term, 1, byte_offset-1) - State.search_text = nil elseif chord == 'down' then State.cursor1.pos = State.cursor1.pos+1 Text.search_next(State) @@ -382,7 +377,6 @@ function edit.keychord_press(State, chord, key) cursor={line=State.cursor1.line, pos=State.cursor1.pos}, screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos}, } - assert(State.search_text == nil) -- zoom elseif chord == 'C-=' then edit.update_font_settings(State, State.font_height+2) @@ -514,7 +508,6 @@ function edit.update_font_settings(State, font_height) State.font_height = font_height love.graphics.setFont(love.graphics.newFont(State.font_height)) State.line_height = math.floor(font_height*1.3) - State.em = App.newText(love.graphics.getFont(), 'm') Text_cache = {} end diff --git a/run.lua b/run.lua index 6da1c59..f3364c2 100644 --- a/run.lua +++ b/run.lua @@ -71,12 +71,10 @@ end function run.initialize_default_settings() local font_height = 20 love.graphics.setFont(love.graphics.newFont(font_height)) - local em = App.newText(love.graphics.getFont(), 'm') - run.initialize_window_geometry(App.width(em)) + run.initialize_window_geometry(App.width('m')) Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right) Editor_state.font_height = font_height Editor_state.line_height = math.floor(font_height*1.3) - Editor_state.em = em Settings = run.settings() end diff --git a/search.lua b/search.lua index f7d4732..7d3d335 100644 --- a/search.lua +++ b/search.lua @@ -13,10 +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) - if State.search_text == nil then - State.search_text = App.newText(love.graphics.getFont(), State.search_term) - end - Text.draw_cursor(State, 25+App.width(State.search_text),y-5) + Text.draw_cursor(State, 25+App.width(State.search_term),y-5) end function Text.search_next(State) diff --git a/select.lua b/select.lua index bae9504..efb6909 100644 --- a/select.lua +++ b/select.lua @@ -58,15 +58,12 @@ 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) - local before_text = App.newText(love.graphics.getFont(), before) - lo_px = App.width(before_text) + lo_px = App.width(before) end --? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px) local s = line.data:sub(lo_offset, hi_offset-1) - local text = App.newText(love.graphics.getFont(), s) - local text_width = App.width(text) App.color(Highlight_color) - love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height) + love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height) App.color(Text_color) return lo_px end diff --git a/source.lua b/source.lua index bc2b1c5..3531bbf 100644 --- a/source.lua +++ b/source.lua @@ -162,13 +162,11 @@ end function source.initialize_default_settings() local font_height = 20 love.graphics.setFont(love.graphics.newFont(font_height)) - local em = App.newText(love.graphics.getFont(), 'm') - source.initialize_window_geometry(App.width(em)) + source.initialize_window_geometry(App.width('m')) Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right) Editor_state.filename = 'run.lua' Editor_state.font_height = font_height Editor_state.line_height = math.floor(font_height*1.3) - Editor_state.em = em end function source.initialize_window_geometry(em_width) diff --git a/source_edit.lua b/source_edit.lua index 2fc7e1b..de3ddd2 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -104,7 +104,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c -- search search_term = nil, - search_text = nil, search_backup = nil, -- stuff to restore when cancelling search } return result @@ -325,7 +324,6 @@ function edit.text_input(State, t) --? print('text input', t) if State.search_term then State.search_term = State.search_term..t - State.search_text = nil Text.search_next(State) elseif State.lines.current_drawing and State.current_drawing_mode == 'name' then local before = snapshot(State, State.lines.current_drawing_index) @@ -356,20 +354,17 @@ function edit.keychord_press(State, chord, key) for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll if chord == 'escape' then State.search_term = nil - State.search_text = nil State.cursor1 = State.search_backup.cursor State.screen_top1 = State.search_backup.screen_top State.search_backup = nil Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks elseif chord == 'return' then State.search_term = nil - State.search_text = nil State.search_backup = nil elseif chord == 'backspace' then local len = utf8.len(State.search_term) local byte_offset = Text.offset(State.search_term, len) State.search_term = string.sub(State.search_term, 1, byte_offset-1) - State.search_text = nil elseif chord == 'down' then State.cursor1.pos = State.cursor1.pos+1 Text.search_next(State) @@ -383,7 +378,6 @@ function edit.keychord_press(State, chord, key) cursor={line=State.cursor1.line, pos=State.cursor1.pos}, screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos}, } - assert(State.search_text == nil) -- zoom elseif chord == 'C-=' then edit.update_font_settings(State, State.font_height+2) @@ -515,7 +509,6 @@ function edit.update_font_settings(State, font_height) State.font_height = font_height love.graphics.setFont(love.graphics.newFont(State.font_height)) State.line_height = math.floor(font_height*1.3) - State.em = App.newText(love.graphics.getFont(), 'm') Text_cache = {} end diff --git a/source_select.lua b/source_select.lua index 297a7bc..2298513 100644 --- a/source_select.lua +++ b/source_select.lua @@ -60,15 +60,12 @@ 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) - local before_text = App.newText(love.graphics.getFont(), before) - lo_px = App.width(before_text) + lo_px = App.width(before) end --? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px) local s = line.data:sub(lo_offset, hi_offset-1) - local text = App.newText(love.graphics.getFont(), s) - local text_width = App.width(text) App.color(Highlight_color) - love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height) + love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height) App.color(Text_color) return lo_px end diff --git a/source_text.lua b/source_text.lua index 2aa27bc..cf47f2a 100644 --- a/source_text.lua +++ b/source_text.lua @@ -26,7 +26,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) --? print('skipping', frag) else -- render fragment - local frag_width = App.width(frag_text) + local frag_width = App.width(f.data) if x + frag_width > State.right then assert(x > State.left) -- no overfull lines y = y + State.line_height @@ -106,7 +106,7 @@ function Text.populate_screen_line_starting_pos(State, line_index) for _, f in ipairs(line_cache.fragments) do local frag, frag_text = f.data, f.text -- render fragment - local frag_width = App.width(frag_text) + local frag_width = App.width(f.data) if x + frag_width > State.right then x = State.left table.insert(line_cache.screen_line_starting_pos, pos) @@ -130,7 +130,7 @@ function Text.compute_fragments(State, line_index) -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do local frag_text = App.newText(love.graphics.getFont(), frag) - local frag_width = App.width(frag_text) + local frag_width = App.width(frag) --? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go') while x + frag_width > State.right do --? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x)) @@ -145,13 +145,13 @@ function Text.compute_fragments(State, line_index) --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' 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) + local frag1_width = App.width(frag1) --? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px') assert(x + frag1_width <= State.right) table.insert(line_cache.fragments, {data=frag1, text=frag1_text}) frag = string.sub(frag, boffset) frag_text = App.newText(love.graphics.getFont(), frag) - frag_width = App.width(frag_text) + frag_width = App.width(frag) end x = State.left -- new line end @@ -775,8 +775,7 @@ function Text.screen_line_width(State, line_index, i) else screen_line = string.sub(line.data, start_pos) end - local screen_line_text = App.newText(love.graphics.getFont(), screen_line) - return App.width(screen_line_text) + return App.width(screen_line) end function Text.screen_line_index(screen_line_starting_pos, pos) @@ -864,15 +863,13 @@ function Text.x_after(s, pos) local offset = Text.offset(s, math.min(pos+1, #s+1)) local s_before = s:sub(1, offset-1) --? print('^'..s_before..'$') - local text_before = App.newText(love.graphics.getFont(), s_before) - return App.width(text_before) + return App.width(s_before) end function Text.x(s, pos) local offset = Text.offset(s, pos) local s_before = s:sub(1, offset-1) - local text_before = App.newText(love.graphics.getFont(), s_before) - return App.width(text_before) + return App.width(s_before) end function Text.to2(State, loc1) diff --git a/text.lua b/text.lua index 589e4fd..5045570 100644 --- a/text.lua +++ b/text.lua @@ -23,7 +23,7 @@ function Text.draw(State, line_index, y, startpos) --? print('skipping', frag) else -- render fragment - local frag_width = App.width(frag_text) + local frag_width = App.width(f.data) if x + frag_width > State.right then assert(x > State.left) -- no overfull lines y = y + State.line_height @@ -90,7 +90,7 @@ function Text.populate_screen_line_starting_pos(State, line_index) for _, f in ipairs(line_cache.fragments) do local frag, frag_text = f.data, f.text -- render fragment - local frag_width = App.width(frag_text) + local frag_width = App.width(f.data) if x + frag_width > State.right then x = State.left table.insert(line_cache.screen_line_starting_pos, pos) @@ -114,7 +114,7 @@ function Text.compute_fragments(State, line_index) -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do local frag_text = App.newText(love.graphics.getFont(), frag) - local frag_width = App.width(frag_text) + local frag_width = App.width(frag) --? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go') while x + frag_width > State.right do --? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x)) @@ -129,13 +129,13 @@ function Text.compute_fragments(State, line_index) --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' 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) + local frag1_width = App.width(frag1) --? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px') assert(x + frag1_width <= State.right) table.insert(line_cache.fragments, {data=frag1, text=frag1_text}) frag = string.sub(frag, boffset) frag_text = App.newText(love.graphics.getFont(), frag) - frag_width = App.width(frag_text) + frag_width = App.width(frag) end x = State.left -- new line end @@ -759,8 +759,7 @@ function Text.screen_line_width(State, line_index, i) else screen_line = string.sub(line.data, start_pos) end - local screen_line_text = App.newText(love.graphics.getFont(), screen_line) - return App.width(screen_line_text) + return App.width(screen_line) end function Text.screen_line_index(screen_line_starting_pos, pos) @@ -848,15 +847,13 @@ function Text.x_after(s, pos) local offset = Text.offset(s, math.min(pos+1, #s+1)) local s_before = s:sub(1, offset-1) --? print('^'..s_before..'$') - local text_before = App.newText(love.graphics.getFont(), s_before) - return App.width(text_before) + return App.width(s_before) end function Text.x(s, pos) local offset = Text.offset(s, pos) local s_before = s:sub(1, offset-1) - local text_before = App.newText(love.graphics.getFont(), s_before) - return App.width(text_before) + return App.width(s_before) end function Text.to2(State, loc1) From f64f680f2b4361cece5e8746a870f986473ca502 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 16:38:22 -0700 Subject: [PATCH 03/11] no more Text allocations Is it just my imagination, or does the app feel lighter and more fluffy? --- app.lua | 9 --------- edit.lua | 1 - source_edit.lua | 1 - source_text.lua | 26 ++++++++++---------------- text.lua | 22 ++++++++-------------- 5 files changed, 18 insertions(+), 41 deletions(-) diff --git a/app.lua b/app.lua index 48ab363..f5e114d 100644 --- a/app.lua +++ b/app.lua @@ -210,14 +210,6 @@ function App.wait_fake_time(t) App.time = App.time + t end --- LÖVE's Text primitive retains no trace of the string it was created from, --- so we'll wrap it for our tests. --- --- This implies that we need to hook any operations we need on Text objects. -function App.newText(font, s) - return {type='text', data=s, text=love.graphics.newText(font, s)} -end - function App.width(text) return love.graphics.getFont():getWidth(text) end @@ -423,7 +415,6 @@ function App.disable_tests() App.screen.move = love.window.setPosition App.screen.position = love.window.getPosition App.screen.print = love.graphics.print - App.newText = love.graphics.newText App.screen.draw = love.graphics.draw if Current_app == nil or Current_app == 'run' then App.open_for_reading = function(filename) return io.open(filename, 'r') end diff --git a/edit.lua b/edit.lua index 407d8fb..e8ce5a5 100644 --- a/edit.lua +++ b/edit.lua @@ -87,7 +87,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c font_height = font_height, line_height = line_height, - em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width top = top, left = math.floor(left), diff --git a/source_edit.lua b/source_edit.lua index de3ddd2..99c2fcb 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -88,7 +88,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c font_height = font_height, line_height = line_height, - em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width top = top, left = math.floor(left), diff --git a/source_text.lua b/source_text.lua index cf47f2a..159e8af 100644 --- a/source_text.lua +++ b/source_text.lua @@ -17,13 +17,12 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) initialize_color() for _, f in ipairs(line_cache.fragments) do App.color(Text_color) - local frag, frag_text = f.data, f.text - select_color(frag) - local frag_len = utf8.len(frag) ---? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y) + select_color(f.data) + local frag_len = utf8.len(f.data) +--? print('text.draw:', f.data, 'at', line_index,pos, 'after', x,y) if pos < startpos then -- render nothing ---? print('skipping', frag) +--? print('skipping', f.data) else -- render fragment local frag_width = App.width(f.data) @@ -41,7 +40,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) Text.draw_highlight(State, line, x,y, pos, lo,hi) end -- Make [[WikiWords]] (single word, all in one screen line) clickable. - local trimmed_word = rtrim(frag) -- compute_fragments puts whitespace at the end + local trimmed_word = rtrim(f.data) -- compute_fragments puts whitespace at the end if starts_with(trimmed_word, '[[') and ends_with(trimmed_word, ']]') then local filename = trimmed_word:gsub('^..(.*)..$', '%1') if source.link_exists(State, filename) then @@ -53,7 +52,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) }) end end - App.screen.draw(frag_text, x,y) + App.screen.print(f.data, x,y) -- render cursor if necessary if line_index == State.cursor1.line then if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then @@ -64,7 +63,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) love.graphics.print(State.search_term, x+lo_px,y) end elseif Focus == 'edit' then - Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y) + Text.draw_cursor(State, x+Text.x(f.data, State.cursor1.pos-pos+1), y) App.color(Text_color) end end @@ -104,7 +103,6 @@ function Text.populate_screen_line_starting_pos(State, line_index) local x = State.left local pos = 1 for _, f in ipairs(line_cache.fragments) do - local frag, frag_text = f.data, f.text -- render fragment local frag_width = App.width(f.data) if x + frag_width > State.right then @@ -112,8 +110,7 @@ function Text.populate_screen_line_starting_pos(State, line_index) table.insert(line_cache.screen_line_starting_pos, pos) end x = x + frag_width - local frag_len = utf8.len(frag) - pos = pos + frag_len + pos = pos + utf8.len(f.data) end end @@ -129,7 +126,6 @@ function Text.compute_fragments(State, line_index) local x = State.left -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do - local frag_text = App.newText(love.graphics.getFont(), frag) local frag_width = App.width(frag) --? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go') while x + frag_width > State.right do @@ -144,20 +140,18 @@ function Text.compute_fragments(State, line_index) local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes') local frag1 = string.sub(frag, 1, boffset-1) - local frag1_text = App.newText(love.graphics.getFont(), frag1) local frag1_width = App.width(frag1) --? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px') assert(x + frag1_width <= State.right) - table.insert(line_cache.fragments, {data=frag1, text=frag1_text}) + table.insert(line_cache.fragments, {data=frag1}) frag = string.sub(frag, boffset) - frag_text = App.newText(love.graphics.getFont(), frag) frag_width = App.width(frag) end x = State.left -- new line end if #frag > 0 then --? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px') - table.insert(line_cache.fragments, {data=frag, text=frag_text}) + table.insert(line_cache.fragments, {data=frag}) end x = x + frag_width end diff --git a/text.lua b/text.lua index 5045570..cb8653e 100644 --- a/text.lua +++ b/text.lua @@ -15,12 +15,11 @@ function Text.draw(State, line_index, y, startpos) Text.compute_fragments(State, line_index) for _, f in ipairs(line_cache.fragments) do App.color(Text_color) - local frag, frag_text = f.data, f.text - local frag_len = utf8.len(frag) ---? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y) + local frag_len = utf8.len(f.data) +--? print('text.draw:', f.data, 'at', line_index,pos, 'after', x,y) if pos < startpos then -- render nothing ---? print('skipping', frag) +--? print('skipping', f.data) else -- render fragment local frag_width = App.width(f.data) @@ -37,7 +36,7 @@ function Text.draw(State, line_index, y, startpos) local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len) Text.draw_highlight(State, line, x,y, pos, lo,hi) end - App.screen.draw(frag_text, x,y) + App.screen.print(f.data, x,y) -- render cursor if necessary if line_index == State.cursor1.line then if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then @@ -48,7 +47,7 @@ function Text.draw(State, line_index, y, startpos) love.graphics.print(State.search_term, x+lo_px,y) end else - Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y) + Text.draw_cursor(State, x+Text.x(f.data, State.cursor1.pos-pos+1), y) end end end @@ -88,7 +87,6 @@ function Text.populate_screen_line_starting_pos(State, line_index) local x = State.left local pos = 1 for _, f in ipairs(line_cache.fragments) do - local frag, frag_text = f.data, f.text -- render fragment local frag_width = App.width(f.data) if x + frag_width > State.right then @@ -96,8 +94,7 @@ function Text.populate_screen_line_starting_pos(State, line_index) table.insert(line_cache.screen_line_starting_pos, pos) end x = x + frag_width - local frag_len = utf8.len(frag) - pos = pos + frag_len + pos = pos + utf8.len(f.data) end end @@ -113,7 +110,6 @@ function Text.compute_fragments(State, line_index) local x = State.left -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do - local frag_text = App.newText(love.graphics.getFont(), frag) local frag_width = App.width(frag) --? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go') while x + frag_width > State.right do @@ -128,20 +124,18 @@ function Text.compute_fragments(State, line_index) local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos --? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes') local frag1 = string.sub(frag, 1, boffset-1) - local frag1_text = App.newText(love.graphics.getFont(), frag1) local frag1_width = App.width(frag1) --? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px') assert(x + frag1_width <= State.right) - table.insert(line_cache.fragments, {data=frag1, text=frag1_text}) + table.insert(line_cache.fragments, {data=frag1}) frag = string.sub(frag, boffset) - frag_text = App.newText(love.graphics.getFont(), frag) frag_width = App.width(frag) end x = State.left -- new line end if #frag > 0 then --? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px') - table.insert(line_cache.fragments, {data=frag, text=frag_text}) + table.insert(line_cache.fragments, {data=frag}) end x = x + frag_width end From fbc8b85bcf73d41d0afa074061f58e93e316fa40 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 16:41:40 -0700 Subject: [PATCH 04/11] clean up some debug prints It's starting to become apparent just how little line_cache.fragments does for me now. Let's see if we can get rid of it entirely. --- source_text.lua | 8 -------- text.lua | 8 -------- 2 files changed, 16 deletions(-) diff --git a/source_text.lua b/source_text.lua index 159e8af..9382fbd 100644 --- a/source_text.lua +++ b/source_text.lua @@ -115,7 +115,6 @@ function Text.populate_screen_line_starting_pos(State, line_index) end function Text.compute_fragments(State, line_index) ---? print('compute_fragments', line_index, 'between', State.left, State.right) local line = State.lines[line_index] if line.mode ~= 'text' then return end local line_cache = State.line_cache[line_index] @@ -127,21 +126,15 @@ function Text.compute_fragments(State, line_index) -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do local frag_width = App.width(frag) ---? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go') while x + frag_width > State.right do ---? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x)) if (x-State.left) < 0.8 * (State.right-State.left) then ---? print('splitting') -- long word; chop it at some letter -- We're not going to reimplement TeX here. local bpos = Text.nearest_pos_less_than(frag, State.right - x) ---? print('bpos', bpos) if bpos == 0 then break end -- avoid infinite loop when window is too narrow local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos ---? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes') local frag1 = string.sub(frag, 1, boffset-1) local frag1_width = App.width(frag1) ---? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px') assert(x + frag1_width <= State.right) table.insert(line_cache.fragments, {data=frag1}) frag = string.sub(frag, boffset) @@ -150,7 +143,6 @@ function Text.compute_fragments(State, line_index) x = State.left -- new line end if #frag > 0 then ---? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px') table.insert(line_cache.fragments, {data=frag}) end x = x + frag_width diff --git a/text.lua b/text.lua index cb8653e..d5af693 100644 --- a/text.lua +++ b/text.lua @@ -99,7 +99,6 @@ function Text.populate_screen_line_starting_pos(State, line_index) end function Text.compute_fragments(State, line_index) ---? print('compute_fragments', line_index, 'between', State.left, State.right) local line = State.lines[line_index] if line.mode ~= 'text' then return end local line_cache = State.line_cache[line_index] @@ -111,21 +110,15 @@ function Text.compute_fragments(State, line_index) -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do local frag_width = App.width(frag) ---? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go') while x + frag_width > State.right do ---? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x)) if (x-State.left) < 0.8 * (State.right-State.left) then ---? print('splitting') -- long word; chop it at some letter -- We're not going to reimplement TeX here. local bpos = Text.nearest_pos_less_than(frag, State.right - x) ---? print('bpos', bpos) if bpos == 0 then break end -- avoid infinite loop when window is too narrow local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos ---? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes') local frag1 = string.sub(frag, 1, boffset-1) local frag1_width = App.width(frag1) ---? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px') assert(x + frag1_width <= State.right) table.insert(line_cache.fragments, {data=frag1}) frag = string.sub(frag, boffset) @@ -134,7 +127,6 @@ function Text.compute_fragments(State, line_index) x = State.left -- new line end if #frag > 0 then ---? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px') table.insert(line_cache.fragments, {data=frag}) end x = x + frag_width From ed27b8dd8582d871fa936621049c6f77cfdccb29 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 16:43:07 -0700 Subject: [PATCH 05/11] stop creating a singleton table for every word --- source_text.lua | 24 ++++++++++++------------ text.lua | 20 ++++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/source_text.lua b/source_text.lua index 9382fbd..3f5ec74 100644 --- a/source_text.lua +++ b/source_text.lua @@ -17,15 +17,15 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) initialize_color() for _, f in ipairs(line_cache.fragments) do App.color(Text_color) - select_color(f.data) - local frag_len = utf8.len(f.data) ---? print('text.draw:', f.data, 'at', line_index,pos, 'after', x,y) + select_color(f) + local frag_len = utf8.len(f) +--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y) if pos < startpos then -- render nothing ---? print('skipping', f.data) +--? print('skipping', f) else -- render fragment - local frag_width = App.width(f.data) + local frag_width = App.width(f) if x + frag_width > State.right then assert(x > State.left) -- no overfull lines y = y + State.line_height @@ -40,7 +40,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) Text.draw_highlight(State, line, x,y, pos, lo,hi) end -- Make [[WikiWords]] (single word, all in one screen line) clickable. - local trimmed_word = rtrim(f.data) -- compute_fragments puts whitespace at the end + local trimmed_word = rtrim(f) -- compute_fragments puts whitespace at the end if starts_with(trimmed_word, '[[') and ends_with(trimmed_word, ']]') then local filename = trimmed_word:gsub('^..(.*)..$', '%1') if source.link_exists(State, filename) then @@ -52,7 +52,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) }) end end - App.screen.print(f.data, x,y) + App.screen.print(f, x,y) -- render cursor if necessary if line_index == State.cursor1.line then if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then @@ -63,7 +63,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) love.graphics.print(State.search_term, x+lo_px,y) end elseif Focus == 'edit' then - Text.draw_cursor(State, x+Text.x(f.data, State.cursor1.pos-pos+1), y) + Text.draw_cursor(State, x+Text.x(f, State.cursor1.pos-pos+1), y) App.color(Text_color) end end @@ -104,13 +104,13 @@ function Text.populate_screen_line_starting_pos(State, line_index) local pos = 1 for _, f in ipairs(line_cache.fragments) do -- render fragment - local frag_width = App.width(f.data) + local frag_width = App.width(f) if x + frag_width > State.right then x = State.left table.insert(line_cache.screen_line_starting_pos, pos) end x = x + frag_width - pos = pos + utf8.len(f.data) + pos = pos + utf8.len(f) end end @@ -136,14 +136,14 @@ function Text.compute_fragments(State, line_index) local frag1 = string.sub(frag, 1, boffset-1) local frag1_width = App.width(frag1) assert(x + frag1_width <= State.right) - table.insert(line_cache.fragments, {data=frag1}) + table.insert(line_cache.fragments, frag1) frag = string.sub(frag, boffset) frag_width = App.width(frag) end x = State.left -- new line end if #frag > 0 then - table.insert(line_cache.fragments, {data=frag}) + table.insert(line_cache.fragments, frag) end x = x + frag_width end diff --git a/text.lua b/text.lua index d5af693..cc9dcb7 100644 --- a/text.lua +++ b/text.lua @@ -15,14 +15,14 @@ function Text.draw(State, line_index, y, startpos) Text.compute_fragments(State, line_index) for _, f in ipairs(line_cache.fragments) do App.color(Text_color) - local frag_len = utf8.len(f.data) ---? print('text.draw:', f.data, 'at', line_index,pos, 'after', x,y) + local frag_len = utf8.len(f) +--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y) if pos < startpos then -- render nothing ---? print('skipping', f.data) +--? print('skipping', f) else -- render fragment - local frag_width = App.width(f.data) + local frag_width = App.width(f) if x + frag_width > State.right then assert(x > State.left) -- no overfull lines y = y + State.line_height @@ -36,7 +36,7 @@ function Text.draw(State, line_index, y, startpos) local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len) Text.draw_highlight(State, line, x,y, pos, lo,hi) end - App.screen.print(f.data, x,y) + App.screen.print(f, x,y) -- render cursor if necessary if line_index == State.cursor1.line then if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then @@ -47,7 +47,7 @@ function Text.draw(State, line_index, y, startpos) love.graphics.print(State.search_term, x+lo_px,y) end else - Text.draw_cursor(State, x+Text.x(f.data, State.cursor1.pos-pos+1), y) + Text.draw_cursor(State, x+Text.x(f, State.cursor1.pos-pos+1), y) end end end @@ -88,13 +88,13 @@ function Text.populate_screen_line_starting_pos(State, line_index) local pos = 1 for _, f in ipairs(line_cache.fragments) do -- render fragment - local frag_width = App.width(f.data) + local frag_width = App.width(f) if x + frag_width > State.right then x = State.left table.insert(line_cache.screen_line_starting_pos, pos) end x = x + frag_width - pos = pos + utf8.len(f.data) + pos = pos + utf8.len(f) end end @@ -120,14 +120,14 @@ function Text.compute_fragments(State, line_index) local frag1 = string.sub(frag, 1, boffset-1) local frag1_width = App.width(frag1) assert(x + frag1_width <= State.right) - table.insert(line_cache.fragments, {data=frag1}) + table.insert(line_cache.fragments, frag1) frag = string.sub(frag, boffset) frag_width = App.width(frag) end x = State.left -- new line end if #frag > 0 then - table.insert(line_cache.fragments, {data=frag}) + table.insert(line_cache.fragments, frag) end x = x + frag_width end From 24a732ebff281dd2dee5f906b4b949315cdd018f Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 16:48:40 -0700 Subject: [PATCH 06/11] update documentation on fragments I see a path to at least maintain a single fragment per screen line. But can we do better? It even seems unnecessary to maintain two copies of the data, chopped up into lines and screen lines. --- edit.lua | 2 +- source_edit.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/edit.lua b/edit.lua index e8ce5a5..9a198f3 100644 --- a/edit.lua +++ b/edit.lua @@ -53,7 +53,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c -- rendering wrapped text lines needs some additional short-lived data per line: -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen -- starty, the y coord in pixels the line starts rendering from - -- fragments: snippets of rendered love.graphics.Text, guaranteed to not straddle screen lines + -- fragments: snippets of the line guaranteed to not straddle screen lines -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line line_cache = {}, diff --git a/source_edit.lua b/source_edit.lua index 99c2fcb..5dabee9 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -54,7 +54,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c -- rendering wrapped text lines needs some additional short-lived data per line: -- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen -- starty, the y coord in pixels the line starts rendering from - -- fragments: snippets of rendered love.graphics.Text, guaranteed to not straddle screen lines + -- fragments: snippets of the line guaranteed to not straddle screen lines -- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line line_cache = {}, From c87e4a3babdc5fc0b338a549f17266783b4bdecd Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 17:05:57 -0700 Subject: [PATCH 07/11] start thinking of compute_fragments as a detail I think all we need to maintain is the populate_screen_line_starting_pos array. It's easy to render screen lines one by one from it, and we'll only ever construct one additional screen line at a time. I'd hoped to delete other calls to Text.populate_screen_line_starting_pos, but it turns out we need to update it when editing sometimes. Give up on that for now; it's a no-op if not needed. --- source_text.lua | 2 +- text.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source_text.lua b/source_text.lua index 3f5ec74..b68bf85 100644 --- a/source_text.lua +++ b/source_text.lua @@ -12,7 +12,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) local x = State.left local pos = 1 local screen_line_starting_pos = startpos - Text.compute_fragments(State, line_index) + Text.populate_screen_line_starting_pos(State, line_index) local pos = 1 initialize_color() for _, f in ipairs(line_cache.fragments) do diff --git a/text.lua b/text.lua index cc9dcb7..0dcd607 100644 --- a/text.lua +++ b/text.lua @@ -12,7 +12,7 @@ function Text.draw(State, line_index, y, startpos) local x = State.left local pos = 1 local screen_line_starting_pos = startpos - Text.compute_fragments(State, line_index) + Text.populate_screen_line_starting_pos(State, line_index) for _, f in ipairs(line_cache.fragments) do App.color(Text_color) local frag_len = utf8.len(f) From 86517606a1fafcbff97b5646b74cd186a575cf68 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 20:39:39 -0700 Subject: [PATCH 08/11] clean up some final bifold code --- source_file.lua | 20 ++------------------ source_undo.lua | 2 +- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/source_file.lua b/source_file.lua index 194ef9b..3adab1f 100644 --- a/source_file.lua +++ b/source_file.lua @@ -1,7 +1,5 @@ -- primitives for saving to file and loading from file -Fold = '\x1e' -- ASCII RS (record separator) - function file_exists(filename) local infile = App.open_for_reading(filename) if infile then @@ -28,13 +26,7 @@ function load_from_file(infile) if line == '```lines' then -- inflexible with whitespace since these files are always autogenerated table.insert(result, load_drawing(infile_next_line)) else - local line_info = {mode='text'} - if line:find(Fold) then - _, _, line_info.data, line_info.dataB = line:find('([^'..Fold..']*)'..Fold..'([^'..Fold..']*)') - else - line_info.data = line - end - table.insert(result, line_info) + table.insert(result, {mode='text', data=line}) end end end @@ -54,10 +46,6 @@ function save_to_disk(State) store_drawing(outfile, line) else outfile:write(line.data) - if line.dataB and #line.dataB > 0 then - outfile:write(Fold) - outfile:write(line.dataB) - end outfile:write('\n') end end @@ -147,11 +135,7 @@ function load_array(a) else --? print('inserting text') local line_info = {mode='text'} - if line:find(Fold) then - _, _, line_info.data, line_info.dataB = line:find('([^'..Fold..']*)'..Fold..'([^'..Fold..']*)') - else - line_info.data = line - end + line_info.data = line table.insert(result, line_info) end end diff --git a/source_undo.lua b/source_undo.lua index 6023324..d699211 100644 --- a/source_undo.lua +++ b/source_undo.lua @@ -61,7 +61,7 @@ function snapshot(State, s,e) for i=s,e do local line = State.lines[i] if line.mode == 'text' then - table.insert(event.lines, {mode='text', data=line.data, dataB=line.dataB}) + table.insert(event.lines, {mode='text', data=line.data}) elseif line.mode == 'drawing' then local points=deepcopy(line.points) --? print('copying', line.points, 'with', #line.points, 'points into', points) From af239935ce849f0a1f4a4708acd65faf6eb6d84b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 17:34:48 -0700 Subject: [PATCH 09/11] minor cleanup and a todo for later --- source_undo.lua | 9 ++------- undo.lua | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/source_undo.lua b/source_undo.lua index d699211..d3d5f0f 100644 --- a/source_undo.lua +++ b/source_undo.lua @@ -61,14 +61,9 @@ function snapshot(State, s,e) for i=s,e do local line = State.lines[i] if line.mode == 'text' then - table.insert(event.lines, {mode='text', data=line.data}) + table.insert(event.lines, {mode='text', data=line.data}) -- I've forgotten: should we deepcopy(line.data)? elseif line.mode == 'drawing' then - local points=deepcopy(line.points) ---? print('copying', line.points, 'with', #line.points, 'points into', points) - local shapes=deepcopy(line.shapes) ---? print('copying', line.shapes, 'with', #line.shapes, 'shapes into', shapes) - table.insert(event.lines, {mode='drawing', h=line.h, points=points, shapes=shapes, pending={}}) ---? table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}}) + table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}}) else print(line.mode) assert(false) diff --git a/undo.lua b/undo.lua index d699211..d3d5f0f 100644 --- a/undo.lua +++ b/undo.lua @@ -61,14 +61,9 @@ function snapshot(State, s,e) for i=s,e do local line = State.lines[i] if line.mode == 'text' then - table.insert(event.lines, {mode='text', data=line.data}) + table.insert(event.lines, {mode='text', data=line.data}) -- I've forgotten: should we deepcopy(line.data)? elseif line.mode == 'drawing' then - local points=deepcopy(line.points) ---? print('copying', line.points, 'with', #line.points, 'points into', points) - local shapes=deepcopy(line.shapes) ---? print('copying', line.shapes, 'with', #line.shapes, 'shapes into', shapes) - table.insert(event.lines, {mode='drawing', h=line.h, points=points, shapes=shapes, pending={}}) ---? table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}}) + table.insert(event.lines, {mode='drawing', h=line.h, points=deepcopy(line.points), shapes=deepcopy(line.shapes), pending={}}) else print(line.mode) assert(false) From d0d39797cff0a558162b73da8550c3914f2d1a89 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 18:13:44 -0700 Subject: [PATCH 10/11] show count of test failures --- app.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.lua b/app.lua index f5e114d..b6be1b2 100644 --- a/app.lua +++ b/app.lua @@ -137,7 +137,7 @@ function App.run_tests_and_initialize() Test_errors = {} App.run_tests() if #Test_errors > 0 then - error('There were test failures:\n\n'..table.concat(Test_errors)) + error(('There were %d test failures:\n\n%s'):format(#Test_errors, table.concat(Test_errors))) end App.disable_tests() App.initialize_globals() From 29f1687f3c6a494eb67029acbeefbf11571bbe2c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 1 Apr 2023 20:03:43 -0700 Subject: [PATCH 11/11] avoid saving fragments in lines Now we render lines one screen line at a time rather than one word at a time. I can't port the source side just yet; I need to fix hyperlinks first.. --- app.lua | 2 +- text.lua | 115 ++++++++++++++++++++----------------------------- text_tests.lua | 2 +- 3 files changed, 49 insertions(+), 70 deletions(-) diff --git a/app.lua b/app.lua index b6be1b2..b883a75 100644 --- a/app.lua +++ b/app.lua @@ -373,7 +373,7 @@ end -- prepend file/line/test function prepend_debug_info_to_test_failure(test_name, err) local err_without_line_number = err:gsub('^[^:]*:[^:]*: ', '') - local stack_trace = debug.traceback('', --[[stack frame]]4) + local stack_trace = debug.traceback('', --[[stack frame]]5) local file_and_line_number = stack_trace:gsub('stack traceback:\n', ''):gsub(': .*', '') local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number --? local full_error = file_and_line_number..':'..test_name..' -- '..err_without_line_number..'\t\t'..stack_trace:gsub('\n', '\n\t\t') diff --git a/text.lua b/text.lua index 0dcd607..4d20deb 100644 --- a/text.lua +++ b/text.lua @@ -4,63 +4,64 @@ Text = {} -- draw a line starting from startpos to screen at y between State.left and State.right -- return the final y, and position of start of final screen line drawn function Text.draw(State, line_index, y, startpos) +--? print('text.draw', line_index, y) local line = State.lines[line_index] local line_cache = State.line_cache[line_index] line_cache.starty = y line_cache.startpos = startpos -- wrap long lines - local x = State.left - local pos = 1 - local screen_line_starting_pos = startpos + local final_screen_line_starting_pos = startpos -- track value to return Text.populate_screen_line_starting_pos(State, line_index) - for _, f in ipairs(line_cache.fragments) do - App.color(Text_color) - local frag_len = utf8.len(f) ---? print('text.draw:', f, 'at', line_index,pos, 'after', x,y) + assert(#line_cache.screen_line_starting_pos >= 1) + for i=1,#line_cache.screen_line_starting_pos do + local pos = line_cache.screen_line_starting_pos[i] if pos < startpos then -- render nothing --? print('skipping', f) else + final_screen_line_starting_pos = pos + local f = Text.screen_line(line, line_cache, i) +--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y) + local frag_len = utf8.len(f) -- render fragment - local frag_width = App.width(f) - if x + frag_width > State.right then - assert(x > State.left) -- no overfull lines - y = y + State.line_height - if y + State.line_height > App.screen.height then - return y, screen_line_starting_pos - end - screen_line_starting_pos = pos - x = State.left - end if State.selection1.line then local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len) - Text.draw_highlight(State, line, x,y, pos, lo,hi) + Text.draw_highlight(State, line, State.left,y, pos, lo,hi) end - App.screen.print(f, x,y) + App.color(Text_color) + App.screen.print(f, State.left,y) -- render cursor if necessary if line_index == State.cursor1.line then - if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then + if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then if State.search_term then if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then - local lo_px = Text.draw_highlight(State, line, x,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)) + local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)) App.color(Text_color) - love.graphics.print(State.search_term, x+lo_px,y) + love.graphics.print(State.search_term, State.left+lo_px,y) end else - Text.draw_cursor(State, x+Text.x(f, State.cursor1.pos-pos+1), y) + Text.draw_cursor(State, State.left+Text.x(f, State.cursor1.pos-pos+1), y) end end end - x = x + frag_width - end - pos = pos + frag_len - end - if State.search_term == nil then - if line_index == State.cursor1.line and State.cursor1.pos == pos then - Text.draw_cursor(State, x, y) + y = y + State.line_height + if y >= App.screen.height then + break + end end end - return y, screen_line_starting_pos + return y - State.line_height, final_screen_line_starting_pos +end + +function Text.screen_line(line, line_cache, i) + local pos = line_cache.screen_line_starting_pos[i] + local offset = Text.offset(line.data, pos) + if i >= #line_cache.screen_line_starting_pos then + return line.data:sub(offset) + end + local endpos = line_cache.screen_line_starting_pos[i+1]-1 + local end_offset = Text.offset(line.data, endpos) + return line.data:sub(offset, end_offset) end function Text.draw_cursor(State, x, y) @@ -81,55 +82,34 @@ function Text.populate_screen_line_starting_pos(State, line_index) if line_cache.screen_line_starting_pos then return end - -- duplicate some logic from Text.draw - Text.compute_fragments(State, line_index) line_cache.screen_line_starting_pos = {1} - local x = State.left + local x = 0 local pos = 1 - for _, f in ipairs(line_cache.fragments) do - -- render fragment - local frag_width = App.width(f) - if x + frag_width > State.right then - x = State.left - table.insert(line_cache.screen_line_starting_pos, pos) - end - x = x + frag_width - pos = pos + utf8.len(f) - end -end - -function Text.compute_fragments(State, line_index) - local line = State.lines[line_index] - if line.mode ~= 'text' then return end - local line_cache = State.line_cache[line_index] - if line_cache.fragments then - return - end - line_cache.fragments = {} - local x = State.left -- try to wrap at word boundaries for frag in line.data:gmatch('%S*%s*') do local frag_width = App.width(frag) - while x + frag_width > State.right do - if (x-State.left) < 0.8 * (State.right-State.left) then +--? 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.right - x) - if bpos == 0 then break end -- avoid infinite loop when window is too narrow + local bpos = Text.nearest_pos_less_than(frag, State.width - x) + -- everything works if bpos == 0, but is a little inefficient + pos = pos + bpos local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos - local frag1 = string.sub(frag, 1, boffset-1) - local frag1_width = App.width(frag1) - assert(x + frag1_width <= State.right) - table.insert(line_cache.fragments, frag1) frag = string.sub(frag, boffset) +--? if bpos > 0 then +--? print('after chop:', frag) +--? end frag_width = App.width(frag) end - x = State.left -- new line - end - if #frag > 0 then - table.insert(line_cache.fragments, frag) +--? print('screen line:', pos) + table.insert(line_cache.screen_line_starting_pos, pos) + x = 0 -- new screen line end x = x + frag_width + pos = pos + utf8.len(frag) end end @@ -974,7 +954,6 @@ function Text.redraw_all(State) end function Text.clear_screen_line_cache(State, line_index) - State.line_cache[line_index].fragments = nil State.line_cache[line_index].screen_line_starting_pos = nil end diff --git a/text_tests.lua b/text_tests.lua index 4fe0ca8..cfdffdd 100644 --- a/text_tests.lua +++ b/text_tests.lua @@ -1007,7 +1007,7 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line() y = y + Editor_state.line_height App.screen.check(y, 'jkl ', 'screen:2') y = y + Editor_state.line_height - App.screen.check(y, 'mno ', 'screen:3') + App.screen.check(y, 'mn', 'screen:3') end function test_pagedown_never_moves_up()