| 1 |
--[[ This file returns the function |
| 2 |
* to_unicode which returns characters inside an mtext |
| 3 |
|
| 4 |
The file is loaded by |
| 5 |
* luamml-convert.lua (as to_text) |
| 6 |
* luamml-amsmath.lua (as to_text) |
| 7 |
* luamml-array.lua (as to_text) |
| 8 |
--]] |
| 9 |
|
| 10 |
local properties = node.get_properties_table() |
| 11 |
|
| 12 |
local function to_unicode(head, tail) |
| 13 |
local result, i = {[0] = 'mtext'}, 0 |
| 14 |
local characters, last_fid |
| 15 |
local iter, state, n = node.traverse(head) |
| 16 |
while true do |
| 17 |
local id, sub n, id, sub = iter(state, n) |
| 18 |
if not n or n == tail then break end |
| 19 |
local props = properties[n] |
| 20 |
if props and props.glyph_info then |
| 21 |
i = i+1 |
| 22 |
result[i] = glyph_info |
| 23 |
else |
| 24 |
local char, fid = node.is_glyph(n) |
| 25 |
if char then |
| 26 |
if fid ~= last_fid then |
| 27 |
local fontdir = font.getfont(fid) |
| 28 |
characters, last_fid = fontdir.characters, fid |
| 29 |
end |
| 30 |
local uni = characters[char] |
| 31 |
local uni = uni and uni.unicode |
| 32 |
i = i+1 |
| 33 |
if uni then |
| 34 |
if type(uni) == 'number' then |
| 35 |
result[i] = utf.char(uni) |
| 36 |
else |
| 37 |
result[i] = utf.char(table.unpack(uni)) |
| 38 |
end |
| 39 |
else |
| 40 |
if char < 0x110000 then |
| 41 |
result[i] = utf.char(char) |
| 42 |
else |
| 43 |
result[i] = '\u{FFFD}' |
| 44 |
end |
| 45 |
end |
| 46 |
elseif node.id'math' == id then |
| 47 |
if props then |
| 48 |
local mml = props.saved_mathml_table or props.saved_mathml_core |
| 49 |
if mml then |
| 50 |
i = i+1 |
| 51 |
result[i] = mml |
| 52 |
n = node.end_of_math(n) |
| 53 |
end |
| 54 |
end |
| 55 |
-- elseif node.id'whatsit' == id then |
| 56 |
-- TODO(?) |
| 57 |
elseif node.id'glue' == id then |
| 58 |
if n.width > 1000 then -- FIXME: Coordinate constant with tagpdf |
| 59 |
i = i+1 |
| 60 |
result[i] = '\u{00A0}' -- non breaking space... There is no real reason why it has to be non breaking, except that MathML often ignore other spaces |
| 61 |
end |
| 62 |
elseif node.id'hlist' == id then |
| 63 |
local nested = to_unicode(n.head) |
| 64 |
table.move(nested, 1, #nested, i+1, result) |
| 65 |
i = i+#nested |
| 66 |
elseif node.id'vlist' == id then |
| 67 |
i = i+1 |
| 68 |
result[i] = '\u{FFFD}' |
| 69 |
elseif node.id'rule' == id then |
| 70 |
if n.width ~= 0 then |
| 71 |
i = i+1 |
| 72 |
result[i] = '\u{FFFD}' |
| 73 |
end |
| 74 |
elseif node.id'disc' == id then |
| 75 |
local subresult = to_unicode(n.replace) |
| 76 |
table.move(subresult, 1, #subresult, i + 1, result) |
| 77 |
i = i+#subresult |
| 78 |
end -- CHECK: Everything else can probably be ignored, otherwise shout at me |
| 79 |
end |
| 80 |
end |
| 81 |
return result |
| 82 |
end |
| 83 |
|
| 84 |
return to_unicode |