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

sile-typesetter / sile / 6915845768

18 Nov 2023 07:23PM UTC coverage: 63.161% (-5.6%) from 68.751%
6915845768

push

github

web-flow
Merge 0f5c09a66 into f64e235fa

9802 of 15519 relevant lines covered (63.16%)

2045.54 hits per line

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

92.17
/languages/unicode.lua
1
local icu = require("justenoughicu")
96✔
2

3
local chardata = require("char-def")
96✔
4

5
SILE.nodeMakers.base = pl.class({
192✔
6

7
    _init = function (self, options)
8
      self.contents = {}
1,414✔
9
      self.options = options
1,414✔
10
      self.token = ""
1,414✔
11
      self.lastnode = false
1,414✔
12
      self.lasttype = false
1,414✔
13
    end,
14

15
    makeToken = function (self)
16
      if #self.contents > 0 then
4,806✔
17
        coroutine.yield(SILE.shaper:formNnode(self.contents, self.token, self.options))
6,386✔
18
        SU.debug("tokenizer", "Token:", self.token)
3,193✔
19
        self.contents = {}
3,193✔
20
        self.token = ""
3,193✔
21
        self.lastnode = "nnode"
3,193✔
22
      end
23
    end,
24

25
    addToken = function (self, char, item)
26
      self.token = self.token .. char
11,585✔
27
      table.insert(self.contents, item)
11,585✔
28
    end,
29

30
    makeGlue = function (self, item)
31
      if SILE.settings:get("typesetter.obeyspaces") or self.lastnode ~= "glue" then
3,254✔
32
        SU.debug("tokenizer", "Space node")
1,606✔
33
        coroutine.yield(SILE.shaper:makeSpaceNode(self.options, item))
3,212✔
34
      end
35
      self.lastnode = "glue"
1,627✔
36
      self.lasttype = "sp"
1,627✔
37
    end,
38

39
    makePenalty = function (self, p)
40
      if self.lastnode ~= "penalty" and self.lastnode ~= "glue" then
1,506✔
41
        coroutine.yield( SILE.nodefactory.penalty({ penalty = p or 0 }) )
28✔
42
      end
43
      self.lastnode = "penalty"
1,506✔
44
    end,
45

46
    iterator = function (_, _)
47
      SU.error("Abstract function nodemaker:iterator called", true)
×
48
    end,
49

50
    charData = function (_, char)
51
      local cp = SU.codepoint(char)
34,217✔
52
      if not chardata[cp] then return {} end
34,217✔
53
      return chardata[cp]
33,604✔
54
    end,
55

56
    isPunctuation = function (self, char)
57
      return self.isPunctuationType[self:charData(char).category]
×
58
    end,
59

60
    isSpace = function (self, char)
61
      return self.isSpaceType[self:charData(char).linebreak]
29,210✔
62
    end,
63

64
    isBreaking = function (self, char)
65
      return self.isBreakingType[self:charData(char).linebreak]
19,612✔
66
    end,
67
    isQuote = function (self, char)
68
      return self.isQuoteType[self:charData(char).linebreak]
19,612✔
69
    end
70

71
  })
96✔
72

73
SILE.nodeMakers.unicode = pl.class(SILE.nodeMakers.base)
192✔
74

75
SILE.nodeMakers.unicode.isWordType = { cm = true }
96✔
76
SILE.nodeMakers.unicode.isSpaceType = { sp = true }
96✔
77
SILE.nodeMakers.unicode.isBreakingType = { ba = true, zw = true }
96✔
78
SILE.nodeMakers.unicode.isPunctuationType = { po = true }
96✔
79
SILE.nodeMakers.unicode.isQuoteType = {} -- quote linebreak category is ambiguous depending on the language
96✔
80

81
function SILE.nodeMakers.unicode:dealWith (item)
192✔
82
  local char = item.text
9,809✔
83
  local cp = SU.codepoint(char)
9,809✔
84
  local thistype = chardata[cp] and chardata[cp].linebreak
9,809✔
85
  if self:isSpace(item.text) then
19,618✔
86
    self:makeToken()
3✔
87
    self:makeGlue(item)
6✔
88
  elseif self:isBreaking(item.text) then
19,612✔
89
    self:addToken(char, item)
×
90
    self:makeToken()
×
91
    self:makePenalty(0)
×
92
  elseif self:isQuote(item.text) then
19,612✔
93
    self:addToken(char, item)
×
94
    self:makeToken()
×
95
  elseif self.lasttype and (thistype and thistype ~= self.lasttype and not self.isWordType[thistype]) then
9,806✔
96
    self:addToken(char, item)
128✔
97
  else
98
    self:letterspace()
9,742✔
99
    self:addToken(char, item)
9,742✔
100
  end
101
  self.lasttype = thistype
9,809✔
102
end
103

104
function SILE.nodeMakers.unicode:handleInitialGlue (items)
192✔
105
  local i = 1
1,412✔
106
  while i <= #items do
1,479✔
107
    local item = items[i]
1,460✔
108
    if self:isSpace(item.text) then self:makeGlue(item) else break end
2,987✔
109
    i = i + 1
67✔
110
  end
111
  return i, items
1,412✔
112
end
113

114
function SILE.nodeMakers.unicode:letterspace ()
192✔
115
  if not SILE.settings:get("document.letterspaceglue") then return end
19,484✔
116
  if self.token then self:makeToken() end
55✔
117
  if self.lastnode and self.lastnode ~= "glue" then
55✔
118
    local w = SILE.settings:get("document.letterspaceglue").width
102✔
119
    SU.debug("tokenizer", "Letter space glue:", w)
51✔
120
    coroutine.yield(SILE.nodefactory.kern({ width = w }))
102✔
121
    self.lastnode = "glue"
51✔
122
    self.lasttype = "sp"
51✔
123
  end
124
end
125

126
function SILE.nodeMakers.unicode.isICUBreakHere (_, chunks, item)
192✔
127
  return chunks[1] and (item.index >= chunks[1].index)
13,145✔
128
end
129

130
function SILE.nodeMakers.unicode:handleICUBreak (chunks, item)
192✔
131
  -- The ICU library has told us there is a breakpoint at
132
  -- this index. We need to...
133
  local bp = chunks[1]
3,336✔
134
  -- ... remove this breakpoint (and any out of order ones)
135
  -- from the ICU breakpoints array so that chunks[1] is
136
  -- the next index point for comparison against the string...
137
  while chunks[1] and item.index >= chunks[1].index do
6,672✔
138
    table.remove(chunks, 1)
6,672✔
139
  end
140
  -- ...decide which kind of breakpoint we have here and
141
  -- handle it appropriately.
142
  if bp.type == "word" then
3,336✔
143
    self:handleWordBreak(item)
3,660✔
144
  elseif bp.type == "line" then
1,506✔
145
    self:handleLineBreak(item, bp.subtype)
1,506✔
146
  end
147
  return chunks
3,336✔
148
end
149

150
function SILE.nodeMakers.unicode:handleWordBreak (item)
192✔
151
  self:makeToken()
1,830✔
152
  if self:isSpace(item.text) then
3,660✔
153
    -- Spacing word break
154
    self:makeGlue(item)
3,114✔
155
  else -- a word break which isn't a space
156
    self:addToken(item.text, item)
273✔
157
  end
158
end
159

160
function SILE.nodeMakers.unicode:handleLineBreak (item, subtype)
192✔
161
  -- Because we are in charge of paragraphing, we
162
  -- will override space-type line breaks, and treat
163
  -- them just as ordinary word spaces.
164
  if self:isSpace(item.text) then
3,012✔
165
    self:handleWordBreak(item)
×
166
    return
×
167
  end
168
  -- But explicit line breaks we will turn into
169
  -- soft and hard breaks.
170
  self:makeToken()
1,506✔
171
  self:makePenalty(subtype == "soft" and 0 or -1000)
1,506✔
172
  local char = item.text
1,506✔
173
  self:addToken(char, item)
1,506✔
174
  local cp = SU.codepoint(char)
1,506✔
175
  self.lasttype = chardata[cp] and chardata[cp].linebreak
1,506✔
176
end
177

178
function SILE.nodeMakers.unicode:iterator (items)
192✔
179
  local fulltext = ""
1,412✔
180
  for i = 1, #items do
14,624✔
181
    fulltext = fulltext .. items[i].text
13,212✔
182
  end
183
  local chunks = { icu.breakpoints(fulltext, self.options.language) }
1,412✔
184
  table.remove(chunks, 1)
1,412✔
185
  return coroutine.wrap(function ()
1,412✔
186
    local i
187
    i, self.items = self:handleInitialGlue(items)
2,824✔
188
    for j = i, #items do
14,557✔
189
      self.i = j
13,145✔
190
      self.item = self.items[self.i]
13,145✔
191
      if self:isICUBreakHere(chunks, self.item) then
26,290✔
192
        chunks = self:handleICUBreak(chunks, self.item)
6,672✔
193
      else
194
        self:dealWith(self.item)
9,809✔
195
      end
196
    end
197
    self:makeToken()
1,412✔
198
  end)
199
end
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

© 2025 Coveralls, Inc