summaryrefslogtreecommitdiff
path: root/tex/context/base/font-enh.lua
blob: 3e3535f87f4614e3ce8659ef8caaac661f3ebeca (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
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_defining     = false  trackers.register("fonts.defining", function(v) trace_defining = v end)
local report_defining    = logs.reporter("fonts","defining")

local fonts              = fonts
local constructors       = fonts.constructors

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

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_defining then
                        report_defining("reencoding U+%04X to U+%04X",newcode,oldcode)
                    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_defining then
                    report_defining("remapping U+%04X to U+%04X",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,
    }
}