summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/good-ini.lua
blob: df79adb61c33806b21da3ee0ca95169891f19f0b (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
if not modules then modules = { } end modules ['good-ini'] = {
    version   = 1.000,
    comment   = "companion to font-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- depends on ctx

local type, next = type, next
local gmatch = string.gmatch
local sortedhash, insert = table.sortedhash, table.insert

local fonts              = fonts

local trace_goodies      = false  trackers.register("fonts.goodies", function(v) trace_goodies = v end)
local report_goodies     = logs.reporter("fonts","goodies")

local allocate           = utilities.storage.allocate
local implement          = interfaces.implement
local findfile           = resolvers.findfile
local formatters         = string.formatters

local otf                = fonts.handlers.otf
local afm                = fonts.handlers.afm
local tfm                = fonts.handlers.tfm

local registerotffeature = otf.features.register
local registerafmfeature = afm.features.register
local registertfmfeature = tfm.features.register

local addotffeature      = otf.enhancers.addfeature

local fontgoodies        = fonts.goodies or { }
fonts.goodies            = fontgoodies

local data               = fontgoodies.data or { }
fontgoodies.data         = data -- no allocate as we want to see what is there

local list               = fontgoodies.list or { }
fontgoodies.list         = list -- no allocate as we want to see what is there

fontgoodies.suffixes     = { "lfg", "lua" } -- lfg is context specific and should not be used elsewhere

local contextsetups      = fonts.specifiers.contextsetups

function fontgoodies.report(what,trace,goodies)
    if trace_goodies or trace then
        local whatever = goodies[what]
        if whatever then
            report_goodies("goodie %a found in %a",what,goodies.name)
        end
    end
end

local function locate(filename)
    local suffixes = fontgoodies.suffixes
    for i=1,#suffixes do
        local suffix = suffixes[i]
        local fullname = findfile(file.addsuffix(filename,suffix))
        if fullname and fullname ~= "" then
            return fullname
        end
    end
end

local function loadgoodies(filename) -- maybe a merge is better
    local goodies = data[filename] -- we assume no suffix is given
    if goodies ~= nil then
        -- found or tagged unfound
    elseif type(filename) == "string" then
        local fullname = locate(filename)
        if not fullname or fullname == "" then
            report_goodies("goodie file %a is not found (suffixes: % t)",filename,fontgoodies.suffixes)
            data[filename] = false -- signal for not found
        else
            goodies = dofile(fullname) or false
            if not goodies then
                report_goodies("goodie file %a is invalid",fullname)
                return nil
            elseif trace_goodies then
                report_goodies("goodie file %a is loaded",fullname)
            end
            goodies.name = goodies.name or "no name"
            for i=1,#list do
                local g = list[i]
                if trace_goodies then
                    report_goodies("handling goodie %a",g[1])
                end
                g[2](goodies)
            end
            goodies.initialized = true
            data[filename] = goodies
        end
    end
    return goodies
end

function fontgoodies.register(name,fnc,prepend) -- will be a proper sequencer
    for i=1,#list do
        local g = list[i]
        if g[1] == name then
            g[2] = fnc --overload
            return
        end
    end
    local g = { name, fnc }
    if prepend then
        insert(list,g,prepend == true and 1 or prepend)
    else
        insert(list,g)
    end
end

fontgoodies.load = loadgoodies

if implement then

    implement {
        name      = "loadfontgoodies",
        actions   = loadgoodies,
        arguments = "string",
        overload  = true, -- for now, permits new font loader
    }

end

-- register goodies file

local function setgoodies(tfmdata,value)
    local goodies = tfmdata.goodies
    if not goodies then -- actually an error
        goodies = { }
        tfmdata.goodies = goodies
    end
    for filename in gmatch(value,"[^, ]+") do
        -- we need to check for duplicates
        local ok = loadgoodies(filename)
        if ok then
            if trace_goodies then
                report_goodies("assigning goodie %a",filename)
            end
            goodies[#goodies+1] = ok
        end
    end
end

-- featuresets

local function flattenedfeatures(t,tt)
    -- first set value dominates
    if not tt then
        tt = { }
    end
    for i=1,#t do
        local ti = t[i]
        local ty = type(ti)
        if ty == "table" then
            flattenedfeatures(ti,tt)
        elseif ty == "string" then
            local set = contextsetups[ti]
            if set then
                for k, v in next, set do
                    if k ~= "number" then
                        tt[k] = v or nil
                    end
                end
            else
                -- bad
            end
        elseif tt[ti] == nil then
            tt[ti] = true
        end
    end
    for k, v in next, t do
        if type(k) ~= "number" then -- not tonumber(k)
            if type(v) == "table" then
                flattenedfeatures(v,tt)
            elseif tt[k] == nil then
                tt[k] = v
            end
        end
    end
    return tt
end

-- fonts.features.flattened = flattenedfeatures

local function prepare_features(goodies,name,set)
    if set then
        local ff = flattenedfeatures(set)
        local fullname = goodies.name .. "::" .. name
        local n, s = fonts.specifiers.presetcontext(fullname,"",ff)
        goodies.featuresets[name] = s -- set
        if trace_goodies then
            report_goodies("feature set %a gets number %a and name %a",name,n,fullname)
        end
        return n
    end
end

fontgoodies.prepare_features = prepare_features

local function initialize(goodies)
    local featuresets = goodies.featuresets
    if featuresets then
        if trace_goodies then
            report_goodies("checking featuresets in %a",goodies.name)
        end
        for name, set in next, featuresets do
            prepare_features(goodies,name,set)
        end
    end
end

fontgoodies.register("featureset",initialize)

local function setfeatureset(tfmdata,set,features)
    local goodies = tfmdata.goodies -- shared ?
    if goodies then
        local properties = tfmdata.properties
        local what
        for i=1,#goodies do
            -- last one wins
            local g = goodies[i]
            what = g.featuresets and g.featuresets[set] or what
        end
        if what then
            for feature, value in next, what do
                if features[feature] == nil then
                    features[feature] = value
                end
            end
            properties.mode = what.mode or properties.mode
        end
    end
end

-- postprocessors (we could hash processor and share code)

function fontgoodies.registerpostprocessor(tfmdata,f,prepend)
    local postprocessors = tfmdata.postprocessors
    if not postprocessors then
        tfmdata.postprocessors = { f }
    elseif prepend then
        insert(postprocessors,f,prepend == true and 1 or prepend)
    else
        insert(postprocessors,f)
    end
end

local function setpostprocessor(tfmdata,processor)
    local goodies = tfmdata.goodies
    if goodies and type(processor) == "string" then
        local found = { }
        local asked = utilities.parsers.settings_to_array(processor)
        for i=1,#goodies do
            local g = goodies[i]
            local p = g.postprocessors
            if p then
                for i=1,#asked do
                    local a = asked[i]
                    local f = p[a]
                    if type(f) == "function" then
                        found[a] = f
                    end
                end
            end
        end
        local postprocessors = tfmdata.postprocessors or { }
        for i=1,#asked do
            local a = asked[i]
            local f = found[a]
            if f then
                postprocessors[#postprocessors+1] = f
            end
        end
        if #postprocessors > 0 then
            tfmdata.postprocessors = postprocessors
        end
    end
end

local function setextrafeatures(tfmdata)
    local goodies = tfmdata.goodies
    if goodies then
        for i=1,#goodies do
            local g = goodies[i]
            local f = g.features
            if f then
                local rawdata = tfmdata.shared.rawdata
                local done    = { }
                -- indexed
                for i=1,#f do
                    local specification = f[i]
                    local feature = specification.name
                    if feature then
                        addotffeature(rawdata,feature,specification)
                        registerotffeature {
                            name        = feature,
                            description = formatters["extra: %s"](feature)
                        }
                    end
                    done[i] = true
                end
                -- hashed
                for feature, specification in sortedhash(f) do
                    if not done[feature] then
                        feature = specification.name or feature
                        specification.name = feature
                        addotffeature(rawdata,feature,specification)
                        registerotffeature {
                            name        = feature,
                            description = formatters["extra: %s"](feature)
                        }
                    end
                end
            end
        end
    end
end

local function setextensions(tfmdata)
    local goodies = tfmdata.goodies
    if goodies then
        for i=1,#goodies do
            local g = goodies[i]
            local e = g.extensions
            if e then
                local goodie = g.name or "unknown"
                for i=1,#e do
                    local name = "extension-" .. i
                 -- report_goodies("adding extension %s from %s",name,goodie)
                    otf.enhancers.addfeature(tfmdata.shared.rawdata,name,e[i])
                end
            end
        end
    end
end

-- installation

local goodies_specification = {
    name         = "goodies",
    description  = "goodies on top of built in features",
    initializers = {
        position = 1,
        base     = setgoodies,
        node     = setgoodies,
    }
}

registerotffeature(goodies_specification)
registerafmfeature(goodies_specification)
registertfmfeature(goodies_specification)

-- maybe more of the following could be for type one too

registerotffeature {
    name        = "extrafeatures",
    description = "extra features",
    default     = true,
    initializers = {
        position = 2,
        base     = setextrafeatures,
        node     = setextrafeatures,
    }
}

registerotffeature {
    name        = "extensions",
    description = "extensions to features",
    default     = true,
    initializers = {
        position = 2,
        base     = setextensions,
        node     = setextensions,
    }
}

registerotffeature {
    name        = "featureset",
    description = "goodie feature set",
    initializers = {
        position = 3,
        base     = setfeatureset,
        node     = setfeatureset,
    }
}

registerotffeature {
    name        = "postprocessor",
    description = "goodie postprocessor",
    initializers = {
        base = setpostprocessor,
        node = setpostprocessor,
    }
}