font-fmp.lmt /size: 3525 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['font-fmp'] = {
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
9-- We only need to pick up the filename and optionally the enc file as we only use
10-- them for old school virtual math fonts. We might as well drop this completely.
11-- This used to be a backend module but the code is rather generic so for now we
12-- just put it here.
13--
14-- As Type 1 is kind of obsolete I don't expect usage of those fonts in ways that
15-- are not yet covered. Actually, we don't need map files at all because we read the
16-- afm files. Maybe for math but there at some point we'll ditch the virtual old
17-- school variants because afaik ConTeXt users are not using these any longer.
18
19local find, match, splitlines = string.find, string.match, string.splitlines
20
21local implement = interfaces.implement
22
23local mappings  = { }
24
25local function setline(n)
26    if trace_fonts then
27        report_fonts("mapline: %s",n)
28    end
29    local name, fullname, encfile, pfbfile = match(n,"(%S+)%s+(%S+).-<(.-%.enc).-<(.-%.pfb)")
30    if name then
31        mappings[name] = { fullname, encfile, pfbfile }
32    end
33end
34
35local function setfile(n)
36    local okay, data = resolvers.loadbinfile(n,"map")
37    if okay and data then
38        data = splitlines(data)
39        for i=1,#data do
40            local d = data[i]
41            if d ~= "" and not find(d,"^[#%%]") then
42                setline(d)
43            end
44        end
45    end
46end
47
48local function getentry(n)
49    local n = file.nameonly(n)
50    local m = mappings[n]
51    if m then
52        local encfile  = m[2]
53        local encoding = fonts.encodings.load(encfile)
54        if not encoding then
55            return
56        end
57        local pfbfile = resolvers.findfile(m[3],"pfb")
58        if not pfbfile or pfbfile == "" then
59            return
60        end
61        return encoding, pfbfile, encfile
62    end
63end
64
65-- soon to be obsolete:
66
67local mappings = fonts.mappings or { }
68fonts.mappings = mappings
69
70local loaded = { -- prevent loading (happens in cont-sys files)
71 -- ["original-base.map"     ] = true,
72 -- ["original-ams-base.map" ] = true,
73 -- ["original-ams-euler.map"] = true,
74 -- ["original-public-lm.map"] = true,
75}
76
77function mappings.loadfile(name)
78    name = file.addsuffix(name,"map")
79    if not loaded[name] then
80        if trace_mapfiles then
81            report_mapfiles("loading map file %a",name)
82        end
83        setfile(name)
84        loaded[name] = true
85    end
86end
87
88local loaded = { -- prevent double loading
89}
90
91function mappings.loadline(how,line)
92    if line then
93        how = how .. " " .. line
94    elseif how == "" then
95        how = "= " .. line
96    end
97    if not loaded[how] then
98        if trace_mapfiles then
99            report_mapfiles("processing map line %a",line)
100        end
101        setline(how)
102        loaded[how] = true
103    end
104end
105
106function mappings.reset()
107    local setmapfile = lpdf and lpdf.setmapfile
108    if setmapfile then
109        setmapfile("") -- tricky ... backend related
110    end
111end
112
113mappings.getentry = getentry
114
115implement {
116    name      = "loadmapfile",
117    actions   = mappings.loadfile,
118    arguments = "string"
119}
120
121implement {
122    name      = "loadmapline",
123    actions   = mappings.loadline,
124    arguments = "string"
125}
126
127implement {
128    name      = "resetmapfiles",
129    actions   = mappings.reset,
130    arguments = "string"
131}
132