summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/meta-blb.lua
blob: e1c0de74f377dda7a5b797016e35bf631344ee27 (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
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 nuts            = nodes.nuts
local hpack           = nuts.hpack
local setbox          = nuts.setbox
local getwhd          = nuts.getwhd
local getwidth        = nuts.getwidth
local toutf           = nuts.toutf

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

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

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 getwhd(blob)
    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(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(getwidth(blob) 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(getwhd(blob))
    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)
    setbox(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 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,
    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 fontkern_code   = kerncodes.fontkern
local italickern_code = kerncodes.italickern

local a_fontkern      = attributes.private("fontkern")

local nuts            = nodes.nuts
local takebox         = nuts.takebox
local getlist         = nuts.getlist
local getid           = nuts.getid
local getsubtype      = nuts.getsubtype
local setlink         = nuts.setlink
local setlist         = nuts.setlist
local getnext         = nuts.getnext
local flatten_list    = nuts.flatten_discretionaries
local remove_node     = nuts.remove
local flush_node      = nuts.flush

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

local visible_codes = {
    [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 = getlist(wrap)
        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 = getid(current)
                if visible_codes[id] then
                    head, current, tail = remove_node(head,current)
                    s = s + 1
                    n[s] = tail
                elseif id == kern_code then
                    local subtype = getsubtype(current)
                    if subtype == fontkern_code or subtype == italickern_code then -- or current[a_fontkern]
                        head, current, temp = remove_node(head,current)
                        setlink(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 = getnext(current)
                end
            end
            for i=1,s do
                n[i] = addblob(category,n[i])
            end
            setlist(wrap,head)
        end
        flush_node(wrap)
    end
end

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

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
}

local 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

metapost.installplugin {
    name    = "followtext",
    process = process,
}