summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/meta-fnt.lua
blob: 95bdfa6d966ec7e59ee869619619d293a4f53e5c (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
if not modules then modules = { } end modules ['meta-fnt'] = {
    version   = 1.001,
    comment   = "companion to meta-fnt.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local concat     = table.concat
local format     = string.format
local formatters = string.formatters
local chardata   = characters.data
local fontdata   = fonts.hashes.identifiers

local vffonts    = fonts.handlers.vf

local mpfonts    = fonts.mp or { }
fonts.mp         = mpfonts

mpfonts.version  = mpfonts.version or 1.20
mpfonts.inline   = true
mpfonts.cache    = containers.define("fonts", "mp", mpfonts.version, true)

metapost.fonts   = metapost.fonts or { }

local function unicodetoactualtext(...)
    unicodetoactualtext = backends.codeinjections.unicodetoactualtext
    return unicodetoactualtext(...)
end

-- a few glocals

local characters, descriptions = { }, { }
local factor, code, slot, width, height, depth, total, variants, bbox, llx, lly, urx, ury = 100, { }, 0, 0, 0, 0, 0, 0, true, 0, 0, 0, 0

local flusher = {
    startfigure = function(_chr_,_llx_,_lly_,_urx_,_ury_)
        code   = { }
        slot   = _chr_
        llx    = _llx_
        lly    = _lly_
        urx    = _urx_
        ury    = _ury_
        width  = urx - llx
        height = ury
        depth  = -lly
        total  = total + 1
        inline = mpfonts.inline
    end,
    flushfigure = function(t)
        for i=1,#t do
            code[#code+1] = t[i]
        end
    end,
    stopfigure = function()
        local cd = chardata[n]
        local code = unicodetoactualtext(slot,concat(code," ")) or ""
        descriptions[slot] = {
        --  unicode     = slot,
            name        = cd and cd.adobename,
            width       = width * 100,
            height      = height * 100,
            depth       = depth * 100,
            boundingbox = { llx, lly, urx, ury },
        }
        if inline then
            characters[slot] = {
                commands = {
                    { "special", "pdf:" .. code },
                }
            }
        else
            characters[slot] = {
                commands = {
                    {
                        "image",
                        {
                            stream = code,
                            bbox   = { 0, -depth * 65536, width * 65536, height * 65536 }
                        },
                    },
                }
            }
        end
        code = nil -- no need to keep that
    end
}

local function process(mpxformat,name,instances,scalefactor)
    local filename = resolvers.findfile(name)
    local attributes = filename and lfs.isfile(filename) and lfs.attributes(filename)
    if attributes then
        statistics.starttiming(metapost.fonts)
        scalefactor = scalefactor or 1
        instances = instances or metapost.fonts.instances or 1 -- maybe store in liost too
        local fontname = file.removesuffix(file.basename(name))
        local modification = attributes.modification
        local filesize = attributes.size
        local hash = file.robustname(formatters["%s %05i %03i"](fontname,scalefactor*1000,instances))
        local lists = containers.read(mpfonts.cache,hash)
        if not lists or lists.modification ~= modification or lists.filesize ~= filesize or lists.instances ~= instances or lists.scalefactor ~= scalefactor then
            statistics.starttiming(flusher)
            local data = io.loaddata(filename)
            metapost.reset(mpxformat)
            metapost.setoutercolor(2) -- no outer color and no reset either
            lists = { }
            for i=1,instances do
                characters   = { }
                descriptions = { }
                metapost.process(
                    mpxformat,
                    {
                        formatters["randomseed := %s ;"](i*10),
                        formatters["charscale  := %s ;"](scalefactor),
                        data,
                    },
                    false,
                    flusher,
                    false,
                    false,
                    "all"
                )
                lists[i] = {
                    characters   = characters,
                    descriptions = descriptions,
                    parameters   = {
                        designsize    = 655360,
                        slant         =      0,
                        space         =    333   * scalefactor,
                        space_stretch =    166.5 * scalefactor,
                        space_shrink  =    111   * scalefactor,
                        x_height      =    431   * scalefactor,
                        quad          =   1000   * scalefactor,
                        extra_space   =      0,
                    },
                    properties  = {
                        name          = formatters["%s-%03i"](hash,i),
                        virtualized   = true,
                        spacer        = "space",
                    }
                }
            end
            lists.version = metapost.variables.fontversion or "1.000"
            lists.modification = modification
            lists.filesize = filesize
            lists.instances = instances
            lists.scalefactor = scalefactor
            metapost.reset(mpxformat) -- saves memory
            lists = containers.write(mpfonts.cache, hash, lists)
            statistics.stoptiming(flusher)
        end
        variants = variants + #lists
        statistics.stoptiming(metapost.fonts)
        return lists
    else
        return { }
    end
end

metapost.fonts.flusher   = flusher
metapost.fonts.instances = 1
metapost.fonts.process   = process

local function build(g,v)
    local size = g.specification.size
    local data = process(v[2],v[3],v[4],size/655360,v[6])
    local list = { }
    local t = { }
    for d=1,#data do
        t = fonts.constructors.scale(data[d],-1000)
        local id = font.nextid()
        t.fonts = { { id = id } }
        fontdata[id] = t
        if v[5] then
            vffonts.helpers.composecharacters(t)
        end
        list[d] = font.define(t)
    end
    for k, v in next, t do -- last t
        g[k] = v -- kind of replace, when not present, make nil
    end
    g.properties.virtualized = true
    g.variants = list
end

vffonts.combiner.commands.metapost = build
vffonts.combiner.commands.metafont = build

statistics.register("metapost font generation", function()
    if total > 0 then
        local time = statistics.elapsedtime(flusher)
        if total > 0 then
            return format("%i glyphs, %.3f seconds runtime, %i glyphs/second", total, time, total/time)
        else
            return format("%i glyphs, %.3f seconds runtime", total, time)
        end
    end
end)

statistics.register("metapost font loading",function()
    if variants > 0 then
        local time = statistics.elapsedtime(metapost.fonts)
        if variants > 0 then
            return format("%.3f seconds, %i instances, %0.3f instances/second", time, variants, variants/time)
        else
            return format("%.3f seconds, %i instances", time, variants)
        end
    end
end)

-- fonts.definers.methods.install( "bidi", {
--     {
--         "metapost",    -- method
--         "metafun",     -- format
--         "fontoeps.mp", -- filename
--         1,             -- instances
--         false,         -- compose
--     },
-- } )

local report = logs.reporter("metapost","fonts")

function metapost.fonts.define(specification)
    local fontname = specification.fontname or ""
    local filename = specification.filename or ""
    local format   = specification.format   or "metafun"
    if fontname == "" then
        report("no fontname given")
        return
    end
    if filename == "" then
        report("no filename given for %a",fontname)
        return
    end
    local fullname = resolvers.findfile(filename)
    if fullname == "" then
        report("unable to locate file %a",filename)
        return
    end
    report("generating font %a using format %a and file %a",fontname,format,filename)
    fonts.definers.methods.install(fontname, {
        {
            specification.engine    or "metapost",
            format,
            filename,
            specification.instances or 1,
            specification.compose   or false,
        },
    } )
end

interfaces.implement {
    name      = "definemetafont",
    actions   = metapost.fonts.define,
    arguments = {
        {
            { "fontname" },
            { "filename" },
        }
    }
}

-- metapost.fonts.define {
--     fontname = "bidi",
--     filename = "bidi-symbols.mp",
-- }