Skip to content

Commit

Permalink
refactor(packages): Re-organize sheet size logic so errors happen in …
Browse files Browse the repository at this point in the history
…a more related function
  • Loading branch information
alerque committed Dec 16, 2023
1 parent 6b9573e commit ad2faa5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
35 changes: 15 additions & 20 deletions classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,6 @@ function class:setOptions (options)
for option, value in pairs(options) do
self.options[option] = value
end
if not SILE.documentState.sheetSize then
SILE.documentState.sheetSize = {
SILE.documentState.paperSize[1],
SILE.documentState.paperSize[2]
}
end
if SILE.documentState.sheetSize[1] < SILE.documentState.paperSize[1]
or SILE.documentState.sheetSize[2] < SILE.documentState.paperSize[2] then
SU.error("Sheet size shall not be smaller than the paper size")
end
if SILE.documentState.sheetSize[1] < SILE.documentState.paperSize[1] + SILE.documentState.bleed then
SU.debug("frames", "Sheet size width augmented to take page bleed into account")
SILE.documentState.sheetSize[1] = SILE.documentState.paperSize[1] + SILE.documentState.bleed
end
if SILE.documentState.sheetSize[2] < SILE.documentState.paperSize[2] + SILE.documentState.bleed then
SU.debug("frames", "Sheet size height augmented to take page bleed into account")
SILE.documentState.sheetSize[2] = SILE.documentState.paperSize[2] + SILE.documentState.bleed
end
end

function class:declareOption (option, setter)
Expand Down Expand Up @@ -153,9 +135,22 @@ function class:declareOptions ()
if size then
self.sheetsize = size
SILE.documentState.sheetSize = SILE.papersize(size, self.options.landscape)
if SILE.documentState.sheetSize[1] < SILE.documentState.paperSize[1]
or SILE.documentState.sheetSize[2] < SILE.documentState.paperSize[2] then
SU.error("Sheet size shall not be smaller than the paper size")
end
if SILE.documentState.sheetSize[1] < SILE.documentState.paperSize[1] + SILE.documentState.bleed then
SU.debug("frames", "Sheet size width augmented to take page bleed into account")
SILE.documentState.sheetSize[1] = SILE.documentState.paperSize[1] + SILE.documentState.bleed
end
if SILE.documentState.sheetSize[2] < SILE.documentState.paperSize[2] + SILE.documentState.bleed then
SU.debug("frames", "Sheet size height augmented to take page bleed into account")
SILE.documentState.sheetSize[2] = SILE.documentState.paperSize[2] + SILE.documentState.bleed
end
else
return self.sheetsize
end
return self.sheetsize
end)
end)
self:declareOption("bleed", function (_, dimen)
if dimen then
self.bleed = dimen
Expand Down
20 changes: 11 additions & 9 deletions documentation/c03-input.sil
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It is even less true now that 3rd party plugins can add their own input formats.

Hence this chanpter has been renamed.
The original chapter title was "SILE’s Input Language", as if there was only one.
The truth is there \em{is} an input syntax we call "SIL", but even that is perhaps best thought of as a structured data systax rather than a unique language.
The truth is there \em{is} an input syntax we call "SIL", but even that is perhaps best thought of as a structured data syntax rather than a unique language.
The input strings \code{\\em\{foo\}} in SIL input syntax is 100\% equivalent to \code{<em>foo</em>} in XML input syntax.
The SIL input syntax is provided as an easier to type alternative than XML which can be a bit verbose and tedious to work with by hand.
On the other hand if you're handling data written by some other program, XML might be a much better solution.
Expand Down Expand Up @@ -61,6 +61,16 @@ Once some of the basic document properties have been set up using these fixed si
For example, once the paper size is set, percentage of page width (\code{\%pw}) and height(\code{\%ph}) become valid units.
In Chapter 4 we will meet more of these relative units, and in Chapter 7 we will meet some other ways of specifying lengths to make them stretchable or shrinkable.

\subsection{Setting orientation as landscape}

The orientation of the page is defined as "portrait" by default, but if you want to set it as landscape there is an option for that:

\begin[type=autodoc:codeblock]{raw}
\begin[landscape=true]{document}
\end{raw}

\subsection{Full bleed printing}

When preparing a book for press printing, you may be asked by the professional printer to output the document on a larger sheet than your target paper, and to reserve a trim area around it.
This trick is often called “full bleed printing”.
Your document will be printed on an oversized sheet that will then be mechanically cut down to the target size.
Expand All @@ -80,14 +90,6 @@ Finally, there is also the case when the actual paper sheets available to you ar

For instance, \code{papersize=6in x 9in, sheetsize=a4} produces an A4-dimensioned document, but with you content formatted as a 6 per 9 inches US trade book. You may, obviously, combine these options and also specify a bleed area.

\subsection{Setting orientation as landscape}

The orientation of the page is defined as "portrait" by default, but if you want to set it as landscape there is an option for that:

\begin[type=autodoc:codeblock]{raw}
\begin[landscape=true]{document}
\end{raw}

\section{Ordinary text}

On the whole, ordinary text isn’t particularly interesting—it’s just typeset.
Expand Down
12 changes: 8 additions & 4 deletions outputters/libtexpdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ local deltaX
local deltaY
local function trueXCoord (x)
if not deltaX then
deltaX = (SILE.documentState.sheetSize[1] - SILE.documentState.paperSize[1]) / 2
local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
deltaX = (sheetSize[1] - SILE.documentState.paperSize[1]) / 2
end
return x + deltaX
end
local function trueYCoord (y)
if not deltaY then
deltaY = (SILE.documentState.sheetSize[2] - SILE.documentState.paperSize[2]) / 2
local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
deltaY = (sheetSize[2] - SILE.documentState.paperSize[2]) / 2
end
return y + deltaY
end
Expand All @@ -45,7 +47,8 @@ end

function outputter:_ensureInit ()
if not started then
local w, h = SILE.documentState.sheetSize[1], SILE.documentState.sheetSize[2]
local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
local w, h = sheetSize[1], sheetSize[2]
local fname = self:getOutputFilename()
-- Ideally we could want to set the PDF CropBox, BleedBox, TrimBox...
-- Our wrapper only manages the MediaBox at this point.
Expand Down Expand Up @@ -193,7 +196,8 @@ function outputter:drawSVG (figure, x, y, _, height, scalefactor)
pdf.add_content("q")
self:setCursor(x, y)
x, y = self:getCursor()
local newy = y - SILE.documentState.paperSize[2] / 2 + height - SILE.documentState.sheetSize[2] / 2
local sheetSize = SILE.documentState.sheetSize or SILE.documentState.paperSize
local newy = y - SILE.documentState.paperSize[2] / 2 + height - sheetSize[2] / 2
pdf.add_content(table.concat({ scalefactor, 0, 0, -scalefactor, trueXCoord(x), newy, "cm" }, " "))
pdf.add_content(figure)
pdf.add_content("Q")
Expand Down
2 changes: 1 addition & 1 deletion packages/cropmarks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ end
function package:registerCommands ()

self:registerCommand("cropmarks:header", function (_, _)
local info = SILE.masterFilename
local info = SILE.input.filenames[1]
.. " - "
.. self.class.packages.date:date({ format = "%x %X" })
.. " - " .. outcounter
Expand Down
2 changes: 1 addition & 1 deletion tests/bug-337.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
\begin[papersize=a7,class=book, sheetsize=a6]{document}
\begin[papersize=a7,class=book,sheetsize=a6]{document}
\use[module=packages.color]
\script[src=inc.bug-337]
\define[command=cropmarks:header]{tests/bug-337.sil}
Expand Down

0 comments on commit ad2faa5

Please sign in to comment.