Debian Codegraph @

texmf-dist/tex/latex/polyglossia/polyglossia-punct.lua

raw
1 --
2 -- polyglossia-punct.lua
3 -- part of polyglossia v2.10 -- 2026/04/18
4 --
5
6 require('polyglossia') -- just in case...
7
8 local new_attribute = luatexbase.new_attribute
9
10 local node = node
11
12 local insert_node_before = node.insert_before
13 local insert_node_after = node.insert_after
14 local remove_node = node.remove
15 local has_attribute = node.has_attribute
16 local node_copy = node.copy
17 local new_node = node.new
18 local end_of_math = node.end_of_math
19 local getnext = node.getnext
20 local getprev = node.getprev
21
22 -- node types according to node.types()
23 local glue_code = node.id"glue"
24 local glyph_code = node.id"glyph"
25 local penalty_code = node.id"penalty"
26 local kern_code = node.id"kern"
27 local math_code = node.id"math"
28
29 -- we need some node subtypes
30 local userkern = 1
31 local userskip = 0
32 local removable_skip = {
33 [0] = true, -- userskip
34 [13] = true, -- spaceskip
35 [14] = true, -- xspaceskip
36 }
37
38 -- we make a new node, so that we can copy it later on
39 local kern_node = new_node(kern_code)
40 kern_node.subtype = userkern -- this kern can be removed later on
41
42 local function get_kern_node(dim)
43 local n = node_copy(kern_node)
44 n.kern = dim
45 return n
46 end
47
48 local glue_node = new_node(glue_code)
49 glue_node.subtype = userskip
50
51 local function get_glue_node(dim, stretch, shrink)
52 local n = node_copy(glue_node)
53 n.width = dim
54 n.stretch = stretch
55 n.shrink = shrink
56 return n
57 end
58
59 local penalty_node = new_node(penalty_code)
60 penalty_node.penalty = 10000
61
62 local function get_penalty_node()
63 return node_copy(penalty_node)
64 end
65
66 -- all possible space characters according to section 6.2 of the Unicode Standard
67 -- https://www.unicode.org/versions/Unicode12.0.0/ch06.pdf
68 local space_chars = {
69 [0x20] = true, -- space
70 [0xA0] = true, -- no-break space
71 [0x1680] = true, -- ogham space mark
72 [0x2000] = true, -- en quad
73 [0x2001] = true, -- em quad
74 [0x2002] = true, -- en space
75 [0x2003] = true, -- em space
76 [0x2004] = true, -- three-per-em-space
77 [0x2005] = true, -- four-per-em space
78 [0x2006] = true, -- six-per-em space
79 [0x2007] = true, -- figure space
80 [0x2008] = true, -- punctuation space
81 [0x2009] = true, -- thin space
82 [0x200A] = true, -- hair space
83 [0x202F] = true, -- narrow no-break space
84 [0x205F] = true, -- medium mathematical space
85 [0x3000] = true -- ideographic space
86 }
87
88 -- all left bracket characters, referenced by their Unicode slot
89 local left_bracket_chars = {
90 [0x28] = true, -- left parenthesis
91 [0x5B] = true, -- left square bracket
92 [0x7B] = true, -- left curly bracket
93 [0x27E8] = true -- mathematical left angle bracket
94 }
95
96 -- all right bracket characters, referenced by their Unicode slot
97 local right_bracket_chars = {
98 [0x29] = true, -- right parenthesis
99 [0x5D] = true, -- right square bracket
100 [0x7D] = true, -- right curly bracket
101 [0x27E9] = true -- mathematical right angle bracket
102 }
103
104 -- question and exclamation marks, referenced by their Unicode slot
105 local question_exclamation_chars = {
106 [0x21] = true, -- exclamation mark !
107 [0x3F] = true, -- question mark ?
108 [0x203C] = true, -- double exclamation mark ‼
109 [0x203D] = true, -- interrobang ‽
110 [0x2047] = true, -- double question mark ⁇
111 [0x2048] = true, -- question exclamation mark ⁈
112 [0x2049] = true -- exclamation question mark ⁉
113 }
114
115 -- from nodes-tst.lua, adapted
116 local function somespace(n)
117 if n then
118 local id, subtype = n.id, n.subtype
119 if id == glue_code then
120 -- it is dangerous to remove all the type of glue
121 return removable_skip[subtype]
122 elseif id == kern_code then
123 -- remove only user's kern
124 return subtype == userkern
125 elseif id == glyph_code then
126 return space_chars[n.char]
127 end
128 end
129 end
130
131 local function someleftbracket(n)
132 if n then
133 local id = n.id
134 if id == glyph_code then
135 return left_bracket_chars[n.char]
136 end
137 end
138 end
139
140 local function somerightbracket(n)
141 if n then
142 local id = n.id
143 if id == glyph_code then
144 return right_bracket_chars[n.char]
145 end
146 end
147 end
148
149 local function question_exclamation_sequence(n1, n2)
150 if n1 and n2 then
151 local id1 = n1.id
152 local id2 = n2.id
153 if id1 == glyph_code and id2 == glyph_code then
154 return question_exclamation_chars[n1.char] and question_exclamation_chars[n2.char]
155 end
156 end
157 end
158
159 -- idem
160 local function somepenalty(n, value)
161 if n then
162 local id = n.id
163 if id == penalty_code then
164 if value then
165 return n.penalty == value
166 else
167 return true
168 end
169 end
170 end
171 end
172
173 local punct_attr = new_attribute("polyglossia_punct")
174
175 local lang_id = {}
176 local lang_counter = 0
177 local left_space = {}
178 local right_space = {}
179
180 local function ensure_lang_id(lang)
181 if not lang_id[lang] then
182 lang_counter = lang_counter + 1
183 lang_id[lang] = lang_counter
184 end
185 return lang_id[lang]
186 end
187
188 local function clear_spaced_characters(lang)
189 local id = ensure_lang_id(lang)
190 left_space[id] = {}
191 right_space[id] = {}
192 end
193
194 local function illegal_unit(unit)
195 if unit then
196 texio.write_nl('Illegal spacing unit "'..unit..'".')
197 else
198 texio.write_nl('Spacing unit is a nil value.')
199 end
200 end
201
202 local function add_left_spaced_character(lang, char, kern, unit, rubber)
203 -- The parameter kern is a number meant as a fraction of the unit.
204 -- The unit can be "quad" (1em) or "space" (interword space).
205 -- The parameter rubber is a Boolean value indicating if the inserted space is
206 -- stretchable and shrinkable (only relevant if the unit is "space").
207 local id = ensure_lang_id(lang)
208 if unit == "quad" or unit == "space" then
209 left_space[id][char] = {}
210 left_space[id][char]["kern"] = kern
211 left_space[id][char]["unit"] = unit
212 left_space[id][char]["rubber"] = rubber
213 else
214 illegal_unit(unit)
215 end
216 end
217
218 local function add_right_spaced_character(lang, char, kern, unit, rubber)
219 local id = ensure_lang_id(lang)
220 if unit == "quad" or unit == "space" then
221 right_space[id][char] = {}
222 right_space[id][char]["kern"] = kern
223 right_space[id][char]["unit"] = unit
224 right_space[id][char]["rubber"] = rubber
225 else
226 illegal_unit(unit)
227 end
228 end
229
230 -- from typo-spa.lua, adapted
231 local function process(head)
232 local current = head
233 while current do
234 local id = current.id
235 if id == glyph_code then
236 local attr = has_attribute(current, punct_attr)
237 if attr then
238 local char, leftspace, rightspace
239 if current.char <= 0x10FFFF then -- greater values may cause problems with utf8.char
240 char = utf8.char(current.char)
241 leftspace = left_space[attr][char]
242 rightspace = right_space[attr][char]
243 end
244 if leftspace or rightspace then
245 local fontparameters = fonts.hashes.parameters[current.font]
246 local unit, stretch, shrink, spacing_node
247 if leftspace and fontparameters then
248 local prev = getprev(current)
249 local space_exception = false
250 if prev then
251 -- do not add space after left (opening) bracket and between question/exclamation marks
252 space_exception = someleftbracket(prev) or question_exclamation_sequence(prev, current)
253 -- TODO: there is a question here: do we override a preceding space or not?...
254 while somespace(prev) do
255 head = remove_node(head, prev)
256 prev = getprev(current)
257 end
258 if somepenalty(prev, 10000) then
259 head = remove_node(head, prev)
260 end
261 end
262 if leftspace.unit == "quad" then
263 unit = fontparameters.quad
264 spacing_node = get_kern_node(leftspace.kern*unit)
265 elseif leftspace.unit == "space" then
266 unit = fontparameters.space
267 if leftspace.rubber then
268 stretch = leftspace.kern*fontparameters.space_stretch
269 shrink = leftspace.kern*fontparameters.space_shrink
270 spacing_node = get_glue_node(leftspace.kern*unit, stretch, shrink)
271 head = insert_node_before(head, current, get_penalty_node())
272 else
273 spacing_node = get_kern_node(leftspace.kern*unit)
274 end
275 end
276 if not space_exception then
277 head = insert_node_before(head, current, spacing_node)
278 end
279 end
280 if rightspace and fontparameters then
281 local next = getnext(current)
282 local space_exception = false
283 if next then
284 -- do not add space before right (closing) bracket
285 space_exception = somerightbracket(next)
286 local nextnext = getnext(next)
287 if somepenalty(next, 10000) and somespace(nextnext) then
288 head, next = remove_node(head, next)
289 end
290 while somespace(next) do
291 head, next = remove_node(head, next)
292 end
293 end
294 if rightspace.unit == "quad" then
295 unit = fontparameters.quad
296 spacing_node = get_kern_node(rightspace.kern*unit)
297 elseif rightspace.unit == "space" then
298 unit = fontparameters.space
299 if rightspace.rubber then
300 stretch = rightspace.kern*fontparameters.space_stretch
301 shrink = rightspace.kern*fontparameters.space_shrink
302 spacing_node = get_glue_node(rightspace.kern*unit, stretch, shrink)
303 if not space_exception then
304 head, current = insert_node_after(head, current, get_penalty_node())
305 end
306 else
307 spacing_node = get_kern_node(rightspace.kern*unit)
308 end
309 end
310 if not space_exception then
311 head, current = insert_node_after(head, current, spacing_node)
312 end
313 end
314 end
315 end
316 elseif id == math_code then
317 -- warning: this is a feature of luatex > 0.76
318 current = end_of_math(current) -- weird, can return nil .. no math end?
319 end
320 current = getnext(current) -- no error even if current is nil
321 end
322 return head
323 end
324
325 local function activate(lang)
326 local id = ensure_lang_id(lang)
327 -- We set the punctuation attribute to a language id here. This is
328 -- important to be able to intermix languages with different spacings
329 -- in one paragraph.
330 tex.setattribute(punct_attr, id)
331 end
332
333 luatexbase.add_to_callback("pre_shaping_filter",process,"polyglossia-punct.process")
334
335 local function deactivate()
336 tex.setattribute(punct_attr, -0x7FFFFFFF) -- this value means "unset"
337 -- Though it would make compilation slightly faster, it is not possible to
338 -- safely uncomment the following lines. Imagine the following case: you
339 -- start a paragraph by some spaced punctuation text, then, in the same
340 -- paragraph, you change the language to something else, and thus call the
341 -- following lines. This means that, at the end of the paragraph, the
342 -- function won't be in the callback, so the beginning of the paragraph
343 -- won't be processed by it.
344 -- if priority_in_callback(callback_name, "polyglossia-punct.process") then
345 -- remove_from_callback(callback_name, "polyglossia-punct.process")
346 -- end
347 end
348
349 polyglossia.activate_punct = activate
350 polyglossia.deactivate_punct = deactivate
351 polyglossia.add_left_spaced_character = add_left_spaced_character
352 polyglossia.add_right_spaced_character = add_right_spaced_character
353 polyglossia.clear_spaced_characters = clear_spaced_characters