| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
local M = {} |
| 24 |
|
| 25 |
local glyph_id = node.id "glyph" |
| 26 |
local whatsit_id = node.id "whatsit" |
| 27 |
local special_subtype = node.subtype "special" |
| 28 |
local dir_id = node.id "dir" |
| 29 |
local glue_id = node.id "glue" |
| 30 |
|
| 31 |
local escape = function(char) |
| 32 |
|
| 33 |
return string.format("t4ht@+&{35}x%x{59}", char) |
| 34 |
end |
| 35 |
|
| 36 |
local make_node = function(data) |
| 37 |
|
| 38 |
local n = node.new(whatsit_id,special_subtype) |
| 39 |
n.data = data |
| 40 |
return n |
| 41 |
end |
| 42 |
|
| 43 |
local function first_node(head) |
| 44 |
local head = head |
| 45 |
while head.prev do |
| 46 |
head = head.prev |
| 47 |
end |
| 48 |
return head |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
local allowed_names = {"^cmr", "^cmb","^cmt", "^cmb", "^cmcs", "^rm%-l", "^cmi", "^ec%-lm", "none"} |
| 53 |
|
| 54 |
local testfont = function(name) |
| 55 |
|
| 56 |
for _, x in ipairs(allowed_names) do |
| 57 |
local r = name:match(x) |
| 58 |
if r then |
| 59 |
return true |
| 60 |
end |
| 61 |
end |
| 62 |
return false |
| 63 |
end |
| 64 |
|
| 65 |
local fonttypes = {} |
| 66 |
local get_font_type = function(id) |
| 67 |
if fonttypes[id]~=nil then return fonttypes[id] end |
| 68 |
local f = font.getfont(id) or {name = "none"} |
| 69 |
local name = f.name |
| 70 |
local type = testfont(string.lower(name)) |
| 71 |
if not type then |
| 72 |
print("Unsupported font", name) |
| 73 |
end |
| 74 |
fonttypes[id] = type |
| 75 |
return type |
| 76 |
end |
| 77 |
|
| 78 |
local xchar = string.byte("x") |
| 79 |
|
| 80 |
local utfchar = unicode.utf8.char |
| 81 |
function M.char_to_entity(head) |
| 82 |
|
| 83 |
for n in node.traverse(head) do |
| 84 |
if n.id == glyph_id then |
| 85 |
|
| 86 |
|
| 87 |
local t = get_font_type(n.font) |
| 88 |
if t == true then |
| 89 |
local char = n.char |
| 90 |
if char > 127 then |
| 91 |
local new = escape(char) |
| 92 |
local x = make_node(new) |
| 93 |
|
| 94 |
node.insert_before(head, n, x) |
| 95 |
|
| 96 |
|
| 97 |
n.char = xchar |
| 98 |
end |
| 99 |
end |
| 100 |
elseif n.id == dir_id then |
| 101 |
|
| 102 |
|
| 103 |
n.dir = "+TLT" |
| 104 |
end |
| 105 |
end |
| 106 |
return first_node(head) |
| 107 |
end |
| 108 |
|
| 109 |
M.allowed = allowed_names |
| 110 |
|
| 111 |
return M |
| 112 |
|