-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathluametalatex-whatsits.lua
96 lines (90 loc) · 2.36 KB
/
luametalatex-whatsits.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local whatsit_id = node.id'whatsit'
local whatsits = {
[0] = "open",
"write",
"close",
"special",
nil,
nil,
"save_pos",
"late_lua",
"user_defined",
nil,
nil,
nil,
nil,
nil,
nil,
nil,
"pdf_literal",
"pdf_refobj",
"pdf_annot",
"pdf_start_link",
"pdf_end_link",
"pdf_dest",
"pdf_action",
"pdf_thread",
"pdf_start_thread",
"pdf_end_thread",
"pdf_thread_data",
"pdf_link_data",
"pdf_colorstack",
"pdf_setmatrix",
"pdf_save",
"pdf_restore",
}
local whatsithandler = {}
-- for i = 0,#whatsits do -- #whatsits isn't guaranteed to work because of the nil entries
for i = 0,31 do
local v = whatsits[i]
if v then
whatsits[v] = i
end
end
function node.whatsits() return whatsits end
function node.subtype(s) return type(s) == "string" and whatsits[s] or nil end
local direct = node.direct
local getsubtype = direct.getsubtype
local properties = direct.get_properties_table()
local tonode, todirect = direct.tonode, direct.todirect
local function get_handler(n, subtype)
local props = properties[n]
return props and props.handle or whatsithandler[subtype or getsubtype(n)], props
end
local function new(name, handler)
assert(type(name) == 'string')
local subtype = whatsits[name]
if subtype then
if whatsithandler[subtype] then
texio.write_nl'WARNING: Overwriting default whatsit handler'
end
else
subtype = #whatsits + 1
whatsits[subtype] = name
whatsits[name] = subtype
end
whatsithandler[subtype] = handler
return subtype
end
-- TODO: Some fields might expect different values
local function setwhatsitfield(n, name, value)
local props = properties[n]
if not props then
props = {}
properties[n] = props
end
props[name] = value
end
direct.setwhatsitfield = setwhatsitfield
local function getwhatsitfield(n, name)
local props = properties[n]
return props and props[name]
end
direct.getwhatsitfield = getwhatsitfield
-- TODO: Some fields might be nodes and therefore have to be converted
function node.setwhatsitfield(n, ...) return setwhatsitfield(todirect(n), ...) end
function node.getwhatsitfield(n, ...) return getwhatsitfield(todirect(n), ...) end
return {
handler = get_handler,
new = new,
}