summaryrefslogtreecommitdiff
path: root/tex/context/modules/mkiv/s-fonts-system.lua
blob: e05eef0fa58e036aa73fae560f9e82c21056bdc2 (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
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 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 = 3

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
        logs.report("system","fontfile: %s",file.nameonly(filename))
        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
            else
                -- nothing
            end
        end
    end
    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