| 1 |
--[[ |
| 2 |
This file defines the luafunctions |
| 3 |
* \RegisterFamilyMapping |
| 4 |
* \RegisterTextFamily |
| 5 |
* \luamml_get_last_mathml_stream:e |
| 6 |
* \luamml_begin_single_file: |
| 7 |
* \luamml_end_single_file: |
| 8 |
* \__luamml_register_output_hook:N |
| 9 |
* \__luamml_disable_output_hook:N |
| 10 |
|
| 11 |
It returns |
| 12 |
* save_result = save_result, (function) |
| 13 |
* labelled = labelled_mathml (a table containing labelled mathml pieces) |
| 14 |
|
| 15 |
It adds a function to the callback |
| 16 |
* pre_mlist_to_hlist_filter |
| 17 |
--]] |
| 18 |
|
| 19 |
-- load needed functions |
| 20 |
local mlist_to_mml = require'luamml-convert' |
| 21 |
local process_mlist = mlist_to_mml.process |
| 22 |
local make_root = mlist_to_mml.make_root |
| 23 |
local register_family = mlist_to_mml.register_family |
| 24 |
local has_relevant_attributes = mlist_to_mml.has_relevant_attributes |
| 25 |
|
| 26 |
local mappings = require'luamml-legacy-mappings' |
| 27 |
local write_xml = require'luamml-xmlwriter' |
| 28 |
local write_struct = require'luamml-structelemwriter'.write |
| 29 |
local restore_struct = require'luamml-structelemwriter'.restore_after_math |
| 30 |
|
| 31 |
local filename_token = token.create'l__luamml_filename_tl' |
| 32 |
local label_token = token.create'l__luamml_label_tl' |
| 33 |
local left_brace = token.new(string.byte'{', 1) |
| 34 |
local right_brace = token.new(string.byte'}', 2) |
| 35 |
|
| 36 |
local output_hook_token |
| 37 |
local global_text_families = {} |
| 38 |
|
| 39 |
-- ???? |
| 40 |
local text_families_meta = {__index = function(t, fam) |
| 41 |
if fam == nil then return nil end |
| 42 |
local assignment = global_text_families[fam] |
| 43 |
if assignment == nil then |
| 44 |
local fid = node.family_font(fam) |
| 45 |
local fontdir = font.getfont(fid) |
| 46 |
if not fontdir then |
| 47 |
-- FIXME(?): If there is no font... |
| 48 |
error'Please load your fonts?!?' |
| 49 |
end |
| 50 |
assignment = not (fontdir.MathConstants and next(fontdir.MathConstants)) |
| 51 |
end |
| 52 |
t[fam] = assignment |
| 53 |
return assignment |
| 54 |
end} |
| 55 |
|
| 56 |
local properties = node.get_properties_table() |
| 57 |
|
| 58 |
-- retrieve the mode numbers for math, horizontal and vertical mode. |
| 59 |
local mmode, hmode, vmode do |
| 60 |
local result, input = {}, tex.getmodevalues() |
| 61 |
for k,v in next, tex.getmodevalues() do |
| 62 |
if v == 'math' then mmode = k |
| 63 |
elseif v == 'horizontal' then hmode = k |
| 64 |
elseif v == 'vertical' then vmode = k |
| 65 |
else assert(v == 'unset') |
| 66 |
end |
| 67 |
end |
| 68 |
assert(mmode and hmode and vmode) |
| 69 |
end |
| 70 |
|
| 71 |
--[[ Documentation for luafunction \RegisterFamilyMapping |
| 72 |
Two argument \RegisterFamilyMapping{<family>}{<encoding>} |
| 73 |
Example usage: \RegisterFamilyMapping\symsymbols{oms} |
| 74 |
The mappings are defined in luamml-legacy-mappings.lua. |
| 75 |
Currently only oml, oms, omx known. |
| 76 |
The remapping function register_family is in luamml-convert (=register_remap) |
| 77 |
TODO: document in luamml.dtx |
| 78 |
--]] |
| 79 |
|
| 80 |
local funcid = luatexbase.new_luafunction'RegisterFamilyMapping' |
| 81 |
token.set_lua('RegisterFamilyMapping', funcid, 'protected') |
| 82 |
lua.get_functions_table()[funcid] = function() |
| 83 |
local fam = token.scan_int() |
| 84 |
local mapping = token.scan_string() |
| 85 |
if mappings[mapping] then |
| 86 |
register_family(fam, mappings[mapping]) |
| 87 |
if global_text_families[fam] == nil then |
| 88 |
global_text_families[fam] = false |
| 89 |
end |
| 90 |
else |
| 91 |
tex.error(string.format('Unknown font mapping %q', mapping)) |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
--[[ Documentation for luafunction \RegisterTextFamily |
| 96 |
Two arguments \RegisterTextFamily{<family>}{<????>} |
| 97 |
There is no example (and logging name was wrong) |
| 98 |
--]] |
| 99 |
|
| 100 |
local funcid = luatexbase.new_luafunction'RegisterTextFamily' |
| 101 |
token.set_lua('RegisterTextFamily', funcid, 'protected') |
| 102 |
lua.get_functions_table()[funcid] = function() |
| 103 |
local fam = token.scan_int() |
| 104 |
local _kind = token.scan_string() |
| 105 |
global_text_families[fam] = true |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
-- A helper function to copy a table. |
| 110 |
|
| 111 |
local function shallow_copy(t) |
| 112 |
local tt = {} |
| 113 |
for k,v in next, t do |
| 114 |
tt[k] = v |
| 115 |
end |
| 116 |
return tt |
| 117 |
end |
| 118 |
|
| 119 |
--[[ Documentation for flag \l__luamml_flag_int |
| 120 |
Possible flag values of \l__luamml_flag_int: |
| 121 |
0: Skip |
| 122 |
1: Generate MathML, but only save it for later usage in startmath node |
| 123 |
3: Normal (This is the only supported one in display mode) ?? what is display mode? display math? |
| 124 |
11: Generate MathML structure elements |
| 125 |
|
| 126 |
More generally, \l__luamml_flag_int: is a bitfield with the defined bits: |
| 127 |
Bit 5-7: See Bit 4 |
| 128 |
Bit 4: Overwrite mathstyle with bit 9-11 |
| 129 |
Bit 3: Generate MathML structure elements |
| 130 |
Bit 2: Change root element name for saved element |
| 131 |
Bit 1: Save MathML as a fully converted formula |
| 132 |
Bit 0: Save MathML for later usage in startmath node. Ignored for display math. |
| 133 |
--]] |
| 134 |
|
| 135 |
local out_file |
| 136 |
|
| 137 |
local mlist_result |
| 138 |
|
| 139 |
local undefined_cmd = token.command_id'undefined_cs' |
| 140 |
local call_cmd = token.command_id'call' |
| 141 |
|
| 142 |
local labelled_mathml = {} |
| 143 |
local labelled_mathml_core = {} |
| 144 |
|
| 145 |
--[[ Documentation for function save_result |
| 146 |
Core function, exported as save_result. |
| 147 |
|
| 148 |
Used in the callback when math is saved in the startmath node. |
| 149 |
|
| 150 |
loaded in luamml-amsmath.lua, luamml-array.lua, luamml-table.lua but |
| 151 |
used only in luamml-amsmath.lua in \__luamml_amsmath_finalize_table:n |
| 152 |
|
| 153 |
Three arguments: xml, display, structelem |
| 154 |
Argument xml is the table, display a number. If display<2 it is a display math. |
| 155 |
?? Argument structelem is not used ?? |
| 156 |
|
| 157 |
make_root is defined in luamml-convert.lua (=to_math). It either converts the |
| 158 |
top level mrow to math or adds a math element. |
| 159 |
|
| 160 |
out_file is set by \luamml_begin_single_file: |
| 161 |
|
| 162 |
write_xml is from luamml_xmlwriter. It takes a "mathml-tree" as first argument |
| 163 |
and writes it out as xml. The second argument is a boolean and decides if the xml uses |
| 164 |
pretty indentation. It is calculated here from \l__luamml_pretty_int with a bitwise AND. |
| 165 |
|
| 166 |
output_hook_token is set by \__luamml_register_output_hook:N, typically this should save |
| 167 |
the result into a command or write it into a file, see latex-lab-math.dtx for an example. |
| 168 |
|
| 169 |
write_struct is defined in luamml-stuctelemwriter.lua (called write_elem there). It writes |
| 170 |
out the tree together with structure element commands. |
| 171 |
--]] |
| 172 |
|
| 173 |
local function save_result(xml, display, structelem) |
| 174 |
mlist_result = make_root(xml, display and 0 or 2) |
| 175 |
if out_file then |
| 176 |
out_file:write(write_xml(mlist_result, tex.count.l__luamml_pretty_int & 1 == 1):sub(2) .. '\n') |
| 177 |
else |
| 178 |
token.put_next(filename_token) |
| 179 |
local filename = token.scan_argument() |
| 180 |
if filename ~= '' then |
| 181 |
assert(io.open(filename, 'w')) |
| 182 |
:write(write_xml(mlist_result, tex.count.l__luamml_pretty_int & 1 == 1):sub(2) .. '\n') |
| 183 |
:close() |
| 184 |
end |
| 185 |
end |
| 186 |
local tracing = tex.count.tracingmathml > 1 |
| 187 |
if tracing then |
| 188 |
texio.write_nl(write_xml(mlist_result, tex.count.l__luamml_pretty_int & 2 == 2) .. '\n') |
| 189 |
end |
| 190 |
if output_hook_token then |
| 191 |
tex.runtoks(function() |
| 192 |
tex.sprint(-2, output_hook_token, left_brace, write_xml(mlist_result, tex.count.l__luamml_pretty_int & 4 == 4), right_brace) |
| 193 |
end) |
| 194 |
end |
| 195 |
if tex.count.l__luamml_flag_int & 8 == 8 then |
| 196 |
write_struct(mlist_result) |
| 197 |
restore_struct() |
| 198 |
end |
| 199 |
return mlist_result |
| 200 |
end |
| 201 |
|
| 202 |
-- Can be overridden by the exported set_extract_eqno. Should either return nil (like the default implementation) |
| 203 |
-- or a function accepting and returning a pair of (mathml node, core node) which will be applied as a result filter. |
| 204 |
local function extract_eqno() end |
| 205 |
|
| 206 |
local saved_eqno_processor |
| 207 |
luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(mlist, style) |
| 208 |
if tex.nest.top.mode == mmode then -- This is a equation label generated with \eqno |
| 209 |
saved_eqno_processor = extract_eqno(mlist, style) |
| 210 |
return true |
| 211 |
end |
| 212 |
local restored_eqno_processor = saved_eqno_processor |
| 213 |
saved_eqno_processor = nil |
| 214 |
|
| 215 |
local flag = tex.count.l__luamml_flag_int |
| 216 |
if flag & 3 == 0 then |
| 217 |
return true |
| 218 |
end |
| 219 |
local display = style == 'display' |
| 220 |
local startmath = tex.nest.top.tail -- Must come before any write_struct calls which adds nodes |
| 221 |
style = flag & 16 == 16 and flag>>5 & 0x7 or display and 0 or 2 |
| 222 |
-- process_mlist==mlist_to_mml.process from luamml_convert. |
| 223 |
local xml, core = process_mlist(mlist, style, setmetatable({}, text_families_meta)) |
| 224 |
if restored_eqno_processor then |
| 225 |
xml, core = restored_eqno_processor(xml, core) |
| 226 |
end |
| 227 |
-- bit 1 set |
| 228 |
if flag & 2 == 2 then |
| 229 |
xml = save_result(shallow_copy(xml), display) |
| 230 |
end |
| 231 |
-- bit 2 set, change root element if \l__luamml_root_tl is different to mrow. |
| 232 |
if flag & 4 == 4 then |
| 233 |
local element_type = token.get_macro'l__luamml_root_tl' |
| 234 |
if element_type ~= 'mrow' then |
| 235 |
if xml[0] == 'mrow' and not has_relevant_attributes(xml) then |
| 236 |
xml[0] = element_type |
| 237 |
else |
| 238 |
xml = {[0] = element_type, xml} |
| 239 |
end |
| 240 |
end |
| 241 |
end |
| 242 |
-- bit 0 set |
| 243 |
if not display and flag & 1 == 1 then |
| 244 |
local props = properties[startmath] |
| 245 |
if not props then |
| 246 |
props = {} |
| 247 |
properties[startmath] = props |
| 248 |
end |
| 249 |
props.saved_mathml_table, props.saved_mathml_core = xml, core |
| 250 |
token.put_next(label_token) |
| 251 |
local label = token.scan_argument() |
| 252 |
if label ~= '' then |
| 253 |
if labelled_mathml[label] then |
| 254 |
tex.error('MathML Label already in use', { |
| 255 |
'A MathML expression has a label which is already used by another \z |
| 256 |
formula. If you do not want to label this formula with a unique \z |
| 257 |
label, set a empty label instead.'}) |
| 258 |
else |
| 259 |
labelled_mathml[label], labelled_mathml_core[label] = xml, core |
| 260 |
end |
| 261 |
end |
| 262 |
-- bit 3 set and bit 1 unset. |
| 263 |
if flag & 10 == 8 then |
| 264 |
write_struct(xml, true) -- This modifies xml in-place to reference the structure element |
| 265 |
end |
| 266 |
end |
| 267 |
return true |
| 268 |
end, 'dump_list') |
| 269 |
|
| 270 |
--[[ Documentation of luafunction luamml_get_last_mathml_stream:e |
| 271 |
this creates a stream object with mathml from the last equation. It should be used after |
| 272 |
the equation. As it nils then the list it can be used only once. |
| 273 |
The return value is an object number which can then be used for example to create a |
| 274 |
filespec dictionary or to reference the stream in some other place. |
| 275 |
It takes an argument: additional dictionary keys for the stream object, e.g. |
| 276 |
/Type /EmbeddedFile or /Params. |
| 277 |
see luamml-demo for an example use. |
| 278 |
TODO: document in luamml.dtx |
| 279 |
--]] |
| 280 |
funcid = luatexbase.new_luafunction'luamml_get_last_mathml_stream:e' |
| 281 |
token.set_lua('luamml_get_last_mathml_stream:e', funcid) |
| 282 |
lua.get_functions_table()[funcid] = function() |
| 283 |
if not mlist_result then |
| 284 |
tex.error('No current MathML data', { |
| 285 |
"I was asked to provide MathML code for the last formula, but there weren't any new formulas since you last asked." |
| 286 |
}) |
| 287 |
end |
| 288 |
local mml = write_xml(mlist_result, tex.count.l__luamml_pretty_int & 8 == 8) |
| 289 |
if tex.count.tracingmathml == 1 then |
| 290 |
texio.write_nl(mml .. '\n') |
| 291 |
end |
| 292 |
tex.sprint(-2, tostring(pdf.immediateobj('stream', mml, '/Subtype/application#2Fmathml+xml' .. token.scan_argument(true)))) |
| 293 |
mlist_result = nil |
| 294 |
end |
| 295 |
|
| 296 |
--[[ Documentation of luafunctions luamml_begin_single_file:,luamml_end_single_file: |
| 297 |
This opens and closes out_file used in save_result. |
| 298 |
The filename (stored in filename_token=\l__luamml_filename_tl) |
| 299 |
is expanded in the begin function. |
| 300 |
--]] |
| 301 |
funcid = luatexbase.new_luafunction'luamml_begin_single_file:' |
| 302 |
token.set_lua('luamml_begin_single_file:', funcid, 'protected') |
| 303 |
lua.get_functions_table()[funcid] = function() |
| 304 |
token.put_next(filename_token) |
| 305 |
local filename = token.scan_argument() |
| 306 |
if filename ~= '' then |
| 307 |
out_file = assert(io.open(filename, 'w')) |
| 308 |
end |
| 309 |
end |
| 310 |
|
| 311 |
funcid = luatexbase.new_luafunction'luamml_end_single_file:' |
| 312 |
token.set_lua('luamml_end_single_file:', funcid, 'protected') |
| 313 |
lua.get_functions_table()[funcid] = function() |
| 314 |
if out_file then |
| 315 |
out_file:close() |
| 316 |
out_file = nil |
| 317 |
end |
| 318 |
end |
| 319 |
|
| 320 |
--[[ Documentation of luafunctions __luamml_register_output_hook:N, |
| 321 |
and __luamml_disable_output_hook:N |
| 322 |
This fills/clears the output_hook_token used in save_result. |
| 323 |
TODO currently clashing uses in luamml.dtx and latex-lab-math. |
| 324 |
--]] |
| 325 |
funcid = luatexbase.new_luafunction'__luamml_register_output_hook:N' |
| 326 |
token.set_lua('__luamml_register_output_hook:N', funcid, 'protected') |
| 327 |
lua.get_functions_table()[funcid] = function() |
| 328 |
output_hook_token = token.get_next() |
| 329 |
end |
| 330 |
|
| 331 |
funcid = luatexbase.new_luafunction'__luamml_disable_output_hook:' |
| 332 |
token.set_lua('__luamml_disable_output_hook:', funcid, 'protected') |
| 333 |
lua.get_functions_table()[funcid] = function() |
| 334 |
output_hook_token = nil |
| 335 |
end |
| 336 |
|
| 337 |
-- ?? |
| 338 |
local annotate_context = require'luamml-tex-annotate' |
| 339 |
annotate_context.data.mathml = labelled_mathml |
| 340 |
annotate_context.data.mathml_core = labelled_mathml_core |
| 341 |
|
| 342 |
return { |
| 343 |
save_result = save_result, |
| 344 |
labelled = labelled_mathml, |
| 345 |
labelled_core = labelled_mathml_core, |
| 346 |
set_extract_eqno = function(handler) extract_eqno = handler end, |
| 347 |
} |