From 91b9a02b2845fde8e56b3414bef0999ad12b890e Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 25 Jul 2024 21:52:29 -0700 Subject: [PATCH] redo font caching This is a do-over of parts of commits b32a60548 and f1de276ce. The key ideas are: if edit.update_font_settings receives a font object, it's already of the requested font_height if update_editor_box receives a font, it's already of the requested height scale(20) if B receives a font, it's already of the requested height scale(20) whether B receives a font or not, all the editors on Surface will share a single font object when it's done, and the font object will have the right size A sets the font and passes it in to all the above functions. --- 0019-B | 7 +++---- 0021-compute_layout | 10 +++++----- 0028-A | 11 ++++++----- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/0019-B b/0019-B index 2bf7d7c..00ec547 100644 --- a/0019-B +++ b/0019-B @@ -1,6 +1,5 @@ -B = function() +B = function(font) -- recompute various aspects based on the current viewport settings - local font = nil -- ensure a single font object over the whole surface for _,obj in ipairs(Surface) do if obj.type == 'line' then obj.zdata = {} @@ -17,8 +16,8 @@ B = function() obj.zdata = love.math.newBezierCurve(zdata):render() elseif obj.type == 'text' then if obj.w then - update_editor_box(obj, font or obj.editor.font) - if obj.editor and obj.editor.font then font = obj.editor.font end + update_editor_box(obj, font) + if font == nil and obj.editor then font = obj.editor.font end else obj.text = love.graphics.newText(love.graphics.getFont(), obj.data) end diff --git a/0021-compute_layout b/0021-compute_layout index eb74338..94e0a78 100644 --- a/0021-compute_layout +++ b/0021-compute_layout @@ -1,4 +1,4 @@ -compute_layout = function(node, x,y, nodes_to_render) +compute_layout = function(node, x,y, nodes_to_render, font) -- append to nodes_to_render flattened instructions to render a hierarchy of nodes -- return x,y rendered until (surface coordinates) if node.type == 'text' then @@ -31,7 +31,7 @@ compute_layout = function(node, x,y, nodes_to_render) if node.editor == nil then initialize_editor(node) else - update_editor_box(node) + update_editor_box(node, font) end node.h = box_height(node) table.insert(nodes_to_render, node) @@ -59,7 +59,7 @@ compute_layout = function(node, x,y, nodes_to_render) if not child.width then child.width = node.width -- HACK: should we set child.w or child.width? Neither is quite satisfactory. end - subx,suby = compute_layout(child, x,suby, nodes_to_render) + subx,suby = compute_layout(child, x,suby, nodes_to_render, font) if w < child.w then w = child.w end @@ -87,7 +87,7 @@ compute_layout = function(node, x,y, nodes_to_render) subx = subx+child.margin w = w+child.margin end - subx,suby = compute_layout(child, subx,y, nodes_to_render) + subx,suby = compute_layout(child, subx,y, nodes_to_render, font) w = w + child.w if h < child.h then h = child.h @@ -101,4 +101,4 @@ compute_layout = function(node, x,y, nodes_to_render) end end return x+node.w,y+node.h -end +end \ No newline at end of file diff --git a/0028-A b/0028-A index 120d1c4..bb73b75 100644 --- a/0028-A +++ b/0028-A @@ -1,5 +1,6 @@ A = function() - love.graphics.setFont(love.graphics.newFont(scale(20))) -- editor objects implicitly depend on current font + local font = love.graphics.newFont(scale(20)) + love.graphics.setFont(font) -- editor objects implicitly depend on current font -- translate Page and Page2 to Surface Surface = {} local red = false @@ -9,9 +10,9 @@ A = function() red = not red end end - compute_layout(Page, Page.x,Page.y, Surface) - compute_layout(Page2, Page2.x,Page2.y, Surface) + compute_layout(Page, Page.x,Page.y, Surface, font) + compute_layout(Page2, Page2.x,Page2.y, Surface, font) -- continue the pipeline - B() + B(font) -- TODO: ugly that we're manipulating editor objects twice -end +end \ No newline at end of file