Skip to content

Commit

Permalink
Merge 1ca2d57 into 076fa1a
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Feb 7, 2024
2 parents 076fa1a + 1ca2d57 commit d0598bb
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 49 deletions.
14 changes: 14 additions & 0 deletions core/utilities/init.lua
Expand Up @@ -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)))
Expand Down
26 changes: 7 additions & 19 deletions outputters/debug.lua
Expand Up @@ -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"
Expand Down Expand Up @@ -81,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

Expand Down
4 changes: 2 additions & 2 deletions outputters/libtexpdf.lua
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/color-fonts/init.lua
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/hanmenkyoshi/init.lua
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/bug-1142.expected
Expand Up @@ -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<r1.0000,g0.8784,b0.5412>
Mx 24.8819
T 7 a=10.0000 ()
Pop color
Push color 0.8980 0.6078 0.1451
Push color C<r0.8980,g0.6078,b0.1451>
T 8 a=10.0000 ()
Pop color
Push color 0.5176 0.2196 0.0510
Push color C<r0.5176,g0.2196,b0.0510>
T 9 w=10.0000 (☺)
Pop color
Mx 34.8819
Expand Down
14 changes: 7 additions & 7 deletions 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<r1.0000,g0.8000,b0.3020>
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<r0.4000,g0.2706,b0.0000>
T 7893 a=10.0000 ()
T 7894 w=10.0000 (😉)
Pop color
Push color 1.0000 0.7961 0.2980
Push color C<r1.0000,g0.7961,b0.2980>
My 107.6592
T 9105 a=10.0000 ()
Pop color
Push color 0.3961 0.2784 0.1059
Push color C<r0.3961,g0.2784,b0.1059>
T 9106 a=10.0000 ()
Pop color
Push color 1.0000 1.0000 1.0000
Push color C<r1.0000,g1.0000,b1.0000>
T 9107 a=10.0000 ()
Pop color
Push color 0.3961 0.2784 0.1059
Push color C<r0.3961,g0.2784,b0.1059>
T 9108 a=10.0000 ()
Pop color
Push color 0.3922 0.6667 0.8667
Push color C<r0.3922,g0.6667,b0.8667>
T 9109 w=10.0000 (🤣)
Pop color
End page
Expand Down
12 changes: 6 additions & 6 deletions tests/bug-1244-fallback-after-color.expected
Expand Up @@ -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<r1.0000,g0.8784,b0.5412>
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<r0.8980,g0.6078,b0.1451>
T 8 a=10.0000 ()
Pop color
Push color 0.5176 0.2196 0.0510
Push color C<r0.5176,g0.2196,b0.0510>
T 9 w=10.0000 (☺)
Pop color
Mx 24.8819
Expand All @@ -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<r1.0000,g0.8784,b0.5412>
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<r0.8980,g0.6078,b0.1451>
T 8 a=10.0000 ()
Pop color
Push color 0.5176 0.2196 0.0510
Push color C<r0.5176,g0.2196,b0.0510>
T 9 w=10.0000 (☺)
Pop color
Mx 24.8819
Expand Down
4 changes: 2 additions & 2 deletions 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<r0.9137,g0.8471,b0.7294>
Draw line 0.0000 0.0000 419.5276 595.2756
Pop color
Push color 0.3529 0.2549 0.1608
Push color C<r0.3529,g0.2549,b0.1608>
Mx 40.9764
My 37.3321
Set font Gentium Plus;10;400;;normal;;;LTR
Expand Down
2 changes: 1 addition & 1 deletion 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<r1.0000,g0.9000,b0.9000>
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
Expand Down
4 changes: 2 additions & 2 deletions tests/bug-865.expected
Expand Up @@ -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<r0.6000,g0.6000,b0.6000>
Mx 14.8819
My 69.7847
Set font Libertinus Serif;30;400;;normal;;;LTR
Expand Down Expand Up @@ -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<r0.6000,g0.6000,b0.6000>
Mx 271.5059
My 125.4747
Set font Libertinus Serif;30;400;;normal;;;LTR
Expand Down
6 changes: 3 additions & 3 deletions 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<r0.5020,g0.5020,b0.5020>
Draw line 10.4882 14.8819 188.7874 34.5868
Pop color
Push color 0.5020 0.5020 0.5020
Push color C<r0.5020,g0.5020,b0.5020>
Draw line 10.4882 53.2819 188.7874 34.5868
Pop color
Push color 0.5020 0.5020 0.5020
Push color C<r0.5020,g0.5020,b0.5020>
Draw line 10.4882 91.6819 116.2969 34.5868
Pop color
Mx 10.4882
Expand Down
2 changes: 1 addition & 1 deletion tests/bug-liner-width.sil
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion types/color.lua
Expand Up @@ -163,13 +163,23 @@ 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
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
Expand Down

0 comments on commit d0598bb

Please sign in to comment.