Debian Codegraph @

texmf-dist/tex/luatex/unibidi-lua/unibidi-lua.lua

raw
1 --[[
2 version = 1.0, 2026-05-19
3 author = Hans Hagen, PRAGMA-ADE, Hasselt NL, Udi Fogiel
4 copyright = PRAGMA ADE / ConTeXt Development Team
5 license = GPL v2.0
6 comment = Unicode bidi (sort of)
7
8
9 This is a follow up on typo-uda which itself is a follow up on t-bidi by Khaled Hosny which
10 in turn is based on minibidi.c from Arabeyes. This is a further optimizations, as well as
11 an update on some recent unicode bidi developments. There is (and will) also be more control
12 added. As a consequence this module is somewhat slower than its precursor which itself is
13 slower than the one-pass bidi handler. This is also a playground and I might add some plugin
14 support. However, in the meantime performance got a bit better and this third variant is again
15 some 10% faster than the second variant.
16
17 todo (cf html):
18
19 * normal The element does not offer a additional level of embedding with respect to the bidirectional algorithm. For inline elements implicit reordering works across element boundaries.
20 * embed If the element is inline, this value opens an additional level of embedding with respect to the bidirectional algorithm. The direction of this embedding level is given by the direction property.
21 * bidi-override For inline elements this creates an override. For block container elements this creates an override for inline-level descendants not within another block container element. This means that inside the element, reordering is strictly in sequence according to the direction property; the implicit part of the bidirectional algorithm is ignored.
22 * isolate This keyword indicates that the element's container directionality should be calculated without considering the content of this element. The element is therefore isolated from its siblings. When applying its bidirectional-resolution algorithm, its container element treats it as one or several U+FFFC Object Replacement Character, i.e. like an image.
23 * isolate-override This keyword applies the isolation behavior of the isolate keyword to the surrounding content and the override behavior o f the bidi-override keyword to the inner content.
24 * plaintext This keyword makes the elements directionality calculated without considering its parent bidirectional state or the value of the direction property. The directionality is calculated using the P2 and P3 rules of the Unicode Bidirectional Algorithm.
25 This value allows to display data which has already formatted using a tool following the Unicode Bidirectional Algorithm.
26
27 todo: check for introduced errors
28 todo: reuse list, we have size, so we can just change values (and auto allocate when not there)
29 todo: reuse the stack
30 todo: no need for a max check
31 todo: collapse bound similar ranges (not ok yet)
32 todo: combine some sweeps
33 todo: removing is not needed when we inject at the same spot (only change the dir property)
34 todo: isolated runs (isolating runs are similar to bidi=local in the basic analyzer)
35
36 todo: check unicode addenda (from the draft):
37
38 Added support for canonical equivalents in BD16.
39 Changed logic in N0 to not check forwards for context in the case of enclosed text opposite the embedding direction.
40 Major extension of the algorithm to allow for the implementation of directional isolates and the introduction of new isolate-related values to the Bidi_Class property.
41 Adds BD8, BD9, BD10, BD11, BD12, BD13, BD14, BD15, and BD16, Sections 2.4 and 2.5, and Rules X5a, X5b, X5c and X6a.
42 Extensively revises Section 3.3.2, Explicit Levels and Directions and its existing X rules to formalize the algorithm for matching a PDF with the embedding or override initiator whose scope it terminates.
43 Moves Rules X9 and X10 into a separate new Section 3.3.3, Preparations for Implicit Processing.
44 Modifies Rule X10 to make the isolating run sequence the unit to which subsequent rules are applied.
45 Modifies Rule W1 to change an NSM preceded by an isolate initiator or PDI into ON.
46 Adds Rule N0 and makes other changes to Section 3.3.5, Resolving Neutral and Isolate Formatting Types to resolve bracket pairs to the same level.
47
48 This file is a derivative of typo-duc.lua from the ConTeXt project.
49
50 ]]--
51
52 local setmetatable = setmetatable
53 local utfcodes = utf8.codes
54 local utfchar = utf8.char
55 local concat = table.concat
56
57 local data = require("unibidi-lua-data")
58 local directiondata = data.directions
59 local mirrordata = data.mirrors
60 local brackettypedata = data.brackets
61
62 local lefttoright_code = 0
63 local righttoleft_code = 1
64
65 local maximum_stack = 125
66 local max_bracket_stack = 63
67
68 local analyze_fences = true
69 local remove_mode = 2
70 local removelevels = {
71 none = 0,
72 controls = 1,
73 full = 2,
74 }
75 local apply_mirror = true
76 local apply_nsm = true
77
78 -- strong (old):
79 --
80 -- l : left to right
81 -- r : right to left
82 -- lro : left to right override
83 -- rlo : left to left override
84 -- lre : left to right embedding
85 -- rle : left to left embedding
86 -- al : right to legt arabic (esp punctuation issues)
87 --
88 -- weak:
89 --
90 -- en : english number
91 -- es : english number separator
92 -- et : english number terminator
93 -- an : arabic number
94 -- cs : common number separator
95 -- nsm : nonspacing mark
96 -- bn : boundary neutral
97 --
98 -- neutral:
99 --
100 -- b : paragraph separator
101 -- s : segment separator
102 -- ws : whitespace
103 -- on : other neutrals
104 --
105 -- interesting: this is indeed better (and more what we expect i.e. we already use this split
106 -- in the old original (also these isolates)
107 --
108 -- strong (new):
109 --
110 -- l : left to right
111 -- r : right to left
112 -- al : right to left arabic (esp punctuation issues)
113 --
114 -- explicit: (new)
115 --
116 -- lro : left to right override
117 -- rlo : left to left override
118 -- lre : left to right embedding
119 -- rle : left to left embedding
120 -- pdf : pop dir format
121 -- lri : left to right isolate
122 -- rli : left to left isolate
123 -- fsi : first string isolate
124 -- pdi : pop directional isolate
125
126 local whitespace = {
127 lre = true,
128 rle = true,
129 lro = true,
130 rlo = true,
131 pdf = true,
132 bn = true,
133 ws = true,
134 lri = true,
135 rli = true,
136 fsi = true,
137 pdi = true,
138 }
139
140 local b_s_ws_on = {
141 b = true,
142 s = true,
143 ws = true,
144 on = true
145 }
146
147 local isolate_initiator = {
148 lri = true,
149 rli = true,
150 fsi = true,
151 }
152
153 -- keeping the list and overwriting doesn't save much runtime, only a few percent
154 -- char is only used for mirror, so in fact we can as well only store it for
155 -- glyphs only
156 --
157 -- tracking what direction is used and skipping tests is not faster (extra kind of
158 -- compensates gain)
159
160 local mt_space = { __index = { char = 0x0020, direction = "ws", original = "ws", level = 0, skip = 0 } }
161 local mt_lre = { __index = { char = 0x202A, direction = "lre", original = "lre", level = 0, skip = 0 } }
162 local mt_rle = { __index = { char = 0x202B, direction = "rle", original = "rle", level = 0, skip = 0 } }
163 local mt_pdf = { __index = { char = 0x202C, direction = "pdf", original = "pdf", level = 0, skip = 0 } }
164 local mt_object = { __index = { char = 0xFFFC, direction = "on", original = "on", level = 0, skip = 0 } }
165
166 local stack = setmetatable({}, { __index = function(t, k)
167 local v = {}
168 t[k] = v
169 return v
170 end }) -- shared
171 local list = { } -- shared
172
173 local function build_list_str(str)
174 local size = 0
175 for _, chr in utfcodes(str) do
176 size = size + 1
177 local dir = directiondata[chr]
178 list[size] = { char = chr, direction = dir, original = dir, level = 0, original_index = size }
179 end
180 return list, size
181 end
182
183 local function build_list_codepoints(codepoints)
184 local size = #codepoints
185 for i = 1, size do
186 local chr = codepoints[i]
187 local dir = directiondata[chr]
188 list[i] = { char = chr, direction = dir, original = dir, level = 0, original_index = i }
189 end
190 return list, size
191 end
192
193 -- new
194
195 -- we could support ( ] and [ ) and such ...
196
197 -- ש ) ל ( א 0-0
198 -- ש ( ל ] א 0-0
199 -- ש ( ל ) א 2-4
200 -- ש ( ל [ א ) כ ] 2-6
201 -- ש ( ל ] א ) כ 2-6
202 -- ש ( ל ) א ) כ 2-4
203 -- ש ( ל ( א ) כ 4-6
204 -- ש ( ל ( א ) כ ) 2-8,4-6
205 -- ש ( ל [ א ] כ ) 2-8,4-6
206
207 local fencestack = setmetatable({}, { __index = function(t, k)
208 local v = {}
209 t[k] = v
210 return v
211 end })
212 local function closing_equivalent(a, b)
213 return a == b
214 or (a == 0x3009 and b == 0x232A)
215 or (a == 0x232A and b == 0x3009)
216 end
217
218 local function resolve_fences(list,size,start,limit)
219 local seq_level = list[start].level
220 local nofstack = 0
221 local overflow = false
222 local nofpaired = 0
223
224 -- pass 1: find bracket pairs (BD16), only at seq_level
225 for i=start,limit do
226 local entry = list[i]
227 if entry.level == seq_level and entry.direction == "on" then
228 local char = entry.char
229 local mirror = mirrordata[char]
230 if mirror then
231 local class = brackettypedata[char]
232 entry.class = class
233 if class == "o" then
234 if nofstack < max_bracket_stack then
235 nofstack = nofstack + 1
236 local stacktop = fencestack[nofstack]
237 stacktop[1] = mirror
238 stacktop[2] = i
239 else
240 overflow = true
241 end
242 elseif class == "c" and nofstack > 0 and not overflow then
243 -- search stack for matching open bracket
244 for k=nofstack,1,-1 do
245 if closing_equivalent(fencestack[k][1], char) then
246 -- found match at position k
247 list[fencestack[k][2]].paired = i
248 list[i].paired = fencestack[k][2]
249 nofstack = k - 1 -- pop down to just below the match
250 nofpaired = nofpaired + 1
251 break
252 end
253 end
254 end
255 end
256 end
257 end
258
259 if nofpaired == 0 then return end
260
261 -- pass 2: N0 resolution
262 local embedding_dir = seq_level % 2 == 1 and "r" or "l"
263 for i=start,limit do
264 local entry = list[i]
265 if entry.level == seq_level and entry.class == "o" and entry.paired then
266 local open = i
267 local close = entry.paired
268 -- scan inside for strong type, skipping different-level entries
269 local found_embedding = false
270 local found_opposite = false
271 for j=open+1,close-1 do
272 local e = list[j]
273 if e.level == seq_level then
274 local d = e.direction
275 local is_r = (d == "r" or d == "an" or d == "en" or d == "al")
276 local is_l = (d == "l")
277 if is_r or is_l then
278 local this_dir = is_r and "r" or "l"
279 if this_dir == embedding_dir then
280 found_embedding = true
281 break
282 else
283 found_opposite = true
284 end
285 end
286 end
287 end
288
289 local strong_inside = nil
290 if found_embedding then
291 strong_inside = embedding_dir
292 elseif found_opposite then
293 strong_inside = (embedding_dir == "r") and "l" or "r"
294 end
295
296 if strong_inside == nil then
297 -- no strong type inside: leave for N1/N2
298 elseif strong_inside == embedding_dir then
299 -- N0b
300 list[open].direction = embedding_dir
301 list[close].direction = embedding_dir
302 else
303 -- scan before opening bracket for strong context,
304 -- skipping different-level entries
305 local strong_before = nil
306 for j=open-1,1,-1 do
307 local e = list[j]
308 if e.level == seq_level then
309 local d = e.direction
310 if d == "l" then
311 strong_before = "l"
312 break
313 elseif d == "r" or d == "an" or d == "en" then
314 strong_before = "r"
315 break
316 end
317 elseif e.direction == "bn" then
318 -- skip
319 end
320 end
321 if strong_before == nil then
322 strong_before = embedding_dir -- use sos
323 end
324 if strong_before == strong_inside then
325 -- N0c
326 list[open].direction = strong_inside
327 list[close].direction = strong_inside
328 else
329 -- N0d
330 list[open].direction = embedding_dir
331 list[close].direction = embedding_dir
332 end
333 end
334 end
335 end
336
337 -- post-N0: NSMs immediately following a resolved bracket inherit its direction
338 for i=start,limit do
339 local entry = list[i]
340 if entry.level == seq_level and entry.class == "c" and entry.paired then
341 local resolved = entry.direction
342 if resolved == "l" or resolved == "r" then
343 for j=i+1,limit do
344 local next = list[j]
345 if next.level == seq_level and next.original == "nsm" then
346 next.direction = resolved
347 elseif next.level ~= seq_level or next.direction == "bn" then
348 -- skip different-level or bn
349 else
350 break
351 end
352 end
353 end
354 end
355 end
356 end
357
358 -- the action
359
360 local function skip_isolate(list, i, size)
361 local depth = 1
362 i = i + 1
363 while i <= size do
364 local dir = list[i].direction
365 if isolate_initiator[dir] then
366 depth = depth + 1
367 elseif dir == "pdi" then
368 depth = depth - 1
369 if depth == 0 then
370 return i
371 end
372 end
373 i = i + 1
374 end
375 return size + 1 -- no matching PDI; treat rest of paragraph as inside isolate
376 end
377
378 local function get_baselevel(direction,list,size,where)
379 if direction == lefttoright_code or direction == righttoleft_code then
380 return direction
381 end
382 -- P2, P3
383 -- P2: find the first strong character that is not inside an isolate run.
384 local i = 1
385 while i <= size do
386 local entry = list[i]
387 local direction = entry.direction
388 -- P2: skip characters enclosed in an isolate sequence
389 if isolate_initiator[direction] then
390 i = skip_isolate(list, i, size)
391 elseif direction == "pdi" then
392 -- unmatched PDI at the paragraph level; treat as neutral, keep scanning
393 elseif direction == "r" or direction == "al" then
394 return righttoleft_code
395 elseif direction == "l" then
396 return lefttoright_code
397 end
398 i = i + 1
399 end
400 return lefttoright_code
401 end
402
403 local function resolve_explicit(list,size,baselevel)
404 -- X1
405 local level = baselevel
406 local override = "on"
407 local nofstack = 0
408 local valid_isolate_count = 0
409 local overflow_isolate_count = 0
410 local overflow_embedding_count = 0
411 local overflow_isolate_oebc_stack = {}
412 for i=1,size do
413 local entry = list[i]
414 local direction = entry.direction
415 -- X2
416 if direction == "rle" then
417 local new_level = level + (level % 2 == 1 and 2 or 1)
418 if overflow_isolate_count == 0 and overflow_embedding_count == 0 and
419 nofstack < maximum_stack and new_level <= maximum_stack then
420 nofstack = nofstack + 1
421 local stacktop = stack[nofstack]
422 stacktop[1] = level
423 stacktop[2] = override
424 stacktop[3] = false
425 level = new_level
426 override = "on"
427 else
428 overflow_embedding_count = overflow_embedding_count + 1
429 end
430 entry.level = level
431 entry.direction = "bn"
432 entry.remove = true
433 -- X3
434 elseif direction == "lre" then
435 local new_level = level + (level % 2 == 1 and 1 or 2)
436 if overflow_isolate_count == 0 and overflow_embedding_count == 0 and
437 nofstack < maximum_stack and new_level <= maximum_stack then
438 nofstack = nofstack + 1
439 local stacktop = stack[nofstack]
440 stacktop[1] = level
441 stacktop[2] = override
442 stacktop[3] = false
443 level = new_level
444 override = "on"
445 else
446 overflow_embedding_count = overflow_embedding_count + 1
447 end
448 entry.level = level
449 entry.direction = "bn"
450 entry.remove = true
451 -- X4
452 elseif direction == "rlo" then
453 local new_level = level + (level % 2 == 1 and 2 or 1)
454 if overflow_isolate_count == 0 and overflow_embedding_count == 0 and
455 nofstack < maximum_stack and new_level <= maximum_stack then
456 nofstack = nofstack + 1
457 local stacktop = stack[nofstack]
458 stacktop[1] = level
459 stacktop[2] = override
460 stacktop[3] = false
461 level = new_level
462 override = "r"
463 else
464 overflow_embedding_count = overflow_embedding_count + 1
465 end
466 entry.level = level
467 entry.direction = "bn"
468 entry.remove = true
469 -- X5
470 elseif direction == "lro" then
471 local new_level = level + (level % 2 == 1 and 1 or 2)
472 if overflow_isolate_count == 0 and overflow_embedding_count == 0 and
473 nofstack < maximum_stack and new_level <= maximum_stack then
474 nofstack = nofstack + 1
475 local stacktop = stack[nofstack]
476 stacktop[1] = level
477 stacktop[2] = override
478 stacktop[3] = false
479 level = new_level
480 override = "l"
481 else
482 overflow_embedding_count = overflow_embedding_count + 1
483 end
484 entry.level = level
485 entry.direction = "bn"
486 entry.remove = true
487 -- X5a: RLI
488 elseif direction == "rli" then
489 entry.level = level
490 if override ~= "on" then
491 entry.direction = override
492 else
493 entry.direction = "on"
494 end
495 local new_level = level + (level % 2 == 1 and 2 or 1)
496 if overflow_isolate_count == 0 and
497 nofstack < maximum_stack and new_level <= maximum_stack then
498 nofstack = nofstack + 1
499 valid_isolate_count = valid_isolate_count + 1
500 local stacktop = stack[nofstack]
501 stacktop[1] = level
502 stacktop[2] = override
503 stacktop[3] = true
504 stacktop[4] = i
505 level = new_level
506 override = "on"
507 entry.matched_isolate = true
508 else
509 overflow_isolate_count = overflow_isolate_count + 1
510 overflow_isolate_oebc_stack[overflow_isolate_count] = overflow_embedding_count
511 end
512 -- X5b: LRI
513 elseif direction == "lri" then
514 entry.level = level
515 if override ~= "on" then
516 entry.direction = override
517 else
518 entry.direction = "on"
519 end
520 local new_level = level + (level % 2 == 1 and 1 or 2)
521 if overflow_isolate_count == 0 and
522 nofstack < maximum_stack and new_level <= maximum_stack then
523 nofstack = nofstack + 1
524 valid_isolate_count = valid_isolate_count + 1
525 local stacktop = stack[nofstack]
526 stacktop[1] = level
527 stacktop[2] = override
528 stacktop[3] = true
529 stacktop[4] = i
530 level = new_level
531 override = "on"
532 entry.matched_isolate = true
533 else
534 overflow_isolate_count = overflow_isolate_count + 1
535 overflow_isolate_oebc_stack[overflow_isolate_count] = overflow_embedding_count
536 end
537 -- X5c: FSI
538 elseif direction == "fsi" then
539 entry.level = level
540 if override ~= "on" then
541 entry.direction = override
542 else
543 entry.direction = "on"
544 end
545 local fsi_dir = lefttoright_code
546 local depth = 1
547 local j = i + 1
548 while j <= size do
549 local jdir = list[j].direction
550 if isolate_initiator[jdir] then
551 depth = depth + 1
552 elseif jdir == "pdi" then
553 depth = depth - 1
554 if depth == 0 then break end
555 elseif depth == 1 then
556 if jdir == "r" or jdir == "al" then
557 fsi_dir = righttoleft_code
558 break
559 elseif jdir == "l" then
560 fsi_dir = lefttoright_code
561 break
562 end
563 end
564 j = j + 1
565 end
566 local new_level
567 if fsi_dir == righttoleft_code then
568 new_level = level + (level % 2 == 1 and 2 or 1)
569 else
570 new_level = level + (level % 2 == 1 and 1 or 2)
571 end
572 if overflow_isolate_count == 0 and
573 nofstack < maximum_stack and new_level <= maximum_stack then
574 nofstack = nofstack + 1
575 valid_isolate_count = valid_isolate_count + 1
576 local stacktop = stack[nofstack]
577 stacktop[1] = level
578 stacktop[2] = override
579 stacktop[3] = true
580 stacktop[4] = i
581 level = new_level
582 override = "on"
583 entry.matched_isolate = true
584 else
585 overflow_isolate_count = overflow_isolate_count + 1
586 overflow_isolate_oebc_stack[overflow_isolate_count] = overflow_embedding_count
587 end
588 -- X7
589 elseif direction == "pdf" then
590 if overflow_isolate_count > 0 then
591 -- do nothing: stray PDF inside overflow isolate
592 elseif overflow_embedding_count > 0 then
593 overflow_embedding_count = overflow_embedding_count - 1
594 elseif nofstack > 0 and not stack[nofstack][3] then
595 local stacktop = stack[nofstack]
596 level = stacktop[1]
597 override = stacktop[2]
598 nofstack = nofstack - 1
599 end
600 entry.level = level
601 entry.direction = "bn"
602 entry.remove = true
603 -- X6a: PDI
604 elseif direction == "pdi" then
605 if overflow_isolate_count > 0 then
606 overflow_embedding_count = overflow_isolate_oebc_stack[overflow_isolate_count]
607 overflow_isolate_count = overflow_isolate_count - 1
608 elseif valid_isolate_count > 0 then
609 overflow_embedding_count = 0
610 valid_isolate_count = valid_isolate_count - 1
611 while nofstack > 0 and not stack[nofstack][3] do
612 nofstack = nofstack - 1
613 end
614 if nofstack > 0 then
615 local stacktop = stack[nofstack]
616 level = stacktop[1]
617 override = stacktop[2]
618 if stacktop[4] then
619 list[stacktop[4]].paired_index = i
620 entry.paired_index = stacktop[4]
621 end
622 nofstack = nofstack - 1
623 end
624 entry.matched_isolate = true
625 end
626 entry.level = level
627 entry.direction = "on"
628 -- X6
629 else
630 entry.level = level
631 if override ~= "on" and entry.direction ~= "bn" then
632 entry.direction = override
633 end
634 end
635 end
636 -- X8 (reset states and overrides after paragraph)
637 end
638
639 local function resolve_weak(list,size,start,limit,orderbefore,orderafter)
640 local seq_level = list[start].level
641 local function is_isolate(entry)
642 return isolate_initiator[entry.original] or entry.original == "pdi"
643 end
644 -- W1
645 for i=start,limit do
646 local entry = list[i]
647 if entry.level ~= seq_level then -- skip
648 elseif entry.direction == "nsm" then
649 if i == start then
650 entry.direction = orderbefore
651 else
652 local prev_dir = orderbefore
653 for j=i-1,start,-1 do
654 local prev = list[j]
655 if prev.level ~= seq_level then
656 -- skip
657 elseif is_isolate(prev) then
658 prev_dir = "on"
659 break
660 elseif prev.direction ~= "bn" then
661 prev_dir = prev.direction
662 break
663 end
664 end
665 entry.direction = prev_dir
666 end
667 end
668 end
669 -- W2
670 for i=start,limit do
671 local entry = list[i]
672 if entry.level == seq_level and entry.direction == "en" then
673 for j=i-1,start,-1 do
674 local prev = list[j]
675 if prev.level ~= seq_level then
676 -- skip
677 elseif not is_isolate(prev) then
678 local direction = prev.direction
679 if direction == "al" then
680 entry.direction = "an"
681 break
682 elseif direction == "r" or direction == "l" then
683 break
684 end
685 end
686 end
687 end
688 end
689 -- W3
690 for i=start,limit do
691 local entry = list[i]
692 if entry.level == seq_level and entry.direction == "al" then
693 entry.direction = "r"
694 end
695 end
696 -- W4
697 local runner = start + 1
698 while runner <= limit - 1 do
699 local current = list[runner]
700 if current.level ~= seq_level then
701 runner = runner + 1
702 else
703 local direction = current.direction
704 if direction == "es" or direction == "cs" then
705 local next_dir = nil
706 for j=runner+1,limit do
707 if list[j].level == seq_level and list[j].direction ~= "bn" then
708 next_dir = list[j].direction
709 break
710 end
711 end
712 local prev_dir = nil
713 for j=runner-1,start,-1 do
714 if list[j].level == seq_level and list[j].direction ~= "bn" then
715 prev_dir = list[j].direction
716 break
717 end
718 end
719 if direction == "es" then
720 if prev_dir == "en" and next_dir == "en" then
721 current.direction = "en"
722 end
723 elseif direction == "cs" then
724 if prev_dir == "en" and next_dir == "en" then
725 current.direction = "en"
726 elseif prev_dir == "an" and next_dir == "an" then
727 current.direction = "an"
728 end
729 end
730 end
731 runner = runner + 1
732 end
733 end
734 -- W5
735 local i = start
736 while i <= limit do
737 if list[i].level == seq_level and list[i].direction == "et" then
738 local runstart = i
739 local runlimit = i
740 while runlimit <= limit and list[runlimit].level == seq_level and
741 (list[runlimit].direction == "et" or list[runlimit].direction == "bn") do
742 runlimit = runlimit + 1
743 end
744 runlimit = runlimit - 1
745 local has_en_before = false
746 for j=runstart-1,start,-1 do
747 if list[j].level ~= seq_level then -- skip
748 elseif list[j].direction == "en" then
749 has_en_before = true
750 break
751 elseif list[j].direction ~= "bn" then
752 break
753 end
754 end
755 local has_en_after = false
756 for j=runlimit+1,limit do
757 if list[j].level ~= seq_level then -- skip
758 elseif list[j].direction == "en" then
759 has_en_after = true
760 break
761 elseif list[j].direction ~= "bn" then
762 break
763 end
764 end
765 if has_en_before or has_en_after then
766 for j=runstart,runlimit do
767 if list[j].level == seq_level then
768 list[j].direction = "en"
769 end
770 end
771 end
772 i = runlimit
773 end
774 i = i + 1
775 end
776 -- W6
777 for i=start,limit do
778 local entry = list[i]
779 if entry.level ~= seq_level then -- skip
780 else
781 local direction = entry.direction
782 if direction == "es" or direction == "et" or direction == "cs" then
783 entry.direction = "on"
784 end
785 end
786 end
787 -- W7
788 for i=start,limit do
789 local entry = list[i]
790 if entry.level ~= seq_level then -- skip
791 elseif entry.direction == "en" then
792 local prev_strong = orderbefore
793 for j=i-1,start,-1 do
794 local d = list[j].direction
795 if list[j].level ~= seq_level then -- skip
796 elseif d == "l" or d == "r" then
797 prev_strong = d
798 break
799 end
800 end
801 if prev_strong == "l" then
802 entry.direction = "l"
803 end
804 end
805 end
806 end
807
808 local function resolve_neutral(list,size,start,limit,orderbefore,orderafter)
809 local seq_level = list[start].level
810 local i = start
811 while i <= limit do
812 local entry = list[i]
813 if entry.level ~= seq_level then
814 -- skip
815 elseif b_s_ws_on[entry.direction] then
816 local leading_direction, trailing_direction, resolved_direction
817 local runstart = i
818 local runlimit = runstart
819 for j=runstart+1,limit do
820 local jentry = list[j]
821 if jentry.level ~= seq_level then
822 -- skip: inside isolate
823 elseif (isolate_initiator[jentry.original] and jentry.matched_isolate) or
824 (jentry.original == "pdi" and jentry.matched_isolate) then
825 break
826 elseif b_s_ws_on[jentry.direction] then
827 runlimit = j
828 else
829 break
830 end
831 end
832 if runstart == start then
833 leading_direction = orderbefore
834 else
835 leading_direction = orderbefore
836 for j=runstart-1,1,-1 do
837 local prev = list[j]
838 if prev.level ~= seq_level then
839 -- skip: inside isolate
840 elseif (isolate_initiator[prev.original] and prev.matched_isolate) or
841 (prev.original == "pdi" and prev.matched_isolate) then
842 -- transparent: keep scanning
843 elseif prev.direction ~= "bn" then
844 leading_direction = prev.direction
845 if leading_direction == "en" or leading_direction == "an" then
846 leading_direction = "r"
847 end
848 break
849 end
850 end
851 end
852 if runlimit == limit then
853 trailing_direction = orderafter
854 else
855 trailing_direction = orderafter
856 for j=runlimit+1,limit do
857 local next = list[j]
858 if next.level ~= seq_level then
859 -- skip: inside isolate
860 elseif (isolate_initiator[next.original] and next.matched_isolate) or
861 (next.original == "pdi" and next.matched_isolate) then
862 -- isolate boundary, keep scanning
863 elseif next.direction == "bn" then
864 -- skip
865 elseif next.direction == "r" or next.direction == "l"
866 or next.direction == "en" or next.direction == "an" then
867 trailing_direction = next.direction
868 if trailing_direction == "en" or trailing_direction == "an" then
869 trailing_direction = "r"
870 end
871 break
872 end
873 end
874 end
875 if leading_direction == trailing_direction then
876 resolved_direction = leading_direction
877 else
878 resolved_direction = entry.level % 2 == 1 and "r" or "l"
879 end
880 for j=runstart,runlimit do
881 if list[j].level == seq_level then
882 list[j].direction = resolved_direction
883 end
884 end
885 i = runlimit
886 end
887 i = i + 1
888 end
889 end
890
891 local function resolve_implicit(list,size,start,limit,orderbefore,orderafter,baselevel)
892 local seq_level = list[start].level
893 for i=start,limit do
894 local entry = list[i]
895 if entry.level ~= seq_level then
896 -- skip: inside isolate
897 else
898 local level = entry.level
899 local direction = entry.direction
900 if level % 2 ~= 1 then
901 if direction == "r" then
902 entry.level = level + 1
903 elseif direction == "an" or direction == "en" then
904 entry.level = level + 2
905 end
906 else
907 if direction == "l" or direction == "en" or direction == "an" then
908 entry.level = level + 1
909 end
910 end
911 end
912 end
913 end
914
915 local function resolve_levels(list,size,baselevel)
916 local original_levels = {}
917 for i=1,size do
918 original_levels[i] = list[i].level
919 end
920
921 -- Step 1: collect all level runs
922 local runs = {}
923 local pos = 1
924 while pos <= size do
925 while pos <= size and list[pos].direction == "bn" do
926 pos = pos + 1
927 end
928 if pos > size then break end
929 local level = list[pos].level
930 local rstart = pos
931 local rlimit = pos
932 local probe = pos + 1
933 while probe <= size do
934 if list[probe].direction == "bn" then
935 probe = probe + 1
936 elseif list[probe].level == level then
937 rlimit = probe
938 probe = probe + 1
939 else
940 break
941 end
942 end
943 runs[#runs+1] = { start = rstart, limit = rlimit, level = level }
944 pos = rlimit + 1
945 end
946
947 -- Step 2: index runs by starting position
948 local run_starting_at = {}
949 for i,run in ipairs(runs) do
950 run_starting_at[run.start] = i
951 end
952
953 -- Step 3: group into isolating run sequences per BD13
954 local sequences = {}
955 local run_in_seq = {}
956 for i,run in ipairs(runs) do
957 if not run_in_seq[i] then
958 local first_char = list[run.start]
959 if not (first_char.original == "pdi" and first_char.matched_isolate) then
960 local seq = { i }
961 run_in_seq[i] = true
962 while true do
963 local last_run = runs[seq[#seq]]
964 local last_char = list[last_run.limit]
965 if isolate_initiator[last_char.original] and last_char.matched_isolate
966 and last_char.paired_index then
967 local pdi_pos = last_char.paired_index
968 local next_run_i = run_starting_at[pdi_pos]
969 if next_run_i and not run_in_seq[next_run_i] then
970 seq[#seq+1] = next_run_i
971 run_in_seq[next_run_i] = true
972 else
973 break
974 end
975 else
976 break
977 end
978 end
979 sequences[#sequences+1] = seq
980 end
981 end
982 end
983 -- Step 4: process each run with its own sor/eor (same as before)
984 for _,seq in ipairs(sequences) do
985 local first_run = runs[seq[1]]
986 local last_run = runs[seq[#seq]]
987 local level = first_run.level
988 local seq_start = first_run.start
989 local seq_limit = last_run.limit
990
991 -- sor: based on first run
992 local sor_level
993 local first_char = list[seq_start]
994 if first_char.original == "pdi" and first_char.matched_isolate then
995 local initiator_level = baselevel
996 if first_char.paired_index then
997 initiator_level = original_levels[first_char.paired_index]
998 end
999 sor_level = level > initiator_level and level or initiator_level
1000 else
1001 local prev_level = baselevel
1002 for j=seq_start-1,1,-1 do
1003 if list[j].direction ~= "bn" then
1004 prev_level = original_levels[j]
1005 break
1006 end
1007 end
1008 sor_level = level > prev_level and level or prev_level
1009 end
1010
1011 -- eor: based on last run
1012 local eor_level
1013 local last_char = list[seq_limit]
1014 if isolate_initiator[last_char.original] and last_char.matched_isolate then
1015 eor_level = level > baselevel and level or baselevel
1016 elseif isolate_initiator[last_char.original] then
1017 eor_level = baselevel
1018 else
1019 local next_level = baselevel
1020 for j=seq_limit+1,size do
1021 if list[j].direction ~= "bn" then
1022 next_level = original_levels[j]
1023 break
1024 end
1025 end
1026 local next_non_bn = nil
1027 for j=seq_limit+1,size do
1028 if list[j].direction ~= "bn" then
1029 next_non_bn = list[j]
1030 break
1031 end
1032 end
1033 if next_non_bn and next_non_bn.original == "b" then
1034 eor_level = baselevel
1035 else
1036 eor_level = level > next_level and level or next_level
1037 end
1038 end
1039
1040 local orderbefore = sor_level % 2 == 1 and "r" or "l"
1041 local orderafter = eor_level % 2 == 1 and "r" or "l"
1042
1043 -- W and I rules per run, with adjusted orderbefore tracking last strong
1044 resolve_weak(list,size,seq_start,seq_limit,orderbefore,orderafter)
1045 if analyze_fences then
1046 resolve_fences(list,size,seq_start,seq_limit)
1047 end
1048 resolve_neutral(list,size,seq_start,seq_limit,orderbefore,orderafter)
1049 resolve_implicit(list,size,seq_start,seq_limit,orderbefore,orderafter,baselevel)
1050 end
1051
1052 -- L1
1053 for i=1,size do
1054 local entry = list[i]
1055 local direction = entry.original
1056 if direction == "s" or direction == "b" then
1057 entry.level = baselevel
1058 for j=i-1,1,-1 do
1059 local entry = list[j]
1060 if whitespace[entry.original] then
1061 entry.level = baselevel
1062 else
1063 break
1064 end
1065 end
1066 end
1067 end
1068 for i=size,1,-1 do
1069 local entry = list[i]
1070 if whitespace[entry.original] then
1071 entry.level = baselevel
1072 else
1073 break
1074 end
1075 end
1076
1077 -- L4
1078 if apply_mirror then
1079 for i=1,size do
1080 local entry = list[i]
1081 if entry.level % 2 == 1 then
1082 local mirror = mirrordata[entry.char]
1083 if mirror then
1084 entry.mirror = mirror
1085 end
1086 end
1087 end
1088 end
1089 end
1090
1091 local function is_removed(entry)
1092 return entry.remove_always
1093 or (remove_mode > 0 and entry.remove)
1094 or (remove_mode > 1 and entry.original == "bn")
1095 end
1096
1097 local function reorder_str(list,size)
1098 -- L2 actual
1099 local maxlevel = 0
1100 for i = 1, size do
1101 if not is_removed(list[i]) then
1102 local l = list[i].level
1103 if l > maxlevel then
1104 maxlevel = l
1105 end
1106 end
1107 end
1108 for level = maxlevel, 1, -1 do
1109 local i = 1
1110 while i <= size do
1111 if not is_removed(list[i]) and list[i].level >= level then
1112 local start = i
1113 -- find end of run, skipping removed entries
1114 local stop = i
1115 i = i + 1
1116 while i <= size do
1117 if is_removed(list[i]) then
1118 i = i + 1 -- skip removed, keep looking
1119 elseif list[i].level >= level then
1120 stop = i
1121 i = i + 1
1122 else
1123 break
1124 end
1125 end
1126 -- reverse between start and stop, skipping removed entries
1127 local lo, hi = start, stop
1128 while lo < hi do
1129 -- advance lo past removed
1130 while lo < hi and is_removed(list[lo]) do
1131 lo = lo + 1
1132 end
1133 -- retreat hi past removed
1134 while lo < hi and is_removed(list[hi]) do
1135 hi = hi - 1
1136 end
1137 if lo < hi then
1138 list[lo], list[hi] = list[hi], list[lo]
1139 lo = lo + 1
1140 hi = hi - 1
1141 end
1142 end
1143 else
1144 i = i + 1
1145 end
1146 end
1147 end
1148 end
1149
1150 local function reorder_nsm(list,size,remove_mode)
1151 -- L3
1152 local i = 1
1153 while i <= size do
1154 -- skip removed entries
1155 while i <= size and is_removed(list[i]) do
1156 i = i + 1
1157 end
1158 if i <= size and list[i].original == "nsm" then
1159 local run_start = i
1160 local run_level = list[i].level
1161 while i <= size and list[i].original == "nsm"
1162 and list[i].level == run_level do
1163 i = i + 1
1164 -- skip removed entries within nsm run
1165 while i <= size and is_removed(list[i]) do
1166 i = i + 1
1167 end
1168 end
1169 -- i is now at the base (skipping removed)
1170 while i <= size and is_removed(list[i]) do
1171 i = i + 1
1172 end
1173 if i <= size and run_level % 2 == 1 then
1174 local base = list[i]
1175 for j = i, run_start + 1, -1 do
1176 list[j] = list[j-1]
1177 end
1178 list[run_start] = base
1179 i = i + 1
1180 else
1181 i = i + 1
1182 end
1183 else
1184 i = i + 1
1185 end
1186 end
1187 end
1188
1189 local function process_str(str,direction,where)
1190 local list, size = build_list_str(str)
1191 local baselevel = get_baselevel(direction,list,size,where)
1192 resolve_explicit(list,size,baselevel)
1193 resolve_levels(list,size,baselevel)
1194 reorder_str(list,size)
1195 if apply_nsm then
1196 reorder_nsm(list,size)
1197 end
1198 local t = {}
1199 local n = 0
1200 for i = 1, size do
1201 local entry = list[i]
1202 if not is_removed(entry) then -- X9
1203 n = n + 1
1204 local cp = entry.mirror or entry.char
1205 t[n] = utfchar(cp)
1206 end
1207 end
1208 return concat(t)
1209 end
1210
1211 local function process_codepoints(codepoints,direction,where)
1212 local list, size = build_list_codepoints(codepoints)
1213 local baselevel = get_baselevel(direction,list,size,where)
1214 resolve_explicit(list, size, baselevel)
1215 resolve_levels(list, size, baselevel)
1216 reorder_str(list, size)
1217 if apply_nsm then
1218 reorder_nsm(list, size)
1219 end
1220 local result = {}
1221 local n = 0
1222 for i = 1, size do
1223 local entry = list[i]
1224 if not is_removed(entry) then
1225 n = n + 1
1226 result[n] = entry.mirror or entry.char
1227 end
1228 end
1229 return result
1230 end
1231
1232 local function levels_str(str,direction,where)
1233 local list, size = build_list_str(str)
1234 local baselevel = get_baselevel(direction,list,size,where)
1235 resolve_explicit(list,size,baselevel)
1236 resolve_levels(list,size,baselevel)
1237 local levels = {}
1238 for i = 1, size do
1239 local entry = list[i]
1240 if is_removed(entry) then
1241 levels[i] = false
1242 else
1243 levels[i] = entry.level
1244 end
1245 end
1246 reorder_str(list,size)
1247 if apply_nsm then
1248 reorder_nsm(list,size)
1249 end
1250 local reorder = {}
1251 local n = 0
1252 for i = 1, size do
1253 local entry = list[i]
1254 if not is_removed(entry) then
1255 n = n + 1
1256 reorder[n] = entry.original_index
1257 end
1258 end
1259 return levels, reorder
1260 end
1261
1262 local function levels_codepoints(codepoints,direction,where)
1263 local list, size = build_list_codepoints(codepoints)
1264 local baselevel = get_baselevel(direction,list,size,where)
1265 resolve_explicit(list,size,baselevel)
1266 resolve_levels(list,size,baselevel)
1267 local levels = {}
1268 for i = 1, size do
1269 local entry = list[i]
1270 if is_removed(entry) then
1271 levels[i] = false
1272 else
1273 levels[i] = entry.level
1274 end
1275 end
1276 reorder_str(list, size)
1277 if apply_nsm then
1278 reorder_nsm(list, size)
1279 end
1280 local reorder = {}
1281 local n = 0
1282 for i = 1, size do
1283 local entry = list[i]
1284 if not is_removed(entry) then
1285 n = n + 1
1286 reorder[n] = entry.original_index
1287 end
1288 end
1289 return levels, reorder
1290 end
1291
1292 if not(
1293 type(status) == "table"
1294 and type(status.luatex_version) == "number"
1295 and not (
1296 type(tex) == "table"
1297 and type(tex.initialize) == "function"
1298 )) then
1299
1300 return {
1301 directions = directiondata,
1302 mirrors = mirrordata,
1303 brackets = brackettypedata,
1304 string = {
1305 reorder = process_str,
1306 levels = levels_str,
1307 },
1308 codepoints = {
1309 reorder = process_codepoints,
1310 levels = levels_codepoints,
1311 },
1312 set = function(k, v)
1313 if k == "fences" then
1314 analyze_fences = v
1315 elseif k == "nsm" then
1316 apply_nsm = v
1317 elseif k == "mirror" then
1318 apply_mirror = v
1319 elseif k == "remove" then
1320 remove_mode = removelevels[v] or v
1321 elseif k == "baselevel" then
1322 get_baselevel = v
1323 else
1324 error("unibidi-lua: unknown option: " .. tostring(k))
1325 end
1326 end,
1327 get = function(k)
1328 if k == "fences" then return analyze_fences
1329 elseif k == "nsm" then return apply_nsm
1330 elseif k == "mirror" then return apply_mirror
1331 elseif k == "remove" then return remove_mode
1332 elseif k == "baselevel" then return get_baselevel
1333 else error("unibidi-lua: unknown option: " .. tostring(k)) end
1334 end,
1335 }
1336 end
1337
1338 local node = node
1339 local direct = node.direct
1340 local getnext = direct.getnext
1341 local getprev = direct.getprev
1342 local getid = direct.getid
1343 local getsubtype = direct.getsubtype
1344 local getchar = direct.getchar
1345 local getdirection = direct.getdirection
1346
1347 local setchar = direct.setchar
1348 local setdirection = direct.setdirection
1349
1350 local properties = direct.get_properties_table()
1351
1352 local new_node = direct.new
1353 local remove_node = direct.remove
1354 local insertnodeafter = direct.insert_after
1355 local insertnodebefore = direct.insert_before
1356
1357 local getfont = direct.getfont
1358 local get_font = font.getfont
1359
1360 local todirect = direct.todirect
1361 local tonode = direct.tonode
1362
1363 local glyph_code = node.id("glyph")
1364 local glue_code = node.id("glue")
1365 local math_code = node.id("math")
1366 local dir_code = node.id("dir")
1367 local par_code = node.id("local_par")
1368 local penalty_code = node.id("penalty")
1369
1370 local startofpar
1371 if tex.luatexversion > 124 then
1372 startofpar = function(n)
1373 local s = getsubtype(n)
1374 return s == 0 or s == 4
1375 end
1376 else
1377 startofpar = function(n)
1378 local s = getsubtype(n)
1379 return s == 0 or s == 3
1380 end
1381 end
1382
1383 local function new_direction(dir,swap)
1384 local t = new_node(dir_code)
1385 setdirection(t,dir,swap)
1386 return t
1387 end
1388
1389 local parfillskip_code = 15
1390
1391 local function build_list(head)
1392 -- P1
1393 local current = head
1394 local size = 0
1395 while current do
1396 size = size + 1
1397 local id = getid(current)
1398 local p = properties[current]
1399 if p and p.directions then
1400 -- tricky as dirs can be injected in between
1401 local skip = 0
1402 local last = id
1403 current = getnext(current)
1404 while current do
1405 local id = getid(current)
1406 local p = properties[current]
1407 if p and p.directions then
1408 skip = skip + 1
1409 last = id
1410 current = getnext(current)
1411 else
1412 break
1413 end
1414 end
1415 if id == last then -- the start id
1416 list[size] = setmetatable({ skip = skip, id = id },mt_object)
1417 else
1418 list[size] = setmetatable({ skip = skip, id = id, last = last },mt_object)
1419 end
1420 elseif id == glyph_code then
1421 local chr = getchar(current)
1422 local dir = directiondata[chr]
1423 -- could also be a metatable
1424 list[size] = { char = chr, direction = dir, original = dir, level = 0 }
1425 current = getnext(current)
1426 -- if not list[dir] then list[dir] = true end -- not faster when we check for usage
1427 elseif id == glue_code then -- and how about kern
1428 list[size] = setmetatable({ },mt_space)
1429 current = getnext(current)
1430 elseif id == dir_code then
1431 local dir, pop = getdirection(current)
1432 if dir == lefttoright_code then
1433 list[size] = setmetatable({ remove_always = true },pop and mt_pdf or mt_lre)
1434 elseif dir == righttoleft_code then
1435 list[size] = setmetatable({ remove_always = true },pop and mt_pdf or mt_rle)
1436 else
1437 list[size] = setmetatable({ id = id },mt_object)
1438 end
1439 current = getnext(current)
1440 elseif id == math_code then
1441 local skip = 0
1442 current = getnext(current)
1443 while getid(current) ~= math_code do
1444 skip = skip + 1
1445 current = getnext(current)
1446 end
1447 skip = skip + 1
1448 current = getnext(current)
1449 list[size] = setmetatable({ id = id, skip = skip },mt_object)
1450 else -- disc_code: we assume that these are the same as the surrounding
1451 local skip = 0
1452 local last = id
1453 current = getnext(current)
1454 while current do
1455 local id = getid(current)
1456 if id ~= glyph_code and id ~= glue_code and id ~= dir_code
1457 and id ~= math_code then
1458 skip = skip + 1
1459 last = id
1460 current = getnext(current)
1461 else
1462 break
1463 end
1464 end
1465 if id == last then -- the start id
1466 list[size] = setmetatable({ id = id, skip = skip },mt_object)
1467 else
1468 list[size] = setmetatable({ id = id, skip = skip, last = last },mt_object)
1469 end
1470 end
1471 end
1472 return list, size
1473 end
1474
1475 local function get_baselevel(direction,list,size,where,head)
1476 if getid(head) == par_code and startofpar(head) then
1477 direction = getdirection(head)
1478 if direction == lefttoright_code or direction == righttoleft_code then
1479 return direction
1480 end
1481 elseif direction == lefttoright_code or direction == righttoleft_code then
1482 return direction
1483 end
1484 -- P2, P3
1485 -- P2: find the first strong character that is not inside an isolate run.
1486 local i = 1
1487 while i <= size do
1488 local entry = list[i]
1489 local direction = entry.direction
1490 -- P2: skip characters enclosed in an isolate sequence
1491 if isolate_initiator[direction] then
1492 i = skip_isolate(list, i, size)
1493 elseif direction == "pdi" then
1494 -- unmatched PDI at the paragraph level; treat as neutral, keep scanning
1495 elseif direction == "r" or direction == "al" then
1496 return righttoleft_code
1497 elseif direction == "l" then
1498 return lefttoright_code
1499 end
1500 i = i + 1
1501 end
1502 return lefttoright_code
1503 end
1504
1505 local function insert_from_level(baselevel)
1506 return baselevel
1507 end
1508
1509 local function insert_dir_points(list,size,baselevel)
1510 -- L2, but no actual reversion is done, we simply annotate where
1511 -- begindir/endddir node will be inserted.
1512 local maxlevel = 0
1513 for i=1,size do
1514 local level = list[i].level
1515 if level > maxlevel then
1516 maxlevel = level
1517 end
1518 end
1519
1520 for level=insert_from_level(baselevel),maxlevel do
1521 local started = false
1522 local runstart = nil
1523 local runlast = nil
1524 local dircode = (level % 2 == 1) and righttoleft_code or lefttoright_code
1525
1526 for i=1,size do
1527 local entry = list[i]
1528 if entry.level >= level then
1529 if not started then
1530 local b = entry.begindirs
1531 if b then
1532 b[#b+1] = dircode
1533 else
1534 entry.begindirs = { dircode }
1535 end
1536 runstart = i
1537 started = true
1538 end
1539 runlast = i
1540 else
1541 if started and runlast then
1542 local l = list[runlast]
1543 local e = l.enddirs
1544 if e then
1545 e[#e+1] = dircode
1546 else
1547 l.enddirs = { dircode }
1548 end
1549 started = false
1550 runstart = nil
1551 runlast = nil
1552 end
1553 end
1554 end
1555
1556 -- Close at end if still open
1557 if started and runlast then
1558 local l = list[runlast]
1559 local e = l.enddirs
1560 if e then
1561 e[#e+1] = dircode
1562 else
1563 l.enddirs = { dircode }
1564 end
1565 end
1566 end
1567 end
1568
1569 -- We flag nodes that can be skipped when we see them again but because whatever
1570 -- mechanism can inject dir nodes that then are not flagged, we don't flag dir
1571 -- nodes that we inject here.
1572
1573 local function mirror_char(current,mirror)
1574 local curr_font = get_font(getfont(current))
1575 local props = curr_font and curr_font.properties
1576 local mode = props and props.mode
1577 if mode ~= 'harf' and mode ~= 'plug' then
1578 setchar(current,mirror)
1579 end
1580 end
1581
1582 local function apply_to_list(list,size,head)
1583 local index = 1
1584 local current = head
1585 while current do
1586 if index > size then
1587 break
1588 end
1589 local id = getid(current)
1590 local entry = list[index]
1591
1592 local p = properties[current]
1593 if p then
1594 p.directions = true
1595 else
1596 properties[current] = { directions = true }
1597 end
1598
1599 -- Handle begindirs
1600 local b = entry.begindirs
1601 if b then
1602 if id == par_code and startofpar(current) then
1603 -- par should always be the 1st node, insert begindirs AFTER it
1604 for i=1,#b do
1605 head, current = insertnodeafter(head,current,new_direction(b[i]))
1606 end
1607 else
1608 -- Insert begindirs BEFORE current node
1609 for i=1,#b do
1610 head = insertnodebefore(head,current,new_direction(b[i]))
1611 end
1612 end
1613 entry.begindirs = nil
1614 end
1615
1616 if id == glyph_code and entry.mirror then
1617 mirror_char(current,entry.mirror)
1618 elseif id == glue_code then
1619 if getsubtype(current) == parfillskip_code then
1620 local e = entry.enddirs
1621 if e then
1622 -- insert the enddirs before \parfillskip glue
1623 local c = current
1624 local p = getprev(c)
1625 if p and getid(p) == penalty_code then -- linepenalty
1626 c = p
1627 end
1628 for i = #e, 1, -1 do
1629 head = insertnodebefore(head,c,new_direction(e[i],true))
1630 end
1631 entry.enddirs = nil
1632 end
1633 end
1634 end
1635
1636 local skip = entry.skip
1637 if skip and skip > 0 then
1638 for i=1,skip do
1639 current = getnext(current)
1640 local p = properties[current]
1641 if p then
1642 p.directions = true
1643 else
1644 properties[current] = { directions = true }
1645 end
1646 end
1647 end
1648
1649 -- Insert all enddirs AFTER current node (in reverse order for proper nesting)
1650 local e = entry.enddirs
1651 if is_removed(entry) then
1652 local to_remove = current
1653 current = getnext(current)
1654 if e then
1655 for i = #e, 1, -1 do
1656 head = insertnodebefore(head, current, new_direction(e[i], true))
1657 end
1658 end
1659 head = remove_node(head, to_remove, true)
1660 else
1661 if e then
1662 for i = #e, 1, -1 do
1663 head, current = insertnodeafter(head, current, new_direction(e[i], true))
1664 end
1665 end
1666 current = getnext(current)
1667 end
1668 index = index + 1
1669 end
1670 return head
1671 end
1672
1673 -- If needed we can optimize for only_one. There is no need to do anything
1674 -- when it's not a glyph. Otherwise we only need to check mirror and apply
1675 -- directions when it's different from the surrounding. Paragraphs always
1676 -- have more than one node. Actually, we only enter this function when we
1677 -- do have a glyph!
1678
1679 local function process(head,direction,where)
1680 local list, size = build_list(head)
1681 local baselevel = get_baselevel(direction,list,size,where,head)
1682 resolve_explicit(list,size,baselevel)
1683 resolve_levels(list,size,baselevel)
1684 insert_dir_points(list,size,baselevel)
1685 return apply_to_list(list,size,head)
1686 end
1687
1688 local function levels_node(head,direction,where)
1689 local list, size = build_list(head)
1690 local baselevel = get_baselevel(direction,list,size,where,head)
1691 resolve_explicit(list,size,baselevel)
1692 resolve_levels(list,size,baselevel)
1693 local levels = {}
1694 for i = 1, size do
1695 local entry = list[i]
1696 if is_removed(entry) then
1697 levels[i] = false
1698 else
1699 levels[i] = entry.level
1700 end
1701 end
1702 return levels
1703 end
1704
1705 return {
1706 directions = directiondata,
1707 mirrors = mirrordata,
1708 brackets = brackettypedata,
1709 string = {
1710 reorder = process_str,
1711 levels = levels_str,
1712 },
1713 direct = {
1714 reorder = process,
1715 levels = levels_node,
1716 },
1717 node = {
1718 reorder = function(head, ...) return tonode(process(todirect(head), ...)) end,
1719 levels = function(head, ...) return levels_node(todirect(head), ...) end,
1720 },
1721 codepoints = {
1722 reorder = process_codepoints,
1723 levels = levels_codepoints,
1724 },
1725 set = function(k, v)
1726 if k == "fences" then
1727 analyze_fences = v
1728 elseif k == "nsm" then
1729 apply_nsm = v
1730 elseif k == "mirror" then
1731 apply_mirror = v
1732 elseif k == "remove" then
1733 remove_mode = removelevels[v] or v
1734 elseif k == "baselevel" then
1735 get_baselevel = v
1736 elseif k == "startlevel" then
1737 insert_from_level = v
1738 elseif k == "mirrorchar" then
1739 mirror_char = v
1740 else
1741 error("unibidi-lua: unknown option: " .. tostring(k))
1742 end
1743 end,
1744 get = function(k)
1745 if k == "fences" then return analyze_fences
1746 elseif k == "nsm" then return apply_nsm
1747 elseif k == "mirror" then return apply_mirror
1748 elseif k == "remove" then return remove_mode
1749 elseif k == "baselevel" then return get_baselevel
1750 elseif k == "startlevel" then return insert_from_level
1751 elseif k == "mirrorchar" then return mirror_char
1752 else error("unibidi-lua: unknown option: " .. tostring(k)) end
1753 end,
1754 }