summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/font-tex.lmt
blob: 2d21687df7e0c6b27761f9f2acde32ec98c4a9f3 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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 formatters     = string.formatters
local sortedhash     = table.sortedhash

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

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

----- getshapes      = fonts.dropins.getshapes

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

local currentfont    = font.current

-- hm, seems to gobble the first \char

local makesetups = formatters["box:%s:%S"]

local function makeglyphbox(char,spec)
    token.expand_macro("makeglyphbox",true,spec.setups or tostring(char))
    local b = tex.takebox("glyphbox")
    spec.code = {
        width  = b.width,
        height = b.height,
        depth  = b.depth,
        objnum = tex.boxresources.save(b,nil,nil,true),
    }
end

local function setboxglyphs(category,fontid,unicode,specification)
    local box = specification.code
    if unicode and box then
        local tfmdata    = fontdata[fontid]
        local characters = tfmdata.characters
        local newdata    = {
            width   = box.width  or 0,
            height  = box.height or 0,
            depth   = box.depth  or 0,
            unicode = unicode,
        }
        -- pass dimensions to lua
        characters[unicode] = newdata
        -- this also adapts newdata (add commands)
        fonts.dropins.swapone("box",tfmdata,specification,unicode)
        -- pass dimensions to tex
        addcharacters(fontid, { characters = { [unicode] = newdata } })
        return fontid, unicode
    else
        logs.report("box glyph", "invalid glyph box for %C",unicode)
    end
end

local function setboxglyph(specification)
    if specification then
        local name    = specification.name
        local unicode = specification.unicode
        local font    = currentfont()
        if not unicode and name then
            unicode = newprivateslot(name)
            specification.unicode = unicode
        end
        if unicode then
            if not specification.setups then
                specification.setups = name
            end
            -- we can actually delay font if needed
            register(font,unicode,function(font,private)
                makeglyphbox(unicode,specification)
                return setboxglyphs(category,font,unicode,specification)
            end)
            checkenabled()
        end
    end
end

local function setboxdirectly(font,unicode,box) -- hash based on wd/ht/dp
    if box then
        local tfmdata      = fontdata[font]
        local glyphboxes   = (tfmdata.glyphboxes or 0) + 1
        tfmdata.glyphboxes = glyphboxes
        local private      = newprivateslot(formatters["BG:%05X"](glyphboxes))
        local newdata      = {
            width   = box.width  or 0,
            height  = box.height or 0,
            depth   = box.depth  or 0,
            unicode = unicode,
            objnum  = tex.boxresources.save(box,nil,nil,true),
        }
        local specification = {
            code = newdata
        }
        tfmdata.characters[private] = newdata
        addcharacters(font, { characters = { [private] = newdata } })
        fonts.dropins.swapone("box",tfmdata,specification,private)
        checkenabled()
        return private
    end
end

fonts.helpers.setboxdirectly = setboxdirectly

local boxes = table.setmetatableindex("table")

function fonts.helpers.registerglyphbox(specification)
    local category = specification.category
    local whatever = specification.unicode or specification.name
    if category and whatever then
        boxes[category][whatever] = { action = makeglyphbox } -- always
    end
end

local function initializebox(tfmdata,kind,value)
    local boxes = boxes[value]
    if value then
        local font = tfmdata.properties.id
        -- just preregister the list
        for char, spec in sortedhash(boxes) do
            spec.setups = makesetups(value,char)
            if type(char) == "string" then
                char = newprivateslot(name)
            end
            -- check if spec.code
            register(font,char,function(font,char)
                local action = spec.action
                if type(action) == "function" then
                    action(char,spec)
                end
                return setboxglyphs(value,font,char,spec)
            end)
        end
        checkenabled()
    end
end

fonts.helpers.setboxglyphs = setboxglyphs
fonts.helpers.setboxglyph  = setboxglyph

interfaces.implement {
    name      = "registerboxglyph", -- combine with next one
    public    = true,
    protected = true,
    actions   = fonts.helpers.registerglyphbox,
    arguments = { {
        { "category" },
        { "unicode", "integer" },
        { "name" },
    } },
}

interfaces.implement {
    name      = "setboxglyph",
    public    = true,
    protected = true,
    actions   = setboxglyph,
    arguments = { {
        { "category" },
        { "unicode", "integer" },
        { "name" },
        { "*" }
    } },
}

fonts.handlers.otf.features.register {
    name         = "box",
    description  = "box glyphs",
    manipulators = {
        base = initializebox,
        node = initializebox,
    }
}

-- fonts.helpers.registerboxglyph { category = "demo", unicode = 103 }
-- fonts.helpers.registerboxglyph { category = "demo", unicode = 104 }
-- fonts.helpers.registerboxglyph { category = "demo", unicode = 105 }
-- fonts.helpers.registerboxglyph { category = "demo", unicode = 106 }

local callbacks      = fonts.callbacks or { }
fonts.callbacks      = callbacks

function callbacks.devirtualize(chardata,f,c)
    if chardata.commands then
        local h = node.hpack(nodes.pool.glyph(f,c))
        local p = setboxdirectly(f,c,h)
        chardata.oldcommands = chardata.commands
        chardata.commands = { { "char", p } }
        chardata.callback = false
    end
end

local function processcallback(f,c)
    local characters = fontchars[f]
    local chardata   = characters[c]
    if chardata then
        local callback = chardata.callback
        if callback then
            local action = type(callback) == "function" and callback or callbacks[callback]
            if action then
                action(chardata,f,c)
            end
        end
    end
end

callback.register("process_character",processcallback)

callbacks.callback = processcallback