Debian Codegraph @

texmf-dist/tex/generic/babel/tests-babel-bidi-basic.lua

raw
1 --
2 -- This is file `babel-bidi-basic.lua',
3 -- generated with the docstrip utility.
4 --
5 -- The original source files were:
6 --
7 -- babel.dtx (with options: `basic')
8 --
9 --
10 -- Copyright (C) 2012-2025 Javier Bezos and Johannes L. Braams.
11 -- Copyright (C) 1989-2012 Johannes L. Braams and
12 -- any individual authors listed elsewhere in this file.
13 -- All rights reserved.
14 --
15 --
16 -- This file is part of the Babel system.
17 -- --------------------------------------
18 --
19 -- It may be distributed and/or modified under the
20 -- conditions of the LaTeX Project Public License, either version 1.3
21 -- of this license or (at your option) any later version.
22 -- The latest version of this license is in
23 -- http://www.latex-project.org/lppl.txt
24 -- and version 1.3 or later is part of all distributions of LaTeX
25 -- version 2003/12/01 or later.
26 --
27 -- This work has the LPPL maintenance status "maintained".
28 --
29 -- The Current Maintainer of this work is Javier Bezos.
30 --
31 -- The list of derived (unpacked) files belonging to the distribution
32 -- and covered by LPPL is defined by the unpacking scripts (with
33 -- extension |.ins|) which are part of the distribution.
34 --
35 -- e.g., Babel.fontmap[1][<prefontid>]=<dirfontid>
36
37 Babel.fontmap = Babel.fontmap or {}
38 Babel.fontmap[0] = {} -- l
39 Babel.fontmap[1] = {} -- r
40 Babel.fontmap[2] = {} -- al/an
41
42 -- To cancel mirroring. Also OML, OMS, U?
43 Babel.symbol_fonts = Babel.symbol_fonts or {}
44 Babel.symbol_fonts[font.id('tenln')] = true
45 Babel.symbol_fonts[font.id('tenlnw')] = true
46 Babel.symbol_fonts[font.id('tencirc')] = true
47 Babel.symbol_fonts[font.id('tencircw')] = true
48
49 Babel.bidi_enabled = true
50 Babel.mirroring_enabled = true
51
52 require('babel-data-bidi.lua')
53
54 local characters = Babel.characters
55 local ranges = Babel.ranges
56
57 local DIR = node.id('dir')
58 local GLYPH = node.id('glyph')
59
60 local function insert_implicit(head, state, outer)
61 local new_state = state
62 if state.sim and state.eim and state.sim ~= state.eim then
63 dir = ((outer == 'r') and 'TLT' or 'TRT') -- i.e., reverse
64 local d = node.new(DIR)
65 d.dir = '+' .. dir
66 node.insert_before(head, state.sim, d)
67 local d = node.new(DIR)
68 d.dir = '-' .. dir
69 node.insert_after(head, state.eim, d)
70 end
71 new_state.sim, new_state.eim = nil, nil
72 return head, new_state
73 end
74
75 local function insert_numeric(head, state)
76 local new
77 local new_state = state
78 if state.san and state.ean and state.san ~= state.ean then
79 local d = node.new(DIR)
80 d.dir = '+TLT'
81 _, new = node.insert_before(head, state.san, d)
82 if state.san == state.sim then state.sim = new end
83 local d = node.new(DIR)
84 d.dir = '-TLT'
85 _, new = node.insert_after(head, state.ean, d)
86 if state.ean == state.eim then state.eim = new end
87 end
88 new_state.san, new_state.ean = nil, nil
89 return head, new_state
90 end
91
92 local function glyph_not_symbol_font(node)
93 if node.id == GLYPH then
94 return not Babel.symbol_fonts[node.font]
95 else
96 return false
97 end
98 end
99
100 -- TODO - \hbox with an explicit dir can lead to wrong results
101 -- <R \hbox dir TLT{<R>}> and <L \hbox dir TRT{<L>}>. A small attempt
102 -- was made to improve the situation, but the problem is the 3-dir
103 -- model in babel/Unicode and the 2-dir model in LuaTeX don't fit
104 -- well.
105
106 function Babel.bidi(head, ispar, hdir)
107 local d -- d is used mainly for computations in a loop
108 local prev_d = ''
109 local new_d = false
110
111 local nodes = {}
112 local outer_first = nil
113 local inmath = false
114
115 local glue_d = nil
116 local glue_i = nil
117
118 local has_en = false
119 local first_et = nil
120
121 local has_hyperlink = false
122
123 local ATDIR = Babel.attr_dir
124 local attr_d
125
126 local save_outer
127 local temp = node.get_attribute(head, ATDIR)
128 if temp then
129 temp = temp & 0x3
130 save_outer = (temp == 0 and 'l') or
131 (temp == 1 and 'r') or
132 (temp == 2 and 'al')
133 elseif ispar then -- Or error? Shouldn't happen
134 save_outer = ('TRT' == tex.pardir) and 'r' or 'l'
135 else -- Or error? Shouldn't happen
136 save_outer = ('TRT' == hdir) and 'r' or 'l'
137 end
138 -- when the callback is called, we are just _after_ the box,
139 -- and the textdir is that of the surrounding text
140 -- if not ispar and hdir ~= tex.textdir then
141 -- save_outer = ('TRT' == hdir) and 'r' or 'l'
142 -- end
143 local outer = save_outer
144 local last = outer
145 -- 'al' is only taken into account in the first, current loop
146 if save_outer == 'al' then save_outer = 'r' end
147
148 local fontmap = Babel.fontmap
149
150 for item in node.traverse(head) do
151
152 -- In what follows, #node is the last (previous) node, because the
153 -- current one is not added until we start processing the neutrals.
154
155 -- three cases: glyph, dir, otherwise
156 if glyph_not_symbol_font(item)
157 or (item.id == 7 and item.subtype == 2) then
158
159 if node.get_attribute(item, ATDIR) == 128 then goto nextnode end
160
161 local d_font = nil
162 local item_r
163 if item.id == 7 and item.subtype == 2 then
164 item_r = item.replace -- automatic discs have just 1 glyph
165 else
166 item_r = item
167 end
168
169 local chardata = characters[item_r.char]
170 d = chardata and chardata.d or nil
171 if not d or d == 'nsm' then
172 for nn, et in ipairs(ranges) do
173 if item_r.char < et[1] then
174 break
175 elseif item_r.char <= et[2] then
176 if not d then d = et[3]
177 elseif d == 'nsm' then d_font = et[3]
178 end
179 break
180 end
181 end
182 end
183 d = d or 'l'
184
185 -- A short 'pause' in bidi for mapfont
186 d_font = d_font or d
187 d_font = (d_font == 'l' and 0) or
188 (d_font == 'nsm' and 0) or
189 (d_font == 'r' and 1) or
190 (d_font == 'al' and 2) or
191 (d_font == 'an' and 2) or nil
192 if d_font and fontmap and fontmap[d_font][item_r.font] then
193 item_r.font = fontmap[d_font][item_r.font]
194 end
195
196 if new_d then
197 table.insert(nodes, {nil, (outer == 'l') and 'l' or 'r', nil})
198 if inmath then
199 attr_d = 0
200 else
201 attr_d = node.get_attribute(item, ATDIR)
202 attr_d = attr_d & 0x3
203 end
204 if attr_d == 1 then
205 outer_first = 'r'
206 last = 'r'
207 elseif attr_d == 2 then
208 outer_first = 'r'
209 last = 'al'
210 else
211 outer_first = 'l'
212 last = 'l'
213 end
214 outer = last
215 has_en = false
216 first_et = nil
217 new_d = false
218 end
219
220 if glue_d then
221 if (d == 'l' and 'l' or 'r') ~= glue_d then
222 table.insert(nodes, {glue_i, 'on', nil})
223 end
224 glue_d = nil
225 glue_i = nil
226 end
227
228 elseif item.id == DIR then
229 d = nil
230
231 if head ~= item then new_d = true end
232
233 elseif item.id == node.id'glue' and item.subtype == 13 then
234 glue_d = d
235 glue_i = item
236 d = nil
237
238 elseif item.id == node.id'math' then
239 inmath = (item.subtype == 0)
240
241 elseif item.id == 8 and item.subtype == 19 then
242 has_hyperlink = true
243
244 else
245 d = nil
246 end
247
248 -- AL <= EN/ET/ES -- W2 + W3 + W6
249 if last == 'al' and d == 'en' then
250 d = 'an' -- W3
251 elseif last == 'al' and (d == 'et' or d == 'es') then
252 d = 'on' -- W6
253 end
254
255 -- EN + CS/ES + EN -- W4
256 if d == 'en' and #nodes >= 2 then
257 if (nodes[#nodes][2] == 'es' or nodes[#nodes][2] == 'cs')
258 and nodes[#nodes-1][2] == 'en' then
259 nodes[#nodes][2] = 'en'
260 end
261 end
262
263 -- AN + CS + AN -- W4 too, because uax9 mixes both cases
264 if d == 'an' and #nodes >= 2 then
265 if (nodes[#nodes][2] == 'cs')
266 and nodes[#nodes-1][2] == 'an' then
267 nodes[#nodes][2] = 'an'
268 end
269 end
270
271 -- ET/EN -- W5 + W7->l / W6->on
272 if d == 'et' then
273 first_et = first_et or (#nodes + 1)
274 elseif d == 'en' then
275 has_en = true
276 first_et = first_et or (#nodes + 1)
277 elseif first_et then -- d may be nil here !
278 if has_en then
279 if last == 'l' then
280 temp = 'l' -- W7
281 else
282 temp = 'en' -- W5
283 end
284 else
285 temp = 'on' -- W6
286 end
287 for e = first_et, #nodes do
288 if glyph_not_symbol_font(nodes[e][1]) then nodes[e][2] = temp end
289 end
290 first_et = nil
291 has_en = false
292 end
293
294 -- Force mathdir in math if ON (currently works as expected only
295 -- with 'l')
296
297 if inmath and d == 'on' then
298 d = ('TRT' == tex.mathdir) and 'r' or 'l'
299 end
300
301 if d then
302 if d == 'al' then
303 d = 'r'
304 last = 'al'
305 elseif d == 'l' or d == 'r' then
306 last = d
307 end
308 prev_d = d
309 table.insert(nodes, {item, d, outer_first})
310 end
311
312 -- node.set_attribute(item, ATDIR, 128)
313 outer_first = nil
314
315 ::nextnode::
316
317 end -- for each node
318
319 -- TODO -- repeated here in case EN/ET is the last node. Find a
320 -- better way of doing things:
321 if first_et then -- dir may be nil here !
322 if has_en then
323 if last == 'l' then
324 temp = 'l' -- W7
325 else
326 temp = 'en' -- W5
327 end
328 else
329 temp = 'on' -- W6
330 end
331 for e = first_et, #nodes do
332 if glyph_not_symbol_font(nodes[e][1]) then nodes[e][2] = temp end
333 end
334 end
335
336 -- dummy node, to close things
337 table.insert(nodes, {nil, (outer == 'l') and 'l' or 'r', nil})
338
339 --------------- NEUTRAL -----------------
340
341 outer = save_outer
342 last = outer
343
344 local first_on = nil
345
346 for q = 1, #nodes do
347 local item
348
349 local outer_first = nodes[q][3]
350 outer = outer_first or outer
351 last = outer_first or last
352
353 texio.write_nl('****')
354 texio.write_nl(outer)
355 if nodes[q][1] then
356 texio.write_nl('char = ' .. nodes[q][1].char)
357 texio.write_nl('dirtext = ' .. (node.get_attribute(nodes[q][1], ATDIR)
358 & 0x3))
359 texio.write(', dir = ' .. nodes[q][2])
360 texio.write(', outer = ' .. (nodes[q][3] or '?'))
361 else
362 texio.write_nl('Ø')
363 end
364 -- outer = 'l'
365
366
367 local d = nodes[q][2]
368 if d == 'an' or d == 'en' then d = 'r' end
369 if d == 'cs' or d == 'et' or d == 'es' then d = 'on' end --- W6
370
371 if d == 'on' then
372 first_on = first_on or q
373 elseif first_on then
374 if last == d then
375 temp = d
376 else
377 temp = outer
378 end
379 for r = first_on, q - 1 do
380 nodes[r][2] = temp
381 item = nodes[r][1] -- MIRRORING
382 if Babel.mirroring_enabled and glyph_not_symbol_font(item)
383 and temp == 'r' and characters[item.char] then
384 texio.write_nl('==== ' .. item.char)
385 texio.write_nl('dir = ' .. node.get_attribute(item, ATDIR))
386 texio.write_nl('dirtext = ' .. (node.get_attribute(item, ATDIR)
387 & 0x3))
388 local font_mode = ''
389 if item.font > 0 and font.fonts[item.font].properties then
390 font_mode = font.fonts[item.font].properties.mode
391 end
392 if font_mode ~= 'harf' and font_mode ~= 'plug' and
393 node.get_attribute(item, ATDIR) & 0x3 ~= 0 then
394 texio.write_nl('change ' .. item.char .. ' -> '
395 .. (characters[item.char].m
396 or item.char))
397 item.char = characters[item.char].m or item.char
398 end
399 end
400 end
401 first_on = nil
402 end
403
404 if d == 'r' or d == 'l' then last = d end
405 end
406
407 -------------- IMPLICIT, REORDER ----------------
408
409 outer = save_outer
410 last = outer
411
412 local state = {}
413 state.has_r = false
414
415 for q = 1, #nodes do
416
417 local item = nodes[q][1]
418
419 outer = nodes[q][3] or outer
420
421 local d = nodes[q][2]
422
423 if d == 'nsm' then d = last end -- W1
424 if d == 'en' then d = 'an' end
425 local isdir = (d == 'r' or d == 'l')
426
427 if outer == 'l' and d == 'an' then
428 state.san = state.san or item
429 state.ean = item
430 elseif state.san then
431 head, state = insert_numeric(head, state)
432 end
433
434 if outer == 'l' then
435 if d == 'an' or d == 'r' then -- im -> implicit
436 if d == 'r' then state.has_r = true end
437 state.sim = state.sim or item
438 state.eim = item
439 elseif d == 'l' and state.sim and state.has_r then
440 head, state = insert_implicit(head, state, outer)
441 elseif d == 'l' then
442 state.sim, state.eim, state.has_r = nil, nil, false
443 end
444 else
445 if d == 'an' or d == 'l' then
446 if nodes[q][3] then -- nil except after an explicit dir
447 state.sim = item -- so we move sim 'inside' the group
448 else
449 state.sim = state.sim or item
450 end
451 state.eim = item
452 elseif d == 'r' and state.sim then
453 head, state = insert_implicit(head, state, outer)
454 elseif d == 'r' then
455 state.sim, state.eim = nil, nil
456 end
457 end
458
459 if isdir then
460 last = d -- Don't search back - best save now
461 elseif d == 'on' and state.san then
462 state.san = state.san or item
463 state.ean = item
464 end
465
466 end
467
468 head = node.prev(head) or head
469 --- FIXES ---
470 if has_hyperlink then
471 local flag, linking = 0, 0
472 for item in node.traverse(head) do
473 if item.id == DIR then
474 if item.dir == '+TRT' or item.dir == '+TLT' then
475 flag = flag + 1
476 elseif item.dir == '-TRT' or item.dir == '-TLT' then
477 flag = flag - 1
478 end
479 elseif item.id == 8 and item.subtype == 19 then
480 linking = flag
481 elseif item.id == 8 and item.subtype == 20 then
482 if linking > 0 then
483 if item.prev.id == DIR and
484 (item.prev.dir == '-TRT' or item.prev.dir == '-TLT') then
485 d = node.new(DIR)
486 d.dir = item.prev.dir
487 node.remove(head, item.prev)
488 node.insert_after(head, item, d)
489 end
490 end
491 linking = 0
492 end
493 end
494 end
495
496 for item in node.traverse_id(10, head) do
497 local p = item
498 local flag = false
499 while p.prev and p.prev.id == 14 do
500 flag = true
501 p = p.prev
502 end
503 if flag then
504 node.insert_before(head, p, node.copy(item))
505 node.remove(head,item)
506 end
507 end
508
509 return head
510 end
511 -- Make sure anything is marked as 'bidi done' (including nodes inserted
512 -- after the babel algorithm). 128 = 1000 0000.
513 function Babel.unset_atdir(head)
514 local ATDIR = Babel.attr_dir
515 for item in node.traverse(head) do
516 node.set_attribute(item, ATDIR, 128)
517 end
518 return head
519 end