From ce31b74b10798799f03b1a9fb54dff491a1cf223 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 24 Aug 2022 13:27:04 -0700 Subject: [PATCH] =?UTF-8?q?infrastructure=20for=20caching=20L=C3=96VE=20te?= =?UTF-8?q?xt=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- edit.lua | 1 + help.lua | 7 +------ main.lua | 11 +++++++++++ 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