s-fonts-system.lua /size: 11 Kb    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['s-fonts-system'] = {
2    version   = 1.001,
3    comment   = "companion to s-fonts-system.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- ["zapfinoforteltpro"]={
10--  ["designsize"]=0,
11--  ["filename"]="zapfinoforteltpro.otf",
12--  ["fontname"]="zapfinoforteltpro",
13--  ["fontweight"]="regular",
14--  ["family"]="zapfinoforteltpro",
15--  ["subfamily"]="regular",
16--  ["familyname"]="zapfinoforteltpro",
17--  ["subfamilyname"]="regular",
18--  ["format"]="otf",
19--  ["fullname"]="zapfinoforteltpro",
20--  ["maxsize"]=0,
21--  ["minsize"]=0,
22--  ["modification"]=1105543074,
23--  ["rawname"]="ZapfinoForteLTPro",
24--  ["style"]="normal",
25--  ["variant"]="normal",
26--  ["weight"]="normal",
27--  ["width"]="normal",
28-- }
29
30moduledata.fonts        = moduledata.fonts        or { }
31moduledata.fonts.system = moduledata.fonts.system or { }
32
33local context = context
34local NC, NR, HL = context.NC, context.NR, context.HL
35local ctx_bold = context.bold
36local ctx_verbatim = context.verbatim
37local lpegmatch = lpeg.match
38local sortedhash = table.sortedhash
39local formatters = string.formatters
40local concat = table.concat
41local lower = string.lower
42local gsub = string.gsub
43local find = string.find
44
45local function allfiles(specification)
46    local pattern = lower(specification.pattern or "")
47    local list    = fonts.names.list(pattern,false,true)
48    if list then
49        local files = { }
50        for k, v in next, list do
51            files[file.basename(string.lower(v.filename))] = v
52        end
53        return files
54    end
55end
56
57function moduledata.fonts.system.showinstalled(specification)
58    specification = interfaces.checkedspecification(specification)
59    local files = allfiles(specification)
60    if files then
61        context.starttabulate { "|Tl|Tl|Tl|Tl|Tl|Tl|" }
62            HL()
63            NC() ctx_bold("filename")
64            NC() ctx_bold("fontname")
65            NC() ctx_bold("subfamily")
66            NC() ctx_bold("variant")
67            NC() ctx_bold("weight")
68            NC() ctx_bold("width")
69            NC() NR()
70            HL()
71            for filename, data in table.sortedpairs(files) do
72                NC() context(filename)
73                NC() context(data.fontname)
74                NC() context(data.subfamily)
75                NC() context(data.variant)
76                NC() context(data.weight)
77                NC() context(data.width)
78                NC() NR()
79            end
80        context.stoptabulate()
81    end
82end
83
84function moduledata.fonts.system.cacheinstalled(specification)
85    specification = interfaces.checkedspecification(specification)
86    local files = allfiles(specification)
87    if files then
88        local threshold = tonumber(specification.threshold)
89        local suffixes  = specification.suffixes
90        if suffixes then
91            suffixes = utilities.parsers.settings_to_set(suffixes)
92        else
93            suffixes = { otf = true, ttf = true }
94        end
95        for filename, data in table.sortedpairs(files) do
96            if string.find(filename," ") then
97                -- skip this one
98            elseif suffixes[file.suffix(filename)] then
99                local fullname = resolvers.findfile(filename)
100                context.start()
101                context.type(fullname)
102                context.par()
103                if threshold and file.size(fullname) > threshold then
104                    logs.report("fonts","ignoring : %s",fullname)
105                else
106                    logs.report("fonts","caching  : %s",fullname)
107                    context.definedfont { filename }
108                end
109                context.stop()
110            end
111        end
112    end
113end
114
115local splitter = lpeg.splitat(lpeg.S("._"),true)
116
117local method = 4
118
119function moduledata.fonts.system.showinstalledglyphnames(specification)
120    specification = interfaces.checkedspecification(specification)
121    local paths   = caches.getreadablepaths()
122    local files   = { }
123    local names   = table.setmetatableindex("table")
124    local f_u     = formatters["%04X"]
125    for i=1,#paths do
126        local list = dir.glob(paths[i].."/fonts/o*/**." .. utilities.lua.suffixes.tmc)
127        for i=1,#list do
128            files[list[i]] = true
129        end
130    end
131    for filename in table.sortedhash(files) do
132        local fontname = file.nameonly(filename)
133        logs.report("system","fontfile: %s",fontname)
134        local data = table.load(filename)
135        if data then
136            if method == 1 then
137                local unicodes = data.resources.unicodes
138                if unicodes then
139                    for n, u in sortedhash(unicodes) do
140                        if u >= 0xF0000 or (u >= 0xE000 and u <= 0xF8FF) then
141                            -- skip
142                        else
143                             local f = lpegmatch(splitter,n) or n
144                             if #f > 0 then
145                                 local t = names[f]
146                                 t[u] = (t[u] or 0) + 1
147                             end
148                        end
149                    end
150                end
151            elseif method == 2 then
152                local unicodes = data.resources.unicodes
153                if unicodes then
154                    for n, u in sortedhash(unicodes) do
155                        if u >= 0xF0000 or (u >= 0xE000 and u <= 0xF8FF) then
156                            -- skip
157                        else
158                            local t = names[n]
159                            t[u] = (t[u] or 0) + 1
160                        end
161                    end
162                end
163            elseif method == 3 then
164                local descriptions = data.descriptions
165                if descriptions then
166                    for u, d in sortedhash(descriptions) do
167                        local n = d.name
168                        local u = d.unicode
169                        if n and u then
170                            if type(u) == "table" then
171                                local t = { }
172                                for i=1,#u do
173                                    t[i] = f_u(u[i])
174                                end
175                                u = concat(t," ")
176                            end
177                            local t = names[n]
178                            t[u] = (t[u] or 0) + 1
179                        end
180                    end
181                end
182            elseif method == 4 then
183                local descriptions = data.descriptions
184                if descriptions then
185                    for u, d in sortedhash(descriptions) do
186                        local n = d.name
187                        local u = d.unicode
188                        if n and not u and not find(n,"^%.") then
189                            local n = names[n]
190                            n[#n+1] = fontname
191                        end
192                    end
193                end
194            else
195                -- nothing
196            end
197        end
198    end
199 -- names[".notdef"] = nil
200 -- names[".null"]   = nil
201    if method == 4 then
202        if next(names) then
203            context.starttabulate { "|l|pl|" }
204            local f_u = formatters["%04X~(%i)"]
205            local f_s = formatters["%s~(%i)"]
206            for k, v in sortedhash(names) do
207                NC() ctx_verbatim(k)
208                NC() context("% t",v)
209                NC() NR()
210            end
211            context.stoptabulate()
212        end
213        table.save("s-fonts-system-glyph-unknowns.lua",names)
214    else
215        if next(names) then
216            context.starttabulate { "|l|pl|" }
217            local f_u = formatters["%04X~(%i)"]
218            local f_s = formatters["%s~(%i)"]
219            for k, v in sortedhash(names) do
220                local t = { }
221                for k, v in sortedhash(v) do
222                    if type(k) == "string" then
223                        t[#t+1] = f_s(k,v)
224                    else
225                        t[#t+1] = f_u(k,v)
226                    end
227                end
228                NC() ctx_verbatim(k)
229                NC() context("%, t",t)
230                NC() NR()
231            end
232            context.stoptabulate()
233        end
234        table.save("s-fonts-system-glyph-names.lua",names)
235    end
236end
237
238-- -- --
239
240-- local skip = {
241--     "adobeblank",
242--     "veramo",
243--     "unitedstates",
244--     "tirek",
245--     "svbasicmanual",
246--     "sahel",
247--     "prsprorg",
248--     "piratdia",
249--     "notoserifthai",
250--     "coelacanthsubhdheavy",
251-- }
252
253-- local function bad(name)
254--     name = lower(name)
255--     for i=1,#skip do
256--         if find(name,skip[i]) then
257--             return true
258--         end
259--     end
260-- end
261
262-- function moduledata.fonts.system.showprivateglyphnames(specification)
263--     specification = interfaces.checkedspecification(specification)
264--     local paths   = caches.getreadablepaths()
265--     local files   = { }
266--     local names   = table.setmetatableindex("table")
267--     local f_u     = formatters["%04X"]
268--     for i=1,#paths do
269--         local list = dir.glob(paths[i].."/fonts/o*/**.tmc")
270--         for i=1,#list do
271--             files[list[i]] = true
272--         end
273--     end
274--     for filename in table.sortedhash(files) do
275--         logs.report("system","fontfile: %s",file.nameonly(filename))
276--         local data = table.load(filename)
277--         if data and data.format == "truetype" or data.format == "opentype" then
278--             local basename  = file.basename(data.resources.filename)
279--             local cleanname = gsub(basename," ","")
280--             if not bad(cleanname) then
281--                 local descriptions = data.descriptions
282--                 if descriptions then
283--                     local done = 0
284--                     for u, d in sortedhash(descriptions) do
285--                         local dn = d.name
286--                         local du = d.unicode
287--                         if dn and du and (u >= 0xF0000 or (u >= 0xE000 and u <= 0xF8FF)) and not find(dn,"notdef") then
288--                             if type(du) == "table" then
289--                                 local t = { }
290--                                 for i=1,#du do
291--                                     t[i] = f_u(du[i])
292--                                 end
293--                                 du = concat(t," ")
294--                             end
295--                             if done == 0 then
296--                                 logs.report("system","basename: %s",basename)
297--                                 context.starttitle { title = basename }
298--                                 context.start()
299--                                 context.definefont( { "tempfont" }, { "file:" .. cleanname })
300--                                 context.starttabulate { "|T||T|T|" }
301--                             end
302--                             NC() context("%U",u)
303--                             NC() context.tempfont() context.char(u) -- could be getglyph
304--                             NC() ctx_verbatim(dn)
305--                             NC() context(du)
306--                             NC() NR()
307--                             done = done + 1
308--                         end
309--                     end
310--                     if done > 0 then
311--                         logs.report("system","privates: %i",done)
312--                         context.stoptabulate()
313--                         context.stop()
314--                         context.stoptitle()
315--                     end
316--                 end
317--             end
318--         end
319--     end
320-- end
321
322