summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/font-enc.lua
blob: 2e8b722de39f467c7887c4074ffb6b3d0c90a978 (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
if not modules then modules = { } end modules ['font-enc'] = {
    version   = 1.001,
    comment   = "companion to font-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- this module is obsolete

local next = next
local match, gmatch, gsub = string.match, string.gmatch, string.gsub

local setmetatableindex = table.setmetatableindex

--[[ldx--
<p>Because encodings are going to disappear, we don't bother defining
them in tables. But we may do so some day, for consistency.</p>
--ldx]]--

local report_encoding = logs.reporter("fonts","encoding")

local encodings = { }
fonts.encodings = encodings

encodings.version = 1.03
encodings.cache   = containers.define("fonts", "enc", fonts.encodings.version, true)
encodings.known   = utilities.storage.allocate { -- sort of obsolete
    texnansi = true,
    ec       = true,
    qx       = true,
    t5       = true,
    t2a      = true,
    t2b      = true,
    t2c      = true,
    unicode  = true,
}

function encodings.is_known(encoding)
    return containers.is_valid(encodings.cache,encoding)
end

--[[ldx--
<p>An encoding file looks like this:</p>

<typing>
/TeXnANSIEncoding [
/.notdef
/Euro
...
/ydieresis
] def
</typing>

<p>Beware! The generic encoding files don't always apply to the ones that
ship with fonts. This has to do with the fact that names follow (slightly)
different standards. However, the fonts where this applies to (for instance
Latin Modern or <l n='tex'> Gyre) come in OpenType variants too, so these
will be used.</p>
--ldx]]--

local enccodes = characters.enccodes or { }

function encodings.load(filename)
    local name = file.removesuffix(filename)
    local data = containers.read(encodings.cache,name)
    if data then
        return data
    end
    if name == "unicode" then
        data = encodings.make_unicode_vector() -- special case, no tex file for this
    end
    if data then
        return data
    end
    local vector, tag, hash, unicodes = { }, "", { }, { }
    local foundname = resolvers.findfile(filename,'enc')
    if foundname and foundname ~= "" then
        local ok, encoding, size = resolvers.loadbinfile(foundname)
        if ok and encoding then
            encoding = gsub(encoding,"%%(.-)\n","")
            local tag, vec = match(encoding,"/(%w+)%s*%[(.*)%]%s*def")
            local i = 0
            for ch in gmatch(vec,"/([%a%d%.]+)") do
                if ch ~= ".notdef" then
                    vector[i] = ch
                    if not hash[ch] then
                        hash[ch] = i
                    else
                        -- duplicate, play safe for tex ligs and take first
                    end
                    if enccodes[ch] then
                        unicodes[enccodes[ch]] = i
                    end
                end
                i = i + 1
            end
        end
    end
    local data = {
        name     = name,
        tag      = tag,
        vector   = vector,
        hash     = hash,
        unicodes = unicodes
    }
    return containers.write(encodings.cache, name, data)
end

--[[ldx--
<p>There is no unicode encoding but for practical purposes we define
one.</p>
--ldx]]--

-- maybe make this a function:

function encodings.make_unicode_vector()
    local vector, hash = { }, { }
    for code, v in next, characters.data do
        local name = v.adobename
        if name then
            vector[code] = name
            hash[name]   = code
        else
            vector[code] = '.notdef'
        end
    end
    for name, code in next, characters.synonyms do
        if not vector[code] then
            vector[code] = name
        end
        if not hash[name] then
            hash[name]   = code
        end
    end
    return containers.write(encodings.cache, 'unicode', { name='unicode', tag='unicode', vector=vector, hash=hash })
end

if not encodings.agl then

    -- We delay delay loading this rather big vector that is only needed when a
    -- font is loaded for caching. Once we're further along the route we can also
    -- delay it in the generic version (which doesn't use this file).

    encodings.agl = { }

    setmetatableindex(encodings.agl, function(t,k)
        report_encoding("loading (extended) adobe glyph list")
        dofile(resolvers.findfile("font-agl.lua"))
        return rawget(encodings.agl,k)
    end)

end