summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/meta-blb.lua
blob: 23d069d1ee41af593fb4412b9c4200ad97dc84fe (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
355
356
357
358
359
360
if not modules then modules = { } end modules ['meta-blb'] = {
    version   = 1.001,
    comment   = "companion to mlib-ctx.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

-- This could be integrated in other modules but for me it also serves
-- as an example of usign the plugin mechanism.

local tonumber = tonumber

local setmetatableindex = table.setmetatableindex
local insert, remove = table.insert, table.remove
local formatters = string.formatters

local topoints        = number.topoints
local mpprint         = mp.print
local mpinteger       = mp.integer
local mppoints        = mp.points
local mptriplet       = mp.triplet
local mptripletpoints = mp.tripletpoints

local texsetbox       = tex.setbox
local toutf           = nodes.toutf
local hpack_nodes     = nodes.hpack

local trace           = false
local report          = logs.reporter("metapost","blobs")

trackers.register("metapost.blobs", function(v) trace = v end)

-- We start with a text that comes from an analyze stage and can end up with
-- one or more results. For practical reasons we store the blobs in one array
-- and work with ranges.

local allblobs = { }

local function newcategory(t,k)
    if trace then
        report("new category %a",k)
    end
    local v = {
        name  = k,
        text  = "",
        blobs = { },
    }
    t[k] = v
    return v
end

local texblobs = setmetatableindex(newcategory)

local function blob_raw_reset(category)
    -- we need to keep the allblobs
    if category then
        if trace then
            report("reset category %a",category)
        end
        texblobs[category] = nil
    else
        if trace then
            report("reset all")
        end
        texblobs = setmetatableindex(newcategory)
    end
end

local function blob_raw_dimensions(i)
    local blob = allblobs[i]
    if blob then
        return blob.width, blob.height, blob.depth
    else
        return 0, 0, 0
    end
end

local function blob_raw_content(i)
    return allblobs[i]
end

local function blob_raw_toutf(i)
    return toutf(allblobs[i])
end

local function blob_raw_wipe(i)
    allblobs[i] = false
end

mp.mf_blob_raw_dimensions = blob_raw_dimensions
mp.mf_blob_raw_content    = blob_raw_content
mp.mf_blob_raw_reset      = blob_raw_reset
mp.mf_blob_raw_wipe       = blob_raw_wipe
mp.mf_blob_raw_toutf      = blob_raw_toutf

function mp.mf_blob_new(category,text)
    if trace then
        report("category %a, text %a",category,text)
    end
    texblobs[category].text = text
end

function mp.mf_blob_add(category,blob)
    local tb = texblobs[category].blobs
    local tn = #allblobs + 1
    blob = hpack_nodes(blob)
    allblobs[tn] = blob
    tb[#tb+1] = tn
    if trace then
        report("category %a, blob %a set, content %a",category,tn,blob_raw_toutf(tn))
    end
end

function mp.mf_blob_width(category,i)
    local index = texblobs[category].blobs[i]
    local blob  = allblobs[index]
    if blob then
        mppoints(blob.width or 0)
    else
        mpinteger(0)
    end
end

function mp.mf_blob_size(category,i)
    mpprint(#texblobs[category].blobs or 0)
end

function mp.mf_blob_index(category,i)
    mpprint(texblobs[category].blobs[i] or 0)
end

function mp.mf_blob_dimensions(category,i)
    local index = texblobs[category].blobs[i]
    local blob  = allblobs[index]
    if blob then
        mptripletpoints(blob.width,blob.height,blob.depth)
    else
        mptriplet(0,0,0)
    end
end

local sxsy = metapost.sxsy
local cm   = metapost.cm

local f_f  = formatters["%.6F"]

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

local function injectblob(object,blob)
    local sx, rx, ry, sy, tx, ty = cm(object)
    local wd, ht, dp = blob_raw_dimensions(blob)
    if wd then
        object.path    = false
        object.color   = false
        object.grouped = true
        object.istext  = true
        return function()
            if trace then
                report("injecting blob %a, width %p, heigth %p, depth %p, text %a",blob,wd,ht,dp,blob_raw_toutf(blob))
            end
            context.MPLIBgetblobscaledcm(blob,
                f_f(sx), f_f(rx), f_f(ry),
                f_f(sy), f_f(tx), f_f(ty),
                sxsy(wd,ht,dp))
        end
    end
end

-- mp.mf_blob_inject = injectblob

local function getblob(box,blob)
    texsetbox(box,blob_raw_content(blob))
    blob_raw_wipe(blob)
end

interfaces.implement {
    name      = "mpgetblob",
    actions   = getblob,
    arguments = { "integer", "integer" },
}

-- the plug:


local function reset()
    blob_raw_reset()
end

local function analyze(object,prescript)
    -- nothing
end

local function process(object,prescript,before,after)
--     if prescript.tb_stage == "inject" then
        local tb_blob = tonumber(prescript.tb_blob)
        if tb_blob then
            before[#before+1] = injectblob(object,tb_blob)
        end
--     end
end

metapost.installplugin {
    name    = "texblob",
    reset   = reset,
    analyze = analyze,
    process = process,
}

-- Here follows an example of usage of the above: a more modern
-- version of followokens (in meta-imp-txt.mkiv).

local nodecodes       = nodes.nodecodes
local kerncodes       = nodes.kerncodes

local glue_code       = nodecodes.glue
local kern_code       = nodecodes.kern
local c_userkern      = kerncodes.userkern
local c_fontkern      = kerncodes.fontkern
local c_italickern    = kerncodes.italickern
local a_fontkern      = attributes.private("fontkern")

local takebox         = tex.takebox
local flatten_list    = node.flatten_discretionaries
local remove_node     = nodes.remove
local flush_node      = nodes.flush

local addblob         = mp.mf_blob_add
local newblob         = mp.mf_blob_new

local visible_code = {
    [nodecodes.glyph] = true,
    [nodecodes.glue]  = true,
    [nodecodes.hlist] = true,
    [nodecodes.vlist] = true,
    [nodecodes.rule]  = true,
}

local function initialize(category,box)
    local wrap = takebox(box)
    if wrap then
        local head = wrap.list
        local tail = nil
        local temp = nil
        if head then
            local n = { }
            local s = 0
            head = flatten_list(head)
            local current = head
            while current do
                local id = current.id
                if visible_code[id] then
                    head, current, tail = remove_node(head,current)
                    s = s + 1
                    n[s] = tail
                elseif id == kern_code then
                    local subtype = current.subtype
                    if subtype == c_fontkern or subtype == italickern then -- or current[a_fontkern]
                        head, current, temp = remove_node(head,current)
                        tail.next = temp
                        temp.prev = tail
                        tail = temp
                    else
                        head, current, temp = remove_node(head,current)
                        s = s + 1
                        n[s] = temp
                    end
                elseif id == glue_code then
                    head, current, temp = remove_node(head,current)
                    s = s + 1
                    n[s] = temp
                else
                    current = current.next
                end
            end
            for i=1,s do
                n[i] = addblob(category,n[i])
            end
            wrap.list = head
        end
        flush_node(wrap)
    end
end

interfaces.implement {
    name      = "MPLIBconvertfollowtext",
    arguments = { "integer","integer" },
    actions   = initialize,
}

local ft_reset, ft_analyze, ft_process  do

    if metapost.use_one_pass then

        local mp_category = 0
        local mp_str      = ""

        function mp.mf_inject_blob(category,str)
            newblob(category,str) -- only for tracing
            mp_category = category
            mp_str      = str
            tex.runtoks("mpblobtext")
        end

        interfaces.implement {
            name    = "mpblobtext",
            actions = function()
                context.MPLIBfollowtext(mp_category,mp_str)
            end
        }

        ft_process = function(object,prescript,before,after)
            if prescript.ft_category then
                object.path    = false
                object.color   = false
                object.grouped = true
                object.istext  = true
            end
        end


    else

        ft_reset = function()
            -- nothing
        end

        ft_analyze = function(object,prescript)
            if prescript.ft_stage == "trial" then
                local ft_category = tonumber(prescript.ft_category)
                if ft_category then
                    newblob(ft_category,object.postscript) -- only for tracing
                    context.MPLIBfollowtext(ft_category,object.postscript)
                    metapost.getjobdata().multipass = true
                end
            end
        end

        ft_process = function(object,prescript,before,after)
            if prescript.ft_stage == "final" then
                object.path    = false
                object.color   = false
                object.grouped = true
                object.istext  = true
            end
        end


    end

end

metapost.installplugin {
    name    = "followtext",
    reset   = ft_reset,
    analyze = ft_analyze,
    process = ft_process,
}