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
|
--
-- This is file `babel-bidi-basic.lua',
-- generated with the docstrip utility.
--
-- The original source files were:
--
-- babel.dtx (with options: `basic')
--
--
-- Copyright (C) 2012-2018 Javier Bezos and Johannes L. Braams.
-- Copyright (C) 1989-2012 Johannes L. Braams and
-- any individual authors listed elsewhere in this file.
-- All rights reserved.
--
-- This file is part of the Babel system.
-- --------------------------------------
--
-- It may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3
-- of this license or (at your option) any later version.
-- The latest version of this license is in
-- http://www.latex-project.org/lppl.txt
-- and version 1.3 or later is part of all distributions of LaTeX
-- version 2003/12/01 or later.
--
-- This work has the LPPL maintenance status "maintained".
--
-- The Current Maintainer of this work is Javier Bezos.
--
-- The list of derived (unpacked) files belonging to the distribution
-- and covered by LPPL is defined by the unpacking scripts (with
-- extension |.ins|) which are part of the distribution.
--
Babel = Babel or {}
Babel.fontmap = Babel.fontmap or {}
Babel.fontmap[0] = {} -- l
Babel.fontmap[1] = {} -- r
Babel.fontmap[2] = {} -- al/an
function Babel.pre_otfload_v(head)
-- head = Babel.numbers(head)
head = Babel.bidi(head, true)
return head
end
function Babel.pre_otfload_h(head, gc, sz, pt, dir)
-- head = Babel.numbers(head)
head = Babel.bidi(head, false, dir)
return head
end
require('babel-bidi.lua')
local characters = Babel.characters
local ranges = Babel.ranges
local DIR = node.id('dir')
local GLYPH = node.id('glyph')
local function insert_implicit(head, state, outer)
local new_state = state
if state.sim and state.eim and state.sim ~= state.eim then
dir = ((outer == 'r') and 'TLT' or 'TRT') -- ie, reverse
local d = node.new(DIR)
d.dir = '+' .. dir
node.insert_before(head, state.sim, d)
local d = node.new(DIR)
d.dir = '-' .. dir
node.insert_after(head, state.eim, d)
end
new_state.sim, new_state.eim = nil, nil
return head, new_state
end
local function insert_numeric(head, state)
local new
local new_state = state
if state.san and state.ean and state.san ~= state.ean then
local d = node.new(DIR)
d.dir = '+TLT'
_, new = node.insert_before(head, state.san, d)
if state.san == state.sim then state.sim = new end
local d = node.new(DIR)
d.dir = '-TLT'
_, new = node.insert_after(head, state.ean, d)
if state.ean == state.eim then state.eim = new end
end
new_state.san, new_state.ean = nil, nil
return head, new_state
end
-- \hbox with an explicit dir can lead to wrong results
-- <R \hbox dir TLT{<R>}> and <L \hbox dir TRT{<L>}>
function Babel.bidi(head, ispar, hdir)
local d -- d is used mainly for computations in a loop
local prev_d = ''
local new_d = false
local nodes = {}
local outer_first = nil
local glue_d = nil
local glue_i = nil
local has_en = false
local first_et = nil
local ATDIR = luatexbase.registernumber'bbl@attr@dir'
local save_outer
local temp = node.get_attribute(head, ATDIR)
if temp then
temp = temp % 3
save_outer = (temp == 0 and 'l') or
(temp == 1 and 'r') or
(temp == 2 and 'al')
elseif ispar then -- Or error? Shouldn't happen
save_outer = ('TRT' == tex.pardir) and 'r' or 'l'
else
save_outer = ('TRT' == hdir) and 'r' or 'l'
end
local outer = save_outer
local last = outer
-- 'al' is only taken into account in the first, current loop
if save_outer == 'al' then save_outer = 'r' end
local fontmap = Babel.fontmap
for item in node.traverse(head) do
-- In what follows, #node is the last (previous) node, because the
-- current one is not added until we start processing the neutrals.
-- three cases: glyph, dir, otherwise
if item.id == GLYPH
or (item.id == 7 and item.subtype == 2) then
local d_font = nil
local item_r
if item.id == 7 and item.subtype == 2 then
item_r = item.replace -- automatic discs have just 1 glyph
else
item_r = item
end
local chardata = characters[item_r.char]
d = chardata and chardata.d or nil
if not d or d == 'nsm' then
for nn, et in ipairs(ranges) do
if item_r.char < et[1] then
break
elseif item_r.char <= et[2] then
if not d then d = et[3]
elseif d == 'nsm' then d_font = et[3]
end
break
end
end
end
d = d or 'l'
d_font = d_font or d
d_font = (d_font == 'l' and 0) or
(d_font == 'nsm' and 0) or
(d_font == 'r' and 1) or
(d_font == 'al' and 2) or
(d_font == 'an' and 2) or nil
if d_font and fontmap and fontmap[d_font][item_r.font] then
item_r.font = fontmap[d_font][item_r.font]
end
if new_d then
table.insert(nodes, {nil, (outer == 'l') and 'l' or 'r', nil})
attr_d = node.get_attribute(item, ATDIR)
attr_d = attr_d % 3
if attr_d == 1 then
outer_first = 'r'
last = 'r'
elseif attr_d == 2 then
outer_first = 'r'
last = 'al'
else
outer_first = 'l'
last = 'l'
end
outer = last
has_en = false
first_et = nil
new_d = false
end
if glue_d then
if (d == 'l' and 'l' or 'r') ~= glue_d then
table.insert(nodes, {glue_i, 'on', nil})
end
glue_d = nil
glue_i = nil
end
elseif item.id == DIR then
d = nil
new_d = true
elseif item.id == node.id'glue' and item.subtype == 13 then
glue_d = d
glue_i = item
d = nil
else
d = nil
end
-- AL <= EN/ET/ES -- W2 + W3 + W6
if last == 'al' and d == 'en' then
d = 'an' -- W3
elseif last == 'al' and (d == 'et' or d == 'es') then
d = 'on' -- W6
end
-- EN + CS/ES + EN -- W4
if d == 'en' and #nodes >= 2 then
if (nodes[#nodes][2] == 'es' or nodes[#nodes][2] == 'cs')
and nodes[#nodes-1][2] == 'en' then
nodes[#nodes][2] = 'en'
end
end
-- AN + CS + AN -- W4 too, because uax9 mixes both cases
if d == 'an' and #nodes >= 2 then
if (nodes[#nodes][2] == 'cs')
and nodes[#nodes-1][2] == 'an' then
nodes[#nodes][2] = 'an'
end
end
-- ET/EN -- W5 + W7->l / W6->on
if d == 'et' then
first_et = first_et or (#nodes + 1)
elseif d == 'en' then
has_en = true
first_et = first_et or (#nodes + 1)
elseif first_et then -- d may be nil here !
if has_en then
if last == 'l' then
temp = 'l' -- W7
else
temp = 'en' -- W5
end
else
temp = 'on' -- W6
end
for e = first_et, #nodes do
if nodes[e][1].id == GLYPH then nodes[e][2] = temp end
end
first_et = nil
has_en = false
end
if d then
if d == 'al' then
d = 'r'
last = 'al'
elseif d == 'l' or d == 'r' then
last = d
end
prev_d = d
table.insert(nodes, {item, d, outer_first})
end
outer_first = nil
end
-- TODO -- repeated here in case EN/ET is the last node. Find a
-- better way of doing things:
if first_et then -- dir may be nil here !
if has_en then
if last == 'l' then
temp = 'l' -- W7
else
temp = 'en' -- W5
end
else
temp = 'on' -- W6
end
for e = first_et, #nodes do
if nodes[e][1].id == GLYPH then nodes[e][2] = temp end
end
end
-- dummy node, to close things
table.insert(nodes, {nil, (outer == 'l') and 'l' or 'r', nil})
--------------- NEUTRAL -----------------
outer = save_outer
last = outer
local first_on = nil
for q = 1, #nodes do
local item
local outer_first = nodes[q][3]
outer = outer_first or outer
last = outer_first or last
local d = nodes[q][2]
if d == 'an' or d == 'en' then d = 'r' end
if d == 'cs' or d == 'et' or d == 'es' then d = 'on' end --- W6
if d == 'on' then
first_on = first_on or q
elseif first_on then
if last == d then
temp = d
else
temp = outer
end
for r = first_on, q - 1 do
nodes[r][2] = temp
item = nodes[r][1] -- MIRRORING
if item.id == GLYPH and temp == 'r' then
item.char = characters[item.char].m or item.char
end
end
first_on = nil
end
if d == 'r' or d == 'l' then last = d end
end
-------------- IMPLICIT, REORDER ----------------
outer = save_outer
last = outer
local state = {}
state.has_r = false
for q = 1, #nodes do
local item = nodes[q][1]
outer = nodes[q][3] or outer
local d = nodes[q][2]
if d == 'nsm' then d = last end -- W1
if d == 'en' then d = 'an' end
local isdir = (d == 'r' or d == 'l')
if outer == 'l' and d == 'an' then
state.san = state.san or item
state.ean = item
elseif state.san then
head, state = insert_numeric(head, state)
end
if outer == 'l' then
if d == 'an' or d == 'r' then -- im -> implicit
if d == 'r' then state.has_r = true end
state.sim = state.sim or item
state.eim = item
elseif d == 'l' and state.sim and state.has_r then
head, state = insert_implicit(head, state, outer)
elseif d == 'l' then
state.sim, state.eim, state.has_r = nil, nil, false
end
else
if d == 'an' or d == 'l' then
state.sim = state.sim or item
state.eim = item
elseif d == 'r' and state.sim then
head, state = insert_implicit(head, state, outer)
elseif d == 'r' then
state.sim, state.eim = nil, nil
end
end
if isdir then
last = d -- Don't search back - best save now
elseif d == 'on' and state.san then
state.san = state.san or item
state.ean = item
end
end
return node.prev(head) or head
end
|