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.
This commit is contained in:
Kartik K. Agaram 2024-07-25 21:52:29 -07:00
parent 4a92fb5818
commit 91b9a02b28
3 changed files with 14 additions and 14 deletions

7
0019-B
View File

@ -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

View File

@ -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

11
0028-A
View File

@ -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