font-trt.lua /size: 3828 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['font-trt'] = {
2    version   = 1.001,
3    comment   = "companion to font-ini.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
9local rawget, dofile, next, type = rawget, dofile, next, type
10
11local cleanfilename = fonts.names.cleanfilename
12local splitbase     = file.splitbase
13local lower         = string.lower
14
15-- We provide a simple treatment mechanism (mostly because I want to demonstrate
16-- something in a manual). It's one of the few places where an lfg file gets loaded
17-- outside the goodies manager.
18
19local treatments       = fonts.treatments or { }
20fonts.treatments       = treatments
21
22local treatmentdata    = treatments.data or utilities.storage.allocate()
23treatments.data        = treatmentdata
24
25treatments.filename    = "treatments.lfg"
26
27local trace_treatments = false  trackers.register("fonts.treatments", function(v) trace_treatments = v end)
28local report_treatment = logs.reporter("fonts","treatment")
29
30treatments.report      = report_treatment
31
32function treatments.trace(...)
33    if trace_treatments then
34        report_treatment(...)
35    end
36end
37
38-- function treatments.load(name)
39--     local filename = resolvers.findfile(name)
40--     if filename and filename ~= "" then
41--         local goodies = dofile(filename)
42--         if goodies then
43--             local treatments = goodies.treatments
44--             if treatments then
45--                 for name, data in next, treatments do
46--                     treatmentdata[name] = data -- always wins
47--                 end
48--             end
49--         end
50--     end
51-- end
52
53table.setmetatableindex(treatmentdata,function(t,k)
54    local files = resolvers.findfiles(treatments.filename)
55    if files then
56        for i=1,#files do
57            local goodies = dofile(files[i])
58            if goodies then
59                local treatments = goodies.treatments
60                if treatments then
61                    for name, data in next, treatments do
62                        if not rawget(t,name) then
63                            t[name] = data
64                        end
65                    end
66                end
67            end
68        end
69    end
70    table.setmetatableindex(treatmentdata,nil)
71    return treatmentdata[k]
72end)
73
74local function applyfix(fix,filename,data,n)
75    if type(fix) == "function" then
76        -- we assume that when needed the fix reports something
77     -- if trace_treatments then
78     --     report_treatment("applying treatment %a to file %a",n,filename)
79     -- end
80        fix(data)
81    elseif trace_treatments then
82        report_treatment("invalid treatment %a for file %a",n,filename)
83    end
84end
85
86function treatments.applyfixes(filename,data)
87    local filename = cleanfilename(filename)
88    local pathpart, basepart = splitbase(filename)
89    local treatment = treatmentdata[filename] or treatmentdata[basepart]
90    if treatment then
91        local fixes = treatment.fixes
92        if not fixes then
93            -- nothing to fix
94        elseif type(fixes) == "table" then
95            for i=1,#fixes do
96                applyfix(fixes[i],filename,data,i)
97            end
98        else
99            applyfix(fixes,filename,data,1)
100        end
101    end
102end
103
104function treatments.ignoredfile(fullname)
105    local treatmentdata = treatments.data or { } -- when used outside context
106    local _, basepart = splitbase(fullname)
107    local treatment = treatmentdata[basepart] or treatmentdata[lower(basepart)]
108    if treatment and treatment.ignored then
109        report_treatment("font file %a resolved as %a is ignored, reason %a",basepart,fullname,treatment.comment or "unknown")
110        return true
111    end
112end
113
114fonts.names.ignoredfile = treatments.ignoredfile
115