| 1 |
-- |
| 2 |
-- This is file `tagpdf.lua', |
| 3 |
-- generated with the docstrip utility. |
| 4 |
-- |
| 5 |
-- The original source files were: |
| 6 |
-- |
| 7 |
-- tagpdf-backend.dtx (with options: `lua') |
| 8 |
-- |
| 9 |
-- Copyright (C) 2019-2025 Ulrike Fischer |
| 10 |
-- |
| 11 |
-- It may be distributed and/or modified under the conditions of |
| 12 |
-- the LaTeX Project Public License (LPPL), either version 1.3c of |
| 13 |
-- this license or (at your option) any later version. The latest |
| 14 |
-- version of this license is in the file: |
| 15 |
-- |
| 16 |
-- https://www.latex-project.org/lppl.txt |
| 17 |
-- |
| 18 |
-- This file is part of the "tagpdf bundle" (The Work in LPPL) |
| 19 |
-- and all files in that bundle must be distributed together. |
| 20 |
-- |
| 21 |
-- File: tagpdf-backend.dtx |
| 22 |
-- tagpdf.lua |
| 23 |
-- Ulrike Fischer |
| 24 |
|
| 25 |
local ProvidesLuaModule = { |
| 26 |
name = "tagpdf", |
| 27 |
version = "1.0c", --TAGVERSION |
| 28 |
date = "2026-05-17", --TAGDATE |
| 29 |
description = "tagpdf lua code", |
| 30 |
license = "The LATEX Project Public License 1.3c" |
| 31 |
} |
| 32 |
|
| 33 |
if luatexbase and luatexbase.provides_module then |
| 34 |
luatexbase.provides_module (ProvidesLuaModule) |
| 35 |
end |
| 36 |
|
| 37 |
--[[ |
| 38 |
The code has quite probably a number of problems |
| 39 |
- more variables should be local instead of global |
| 40 |
- the naming is not always consistent due to the development of the code |
| 41 |
- the traversing of the shipout box must be tested with more complicated setups |
| 42 |
- it should probably handle more node types |
| 43 |
- |
| 44 |
--]] |
| 45 |
|
| 46 |
--[[ |
| 47 |
the main table is named ltx.__tag. It contains the functions and also the data |
| 48 |
collected during the compilation. |
| 49 |
|
| 50 |
ltx.__tag.mc will contain mc connected data. |
| 51 |
ltx.__tag.role will contain data related to parent-child relations. |
| 52 |
ltx.__tag.struct will contain structure related data. |
| 53 |
ltx.__tag.page will contain page data |
| 54 |
ltx.__tag.tables contains also data from mc and struct (from older code). This needs cleaning up. |
| 55 |
There are certainly dublettes, but I don't dare yet ... |
| 56 |
ltx.__tag.func will contain (public) functions. |
| 57 |
ltx.__tag.trace will contain tracing/logging functions. |
| 58 |
local functions starts with __ |
| 59 |
functions meant for users will be in ltx.tag |
| 60 |
|
| 61 |
functions |
| 62 |
ltx.__tag.func.get_num_from (tag): takes a tag (string) and returns the id number |
| 63 |
ltx.__tag.func.output_num_from (tag): takes a tag (string) and prints (to tex) the id number |
| 64 |
ltx.__tag.func.get_tag_from (num): takes a num and returns the tag |
| 65 |
ltx.__tag.func.output_tag_from (num): takes a num and prints (to tex) the tag |
| 66 |
ltx.__tag.func.store_mc_data (num,key,data): stores key=data in ltx.__tag.mc[num] |
| 67 |
ltx.__tag.func.store_mc_label (label,num): stores label=num in ltx.__tag.mc.labels |
| 68 |
ltx.__tag.func.store_mc_kid (mcnum,kid,page): stores the mc-kids of mcnum on page page |
| 69 |
ltx.__tag.func.store_mc_in_page(mcnum,mcpagecnt,page): stores in the page table the number of mcnum on this page |
| 70 |
ltx.__tag.func.store_struct_mcabs (structnum,mcnum): stores relations structnum<->mcnum (abs) |
| 71 |
ltx.__tag.func.mc_insert_kids (mcnum): inserts the /K entries for mcnum by wandering through the [kids] table |
| 72 |
ltx.__tag.func.mark_page_elements(box,mcpagecnt,mccntprev,mcopen,name,mctypeprev) : the main function |
| 73 |
ltx.__tag.func.mark_shipout (): a wrapper around the core function which inserts the last EMC |
| 74 |
ltx.__tag.func.fill_parent_tree_line (page): outputs the entries of the parenttree for this page |
| 75 |
ltx.__tag.func.output_parenttree(): outputs the content of the parenttree |
| 76 |
ltx.__tag.func.pdf_object_ref(name,index): outputs the object reference for the object name |
| 77 |
ltx.__tag.func.markspaceon(), ltx.__tag.func.markspaceoff(): (de)activates the marking of positions for space chars |
| 78 |
ltx.__tag.func.linkbin() returns the number of OBJR stored in the linkbin structure |
| 79 |
ltx.__tag.trace.show_mc_data (num,loglevel): shows ltx.__tag.mc[num] is the current log level is >= loglevel |
| 80 |
ltx.__tag.trace.show_all_mc_data (max,loglevel): shows a maximum about mc's if the current log level is >= loglevel |
| 81 |
ltx.__tag.trace.show_seq: shows a sequence (array) |
| 82 |
ltx.__tag.trace.show_struct_data (num): shows data of structure num |
| 83 |
ltx.__tag.trace.show_prop: shows a prop |
| 84 |
ltx.__tag.trace.log |
| 85 |
ltx.__tag.trace.showspaces : boolean |
| 86 |
|
| 87 |
ltx.tag.get_structnum: number, shows the current structure number |
| 88 |
ltx.tag.get_structnum_next: number, shows the next structure number |
| 89 |
--]] |
| 90 |
|
| 91 |
local mctypeattributeid = luatexbase.new_attribute ("g__tag_mc_type_attr") |
| 92 |
local mccntattributeid = luatexbase.new_attribute ("g__tag_mc_cnt_attr") |
| 93 |
local structnumattributeid = luatexbase.new_attribute ("g__tag_structnum_attr") |
| 94 |
local iwspaceOffattributeid = luatexbase.new_attribute ("g__tag_interwordspaceOff_attr") |
| 95 |
local iwspaceattributeid = luatexbase.new_attribute ("g__tag_interwordspace_attr") |
| 96 |
local iwfontattributeid = luatexbase.new_attribute ("g__tag_interwordfont_attr") |
| 97 |
local softhyphenattribute = luatexbase.new_attribute ("l__tag_softhyphen_attr") |
| 98 |
local tagunmarkedbool= token.create("g__tag_tagunmarked_bool") |
| 99 |
local truebool = token.create("c_true_bool") |
| 100 |
local softhyphenbool = token.create("g__tag_softhyphen_bool") |
| 101 |
|
| 102 |
local catlatex = luatexbase.registernumber("catcodetable@latex") |
| 103 |
local tableinsert = table.insert |
| 104 |
local nodeid = node.id |
| 105 |
local nodecopy = node.copy |
| 106 |
local nodegetattribute = node.get_attribute |
| 107 |
local nodesetattribute = node.set_attribute |
| 108 |
local nodehasattribute = node.has_attribute |
| 109 |
local nodenew = node.new |
| 110 |
local nodetail = node.tail |
| 111 |
local nodeslide = node.slide |
| 112 |
local noderemove = node.remove |
| 113 |
local nodetraverseid = node.traverse_id |
| 114 |
local nodetraverse = node.traverse |
| 115 |
local nodeinsertafter = node.insert_after |
| 116 |
local nodeinsertbefore = node.insert_before |
| 117 |
local pdfpageref = pdf.pageref |
| 118 |
|
| 119 |
local fonthashes = fonts.hashes |
| 120 |
local identifiers = fonthashes.identifiers |
| 121 |
local fontid = font.id |
| 122 |
|
| 123 |
local HLIST = node.id("hlist") |
| 124 |
local VLIST = node.id("vlist") |
| 125 |
local RULE = node.id("rule") |
| 126 |
local DISC = node.id("disc") |
| 127 |
local GLUE = node.id("glue") |
| 128 |
local GLYPH = node.id("glyph") |
| 129 |
local KERN = node.id("kern") |
| 130 |
local PENALTY = node.id("penalty") |
| 131 |
local LOCAL_PAR = node.id("local_par") |
| 132 |
local MATH = node.id("math") |
| 133 |
|
| 134 |
local NEXT = next |
| 135 |
local explicit_disc = 1 |
| 136 |
local regular_disc = 3 |
| 137 |
ltx = ltx or { } |
| 138 |
ltx.tag = ltx.tag or { } -- user commands |
| 139 |
ltx.__tag = ltx.__tag or { } |
| 140 |
ltx.__tag.mc = ltx.__tag.mc or { } -- mc data |
| 141 |
ltx.__tag.role = ltx.__tag.role or { } -- parent-child data |
| 142 |
ltx.__tag.role.states = ltx.__tag.role.states or { } -- the states |
| 143 |
ltx.__tag.role.index = ltx.__tag.role.index or { } -- standard types to index |
| 144 |
--- numbers |
| 145 |
ltx.__tag.role.matrix = ltx.__tag.role.matrix or { } -- implements the matrix |
| 146 |
ltx.__tag.struct = ltx.__tag.struct or { } -- struct data |
| 147 |
ltx.__tag.tables = ltx.__tag.tables or { } -- tables created with new prop and new seq. |
| 148 |
-- wasn't a so great idea ... |
| 149 |
-- g__tag_role_tags_seq used by tag<-> is in this tables! |
| 150 |
-- used for pure lua tables too now! |
| 151 |
ltx.__tag.page = ltx.__tag.page or { } -- page data, currently only i->{0->mcnum,1->mcnum,...} |
| 152 |
ltx.__tag.trace = ltx.__tag.trace or { } -- show commands |
| 153 |
ltx.__tag.func = ltx.__tag.func or { } -- functions |
| 154 |
ltx.__tag.conf = ltx.__tag.conf or { } -- configuration variables |
| 155 |
|
| 156 |
local __tag_get_struct_num = |
| 157 |
function() |
| 158 |
local a = token.get_macro("g__tag_struct_stack_current_tl") |
| 159 |
return a |
| 160 |
end |
| 161 |
|
| 162 |
local __tag_get_struct_counter = |
| 163 |
function() |
| 164 |
local a = tex.getcount("c@g__tag_struct_abs_int") |
| 165 |
return a |
| 166 |
end |
| 167 |
|
| 168 |
local __tag_get_struct_num_next = |
| 169 |
function() |
| 170 |
local a = tex.getcount("c@g__tag_struct_abs_int") + 1 |
| 171 |
return a |
| 172 |
end |
| 173 |
|
| 174 |
ltx.tag.get_struct_num = __tag_get_struct_num |
| 175 |
ltx.tag.get_struct_counter = __tag_get_struct_counter |
| 176 |
ltx.tag.get_struct_num_next = __tag_get_struct_num_next |
| 177 |
local __tag_log = |
| 178 |
function (message,loglevel) |
| 179 |
if (loglevel or 3) <= tex.count["l__tag_loglevel_int"] then |
| 180 |
texio.write_nl("tagpdf: ".. message) |
| 181 |
end |
| 182 |
end |
| 183 |
|
| 184 |
ltx.__tag.trace.log = __tag_log |
| 185 |
function ltx.__tag.trace.show_seq (seq) |
| 186 |
if (type(seq) == "table") then |
| 187 |
for i,v in ipairs(seq) do |
| 188 |
__tag_log ("[" .. i .. "] => " .. tostring(v),1) |
| 189 |
end |
| 190 |
else |
| 191 |
__tag_log ("sequence " .. tostring(seq) .. " not found",1) |
| 192 |
end |
| 193 |
end |
| 194 |
local __tag_pairs_prop = |
| 195 |
function (prop) |
| 196 |
local a = {} |
| 197 |
for n in pairs(prop) do tableinsert(a, n) end |
| 198 |
table.sort(a) |
| 199 |
local i = 0 -- iterator variable |
| 200 |
local iter = function () -- iterator function |
| 201 |
i = i + 1 |
| 202 |
if a[i] == nil then return nil |
| 203 |
else return a[i], prop[a[i]] |
| 204 |
end |
| 205 |
end |
| 206 |
return iter |
| 207 |
end |
| 208 |
|
| 209 |
function ltx.__tag.trace.show_prop (prop) |
| 210 |
if (type(prop) == "table") then |
| 211 |
for i,v in __tag_pairs_prop (prop) do |
| 212 |
__tag_log ("[" .. i .. "] => " .. tostring(v),1) |
| 213 |
end |
| 214 |
else |
| 215 |
__tag_log ("prop " .. tostring(prop) .. " not found or not a table",1) |
| 216 |
end |
| 217 |
end |
| 218 |
function ltx.__tag.trace.show_mc_data (num,loglevel) |
| 219 |
if ltx.__tag and ltx.__tag.mc and ltx.__tag.mc[num] then |
| 220 |
for k,v in pairs(ltx.__tag.mc[num]) do |
| 221 |
__tag_log ("mc"..num..": "..tostring(k).."=>"..tostring(v),loglevel) |
| 222 |
end |
| 223 |
if ltx.__tag.mc[num]["kids"] then |
| 224 |
__tag_log ("mc" .. num .. " has " .. #ltx.__tag.mc[num]["kids"] .. " kids",loglevel) |
| 225 |
for k,v in ipairs(ltx.__tag.mc[num]["kids"]) do |
| 226 |
__tag_log ("mc ".. num .. " kid "..k.." =>" .. v.kid.." on page " ..v.page,loglevel) |
| 227 |
end |
| 228 |
end |
| 229 |
else |
| 230 |
__tag_log ("mc"..num.." not found",loglevel) |
| 231 |
end |
| 232 |
end |
| 233 |
function ltx.__tag.trace.show_all_mc_data (min,max,loglevel) |
| 234 |
for i = min, max do |
| 235 |
ltx.__tag.trace.show_mc_data (i,loglevel) |
| 236 |
end |
| 237 |
texio.write_nl("") |
| 238 |
end |
| 239 |
|
| 240 |
function ltx.__tag.trace.show_struct_data (num) |
| 241 |
if ltx.__tag and ltx.__tag.struct and ltx.__tag.struct[num] then |
| 242 |
for k,v in ipairs(ltx.__tag.struct[num]) do |
| 243 |
__tag_log ("struct "..num..": "..tostring(k).."=>"..tostring(v),1) |
| 244 |
end |
| 245 |
else |
| 246 |
__tag_log ("struct "..num.." not found ",1) |
| 247 |
end |
| 248 |
end |
| 249 |
|
| 250 |
local __tag_get_mc_cnt_type_tag = function (n) |
| 251 |
local mccnt = nodegetattribute(n,mccntattributeid) or -1 |
| 252 |
local mctype = nodegetattribute(n,mctypeattributeid) or -1 |
| 253 |
local tag = ltx.__tag.func.get_tag_from(mctype) |
| 254 |
return mccnt,mctype,tag |
| 255 |
end |
| 256 |
local function __tag_get_mathsubtype (mathnode) |
| 257 |
if mathnode.subtype == 0 then |
| 258 |
subtype = "beginmath" |
| 259 |
else |
| 260 |
subtype = "endmath" |
| 261 |
end |
| 262 |
return subtype |
| 263 |
end |
| 264 |
ltx.__tag.tables.role_tag_attribute = {} |
| 265 |
ltx.__tag.tables.role_attribute_tag = {} |
| 266 |
local __tag_alloctag = |
| 267 |
function (tag) |
| 268 |
if not ltx.__tag.tables.role_tag_attribute[tag] then |
| 269 |
table.insert(ltx.__tag.tables.role_attribute_tag,tag) |
| 270 |
ltx.__tag.tables.role_tag_attribute[tag]=#ltx.__tag.tables.role_attribute_tag |
| 271 |
__tag_log ("Add "..tag.." "..ltx.__tag.tables.role_tag_attribute[tag],3) |
| 272 |
end |
| 273 |
end |
| 274 |
ltx.__tag.func.alloctag = __tag_alloctag |
| 275 |
local __tag_get_num_from = |
| 276 |
function (tag) |
| 277 |
if ltx.__tag.tables.role_tag_attribute[tag] then |
| 278 |
a= ltx.__tag.tables.role_tag_attribute[tag] |
| 279 |
else |
| 280 |
a= -1 |
| 281 |
end |
| 282 |
return a |
| 283 |
end |
| 284 |
|
| 285 |
ltx.__tag.func.get_num_from = __tag_get_num_from |
| 286 |
|
| 287 |
function ltx.__tag.func.output_num_from (tag) |
| 288 |
local num = __tag_get_num_from (tag) |
| 289 |
tex.sprint(catlatex,num) |
| 290 |
if num == -1 then |
| 291 |
__tag_log ("Unknown tag "..tag.." used") |
| 292 |
end |
| 293 |
end |
| 294 |
local __tag_get_tag_from = |
| 295 |
function (num) |
| 296 |
if ltx.__tag.tables.role_attribute_tag[num] then |
| 297 |
a = ltx.__tag.tables.role_attribute_tag[num] |
| 298 |
else |
| 299 |
a= "UNKNOWN" |
| 300 |
end |
| 301 |
return a |
| 302 |
end |
| 303 |
|
| 304 |
ltx.__tag.func.get_tag_from = __tag_get_tag_from |
| 305 |
|
| 306 |
function ltx.__tag.func.output_tag_from (num) |
| 307 |
tex.sprint(catlatex,__tag_get_tag_from (num)) |
| 308 |
end |
| 309 |
function ltx.__tag.func.store_mc_data (num,key,data) |
| 310 |
ltx.__tag.mc[num] = ltx.__tag.mc[num] or { } |
| 311 |
ltx.__tag.mc[num][key] = data |
| 312 |
__tag_log ("INFO TEX-STORE-MC-DATA: "..num.." => "..tostring(key).." => "..tostring(data),3) |
| 313 |
end |
| 314 |
function ltx.__tag.func.store_mc_label (label,num) |
| 315 |
ltx.__tag.mc["labels"] = ltx.__tag.mc["labels"] or { } |
| 316 |
ltx.__tag.mc.labels[label] = num |
| 317 |
end |
| 318 |
function ltx.__tag.func.store_mc_kid (mcnum,kid,page) |
| 319 |
__tag_log("INFO TAG-STORE-MC-KID: "..mcnum.." => " .. kid.." on page " .. page,3) |
| 320 |
ltx.__tag.mc[mcnum]["kids"] = ltx.__tag.mc[mcnum]["kids"] or { } |
| 321 |
local kidtable = {kid=kid,page=page} |
| 322 |
tableinsert(ltx.__tag.mc[mcnum]["kids"], kidtable ) |
| 323 |
end |
| 324 |
|
| 325 |
function ltx.__tag.func.mc_num_of_kids (mcnum) |
| 326 |
local num = 0 |
| 327 |
if ltx.__tag.mc[mcnum] and ltx.__tag.mc[mcnum]["kids"] then |
| 328 |
num = #ltx.__tag.mc[mcnum]["kids"] |
| 329 |
end |
| 330 |
__tag_log ("INFO MC-KID-NUMBERS: " .. mcnum .. "has " .. num .. "KIDS",4) |
| 331 |
return num |
| 332 |
end |
| 333 |
local __tag_backend_create_emc_node |
| 334 |
if tex.outputmode == 0 then |
| 335 |
if token.get_macro("c_sys_backend_str") == "dvipdfmx" then |
| 336 |
function __tag_backend_create_emc_node () |
| 337 |
local emcnode = nodenew("whatsit","special") |
| 338 |
emcnode.data = "pdf:code EMC" |
| 339 |
return emcnode |
| 340 |
end |
| 341 |
else -- assume a dvips variant |
| 342 |
function __tag_backend_create_emc_node () |
| 343 |
local emcnode = nodenew("whatsit","special") |
| 344 |
emcnode.data = "ps:SDict begin mark /EMC pdfmark end" |
| 345 |
return emcnode |
| 346 |
end |
| 347 |
end |
| 348 |
else -- pdf mode |
| 349 |
function __tag_backend_create_emc_node () |
| 350 |
local emcnode = nodenew("whatsit","pdf_literal") |
| 351 |
emcnode.data = "EMC" |
| 352 |
emcnode.mode=1 |
| 353 |
return emcnode |
| 354 |
end |
| 355 |
end |
| 356 |
|
| 357 |
local function __tag_insert_emc_node (head,current) |
| 358 |
local emcnode= __tag_backend_create_emc_node() |
| 359 |
head = node.insert_before(head,current,emcnode) |
| 360 |
return head |
| 361 |
end |
| 362 |
local __tag_backend_create_bmc_node |
| 363 |
if tex.outputmode == 0 then |
| 364 |
if token.get_macro("c_sys_backend_str") == "dvipdfmx" then |
| 365 |
function __tag_backend_create_bmc_node (tag) |
| 366 |
local bmcnode = nodenew("whatsit","special") |
| 367 |
bmcnode.data = "pdf:code /"..tag.." BMC" |
| 368 |
return bmcnode |
| 369 |
end |
| 370 |
else -- assume a dvips variant |
| 371 |
function __tag_backend_create_bmc_node (tag) |
| 372 |
local bmcnode = nodenew("whatsit","special") |
| 373 |
bmcnode.data = "ps:SDict begin mark/"..tag.." /BMC pdfmark end" |
| 374 |
return bmcnode |
| 375 |
end |
| 376 |
end |
| 377 |
else -- pdf mode |
| 378 |
function __tag_backend_create_bmc_node (tag) |
| 379 |
local bmcnode = nodenew("whatsit","pdf_literal") |
| 380 |
bmcnode.data = "/"..tag.." BMC" |
| 381 |
bmcnode.mode=1 |
| 382 |
return bmcnode |
| 383 |
end |
| 384 |
end |
| 385 |
|
| 386 |
local function __tag_insert_bmc_node (head,current,tag) |
| 387 |
local bmcnode = __tag_backend_create_bmc_node (tag) |
| 388 |
head = node.insert_before(head,current,bmcnode) |
| 389 |
return head |
| 390 |
end |
| 391 |
local __tag_backend_create_bdc_node |
| 392 |
|
| 393 |
if tex.outputmode == 0 then |
| 394 |
if token.get_macro("c_sys_backend_str") == "dvipdfmx" then |
| 395 |
function __tag_backend_create_bdc_node (tag,dict) |
| 396 |
local bdcnode = nodenew("whatsit","special") |
| 397 |
bdcnode.data = "pdf:code /"..tag.."<<"..dict..">> BDC" |
| 398 |
return bdcnode |
| 399 |
end |
| 400 |
else -- assume a dvips variant |
| 401 |
function __tag_backend_create_bdc_node (tag,dict) |
| 402 |
local bdcnode = nodenew("whatsit","special") |
| 403 |
bdcnode.data = "ps:SDict begin mark/"..tag.."<<"..dict..">> /BDC pdfmark end" |
| 404 |
return bdcnode |
| 405 |
end |
| 406 |
end |
| 407 |
else -- pdf mode |
| 408 |
function __tag_backend_create_bdc_node (tag,dict) |
| 409 |
local bdcnode = nodenew("whatsit","pdf_literal") |
| 410 |
bdcnode.data = "/"..tag.."<<"..dict..">> BDC" |
| 411 |
bdcnode.mode=1 |
| 412 |
return bdcnode |
| 413 |
end |
| 414 |
end |
| 415 |
|
| 416 |
local function __tag_insert_bdc_node (head,current,tag,dict) |
| 417 |
bdcnode= __tag_backend_create_bdc_node (tag,dict) |
| 418 |
head = node.insert_before(head,current,bdcnode) |
| 419 |
return head |
| 420 |
end |
| 421 |
local function __tag_pdf_object_ref (name,index) |
| 422 |
local object |
| 423 |
if ltx.pdf.object_id then |
| 424 |
object = ltx.pdf.object_id (name,index) ..' 0 R' |
| 425 |
else |
| 426 |
local tokenname = 'c__pdf_object_'..name..'/'..index..'_int' |
| 427 |
object = token.create(tokenname).mode ..' 0 R' |
| 428 |
end |
| 429 |
return object |
| 430 |
end |
| 431 |
ltx.__tag.func.pdf_object_ref = __tag_pdf_object_ref |
| 432 |
local function __tag_show_spacemark (head,current,color,height) |
| 433 |
local markcolor = color or "1 0 0" |
| 434 |
local markheight = height or 10 |
| 435 |
local pdfstring |
| 436 |
if tex.outputmode == 0 then |
| 437 |
-- ignore dvi mode for now |
| 438 |
else |
| 439 |
pdfstring = node.new("whatsit","pdf_literal") |
| 440 |
pdfstring.data = |
| 441 |
string.format("q "..markcolor.." RG "..markcolor.." rg 0.4 w 0 %g m 0 %g l S Q",-3,markheight) |
| 442 |
head = node.insert_after(head,current,pdfstring) |
| 443 |
return head |
| 444 |
end |
| 445 |
end |
| 446 |
local function __tag_fakespace() |
| 447 |
tex.setattribute(iwspaceattributeid,1) |
| 448 |
tex.setattribute(iwfontattributeid,font.current()) |
| 449 |
end |
| 450 |
ltx.__tag.func.fakespace = __tag_fakespace |
| 451 |
--[[ a function to mark up places where real space chars should be inserted |
| 452 |
it only sets an attribute. |
| 453 |
--]] |
| 454 |
|
| 455 |
local function __tag_mark_spaces (head) |
| 456 |
local inside_math = false |
| 457 |
for n in nodetraverse(head) do |
| 458 |
local id = n.id |
| 459 |
if id == GLYPH then |
| 460 |
local glyph = n |
| 461 |
default_currfontid = glyph.font |
| 462 |
if glyph.next and (glyph.next.id == GLUE) |
| 463 |
and not inside_math and (glyph.next.width >0) |
| 464 |
then |
| 465 |
nodesetattribute(glyph.next,iwspaceattributeid,1) |
| 466 |
nodesetattribute(glyph.next,iwfontattributeid,glyph.font) |
| 467 |
-- for debugging |
| 468 |
if ltx.__tag.trace.showspaces then |
| 469 |
__tag_show_spacemark (head,glyph) |
| 470 |
end |
| 471 |
elseif glyph.next and (glyph.next.id==KERN) and not inside_math then |
| 472 |
local kern = glyph.next |
| 473 |
if kern.next and (kern.next.id== GLUE) and (kern.next.width >0) |
| 474 |
-- the attribute is also set on the kern in case the kern+glue is |
| 475 |
-- discarded at a line break tagging issue #1102 |
| 476 |
-- TODO iterate back through all discardable nodes. |
| 477 |
then |
| 478 |
nodesetattribute(kern,iwspaceattributeid,1) |
| 479 |
nodesetattribute(kern,iwfontattributeid,glyph.font) |
| 480 |
nodesetattribute(kern.next,iwspaceattributeid,1) |
| 481 |
nodesetattribute(kern.next,iwfontattributeid,glyph.font) |
| 482 |
end |
| 483 |
end |
| 484 |
-- look also back |
| 485 |
if glyph.prev and (glyph.prev.id == GLUE) |
| 486 |
and not inside_math |
| 487 |
and (glyph.prev.width >0) |
| 488 |
and not nodehasattribute(glyph.prev,iwspaceattributeid) |
| 489 |
then |
| 490 |
nodesetattribute(glyph.prev,iwspaceattributeid,1) |
| 491 |
nodesetattribute(glyph.prev,iwfontattributeid,glyph.font) |
| 492 |
-- for debugging |
| 493 |
if ltx.__tag.trace.showspaces then |
| 494 |
__tag_show_spacemark (head,glyph) |
| 495 |
end |
| 496 |
end |
| 497 |
elseif id == PENALTY then |
| 498 |
local glyph = n |
| 499 |
-- __tag_log ("PENALTY ".. n.subtype.."VALUE"..n.penalty,3) |
| 500 |
if glyph.next and (glyph.next.id == GLUE) |
| 501 |
and not inside_math and (glyph.next.width >0) and n.subtype==0 |
| 502 |
then |
| 503 |
nodesetattribute(glyph.next,iwspaceattributeid,1) |
| 504 |
-- changed 2024-01-18, issue #72 |
| 505 |
nodesetattribute(glyph.next,iwfontattributeid,default_currfontid) |
| 506 |
-- for debugging |
| 507 |
if ltx.__tag.trace.showspaces then |
| 508 |
__tag_show_spacemark (head,glyph) |
| 509 |
end |
| 510 |
end |
| 511 |
elseif id == MATH then |
| 512 |
inside_math = (n.subtype == 0) |
| 513 |
end |
| 514 |
end |
| 515 |
return head |
| 516 |
end |
| 517 |
local function __tag_activate_mark_space () |
| 518 |
if not luatexbase.in_callback ("pre_linebreak_filter","markspaces") then |
| 519 |
luatexbase.add_to_callback("pre_linebreak_filter",__tag_mark_spaces,"markspaces") |
| 520 |
luatexbase.add_to_callback("hpack_filter",__tag_mark_spaces,"markspaces") |
| 521 |
end |
| 522 |
end |
| 523 |
|
| 524 |
ltx.__tag.func.markspaceon=__tag_activate_mark_space |
| 525 |
|
| 526 |
local function __tag_deactivate_mark_space () |
| 527 |
if luatexbase.in_callback ("pre_linebreak_filter","markspaces") then |
| 528 |
luatexbase.remove_from_callback("pre_linebreak_filter","markspaces") |
| 529 |
luatexbase.remove_from_callback("hpack_filter","markspaces") |
| 530 |
end |
| 531 |
end |
| 532 |
|
| 533 |
ltx.__tag.func.markspaceoff=__tag_deactivate_mark_space |
| 534 |
local default_space_char = nodenew(GLYPH) |
| 535 |
local default_fontid = fontid("TU/lmr/m/n/10") |
| 536 |
local default_currfontid = fontid("TU/lmr/m/n/10") |
| 537 |
default_space_char.char = 32 |
| 538 |
default_space_char.font = default_fontid |
| 539 |
local function __tag_font_has_space (fontid) |
| 540 |
t= fonts.hashes.identifiers[fontid] |
| 541 |
if luaotfload.aux.slot_of_name(fontid,"space") |
| 542 |
or t and t.characters and t.characters[32] and t.characters[32]["unicode"]==32 |
| 543 |
then |
| 544 |
return true |
| 545 |
else |
| 546 |
return false |
| 547 |
end |
| 548 |
end |
| 549 |
local function __tag_space_chars_shipout (box) |
| 550 |
local head = box.head |
| 551 |
if head then |
| 552 |
for n in node.traverse(head) do |
| 553 |
local spaceattr = -1 |
| 554 |
if not nodehasattribute(n,iwspaceOffattributeid) then |
| 555 |
spaceattr = nodegetattribute(n,iwspaceattributeid) or -1 |
| 556 |
end |
| 557 |
if n.id == HLIST then -- enter the hlist |
| 558 |
__tag_space_chars_shipout (n) |
| 559 |
elseif n.id == VLIST then -- enter the vlist |
| 560 |
__tag_space_chars_shipout (n) |
| 561 |
elseif n.id == GLUE then |
| 562 |
if ltx.__tag.trace.showspaces and spaceattr==1 then |
| 563 |
__tag_show_spacemark (head,n,"0 1 0") |
| 564 |
end |
| 565 |
if spaceattr==1 then |
| 566 |
local space |
| 567 |
local space_char = node.copy(default_space_char) |
| 568 |
local curfont = nodegetattribute(n,iwfontattributeid) |
| 569 |
__tag_log ("INFO SPACE-FUNCTION-FONT: ".. tostring(curfont),3) |
| 570 |
if curfont and |
| 571 |
-- luaotfload.aux.slot_of_name(curfont,"space") |
| 572 |
__tag_font_has_space (curfont) |
| 573 |
then |
| 574 |
space_char.font=curfont |
| 575 |
end |
| 576 |
head, space = node.insert_before(head, n, space_char) -- |
| 577 |
n.width = n.width - space.width |
| 578 |
space.attr = n.attr |
| 579 |
end |
| 580 |
end |
| 581 |
end |
| 582 |
box.head = head |
| 583 |
end |
| 584 |
end |
| 585 |
|
| 586 |
function ltx.__tag.func.space_chars_shipout (box) |
| 587 |
__tag_space_chars_shipout (box) |
| 588 |
end |
| 589 |
function ltx.__tag.func.mc_insert_kids (mcnum,single) |
| 590 |
if ltx.__tag.mc[mcnum] then |
| 591 |
__tag_log("INFO TEX-MC-INSERT-KID-TEST: " .. mcnum,4) |
| 592 |
if ltx.__tag.mc[mcnum]["kids"] then |
| 593 |
if #ltx.__tag.mc[mcnum]["kids"] > 1 and single==1 then |
| 594 |
tex.sprint(catlatex,"[") |
| 595 |
end |
| 596 |
for i,kidstable in ipairs( ltx.__tag.mc[mcnum]["kids"] ) do |
| 597 |
local kidnum = kidstable["kid"] |
| 598 |
local kidpage = kidstable["page"] |
| 599 |
local kidpageobjnum = pdfpageref(kidpage) |
| 600 |
__tag_log("INFO TEX-MC-INSERT-KID: " .. mcnum .. |
| 601 |
" insert KID " ..i.. |
| 602 |
" with num " .. kidnum .. |
| 603 |
" on page " .. kidpage.."/"..kidpageobjnum,3) |
| 604 |
tex.sprint(catlatex,"<</Type /MCR /Pg "..kidpageobjnum .. " 0 R /MCID "..kidnum.. ">> " ) |
| 605 |
end |
| 606 |
if #ltx.__tag.mc[mcnum]["kids"] > 1 and single==1 then |
| 607 |
tex.sprint(catlatex,"]") |
| 608 |
end |
| 609 |
else |
| 610 |
-- this is typically not a problem, e.g. empty hbox in footer/header can |
| 611 |
-- trigger this warning. |
| 612 |
__tag_log("WARN TEX-MC-INSERT-NO-KIDS: "..mcnum.." has no kids",2) |
| 613 |
if single==1 then |
| 614 |
tex.sprint(catlatex,"null") |
| 615 |
end |
| 616 |
end |
| 617 |
else |
| 618 |
__tag_log("WARN TEX-MC-INSERT-MISSING: "..mcnum.." doesn't exist",0) |
| 619 |
end |
| 620 |
end |
| 621 |
function ltx.__tag.func.store_struct_mcabs (structnum,mcnum) |
| 622 |
ltx.__tag.struct[structnum]=ltx.__tag.struct[structnum] or { } |
| 623 |
ltx.__tag.struct[structnum]["mc"]=ltx.__tag.struct[structnum]["mc"] or { } |
| 624 |
-- a structure can contain more than on mc chunk, the content should be ordered |
| 625 |
tableinsert(ltx.__tag.struct[structnum]["mc"],mcnum) |
| 626 |
__tag_log("INFO TEX-MC-INTO-STRUCT: ".. |
| 627 |
mcnum.." inserted in struct "..structnum,3) |
| 628 |
-- but every mc can only be in one structure |
| 629 |
ltx.__tag.mc[mcnum]= ltx.__tag.mc[mcnum] or { } |
| 630 |
ltx.__tag.mc[mcnum]["parent"] = structnum |
| 631 |
end |
| 632 |
|
| 633 |
-- pay attention: lua counts arrays from 1, tex pages from one |
| 634 |
-- mcid and arrays in pdf count from 0. |
| 635 |
function ltx.__tag.func.store_mc_in_page (mcnum,mcpagecnt,page) |
| 636 |
ltx.__tag.page[page] = ltx.__tag.page[page] or {} |
| 637 |
ltx.__tag.page[page][mcpagecnt] = mcnum |
| 638 |
__tag_log("INFO TAG-MC-INTO-PAGE: page " .. page .. |
| 639 |
": inserting MCID " .. mcpagecnt .. " => " .. mcnum,3) |
| 640 |
end |
| 641 |
local function __tag_update_mc_attributes (head,mcnum,type) |
| 642 |
for n in node.traverse(head) do |
| 643 |
node.set_attribute(n,mccntattributeid,mcnum) |
| 644 |
node.set_attribute(n,mctypeattributeid,type) |
| 645 |
if n.id == HLIST or n.id == VLIST then |
| 646 |
__tag_update_mc_attributes (n.list,mcnum,type) |
| 647 |
end |
| 648 |
end |
| 649 |
return head |
| 650 |
end |
| 651 |
ltx.__tag.func.update_mc_attributes = __tag_update_mc_attributes |
| 652 |
--[[ |
| 653 |
Now follows the core function |
| 654 |
It wades through the shipout box and checks the attributes |
| 655 |
ARGUMENTS |
| 656 |
box: is a box, |
| 657 |
mcpagecnt: num, the current page cnt of mc (should start at -1 in shipout box), needed for recursion |
| 658 |
mccntprev: num, the attribute cnt of the previous node/whatever - if different we have a chunk border |
| 659 |
mcopen: num, records if some bdc/emc is open |
| 660 |
These arguments are only needed for log messages, if not present are replaces by fix strings: |
| 661 |
name: string to describe the box |
| 662 |
mctypeprev: num, the type attribute of the previous node/whatever |
| 663 |
|
| 664 |
there are lots of logging messages currently. Should be cleaned up in due course. |
| 665 |
One should also find ways to make the function shorter. |
| 666 |
--]] |
| 667 |
|
| 668 |
function ltx.__tag.func.mark_page_elements (box,mcpagecnt,mccntprev,mcopen,name,mctypeprev) |
| 669 |
local name = name or ("SOMEBOX") |
| 670 |
local mctypeprev = mctypeprev or -1 |
| 671 |
local abspage = status.total_pages + 1 -- the real counter is increased |
| 672 |
-- inside the box so one off |
| 673 |
-- if the callback is not used. (???) |
| 674 |
__tag_log ("INFO TAG-ABSPAGE: " .. abspage,3) |
| 675 |
__tag_log ("INFO TAG-ARGS: pagecnt".. mcpagecnt.. |
| 676 |
" prev "..mccntprev .. |
| 677 |
" type prev "..mctypeprev,4) |
| 678 |
__tag_log ("INFO TAG-TRAVERSING-BOX: ".. tostring(name).. |
| 679 |
" TYPE ".. node.type(node.getid(box)),3) |
| 680 |
local head = box.head -- ShipoutBox is a vlist? |
| 681 |
if head then |
| 682 |
mccnthead, mctypehead,taghead = __tag_get_mc_cnt_type_tag (head) |
| 683 |
__tag_log ("INFO TAG-HEAD: " .. |
| 684 |
node.type(node.getid(head)).. |
| 685 |
" MC"..tostring(mccnthead).. |
| 686 |
" => TAG " .. tostring(mctypehead).. |
| 687 |
" => ".. tostring(taghead),3) |
| 688 |
else |
| 689 |
__tag_log ("INFO TAG-NO-HEAD: head is ".. |
| 690 |
tostring(head),3) |
| 691 |
end |
| 692 |
for n in node.traverse(head) do |
| 693 |
local mccnt, mctype, tag = __tag_get_mc_cnt_type_tag (n) |
| 694 |
local spaceattr = nodegetattribute(n,iwspaceattributeid) or -1 |
| 695 |
__tag_log ("INFO TAG-NODE: ".. |
| 696 |
node.type(node.getid(n)).. |
| 697 |
" MC".. tostring(mccnt).. |
| 698 |
" => TAG ".. tostring(mctype).. |
| 699 |
" => " .. tostring(tag),3) |
| 700 |
if n.id == HLIST |
| 701 |
then -- enter the hlist |
| 702 |
mcopen,mcpagecnt,mccntprev,mctypeprev= |
| 703 |
ltx.__tag.func.mark_page_elements (n,mcpagecnt,mccntprev,mcopen,"INTERNAL HLIST",mctypeprev) |
| 704 |
elseif n.id == VLIST then -- enter the vlist |
| 705 |
mcopen,mcpagecnt,mccntprev,mctypeprev= |
| 706 |
ltx.__tag.func.mark_page_elements (n,mcpagecnt,mccntprev,mcopen,"INTERNAL VLIST",mctypeprev) |
| 707 |
elseif n.id == GLUE and not n.leader then -- at glue real space chars are inserted, but this has |
| 708 |
-- been done if the previous shipout wandering, so here it is ignored |
| 709 |
elseif n.id == LOCAL_PAR then -- local_par is ignored |
| 710 |
elseif n.id == PENALTY then -- penalty is ignored |
| 711 |
elseif n.id == KERN then -- kern is ignored |
| 712 |
__tag_log ("INFO TAG-KERN-SUBTYPE: ".. |
| 713 |
node.type(node.getid(n)).." "..n.subtype,4) |
| 714 |
else |
| 715 |
-- math is currently only logged. |
| 716 |
-- we could mark the whole as math |
| 717 |
-- for inner processing the mlist_to_hlist callback is probably needed. |
| 718 |
if n.id == MATH then |
| 719 |
__tag_log("INFO TAG-MATH-SUBTYPE: ".. |
| 720 |
node.type(node.getid(n)).." "..__tag_get_mathsubtype(n),4) |
| 721 |
end |
| 722 |
-- endmath |
| 723 |
__tag_log("INFO TAG-MC-COMPARE: current ".. |
| 724 |
mccnt.." prev "..mccntprev,4) |
| 725 |
if mccnt~=mccntprev then -- a new mc chunk |
| 726 |
__tag_log ("INFO TAG-NEW-MC-NODE: ".. |
| 727 |
node.type(node.getid(n)).. |
| 728 |
" MC"..tostring(mccnt).. |
| 729 |
" <=> PREVIOUS "..tostring(mccntprev),4) |
| 730 |
if mcopen~=0 then -- there is a chunk open, close it (hope there is only one ... |
| 731 |
box.list=__tag_insert_emc_node (box.list,n) |
| 732 |
mcopen = mcopen - 1 |
| 733 |
__tag_log ("INFO TAG-INSERT-EMC: " .. |
| 734 |
mcpagecnt .. " MCOPEN = " .. mcopen,3) |
| 735 |
if mcopen ~=0 then |
| 736 |
__tag_log ("WARN TAG-OPEN-MC: " .. mcopen,1) |
| 737 |
end |
| 738 |
end |
| 739 |
if ltx.__tag.mc[mccnt] then |
| 740 |
if ltx.__tag.mc[mccnt]["artifact"] then |
| 741 |
__tag_log("INFO TAG-INSERT-ARTIFACT: ".. |
| 742 |
tostring(ltx.__tag.mc[mccnt]["artifact"]),3) |
| 743 |
if ltx.__tag.mc[mccnt]["artifact"] == "" then |
| 744 |
box.list = __tag_insert_bmc_node (box.list,n,"Artifact") |
| 745 |
else |
| 746 |
box.list = __tag_insert_bdc_node (box.list,n,"Artifact", "/Type /"..ltx.__tag.mc[mccnt]["artifact"]) |
| 747 |
end |
| 748 |
else |
| 749 |
__tag_log("INFO TAG-INSERT-TAG: ".. |
| 750 |
tostring(tag),3) |
| 751 |
mcpagecnt = mcpagecnt +1 |
| 752 |
__tag_log ("INFO TAG-INSERT-BDC: "..mcpagecnt,3) |
| 753 |
local dict= "/MCID "..mcpagecnt |
| 754 |
if ltx.__tag.mc[mccnt]["raw"] then |
| 755 |
__tag_log("INFO TAG-USE-RAW: ".. |
| 756 |
tostring(ltx.__tag.mc[mccnt]["raw"]),3) |
| 757 |
dict= dict .. " " .. ltx.__tag.mc[mccnt]["raw"] |
| 758 |
end |
| 759 |
if ltx.__tag.mc[mccnt]["alt"] then |
| 760 |
__tag_log("INFO TAG-USE-ALT: ".. |
| 761 |
tostring(ltx.__tag.mc[mccnt]["alt"]),3) |
| 762 |
dict= dict .. " " .. ltx.__tag.mc[mccnt]["alt"] |
| 763 |
end |
| 764 |
if ltx.__tag.mc[mccnt]["lang"] then |
| 765 |
__tag_log("INFO TAG-USE-LANG: ".. |
| 766 |
tostring(ltx.__tag.mc[mccnt]["lang"]),3) |
| 767 |
dict= dict .. " " .. ltx.__tag.mc[mccnt]["lang"] |
| 768 |
end |
| 769 |
if ltx.__tag.mc[mccnt]["actualtext"] then |
| 770 |
__tag_log("INFO TAG-USE-ACTUALTEXT: ".. |
| 771 |
tostring(ltx.__tag.mc[mccnt]["actualtext"]),3) |
| 772 |
dict= dict .. " " .. ltx.__tag.mc[mccnt]["actualtext"] |
| 773 |
end |
| 774 |
box.list = __tag_insert_bdc_node (box.list,n,tag, dict) |
| 775 |
ltx.__tag.func.store_mc_kid (mccnt,mcpagecnt,abspage) |
| 776 |
ltx.__tag.func.store_mc_in_page(mccnt,mcpagecnt,abspage) |
| 777 |
ltx.__tag.trace.show_mc_data (mccnt,3) |
| 778 |
end |
| 779 |
mcopen = mcopen + 1 |
| 780 |
else |
| 781 |
if tagunmarkedbool.mode == truebool.mode then |
| 782 |
__tag_log("INFO TAG-NOT-TAGGED: this has not been tagged, using artifact",2) |
| 783 |
box.list = __tag_insert_bmc_node (box.list,n,"Artifact") |
| 784 |
mcopen = mcopen + 1 |
| 785 |
else |
| 786 |
__tag_log("WARN TAG-NOT-TAGGED: this has not been tagged",1) |
| 787 |
end |
| 788 |
end |
| 789 |
mccntprev = mccnt |
| 790 |
end |
| 791 |
end -- end if |
| 792 |
end -- end for |
| 793 |
if head then |
| 794 |
mccnthead, mctypehead,taghead = __tag_get_mc_cnt_type_tag (head) |
| 795 |
__tag_log ("INFO TAG-ENDHEAD: " .. |
| 796 |
node.type(node.getid(head)).. |
| 797 |
" MC"..tostring(mccnthead).. |
| 798 |
" => TAG "..tostring(mctypehead).. |
| 799 |
" => "..tostring(taghead),4) |
| 800 |
else |
| 801 |
__tag_log ("INFO TAG-ENDHEAD: ".. tostring(head),4) |
| 802 |
end |
| 803 |
__tag_log ("INFO TAG-QUITTING-BOX ".. |
| 804 |
tostring(name).. |
| 805 |
" TYPE ".. node.type(node.getid(box)),4) |
| 806 |
return mcopen,mcpagecnt,mccntprev,mctypeprev |
| 807 |
end |
| 808 |
|
| 809 |
function ltx.__tag.func.mark_shipout (box) |
| 810 |
mcopen = ltx.__tag.func.mark_page_elements (box,-1,-100,0,"Shipout",-1) |
| 811 |
if mcopen~=0 then -- there is a chunk open, close it (hope there is only one ... |
| 812 |
local emcnode = __tag_backend_create_emc_node () |
| 813 |
local list = box.list |
| 814 |
if list then |
| 815 |
list = node.insert_after (list,node.tail(list),emcnode) |
| 816 |
mcopen = mcopen - 1 |
| 817 |
__tag_log ("INFO SHIPOUT-INSERT-LAST-EMC: MCOPEN " .. mcopen,3) |
| 818 |
else |
| 819 |
__tag_log ("WARN SHIPOUT-UPS: this shouldn't happen",0) |
| 820 |
end |
| 821 |
if mcopen ~=0 then |
| 822 |
__tag_log ("WARN SHIPOUT-MC-OPEN: " .. mcopen,1) |
| 823 |
end |
| 824 |
end |
| 825 |
end |
| 826 |
|
| 827 |
function ltx.__tag.func.fill_parent_tree_line (page) |
| 828 |
-- we need to get page-> i=kid -> mcnum -> structnum |
| 829 |
-- pay attention: the kid numbers and the page number in the parent tree start with 0! |
| 830 |
local numsentry ="" |
| 831 |
local pdfpage = page-1 |
| 832 |
if ltx.__tag.page[page] and ltx.__tag.page[page][0] then |
| 833 |
mcchunks=#ltx.__tag.page[page] |
| 834 |
__tag_log("INFO PARENTTREE-NUM: page ".. |
| 835 |
page.." has "..mcchunks.."+1 Elements ",4) |
| 836 |
for i=0,mcchunks do |
| 837 |
-- what does this log?? |
| 838 |
__tag_log("INFO PARENTTREE-CHUNKS: ".. |
| 839 |
ltx.__tag.page[page][i],4) |
| 840 |
end |
| 841 |
if mcchunks == 0 then |
| 842 |
-- only one chunk so no need for an array |
| 843 |
local mcnum = ltx.__tag.page[page][0] |
| 844 |
local structnum = ltx.__tag.mc[mcnum]["parent"] |
| 845 |
local propname = "g__tag_struct_"..structnum.."_prop" |
| 846 |
--local objref = ltx.__tag.tables[propname]["objref"] or "XXXX" |
| 847 |
local objref = __tag_pdf_object_ref('__tag/struct',structnum) |
| 848 |
__tag_log("INFO PARENTTREE-STRUCT-OBJREF: =====>".. |
| 849 |
tostring(objref),5) |
| 850 |
numsentry = pdfpage .. " [".. objref .. "]" |
| 851 |
__tag_log("INFO PARENTTREE-NUMENTRY: page " .. |
| 852 |
page.. " num entry = ".. numsentry,3) |
| 853 |
else |
| 854 |
numsentry = pdfpage .. " [" |
| 855 |
for i=0,mcchunks do |
| 856 |
local mcnum = ltx.__tag.page[page][i] |
| 857 |
local structnum = ltx.__tag.mc[mcnum]["parent"] or 0 |
| 858 |
local propname = "g__tag_struct_"..structnum.."_prop" |
| 859 |
--local objref = ltx.__tag.tables[propname]["objref"] or "XXXX" |
| 860 |
local objref = __tag_pdf_object_ref('__tag/struct',structnum) |
| 861 |
numsentry = numsentry .. " ".. objref |
| 862 |
end |
| 863 |
numsentry = numsentry .. "] " |
| 864 |
__tag_log("INFO PARENTTREE-NUMENTRY: page " .. |
| 865 |
page.. " num entry = ".. numsentry,3) |
| 866 |
end |
| 867 |
else |
| 868 |
__tag_log ("INFO PARENTTREE-NO-DATA: page "..page,3) |
| 869 |
numsentry = pdfpage.." []" |
| 870 |
end |
| 871 |
return numsentry |
| 872 |
end |
| 873 |
|
| 874 |
function ltx.__tag.func.output_parenttree (abspage) |
| 875 |
for i=1,abspage do |
| 876 |
line = ltx.__tag.func.fill_parent_tree_line (i) .. "^^J" |
| 877 |
tex.sprint(catlatex,line) |
| 878 |
end |
| 879 |
end |
| 880 |
do |
| 881 |
local properties = node.get_properties_table() |
| 882 |
local is_soft_hyphen_prop = 'tagpdf.rewrite-softhyphen.is_soft_hyphen' |
| 883 |
local hyphen_char = 0x2D |
| 884 |
local soft_hyphen_char = 0xAD |
| 885 |
local softhyphen_fonts = setmetatable({}, {__index = function(t, fid) |
| 886 |
local fdir = identifiers[fid] |
| 887 |
local format = fdir and fdir.format |
| 888 |
local result = (format == 'opentype' or format == 'truetype') |
| 889 |
local characters = fdir and fdir.characters |
| 890 |
result = result and (characters and characters[soft_hyphen_char]) ~= nil |
| 891 |
t[fid] = result |
| 892 |
return result |
| 893 |
end}) |
| 894 |
local function process_softhyphen_pre(head, _context, _dir) |
| 895 |
if softhyphenbool.mode ~= truebool.mode then return true end |
| 896 |
for disc, sub in node.traverse_id(DISC, head) do |
| 897 |
if sub == explicit_disc or sub == regular_disc then |
| 898 |
for n, _ch, _f in node.traverse_char(disc.pre) do |
| 899 |
local props = properties[n] |
| 900 |
if not props then |
| 901 |
props = {} |
| 902 |
properties[n] = props |
| 903 |
end |
| 904 |
props[is_soft_hyphen_prop] = true |
| 905 |
end |
| 906 |
end |
| 907 |
end |
| 908 |
return true |
| 909 |
end |
| 910 |
|
| 911 |
local function process_softhyphen_post(head, _context, _dir) |
| 912 |
if softhyphenbool.mode ~= truebool.mode then return true end |
| 913 |
for disc, sub in node.traverse_id(DISC, head) do |
| 914 |
for n, ch, fid in node.traverse_glyph(disc.pre) do |
| 915 |
local props = properties[n] |
| 916 |
if softhyphen_fonts[fid] and ch == hyphen_char and props and props[is_soft_hyphen_prop] then |
| 917 |
if nodegetattribute(n,softhyphenattribute) then |
| 918 |
n.char = soft_hyphen_char |
| 919 |
props.glyph_info = nil |
| 920 |
else |
| 921 |
nodesetattribute(n,mctypeattributeid,-2147483647) |
| 922 |
nodesetattribute(n,mccntattributeid,-2147483647) |
| 923 |
end |
| 924 |
end |
| 925 |
end |
| 926 |
end |
| 927 |
return true |
| 928 |
end |
| 929 |
|
| 930 |
luatexbase.add_to_callback('pre_shaping_filter', process_softhyphen_pre, 'tagpdf.rewrite-softhyphen') |
| 931 |
luatexbase.add_to_callback('post_shaping_filter', process_softhyphen_post, 'tagpdf.rewrite-softhyphen') |
| 932 |
end |
| 933 |
local function role_get_parent_child_rule (parent,child) |
| 934 |
local state= |
| 935 |
ltx.__tag.role.matrix[ltx.__tag.role.index[parent]] |
| 936 |
and ltx.__tag.role.matrix[ltx.__tag.role.index[parent]][ltx.__tag.role.index[child]] or 0 |
| 937 |
return state |
| 938 |
end |
| 939 |
ltx.__tag.func.role_get_parent_child_rule=role_get_parent_child_rule |
| 940 |
function check_update_stashed (struct,loglevel,loop) |
| 941 |
loop = (loop or 0) + 1 |
| 942 |
if loop > 10 then |
| 943 |
__tag_log ('Warning: Too deeply nested stashed structures',0) |
| 944 |
return |
| 945 |
end |
| 946 |
__tag_log ('updating parentrole for stashed structure '..struct,loglevel) |
| 947 |
local parent = ltx.__tag.tables['g__tag_struct_'..struct..'_prop']['parentnum'] |
| 948 |
if parent then |
| 949 |
local ptag = |
| 950 |
string.match(ltx.__tag.tables['g__tag_struct_'..parent..'_prop']['parentrole'], "{(.-)}{(.-)}") |
| 951 |
if ptag == 'STASHED' then |
| 952 |
-- look at the parent and update it first |
| 953 |
check_update_stashed (parent,loglevel,loop) |
| 954 |
end |
| 955 |
-- now copy the parent role from the parent |
| 956 |
ltx.__tag.tables['g__tag_struct_'..struct..'_prop']['parentrole'] |
| 957 |
= |
| 958 |
ltx.__tag.tables['g__tag_struct_'..parent..'_prop']['parentrole'] |
| 959 |
__tag_log |
| 960 |
('new parentrole: ' .. ltx.__tag.tables['g__tag_struct_'..struct..'_prop']['parentrole'], loglevel) |
| 961 |
else |
| 962 |
__tag_log ('Warning: structure '..struct.. 'has no parent.',0) |
| 963 |
end |
| 964 |
end |
| 965 |
|
| 966 |
function check_parent_child_rules (loglevel) |
| 967 |
texio.write_nl('\n') |
| 968 |
__tag_log ('checking parent-child rules ...' ,0) |
| 969 |
for i=2,ltx.tag.get_struct_counter() do |
| 970 |
local t,tNS= |
| 971 |
string.match(ltx.__tag.tables['g__tag_struct_'..i..'_prop']['tag'], "{(.-)}{(.-)}") |
| 972 |
local r,rNS= |
| 973 |
string.match(ltx.__tag.tables['g__tag_struct_'..i..'_prop']['rolemap'], "{(.-)}{(.-)}") |
| 974 |
local p,pNS= |
| 975 |
string.match(ltx.__tag.tables['g__tag_struct_'..i..'_prop']['parentrole'], "{(.-)}{(.-)}") |
| 976 |
local parent=ltx.__tag.tables['g__tag_struct_'..i..'_prop']['parentnum'] |
| 977 |
if parent then |
| 978 |
__tag_log (i..': '.. t..':'..tNS,loglevel) |
| 979 |
__tag_log (i..': '.. r..':'..rNS,loglevel) |
| 980 |
__tag_log (i..': '.. p..':'..pNS,loglevel) |
| 981 |
__tag_log ('parent of ' ..i..': '.. parent,loglevel ) |
| 982 |
if p == 'STASHED' then |
| 983 |
check_update_stashed (i,loglevel,0) |
| 984 |
p,pNS= |
| 985 |
string.match(ltx.__tag.tables['g__tag_struct_'..i..'_prop']['parentrole'], "{(.-)}{(.-)}") |
| 986 |
end |
| 987 |
local pt,ptNS= |
| 988 |
string.match(ltx.__tag.tables['g__tag_struct_'..parent..'_prop']['tag'], "{(.-)}{(.-)}") |
| 989 |
local pr,prNS= |
| 990 |
string.match(ltx.__tag.tables['g__tag_struct_'..parent..'_prop']['rolemap'], "{(.-)}{(.-)}") |
| 991 |
local pp,ppNS= |
| 992 |
string.match(ltx.__tag.tables['g__tag_struct_'..parent..'_prop']['parentrole'], "{(.-)}{(.-)}") |
| 993 |
if pp == 'STASHED' then |
| 994 |
check_update_stashed (parent,loglevel,0) |
| 995 |
pp,ppNS= |
| 996 |
string.match(ltx.__tag.tables['g__tag_struct_'..parent..'_prop']['parentrole'], "{(.-)}{(.-)}") |
| 997 |
end |
| 998 |
__tag_log (parent..': '.. pt..':'..ptNS,loglevel) |
| 999 |
__tag_log (parent..': '.. pr..':'..prNS,loglevel) |
| 1000 |
__tag_log (parent..': '.. pp..':'..ppNS,loglevel) |
| 1001 |
-- now check the rule. |
| 1002 |
-- at first rolemap of child against rolemap of parent. |
| 1003 |
local state=ltx.__tag.func.role_get_parent_child_rule (pr,r) |
| 1004 |
__tag_log ('rule of '..pr.."->"..r..' is '..state,loglevel) |
| 1005 |
-- if the state is 7 we check against parentrole of the parent |
| 1006 |
if state == 7 then |
| 1007 |
state=ltx.__tag.func.role_get_parent_child_rule (pp,r) |
| 1008 |
__tag_log ('Parent-Child relation '..pp.."->"..r..' is '..state,loglevel) |
| 1009 |
end |
| 1010 |
if state == 0 then |
| 1011 |
__tag_log |
| 1012 |
('Warning: Parent-Child relation ' |
| 1013 |
..ptNS..':'..pt..' -> '..tNS..':'..t..' is unknown',0) |
| 1014 |
__tag_log |
| 1015 |
('Structure ' ..parent..' -> '..i,0) |
| 1016 |
end |
| 1017 |
if state == -1 then |
| 1018 |
__tag_log |
| 1019 |
('Warning: Parent-Child relation ' |
| 1020 |
..ptNS..':'..pt..' -> '..tNS..':'..t..' is not allowed',0) |
| 1021 |
__tag_log |
| 1022 |
('Structure ' ..parent..' -> '..i,0) |
| 1023 |
end |
| 1024 |
-- check also for MC |
| 1025 |
state =ltx.__tag.func.role_get_parent_child_rule ( r ,'MC') |
| 1026 |
local curtag=r |
| 1027 |
if state == 7 then |
| 1028 |
state =ltx.__tag.func.role_get_parent_child_rule ( p ,'MC') |
| 1029 |
local curtag=p |
| 1030 |
end |
| 1031 |
if state == -1 then |
| 1032 |
if ltx.__tag.struct[i] and NEXT(ltx.__tag.struct[i]) then |
| 1033 |
__tag_log |
| 1034 |
('Warning: Real content (MC) is not allowed in ' ..curtag,0) |
| 1035 |
end |
| 1036 |
end |
| 1037 |
__tag_log('=======================================',loglevel) |
| 1038 |
end |
| 1039 |
end -- end for |
| 1040 |
end |
| 1041 |
|
| 1042 |
ltx.__tag.func.check_parent_child_rules=check_parent_child_rules |
| 1043 |
|
| 1044 |
local linkbincnt = 0 |
| 1045 |
|
| 1046 |
function ltx.__tag.func.linkbin() |
| 1047 |
return linkbincnt |
| 1048 |
end |
| 1049 |
|
| 1050 |
if luatexbase.callbacktypes['linksplit'] then |
| 1051 |
luatexbase.add_to_callback('linksplit', function(start_link, position) |
| 1052 |
if start_link == nil then return end |
| 1053 |
local structnum = |
| 1054 |
node.get_attribute(start_link,luatexbase.attributes.g__tag_structnum_attr) |
| 1055 |
if structnum and structnum > -1 then |
| 1056 |
local s = ltx.__tag.tables['g__tag_struct_'..structnum..'_prop']['rolemap'] |
| 1057 |
if s and (string.find(s,'Link') or string.find(s,'Reference')) then |
| 1058 |
local struct_insert_annot_shipout = token.create'__tag_struct_insert_annot_shipout:nnn' |
| 1059 |
local parentnum = tex.count['c@g__tag_parenttree_obj_int'] |
| 1060 |
start_link.link_attr = |
| 1061 |
start_link.link_attr .. |
| 1062 |
' /LTEX_position /' .. position .. |
| 1063 |
'/StructParent ' .. parentnum |
| 1064 |
tex.sprint(catlatex,struct_insert_annot_shipout,'{'.. |
| 1065 |
structnum..'}{'.. |
| 1066 |
start_link.objnum..' 0 R}{'.. |
| 1067 |
parentnum ..'}') |
| 1068 |
-- the counter must be set explicitly as struct_insert_annot_shipout doesn't do it! |
| 1069 |
tex.setcount('global','c@g__tag_parenttree_obj_int',parentnum +1) |
| 1070 |
__tag_log(position .. " link part has object id " .. start_link.objnum .. " and structparent id " .. parentnum,2) |
| 1071 |
else |
| 1072 |
local linkbin = token.get_macro("c__tag_struct_link_bin_tl") |
| 1073 |
if linkbin then |
| 1074 |
local struct_insert_annot_shipout = token.create'__tag_struct_insert_annot_shipout:nnn' |
| 1075 |
local parentnum = tex.count['c@g__tag_parenttree_obj_int'] |
| 1076 |
start_link.link_attr = string.gsub (start_link.link_attr, "/Contents%s+<%w+>","") |
| 1077 |
start_link.link_attr = |
| 1078 |
start_link.link_attr .. |
| 1079 |
' /LTEX_position /' .. position .. |
| 1080 |
'/StructParent ' .. parentnum .. '/Contents (artifact)' |
| 1081 |
tex.sprint(catlatex,struct_insert_annot_shipout,'{'.. |
| 1082 |
linkbin..'}{'.. |
| 1083 |
start_link.objnum..' 0 R}{'.. |
| 1084 |
parentnum ..'}') |
| 1085 |
tex.setcount('global','c@g__tag_parenttree_obj_int',parentnum +1) |
| 1086 |
linkbincnt=linkbincnt+1 |
| 1087 |
if linkbincnt == 1 then |
| 1088 |
__tag_log('Info: some Link annotation(s) are not in Link or Reference structure elements',0) |
| 1089 |
__tag_log(' moved into a container at the end of the structure tree\n',0) |
| 1090 |
end |
| 1091 |
else |
| 1092 |
__tag_log('Warning: Link not in Link or Reference structure element',0) |
| 1093 |
__tag_log('OBJR not created',0) |
| 1094 |
__tag_log('',0) |
| 1095 |
end |
| 1096 |
end |
| 1097 |
end |
| 1098 |
end, 'tagpdf') |
| 1099 |
end |
| 1100 |
-- |
| 1101 |
-- End of File `tagpdf.lua'. |