1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
if not modules then modules = { } end modules ['typo-dua'] = {
version = 1.001,
comment = "companion to typo-dir.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team / See below",
license = "see context related readme files / whatever applies",
comment = "Unicode bidi (sort of) variant a",
derived = "derived from t-bidi by Khaled Hosny who derived from minibidi.c by Arabeyes",
}
-- Comment by Khaled Hosny:
--
-- This code started as a line for line translation of Arabeyes' minibidi.c from C to Lua,
-- excluding parts that of no use to us like shaping. The C code is Copyright (c) 2004
-- Ahmad Khalifa, and is distributed under the MIT Licence. The full license text can be
-- found at: http://svn.arabeyes.org/viewvc/projects/adawat/minibidi/LICENCE.
--
-- Comment by Hans Hagen:
--
-- The initial conversion to Lua has been done by Khaled Hosny. As a first step I optimized the
-- code (to suit todays context mkiv). Next I fixed the foreign object handling, for instance,
-- we can skip over math but we need to inject before the open math node and after the close node,
-- so we need to keep track of the endpoint. After I fixed that bit I realized that it was possible
-- to generalize the object skipper if only because it saves memory (and processing time). The
-- current implementation is about three times as fast (roughly measured) and I can probably squeeze
-- out some more, only to sacrifice soem when I start adding features. A next stage will be to have
-- more granularity in foreign objects. Of course all errors are mine. I'll also added the usual bit
-- of context tracing and reshuffled some code. A memory optimization is on the agenda (already sort
-- of prepared). It is no longer line by line.
--
-- The first implementation of bidi in context started out from examples of mixed usage (including
-- more than text) with an at that point bugged r2l support. It has some alternatives for letting
-- the tex markup having a bit higher priority. I will probably add some local (style driven)
-- overrides to the following code as well. It also means that we can selectively enable and disable
-- the parser (because a document wide appliance migh tnot be what we want). This will bring a
-- slow down but not that much. (I need to check with Idris why we have things like isol there.)
--
-- We'll probably keep multiple methods around (this is just a side track of improving the already
-- available scanner). I need to look into the changed unicode recomendations anyway as a first
-- impression is that some fuzzyness has been removed. I finally need to spend time on those specs. So,
-- there will be a third variant (written from scratch) so some point. The fun about TeX is that we
-- can provide alternative solutions (given that it doesn't bloat the engine!)
--
-- A test with some hebrew, mixed with hboxes with latin/hebrew and simple math. In fact this triggered
-- playing with bidi again:
--
-- 0.11 : nothing
-- 0.14 : 0.03 node list only, one pass
-- 0.23 : 0.12 close to unicode bidi, multipass
-- 0.44 : 0.33 original previous
--
-- todo: check for introduced errors
-- todo: reuse list, we have size, so we can just change values (and auto allocate when not there)
-- todo: reuse the stack
-- todo: no need for a max check
-- todo: collapse bound similar ranges (not ok yet)
-- tood: combine some sweeps
--
-- This one wil get frozen (or if needed in sync with basic t-bidi) and I will explore more options
-- in typo-dub.lua. There I might also be able to improve performance a bit.
local insert, remove, unpack, concat = table.insert, table.remove, table.unpack, table.concat
local utfchar = utf.char
local formatters = string.formatters
local directiondata = characters.directions
local mirrordata = characters.mirrors
local remove_node = nodes.remove
local insert_node_after = nodes.insert_after
local insert_node_before = nodes.insert_before
local nodepool = nodes.pool
local new_textdir = nodepool.textdir
local nodecodes = nodes.nodecodes
local whatsitcodes = nodes.whatsitcodes
local skipcodes = nodes.skipcodes
local glyph_code = nodecodes.glyph
local glue_code = nodecodes.glue
local hlist_code = nodecodes.hlist
local vlist_code = nodecodes.vlist
local math_code = nodecodes.math
local whatsit_code = nodecodes.whatsit
local dir_code = whatsitcodes.dir
local localpar_code = whatsitcodes.localpar
local parfillskip_code = skipcodes.skipcodes
----- object_replacement = 0xFFFC -- object replacement character
local maximum_stack = 60 -- probably spec but not needed
local setcolor = nodes.tracers.colors.set
local resetcolor = nodes.tracers.colors.reset
local directions = typesetters.directions
local a_directions = attributes.private('directions')
local remove_controls = true directives.register("typesetters.directions.one.removecontrols",function(v) remove_controls = v end)
local trace_directions = false trackers .register("typesetters.directions.one", function(v) trace_directions = v end)
local trace_details = false trackers .register("typesetters.directions.one.details", function(v) trace_details = v end)
local report_directions = logs.reporter("typesetting","directions one")
local whitespace = {
lre = true,
rle = true,
lro = true,
rlo = true,
pdf = true,
bn = true,
ws = true,
}
local b_s_ws_on = {
b = true,
s = true,
ws = true,
on = true
}
-- tracing
local function show_list(list,size,what)
local what = what or "direction"
local joiner = utfchar(0x200C)
local result = { }
for i=1,size do
local entry = list[i]
local character = entry.char
local direction = entry[what]
if character == 0xFFFC then
local first = entry.id
local last = entry.last
local skip = entry.skip
if last then
result[i] = formatters["%-3s:%s %s..%s (%i)"](direction,joiner,nodecodes[first],nodecodes[last],skip or 0)
else
result[i] = formatters["%-3s:%s %s (%i)"](direction,joiner,nodecodes[first],skip or 0)
end
elseif character >= 0x202A and character <= 0x202C then
result[i] = formatters["%-3s:%s %U"](direction,joiner,character)
else
result[i] = formatters["%-3s:%s %c %U"](direction,joiner,character,character)
end
end
return concat(result,joiner .. " | " .. joiner)
end
-- preparation
local function show_done(list,size)
local joiner = utfchar(0x200C)
local result = { }
for i=1,size do
local entry = list[i]
local character = entry.char
local begindir = entry.begindir
local enddir = entry.enddir
if begindir then
result[#result+1] = formatters["<%s>"](begindir)
end
if entry.remove then
-- continue
elseif character == 0xFFFC then
result[#result+1] = formatters["<%s>"]("?")
elseif character == 0x0020 then
result[#result+1] = formatters["<%s>"](" ")
elseif character >= 0x202A and character <= 0x202C then
result[#result+1] = formatters["<%s>"](entry.original)
else
result[#result+1] = utfchar(character)
end
if enddir then
result[#result+1] = formatters["<%s>"](enddir)
end
end
return concat(result,joiner)
end
-- keeping the list and overwriting doesn't save much runtime, only a few percent
-- char is only used for mirror, so in fact we can as well only store it for
-- glyphs only
local function build_list(head) -- todo: store node pointer ... saves loop
-- P1
local current = head
local list = { }
local size = 0
while current do
size = size + 1
local id = current.id
if id == glyph_code then
local chr = current.char
local dir = directiondata[chr]
list[size] = { char = chr, direction = dir, original = dir, level = 0 }
current = current.next
elseif id == glue_code then
list[size] = { char = 0x0020, direction = "ws", original = "ws", level = 0 }
current = current.next
elseif id == whatsit_code and current.subtype == dir_code then
local dir = current.dir
if dir == "+TLT" then
list[size] = { char = 0x202A, direction = "lre", original = "lre", level = 0 }
elseif dir == "+TRT" then
list[size] = { char = 0x202B, direction = "rle", original = "rle", level = 0 }
elseif dir == "-TLT" or dir == "-TRT" then
list[size] = { char = 0x202C, direction = "pdf", original = "pdf", level = 0 }
else
list[size] = { char = 0xFFFC, direction = "on", original = "on", level = 0, id = id } -- object replacement character
end
current = current.next
elseif id == math_code then
local skip = 0
current = current.next
while current.id ~= math_code do
skip = skip + 1
current = current.next
end
skip = skip + 1
current = current.next
list[size] = { char = 0xFFFC, direction = "on", original = "on", level = 0, skip = skip, id = id }
else
local skip = 0
local last = id
current = current.next
while n do
local id = current.id
if id ~= glyph_code and id ~= glue_code and not (id == whatsit_code and current.subtype == dir_code) then
skip = skip + 1
last = id
current = current.next
else
break
end
end
if id == last then
list[size] = { char = 0xFFFC, direction = "on", original = "on", level = 0, skip = skip, id = id }
else
list[size] = { char = 0xFFFC, direction = "on", original = "on", level = 0, skip = skip, id = id, last = last }
end
end
end
return list, size
end
-- the action
-- local function find_run_limit_et(list,run_start,limit)
-- local run_limit = run_start
-- local i = run_start
-- while i <= limit and list[i].direction == "et" do
-- run_limit = i
-- i = i + 1
-- end
-- return run_limit
-- end
local function find_run_limit_et(list,start,limit) -- returns last match
for i=start,limit do
if list[i].direction == "et" then
start = i
else
return start
end
end
return start
end
-- local function find_run_limit_b_s_ws_on(list,run_start,limit)
-- local run_limit = run_start
-- local i = run_start
-- while i <= limit and b_s_ws_on[list[i].direction] do
-- run_limit = i
-- i = i + 1
-- end
-- return run_limit
-- end
local function find_run_limit_b_s_ws_on(list,start,limit)
for i=start,limit do
if b_s_ws_on[list[i].direction] then
start = i
else
return start
end
end
return start
end
-- directions.maindir = "r2l"
local function get_baselevel(head,list,size) -- todo: skip if first is object (or pass head and test for local_par)
local maindir = directions.maindir
if maindir == "r2l" then
return 1, "TRT", false
elseif maindir == "l2r" then
return 0, "TLT", false
elseif head.id == whatsit_code and head.subtype == localpar_code then
if head.dir == "TRT" then
return 1, "TRT", true
else
return 0, "TLT", true
end
else
-- P2, P3
for i=1,size do
local entry = list[i]
local direction = entry.direction
if direction == "r" or direction == "al" then
return 1, "TRT", true
elseif direction == "l" then
return 0, "TLT", true
end
end
return 0, "TLT", false
end
end
local function resolve_explicit(list,size,baselevel)
-- X1
local level = baselevel
local override = "on"
local stack = { }
local nofstack = 0
for i=1,size do
local entry = list[i]
local direction = entry.direction
-- X2
if direction == "rle" then
if nofstack < maximum_stack then
nofstack = nofstack + 1
stack[nofstack] = { level, override }
level = level + (level % 2 == 1 and 2 or 1) -- least_greater_odd(level)
override = "on"
entry.level = level
entry.direction = "bn"
entry.remove = true
elseif trace_directions then
report_directions("stack overflow at position %a with direction %a",i,direction)
end
-- X3
elseif direction == "lre" then
if nofstack < maximum_stack then
nofstack = nofstack + 1
stack[nofstack] = { level, override }
level = level + (level % 2 == 1 and 1 or 2) -- least_greater_even(level)
override = "on"
entry.level = level
entry.direction = "bn"
entry.remove = true
elseif trace_directions then
report_directions("stack overflow at position %a with direction %a",i,direction)
end
-- X4
elseif direction == "rlo" then
if nofstack < maximum_stack then
nofstack = nofstack + 1
stack[nofstack] = { level, override }
level = level + (level % 2 == 1 and 2 or 1) -- least_greater_odd(level)
override = "r"
entry.level = level
entry.direction = "bn"
entry.remove = true
elseif trace_directions then
report_directions("stack overflow at position %a with direction %a",i,direction)
end
-- X5
elseif direction == "lro" then
if nofstack < maximum_stack then
nofstack = nofstack + 1
stack[nofstack] = { level, override }
level = level + (level % 2 == 1 and 1 or 2) -- least_greater_even(level)
override = "l"
entry.level = level
entry.direction = "bn"
entry.remove = true
elseif trace_directions then
report_directions("stack overflow at position %a with direction %a",i,direction)
end
-- X7
elseif direction == "pdf" then
if nofstack < maximum_stack then
local stacktop = stack[nofstack]
nofstack = nofstack - 1
level = stacktop[1]
override = stacktop[2]
entry.level = level
entry.direction = "bn"
entry.remove = true
elseif trace_directions then
report_directions("stack overflow at position %a with direction %a",i,direction)
end
-- X6
else
entry.level = level
if override ~= "on" then
entry.direction = override
end
end
end
-- X8 (reset states and overrides after paragraph)
end
local function resolve_weak(list,size,start,limit,sor,eor)
-- W1
for i=start,limit do
local entry = list[i]
if entry.direction == "nsm" then
if i == start then
entry.direction = sor
else
entry.direction = list[i-1].direction
end
end
end
-- W2
for i=start,limit do
local entry = list[i]
if entry.direction == "en" then
for j=i-1,start,-1 do
local prev = list[j]
local direction = prev.direction
if direction == "al" then
entry.direction = "an"
break
elseif direction == "r" or direction == "l" then
break
end
end
end
end
-- W3
for i=start,limit do
local entry = list[i]
if entry.direction == "al" then
entry.direction = "r"
end
end
-- W4
for i=start+1,limit-1 do
local entry = list[i]
local direction = entry.direction
if direction == "es" then
if list[i-1].direction == "en" and list[i+1].direction == "en" then
entry.direction = "en"
end
elseif direction == "cs" then
local prevdirection = list[i-1].direction
if prevdirection == "en" then
if list[i+1].direction == "en" then
entry.direction = "en"
end
elseif prevdirection == "an" and list[i+1].direction == "an" then
entry.direction = "an"
end
end
end
-- W5
local i = start
while i <= limit do
if list[i].direction == "et" then
local runstart = i
local runlimit = find_run_limit_et(list,runstart,limit) -- when moved inline we can probably collapse a lot
local rundirection = runstart == start and sor or list[runstart-1].direction
if rundirection ~= "en" then
rundirection = runlimit == limit and eor or list[runlimit+1].direction
end
if rundirection == "en" then
for j=runstart,runlimit do
list[j].direction = "en"
end
end
i = runlimit
end
i = i + 1
end
-- W6
for i=start,limit do
local entry = list[i]
local direction = entry.direction
if direction == "es" or direction == "et" or direction == "cs" then
entry.direction = "on"
end
end
-- W7
for i=start,limit do
local entry = list[i]
if entry.direction == "en" then
local prev_strong = sor
for j=i-1,start,-1 do
local direction = list[j].direction
if direction == "l" or direction == "r" then
prev_strong = direction
break
end
end
if prev_strong == "l" then
entry.direction = "l"
end
end
end
end
local function resolve_neutral(list,size,start,limit,sor,eor)
-- N1, N2
for i=start,limit do
local entry = list[i]
if b_s_ws_on[entry.direction] then
local leading_direction, trailing_direction, resolved_direction
local runstart = i
local runlimit = find_run_limit_b_s_ws_on(list,runstart,limit)
if runstart == start then
leading_direction = sor
else
leading_direction = list[runstart-1].direction
if leading_direction == "en" or leading_direction == "an" then
leading_direction = "r"
end
end
if runlimit == limit then
trailing_direction = eor
else
trailing_direction = list[runlimit+1].direction
if trailing_direction == "en" or trailing_direction == "an" then
trailing_direction = "r"
end
end
if leading_direction == trailing_direction then
-- N1
resolved_direction = leading_direction
else
-- N2 / does the weird period
resolved_direction = entry.level % 2 == 1 and "r" or "l" -- direction_of_level(entry.level)
end
for j=runstart,runlimit do
list[j].direction = resolved_direction
end
i = runlimit
end
i = i + 1
end
end
local function resolve_implicit(list,size,start,limit,sor,eor)
-- I1
for i=start,limit do
local entry = list[i]
local level = entry.level
if level % 2 ~= 1 then -- not odd(level)
local direction = entry.direction
if direction == "r" then
entry.level = level + 1
elseif direction == "an" or direction == "en" then
entry.level = level + 2
end
end
end
-- I2
for i=start,limit do
local entry = list[i]
local level = entry.level
if level % 2 == 1 then -- odd(level)
local direction = entry.direction
if direction == "l" or direction == "en" or direction == "an" then
entry.level = level + 1
end
end
end
end
local function resolve_levels(list,size,baselevel)
-- X10
local start = 1
while start < size do
local level = list[start].level
local limit = start + 1
while limit < size and list[limit].level == level do
limit = limit + 1
end
local prev_level = start == 1 and baselevel or list[start-1].level
local next_level = limit == size and baselevel or list[limit+1].level
local sor = (level > prev_level and level or prev_level) % 2 == 1 and "r" or "l" -- direction_of_level(max(level,prev_level))
local eor = (level > next_level and level or next_level) % 2 == 1 and "r" or "l" -- direction_of_level(max(level,next_level))
-- W1 .. W7
resolve_weak(list,size,start,limit,sor,eor)
-- N1 .. N2
resolve_neutral(list,size,start,limit,sor,eor)
-- I1 .. I2
resolve_implicit(list,size,start,limit,sor,eor)
start = limit
end
-- L1
for i=1,size do
local entry = list[i]
local direction = entry.original
-- (1)
if direction == "s" or direction == "b" then
entry.level = baselevel
-- (2)
for j=i-1,1,-1 do
local entry = list[j]
if whitespace[entry.original] then
entry.level = baselevel
else
break
end
end
end
end
-- (3)
for i=size,1,-1 do
local entry = list[i]
if whitespace[entry.original] then
entry.level = baselevel
else
break
end
end
-- L4
for i=1,size do
local entry = list[i]
if entry.level % 2 == 1 then -- odd(entry.level)
local mirror = mirrordata[entry.char]
if mirror then
entry.mirror = mirror
end
end
end
end
local function insert_dir_points(list,size)
-- L2, but no actual reversion is done, we simply annotate where
-- begindir/endddir node will be inserted.
local maxlevel = 0
local finaldir = false
for i=1,size do
local level = list[i].level
if level > maxlevel then
maxlevel = level
end
end
for level=0,maxlevel do
local started = false
local begindir = nil
local enddir = nil
if level % 2 == 1 then
begindir = "+TRT"
enddir = "-TRT"
else
begindir = "+TLT"
enddir = "-TLT"
end
for i=1,size do
local entry = list[i]
if entry.level >= level then
if not started then
entry.begindir = begindir
started = true
end
else
if started then
list[i-1].enddir = enddir
started = false
end
end
end
-- make sure to close the run at end of line
if started then
finaldir = enddir
end
end
if finaldir then
list[size].enddir = finaldir
end
end
local function apply_to_list(list,size,head,pardir)
local index = 1
local current = head
local done = false
while current do
if index > size then
report_directions("fatal error, size mismatch")
break
end
local id = current.id
local entry = list[index]
local begindir = entry.begindir
local enddir = entry.enddir
if id == glyph_code then
local mirror = entry.mirror
if mirror then
current.char = mirror
end
if trace_directions then
local original = entry.original
local direction = entry.direction
if mirror then
setcolor(current,"trace:dc")
elseif direction == "l" then
if original == direction then
setcolor(current,"trace:dr")
else
setcolor(current,"trace:dm")
end
elseif direction == "r" then
if original == direction then
setcolor(current,"trace:db")
else
setcolor(current,"trace:dg")
end
else
resetcolor(current)
end
end
elseif id == hlist_code or id == vlist_code then
-- current.list = process(current.list) -- not needed
current.dir = pardir -- is this really needed?
elseif id == glue_code then
if enddir and current.subtype == parfillskip_code then
-- insert the last enddir before \parfillskip glue
head = insert_node_before(head,current,new_textdir(enddir))
enddir = false
done = true
end
elseif id == whatsit_code then
if begindir and current.subtype == localpar_code then
-- local_par should always be the 1st node
head, current = insert_node_after(head,current,new_textdir(begindir))
begindir = nil
done = true
end
end
if begindir then
head = insert_node_before(head,current,new_textdir(begindir))
done = true
end
local skip = entry.skip
if skip and skip > 0 then
for i=1,skip do
current = current.next
end
end
if enddir then
head, current = insert_node_after(head,current,new_textdir(enddir))
done = true
end
if not entry.remove then
current = current.next
elseif remove_controls then
-- X9
head, current = remove_node(head,current,true)
done = true
else
current = current.next
end
index = index + 1
end
return head, done
end
local function process(namespace,attribute,head)
local list, size = build_list(head)
local baselevel, pardir, dirfound = get_baselevel(head,list,size) -- we always have an inline dir node in context
if not dirfound and trace_details then
report_directions("no initial direction found, gambling")
end
if trace_details then
report_directions("before : %s",show_list(list,size,"original"))
end
resolve_explicit(list,size,baselevel)
resolve_levels(list,size,baselevel)
insert_dir_points(list,size)
if trace_details then
report_directions("after : %s",show_list(list,size,"direction"))
report_directions("result : %s",show_done(list,size))
end
head, done = apply_to_list(list,size,head,pardir)
return head, done
end
directions.installhandler(interfaces.variables.one,process)
|