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