about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-04-01 14:48:59 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-04-01 16:12:55 -0700
commit4ab901c92e11f47828bc7f5f16d8d2250812d53f (patch)
tree288dcf758b377c9b8586a9fc4bd7706d3a91e1f4
parentfd2e5acb464c439325455acb63683d4fa316497c (diff)
downloadlines.love-4ab901c92e11f47828bc7f5f16d8d2250812d53f.tar.gz
get rid of to_text
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
-rw-r--r--app.lua10
-rw-r--r--commands.lua29
-rw-r--r--help.lua2
-rw-r--r--log_browser.lua13
-rw-r--r--run.lua8
-rw-r--r--source.lua8
-rw-r--r--source_text.lua3
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)