summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/back-lua.lua
blob: 4e95d37e3fb88adeb0b78c7a9b9a3740ab129d6f (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
if not modules then modules = { } end modules ['back-lua'] = {
    version   = 1.001,
    comment   = "companion to lpdf-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- we can remap fonts

local fontproperties    = fonts.hashes.properties
local fontparameters    = fonts.hashes.parameters
local fontshapes        = fonts.hashes.shapes

local starttiming       = statistics.starttiming
local stoptiming        = statistics.stoptiming

local bpfactor          = number.dimenfactors.bp
local texgetbox         = tex.getbox

local rulecodes         = nodes.rulecodes
local normalrule_code   = rulecodes.normal
----- boxrule_code      = rulecodes.box
----- imagerule_code    = rulecodes.image
----- emptyrule_code    = rulecodes.empty
----- userrule_code     = rulecodes.user
----- overrule_code     = rulecodes.over
----- underrule_code    = rulecodes.under
----- fractionrule_code = rulecodes.fraction
----- radicalrule_code  = rulecodes.radical
local outlinerule_code  = rulecodes.outline

-- todo : per instance

local pages     = { }
local fonts     = { }
local names     = { }
local mapping   = { }
local used      = { }
local shapes    = { }
local glyphs    = { }
local buffer    = { }
local b         = 0
local converter = nil

local compact   = false -- true
local shapestoo = true

local x, y, d, f, c, w, h, t, r, o

local function reset()
    buffer = { }
    b      = 0
    t      = nil
    x      = nil
    y      = nil
    d      = nil
    f      = nil
    c      = nil
    w      = nil
    h      = nil
    r      = nil
    o      = nil
end

local function result()
    -- todo: we're now still in the pdf backend but need different codeinjections
    local codeinjections = backends.pdf.codeinjections
    local getvariable    = codeinjections.getidentityvariable or function() end
    local jobname        = environment.jobname or tex.jobname or "unknown"
    return {
        metadata = {
            unit     = "bp",
            jobname  = jobname,
            title    = getvariable("title") or jobname,
            subject  = getvariable("subject"),
            author   = getvariable("author"),
            keywords = getvariable("keywords"),
            time     = os.date("%Y-%m-%d %H:%M"),
            engine   = environment.luatexengine .. " " .. environment.luatexversion,
            context  = environment.version,
        },
        fonts  = fonts,
        pages  = pages,
        shapes = shapes,
    }
end

-- actions

local function outputfilename(driver)
    return tex.jobname .. "-output.lua"
end

local function save() -- might become a driver function that already is plugged into stopactions
    local filename = outputfilename()
    drivers.report("saving result in %a",filename)
    starttiming(drivers)
    local data = result()
    if data then
        io.savedata(filename,table.serialize(data))
    end
    stoptiming(drivers)
end

local function prepare(driver)
    converter = drivers.converters.lmtx
    luatex.registerstopactions(1,function()
        save()
    end)
end

local function initialize(driver,details)
    reset()
end

local function finalize(driver,details)
    local n = details.pagenumber
    local b = details.boundingbox
    pages[n] = {
        number      = n,
        content     = buffer,
        boundingbox = {
            b[1] * bpfactor,
            b[2] * bpfactor,
            b[3] * bpfactor,
            b[4] * bpfactor,
        },
    }
end

local function wrapup(driver)
end

local function cleanup(driver)
    reset()
end

local function convert(driver,boxnumber,pagenumber)
    converter(driver,texgetbox(boxnumber),"page",pagenumber)
end

-- flushers

local function updatefontstate(id)
    if not mapping[id] then
        local fn = #fonts + 1
        mapping[id] = fn
        local properties = fontproperties[id]
        local parameters = fontparameters[id]
        local filename   = file.basename(properties.filename)
        local fontname   = properties.fullname or properties.fontname
        if shapestoo then
            if not names[fontname] then
                local sn = #shapes+1
                names[fontname] = sn
                shapes[sn] = { }
                glyphs[sn] = fontshapes[id].glyphs
            end
        end
        fonts[fn] = {
            filename = filename,
            name     = fontname,
            size     = parameters.size * bpfactor,
            shapes   = shapestoo and names[fontname] or nil,
        }
    end
end

local function flushcharacter(current, pos_h, pos_v, pos_r, font, char)
    local fnt = mapping[font]
    b = b + 1
    buffer[b] = {
        t = "glyph" ~= t and "glyph" or nil,
        f = font    ~= f and fnt or nil,
        c = char    ~= c and char or nil,
        x = pos_h   ~= x and (pos_h * bpfactor) or nil,
        y = pos_v   ~= y and (pos_v * bpfactor) or nil,
        d = pos_r   ~= d and (pos_r == 1 and "r2l" or "l2r") or nil,
    }
    t = "glyph"
    f = font
    c = char
    x = pos_h
    y = pos_v
    d = pos_r
    if shapestoo then
        -- can be sped up if needed
        local sn = fonts[fnt].shapes
        local shp = shapes[sn]
        if not shp[char] then
            shp[char] = glyphs[sn][char]
        end
    end
end

local function rule(pos_h, pos_v, pos_r, size_h, size_v, rule_s, rule_o)
    b = b + 1
    buffer[b] = {
        t = "rule" ~= t and "rule" or nil,
        r = rule_s ~= r and rule_s or nil,
        o = rule_s == "outline" and rule_o ~= o and (rule_o * bpfactor) or nil,
        w = size_h ~= w and (size_h * bpfactor) or nil,
        h = size_v ~= h and (size_v * bpfactor) or nil,
        x = pos_h  ~= x and (pos_h  * bpfactor) or nil,
        y = pos_v  ~= y and (pos_v  * bpfactor) or nil,
        d = pos_r  ~= d and (pos_r == 1 and "r2l" or "l2r") or nil,
    }
    t = "rule"
    w = size_h
    h = size_v
    x = pos_h
    y = pos_v
    d = pos_r
end

local function flushrule(current, pos_h, pos_v, pos_r, size_h, size_v, subtype)
    local rule_s, rule_o
    if subtype == normalrule_code then
        rule_s = "normal"
    elseif subtype == outlinerule_code then
        rule_s = "outline"
        rule_o = getdata(current)
    else
        return
    end
    return rule(pos_h, pos_v, pos_r, size_h, size_v, rule_s, rule_o)
end

local function flushsimplerule(current, pos_h, pos_v, pos_r, size_h, size_v)
    return rule(pos_h, pos_v, pos_r, size_h, size_v, "normal", nil)
end

-- file stuff too
-- todo: default flushers
-- also color (via hash)

-- installer

drivers.install {
    name    = "lua",
    actions = {
        prepare         = prepare,
        initialize      = initialize,
        finalize        = finalize,
        wrapup          = wrapup,
        cleanup         = cleanup,
        convert         = convert,
        outputfilename  = outputfilename,
    },
    flushers = {
        updatefontstate = updatefontstate,
        character       = flushcharacter,
        rule            = flushrule,
    }
}

-- actions

local function outputfilename(driver)
    return tex.jobname .. "-output.json"
end

local function save() -- might become a driver function that already is plugged into stopactions
    local filename = outputfilename()
    drivers.report("saving result in %a",filename)
    starttiming(drivers)
    local data = result()
    if data then
        if not utilities.json then
            require("util-jsn")
        end
        io.savedata(filename,utilities.json.tostring(data,not compact))
    end
    stoptiming(drivers)
end

local function prepare(driver)
    converter = drivers.converters.lmtx
    luatex.registerstopactions(1,function()
        save()
    end)
end

-- installer

drivers.install {
    name    = "json",
    actions = {
        prepare         = prepare,
        initialize      = initialize,
        finalize        = finalize,
        wrapup          = wrapup,
        cleanup         = cleanup,
        convert         = convert,
        outputfilename  = outputfilename,
    },
    flushers = {
        updatefontstate = updatefontstate,
        character       = flushcharacter,
        rule            = flushrule,
    }
}

-- actions

local function outputfilename(driver)
    return tex.jobname .. "-output.js"
end

local function save() -- might become a driver function that already is plugged into stopactions
    local filename = outputfilename()
    drivers.report("saving result in %a",filename)
    starttiming(drivers)
    local data = result()
    if data then
        if not utilities.json then
            require("util-jsn")
        end
        io.savedata(filename,
            "const JSON_CONTEXT = JSON.parse ( `" ..
            utilities.json.tostring(data,not compact) ..
            "` ) ;\n"
        )
    end
    stoptiming(drivers)
end

local function prepare(driver)
    converter = drivers.converters.lmtx
    luatex.registerstopactions(1,function()
        save()
    end)
end

-- installer

drivers.install {
    name    = "js",
    actions = {
        prepare         = prepare,
        initialize      = initialize,
        finalize        = finalize,
        wrapup          = wrapup,
        cleanup         = cleanup,
        convert         = convert,
        outputfilename  = outputfilename,
    },
    flushers = {
        updatefontstate = updatefontstate,
        character       = flushcharacter,
        rule            = flushrule,
    }
}