Skip to content

Commit

Permalink
refactor: Rewrite landscape handling PR
Browse files Browse the repository at this point in the history
* Avoid possible race conditions
* Only set defaults once
* Handle landscape mode on custom paper sizes
* Avoid setting usused variables
* Add code comments for the benefit of other coders and future self
  • Loading branch information
alerque committed Oct 23, 2023
1 parent f883fea commit 82bdafe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
11 changes: 7 additions & 4 deletions classes/base.lua
Expand Up @@ -79,8 +79,12 @@ end

function class:setOptions (options)
options = options or {}
-- 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.papersize = options.papersize or "a4"
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 @@ -103,9 +107,8 @@ function class:declareOptions ()
return self._name
end)
self:declareOption("landscape", function(_, landscape)
if landscape then
self.landscape = SU.boolean(landscape, false)
SILE.documentState.landscape = self.landscape
if landscape then
self.landscape = landscape
end
return self.landscape
end)
Expand Down
16 changes: 8 additions & 8 deletions core/papersize.lua
Expand Up @@ -64,18 +64,18 @@ local papersize = {

setmetatable(papersize, {
__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
if landscape then
self[size][1], self[size][2] = self[size][2], self[size][1]
end
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

0 comments on commit 82bdafe

Please sign in to comment.