Skip to content

Commit b17b2f5

Browse files
author
Olivier Bonnaure
committed
feat: improve text width calculation
1 parent 5912ea6 commit b17b2f5

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

.lua/pdfgenerator.lua

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,12 @@ function PDFGenerator:setFont(fontName)
364364
end
365365

366366
-- Get text width for current font and size using font metrics
367+
local wordCache = setmetatable({}, { __mode = "kv" })
367368
function PDFGenerator:getTextWidth(text, fontSize, fontWeight)
369+
local key = table.concat({text, fontSize, fontWeight})
370+
if wordCache[key] then
371+
return wordCache[key]
372+
end
368373
fontSize = fontSize or 12
369374
fontWeight = fontWeight or "normal"
370375

@@ -381,7 +386,8 @@ function PDFGenerator:getTextWidth(text, fontSize, fontWeight)
381386
end
382387

383388
-- Convert from font units (1/1000) to points
384-
return (width * fontSize) / 1000
389+
wordCache[key] = (width * fontSize) / 1000
390+
return wordCache[key]
385391
end
386392

387393
-- Split text into lines based on page width
@@ -623,34 +629,36 @@ end
623629
function PDFGenerator:calculateMaxHeight(columns)
624630
local max_height = 0
625631

626-
for _, column in ipairs(columns) do
627-
if column.content == nil then
628-
local text_height = self:calculateTextHeight(column)
629-
-- Update max_height if this item is taller
630-
if text_height > max_height then
631-
max_height = text_height
632-
end
632+
local function getContentHeight(content)
633+
if content.type == "text" then
634+
-- preserve the original /1.5 behavior
635+
return self:calculateTextHeight(content) / 1.5
636+
elseif content.type == "image" then
637+
local resource = self.resources.images[content.value]
638+
if not resource then return 0 end
639+
return (resource.height * content.width / resource.width) + 10
633640
end
641+
return 0
642+
end
634643

635-
if column.content ~= nil then
636-
local text_height = 0
637-
for _, content in ipairs(column.content) do
638-
if content.type == "text" then
639-
text_height = text_height + self:calculateTextHeight(content) / 1.5
640-
end
644+
for _, column in ipairs(columns) do
645+
local text_height = 0
641646

642-
if content.type == "image" then
643-
local resource = self.resources.images[content.value]
644-
text_height = text_height + (resource.height * content.width / resource.width) + 10
645-
end
647+
if column.content then
648+
for _, content in ipairs(column.content) do
649+
text_height = text_height + getContentHeight(content)
646650
end
651+
else
652+
-- keep the plain case unchanged (no /1.5 here)
653+
text_height = self:calculateTextHeight(column)
654+
end
647655

648-
if text_height > max_height then max_height = text_height end
656+
if text_height > max_height then
657+
max_height = text_height
649658
end
650659
end
651660

652661
self.current_table.current_row.height = max_height
653-
654662
return max_height
655663
end
656664

0 commit comments

Comments
 (0)