| 1 |
-- |
| 2 |
-- polyglossia-tibt.lua |
| 3 |
-- part of polyglossia v2.10 -- 2026/04/18 |
| 4 |
-- |
| 5 |
|
| 6 |
require('polyglossia') -- just in case... |
| 7 |
|
| 8 |
local add_to_callback = luatexbase.add_to_callback |
| 9 |
local remove_from_callback = luatexbase.remove_from_callback |
| 10 |
local declare_callback_rule = luatexbase.declare_callback_rule |
| 11 |
local priority_in_callback = luatexbase.priority_in_callback |
| 12 |
|
| 13 |
local next, type = next, type |
| 14 |
local node = node |
| 15 |
|
| 16 |
local insert_node_before = node.insert_before |
| 17 |
local insert_node_after = node.insert_after |
| 18 |
local remove_node = node.remove |
| 19 |
local copy_node = node.copy |
| 20 |
local has_attribute = node.has_attribute |
| 21 |
|
| 22 |
local end_of_math = node.end_of_math |
| 23 |
if not end_of_math then -- luatex < .76 |
| 24 |
local traverse_nodes = node.traverse_id |
| 25 |
local math_code = node.id('math_char') |
| 26 |
local end_of_math = function (n) |
| 27 |
for n in traverse_nodes(math_code, n.next) do |
| 28 |
return n |
| 29 |
end |
| 30 |
end |
| 31 |
end |
| 32 |
|
| 33 |
-- node types as of April 2024 |
| 34 |
local glyph_code = node.id('glyph') |
| 35 |
local penalty_code = node.id('penalty') |
| 36 |
local kern_code = node.id('kern') |
| 37 |
|
| 38 |
-- we make a new node, so that we can copy it later on |
| 39 |
local penalty_node = node.new(penalty_code) |
| 40 |
penalty_node.penalty = 50 -- corresponds to the penalty LaTeX sets at explicit hyphens |
| 41 |
|
| 42 |
local function get_penalty_node() |
| 43 |
return copy_node(penalty_node) |
| 44 |
end |
| 45 |
|
| 46 |
local xpgtibtattr = luatexbase.attributes['xpg@tibteol'] |
| 47 |
|
| 48 |
local tsheg = unicode.utf8.byte('་') |
| 49 |
|
| 50 |
-- from typo-spa.lua |
| 51 |
local function process(head) |
| 52 |
local start = head |
| 53 |
-- head is always begin of par (whatsit), so we have at least two prev nodes |
| 54 |
-- penalty followed by glue |
| 55 |
while start do |
| 56 |
local id = start.id |
| 57 |
if id == glyph_code then |
| 58 |
local attr = has_attribute(start, xpgtibtattr) |
| 59 |
if attr and attr > 0 then |
| 60 |
if start.char == tsheg then |
| 61 |
if start.next then |
| 62 |
insert_node_after(head,start,get_penalty_node()) |
| 63 |
end |
| 64 |
end |
| 65 |
end |
| 66 |
elseif id == math_code then |
| 67 |
start = end_of_math(start) -- weird, can return nil .. no math end? |
| 68 |
end |
| 69 |
if start then |
| 70 |
start = start.next |
| 71 |
end |
| 72 |
end |
| 73 |
return head |
| 74 |
end |
| 75 |
|
| 76 |
local callback_name = "pre_linebreak_filter" |
| 77 |
|
| 78 |
local function activate() |
| 79 |
add_to_callback(callback_name, process, "polyglossia-tibt.process") |
| 80 |
declare_callback_rule(callback_name, |
| 81 |
"polyglossia-tibt.process", "before", "luaotfload.node_processor") |
| 82 |
end |
| 83 |
|
| 84 |
local function desactivate() |
| 85 |
remove_from_callback(callback_name, "polyglossia-tibt.process") |
| 86 |
end |
| 87 |
|
| 88 |
polyglossia.activate_tibt_eol = activate |
| 89 |
polyglossia.desactivate_tibt_eol = desactivate |