| 1 |
-- |
| 2 |
-- This is file `babel-bidi-basic-r.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- babel.dtx (with options: `basic-r') |
| 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 |
Babel.bidi_enabled = true |
| 36 |
|
| 37 |
require('babel-data-bidi.lua') |
| 38 |
|
| 39 |
local characters = Babel.characters |
| 40 |
local ranges = Babel.ranges |
| 41 |
|
| 42 |
local DIR = node.id("dir") |
| 43 |
|
| 44 |
local function dir_mark(head, from, to, outer) |
| 45 |
dir = (outer == 'r') and 'TLT' or 'TRT' -- i.e., reverse |
| 46 |
local d = node.new(DIR) |
| 47 |
d.dir = '+' .. dir |
| 48 |
node.insert_before(head, from, d) |
| 49 |
d = node.new(DIR) |
| 50 |
d.dir = '-' .. dir |
| 51 |
node.insert_after(head, to, d) |
| 52 |
end |
| 53 |
|
| 54 |
function Babel.bidi(head, ispar) |
| 55 |
local first_n, last_n -- first and last char with nums |
| 56 |
local last_es -- an auxiliary 'last' used with nums |
| 57 |
local first_d, last_d -- first and last char in L/R block |
| 58 |
local dir, dir_real |
| 59 |
local strong = ('TRT' == tex.pardir) and 'r' or 'l' |
| 60 |
local strong_lr = (strong == 'l') and 'l' or 'r' |
| 61 |
local outer = strong |
| 62 |
|
| 63 |
local new_dir = false |
| 64 |
local first_dir = false |
| 65 |
local inmath = false |
| 66 |
|
| 67 |
local last_lr |
| 68 |
|
| 69 |
local type_n = '' |
| 70 |
|
| 71 |
for item in node.traverse(head) do |
| 72 |
|
| 73 |
-- three cases: glyph, dir, otherwise |
| 74 |
if item.id == node.id'glyph' |
| 75 |
or (item.id == 7 and item.subtype == 2) then |
| 76 |
|
| 77 |
local itemchar |
| 78 |
if item.id == 7 and item.subtype == 2 then |
| 79 |
itemchar = item.replace.char |
| 80 |
else |
| 81 |
itemchar = item.char |
| 82 |
end |
| 83 |
local chardata = characters[itemchar] |
| 84 |
dir = chardata and chardata.d or nil |
| 85 |
if not dir then |
| 86 |
for nn, et in ipairs(ranges) do |
| 87 |
if itemchar < et[1] then |
| 88 |
break |
| 89 |
elseif itemchar <= et[2] then |
| 90 |
dir = et[3] |
| 91 |
break |
| 92 |
end |
| 93 |
end |
| 94 |
end |
| 95 |
dir = dir or 'l' |
| 96 |
if inmath then dir = ('TRT' == tex.mathdir) and 'r' or 'l' end |
| 97 |
if new_dir then |
| 98 |
attr_dir = 0 |
| 99 |
for at in node.traverse(item.attr) do |
| 100 |
if at.number == Babel.attr_dir then |
| 101 |
attr_dir = at.value & 0x3 |
| 102 |
end |
| 103 |
end |
| 104 |
if attr_dir == 1 then |
| 105 |
strong = 'r' |
| 106 |
elseif attr_dir == 2 then |
| 107 |
strong = 'al' |
| 108 |
else |
| 109 |
strong = 'l' |
| 110 |
end |
| 111 |
strong_lr = (strong == 'l') and 'l' or 'r' |
| 112 |
outer = strong_lr |
| 113 |
new_dir = false |
| 114 |
end |
| 115 |
|
| 116 |
if dir == 'nsm' then dir = strong end -- W1 |
| 117 |
dir_real = dir -- We need dir_real to set strong below |
| 118 |
if dir == 'al' then dir = 'r' end -- W3 |
| 119 |
if strong == 'al' then |
| 120 |
if dir == 'en' then dir = 'an' end -- W2 |
| 121 |
if dir == 'et' or dir == 'es' then dir = 'on' end -- W6 |
| 122 |
strong_lr = 'r' -- W3 |
| 123 |
end |
| 124 |
elseif item.id == node.id'dir' and not inmath then |
| 125 |
new_dir = true |
| 126 |
dir = nil |
| 127 |
elseif item.id == node.id'math' then |
| 128 |
inmath = (item.subtype == 0) |
| 129 |
else |
| 130 |
dir = nil -- Not a char |
| 131 |
end |
| 132 |
if dir == 'en' or dir == 'an' or dir == 'et' then |
| 133 |
if dir ~= 'et' then |
| 134 |
type_n = dir |
| 135 |
end |
| 136 |
first_n = first_n or item |
| 137 |
last_n = last_es or item |
| 138 |
last_es = nil |
| 139 |
elseif dir == 'es' and last_n then -- W3+W6 |
| 140 |
last_es = item |
| 141 |
elseif dir == 'cs' then -- it's right - do nothing |
| 142 |
elseif first_n then -- & if dir = any but en, et, an, es, cs, inc nil |
| 143 |
if strong_lr == 'r' and type_n ~= '' then |
| 144 |
dir_mark(head, first_n, last_n, 'r') |
| 145 |
elseif strong_lr == 'l' and first_d and type_n == 'an' then |
| 146 |
dir_mark(head, first_n, last_n, 'r') |
| 147 |
dir_mark(head, first_d, last_d, outer) |
| 148 |
first_d, last_d = nil, nil |
| 149 |
elseif strong_lr == 'l' and type_n ~= '' then |
| 150 |
last_d = last_n |
| 151 |
end |
| 152 |
type_n = '' |
| 153 |
first_n, last_n = nil, nil |
| 154 |
end |
| 155 |
if dir == 'l' or dir == 'r' then |
| 156 |
if dir ~= outer then |
| 157 |
first_d = first_d or item |
| 158 |
last_d = item |
| 159 |
elseif first_d and dir ~= strong_lr then |
| 160 |
dir_mark(head, first_d, last_d, outer) |
| 161 |
first_d, last_d = nil, nil |
| 162 |
end |
| 163 |
end |
| 164 |
if dir and not last_lr and dir ~= 'l' and outer == 'r' then |
| 165 |
item.char = characters[item.char] and |
| 166 |
characters[item.char].m or item.char |
| 167 |
elseif (dir or new_dir) and last_lr ~= item then |
| 168 |
local mir = outer .. strong_lr .. (dir or outer) |
| 169 |
if mir == 'rrr' or mir == 'lrr' or mir == 'rrl' or mir == 'rlr' then |
| 170 |
for ch in node.traverse(node.next(last_lr)) do |
| 171 |
if ch == item then break end |
| 172 |
if ch.id == node.id'glyph' and characters[ch.char] then |
| 173 |
ch.char = characters[ch.char].m or ch.char |
| 174 |
end |
| 175 |
end |
| 176 |
end |
| 177 |
end |
| 178 |
if dir == 'l' or dir == 'r' then |
| 179 |
last_lr = item |
| 180 |
strong = dir_real -- Don't search back - best save now |
| 181 |
strong_lr = (strong == 'l') and 'l' or 'r' |
| 182 |
elseif new_dir then |
| 183 |
last_lr = nil |
| 184 |
end |
| 185 |
end |
| 186 |
if last_lr and outer == 'r' then |
| 187 |
for ch in node.traverse_id(node.id'glyph', node.next(last_lr)) do |
| 188 |
if characters[ch.char] then |
| 189 |
ch.char = characters[ch.char].m or ch.char |
| 190 |
end |
| 191 |
end |
| 192 |
end |
| 193 |
if first_n then |
| 194 |
dir_mark(head, first_n, last_n, outer) |
| 195 |
end |
| 196 |
if first_d then |
| 197 |
dir_mark(head, first_d, last_d, outer) |
| 198 |
end |
| 199 |
return node.prev(head) or head |
| 200 |
end |