about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-08-24 13:27:04 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-08-24 13:27:04 -0700
commitce31b74b10798799f03b1a9fb54dff491a1cf223 (patch)
tree3c4ff3c6fc1501fbc743a85157b2768b2c7bfbe2
parent89222f86a0dc3dd774ce46afaa1ba791ed65dba9 (diff)
downloadview.love-ce31b74b10798799f03b1a9fb54dff491a1cf223.tar.gz
infrastructure for caching LÖVE text objects
-rw-r--r--edit.lua1
-rw-r--r--help.lua7
-rw-r--r--main.lua11
3 files changed, 13 insertions, 6 deletions
diff --git a/edit.lua b/edit.lua
index 7a59ff3..f5dc1f2 100644
--- a/edit.lua
+++ b/edit.lua
@@ -467,6 +467,7 @@ function edit.update_font_settings(State, font_height)
   love.graphics.setFont(love.graphics.newFont(Editor_state.font_height))
   State.line_height = math.floor(font_height*1.3)
   State.em = App.newText(love.graphics.getFont(), 'm')
+  Text_cache = {}
 end
 
 --== some methods for tests
diff --git a/help.lua b/help.lua
index 2abeb00..145692f 100644
--- a/help.lua
+++ b/help.lua
@@ -146,11 +146,6 @@ function current_shape(State, shape)
   end
 end
 
-_bullet_indent = nil
 function bullet_indent()
-  if _bullet_indent == nil then
-    local text = love.graphics.newText(love.graphics.getFont(), '* ')
-    _bullet_indent = text:getWidth()
-  end
-  return _bullet_indent
+  return App.width(to_text('* '))
 end
diff --git a/main.lua b/main.lua
index f328139..a41d93b 100644
--- a/main.lua
+++ b/main.lua
@@ -16,6 +16,9 @@ Editor_state = {}
 function App.initialize_globals()
   -- tests currently mostly clear their own state
 
+  -- a few text objects we can avoid recomputing unless the font changes
+  Text_cache = {}
+
   -- blinking cursor
   Cursor_time = 0
 
@@ -204,3 +207,11 @@ function App.keyreleased(key, scancode)
   Cursor_time = 0  -- ensure cursor is visible immediately after it moves
   return edit.key_released(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]
+end