• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 11130695999

01 Oct 2024 05:48PM UTC coverage: 33.585% (+4.0%) from 29.567%
11130695999

push

github

web-flow
Merge pull request #2119 from Omikhleia/feat-math-sqrt

0 of 49 new or added lines in 2 files covered. (0.0%)

81 existing lines in 6 files now uncovered.

5867 of 17469 relevant lines covered (33.59%)

2199.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/core/settings.lua
1
--- core settings instance
2
--- @module SILE.settings
3

4
local deprecator = function ()
5
   SU.deprecated("SILE.settings.*", "SILE.settings:*", "0.13.0", "0.15.0")
×
6
end
7

8
--- @type settings
9
local settings = pl.class()
99✔
10

11
function settings:_init ()
99✔
12
   self.state = {}
99✔
13
   self.declarations = {}
99✔
14
   self.stateQueue = {}
99✔
15
   self.defaults = {}
99✔
16
   self.hooks = {}
99✔
17

18
   self:declare({
198✔
19
      parameter = "document.language",
20
      type = "string",
21
      default = "en",
22
      hook = function (language)
23
         if SILE.scratch.loaded_languages and not SILE.scratch.loaded_languages[language] then
527✔
24
            SU.warn(([[Setting document.language to '%s', but support for '%s' has not been loaded!
16✔
25

26
  Consider invoking \language[main=%s] which loads language support before
27
  setting it or manually calling SILE.languageSupport.loadLanguage("%s").
28
            ]]):format(language, language, language, language))
8✔
29
         end
30
         fluent:set_locale(language)
527✔
31
      end,
32
      help = "Locale for localized language support",
33
   })
34

35
   self:declare({
198✔
36
      parameter = "document.parindent",
37
      type = "glue",
38
      default = SILE.types.node.glue("1bs"),
297✔
39
      help = "Glue at start of paragraph",
40
   })
41

42
   self:declare({
198✔
43
      parameter = "document.baselineskip",
44
      type = "vglue",
45
      default = SILE.types.node.vglue("1.2em plus 1pt"),
198✔
46
      help = "Leading",
47
   })
48

49
   self:declare({
198✔
50
      parameter = "document.lineskip",
51
      type = "vglue",
52
      default = SILE.types.node.vglue("1pt"),
198✔
53
      help = "Leading",
54
   })
55

56
   self:declare({
198✔
57
      parameter = "document.parskip",
58
      type = "vglue",
59
      default = SILE.types.node.vglue("0pt plus 1pt"),
198✔
60
      help = "Leading",
61
   })
62

63
   self:declare({
99✔
64
      parameter = "document.spaceskip",
65
      type = "length or nil",
66
      default = nil,
67
      help = "The length of a space (if nil, then measured from the font)",
68
   })
69

70
   self:declare({
99✔
71
      parameter = "document.rskip",
72
      type = "glue or nil",
73
      default = nil,
74
      help = "Skip to be added to right side of line",
75
   })
76

77
   self:declare({
99✔
78
      parameter = "document.lskip",
79
      type = "glue or nil",
80
      default = nil,
81
      help = "Skip to be added to left side of line",
82
   })
83

84
   self:declare({
99✔
85
      parameter = "document.zenkakuchar",
86
      default = "あ",
87
      type = "string",
88
      help = "The character measured to determine the length of a zenkaku width (全角幅)",
89
   })
90

91
   SILE.registerCommand(
198✔
92
      "set",
99✔
93
      function (options, content)
94
         local makedefault = SU.boolean(options.makedefault, false)
42✔
95
         local reset = SU.boolean(options.reset, false)
42✔
96
         local value = options.value
42✔
97
         if content and (type(content) == "function" or content[1]) then
42✔
98
            if makedefault then
8✔
99
               SU.warn(
×
100
                  "Are you sure meant to set default settings *and* pass content to ostensibly apply them to temporarily?"
101
               )
102
            end
103
            self:temporarily(function ()
16✔
104
               if options.parameter then
8✔
105
                  local parameter = SU.required(options, "parameter", "\\set command")
8✔
106
                  self:set(parameter, value, makedefault, reset)
8✔
107
               end
108
               SILE.process(content)
8✔
109
            end)
110
         else
111
            local parameter = SU.required(options, "parameter", "\\set command")
34✔
112
            self:set(parameter, value, makedefault, reset)
34✔
113
         end
114
      end,
115
      "Set a SILE parameter <parameter> to value <value> (restoring the value afterwards if <content> is provided)",
99✔
116
      nil,
99✔
117
      true
118
   )
99✔
119
end
120

121
--- Stash the current values of all settings in a stack to be returned to later
122
function settings:pushState ()
99✔
123
   if not self then
619✔
124
      return deprecator()
×
125
   end
126
   table.insert(self.stateQueue, self.state)
619✔
127
   self.state = pl.tablex.copy(self.state)
1,238✔
128
end
129

130
--- Return the most recently pushed set of values in the setting stack
131
function settings:popState ()
99✔
132
   if not self then
619✔
133
      return deprecator()
×
134
   end
135
   local previous = self.state
619✔
136
   self.state = table.remove(self.stateQueue)
1,238✔
137
   for parameter, oldvalue in pairs(previous) do
34,000✔
138
      if self.hooks[parameter] then
33,381✔
139
         local newvalue = self.state[parameter]
33,381✔
140
         if oldvalue ~= newvalue then
33,381✔
141
            self:runHooks(parameter, newvalue)
841✔
142
         end
143
      end
144
   end
145
end
146

147
--- Declare a new setting
148
--- @tparam table specs { parameter, type, default, help, hook, ... } declaration specification
149
function settings:declare (spec)
99✔
150
   if not spec then
5,484✔
151
      return deprecator()
×
152
   end
153
   if spec.name then
5,484✔
154
      SU.deprecated(
×
155
         "'name' argument of SILE.settings:declare",
156
         "'parameter' argument of SILE.settings:declare",
157
         "0.10.10",
158
         "0.11.0"
159
      )
160
   end
161
   if self.declarations[spec.parameter] then
5,484✔
162
      SU.debug("settings", "Attempt to re-declare setting:", spec.parameter)
650✔
163
      return
650✔
164
   end
165
   self.declarations[spec.parameter] = spec
4,834✔
166
   self.hooks[spec.parameter] = {}
4,834✔
167
   if spec.hook then
4,834✔
168
      self:registerHook(spec.parameter, spec.hook)
99✔
169
   end
170
   self:set(spec.parameter, spec.default, true)
4,834✔
171
end
172

173
--- Reset all settings to their registered default values.
174
function settings:reset ()
99✔
175
   if not self then
×
176
      return deprecator()
×
177
   end
178
   for k, _ in pairs(self.state) do
×
179
      self:set(k, self.defaults[k])
×
180
   end
181
end
182

183
--- Restore all settings to the value they had in the top-level state,
184
-- that is at the tap of the settings stack (normally the document level).
185
function settings:toplevelState ()
99✔
186
   if not self then
51✔
187
      return deprecator()
×
188
   end
189
   if #self.stateQueue ~= 0 then
51✔
190
      for parameter, _ in pairs(self.state) do
2,653✔
191
         -- Bypass self:set() as the latter performs some tests and a cast,
192
         -- but the setting might not have been defined in the top level state
193
         -- (in which case, assume the default value).
194
         self.state[parameter] = self.stateQueue[1][parameter] or self.defaults[parameter]
2,602✔
195
      end
196
   end
197
end
198

199
--- Get the value of a setting
200
-- @tparam string parameter The full name of the setting to fetch.
201
-- @return Value of setting
202
function settings:get (parameter)
99✔
203
   -- HACK FIXME https://github.com/sile-typesetter/sile/issues/1699
204
   -- See comment on set() below.
205
   if parameter == "current.parindent" then
106,834✔
206
      return SILE.typesetter and SILE.typesetter.state.parindent
610✔
207
   end
208
   if not parameter then
106,224✔
209
      return deprecator()
×
210
   end
211
   if not self.declarations[parameter] then
106,224✔
212
      SU.error("Undefined setting '" .. parameter .. "'")
×
213
   end
214
   if type(self.state[parameter]) ~= "nil" then
106,224✔
215
      return self.state[parameter]
76,451✔
216
   else
217
      return self.defaults[parameter]
29,773✔
218
   end
219
end
220

221
--- Set the value of a setting
222
-- @tparam string parameter The full name of the setting to change.
223
-- @param value The new value to change it to.
224
-- @tparam[opt=false] boolean makedefault Whether to make this the new default value.
225
-- @tparam[opt=false] boolean reset Whether to reset the value to the current default value.
226
function settings:set (parameter, value, makedefault, reset)
99✔
227
   -- HACK FIXME https://github.com/sile-typesetter/sile/issues/1699
228
   -- Anything dubbed current.xxx should likely NOT be a "setting" (subject
229
   -- to being pushed/popped via temporary stacking) and actually has its
230
   -- own lifecycle (e.g. reset for the next paragraph).
231
   -- These should be rather typesetter states, or something to that extent
232
   -- yet to clarify. Notably, current.parindent falls in that category,
233
   -- BUT probably current.hangAfter and current.hangIndent too.
234
   -- To avoid breaking too much code yet without being sure of the solution,
235
   -- we implement a hack of sorts for current.parindent only.
236
   -- Note moreover that current.parindent is currently probably a bad concept
237
   -- anyway:
238
   --   - It can be nil (= document.parindent applies)
239
   --   - It can be a zero-glue (\noindent, ragged environments, etc.)
240
   --   - It can be a valued glue set to document.parindent
241
   --     (e.g. from \indent, and document.parindent thus applies)
242
   --   - It could be another valued glue (uh, use case to ascertain)
243
   -- What we would _likely_ only need to track is whether document.parindent
244
   -- applies or not on the paragraph just composed afterwards...
245
   if parameter == "current.parindent" then
10,888✔
246
      if SILE.typesetter and not SILE.typesetter.state.hmodeOnly then
965✔
247
         SILE.typesetter.state.parindent = SU.cast("glue or nil", value)
1,828✔
248
      end
249
      return
965✔
250
   end
251
   if type(self) ~= "table" then
9,923✔
252
      return deprecator()
×
253
   end
254
   if not self.declarations[parameter] then
9,923✔
255
      SU.error("Undefined setting '" .. parameter .. "'")
×
256
   end
257
   if reset then
9,923✔
258
      if makedefault then
4✔
259
         SU.error("Can't set a new default and revert to and old default setting at the same time!")
×
260
      end
261
      value = self.defaults[parameter]
4✔
262
   else
263
      value = SU.cast(self.declarations[parameter].type, value)
19,838✔
264
   end
265
   self.state[parameter] = value
9,923✔
266
   if makedefault then
9,923✔
267
      self.defaults[parameter] = value
4,861✔
268
   end
269
   self:runHooks(parameter, value)
9,923✔
270
end
271

272
--- Register a callback hook to be run when a setting changes.
273
-- @tparam string parameter Name of the setting to add a hook to.
274
-- @tparam function func Callback function accepting one argument (the new value).
275
function settings:registerHook (parameter, func)
99✔
276
   table.insert(self.hooks[parameter], func)
99✔
277
end
278

279
--- Trigger execution of callback hooks for a given setting.
280
-- @tparam string parameter The name of the parameter changes.
281
-- @param value The new value of the setting, passed as the first argument to the hook function.
282
function settings:runHooks (parameter, value)
99✔
283
   if self.hooks[parameter] then
10,764✔
284
      for _, func in ipairs(self.hooks[parameter]) do
11,291✔
285
         SU.debug("classhooks", "Running setting hook for", parameter)
527✔
286
         func(value)
527✔
287
      end
288
   end
289
end
290

291
--- Isolate a block of processing so that setting changes made during the block don't last past the block.
292
-- (Under the hood this just uses `:pushState()`, the processes the function, then runs `:popState()`)
293
-- @tparam function func A function wrapping the actions to take without affecting settings for future use.
294
function settings:temporarily (func)
99✔
295
   if not func then
524✔
296
      return deprecator()
×
297
   end
298
   self:pushState()
524✔
299
   func()
524✔
300
   self:popState()
524✔
301
end
302

303
--- Create a settings wrapper function that applies current settings to later content processing.
304
--- @treturn function a closure function accepting one argument (content) to process using
305
--- typesetter settings as they are at the time of closure creation.
306
function settings:wrap ()
99✔
UNCOV
307
   if not self then
×
308
      return deprecator()
×
309
   end
UNCOV
310
   local clSettings = pl.tablex.copy(self.state)
×
311
   return function (content)
UNCOV
312
      table.insert(self.stateQueue, self.state)
×
UNCOV
313
      self.state = clSettings
×
UNCOV
314
      SILE.process(content)
×
UNCOV
315
      self:popState()
×
316
   end
317
end
318

319
return settings
99✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc