summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-fonts-system.lua
blob: dc0f2e6a3f8b3ea43895e59a396fa3a115279b5f (plain)
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
if not modules then modules = { } end modules ['s-fonts-system'] = {
    version   = 1.001,
    comment   = "companion to s-fonts-system.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- ["zapfinoforteltpro"]={
--  ["designsize"]=0,
--  ["filename"]="zapfinoforteltpro.otf",
--  ["fontname"]="zapfinoforteltpro",
--  ["fontweight"]="regular",
--  ["family"]="zapfinoforteltpro",
--  ["subfamily"]="regular",
--  ["familyname"]="zapfinoforteltpro",
--  ["subfamilyname"]="regular",
--  ["format"]="otf",
--  ["fullname"]="zapfinoforteltpro",
--  ["maxsize"]=0,
--  ["minsize"]=0,
--  ["modification"]=1105543074,
--  ["rawname"]="ZapfinoForteLTPro",
--  ["style"]="normal",
--  ["variant"]="normal",
--  ["weight"]="normal",
--  ["width"]="normal",
-- }

moduledata.fonts        = moduledata.fonts        or { }
moduledata.fonts.system = moduledata.fonts.system or { }

local context = context
local NC, NR, HL = context.NC, context.NR, context.HL
local ctx_bold = context.bold
local ctx_verbatim = context.verbatim
local lpegmatch = lpeg.match
local sortedhash = table.sortedhash
local formatters = string.formatters
local concat = table.concat
local lower = string.lower
local gsub = string.gsub
local find = string.find

local function allfiles(specification)
    local pattern = lower(specification.pattern or "")
    local list    = fonts.names.list(pattern,false,true)
    if list then
        local files = { }
        for k, v in next, list do
            files[file.basename(string.lower(v.filename))] = v
        end
        return files
    end
end

function moduledata.fonts.system.showinstalled(specification)
    specification = interfaces.checkedspecification(specification)
    local files = allfiles(specification)
    if files then
        context.starttabulate { "|Tl|Tl|Tl|Tl|Tl|Tl|" }
            HL()
            NC() ctx_bold("filename")
            NC() ctx_bold("fontname")
            NC() ctx_bold("subfamily")
            NC() ctx_bold("variant")
            NC() ctx_bold("weight")
            NC() ctx_bold("width")
            NC() NR()
            HL()
            for filename, data in table.sortedpairs(files) do
                NC() context(filename)
                NC() context(data.fontname)
                NC() context(data.subfamily)
                NC() context(data.variant)
                NC() context(data.weight)
                NC() context(data.width)
                NC() NR()
            end
        context.stoptabulate()
    end
end

function moduledata.fonts.system.cacheinstalled(specification)
    specification = interfaces.checkedspecification(specification)
    local files = allfiles(specification)
    if files then
        local threshold = tonumber(specification.threshold)
        local suffixes  = specification.suffixes
        if suffixes then
            suffixes = utilities.parsers.settings_to_set(suffixes)
        else
            suffixes = { otf = true, ttf = true }
        end
        for filename, data in table.sortedpairs(files) do
            if string.find(filename," ") then
                -- skip this one
            elseif suffixes[file.suffix(filename)] then
                local fullname = resolvers.findfile(filename)
                context.start()
                context.type(fullname)
                context.par()
                if threshold and file.size(fullname) > threshold then
                    logs.report("fonts","ignoring : %s",fullname)
                else
                    logs.report("fonts","caching  : %s",fullname)
                    context.definedfont { filename }
                end
                context.stop()
            end
        end
    end
end

local splitter = lpeg.splitat(lpeg.S("._"),true)

local method = 4

function moduledata.fonts.system.showinstalledglyphnames(specification)
    specification = interfaces.checkedspecification(specification)
    local paths   = caches.getreadablepaths()
    local files   = { }
    local names   = table.setmetatableindex("table")
    local f_u     = formatters["%04X"]
    for i=1,#paths do
        local list = dir.glob(paths[i].."/fonts/o*/**.tmc")
        for i=1,#list do
            files[list[i]] = true
        end
    end
    for filename in table.sortedhash(files) do
        local fontname = file.nameonly(filename)
        logs.report("system","fontfile: %s",fontname)
        local data = table.load(filename)
        if data then
            if method == 1 then
                local unicodes = data.resources.unicodes
                if unicodes then
                    for n, u in sortedhash(unicodes) do
                        if u >= 0xF0000 or (u >= 0xE000 and u <= 0xF8FF) then
                            -- skip
                        else
                             local f = lpegmatch(splitter,n) or n
                             if #f > 0 then
                                 local t = names[f]
                                 t[u] = (t[u] or 0) + 1
                             end
                        end
                    end
                end
            elseif method == 2 then
                local unicodes = data.resources.unicodes
                if unicodes then
                    for n, u in sortedhash(unicodes) do
                        if u >= 0xF0000 or (u >= 0xE000 and u <= 0xF8FF) then
                            -- skip
                        else
                            local t = names[n]
                            t[u] = (t[u] or 0) + 1
                        end
                    end
                end
            elseif method == 3 then
                local descriptions = data.descriptions
                if descriptions then
                    for u, d in sortedhash(descriptions) do
                        local n = d.name
                        local u = d.unicode
                        if n and u then
                            if type(u) == "table" then
                                local t = { }
                                for i=1,#u do
                                    t[i] = f_u(u[i])
                                end
                                u = concat(t," ")
                            end
                            local t = names[n]
                            t[u] = (t[u] or 0) + 1
                        end
                    end
                end
            elseif method == 4 then
                local descriptions = data.descriptions
                if descriptions then
                    for u, d in sortedhash(descriptions) do
                        local n = d.name
                        local u = d.unicode
                        if n and not u and not find(n,"^%.") then
                            local n = names[n]
                            n[#n+1] = fontname
                        end
                    end
                end
            else
                -- nothing
            end
        end
    end
 -- names[".notdef"] = nil
 -- names[".null"]   = nil
    if method == 4 then
        if next(names) then
            context.starttabulate { "|l|pl|" }
            local f_u = formatters["%04X~(%i)"]
            local f_s = formatters["%s~(%i)"]
            for k, v in sortedhash(names) do
                NC() ctx_verbatim(k)
                NC() context("% t",v)
                NC() NR()
            end
            context.stoptabulate()
        end
        table.save("s-fonts-system-glyph-unknowns.lua",names)
    else
        if next(names) then
            context.starttabulate { "|l|pl|" }
            local f_u = formatters["%04X~(%i)"]
            local f_s = formatters["%s~(%i)"]
            for k, v in sortedhash(names) do
                local t = { }
                for k, v in sortedhash(v) do
                    if type(k) == "string" then
                        t[#t+1] = f_s(k,v)
                    else
                        t[#t+1] = f_u(k,v)
                    end
                end
                NC() ctx_verbatim(k)
                NC() context("%, t",t)
                NC() NR()
            end
            context.stoptabulate()
        end
        table.save("s-fonts-system-glyph-names.lua",names)
    end
end

-- -- --

-- local skip = {
--     "adobeblank",
--     "veramo",
--     "unitedstates",
--     "tirek",
--     "svbasicmanual",
--     "sahel",
--     "prsprorg",
--     "piratdia",
--     "notoserifthai",
--     "coelacanthsubhdheavy",
-- }

-- local function bad(name)
--     name = lower(name)
--     for i=1,#skip do
--         if find(name,skip[i]) then
--             return true
--         end
--     end
-- end

-- function moduledata.fonts.system.showprivateglyphnames(specification)
--     specification = interfaces.checkedspecification(specification)
--     local paths   = caches.getreadablepaths()
--     local files   = { }
--     local names   = table.setmetatableindex("table")
--     local f_u     = formatters["%04X"]
--     for i=1,#paths do
--         local list = dir.glob(paths[i].."/fonts/o*/**.tmc")
--         for i=1,#list do
--             files[list[i]] = true
--         end
--     end
--     for filename in table.sortedhash(files) do
--         logs.report("system","fontfile: %s",file.nameonly(filename))
--         local data = table.load(filename)
--         if data and data.format == "truetype" or data.format == "opentype" then
--             local basename  = file.basename(data.resources.filename)
--             local cleanname = gsub(basename," ","")
--             if not bad(cleanname) then
--                 local descriptions = data.descriptions
--                 if descriptions then
--                     local done = 0
--                     for u, d in sortedhash(descriptions) do
--                         local dn = d.name
--                         local du = d.unicode
--                         if dn and du and (u >= 0xF0000 or (u >= 0xE000 and u <= 0xF8FF)) and not find(dn,"notdef") then
--                             if type(du) == "table" then
--                                 local t = { }
--                                 for i=1,#du do
--                                     t[i] = f_u(du[i])
--                                 end
--                                 du = concat(t," ")
--                             end
--                             if done == 0 then
--                                 logs.report("system","basename: %s",basename)
--                                 context.starttitle { title = basename }
--                                 context.start()
--                                 context.definefont( { "tempfont" }, { "file:" .. cleanname })
--                                 context.starttabulate { "|T||T|T|" }
--                             end
--                             NC() context("%U",u)
--                             NC() context.tempfont() context.char(u) -- could be getglyph
--                             NC() ctx_verbatim(dn)
--                             NC() context(du)
--                             NC() NR()
--                             done = done + 1
--                         end
--                     end
--                     if done > 0 then
--                         logs.report("system","privates: %i",done)
--                         context.stoptabulate()
--                         context.stop()
--                         context.stoptitle()
--                     end
--                 end
--             end
--         end
--     end
-- end