Skip to content

Commit

Permalink
fix(outputters): Compatibility of modified outputters with SILE 0.15.9
Browse files Browse the repository at this point in the history
Code styling (3-space indentation etc.)
Backport the new abort() method introduced in SILE 0.15.9
  • Loading branch information
Omikhleia authored and Didier Willis committed Jan 9, 2025
1 parent 07b03f4 commit 0d64fe0
Show file tree
Hide file tree
Showing 3 changed files with 437 additions and 360 deletions.
51 changes: 31 additions & 20 deletions silex/outputters/base.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
--- SILE outputter class.
-- @interfaces outputters

local outputter = pl.class()
outputter.type = "outputter"
outputter._name = "base"
Expand All @@ -8,22 +11,30 @@ function outputter:_init ()
end

function outputter:registerHook (category, func)
if not self.hooks[category] then self.hooks[category] = {} end
if not self.hooks[category] then
self.hooks[category] = {}
end
table.insert(self.hooks[category], func)
end

function outputter:runHooks (category, data)
if not self.hooks[category] then return nil end
for _, func in ipairs(self.hooks[category]) do
data = func(self, data)
end
return data
if not self.hooks[category] then
return nil
end
for _, func in ipairs(self.hooks[category]) do
data = func(self, data)
end
return data
end

function outputter.newPage () end

function outputter:abort ()
return self:finish() -- unless otherwise defined
end

function outputter:finish ()
self:runHooks("prefinish")
self:runHooks("prefinish")
end

function outputter.getCursor () end
Expand Down Expand Up @@ -65,19 +76,19 @@ function outputter.setBookmark (_, _, _) end
function outputter.drawRaw (_) end

function outputter:getOutputFilename ()
local fname
if SILE.outputFilename then
fname = SILE.outputFilename
elseif SILE.input.filenames[1] then
fname = pl.path.splitext(SILE.input.filenames[1])
if self.extension then
fname = fname .. "." .. self.extension
end
end
if not fname then
SU.error("Cannot guess output filename without an input name")
end
return fname
local fname
if SILE.outputFilename then
fname = SILE.outputFilename
elseif SILE.input.filenames[1] then
fname = pl.path.splitext(SILE.input.filenames[1])
if self.extension then
fname = fname .. "." .. self.extension
end
end
if not fname then
SU.error("Cannot guess output filename without an input name")
end
return fname
end

return outputter
207 changes: 116 additions & 91 deletions silex/outputters/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,147 +31,172 @@ outputter.extension = "debug"
-- function outputter:_init () end

function outputter:_ensureInit ()
if not started then
started = true -- keep this before self:_writeline or it will be a race condition!
local fname = self:getOutputFilename()
outfile = fname == "-" and io.stdout or io.open(fname, "w+")
if SILE.documentState.paperSize then
self:_writeline("Set paper size ", SILE.documentState.paperSize[1], SILE.documentState.paperSize[2])
end
self:_writeline("Begin page")
end
if not started then
started = true -- keep this before self:_writeline or it will be a race condition!
local fname = self:getOutputFilename()
outfile = fname == "-" and io.stdout or io.open(fname, "w+")
if SILE.documentState.paperSize then
self:_writeline("Set paper size ", SILE.documentState.paperSize[1], SILE.documentState.paperSize[2])
end
self:_writeline("Begin page")
end
end

function outputter:_writeline (...)
self:_ensureInit()
local args = pl.utils.pack(...)
for i = 1, #args do
outfile:write(args[i])
if i < #args then outfile:write("\t") end
end
outfile:write("\n")
self:_ensureInit()
local args = pl.utils.pack(...)
for i = 1, #args do
outfile:write(args[i])
if i < #args then
outfile:write("\t")
end
end
outfile:write("\n")
end

function outputter:newPage ()
self:_writeline("New page")
self:_writeline("New page")
end

function outputter:abort ()
if started then
self:_writeline("Aborted")
outfile:close()
started = false
end
end

function outputter:finish ()
if SILE.status.unsupported then self:_writeline("UNSUPPORTED") end
self:_writeline("End page")
self:runHooks("prefinish")
self:_writeline("Finish")
outfile:close()
if SILE.status.unsupported then
self:_writeline("UNSUPPORTED")
end
self:_writeline("End page")
self:runHooks("prefinish")
self:_writeline("Finish")
outfile:close()
started = false
end

function outputter.getCursor (_)
return cursorX, cursorY
return cursorX, cursorY
end

function outputter:setCursor (x, y, relative)
x = SU.cast("number", x)
y = SU.cast("number", y)
local oldx, oldy = self:getCursor()
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y - y
if _round(oldx) ~= _round(cursorX) then self:_writeline("Mx ", _round(x)) end
if _round(oldy) ~= _round(cursorY) then self:_writeline("My ", _round(y)) end
x = SU.cast("number", x)
y = SU.cast("number", y)
local oldx, oldy = self:getCursor()
local offset = relative and { x = cursorX, y = cursorY } or { x = 0, y = 0 }
cursorX = offset.x + x
cursorY = offset.y - y
if _round(oldx) ~= _round(cursorX) then
self:_writeline("Mx ", _round(x))
end
if _round(oldy) ~= _round(cursorY) then
self:_writeline("My ", _round(y))
end
end

function outputter:setColor (color)
if color.r then
self:_writeline("Set color", _round(color.r), _round(color.g), _round(color.b))
elseif color.c then
self:_writeline("Set color", _round(color.c), _round(color.m), _round(color.y), _round(color.k))
elseif color.l then
self:_writeline("Set color", _round(color.l))
end
if color.r then
self:_writeline("Set color", _round(color.r), _round(color.g), _round(color.b))
elseif color.c then
self:_writeline("Set color", _round(color.c), _round(color.m), _round(color.y), _round(color.k))
elseif color.l then
self:_writeline("Set color", _round(color.l))
end
end

function outputter:pushColor (color)
if color.r then
self:_writeline("Push color", _round(color.r), _round(color.g), _round(color.b))
elseif color.c then
self:_writeline("Push color (CMYK)", _round(color.c), _round(color.m), _round(color.y), _round(color.k))
elseif color.l then
self:_writeline("Push color (grayscale)", _round(color.l))
end
if color.r then
self:_writeline("Push color", _round(color.r), _round(color.g), _round(color.b))
elseif color.c then
self:_writeline("Push color (CMYK)", _round(color.c), _round(color.m), _round(color.y), _round(color.k))
elseif color.l then
self:_writeline("Push color (grayscale)", _round(color.l))
end
end

function outputter:popColor ()
self:_writeline("Pop color")
self:_writeline("Pop color")
end

function outputter:drawHbox (value, width)
if not value.glyphString then return end
width = SU.cast("number", width)
local buf
if value.complex then
local cluster = {}
for i = 1, #value.items do
local item = value.items[i]
cluster[#cluster+1] = item.gid
-- For the sake of terseness we're only dumping non-zero values
if item.glyphAdvance ~= 0 then cluster[#cluster+1] = "a=".._round(item.glyphAdvance) end
if item.x_offset then cluster[#cluster+1] = "x=".._round(item.x_offset) end
if item.y_offset then cluster[#cluster+1] = "y=".._round(item.y_offset) end
self:setCursor(item.width, 0, true)
end
buf = table.concat(cluster, " ")
else
buf = table.concat(value.glyphString, " ") .. " w=" .. _round(width)
end
self:_writeline("T", buf, "(" .. tostring(value.text) .. ")")
if not value.glyphString
then return
end
width = SU.cast("number", width)
local buf
if value.complex then
local cluster = {}
for i = 1, #value.items do
local item = value.items[i]
cluster[#cluster + 1] = item.gid
-- For the sake of terseness we're only dumping non-zero values
if item.glyphAdvance ~= 0 then
cluster[#cluster + 1] = "a=" .. _round(item.glyphAdvance)
end
if item.x_offset then
cluster[#cluster + 1] = "x=" .. _round(item.x_offset)
end
if item.y_offset then
cluster[#cluster + 1] = "y=" .. _round(item.y_offset)
end
self:setCursor(item.width, 0, true)
end
buf = table.concat(cluster, " ")
else
buf = table.concat(value.glyphString, " ") .. " w=" .. _round(width)
end
self:_writeline("T", buf, "(" .. tostring(value.text) .. ")")
end

function outputter:setFont (options)
local font = SILE.font._key(options)
if lastFont ~= font then
self:_writeline("Set font ", font)
lastFont = font
end
local font = SILE.font._key(options)
if lastFont ~= font then
self:_writeline("Set font ", font)
lastFont = font
end
end

function outputter:drawImage (src, x, y, width, height)
x = SU.cast("number", x)
y = SU.cast("number", y)
width = SU.cast("number", width)
height = SU.cast("number", height)
self:_writeline("Draw image", src, _round(x), _round(y), _round(width), _round(height))
x = SU.cast("number", x)
y = SU.cast("number", y)
width = SU.cast("number", width)
height = SU.cast("number", height)
self:_writeline("Draw image", src, _round(x), _round(y), _round(width), _round(height))
end

function outputter.getImageSize (_, src, pageno)
local pdf = require("justenoughlibtexpdf")
local llx, lly, urx, ury, xresol, yresol = pdf.imagebbox(src, pageno)
return (urx-llx), (ury-lly), xresol, yresol
local pdf = require("justenoughlibtexpdf")
local llx, lly, urx, ury, xresol, yresol = pdf.imagebbox(src, pageno)
return (urx - llx), (ury - lly), xresol, yresol
end

function outputter:drawSVG (figure, _, x, y, width, height, scalefactor)
x = SU.cast("number", x)
y = SU.cast("number", y)
width = SU.cast("number", width)
height = SU.cast("number", height)
self:_writeline("Draw SVG", _round(x), _round(y), _round(width), _round(height), figure, scalefactor)
x = SU.cast("number", x)
y = SU.cast("number", y)
width = SU.cast("number", width)
height = SU.cast("number", height)
self:_writeline("Draw SVG", _round(x), _round(y), _round(width), _round(height), figure, scalefactor)
end

function outputter:drawRule (x, y, width, depth)
x = SU.cast("number", x)
y = SU.cast("number", y)
width = SU.cast("number", width)
depth = SU.cast("number", depth)
self:_writeline("Draw line", _round(x), _round(y), _round(width), _round(depth))
x = SU.cast("number", x)
y = SU.cast("number", y)
width = SU.cast("number", width)
depth = SU.cast("number", depth)
self:_writeline("Draw line", _round(x), _round(y), _round(width), _round(depth))
end

function outputter:setLinkAnchor (name, x, y)
self:_writeline("Setting link anchor", name, x, y)
self:_writeline("Setting link anchor", name, x, y)
end

function outputter:beginLink (dest, opts)
self:_writeline("Begining a link", dest, opts)
self:_writeline("Beginning a link", dest, opts)
end

function outputter:endLink(dest, opts, x0, y0, x1, y1)
function outputter:endLink (dest, opts, x0, y0, x1, y1)
self:_writeline("Ending a link", dest, opts, x0, y0, x1, y1)
end

Expand Down
Loading

0 comments on commit 0d64fe0

Please sign in to comment.