Skip to content

Commit f6a3dbb

Browse files
author
Olivier Bonnaure
committed
fix: less warnings in code
1 parent d18c3d1 commit f6a3dbb

File tree

2 files changed

+71
-58
lines changed

2 files changed

+71
-58
lines changed

.init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ ProgramMaxPayloadSize(10485760) -- 10 MB
2020
Views = {}
2121
IsApi = false -- set it to true to return json for 404 errors
2222

23+
-- Preload Font assets for PDF generation
24+
Resources = {
25+
["fonts/TitilliumWeb-Regular.ttf"] = LoadAsset("fonts/TitilliumWeb-Regular.ttf"),
26+
["fonts/TitilliumWeb-Regular.json"] = LoadAsset("fonts/TitilliumWeb-Regular.json"),
27+
["fonts/TitilliumWeb-Bold.ttf"] = LoadAsset("fonts/TitilliumWeb-Bold.ttf"),
28+
["fonts/TitilliumWeb-Bold.json"] = LoadAsset("fonts/TitilliumWeb-Bold.json")
29+
}
2330
-- LastModifiedAt is used to cache the last modified time of the assets
2431
-- so that we can use it to send the correct last modified time to the client
2532
-- and avoid sending the whole file to the client

.lua/pdfgenerator.lua

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
-- PDF Generator Library for PDF 1.7
22
local PDFGenerator = {}
33

4+
-- Used for pre-fetching data from .init.lua (not sure it really help)
5+
Resources = Resources or {}
6+
47
-- PDF object counter
58
local objCounter = 1
69

710
local function loadAsset(path)
11+
if Resources[path] then return Resources[path] end
812
return LoadAsset(path) -- TODO: update for another framework (e.g. Lapis / openresty)
913
end
1014

@@ -181,7 +185,7 @@ function PDFGenerator:addCustomFont(fontPath, fontName, fontWeight)
181185

182186
table.insert(self.fonts, { fontName, fontWeight })
183187

184-
fullFontName = fontName .. "-" .. fontWeight
188+
local fullFontName = fontName .. "-" .. fontWeight
185189

186190
-- Read font file
187191
local fontData = loadAsset(fontPath)
@@ -406,7 +410,7 @@ function PDFGenerator:splitTextToLines(text, fontSize, maxWidth)
406410
local currentLine = ""
407411
local currentWidth = 0
408412

409-
for i, word in ipairs(words) do
413+
for _, word in ipairs(words) do
410414
local wordWidth = self:getTextWidth(word, fontSize, self.last_font.fontWeight) or 0
411415
local spaceWidth = self:getTextWidth(" ", fontSize, self.last_font.fontWeight)
412416

@@ -527,8 +531,8 @@ function PDFGenerator:addParagraph(text, options)
527531

528532
local splittedText = string.split(text, "\n")
529533

530-
for _, text in ipairs(splittedText) do
531-
local lines = self:splitTextToLines(text, options.fontSize, options.width - options.paddingX)
534+
for _, textline in ipairs(splittedText) do
535+
local lines = self:splitTextToLines(textline, options.fontSize, options.width - options.paddingX)
532536
for _, line in ipairs(lines) do
533537
if options.newLine == true then
534538
self.current_y = self.current_y + options.fontSize * 1.2
@@ -1125,7 +1129,7 @@ end
11251129
function PDFGenerator:drawStar(outerRadius, branches, borderWidth, borderStyle, borderColor, fillColor)
11261130
borderWidth = borderWidth or 1
11271131
branches = branches or 5
1128-
innerRadius = outerRadius * 0.382 -- Golden ratio for default inner radius
1132+
local innerRadius = outerRadius * 0.382 -- Golden ratio for default inner radius
11291133
borderStyle = borderStyle or "solid"
11301134
borderColor = borderColor or "000000" -- default black
11311135
borderColor = PDFGenerator:hexToRGB(borderColor)
@@ -1253,6 +1257,8 @@ function PDFGenerator:addImage(imgData, format)
12531257

12541258
local width, height, err = get_jpeg_dimensions(imgData)
12551259

1260+
assert(err == nil, err)
1261+
12561262
-- Store image information
12571263
self.resources.images[imgName] = {
12581264
obj = imageObj,
@@ -1369,7 +1375,7 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
13691375
end
13701376
local hexToRGB = self.hexToRGB and function(h)
13711377
return self:hexToRGB(h)
1372-
end or function(h)
1378+
end or function(_)
13731379
return { 0, 0, 0 }
13741380
end
13751381
local strokeRGB = hexToRGB(options.strokeColor)
@@ -1477,81 +1483,81 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
14771483
end
14781484
elseif cmd == "C" then
14791485
while i <= #nums do
1480-
local x1, y1, x2, y2, x, y =
1486+
local x1, y1, x2, y2, xx, yy =
14811487
nums[i], nums[i + 1], nums[i + 2], nums[i + 3], nums[i + 4], nums[i + 5]
14821488
i = i + 6
1483-
updateBounds(x1, y1, x2, y2, x, y)
1489+
updateBounds(x1, y1, x2, y2, xx, yy)
14841490
cpx, cpy = x2, y2
1485-
cx, cy = x, y
1491+
cx, cy = xx, yy
14861492
lastCmd = "C"
14871493
end
14881494
elseif cmd == "c" then
14891495
while i <= #nums do
14901496
local x1, y1 = cx + nums[i], cy + nums[i + 1]
14911497
local x2, y2 = cx + nums[i + 2], cy + nums[i + 3]
1492-
local x, y = cx + nums[i + 4], cy + nums[i + 5]
1498+
local xx, yy = cx + nums[i + 4], cy + nums[i + 5]
14931499
i = i + 6
1494-
updateBounds(x1, y1, x2, y2, x, y)
1500+
updateBounds(x1, y1, x2, y2, xx, yy)
14951501
cpx, cpy = x2, y2
1496-
cx, cy = x, y
1502+
cx, cy = xx, yy
14971503
lastCmd = "c"
14981504
end
14991505
elseif cmd == "S" then
15001506
while i <= #nums do
1501-
local x2, y2, x, y = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
1507+
local x2, y2, xx, yy = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
15021508
i = i + 4
15031509
local x1, y1 = lastWasCubic() and (2 * cx - cpx), (2 * cy - cpy) or cx, cy
1504-
updateBounds(x1, y1, x2, y2, x, y)
1510+
updateBounds(x1, y1, x2, y2, xx, yy)
15051511
cpx, cpy = x2, y2
1506-
cx, cy = x, y
1512+
cx, cy = xx, yy
15071513
lastCmd = "S"
15081514
end
15091515
elseif cmd == "s" then
15101516
while i <= #nums do
1511-
local x2, y2, x, y = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
1517+
local x2, y2, xx, yy = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
15121518
i = i + 4
15131519
local x1, y1 = lastWasCubic() and (2 * cx - cpx), (2 * cy - cpy) or cx, cy
15141520
updateBounds(x1, y1, x2, y2, x, y)
15151521
cpx, cpy = x2, y2
1516-
cx, cy = x, y
1522+
cx, cy = xx, yy
15171523
lastCmd = "s"
15181524
end
15191525
elseif cmd == "Q" then
15201526
while i <= #nums do
1521-
local x1, y1, x, y = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
1527+
local x1, y1, xx, yy = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
15221528
i = i + 4
1523-
updateBounds(x1, y1, x, y)
1529+
updateBounds(x1, y1, xx, yy)
15241530
qx, qy = x1, y1
1525-
cx, cy = x, y
1531+
cx, cy = xx, yy
15261532
lastCmd = "Q"
15271533
end
15281534
elseif cmd == "q" then
15291535
while i <= #nums do
1530-
local x1, y1, x, y = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
1536+
local x1, y1, xx, yy = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
15311537
i = i + 4
1532-
updateBounds(x1, y1, x, y)
1538+
updateBounds(x1, y1, xx, yy)
15331539
qx, qy = x1, y1
1534-
cx, cy = x, y
1540+
cx, cy = xx, yy
15351541
lastCmd = "q"
15361542
end
15371543
elseif cmd == "T" then
15381544
while i <= #nums do
1539-
local x, y = nums[i], nums[i + 1]
1545+
local xx, yy = nums[i], nums[i + 1]
15401546
i = i + 2
15411547
local x1, y1 = lastWasQuad() and qx and (2 * cx - qx), (2 * cy - qy) or cx, cy
1542-
updateBounds(x1, y1, x, y)
1548+
updateBounds(x1, y1, xx, yy)
15431549
qx, qy = x1, y1
1544-
cx, cy = x, y
1550+
cx, cy = xx, yy
15451551
lastCmd = "T"
15461552
end
15471553
elseif cmd == "t" then
15481554
while i <= #nums do
1549-
local x, y = cx + nums[i], cy + nums[i + 1]
1555+
local xx, yy = cx + nums[i], cy + nums[i + 1]
15501556
i = i + 2
15511557
local x1, y1 = lastWasQuad() and qx and (2 * cx - qx), (2 * cy - qy) or cx, cy
1552-
updateBounds(x1, y1, x, y)
1558+
updateBounds(x1, y1, xx, yy)
15531559
qx, qy = x1, y1
1554-
cx, cy = x, y
1560+
cx, cy = xx, yy
15551561
lastCmd = "t"
15561562
end
15571563
elseif cmd == "Z" or cmd == "z" then
@@ -1785,39 +1791,39 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
17851791
end
17861792
elseif cmd == "C" then
17871793
while i <= #nums do
1788-
local x1, y1, x2, y2, x, y = nums[i], nums[i + 1], nums[i + 2], nums[i + 3], nums[i + 4], nums[i + 5]
1794+
local x1, y1, x2, y2, xx, yy = nums[i], nums[i + 1], nums[i + 2], nums[i + 3], nums[i + 4], nums[i + 5]
17891795
i = i + 6
17901796
local tx1, ty1 = transform(x1, y1)
17911797
local tx2, ty2 = transform(x2, y2)
1792-
local tx, ty = transform(x, y)
1798+
local tx, ty = transform(xx, yy)
17931799
table.insert(
17941800
content.streams,
17951801
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
17961802
)
17971803
cpx, cpy = x2, y2
1798-
cx, cy = x, y
1804+
cx, cy = xx, yy
17991805
lastCmd = "C"
18001806
end
18011807
elseif cmd == "c" then
18021808
while i <= #nums do
18031809
local x1, y1 = cx + nums[i], cy + nums[i + 1]
18041810
local x2, y2 = cx + nums[i + 2], cy + nums[i + 3]
1805-
local x, y = cx + nums[i + 4], cy + nums[i + 5]
1811+
local xx, yy = cx + nums[i + 4], cy + nums[i + 5]
18061812
i = i + 6
18071813
local tx1, ty1 = transform(x1, y1)
18081814
local tx2, ty2 = transform(x2, y2)
1809-
local tx, ty = transform(x, y)
1815+
local tx, ty = transform(xx, yy)
18101816
table.insert(
18111817
content.streams,
18121818
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
18131819
)
18141820
cpx, cpy = x2, y2
1815-
cx, cy = x, y
1821+
cx, cy = xx, yy
18161822
lastCmd = "c"
18171823
end
18181824
elseif cmd == "S" then
18191825
while i <= #nums do
1820-
local x2, y2, x, y = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
1826+
local x2, y2, xx, yy = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
18211827
i = i + 4
18221828
local x1, y1
18231829
if lastWasCubic() then
@@ -1827,18 +1833,18 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
18271833
end
18281834
local tx1, ty1 = transform(x1, y1)
18291835
local tx2, ty2 = transform(x2, y2)
1830-
local tx, ty = transform(x, y)
1836+
local tx, ty = transform(xx, yy)
18311837
table.insert(
18321838
content.streams,
18331839
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
18341840
)
18351841
cpx, cpy = x2, y2
1836-
cx, cy = x, y
1842+
cx, cy = xx, yy
18371843
lastCmd = "S"
18381844
end
18391845
elseif cmd == "s" then
18401846
while i <= #nums do
1841-
local x2, y2, x, y = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
1847+
local x2, y2, xx, yy = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
18421848
i = i + 4
18431849
local x1, y1
18441850
if lastWasCubic() then
@@ -1848,18 +1854,18 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
18481854
end
18491855
local tx1, ty1 = transform(x1, y1)
18501856
local tx2, ty2 = transform(x2, y2)
1851-
local tx, ty = transform(x, y)
1857+
local tx, ty = transform(xx, yy)
18521858
table.insert(
18531859
content.streams,
18541860
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
18551861
)
18561862
cpx, cpy = x2, y2
1857-
cx, cy = x, y
1863+
cx, cy = xx, yy
18581864
lastCmd = "s"
18591865
end
18601866
elseif cmd == "Q" then
18611867
while i <= #nums do
1862-
local x1, y1, x, y = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
1868+
local x1, y1, xx, yy = nums[i], nums[i + 1], nums[i + 2], nums[i + 3]
18631869
i = i + 4
18641870
-- convert quadratic to cubic:
18651871
local c1x = cx + (2 / 3) * (x1 - cx)
@@ -1868,37 +1874,37 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
18681874
local c2y = y + (2 / 3) * (y1 - y)
18691875
local tx1, ty1 = transform(c1x, c1y)
18701876
local tx2, ty2 = transform(c2x, c2y)
1871-
local tx, ty = transform(x, y)
1877+
local tx, ty = transform(xx, yy)
18721878
table.insert(
18731879
content.streams,
18741880
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
18751881
)
18761882
qx, qy = x1, y1
1877-
cx, cy = x, y
1883+
cx, cy = xx, yy
18781884
lastCmd = "Q"
18791885
end
18801886
elseif cmd == "q" then
18811887
while i <= #nums do
1882-
local x1, y1, x, y = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
1888+
local x1, y1, xx, yy = cx + nums[i], cy + nums[i + 1], cx + nums[i + 2], cy + nums[i + 3]
18831889
i = i + 4
18841890
local c1x = cx + (2 / 3) * (x1 - cx)
18851891
local c1y = cy + (2 / 3) * (y1 - cy)
18861892
local c2x = x + (2 / 3) * (x1 - x)
18871893
local c2y = y + (2 / 3) * (y1 - y)
18881894
local tx1, ty1 = transform(c1x, c1y)
18891895
local tx2, ty2 = transform(c2x, c2y)
1890-
local tx, ty = transform(x, y)
1896+
local tx, ty = transform(xx, yy)
18911897
table.insert(
18921898
content.streams,
18931899
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
18941900
)
18951901
qx, qy = x1, y1
1896-
cx, cy = x, y
1902+
cx, cy = xx, yy
18971903
lastCmd = "q"
18981904
end
18991905
elseif cmd == "T" then
19001906
while i <= #nums do
1901-
local x, y = nums[i], nums[i + 1]
1907+
local xx, yy = nums[i], nums[i + 1]
19021908
i = i + 2
19031909
local x1, y1
19041910
if lastWasQuad() and qx then
@@ -1912,18 +1918,18 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
19121918
local c2y = y + (2 / 3) * (y1 - y)
19131919
local tx1, ty1 = transform(c1x, c1y)
19141920
local tx2, ty2 = transform(c2x, c2y)
1915-
local tx, ty = transform(x, y)
1921+
local tx, ty = transform(xx, yy)
19161922
table.insert(
19171923
content.streams,
19181924
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
19191925
)
19201926
qx, qy = x1, y1
1921-
cx, cy = x, y
1927+
cx, cy = xx, yy
19221928
lastCmd = "T"
19231929
end
19241930
elseif cmd == "t" then
19251931
while i <= #nums do
1926-
local x, y = cx + nums[i], cy + nums[i + 1]
1932+
local xx, yy = cx + nums[i], cy + nums[i + 1]
19271933
i = i + 2
19281934
local x1, y1
19291935
if lastWasQuad() and qx then
@@ -1937,13 +1943,13 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
19371943
local c2y = y + (2 / 3) * (y1 - y)
19381944
local tx1, ty1 = transform(c1x, c1y)
19391945
local tx2, ty2 = transform(c2x, c2y)
1940-
local tx, ty = transform(x, y)
1946+
local tx, ty = transform(xx, yy)
19411947
table.insert(
19421948
content.streams,
19431949
string.format("%s %s %s %s %s %s c\n", nts(tx1), nts(ty1), nts(tx2), nts(ty2), nts(tx), nts(ty))
19441950
)
19451951
qx, qy = x1, y1
1946-
cx, cy = x, y
1952+
cx, cy = xx, yy
19471953
lastCmd = "t"
19481954
end
19491955
elseif cmd == "A" or cmd == "a" then
@@ -1952,15 +1958,15 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
19521958
local xAxisRot = nums[i + 2]
19531959
local largeArcFlag = nums[i + 3]
19541960
local sweepFlag = nums[i + 4]
1955-
local x, y
1961+
local xx, yy
19561962

19571963
if cmd == "a" then
1958-
x, y = cx + nums[i + 5], cy + nums[i + 6]
1964+
xx, yy = cx + nums[i + 5], cy + nums[i + 6]
19591965
else
1960-
x, y = nums[i + 5], nums[i + 6]
1966+
xx, yy = nums[i + 5], nums[i + 6]
19611967
end
19621968

1963-
local beziers = arcToBeziers(cx, cy, rx, ry, xAxisRot, largeArcFlag ~= 0, sweepFlag ~= 0, x, y)
1969+
local beziers = arcToBeziers(cx, cy, rx, ry, xAxisRot, largeArcFlag ~= 0, sweepFlag ~= 0, xx, yy)
19641970

19651971
for _, b in ipairs(beziers) do
19661972
local x1, y1, x2, y2, x3, y3 = table.unpack(b)
@@ -1970,7 +1976,7 @@ function PDFGenerator:drawSvgPath(width, height, pathData, options)
19701976
)
19711977
end
19721978

1973-
cx, cy = x, y
1979+
cx, cy = xx, yy
19741980
i = i + 7
19751981
end
19761982
elseif cmd == "Z" or cmd == "z" then

0 commit comments

Comments
 (0)