| 1 |
--[[ |
| 2 |
This file defines and returns the main function |
| 3 |
* write_elem(element,stash) |
| 4 |
which adds structure elements/tagging commands to the mathml |
| 5 |
|
| 6 |
The function has two arguments: tree (xml-tree), stash (boolean). |
| 7 |
The function is loaded by luamml-tex as `write_struct` and used there in |
| 8 |
save_result as `write_struct(mlist_result)` (so with stash=false/nil) |
| 9 |
and as `write_struct(xml, true)` in the callback. |
| 10 |
--]] |
| 11 |
|
| 12 |
-- Create tokens from the main tagpdf commands. |
| 13 |
local struct_begin = token.create'tag_struct_begin:n' |
| 14 |
local struct_use = token.create'tag_struct_use:n' |
| 15 |
local struct_use_num = token.create'tag_struct_use_num:n' |
| 16 |
local struct_end = token.create'tag_struct_end:' |
| 17 |
local struct_prop_gput = token.create'__tag_struct_prop_gput:nnn' |
| 18 |
|
| 19 |
local mc_begin = token.create'tag_mc_begin:n' |
| 20 |
local mc_end = token.create'tag_mc_end:' |
| 21 |
|
| 22 |
local tagpdfsetup = token.create'tagpdfsetup' |
| 23 |
|
| 24 |
local lbrace, rbrace = token.new(string.byte'{', 1), token.new(string.byte'}', 2) |
| 25 |
|
| 26 |
local function escape_name(name) |
| 27 |
return name |
| 28 |
end |
| 29 |
|
| 30 |
local function escape_string(str) |
| 31 |
return str |
| 32 |
end |
| 33 |
|
| 34 |
local ltx |
| 35 |
local function get_ltx() |
| 36 |
ltx = _ENV.ltx |
| 37 |
if not ltx then |
| 38 |
tex.error("LaTeX PDF support not loaded", {"Maybe try adding \\DocumentMetadata."}) |
| 39 |
ltx = {pdf = {object_id = function() return 0 end}, __tag = {tables = {}}} |
| 40 |
end |
| 41 |
function get_ltx() |
| 42 |
return ltx |
| 43 |
end |
| 44 |
return get_ltx() |
| 45 |
end |
| 46 |
|
| 47 |
-- a function to retrieve the object number of the mathml NS. |
| 48 |
local mathml_ns_obj |
| 49 |
local function get_mathml_ns_obj() |
| 50 |
mathml_ns_obj = get_ltx().pdf.object_id'tag/NS/mathml' |
| 51 |
if not mathml_ns_obj then |
| 52 |
tex.error("Failed to find MathML namespace", {"The PDF support does not know the mathml namespace"}) |
| 53 |
mathml_ns_obj = 0 |
| 54 |
end |
| 55 |
function get_mathml_ns_obj() |
| 56 |
return mathml_ns_obj |
| 57 |
end |
| 58 |
return get_mathml_ns_obj() |
| 59 |
end |
| 60 |
|
| 61 |
local tag_tables |
| 62 |
local function get_tag_tables() |
| 63 |
tag_tables = assert(get_ltx().__tag.tables) |
| 64 |
function get_tag_tables() |
| 65 |
return tag_tables |
| 66 |
end |
| 67 |
return get_tag_tables() |
| 68 |
end |
| 69 |
|
| 70 |
local function get_struct_num_next() |
| 71 |
get_struct_num_next = get_ltx().tag.get_struct_num_next |
| 72 |
return get_struct_num_next() |
| 73 |
end |
| 74 |
|
| 75 |
-- a function to create and manage PDF MathML attributes. This is PDF 2.0 specific |
| 76 |
local attribute_counter = 0 |
| 77 |
local attributes = setmetatable({}, {__index = function(t, k) |
| 78 |
attribute_counter = attribute_counter + 1 |
| 79 |
local attr_name = string.format('luamml_attr_%i', attribute_counter) |
| 80 |
t[k] = attr_name |
| 81 |
tex.runtoks(function() |
| 82 |
tex.sprint(-2, tagpdfsetup, lbrace, 'role/new-attribute=', lbrace, attr_name, rbrace, lbrace, '/O/NSO/NS ', get_mathml_ns_obj(), ' 0 R') |
| 83 |
tex.cprint(12, k, rbrace, rbrace) |
| 84 |
end) |
| 85 |
return attr_name |
| 86 |
end}) |
| 87 |
|
| 88 |
local attribute_object_refs = setmetatable({}, {__index = function(t, k) |
| 89 |
local objref = pdf.immediateobj(string.format('<< /O/NSO/NS %i 0 R%s >>', get_mathml_ns_obj(), k)) .. ' 0 R' |
| 90 |
t[k] = objref |
| 91 |
return objref |
| 92 |
end}) |
| 93 |
|
| 94 |
-- the mc-(luatex)-attributes of tagpdf |
| 95 |
local mc_type = luatexbase.attributes.g__tag_mc_type_attr |
| 96 |
local mc_cnt = luatexbase.attributes.g__tag_mc_cnt_attr |
| 97 |
|
| 98 |
local attrs = {} |
| 99 |
local function build_attributes(tree_node) |
| 100 |
local i = 0 |
| 101 |
for attr, val in next, tree_node do |
| 102 |
if type(attr) == 'string' and not string.find(attr, ':') and attr ~= 'xmlns' then |
| 103 |
i = i + 1 |
| 104 |
attrs[i] = string.format('/%s(%s)', escape_name(attr), escape_string(val)) |
| 105 |
end |
| 106 |
end |
| 107 |
if i == 0 then return end |
| 108 |
table.sort(attrs) |
| 109 |
|
| 110 |
local attr_list = table.concat(attrs) |
| 111 |
for j = 1, i do attrs[j] = nil end |
| 112 |
|
| 113 |
return attr_list |
| 114 |
end |
| 115 |
|
| 116 |
local stash_cnt = 0 |
| 117 |
local function write_elem(tree, stash) |
| 118 |
if tree[':struct'] then |
| 119 |
return tex.runtoks(function() |
| 120 |
return tex.sprint(-2, struct_use, lbrace, tree[':struct'], rbrace) |
| 121 |
end) |
| 122 |
end |
| 123 |
|
| 124 |
local attrs = build_attributes(tree) |
| 125 |
|
| 126 |
if tree[':structnum'] then |
| 127 |
return tex.runtoks(function() |
| 128 |
local structnum = tree[':structnum'] |
| 129 |
if attrs then |
| 130 |
local current_attrs = get_tag_tables()[string.format('g__tag_struct_%i_prop', structnum)].A |
| 131 |
local stripped_attrs = current_attrs and current_attrs:match'^%s*%[(.*)%]%s$' or current_attrs |
| 132 |
attrs = attribute_object_refs[attrs] |
| 133 |
local new_attrs = stripped_attrs and string.format('[%s %s]', stripped_attrs, attrs) or attrs |
| 134 |
tex.sprint(-2, struct_prop_gput, lbrace, structnum, rbrace, lbrace, 'A', rbrace, lbrace, new_attrs, rbrace) |
| 135 |
end |
| 136 |
return tex.sprint(-2, struct_use_num, lbrace, tree[':structnum'], rbrace) |
| 137 |
end) |
| 138 |
end |
| 139 |
if not tree[0] then print('ERR', require'inspect'(tree)) end |
| 140 |
|
| 141 |
if stash then |
| 142 |
tree[':structnum'] = get_struct_num_next() |
| 143 |
stash = ', stash' |
| 144 |
end |
| 145 |
|
| 146 |
attrs = attrs and attributes[attrs] |
| 147 |
tex.sprint(-2, struct_begin, lbrace, 'tag=', tree[0], '/mathml') -- 'tag=mo/mathml' is supported syntax |
| 148 |
if stash then tex.sprint(-2, stash) end |
| 149 |
if attrs then tex.sprint(-2, ', attribute=' .. attrs) end |
| 150 |
tex.sprint(rbrace) |
| 151 |
|
| 152 |
if tree[':nodes'] then |
| 153 |
local n = tree[':nodes'] |
| 154 |
tex.runtoks(function() |
| 155 |
-- luamml-convert sets :actual on some nodes in delim_to_table and acc_to_table. |
| 156 |
if tree[':artifact'] then |
| 157 |
tex.sprint(-2, mc_begin, lbrace, 'artifact', rbrace) |
| 158 |
elseif tree[':actual'] then |
| 159 |
tex.sprint(-2, mc_begin, lbrace, 'tag=Span,actualtext=') |
| 160 |
tex.cprint(12, tree[':actual'], rbrace) |
| 161 |
else |
| 162 |
tex.sprint(-2, mc_begin, lbrace, 'tag=', tree[0], rbrace) |
| 163 |
end |
| 164 |
-- NOTE: This will also flush all previous sprint's... That's often annoying, but in this |
| 165 |
-- case actually intentional. |
| 166 |
end) |
| 167 |
local mct, mcc = tex.attribute[mc_type], tex.attribute[mc_cnt] |
| 168 |
for i = 1, #n do |
| 169 |
node.set_attribute(n[i], mc_type, mct) |
| 170 |
node.set_attribute(n[i], mc_cnt, mcc) |
| 171 |
end |
| 172 |
tex.runtoks(function() |
| 173 |
tex.sprint(mc_end) |
| 174 |
end) |
| 175 |
end |
| 176 |
for _, elem in ipairs(tree) do |
| 177 |
if type(elem) ~= 'string' then |
| 178 |
write_elem(elem) |
| 179 |
end |
| 180 |
end |
| 181 |
tex.runtoks(function() |
| 182 |
tex.sprint(struct_end) |
| 183 |
end) |
| 184 |
end |
| 185 |
|
| 186 |
return { |
| 187 |
write = function(element, stash) |
| 188 |
return write_elem(element, stash) |
| 189 |
end, |
| 190 |
restore_after_math = function() |
| 191 |
tex.runtoks(function() |
| 192 |
tex.sprint(-2, mc_begin, lbrace, rbrace) |
| 193 |
end) |
| 194 |
end, |
| 195 |
} |