summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkxl/font-mpf.lmt
blob: b1e6a00dd5559ba6d23c537e32d3c9b792baad22 (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
if not modules then modules = { } end modules ['font-mpf'] = {
    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 tonumber, unpack, type = tonumber, unpack, type

local context  = context
local metapost = metapost

local metafonts    = { }
metapost.metafonts = metafonts

local sortedhash        = table.sortedhash

local addcharacters     = fonts.constructors.addcharacters
local fontdata          = fonts.hashes.identifiers

local otf               = fonts.handlers.otf
local otfregister       = otf.features.register

local getshapes         = fonts.dropins.getshapes

local setparameterset   = metapost.setparameterset
local simplemetapost    = metapost.simple

local register          = fonts.collections.register
local checkenabled      = fonts.collections.checkenabled
local newprivateslot    = fonts.helpers.newprivateslot

local currentfont       = font.current

local function setmetaglyphs(category,fontid,unicode,specification)
    if unicode then
        local tfmdata    = fontdata[fontid]
        local characters = tfmdata.characters
        specification.unicode  = unicode
        specification.category = category
        -- Generate one shape, make a use we pass the unicode as that is
        -- the index in a picture list (normally). We could actually already
        -- fetch the stream but for now we do that later (i.e. we delay the
        -- serialization to pdf).
        local code = specification.code
        if code then
            setparameterset("mpsfont",specification)
         -- simplemetapost(instance,"begingroup;",true,true)
            simplemetapost("simplefun",code)
         -- simplemetapost(instance,"endgroup;",true,true)
        end
        -- We now know (hopefully) the dimensions of the image which is what
        -- we need to pass to the engine in order to let it do its work.
        local shapes = getshapes(category)
        local units  = shapes.parameters.units
        local scale  = tfmdata.parameters.size / units
        local shape  = shapes.glyphs[unicode]
        --
        local llx, lly, urx, ury = unpack(shape.boundingbox)
        llx = llx * scale
        urx = urx * scale
        lly = lly * scale
        ury = ury * scale
        --
        local newdata = { }
        characters[unicode] = newdata -- so that we can register commands
        fonts.dropins.swapone("mps",tfmdata,shape,unicode)
        --
        local olddata = characters[unicode]
        newdata.width   = urx - llx
        newdata.height  = ury
        newdata.depth   = -lly
        newdata.unicode = unicode
        -- commands = { { "offset", -llx, 0, newdata.commands[1][1], newdata.commands[1][2] } }
        if llx ~= 0 then
            newdata.commands = { { "offset", -llx, 0 }, newdata.commands[1] }
        end
        -- pass dimensions to lua
        characters[unicode] = newdata
        -- pass dimensions to tex
        addcharacters(fontid, { characters = { [unicode] = newdata } })
        return fontid, unicode
    end
end

local function setmetaglyph(specification)
    if specification then
        local category = specification.category
        local name     = specification.name
        if category and name then
            local fontid  = specification.fontid or currentfont()
            local private = newprivateslot(name)
            register(fontid,private,function(font,char)
                return setmetaglyphs(category,font,char,specification) end
            )
            checkenabled()
        end
    end
end

local function initializempf(tfmdata,kind,value)
    local metafont = metafonts[value]
    if value then
        local font = tfmdata.properties.id
        for char, spec in sortedhash(metafont) do
            if type(char) == "string" then
                char = newprivateslot(char)
            end
            register(font,char,function(font,char)
                return setmetaglyphs(value,font,char,spec)
            end)
        end
        checkenabled()
    end
end

fonts.helpers.setmetaglyphs = setmetaglyphs
fonts.helpers.setmetaglyph  = setmetaglyph

otfregister {
    name         = "metafont",
    description  = "metafont glyphs",
    manipulators = {
        base = initializempf,
        node = initializempf,
    }
}

do

    local scanners   = tokens.scanners
    local scanopen   = scanners.scanopen
    local scanclose  = scanners.scanclose
    local scanword   = scanners.word
    local scanstring = scanners.string

    interfaces.implement {
        name      = "setmetaglyph",
        public    = true,
        protected = true,
        actions   = function(t)
            local t = { }
            if scanopen() then
                 while not scanclose() do
                    local key = scanword()
                    if key == "shapes" then
                        if scanopen() then
                            local tt = { }
                            local nn = 0
                            while not scanclose() do
                                if scanopen() then
                                    local ttt = { }
                                    while not scanclose() do
                                        local key = scanword()
                                        ttt[key] = scanstring()
                                    end
                                    nn= nn + 1
                                    tt[nn] = ttt
                                end
                            end
                            t[key] = tt
                        end
                    else
                        t[key] = scanstring()
                    end
                end
            end
            setmetaglyph(t)
        end
    }

end