-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxxetex.lua
289 lines (264 loc) · 8.12 KB
/
xxetex.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
local fontid = token.create("fontid")
local function XeTeXfonttype()
token.put_next(fontid)
local fid = token.scan_int()
local tfmdata = font.getfont(fid)
local fonttype = tfmdata.format == "opentype" and 2 or 0
tex.print([[\numexpr]] .. fonttype .. [[\relax]])
end
local function XeTeXfirstfontchar()
token.put_next(fontid)
local fid = token.scan_int()
local tfmdata = font.getfont(fid)
local min
for slot in pairs(tfmdata.characters) do
min = min and (slot < min and slot or min) or slot
end
tex.print([[\numexpr]] .. min .. [[\relax]])
end
local function XeTeXlastfontchar()
token.put_next(fontid)
local fid = token.scan_int()
local tfmdata = font.getfont(fid)
local max
for slot in pairs(tfmdata.characters) do
max = max and (slot > max and slot < 2^16 and slot or max) or slot
end
tex.print([[\numexpr]] .. max .. [[\relax]])
end
local function XeTeXglyph()
local index = token.scan_int()
local tfmdata = font.getfont(font.current())
if tfmdata.format ~= "opentype"
and tfmdata.format ~= "truetype" then
tex.error([[Cannot use \XeTeXglyph with ]] ..
tfmdata.name .. tfmdata.format ..
[[; not opentype or truetype]])
end
for slot, char in pairs(tfmdata.characters) do
if char.index == index then
tex.print(utf.char(char.unicode))
return
end
end
end
local function XeTeXcountglyphs()
token.put_next(fontid)
local fid = token.scan_int()
local tfmdata = font.getfont(fid)
local count = 0
if tfmdata.format == "opentype" then
for _ in pairs(tfmdata.characters) do
count = count + 1
end
end
tex.print([[\numexpr]] .. count .. [[\relax]])
end
local function XeTeXglyphname()
token.put_next(fontid)
local fid = token.scan_int()
local index = token.scan_int()
local tfmdata = font.getfont(fid)
if tfmdata.format ~= "opentype" then
tex.error([[Cannot use \XeTeXglyphname with ]] ..
tfmdata.name .. [[; not a native platform font]])
end
local count = 0
for slot, char in pairs(tfmdata.shared.rawdata.descriptions) do
if char.index == index then
tex.print(char.name)
return
end
end
end
local function XeTeXglyphindex()
local k = string.gsub(token.scan_string(),'"','')
token.scan_keyword(' ') -- remove optional space
local tfmdata = font.getfont(font.current())
if tfmdata.format ~= "opentype" then
tex.error([[Cannot use \XeTeXglyphindex with ]] ..
tfmdata.name .. [[; not a native platform font]])
end
local index = 0
for slot, char in pairs(tfmdata.shared.rawdata.descriptions) do
if char.name == k then
index = char.index
break
end
end
tex.print([[\numexpr]] .. index .. [[\relax]])
end
local function XeTeXcharglyph()
local id = token.scan_int()
local tfmdata = font.getfont(font.current())
if tfmdata.format ~= "opentype" then
tex.error([[Cannot use \XeTeXcharglyph with ]] ..
tfmdata.name .. [[; not a native platform font]])
end
local index = tfmdata.characters[id].index
tex.print([[\numexpr]] .. index .. [[\relax]])
end
local function XeTeXglyphbounds()
local edge = token.scan_int()
local slot = token.scan_int()
local tfmdata = font.getfont(font.current())
if tfmdata.format ~= "opentype" then
tex.error([[Cannot use \XeTeXglyphname with ]] ..
tfmdata.name .. [[; not a native platform font]])
end
for _,char in pairs(tfmdata.shared.rawdata.descriptions) do
if char and char.index == slot then
local bbox = char.boundingbox
local dimen = 0
if edge == 1 then
dimen = bbox and bbox[1] / 100 * 2^16 or 0
elseif edge == 2 then
dimen = tfmdata.characters[char.unicode].height
elseif edge == 3 then
dimen = bbox and (char.width - bbox[3]) / 100 * 2^16 or 0
elseif edge == 4 then
dimen = tfmdata.characters[char.unicode].depth
end
tex.print([[\dimexpr]] .. tex.round(dimen) .. [[sp\relax]])
break
end
end
end
-- Character Classes
local xxetexclasses={}
local xxetexclasstoks={}
local function XeTeXcharclass()
local a = token.scan_int()
local b = token.scan_int()
xxetexclasses[a]=b
end
local func = luatexbase.new_luafunction 'XeTeXcharclass'
token.set_lua('XeTeXcharclass', func , "protected")
lua.get_functions_table()[func] = XeTeXcharclass
local function XeTeXinterchartoks()
local a = token.scan_int()
local b = token.scan_int()
token.scan_keyword(' ')
token.scan_keyword('=')
token.scan_keyword(' ')
local c = token.scan_argument(false)
xxetexclasstoks[a .. "/" .. b]=c
end
local func = luatexbase.new_luafunction 'XeTeXinterchartoks'
token.set_lua('XeTeXinterchartoks', func , "protected")
lua.get_functions_table()[func] = XeTeXinterchartoks
local function xxetexinterchartoks(head)
if not (tex.count.XXeTeXinterchartokenstate > 0) then
return head
end
local glyphid=node.id"glyph"
local xxetexflag=-1
for n in node.traverse(head) do
local a = 4095
local b = 4095
if xxetexflag==n then xxetexflag=-1 end
if xxetexflag ==-1 then
if n.id==glyphid then
a = xxetexclasses[n.char] or 0
end
if n.next and n.next.id==glyphid then
b = xxetexclasses[n.next.char] or 0
end
if xxetexclasstoks[a .. "/" .. b] then
xxetexflag=-1
if n.next then xxetexflag=n.next end
tex.scantoks(xxetexinterchartoksnum,0,xxetexclasstoks[a .. "/" .. b])
tex.runtoks("xxetexruninterchartoks")
local box = tex.getbox("xxetex@intercharbox")
node.insert_after(head, n, node.copy_list(box))
end
end
end
return head
end
-- graphics
local function XeTeXpicfile()
local filename = token.scan_string()
local t=img.scan{filename=filename}
--local t ={}
--t.filename = token.scan_string()
local scan=false
-- ignore key order for now
for i,v in pairs(img.fields()) do
print (i .. ": " .. v .. "=" .. (t[v] or "?"))
end
repeat
scan=false
token.scan_keyword(' ')
if token.scan_keyword('scaled') then
scan=true
scale= 0.001*token.scan_int()
print("xs: " .. scale .. " * " .. t.xsize.. " * " .. t.xres)
-- 65000???
t.width=scale*t.xsize * 65000
t.height=scale*t.ysize * 65000
end
token.scan_keyword(' ')
if token.scan_keyword('xscaled') then
scan=true
scale= 0.001*token.scan_int()
t.width=scale*t.xsize * 65000
end
token.scan_keyword(' ')
if token.scan_keyword('yscaled') then
scan=true
scale= 0.0001*token.scan_int()
-- t.height=scale*t.ysize * 65000
end
token.scan_keyword(' ')
if token.scan_keyword('width') then
scan=true
t.width= token.scan_dimen()
end
token.scan_keyword(' ')
if token.scan_keyword('height') then
scan=true
t.height= token.scan_dimen()
end
token.scan_keyword(' ')
if token.scan_keyword('rotated') then
-- full coverage would need to wrap image
scan=true
local angle=token.scan_real()
if angle==90 then
t.transform= 1
end
if angle==180 then
t.transform= 2
end
if angle==-90 then
t.transform= 3
end
end
-- just pdf but do for both here
token.scan_keyword(' ')
if token.scan_keyword('page') then
scan=true
-- img.page is now read-only
-- t.page= token.scan_int()
end
until not(scan)
img.write(t)
end
local func = luatexbase.new_luafunction 'XeTeXpicfile'
lua.get_functions_table()[func] = XeTeXpicfile
token.set_lua('XeTeXpicfile', func , "protected")
token.set_lua('XeTeXpdffile', func , "protected")
return {
XeTeXfonttype = XeTeXfonttype,
XeTeXfirstfontchar = XeTeXfirstfontchar,
XeTeXlastfontchar = XeTeXlastfontchar,
XeTeXglyph = XeTeXglyph,
XeTeXcountglyphs = XeTeXcountglyphs,
XeTeXglyphname = XeTeXglyphname,
XeTeXglyphindex = XeTeXglyphindex,
XeTeXcharglyph = XeTeXcharglyph,
XeTeXglyphbounds = XeTeXglyphbounds,
--
xxetexinterchartoks = xxetexinterchartoks,
}