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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
if not modules then modules = { } end modules ['mtx-fonts'] = {
version = 1.001,
comment = "companion to mtxrun.lua",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
if not fontloader then fontloader = fontforge end
dofile(resolvers.find_file("font-otp.lua","tex"))
dofile(resolvers.find_file("font-syn.lua","tex"))
dofile(resolvers.find_file("font-mis.lua","tex"))
scripts = scripts or { }
scripts.fonts = scripts.fonts or { }
function fonts.names.simple()
local simpleversion = 1.001
local simplelist = { "ttf", "otf", "ttc", "dfont" }
local name = "luatex-fonts-names.lua"
fonts.names.filters.list = simplelist
fonts.names.version = simpleversion -- this number is the same as in font-dum.lua
logs.report("fontnames","generating font database for 'luatex-fonts' version %s",fonts.names.version)
fonts.names.identify(true)
local data = fonts.names.data
if data then
local simplemappings = { }
local simplified = {
mappings = simplemappings,
version = simpleversion,
}
local specifications = data.specifications
for i=1,#simplelist do
local format = simplelist[i]
for tag, index in next, data.mappings[format] do
local s = specifications[index]
simplemappings[tag] = { s.rawname, s.filename, s.subfont }
end
end
logs.report("fontnames","saving names in '%s'",name)
io.savedata(name,table.serialize(simplified,true))
local data = io.loaddata(resolvers.find_file("font-dum.lua","tex"))
local dummy = string.match(data,"fonts%.names%.version%s*=%s*([%d%.]+)")
if tonumber(dummy) ~= simpleversion then
logs.report("fontnames","warning: version number %s in 'font-dum' does not match database version number %s",dummy or "?",simpleversion)
end
elseif lfs.isfile(name) then
os.remove(name)
end
end
function scripts.fonts.reload()
if environment.argument("simple") then
fonts.names.simple()
else
fonts.names.load(true)
end
end
local function subfont(sf)
if sf then
return string.format("index: % 2s", sf)
else
return ""
end
end
local function fontweight(fw)
if fw then
return string.format("conflict: %s", fw)
else
return ""
end
end
local function showfeatures(tag,specification)
logs.simple("mapping : %s",tag)
logs.simple("fontname: %s",specification.fontname)
logs.simple("fullname: %s",specification.fullname)
logs.simple("filename: %s",specification.filename)
logs.simple("family : %s",specification.familyname or "<nofamily>")
logs.simple("weight : %s",specification.weight or "<noweight>")
logs.simple("style : %s",specification.style or "<nostyle>")
logs.simple("width : %s",specification.width or "<nowidth>")
logs.simple("variant : %s",specification.variant or "<novariant>")
logs.simple("subfont : %s",subfont(specification.subfont))
logs.simple("fweight : %s",fontweight(specification.fontweight))
-- maybe more
local features = fonts.get_features(specification.filename,specification.format)
if features then
for what, v in table.sortedhash(features) do
local data = features[what]
if data and next(data) then
logs.simple()
logs.simple("%s features:",what)
logs.simple()
logs.simple("feature script languages")
logs.simple()
for f,ff in table.sortedhash(data) do
local done = false
for s, ss in table.sortedhash(ff) do
if s == "*" then s = "all" end
if ss ["*"] then ss["*"] = nil ss.all = true end
if done then
f = ""
else
done = true
end
logs.simple("% -8s % -8s % -8s",f,s,table.concat(table.sortedkeys(ss), " "))
end
end
end
end
else
logs.simple()
logs.simple("no features")
logs.simple()
end
logs.reportline()
end
local function reloadbase(reload)
if reload then
logs.simple("fontnames, reloading font database")
names.load(true)
logs.simple("fontnames, done\n\n")
end
end
local function list_specifications(t,info)
if t then
local s = table.sortedkeys(t)
if info then
for k=1,#s do
local v = s[k]
showfeatures(v,t[v])
end
else
for k=1,#s do
local v = s[k]
local entry = t[v]
s[k] = {
entry.familyname or "<nofamily>",
entry.weight or "<noweight>",
entry.style or "<nostyle>",
entry.width or "<nowidth>",
entry.variant or "<novariant>",
entry.fontname,
entry.filename,
subfont(entry.subfont),
fontweight(entry.fontweight),
}
e[k] = entry
end
table.formatcolumns(s)
for k=1,#s do
local v = s[k]
texio.write_nl(v)
end
end
end
end
local function list_matches(t,info)
if t then
local s, w = table.sortedkeys(t), { 0, 0, 0 }
if info then
for k=1,#s do
local v = s[k]
showfeatures(v,t[v])
end
else
for k=1,#s do
local v = s[k]
local entry = t[v]
s[k] = {
v,
entry.fontname,
entry.filename,
subfont(entry.subfont)
}
end
table.formatcolumns(s)
for k=1,#s do
texio.write_nl(s[k])
end
end
end
end
function scripts.fonts.list()
local all = environment.argument("all")
local info = environment.argument("info")
local reload = environment.argument("reload")
local pattern = environment.argument("pattern")
local filter = environment.argument("filter")
local given = environment.files[1]
reloadbase(reload)
if environment.argument("name") then
if pattern then
--~ mtxrun --script font --list --name --pattern=*somename*
list_matches(fonts.names.list(string.topattern(pattern,true),reload,all),info)
elseif filter then
logs.report("fontnames","not supported: --list --name --filter",name)
elseif given then
--~ mtxrun --script font --list --name somename
list_matches(fonts.names.list(given,reload,all),info)
else
logs.report("fontnames","not supported: --list --name <no specification>",name)
end
elseif environment.argument("spec") then
if pattern then
--~ mtxrun --script font --list --spec --pattern=*somename*
logs.report("fontnames","not supported: --list --spec --pattern",name)
elseif filter then
--~ mtxrun --script font --list --spec --filter="fontname=somename"
list_specifications(fonts.names.getlookups(filter),info)
elseif given then
--~ mtxrun --script font --list --spec somename
list_specifications(fonts.names.collectspec(given,reload,all),info)
else
logs.report("fontnames","not supported: --list --spec <no specification>",name)
end
elseif environment.argument("file") then
if pattern then
--~ mtxrun --script font --list --file --pattern=*somename*
list_specifications(fonts.names.collectfiles(string.topattern(pattern,true),reload,all),info)
elseif filter then
logs.report("fontnames","not supported: --list --spec",name)
elseif given then
--~ mtxrun --script font --list --file somename
list_specifications(fonts.names.collectfiles(given,reload,all),info)
else
logs.report("fontnames","not supported: --list --file <no specification>",name)
end
elseif pattern then
--~ mtxrun --script font --list --pattern=*somename*
list_matches(fonts.names.list(string.topattern(pattern,true),reload,all),info)
elseif given then
--~ mtxrun --script font --list somename
list_matches(fonts.names.list(given,reload,all),info)
else
logs.report("fontnames","not supported: --list <no specification>",name)
end
end
function scripts.fonts.save()
local name = environment.files[1] or ""
local sub = environment.files[2] or ""
local function save(savename,fontblob)
if fontblob then
savename = savename:lower() .. ".lua"
logs.simple("fontsave, saving data in %s",savename)
table.tofile(savename,fontloader.to_table(fontblob),"return")
fontloader.close(fontblob)
end
end
if name and name ~= "" then
local filename = resolvers.find_file(name) -- maybe also search for opentype
if filename and filename ~= "" then
local suffix = file.extname(filename)
if suffix == 'ttf' or suffix == 'otf' or suffix == 'ttc' or suffix == "dfont" then
local fontinfo = fontloader.info(filename)
if fontinfo then
logs.simple("font: %s located as %s",name,filename)
if fontinfo[1] then
for k=1,#fontinfo do
local v = fontinfo[k]
save(v.fontname,fontloader.open(filename,v.fullname))
end
else
save(fontinfo.fullname,fontloader.open(filename))
end
else
logs.simple("font: %s cannot be read",filename)
end
else
logs.simple("font: %s not saved",filename)
end
else
logs.simple("font: %s not found",name)
end
else
logs.simple("font: no name given")
end
end
logs.extendbanner("ConTeXt Font Database Management 0.21",true)
messages.help = [[
--save save open type font in raw table
--reload generate new font database
--reload --simple generate 'luatex-fonts-names.lua' (not for context!)
--list --name list installed fonts, filter by name [--pattern]
--list --spec list installed fonts, filter by spec [--filter]
--list --file list installed fonts, filter by file [--pattern]
--pattern=str filter files using pattern
--filter=list key-value pairs
--all show all found instances
--info give more details
--track=list enable trackers
examples of searches:
mtxrun --script font --list somename (== --pattern=*somename*)
mtxrun --script font --list --name somename
mtxrun --script font --list --name --pattern=*somename*
mtxrun --script font --list --spec somename
mtxrun --script font --list --spec somename-bold-italic
mtxrun --script font --list --spec --pattern=*somename*
mtxrun --script font --list --spec --filter="fontname=somename"
mtxrun --script font --list --spec --filter="familyname=somename,weight=bold,style=italic,width=condensed"
mtxrun --script font --list --file somename
mtxrun --script font --list --file --pattern=*somename*
]]
local track = environment.argument("track")
if track then trackers.enable(track) end
if environment.argument("names") then
environment.setargument("reload",true)
environment.setargument("simple",true)
end
if environment.argument("list") then
scripts.fonts.list()
elseif environment.argument("reload") then
scripts.fonts.reload()
elseif environment.argument("save") then
scripts.fonts.save()
else
logs.help(messages.help)
end
|