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

sile-typesetter / sile / 9304060604

30 May 2024 02:07PM UTC coverage: 74.124% (-0.6%) from 74.707%
9304060604

push

github

alerque
style: Reformat Lua with stylua

8104 of 11995 new or added lines in 184 files covered. (67.56%)

15 existing lines in 11 files now uncovered.

12444 of 16788 relevant lines covered (74.12%)

7175.1 hits per line

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

90.2
/shapers/base.lua
1
-- local smallTokenSize = 20 -- Small words will be cached
2
-- local shapeCache = {}
3
-- local _key = function (options)
4
--   return table.concat({ options.family;options.language;options.script;options.size;("%d"):format(options.weight);options.style;options.variant;options.features;options.variations;options.direction;options.filename }, ";")
5
-- end
6

7
SILE.settings:declare({ parameter = "shaper.variablespaces", type = "boolean", default = true })
181✔
8
SILE.settings:declare({ parameter = "shaper.spaceenlargementfactor", type = "number or integer", default = 1.2 })
181✔
9
SILE.settings:declare({ parameter = "shaper.spacestretchfactor", type = "number or integer", default = 1 / 2 })
181✔
10
SILE.settings:declare({ parameter = "shaper.spaceshrinkfactor", type = "number or integer", default = 1 / 3 })
181✔
11

12
SILE.settings:declare({
181✔
13
   parameter = "shaper.tracking",
14
   type = "number or nil",
15
   default = nil,
16
})
17

18
-- Function for testing shaping in the repl
19
-- luacheck: ignore makenodes
20
-- TODO, figure out a way to explicitly register things in the repl env
21
makenodes = function (token, options)
NEW
22
   return SILE.shaper:createNnodes(token, SILE.font.loadDefaults(options or {}))
×
23
end
24

25
local function shapespace (spacewidth)
26
   spacewidth = SU.cast("measurement", spacewidth)
12,968✔
27
   -- In some scripts with word-level kerning, glue can be negative.
28
   -- Use absolute value to ensure stretch and shrink work as expected.
29
   local absoluteSpaceWidth = math.abs(spacewidth:tonumber())
12,968✔
30
   local length = spacewidth * SILE.settings:get("shaper.spaceenlargementfactor")
12,968✔
31
   local stretch = absoluteSpaceWidth * SILE.settings:get("shaper.spacestretchfactor")
12,968✔
32
   local shrink = absoluteSpaceWidth * SILE.settings:get("shaper.spaceshrinkfactor")
12,968✔
33
   return SILE.length(length, stretch, shrink)
6,484✔
34
end
35

36
local shaper = pl.class()
181✔
37
shaper.type = "shaper"
181✔
38
shaper._name = "base"
181✔
39

40
-- Return the length of a space character
41
-- with a particular set of font options,
42
-- giving preference to document.spaceskip
43
-- Caching this has no significant speedup
44
function shaper:measureSpace (options)
181✔
45
   local ss = SILE.settings:get("document.spaceskip")
56✔
46
   if ss then
56✔
47
      SILE.settings:temporarily(function ()
46✔
48
         SILE.settings:set("font.size", options.size)
23✔
49
         SILE.settings:set("font.family", options.family)
23✔
50
         SILE.settings:set("font.filename", options.filename)
23✔
51
         ss = ss:absolute()
46✔
52
      end)
53
      return ss
23✔
54
   end
55
   local items, width = self:shapeToken(" ", options)
33✔
56
   if not width and not items[1] then
33✔
NEW
57
      SU.warn("Could not measure the width of a space")
×
NEW
58
      return SILE.length()
×
59
   end
60
   return shapespace(width and width.length or items[1].width)
33✔
61
end
62

63
function shaper:measureChar (char)
181✔
64
   local options = SILE.font.loadDefaults({})
1,931✔
65
   options.tracking = SILE.settings:get("shaper.tracking")
3,862✔
66
   local items = self:shapeToken(char, options)
1,931✔
67
   if #items > 0 then
1,931✔
68
      return { height = items[1].height, width = items[1].width }
1,931✔
69
   else
NEW
70
      SU.error("Unable to measure character", char)
×
71
   end
72
end
73

74
-- Given a text and some font options, return a bunch of boxes
75
function shaper.shapeToken (_, _, _)
181✔
NEW
76
   SU.error("Abstract function shapeToken called", true)
×
77
end
78

79
-- Given font options, select a font. We will handle
80
-- caching here. Returns an arbitrary, implementation-specific
81
-- object (ie a PAL for Pango, font number for libtexpdf, ...)
82
function shaper.getFace (_)
181✔
NEW
83
   SU.error("Abstract function getFace called", true)
×
84
end
85

86
function shaper.addShapedGlyphToNnodeValue (_, _, _)
181✔
NEW
87
   SU.error("Abstract function addShapedGlyphToNnodeValue called", true)
×
88
end
89

90
function shaper.preAddNodes (_, _, _) end
181✔
91

92
function shaper:createNnodes (token, options)
181✔
93
   options.tracking = SILE.settings:get("shaper.tracking")
22,444✔
94
   local items, _ = self:shapeToken(token, options)
11,222✔
95
   if #items < 1 then
11,222✔
NEW
96
      return {}
×
97
   end
98
   local lang = options.language
11,222✔
99
   SILE.languageSupport.loadLanguage(lang)
11,222✔
100
   local nodeMaker = SILE.nodeMakers[lang] or SILE.nodeMakers.unicode
11,222✔
101
   local nodes = {}
11,222✔
102
   for node in nodeMaker(options):iterator(items, token) do
58,994✔
103
      table.insert(nodes, node)
25,328✔
104
   end
105
   return nodes
11,222✔
106
end
107

108
function shaper:formNnode (contents, token, options)
181✔
109
   local nnodeContents = {}
18,663✔
110
   -- local glyphs = {}
111
   local totalWidth = 0
18,663✔
112
   local totalDepth = 0
18,663✔
113
   local totalHeight = 0
18,663✔
114
   -- local glyphNames = {}
115
   local nnodeValue = { text = token, options = options, glyphString = {} }
18,663✔
116
   SILE.shaper:preAddNodes(contents, nnodeValue)
18,663✔
117
   local misfit = false
18,663✔
118
   if SILE.typesetter.frame and SILE.typesetter.frame:writingDirection() == "TTB" then
37,326✔
119
      if options.direction == "LTR" then
12✔
NEW
120
         misfit = true
×
121
      end
122
   else
123
      if options.direction == "TTB" then
18,651✔
NEW
124
         misfit = true
×
125
      end
126
   end
127
   for i = 1, #contents do
74,938✔
128
      local glyph = contents[i]
56,275✔
129
      if (options.direction == "TTB") ~= misfit then
56,275✔
130
         if glyph.width > totalHeight then
12✔
131
            totalHeight = glyph.width
6✔
132
         end
133
         totalWidth = totalWidth + glyph.height
12✔
134
      else
135
         if glyph.depth > totalDepth then
56,263✔
136
            totalDepth = glyph.depth
15,448✔
137
         end
138
         if glyph.height > totalHeight then
56,263✔
139
            totalHeight = glyph.height
24,336✔
140
         end
141
         totalWidth = totalWidth + glyph.width
56,263✔
142
      end
143
      self:addShapedGlyphToNnodeValue(nnodeValue, glyph)
56,275✔
144
   end
145
   table.insert(
37,326✔
146
      nnodeContents,
18,663✔
147
      SILE.nodefactory.hbox({
37,326✔
148
         depth = totalDepth,
18,663✔
149
         height = totalHeight,
18,663✔
150
         misfit = misfit,
18,663✔
151
         width = SILE.length(totalWidth),
37,326✔
152
         value = nnodeValue,
18,663✔
153
      })
154
   )
155
   return SILE.nodefactory.nnode({
18,663✔
156
      nodes = nnodeContents,
18,663✔
157
      text = token,
18,663✔
158
      misfit = misfit,
18,663✔
159
      options = options,
18,663✔
160
      language = options.language,
18,663✔
161
   })
18,663✔
162
end
163

164
function shaper.makeSpaceNode (_, options, item)
181✔
165
   local width
166
   if SILE.settings:get("shaper.variablespaces") then
12,952✔
167
      width = shapespace(item.width)
12,902✔
168
   else
169
      width = SILE.shaper:measureSpace(options)
50✔
170
   end
171
   return SILE.nodefactory.glue(width)
6,476✔
172
end
173

174
function shaper.debugVersions (_) end
181✔
175

176
return shaper
181✔
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