summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/font-enh.lua
blob: 2bf0741f535b28c91727bb4558b2410d546224a5 (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
if not modules then modules = { } end modules ['font-enh'] = {
    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"
}

local next = next

local trace_unicoding    = false

trackers.register("fonts.defining",  function(v) trace_unicoding = v end)
trackers.register("fonts.unicoding", function(v) trace_unicoding = v end)

local report_unicoding   = logs.reporter("fonts","unicoding")

local fonts              = fonts
local constructors       = fonts.constructors

local tfmfeatures        = constructors.newfeatures("tfm")
local registertfmfeature = tfmfeatures.register

local afmfeatures        = fonts.constructors.newfeatures("afm")
local registerafmfeature = afmfeatures.register

local otffeatures        = fonts.constructors.newfeatures("otf")
local registerotffeature = otffeatures.register

-- -- these will become goodies (when needed at all)
--
-- local fontencodings      = fonts.encodings
-- fontencodings.remappings = fontencodings.remappings or { }
--
-- local function reencode(tfmdata,encoding)
--     if encoding and fontencodings.known[encoding] then
--         local data = fontencodings.load(encoding)
--         if data then
--             tfmdata.properties.encoding = encoding
--             local characters = tfmdata.characters
--             local original   = { }
--             local vector     = data.vector
--             for unicode, character in next, characters do
--                 character.name    = vector[unicode]
--                 character.index   = unicode, character
--                 original[unicode] = character
--             end
--             for newcode, oldcode in next, data.unicodes do
--                 if newcode ~= oldcode then
--                     if trace_unicoding then
--                         report_unicoding("reencoding %U to %U",oldcode,newcode)
--                     end
--                     characters[newcode] = original[oldcode]
--                 end
--             end
--         end
--     end
-- end
--
-- registertfmfeature {
--     name         = "reencode",
--     description  = "reencode",
--     manipulators = {
--         base = reencode,
--         node = reencode,
--     }
-- }
--
-- local function remap(tfmdata,remapping)
--     local vector = remapping and fontencodings.remappings[remapping]
--     if vector then
--         local characters, original = tfmdata.characters, { }
--         for k, v in next, characters do
--             original[k], characters[k] = v, nil
--         end
--         for k,v in next, vector do
--             if k ~= v then
--                 if trace_unicoding then
--                     report_unicoding("remapping %U to %U",k,v)
--                 end
--                 local c = original[k]
--                 characters[v] = c
--                 c.index = k
--             end
--         end
--         local properties = tfmdata.properties
--         if not properties then
--             properties = { }
--             tfmdata.properties = properties
--         else
--             properties.encodingbytes = 2
--             properties.format        = properties.format or 'type1'
--         end
--     end
-- end
--
-- registertfmfeature {
--     name         = "remap",
--     description  = "remap",
--     manipulators = {
--         base = remap,
--         node = remap,
--     }
-- }

-- \definefontfeature[dingbats][goodies=dingbats,unicoding=yes]

-- we only add and don't replace
-- we could also add kerns but we asssume symbols
-- todo: complain if not basemode

--  remapping = {
--      tounicode = true,
--      unicodes = {
--         a1   = 0x2701,

local tosixteen = fonts.mappings.tounicode16

local function initializeunicoding(tfmdata)
    local goodies   = tfmdata.goodies
    local newcoding = nil
    local tounicode = false
    for i=1,#goodies do
        local remapping = goodies[i].remapping
        if remapping and remapping.unicodes then
            newcoding = remapping.unicodes -- names to unicodes
            tounicode = remapping.tounicode
        end
    end
    if newcoding then
        local characters   = tfmdata.characters
        local descriptions = tfmdata.descriptions
        local oldcoding    = tfmdata.resources.unicodes
        local tounicodes   = tfmdata.resources.tounicode -- index to unicode
        local originals    = { }
        for name, newcode in next, newcoding do
            local oldcode = oldcoding[name]
            if characters[newcode] and not originals[newcode] then
                originals[newcode] = {
                    character   = characters  [newcode],
                    description = descriptions[newcode],
                }
            end
            if oldcode then
                local original = originals[oldcode]
                if original then
                    characters  [newcode] = original.character
                    descriptions[newcode] = original.description
                else
                    characters  [newcode] = characters  [oldcode]
                    descriptions[newcode] = descriptions[oldcode]
                end
            else
                oldcoding[name] = newcode
            end
            if tounicode then
                local description = descriptions[newcode]
                if description then
                    local index = description.index
                    if not tounicodes[index] then
                        tounicodes[index] = tosixteen(newcode) -- shared (we could have a metatable)
                    end
                end
            end
            if trace_unicoding then
                if oldcode then
                    report_unicoding("aliasing glyph %a from %U to %U",name,oldcode,newcode)
                else
                    report_unicoding("aliasing glyph %a to %U",name,newcode)
                end
            end
        end
    end
end

registerafmfeature {
    name        = "unicoding",
    description = "adapt unicode table",
    initializers = {
        base = initializeunicoding,
        node = initializeunicoding,
    },
 -- manipulators = {
 --     base = finalizeunicoding,
 --     node = finalizeunicoding,
 -- }
}

registerotffeature {
    name        = "unicoding",
    description = "adapt unicode table",
    initializers = {
        base = initializeunicoding,
        node = initializeunicoding,
    },
 -- manipulators = {
 --     base = finalizeunicoding,
 --     node = finalizeunicoding,
 -- }
}