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-02 09:22:32 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-04-02 09:22:32 -0700
commit3547c18db6b9d508fb5c2399c124e33f4cc8c488 (patch)
treeefec742d0c0a10017b1af35ae1a586077ee0cb05 /text.lua
parent3bdaf97baab7fd03c53a91ebd11bf359ab97a5d3 (diff)
parent29f1687f3c6a494eb67029acbeefbf11571bbe2c (diff)
downloadview.love-3547c18db6b9d508fb5c2399c124e33f4cc8c488.tar.gz
Merge lines.love
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua143
1 files changed, 53 insertions, 90 deletions
diff --git a/text.lua b/text.lua
index 464ab85..35c98f5 100644
--- a/text.lua
+++ b/text.lua
@@ -4,64 +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
-  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 final_screen_line_starting_pos = startpos  -- track value to return
+  Text.populate_screen_line_starting_pos(State, line_index)
+  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', frag)
+--?       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(frag_text)
-      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.draw(frag_text, 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(frag, 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
+      y = y + State.line_height
+      if y >= App.screen.height then
+        break
+      end
     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)
-    end
+  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
-  return y, screen_line_starting_pos
+  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,67 +81,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
-    local frag, frag_text = f.data, f.text
-    -- render fragment
-    local frag_width = App.width(frag_text)
-    if x + frag_width > State.right then
-      x = State.left
-      table.insert(line_cache.screen_line_starting_pos, pos)
-    end
-    x = x + frag_width
-    local frag_len = utf8.len(frag)
-    pos = pos + frag_len
-  end
-end
-
-function Text.compute_fragments(State, line_index)
---?   print('compute_fragments', line_index, 'between', State.left, State.right)
-  local line = State.lines[line_index]
-  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_text = App.newText(love.graphics.getFont(), frag)
-    local frag_width = App.width(frag_text)
---?     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')
+    local frag_width = App.width(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.right - x)
---?         print('bpos', bpos)
-        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
---?         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)
---?         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)
+--?         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
---?       print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px')
-      table.insert(line_cache.fragments, {data=frag, text=frag_text})
+--?       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
 
@@ -687,8 +654,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)
@@ -776,15 +742,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)
@@ -914,7 +878,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