summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/driv-ini.lua
blob: a19ebb359cce03b5bdc73101f9f2754f3ccbee08 (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
if not modules then modules = { } end modules ['driv-ini'] = {
    version   = 1.001,
    comment   = "companion to driv-ini.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type = type
local addsuffix = file.addsuffix

local setmetatableindex = table.setmetatableindex
local formatters        = string.formatters

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

local report            = logs.reporter("drivers")

local instances         = { }
local helpers           = { }
local converters        = { }
local prepared          = { }
local wrappedup         = { }
local cleanedup         = { }
local currentdriver     = "default"
local currentinstance   = nil

local shipout           = tex.shipout
local texgetbox         = tex.getbox
local texgetcount       = tex.getcount

function converters.engine(driver,boxnumber,mode,number,specification)
    return shipout(boxnumber)
end

local prepare           = nil
local convert           = nil
local wrapup            = nil
local cleanup           = nil
local outputfilename    = nil

drivers = drivers or {
    instances   = instances,
    helpers     = helpers,
    converters  = converters,
    lmtxversion = 0.10,
    report      = report,
}

local dummy = function() end

local defaulthandlers = {
    prepare         = dummy,
    initialize      = dummy,
    finalize        = dummy,
    updatefontstate = dummy,
    wrapup          = dummy,
    cleanup         = dummy,
    convert         = dummy,
    outputfilename  = dummy,
}

function drivers.install(specification)
    local name = specification.name
    if not name then
        report("missing driver name")
        return
    end
    local actions = specification.actions
    if not actions then
        report("no actions for driver %a",name)
        return
    end
    local flushers = specification.flushers
    if not flushers then
        report("no flushers for driver %a",name)
        return
    end
 -- report("driver %a is installed",name)
    setmetatableindex(actions, defaulthandlers)
    setmetatableindex(flushers, function() return dummy end)
    instances[name] = specification
end

function drivers.convert(boxnumber)
    if currentinstance then
        callbacks.functions.start_page_number()
        starttiming(drivers)
        convert(currentinstance,boxnumber,texgetcount("realpageno"))
        stoptiming(drivers)
        callbacks.functions.stop_page_number()
    end
end

function drivers.outputfilename()
    if currentinstance then
        return outputfilename(currentinstance)
    end
end

luatex.wrapup(function()
    if currentinstance and wrapup and not wrappedup[currentdriver] then
        starttiming(drivers)
        wrapup(currentinstance)
        stoptiming(drivers)
        wrappedup[currentdriver] = true
        cleanedup[currentdriver] = true
    end
end)

luatex.cleanup(function()
    if currentinstance and cleanup and not cleanedup[currentdriver] then
        starttiming(drivers)
        cleanup(currentinstance)
        stoptiming(drivers)
        wrappedup[currentdriver] = true
        cleanedup[currentdriver] = true
    end
end)

function drivers.enable(name)
    if name ~= currentdriver then
        if currentinstance then
            starttiming(drivers)
            cleanup(currentinstance)
            stoptiming(drivers)
        end
        currentdriver   = name or "default"
        currentinstance = instances[currentdriver]
        if currentinstance then
            local actions   = currentinstance.actions
            prepare         = actions.prepare
            wrapup          = actions.wrapup
            cleanup         = actions.cleanup
            convert         = actions.convert
            outputfilename  = actions.outputfilename
            --
            if prepare and not prepared[currentdriver] then
                starttiming(drivers)
                prepare(currentinstance)
                stoptiming(drivers)
                prepared[currentdriver] = true
            end
        else
            report("bad driver")
        end
    end
end

statistics.register("driver time",function()
    return statistics.elapsedseconds(drivers)
end)

interfaces.implement {
    name      = "shipoutpage",
    arguments = "integer",
    actions   = drivers.convert,
}

interfaces.implement {
    name      = "enabledriver",
    arguments = "string",
    actions   = drivers.enable,
}

-- The default driver:

do

    local filename = nil

    drivers.install {
        name     = "default",
        actions  = {
            convert        = drivers.converters.engine,
            outputfilename = function(driver)
                if not filename then
                    filename = addsuffix(tex.jobname,"pdf")
                end
                return filename
            end,
        },
        flushers = {
            -- we always need this entry
        },
    }

end

-- No driver:

do

    drivers.install {
        name     = "none",
        actions  = { },
        flushers = { },
    }

end

do

    local function prepare(driver)
        converter = drivers.converters.lmtx
    end

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

    drivers.install {
        name     = "empty",
        actions  = {
            prepare = prepare,
            convert = convert,
        },
        flushers = { },
    }

end

--

setmetatableindex(instances,function() return instances.default end)

-- for now:

drivers.enable("default")

-- helpers

local s_matrix_0 = "1 0 0 1"
local f_matrix_2 = formatters["%.6F 0 0 %.6F"]
local f_matrix_4 = formatters["%.6F %.6F %.6F %.6F"]

directives.register("pdf.stripzeros",function()
    f_matrix_2 = formatters["%.6N 0 0 %.6N"]
    f_matrix_4 = formatters["%.6N %.6N %.6N %.6N"]
end)

function helpers.tomatrix(rx,sx,sy,ry,tx,ty) -- todo: tx ty
    if type(rx) == "string" then
        return rx
    else
        if not rx then
            rx = 1
        elseif rx == 0 then
            rx = 0.0001
        end
        if not ry then
            ry = 1
        elseif ry == 0 then
            ry = 0.0001
        end
        if not sx then
            sx = 0
        end
        if not sy then
            sy = 0
        end
        if sx == 0 and sy == 0 then
            if rx == 1 and ry == 1 then
                return s_matrix_0
            else
                return f_matrix_2(rx,ry)
            end
        else
            return f_matrix_4(rx,sx,sy,ry)
        end
    end
end