From 2ade33ce98cc9bb3410732095a6c207febcc67e5 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 7 Feb 2024 16:15:46 +0300 Subject: [PATCH 1/5] refactor(utilities): Move debug rounder into utilities --- core/utilities/init.lua | 14 ++++++++++++++ outputters/debug.lua | 14 +------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/core/utilities/init.lua b/core/utilities/init.lua index 0d31bb30f..35cbd8ca1 100644 --- a/core/utilities/init.lua +++ b/core/utilities/init.lua @@ -259,6 +259,20 @@ utilities.min = function (...) return min end +-- LuaJIT 2.1 betas (and inheritors such as OpenResty and Moonjit) are biased +-- towards rounding 0.5 up to 1, all other Lua interpreters are biased +-- towards rounding such floating point numbers down. This hack shaves off +-- just enough to fix the bias so our test suite works across interpreters. +-- Note that even a true rounding function here will fail because the bias is +-- inherent to the floating point type. Also note we are erroring in favor of +-- the *less* common option because the LuaJIT VMS are hopelessly broken +-- whereas normal LUA VMs can be cooerced. +utilities.debug_round = function (input) + if input > 0 then input = input + .00000000000001 end + if input < 0 then input = input - .00000000000001 end + return string.format("%.4f", input) +end + utilities.compress = function (items) local rv = {} local max = math.max(pl.utils.unpack(pl.tablex.keys(items))) diff --git a/outputters/debug.lua b/outputters/debug.lua index fa08e7152..6fc22b629 100644 --- a/outputters/debug.lua +++ b/outputters/debug.lua @@ -8,19 +8,7 @@ local started = false local lastFont local outfile -local function _round (input) - -- LuaJIT 2.1 betas (and inheritors such as OpenResty and Moonjit) are biased - -- towards rounding 0.5 up to 1, all other Lua interpreters are biased - -- towards rounding such floating point numbers down. This hack shaves off - -- just enough to fix the bias so our test suite works across interpreters. - -- Note that even a true rounding function here will fail because the bias is - -- inherent to the floating point type. Also note we are erroring in favor of - -- the *less* common option because the LuaJIT VMS are hopelessly broken - -- whereas normal LUA VMs can be cooerced. - if input > 0 then input = input + .00000000000001 end - if input < 0 then input = input - .00000000000001 end - return string.format("%.4f", input) -end +local _round = SU.debug_round local outputter = pl.class(base) outputter._name = "debug" From e1106a7ab92cc4e9ec34a532b4556e417a386881 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 7 Feb 2024 15:59:42 +0300 Subject: [PATCH 2/5] feat(types): Add __tostring to colors for easier debugging --- types/color.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/types/color.lua b/types/color.lua index 1e28c0f65..be30094be 100644 --- a/types/color.lua +++ b/types/color.lua @@ -170,6 +170,16 @@ function color:_init (input) return self end +function color:__tostring() + local p = {} + for _, k in pairs({"r", "g", "b", "c", "m", "y", "k", "l"}) do + if self[k] then + table.insert(p, k .. SU.debug_round(self[k])) + end + end + return ("C<%s>"):format(pl.pretty.write(p, ""):gsub('[{}"]', "")) +end + function color.parse (_, input) local r, g, b, c, m, y, k, l if not input or type(input) ~= "string" then From 14320ec6fbee939ef523a403b0c3d30137f30391 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 7 Feb 2024 16:06:53 +0300 Subject: [PATCH 3/5] test(types): Use internal debug output for colors --- outputters/debug.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/outputters/debug.lua b/outputters/debug.lua index 6fc22b629..dc9df6b59 100644 --- a/outputters/debug.lua +++ b/outputters/debug.lua @@ -69,21 +69,21 @@ end function outputter:setColor (color) if color.r then - self:_writeline("Set color", _round(color.r), _round(color.g), _round(color.b)) + self:_writeline("Set color", tostring(color)) elseif color.c then - self:_writeline("Set color", _round(color.c), _round(color.m), _round(color.y), _round(color.k)) + self:_writeline("Set color", tostring(color)) elseif color.l then - self:_writeline("Set color", _round(color.l)) + self:_writeline("Set color", tostring(color)) end end function outputter:pushColor (color) if color.r then - self:_writeline("Push color", _round(color.r), _round(color.g), _round(color.b)) + self:_writeline("Push color", tostring(color)) elseif color.c then - self:_writeline("Push color (CMYK)", _round(color.c), _round(color.m), _round(color.y), _round(color.k)) + self:_writeline("Push color (CMYK)", tostring(color)) elseif color.l then - self:_writeline("Push color (grayscale)", _round(color.l)) + self:_writeline("Push color (grayscale)", tostring(color)) end end From e7fb6eb3ab0da700c4712d958c3b51b0c9796f5a Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 7 Feb 2024 16:47:43 +0300 Subject: [PATCH 4/5] chore(types): Push actual color types, not just color values --- outputters/libtexpdf.lua | 4 ++-- packages/color-fonts/init.lua | 3 ++- packages/hanmenkyoshi/init.lua | 3 ++- tests/bug-liner-width.sil | 2 +- types/color.lua | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/outputters/libtexpdf.lua b/outputters/libtexpdf.lua index d7932cd94..a752cacb2 100644 --- a/outputters/libtexpdf.lua +++ b/outputters/libtexpdf.lua @@ -215,7 +215,7 @@ end function outputter:debugFrame (frame) self:_ensureInit() - self:pushColor({ r = 0.8, g = 0, b = 0 }) + self:pushColor(SILE.types.color({ r = 0.8, g = 0, b = 0 })) self:drawRule(frame:left()-_dl/2, frame:top()-_dl/2, frame:width()+_dl, _dl) self:drawRule(frame:left()-_dl/2, frame:top()-_dl/2, _dl, frame:height()+_dl) self:drawRule(frame:right()-_dl/2, frame:top()-_dl/2, _dl, frame:height()+_dl) @@ -237,7 +237,7 @@ end function outputter:debugHbox (hbox, scaledWidth) self:_ensureInit() - self:pushColor({ r = 0.8, g = 0.3, b = 0.3 }) + self:pushColor(SILE.types.color({ r = 0.8, g = 0.3, b = 0.3 })) local paperY = SILE.documentState.paperSize[2] local x, y = self:getCursor() y = paperY - y diff --git a/packages/color-fonts/init.lua b/packages/color-fonts/init.lua index d0ae4f63e..3484462e0 100644 --- a/packages/color-fonts/init.lua +++ b/packages/color-fonts/init.lua @@ -79,8 +79,9 @@ function package:_init () for i=1, #run do options = pl.tablex.deepcopy(options) if run[i].color then + local color = SILE.types.color(run[i].color) nodes[#nodes+1] = SILE.types.node.hbox({ - outputYourself = function () SILE.outputter:pushColor(run[i].color) end + outputYourself = function () SILE.outputter:pushColor(color) end }) end for node in nodeMaker(options):iterator(run[i].slice, run[i].chunk) do diff --git a/packages/hanmenkyoshi/init.lua b/packages/hanmenkyoshi/init.lua index 8feaa2dac..648fefa59 100644 --- a/packages/hanmenkyoshi/init.lua +++ b/packages/hanmenkyoshi/init.lua @@ -79,7 +79,8 @@ function package:registerCommands () if not frame.hanmen then SU.error("show-hanmen called on a frame with no hanmen") end - SILE.outputter:pushColor({r = 1, g= 0.9, b = 0.9 }) + local color = SILE.types.color({r = 1, g= 0.9, b = 0.9 }) + SILE.outputter:pushColor(color) if frame:writingDirection() == "TTB" then showHanmenTate(frame) else diff --git a/tests/bug-liner-width.sil b/tests/bug-liner-width.sil index 2dc8cb458..618d2ab3d 100644 --- a/tests/bug-liner-width.sil +++ b/tests/bug-liner-width.sil @@ -13,7 +13,7 @@ SILE.documentState.documentClass:registerCommand("advance-box-width", function ( local height = SU.max(box.height:tonumber(), (1 - bsratio) * bs) local depth = SU.max(box.depth:tonumber(), bsratio * bs) local cx, cy = typesetter.frame.state.cursorX, typesetter.frame.state.cursorY - SILE.outputter:pushColor(SILE.color("gray")) + SILE.outputter:pushColor(SILE.types.color("gray")) SILE.outputter:drawRule(cx, cy - height, outputWidth, height + depth) SILE.outputter:popColor() typesetter.frame:advanceWritingDirection(outputWidth) diff --git a/types/color.lua b/types/color.lua index be30094be..c6b112dba 100644 --- a/types/color.lua +++ b/types/color.lua @@ -163,7 +163,7 @@ local color = pl.class({ }) function color:_init (input) - local c = self:parse(input) + local c = type(input) == "string" and self:parse(input) or input for k, v in pairs(c) do self[k] = v end From 1ca2d574e3cd8673c61986958c1890d022f16762 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Wed, 7 Feb 2024 16:23:45 +0300 Subject: [PATCH 5/5] test(types): Update expected outputs --- tests/bug-1142.expected | 6 +++--- tests/bug-1171.expected | 14 +++++++------- tests/bug-1244-fallback-after-color.expected | 12 ++++++------ tests/bug-353.expected | 4 ++-- tests/bug-524.expected | 2 +- tests/bug-865.expected | 4 ++-- tests/bug-liner-width.expected | 6 +++--- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/bug-1142.expected b/tests/bug-1142.expected index c276b649c..f37cbc8e3 100644 --- a/tests/bug-1142.expected +++ b/tests/bug-1142.expected @@ -4,14 +4,14 @@ Mx 14.8819 My 28.9764 Set font ;10;400;;normal;;;LTR;tests/TestCLR-Regular.ttf T 3 w=10.0000 (a) -Push color 1.0000 0.8784 0.5412 +Push color C Mx 24.8819 T 7 a=10.0000 () Pop color -Push color 0.8980 0.6078 0.1451 +Push color C T 8 a=10.0000 () Pop color -Push color 0.5176 0.2196 0.0510 +Push color C T 9 w=10.0000 (☺) Pop color Mx 34.8819 diff --git a/tests/bug-1171.expected b/tests/bug-1171.expected index 7100a6dbf..0a30d6b1f 100644 --- a/tests/bug-1171.expected +++ b/tests/bug-1171.expected @@ -1,29 +1,29 @@ Set paper size 595.275597 841.8897729 Begin page -Push color 1.0000 0.8000 0.3020 +Push color C Mx 542.4079 My 97.6592 Set font Twemoji Mozilla;10;400;;normal;;;TTB T 7876 a=10.0000 () Pop color -Push color 0.4000 0.2706 0.0000 +Push color C T 7893 a=10.0000 () T 7894 w=10.0000 (😉) Pop color -Push color 1.0000 0.7961 0.2980 +Push color C My 107.6592 T 9105 a=10.0000 () Pop color -Push color 0.3961 0.2784 0.1059 +Push color C T 9106 a=10.0000 () Pop color -Push color 1.0000 1.0000 1.0000 +Push color C T 9107 a=10.0000 () Pop color -Push color 0.3961 0.2784 0.1059 +Push color C T 9108 a=10.0000 () Pop color -Push color 0.3922 0.6667 0.8667 +Push color C T 9109 w=10.0000 (🤣) Pop color End page diff --git a/tests/bug-1244-fallback-after-color.expected b/tests/bug-1244-fallback-after-color.expected index 802612b9e..08180bdf5 100644 --- a/tests/bug-1244-fallback-after-color.expected +++ b/tests/bug-1244-fallback-after-color.expected @@ -7,16 +7,16 @@ T 1542 w=10.0000 (ん) Mx 24.8819 Set font Gentium Plus;10;400;;normal;;;LTR T 68 w=4.5898 (a) -Push color 1.0000 0.8784 0.5412 +Push color C Mx 14.8819 My 40.6664 Set font ;10;400;;normal;;;LTR;tests/TestCLR-Regular.ttf T 7 a=10.0000 () Pop color -Push color 0.8980 0.6078 0.1451 +Push color C T 8 a=10.0000 () Pop color -Push color 0.5176 0.2196 0.0510 +Push color C T 9 w=10.0000 (☺) Pop color Mx 24.8819 @@ -28,16 +28,16 @@ T 1542 w=10.0000 (ん) Mx 24.8819 Set font Gentium Plus;10;400;;normal;;;LTR T 68 w=4.5898 (a) -Push color 1.0000 0.8784 0.5412 +Push color C Mx 14.8819 My 64.6664 Set font ;10;400;;normal;;;LTR;tests/TestCLR-Regular.ttf T 7 a=10.0000 () Pop color -Push color 0.8980 0.6078 0.1451 +Push color C T 8 a=10.0000 () Pop color -Push color 0.5176 0.2196 0.0510 +Push color C T 9 w=10.0000 (☺) Pop color Mx 24.8819 diff --git a/tests/bug-353.expected b/tests/bug-353.expected index 47f50f2ca..9041381f9 100644 --- a/tests/bug-353.expected +++ b/tests/bug-353.expected @@ -1,9 +1,9 @@ Set paper size 419.5275636 595.275597 Begin page -Push color 0.9137 0.8471 0.7294 +Push color C Draw line 0.0000 0.0000 419.5276 595.2756 Pop color -Push color 0.3529 0.2549 0.1608 +Push color C Mx 40.9764 My 37.3321 Set font Gentium Plus;10;400;;normal;;;LTR diff --git a/tests/bug-524.expected b/tests/bug-524.expected index 81d11b92f..b329db328 100644 --- a/tests/bug-524.expected +++ b/tests/bug-524.expected @@ -1,6 +1,6 @@ Set paper size 595.275597 841.8897729 Begin page -Push color 1.0000 0.9000 0.9000 +Push color C Draw line 49.4079 97.4092 500.0000 0.5000 Draw line 49.1579 107.4092 0.5000 -10.0000 Draw line 59.1579 107.4092 0.5000 -10.0000 diff --git a/tests/bug-865.expected b/tests/bug-865.expected index 016f0de20..ec70b85ed 100644 --- a/tests/bug-865.expected +++ b/tests/bug-865.expected @@ -27,7 +27,7 @@ Mx 38.0763 T 81 82 81 88 80 92 w=34.3262 (nonumy) Mx 75.0647 T 72 76 85 80 82 71 w=29.5508 (eirmod) -Push color 0.6000 0.6000 0.6000 +Push color C Mx 14.8819 My 69.7847 Set font Libertinus Serif;30;400;;normal;;;LTR @@ -112,7 +112,7 @@ Mx 94.4649 T 81 82 81 88 80 92 w=34.3262 (nonumy) Mx 131.4336 T 72 76 85 80 82 71 w=29.5508 (eirmod) -Push color 0.6000 0.6000 0.6000 +Push color C Mx 271.5059 My 125.4747 Set font Libertinus Serif;30;400;;normal;;;LTR diff --git a/tests/bug-liner-width.expected b/tests/bug-liner-width.expected index 9df478fab..6d21f07f1 100644 --- a/tests/bug-liner-width.expected +++ b/tests/bug-liner-width.expected @@ -1,12 +1,12 @@ Set paper size 209.7637818 297.6377985 Begin page -Push color 0.5020 0.5020 0.5020 +Push color C Draw line 10.4882 14.8819 188.7874 34.5868 Pop color -Push color 0.5020 0.5020 0.5020 +Push color C Draw line 10.4882 53.2819 188.7874 34.5868 Pop color -Push color 0.5020 0.5020 0.5020 +Push color C Draw line 10.4882 91.6819 116.2969 34.5868 Pop color Mx 10.4882