about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-07-20 09:11:29 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-07-20 09:11:29 -0700
commite2734cd572a8578ee07637f9c5471c0ccce119cc (patch)
tree71d09bdea7815b3f9c334351f65d322a01420ddc /text.lua
parent192e16b42f1312dde5a23a3907de2af5a46de9a7 (diff)
downloadtext.love-e2734cd572a8578ee07637f9c5471c0ccce119cc.tar.gz
bugfix: where cursor is drawn
The published version of lines.love was broken for almost an hour. The
cursor would render one position to the right of where it really is. To
fix it, this commit rolls back 26ba6e4e5a71. There doesn't seem a good
way to test it.
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua15
1 files changed, 11 insertions, 4 deletions
diff --git a/text.lua b/text.lua
index 5a9896f..db0c3b7 100644
--- a/text.lua
+++ b/text.lua
@@ -803,15 +803,15 @@ end
 function Text.nearest_pos_less_than(line, x)
 --?   print('', '-- nearest_pos_less_than', line, x)
   local len = utf8.len(line)
-  local max_x = Text.x(line, len)
+  local max_x = Text.x_after(line, len)
   if x > max_x then
     return len+1
   end
   local left, right = 0, len+1
   while true do
     local curr = math.floor((left+right)/2)
-    local currxmin = Text.x(line, curr+1)
-    local currxmax = Text.x(line, curr+2)
+    local currxmin = Text.x_after(line, curr+1)
+    local currxmax = Text.x_after(line, curr+2)
 --?     print('', x, left, right, curr, currxmin, currxmax)
     if currxmin <= x and x < currxmax then
       return curr
@@ -828,7 +828,7 @@ function Text.nearest_pos_less_than(line, x)
   assert(false)
 end
 
-function Text.x(s, pos)
+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..'$')
@@ -836,6 +836,13 @@ function Text.x(s, pos)
   return App.width(text_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)
+end
+
 function Text.to2(State, pos1)
   if State.lines[pos1.line].mode == 'drawing' then
     return {line=pos1.line, screen_line=1, screen_pos=1}