From 0fb9adefca3adf9ff8a56755e69474960792d85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20A=2E=20Quinaglia?= Date: Mon, 23 Oct 2023 07:50:43 -0300 Subject: [PATCH] feat(classes): Add landscape option to base class (#1892) Co-authored-by: Caleb Maclennan --- classes/base.lua | 15 +++++++++++++-- core/papersize.lua | 15 +++++++++------ documentation/c03-input.sil | 8 ++++++++ packages/cropmarks/init.lua | 3 ++- spec/measurements_spec.lua | 7 ++++++- tests/landscape.expected | 8 ++++++++ tests/landscape.sil | 2 ++ 7 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 tests/landscape.expected create mode 100644 tests/landscape.sil diff --git a/classes/base.lua b/classes/base.lua index b06428202..0d469006a 100644 --- a/classes/base.lua +++ b/classes/base.lua @@ -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 @@ -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", diff --git a/core/papersize.lua b/core/papersize.lua index 2b8a9d062..f1b9c4a04 100644 --- a/core/papersize.lua +++ b/core/papersize.lua @@ -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= x '. Predefined paper sizes include: %s]], diff --git a/documentation/c03-input.sil b/documentation/c03-input.sil index f41a7cbc5..3151e7f94 100644 --- a/documentation/c03-input.sil +++ b/documentation/c03-input.sil @@ -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. diff --git a/packages/cropmarks/init.lua b/packages/cropmarks/init.lua index 59b6b8f71..a3c5af14f 100644 --- a/packages/cropmarks/init.lua +++ b/packages/cropmarks/init.lua @@ -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 diff --git a/spec/measurements_spec.lua b/spec/measurements_spec.lua index 52d9b1966..e4b9eec16 100644 --- a/spec/measurements_spec.lua +++ b/spec/measurements_spec.lua @@ -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) @@ -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) diff --git a/tests/landscape.expected b/tests/landscape.expected new file mode 100644 index 000000000..69035d1aa --- /dev/null +++ b/tests/landscape.expected @@ -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 diff --git a/tests/landscape.sil b/tests/landscape.sil new file mode 100644 index 000000000..5e3f9b645 --- /dev/null +++ b/tests/landscape.sil @@ -0,0 +1,2 @@ +\begin[landscape=true]{document} +\end{document}