Skip to content

Commit

Permalink
feat(classes): Add landscape option to base class (#1892)
Browse files Browse the repository at this point in the history
Co-authored-by: Caleb Maclennan <[email protected]>
  • Loading branch information
jodros and alerque authored Oct 23, 2023
1 parent 7bd675e commit 2ff0b31
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
15 changes: 13 additions & 2 deletions classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ end

function class:setOptions (options)
options = options or {}
options.papersize = options.papersize or "a4"
-- Classes that add options with dependencies should explicitly handle them, then exempt them from furthur processing.
-- The landscape option is handled explicitly before papersize, then the "rest" of options that are not interdependent.
self.options.landscape = SU.boolean(options.landscape, false)
options.landscape = nil
self.options.papersize = options.papersize or "a4"
options.papersize = nil
for option, value in pairs(options) do
self.options[option] = value
end
Expand All @@ -101,10 +106,16 @@ function class:declareOptions ()
end
return self._name
end)
self:declareOption("landscape", function(_, landscape)
if landscape then
self.landscape = landscape
end
return self.landscape
end)
self:declareOption("papersize", function (_, size)
if size then
self.papersize = size
SILE.documentState.paperSize = SILE.papersize(size)
SILE.documentState.paperSize = SILE.papersize(size, self.options.landscape)
SILE.documentState.orgPaperSize = SILE.documentState.paperSize
SILE.newFrame({
id = "page",
Expand Down
15 changes: 9 additions & 6 deletions core/papersize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ local papersize = {
}

setmetatable(papersize, {
__call = function (self, size)
__call = function (self, size, landscape)
local geometry
local _, _, x, y = string.find(size, "(.+)%s+x%s+(.+)")
if x and y then
return { SILE.measurement(x):tonumber(), SILE.measurement(y):tonumber() }
geometry = { SILE.measurement(x):tonumber(), SILE.measurement(y):tonumber() }
else
size = string.lower(size:gsub("[-%s]+", ""))
if self[size] then
return self[size]
end
local preset_name = string.lower(size:gsub("[-%s]+", ""))
geometry = self[preset_name]
end
if SU.boolean(landscape) then
geometry[1], geometry[2] = geometry[2], geometry[1]
end
if geometry then return geometry end
SU.error(string.format([[Unable to parse papersize '%s'.
Custom sizes may be entered with 'papersize=<measurement> x <measurement>'.
Predefined paper sizes include: %s]],
Expand Down
8 changes: 8 additions & 0 deletions documentation/c03-input.sil
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ 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}

\section{Ordinary text}

On the whole, ordinary text isn’t particularly interesting—it’s just typeset.
Expand Down
3 changes: 2 additions & 1 deletion packages/cropmarks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ function package:registerCommands ()

self:registerCommand("crop:setup", function (options, _)
local papersize = SU.required(options, "papersize", "setting up crop marks")
local size = SILE.papersize(papersize)
local landscape = SU.boolean(options.landscape, self.class.options.landscape)
local size = SILE.papersize(papersize, landscape)
local oldsize = SILE.documentState.paperSize
SILE.documentState.paperSize = size
local offsetx = ( SILE.documentState.paperSize[1] - oldsize[1] ) /2
Expand Down
7 changes: 6 additions & 1 deletion spec/measurements_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SILE = require("core.sile")
SU.warn = function () end

describe("The papersize parser", function()
local parse = SILE.paperSizeParser
local parse = SILE.papersize
it("should return the correct values for a6", function()
local a6 = { 297.6377985, 419.52756359999995 }
assert.is.same(parse("a6"), a6)
Expand All @@ -14,6 +14,11 @@ describe("The papersize parser", function()
assert.is.same(parse("2in x 4in"), size)
assert.is.same(parse("144 x 288"), size)
end)
it("should flip x and y page geometry for landscape mode", function()
local size = { 288, 144 }
assert.is.same(parse("2in x 4in", true), size)
assert.is.same(parse("144 x 288", true), size)
end)
it("error if unable to parse", function()
assert.has.errors(function () parse("notapaper") end)
assert.has.errors(function () parse(nil) end)
Expand Down
8 changes: 8 additions & 0 deletions tests/landscape.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Set paper size 841.8897729 595.275597
Begin page
Mx 418.5987
My 553.7327
Set font Gentium Plus;10;400;;normal;;;LTR
T 20 w=4.6924 (1)
End page
Finish
2 changes: 2 additions & 0 deletions tests/landscape.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
\begin[landscape=true]{document}
\end{document}

0 comments on commit 2ff0b31

Please sign in to comment.