| 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-2026 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, temp |
| 125 |
local locale_d |
| 126 |
|
| 127 |
local save_outer |
| 128 |
local locale_d = node.get_attribute(head, ATDIR) |
| 129 |
if locale_d then |
| 130 |
locale_d = locale_d & 0x3 |
| 131 |
save_outer = (locale_d == 0 and 'l') or |
| 132 |
(locale_d == 1 and 'r') or |
| 133 |
(locale_d == 2 and 'al') |
| 134 |
elseif ispar then -- Or error? Shouldn't happen |
| 135 |
-- when the callback is called, we are just _after_ the box, |
| 136 |
-- and the textdir is that of the surrounding text |
| 137 |
save_outer = ('TRT' == tex.pardir) and 'r' or 'l' |
| 138 |
else -- Empty box |
| 139 |
save_outer = ('TRT' == hdir) and 'r' or 'l' |
| 140 |
end |
| 141 |
local outer = save_outer |
| 142 |
local last = outer |
| 143 |
-- 'al' is only taken into account in the first, current loop |
| 144 |
if save_outer == 'al' then save_outer = 'r' end |
| 145 |
|
| 146 |
local fontmap = Babel.fontmap |
| 147 |
|
| 148 |
for item in node.traverse(head) do |
| 149 |
|
| 150 |
-- Mask: DxxxPPTT (Done, Pardir [0-2], Textdir [0-2]) |
| 151 |
locale_d = node.get_attribute(item, ATDIR) |
| 152 |
node.set_attribute(item, ATDIR, 0x80) |
| 153 |
|
| 154 |
-- In what follows, #node is the last (previous) node, because the |
| 155 |
-- current one is not added until we start processing the neutrals. |
| 156 |
-- three cases: glyph, dir, otherwise |
| 157 |
if glyph_not_symbol_font(item) |
| 158 |
or (item.id == 7 and item.subtype == 2) then |
| 159 |
|
| 160 |
if inmath then goto nextnode end |
| 161 |
|
| 162 |
if locale_d == 0x80 then goto nextnode end |
| 163 |
locale_d = locale_d or ((save_outer=='l') and 0 or 1) |
| 164 |
|
| 165 |
local d_font = nil |
| 166 |
local item_r |
| 167 |
if item.id == 7 and item.subtype == 2 then |
| 168 |
item_r = item.replace -- automatic discs have just 1 glyph |
| 169 |
else |
| 170 |
item_r = item |
| 171 |
end |
| 172 |
|
| 173 |
local chardata = characters[item_r.char] |
| 174 |
d = chardata and chardata.d or nil |
| 175 |
if not d or d == 'nsm' then |
| 176 |
for nn, et in ipairs(ranges) do |
| 177 |
if item_r.char < et[1] then |
| 178 |
break |
| 179 |
elseif item_r.char <= et[2] then |
| 180 |
if not d then d = et[3] |
| 181 |
elseif d == 'nsm' then d_font = et[3] |
| 182 |
end |
| 183 |
break |
| 184 |
end |
| 185 |
end |
| 186 |
end |
| 187 |
d = d or 'l' |
| 188 |
|
| 189 |
-- A short 'pause' in bidi for mapfont |
| 190 |
-- %%%% TODO. move if fontmap here |
| 191 |
d_font = d_font or d |
| 192 |
d_font = (d_font == 'l' and 0) or |
| 193 |
(d_font == 'nsm' and 0) or |
| 194 |
(d_font == 'r' and 1) or |
| 195 |
(d_font == 'al' and 2) or |
| 196 |
(d_font == 'an' and 2) or nil |
| 197 |
if d_font and fontmap and fontmap[d_font][item_r.font] then |
| 198 |
item_r.font = fontmap[d_font][item_r.font] |
| 199 |
end |
| 200 |
|
| 201 |
if new_d then |
| 202 |
table.insert(nodes, {nil, (outer == 'l') and 'l' or 'r', nil}) |
| 203 |
if inmath then |
| 204 |
attr_d = 0 |
| 205 |
else |
| 206 |
attr_d = locale_d & 0x3 |
| 207 |
end |
| 208 |
if attr_d == 1 then |
| 209 |
outer_first = 'r' |
| 210 |
last = 'r' |
| 211 |
elseif attr_d == 2 then |
| 212 |
outer_first = 'r' |
| 213 |
last = 'al' |
| 214 |
else |
| 215 |
outer_first = 'l' |
| 216 |
last = 'l' |
| 217 |
end |
| 218 |
outer = last |
| 219 |
has_en = false |
| 220 |
first_et = nil |
| 221 |
new_d = false |
| 222 |
end |
| 223 |
|
| 224 |
if glue_d then |
| 225 |
if (d == 'l' and 'l' or 'r') ~= glue_d then |
| 226 |
table.insert(nodes, {glue_i, 'on', nil}) |
| 227 |
end |
| 228 |
glue_d = nil |
| 229 |
glue_i = nil |
| 230 |
end |
| 231 |
|
| 232 |
elseif item.id == DIR then |
| 233 |
if inmath then goto nextnode end |
| 234 |
d = nil |
| 235 |
new_d = true |
| 236 |
|
| 237 |
elseif item.id == node.id'glue' and item.subtype == 13 then |
| 238 |
if inmath then goto nextnode end |
| 239 |
glue_d = d |
| 240 |
glue_i = item |
| 241 |
d = nil |
| 242 |
|
| 243 |
elseif item.id == node.id'math' then |
| 244 |
if item.subtype == 0 then -- mathon |
| 245 |
inmath = true |
| 246 |
d = 'on' |
| 247 |
table.insert(nodes, {item, 'on', outer_first}) |
| 248 |
outer_first = nil |
| 249 |
goto nextnode |
| 250 |
else -- mathoff |
| 251 |
inmath = false |
| 252 |
end |
| 253 |
|
| 254 |
elseif item.id == 8 and item.subtype == 19 then |
| 255 |
has_hyperlink = true |
| 256 |
|
| 257 |
else |
| 258 |
d = nil |
| 259 |
end |
| 260 |
|
| 261 |
-- AL <= EN/ET/ES -- W2 + W3 + W6 |
| 262 |
if last == 'al' and d == 'en' then |
| 263 |
d = 'an' -- W3 |
| 264 |
elseif last == 'al' and (d == 'et' or d == 'es') then |
| 265 |
d = 'on' -- W6 |
| 266 |
end |
| 267 |
|
| 268 |
-- EN + CS/ES + EN -- W4 |
| 269 |
if d == 'en' and #nodes >= 2 then |
| 270 |
if (nodes[#nodes][2] == 'es' or nodes[#nodes][2] == 'cs') |
| 271 |
and nodes[#nodes-1][2] == 'en' then |
| 272 |
nodes[#nodes][2] = 'en' |
| 273 |
end |
| 274 |
end |
| 275 |
|
| 276 |
-- AN + CS + AN -- W4 too, because uax9 mixes both cases |
| 277 |
if d == 'an' and #nodes >= 2 then |
| 278 |
if (nodes[#nodes][2] == 'cs') |
| 279 |
and nodes[#nodes-1][2] == 'an' then |
| 280 |
nodes[#nodes][2] = 'an' |
| 281 |
end |
| 282 |
end |
| 283 |
|
| 284 |
-- ET/EN -- W5 + W7->l / W6->on |
| 285 |
if d == 'et' then |
| 286 |
first_et = first_et or (#nodes + 1) |
| 287 |
elseif d == 'en' then |
| 288 |
has_en = true |
| 289 |
first_et = first_et or (#nodes + 1) |
| 290 |
elseif first_et then -- d may be nil here ! |
| 291 |
if has_en then |
| 292 |
if last == 'l' then |
| 293 |
temp = 'l' -- W7 |
| 294 |
else |
| 295 |
temp = 'en' -- W5 |
| 296 |
end |
| 297 |
else |
| 298 |
temp = 'on' -- W6 |
| 299 |
end |
| 300 |
for e = first_et, #nodes do |
| 301 |
if glyph_not_symbol_font(nodes[e][1]) then nodes[e][2] = temp end |
| 302 |
end |
| 303 |
first_et = nil |
| 304 |
has_en = false |
| 305 |
end |
| 306 |
|
| 307 |
-- Force mathdir in math if ON (currently works as expected only |
| 308 |
-- with 'l') |
| 309 |
|
| 310 |
if inmath and d == 'on' then |
| 311 |
d = ('TRT' == tex.mathdir) and 'r' or 'l' |
| 312 |
end |
| 313 |
|
| 314 |
if d then |
| 315 |
if d == 'al' then |
| 316 |
d = 'r' |
| 317 |
last = 'al' |
| 318 |
elseif d == 'l' or d == 'r' then |
| 319 |
last = d |
| 320 |
end |
| 321 |
prev_d = d |
| 322 |
table.insert(nodes, {item, d, outer_first}) |
| 323 |
end |
| 324 |
|
| 325 |
outer_first = nil |
| 326 |
|
| 327 |
::nextnode:: |
| 328 |
|
| 329 |
end -- for each node |
| 330 |
|
| 331 |
-- TODO -- repeated here in case EN/ET is the last node. Find a |
| 332 |
-- better way of doing things: |
| 333 |
if first_et then -- dir may be nil here ! |
| 334 |
if has_en then |
| 335 |
if last == 'l' then |
| 336 |
temp = 'l' -- W7 |
| 337 |
else |
| 338 |
temp = 'en' -- W5 |
| 339 |
end |
| 340 |
else |
| 341 |
temp = 'on' -- W6 |
| 342 |
end |
| 343 |
for e = first_et, #nodes do |
| 344 |
if glyph_not_symbol_font(nodes[e][1]) then nodes[e][2] = temp end |
| 345 |
end |
| 346 |
end |
| 347 |
|
| 348 |
-- dummy node, to close things |
| 349 |
table.insert(nodes, {nil, (outer == 'l') and 'l' or 'r', nil}) |
| 350 |
|
| 351 |
--------------- NEUTRAL ----------------- |
| 352 |
|
| 353 |
outer = save_outer |
| 354 |
last = outer |
| 355 |
|
| 356 |
local first_on = nil |
| 357 |
|
| 358 |
for q = 1, #nodes do |
| 359 |
local item |
| 360 |
|
| 361 |
local outer_first = nodes[q][3] |
| 362 |
outer = outer_first or outer |
| 363 |
last = outer_first or last |
| 364 |
|
| 365 |
local d = nodes[q][2] |
| 366 |
if d == 'an' or d == 'en' then d = 'r' end |
| 367 |
if d == 'cs' or d == 'et' or d == 'es' then d = 'on' end --- W6 |
| 368 |
|
| 369 |
if d == 'on' then |
| 370 |
first_on = first_on or q |
| 371 |
elseif first_on then |
| 372 |
if last == d then |
| 373 |
temp = d |
| 374 |
else |
| 375 |
temp = outer |
| 376 |
end |
| 377 |
for r = first_on, q - 1 do |
| 378 |
nodes[r][2] = temp |
| 379 |
item = nodes[r][1] -- MIRRORING |
| 380 |
if Babel.mirroring_enabled and glyph_not_symbol_font(item) |
| 381 |
and temp == 'r' and characters[item.char] then |
| 382 |
local font_mode = '' |
| 383 |
if item.font > 0 and font.fonts[item.font].properties then |
| 384 |
font_mode = font.fonts[item.font].properties.mode |
| 385 |
end |
| 386 |
if font_mode ~= 'harf' and font_mode ~= 'plug' then |
| 387 |
item.char = characters[item.char].m or item.char |
| 388 |
end |
| 389 |
end |
| 390 |
end |
| 391 |
first_on = nil |
| 392 |
end |
| 393 |
|
| 394 |
if d == 'r' or d == 'l' then last = d end |
| 395 |
end |
| 396 |
|
| 397 |
-------------- IMPLICIT, REORDER ---------------- |
| 398 |
|
| 399 |
outer = save_outer |
| 400 |
last = outer |
| 401 |
|
| 402 |
local state = {} |
| 403 |
state.has_r = false |
| 404 |
|
| 405 |
for q = 1, #nodes do |
| 406 |
|
| 407 |
local item = nodes[q][1] |
| 408 |
|
| 409 |
outer = nodes[q][3] or outer |
| 410 |
|
| 411 |
local d = nodes[q][2] |
| 412 |
|
| 413 |
if d == 'nsm' then d = last end -- W1 |
| 414 |
if d == 'en' then d = 'an' end |
| 415 |
local isdir = (d == 'r' or d == 'l') |
| 416 |
|
| 417 |
if outer == 'l' and d == 'an' then |
| 418 |
state.san = state.san or item |
| 419 |
state.ean = item |
| 420 |
elseif state.san then |
| 421 |
head, state = insert_numeric(head, state) |
| 422 |
end |
| 423 |
|
| 424 |
if outer == 'l' then |
| 425 |
if d == 'an' or d == 'r' then -- im -> implicit |
| 426 |
if d == 'r' then state.has_r = true end |
| 427 |
state.sim = state.sim or item |
| 428 |
state.eim = item |
| 429 |
elseif d == 'l' and state.sim and state.has_r then |
| 430 |
head, state = insert_implicit(head, state, outer) |
| 431 |
elseif d == 'l' then |
| 432 |
state.sim, state.eim, state.has_r = nil, nil, false |
| 433 |
end |
| 434 |
else |
| 435 |
if d == 'an' or d == 'l' then |
| 436 |
if nodes[q][3] then -- nil except after an explicit dir |
| 437 |
state.sim = item -- so we move sim 'inside' the group |
| 438 |
else |
| 439 |
state.sim = state.sim or item |
| 440 |
end |
| 441 |
state.eim = item |
| 442 |
elseif d == 'r' and state.sim then |
| 443 |
head, state = insert_implicit(head, state, outer) |
| 444 |
elseif d == 'r' then |
| 445 |
state.sim, state.eim = nil, nil |
| 446 |
end |
| 447 |
end |
| 448 |
|
| 449 |
if isdir then |
| 450 |
last = d -- Don't search back - best save now |
| 451 |
elseif d == 'on' and state.san then |
| 452 |
state.san = state.san or item |
| 453 |
state.ean = item |
| 454 |
end |
| 455 |
|
| 456 |
end |
| 457 |
|
| 458 |
head = node.prev(head) or head |
| 459 |
--- FIXES --- |
| 460 |
if has_hyperlink then |
| 461 |
local flag, linking = 0, 0 |
| 462 |
for item in node.traverse(head) do |
| 463 |
if item.id == DIR then |
| 464 |
if item.dir == '+TRT' or item.dir == '+TLT' then |
| 465 |
flag = flag + 1 |
| 466 |
elseif item.dir == '-TRT' or item.dir == '-TLT' then |
| 467 |
flag = flag - 1 |
| 468 |
end |
| 469 |
elseif item.id == 8 and item.subtype == 19 then |
| 470 |
linking = flag |
| 471 |
elseif item.id == 8 and item.subtype == 20 then |
| 472 |
if linking > 0 then |
| 473 |
if item.prev.id == DIR and |
| 474 |
(item.prev.dir == '-TRT' or item.prev.dir == '-TLT') then |
| 475 |
d = node.new(DIR) |
| 476 |
d.dir = item.prev.dir |
| 477 |
node.remove(head, item.prev) |
| 478 |
node.insert_after(head, item, d) |
| 479 |
end |
| 480 |
end |
| 481 |
linking = 0 |
| 482 |
end |
| 483 |
end |
| 484 |
end |
| 485 |
|
| 486 |
for item in node.traverse_id(10, head) do |
| 487 |
local p = item |
| 488 |
local flag = false |
| 489 |
while p.prev and p.prev.id == 14 do |
| 490 |
flag = true |
| 491 |
p = p.prev |
| 492 |
end |
| 493 |
if flag then |
| 494 |
node.insert_before(head, p, node.copy(item)) |
| 495 |
node.remove(head,item) |
| 496 |
end |
| 497 |
end |
| 498 |
|
| 499 |
return head |
| 500 |
end |
| 501 |
function Babel.unset_atdir(head) |
| 502 |
local ATDIR = Babel.attr_dir |
| 503 |
for item in node.traverse(head) do |
| 504 |
node.set_attribute(item, ATDIR, 0x80) |
| 505 |
end |
| 506 |
return head |
| 507 |
end |