font-enh.lmt /size: 3060 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['font-enh'] = {
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-- This module is already stripped from old stuff but more might go away
10-- in lmtx. Stay tuned.
11
12local next = next
13
14local trace_unicoding    = false
15
16trackers.register("fonts.defining",  function(v) trace_unicoding = v end)
17trackers.register("fonts.unicoding", function(v) trace_unicoding = v end)
18
19local report_unicoding   = logs.reporter("fonts","unicoding")
20
21local fonts              = fonts
22local constructors       = fonts.constructors
23
24local afmfeatures        = constructors.features.afm
25local otffeatures        = constructors.features.otf
26
27local registerafmfeature = afmfeatures.register
28local registerotffeature = otffeatures.register
29
30local function initialize(tfmdata)
31    local goodies   = tfmdata.goodies
32    local newcoding = nil
33    for i=1,#goodies do
34        local remapping = goodies[i].remapping
35        if remapping and remapping.unicodes then
36            newcoding = remapping.unicodes  -- names to unicodes
37        end
38    end
39    if newcoding then
40        local characters   = tfmdata.characters
41        local descriptions = tfmdata.descriptions
42        local oldcoding    = tfmdata.resources.unicodes
43        local originals    = { }
44        for name, newcode in next, newcoding do
45            local oldcode = oldcoding[name]
46            if characters[newcode] and not originals[newcode] then
47                originals[newcode] = {
48                    character   = characters  [newcode],
49                    description = descriptions[newcode],
50                }
51            end
52            if oldcode then
53                local original = originals[oldcode]
54                local character, description
55                if original then
56                    character   = original.character
57                    description = original.description
58                else
59                    character   = characters  [oldcode]
60                    description = descriptions[oldcode]
61                end
62                characters  [newcode] = character
63                descriptions[newcode] = description
64                character  .unicode = newcode
65                description.unicode = newcode
66            else
67                oldcoding[name] = newcode
68            end
69            if trace_unicoding then
70                if oldcode then
71                    report_unicoding("aliasing glyph %a from %U to %U",name,oldcode,newcode)
72                else
73                    report_unicoding("aliasing glyph %a to %U",name,newcode)
74                end
75            end
76        end
77    end
78end
79
80local specification = {
81    name        = "unicoding",
82    description = "adapt unicode table",
83    initializers = {
84        base = initialize,
85        node = initialize,
86    },
87}
88
89registerotffeature(specification)
90registerafmfeature(specification)
91