about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-04-01 16:38:22 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-04-01 18:12:29 -0700
commitf64f680f2b4361cece5e8746a870f986473ca502 (patch)
treef9187930631d93aff59ad9884a4482f43dc41223 /text.lua
parent876d6298b40fc8b00bf559d4ec2d909ab1e6bc80 (diff)
downloadlines.love-f64f680f2b4361cece5e8746a870f986473ca502.tar.gz
no more Text allocations
Is it just my imagination, or does the app feel lighter and more fluffy?
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua22
1 files changed, 8 insertions, 14 deletions
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