summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/mlib-pdf.lmt
blob: f537743aa16470f533f47b67fb2bbfd301f597fb (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
if not modules then modules = { } end modules ['mlib-pdf'] = {
    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",
}

local gsub = string.gsub
local concat, insert, remove, sortedkeys = table.concat, table.insert, table.remove, table.sortedkeys
local abs, sqrt, round = math.abs, math.sqrt, math.round
local setmetatable, rawset, tostring, tonumber, type = setmetatable, rawset, tostring, tonumber, type
local P, S, C, Ct, Cc, Cg, Cf, Carg = lpeg.P, lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.Carg
local lpegmatch = lpeg.match
local formatters = string.formatters

local report_metapost = logs.reporter("metapost")

local trace_variables = false  trackers.register("metapost.variables",function(v) trace_variables = v end)

local mplib           = mplib
local context         = context

local allocate        = utilities.storage.allocate

local peninfo         = mplib.peninfo
local getfields       = mplib.getfields or mplib.fields -- todo: in lmtx get them once and then use gettype

local save_table      = false
local force_stroke    = false

metapost              = metapost or { }
local metapost        = metapost

metapost.flushers     = metapost.flushers or { }
local pdfflusher      = { }
metapost.flushers.pdf = pdfflusher

metapost.n            = 0

local mpsliteral      = nodes.pool.originliteral

local f_f  = formatters["%.6N"]
local f_m  = formatters["%.6N %.6N m"]
local f_c  = formatters["%.6N %.6N %.6N %.6N %.6N %.6N c"]
local f_l  = formatters["%.6N %.6N l"]
local f_cm = formatters["%.6N %.6N %.6N %.6N %.6N %.6N cm"]
local f_M  = formatters["%.6N M"]
local f_j  = formatters["%i j"]
local f_J  = formatters["%i J"]
local f_d  = formatters["[%s] %.6N d"]
local f_w  = formatters["%.6N w"]

directives.register("metapost.savetable",function(v)
    if type(v) == "string" then
        save_table = file.addsuffix(v,"mpl")
    elseif v then
        save_table = file.addsuffix(environment.jobname .. "-graphic","mpl")
    else
        save_table = false
    end
end)

trackers.register("metapost.forcestroke",function(v)
    force_stroke = v
end)

-- local function gettolerance(objects)
--     return objects:tolerance()
-- end

function metapost.convert(specification,result)
    local flusher  = specification.flusher
    local askedfig = specification.askedfig
    if save_table then
        table.save(save_table,metapost.totable(result,1)) -- direct
    end
    metapost.flush(specification,result)
    return true -- done
end

function pdfflusher.tocomment(message)
    if message then
        return formatters["%% mps graphic %s: %s"](metapost.n,message)
    else
        return formatters["%% mps graphic %s"](metapost.n)
    end
end

function pdfflusher.startfigure(n,llx,lly,urx,ury,message)
    metapost.n = metapost.n + 1
    context.startMPLIBtoPDF(f_f(llx),f_f(lly),f_f(urx),f_f(ury))
end

function pdfflusher.stopfigure(message)
    context.stopMPLIBtoPDF()
end

function pdfflusher.flushfigure(pdfliterals) -- table
    if #pdfliterals > 0 then
        pdfliterals = concat(pdfliterals,"\n")
        context(mpsliteral(pdfliterals))
    end
end

function pdfflusher.textfigure(font,size,text,width,height,depth) -- we could save the factor
    text = gsub(text,".","\\hbox{%1}") -- kerning happens in metapost (i have to check if this is true for mplib)
    context.MPtextext(font,size,text,0,-number.dimenfactors.bp*depth)
end

local rx, sx, sy, ry, tx, ty, divider = 1, 0, 0, 1, 0, 0, 1

local function pen_characteristics(object)
    local t = peninfo(object)
    rx, ry, sx, sy, tx, ty = t.rx, t.ry, t.sx, t.sy, t.tx, t.ty
    divider = sx*sy - rx*ry
    return not (sx == 1 and rx == 0 and ry == 0 and sy == 1 and tx == 0 and ty == 0), t.width
end

local function mpconcat(px, py) -- no tx, ty here / we can move this one inline if needed
    return (sy*px-ry*py)/divider,(sx*py-rx*px)/divider
end

local getbendtolerance = metapost.getbendtolerance

local function curved(ith,pth,tolerance) --- still better than the build in
 -- if pth.right_x == pth.x_coord then
 --     if pth.right_y == pth.y_coord then
 --         if ith.left_x == ith.x_coord then
 --             if ith.left_y == ith.y_coord then
 --                 return false
 --             end
 --         end
 --     end
 -- end
    local d = pth.left_x - ith.right_x
    local b = abs(ith.right_x - ith.x_coord - d)
    if b <= tolerance then
        b = abs(pth.x_coord - pth.left_x - d)
        if b <= tolerance then
            d = pth.left_y - ith.right_y
            b = abs(ith.right_y - ith.y_coord - d)
            if b <= tolerance then
                b = abs(pth.y_coord - pth.left_y - d)
                if b <= tolerance then
--                  print(tolerance,ith.curved,pth.curved,false)
                    return false
                end
            end
        end
    end
--  print(tolerance,ith.curved,pth.curved,true)
    return true
end

local function flushnormalpath(path, t, open, tolerance)
    local pth, ith, nt
    local length = #path
    if t then
        nt = #t
    else
        t = { }
        nt = 0
    end
    for i=1,length do
        nt = nt + 1
        pth = path[i]
        if not ith then
            t[nt] = f_m(pth.x_coord,pth.y_coord)
        elseif curved(ith,pth,tolerance) then
-- elseif pth.curved then
            t[nt] = f_c(ith.right_x,ith.right_y,pth.left_x,pth.left_y,pth.x_coord,pth.y_coord)
        else
            t[nt] = f_l(pth.x_coord,pth.y_coord)
        end
        ith = pth
    end
    if not open then
        nt = nt + 1
        local one = path[1]
        if curved(pth,one,tolerance) then
-- if one.curved then
            t[nt] = f_c(pth.right_x,pth.right_y,one.left_x,one.left_y,one.x_coord,one.y_coord)
        else
            t[nt] = f_l(one.x_coord,one.y_coord)
        end
    elseif length == 1 then
        -- special case .. draw point
        local one = path[1]
        nt = nt + 1
        t[nt] = f_l(one.x_coord,one.y_coord)
    end
    return t
end

local function flushconcatpath(path, t, open, tolerance, transform)
    local pth, ith, nt
    local length = #path
    if t then
        nt = #t
    else
        t = { }
        nt = 0
    end
    if transform then
        nt = nt + 1
        t[nt] = f_cm(sx,rx,ry,sy,tx,ty)
    end
    for i=1,length do
        nt = nt + 1
        pth = path[i]
        if not ith then
            t[nt] = f_m(mpconcat(pth.x_coord,pth.y_coord))
        elseif curved(ith,pth,tolerance) then
-- elseif pth.curved then
            local a, b = mpconcat(ith.right_x,ith.right_y)
            local c, d = mpconcat(pth.left_x,pth.left_y)
            t[nt] = f_c(a,b,c,d,mpconcat(pth.x_coord,pth.y_coord))
        else
           t[nt] = f_l(mpconcat(pth.x_coord, pth.y_coord))
        end
        ith = pth
    end
    if not open then
        nt = nt + 1
        local one = path[1]
        if curved(pth,one,tolerance) then
-- if one.curved then
            local a, b = mpconcat(pth.right_x,pth.right_y)
            local c, d = mpconcat(one.left_x,one.left_y)
            t[nt] = f_c(a,b,c,d,mpconcat(one.x_coord, one.y_coord))
        else
            t[nt] = f_l(mpconcat(one.x_coord,one.y_coord))
        end
    elseif length == 1 then
        -- special case .. draw point
        nt = nt + 1
        local one = path[1]
        t[nt] = f_l(mpconcat(one.x_coord,one.y_coord))
    end
    return t
end

local function toboundingbox(path)
    local size = #path
    if size == 4 then
        local pth = path[1]
        local x = pth.x_coord
        local y = pth.y_coord
        local llx, lly, urx, ury = x, y, x, y
        for i=2,size do
            pth = path[i]
            x   = pth.x_coord
            y   = pth.y_coord
            if x < llx then
                llx = x
            elseif x > urx then
                urx = x
            end
            if y < lly then
                lly = y
            elseif y > ury then
                ury = y
            end
        end
        return { llx, lly, urx, ury }
    else
        return { 0, 0, 0, 0 }
    end
end


function metapost.flushnormalpath(path, t, open, tolerance)
    return flushnormalpath(path, t, open, tolerance or getbendtolerance())
end

-- The flusher is pdf based, if another backend is used, we need to overload the
-- flusher; this is beta code, the organization will change (already upgraded in
-- sync with mplib)
--
-- We can avoid the before table but I like symmetry. There is of course a small
-- performance penalty, but so is passing extra arguments (result, flusher, after)
-- and returning stuff.
--
-- This variable stuff will change in lmtx.

local ignore   = function() end

local space    = P(" ")
local equal    = P("=")
local key      = C((1-equal)^1) * equal
local newline  = S("\n\r")^1
local number   = (((1-space-newline)^1) / tonumber) * (space^0)

local p_number  = number
local p_string  = C((1-newline)^0)
local p_boolean = P("false") * Cc(false) + P("true") * Cc(true)
local p_set     = Ct(number^1)
local p_path    = Ct(Ct(number * number^-5)^1)

local variable =
    P("1:")            * p_number
  + P("2:")            * p_string
  + P("3:")            * p_boolean
  + S("4568") * P(":") * p_set
  + P("7:")            * p_path

local pattern_tab = Cf ( Carg(1) * (Cg(variable * newline^0)^0), rawset)

local variable =
    P("1:")            * p_number
  + P("2:")            * p_string
  + P("3:")            * p_boolean
  + S("4568") * P(":") * number^1
  + P("7:")            * (number * number^-5)^1

local pattern_lst = (variable * newline^0)^0

metapost.variables  = { } -- currently across instances
metapost.properties = { } -- to be stacked

function metapost.untagvariable(str,variables) -- will be redone
    if variables == false then
        return lpegmatch(pattern_lst,str)
    else
        return lpegmatch(pattern_tab,str,1,variables or { })
    end
end

-- function metapost.processspecial(str)
--     lpegmatch(pattern_key,object.prescript,1,variables)
-- end

-- function metapost.processspecial(str)
--     local code = loadstring(str)
--     if code then
--         if trace_variables then
--             report_metapost("executing special code: %s",str)
--         end
--         code()
--     else
--         report_metapost("invalid special code: %s",str)
--     end
-- end

local stack      = { }   -- general stack (not related to stacking)
local nostacking = { 0 } -- layers in figures

local function pushproperties(figure)
    -- maybe there will be getters in lmtx
    local boundingbox = figure:boundingbox()
    local slot = figure:charcode() or 0
    local properties = {
        llx    = boundingbox[1],
        lly    = boundingbox[2],
        urx    = boundingbox[3],
        ury    = boundingbox[4],
        slot   = slot,
        width  = figure:width(),
        height = figure:height(),
        depth  = figure:depth(),
        italic = figure:italic(),
        number = slot,
    }
    insert(stack,properties)
    metapost.properties = properties
    return properties
end

local function popproperties()
    metapost.properties = remove(stack)
end

function metapost.flush(specification,result)
    if result then
        local flusher   = specification.flusher
        local askedfig  = specification.askedfig
        local incontext = specification.incontext
        local filtering = specification.filtering
        local figures   = result.fig
        if figures then
            flusher = flusher or pdfflusher
            local resetplugins       = metapost.resetplugins       or ignore -- before figure
            local processplugins     = metapost.processplugins     or ignore -- each object
            local synchronizeplugins = metapost.synchronizeplugins or ignore
            local pluginactions      = metapost.pluginactions      or ignore -- before / after
            local startfigure        = flusher.startfigure
            local stopfigure         = flusher.stopfigure
            local flushfigure        = flusher.flushfigure
            local textfigure         = flusher.textfigure
         -- local processspecial     = flusher.processspecial or metapost.processspecial
            local tocomment          = flusher.tocomment
            if type(filtering) ~= "table" or not next(filtering) then
                filtering = false
            end
            -- patterns: we always use image 1 and then can use patterns for 2..n (or one number)
            -- we can then do an intermediate flush

            for index=1,#figures do
                local figure     = figures[index]
                local properties = pushproperties(figure)
                if askedfig == "direct" or askedfig == "all" or askedfig == properties.number then
                    local stacking   = figure:stacking() -- This has to happen before fetching objects!
                    local objects    = figure:objects()
                    local tolerance  = figure:tolerance() or getbendtolerance()
                    local result     = { }
                    local miterlimit = -1
                    local linecap    = -1
                    local linejoin   = -1
                    local dashed     = false
                    local linewidth  = false
                    local llx        = properties.llx
                    local lly        = properties.lly
                    local urx        = properties.urx
                    local ury        = properties.ury
                    if urx < llx then
                        -- invalid
                        startfigure(properties.number,0,0,0,0,"invalid",figure)
                        if tocomment then
                            result[#result+1] = tocomment("invalid")
                        end
                        stopfigure()
                    else

                        -- we need to be indirect if we want the one-pass solution

                        local groupstack = { }

                        local function processfigure()
                            if tocomment then
                                result[#result+1] = tocomment("begin")
                            end
                            result[#result+1] = "q"
                            if objects then
    --                             resetplugins(result) -- we should move the colorinitializer here
                                local savedpath = nil
                                local savedhtap = nil
                                if stacking then
                                    stacking = { }
                                    for o=1,#objects do
                                        local stack = objects[o].stacking
                                        if stack then
                                            if filtering then
                                                stacking[stack] = filtering[stack]
                                            else
                                                stacking[stack] = true
                                            end
                                        end
                                    end
                                    stacking = sortedkeys(stacking)
                                else
                                    stacking = nostacking
                                end
                                for i=1,#stacking do
                                    local stack = stacking[i]
                                    for o=1,#objects do
                                        local object = objects[o]
                                        if stack == object.stacking then
                                            local objecttype = object.type
                                         -- print(i,o,stack,objecttype)
                                            if objecttype == "fill" or objecttype == "outline" then
                                                -- we use an indirect table as we want to overload
                                                -- entries but this is not possible in userdata
                                                --
                                                -- can be optimized if no path
                                                --
                                                local original = object
                                                local object   = { }
                                                setmetatable(object, {
                                                    __index = original
                                                })
                                                local before,
                                                      after,
                                                      options    = processplugins(object)
                                                local evenodd    = false
                                                local collect    = false
                                                local both       = false
                                                local flush      = false
                                                local postscript = object.postscript
                                                local tolerance  = options and tonumber(options.tolerance) or tolerance
                                        -- if not object.istext then
                                                if postscript == "evenodd" then
                                                    evenodd = true
                                                elseif postscript == "collect" then
                                                    collect = true
                                                elseif postscript == "flush" then
                                                    flush   = true
                                                elseif postscript == "both" then
                                                    both = true
                                                elseif postscript == "eoboth" then
                                                    evenodd = true
                                                    both    = true
                                                end
                                        --  end
                                                --
                                                if flush and not savedpath then
                                                    -- forget about it
                                                elseif collect then
                                                    if not savedpath then
                                                        savedpath = { object.path or false }
                                                        savedhtap = { object.htap or false }
                                                    else
                                                        savedpath[#savedpath+1] = object.path or false
                                                        savedhtap[#savedhtap+1] = object.htap or false
                                                    end
                                                else
                                                    local objecttype = object.type -- can have changed
                                                    if before then
                                                        result = pluginactions(before,result,flushfigure)
                                                    end
                                                    local ml = object.miterlimit
                                                    if ml and ml ~= miterlimit then
                                                        miterlimit = ml
                                                        result[#result+1] = f_M(ml)
                                                    end
                                                    local lj = object.linejoin
                                                    if lj and lj ~= linejoin then
                                                        linejoin = lj
                                                        result[#result+1] = f_j(lj)
                                                    end
                                                    local lc = object.linecap
                                                    if lc and lc ~= linecap then
                                                        linecap = lc
                                                        result[#result+1] = f_J(lc)
                                                    end
                                                    if both then
                                                        if dashed ~= false then -- was just dashed test
                                                           result[#result+1] = "[] 0 d"
                                                           dashed = false
                                                        end
                                                    else
                                                        local dl = object.dash
                                                        if dl then
                                                            local d = f_d(concat(dl.dashes or {}," "),dl.offset)
                                                            if d ~= dashed then
                                                                dashed = d
                                                                result[#result+1] = d
                                                            end
                                                        elseif dashed ~= false then -- was just dashed test
                                                           result[#result+1] = "[] 0 d"
                                                           dashed = false
                                                        end
                                                    end
                                                    local path        = object.path -- newpath
                                                    local transformed = false
                                                    local penwidth    = 1
                                                    local open        = path and path[1].left_type and path[#path].right_type -- at this moment only "end_point"
                                                    local pen         = object.pen
                                                    if pen then
                                                       if pen.type == "elliptical" then
                                                            transformed, penwidth = pen_characteristics(original) -- boolean, value
                                                            if penwidth ~= linewidth then
                                                                result[#result+1] = f_w(penwidth)
                                                                linewidth = penwidth
                                                            end
                                                            if objecttype == "fill" then
                                                                objecttype = "both"
                                                            end
                                                       else -- calculated by mplib itself
                                                            objecttype = "fill"
                                                       end
                                                    end
                                                    if transformed then
                                                        result[#result+1] = "q"
                                                    end
                                                    if path then
                                                        if savedpath then
                                                            for i=1,#savedpath do
                                                                local path = savedpath[i]
                                                                if transformed then
                                                                    flushconcatpath(path,result,open,tolerance,i==1)
                                                                else
                                                                    flushnormalpath(path,result,open,tolerance)
                                                                end
                                                            end
                                                            savedpath = nil
                                                        end
                                                        if flush then
                                                            -- ignore this path
                                                        elseif transformed then
                                                            flushconcatpath(path,result,open,tolerance,true)
                                                        else
                                                            flushnormalpath(path,result,open,tolerance)
                                                        end
                                                        if force_stroke then
                                                            result[#result+1] = open and "S" or "h S"
                                                        elseif objecttype == "fill" then
                                                            result[#result+1] = evenodd and "h f*" or "h f" -- f* = eo
                                                        elseif objecttype == "outline" then
                                                            if both then
                                                                result[#result+1] = evenodd and "h B*" or "h B" -- B* = eo
                                                            else
                                                                result[#result+1] = open and "S" or "h S"
                                                            end
                                                        elseif objecttype == "both" then
                                                            result[#result+1] = evenodd and "h B*" or "h B" -- B* = eo -- b includes closepath
                                                        end
                                                    end
                                                    if transformed then
                                                        result[#result+1] = "Q"
                                                    end
                                                    local path = object.htap
                                                    if path then
                                                        if transformed then
                                                            result[#result+1] = "q"
                                                        end
                                                        if savedhtap then
                                                            for i=1,#savedhtap do
                                                                local path = savedhtap[i]
                                                                if transformed then
                                                                    flushconcatpath(path,result,open,tolerance,i==1)
                                                                else
                                                                    flushnormalpath(path,result,open,tolerance)
                                                                end
                                                            end
                                                            savedhtap = nil
                                                            evenodd   = true
                                                        end
                                                        if transformed then
                                                            flushconcatpath(path,result,open,tolerance,true)
                                                        else
                                                            flushnormalpath(path,result,open,tolerance)
                                                        end
                                                        if force_stroke then
                                                            result[#result+1] = open and "S" or "h S"
                                                        elseif objecttype == "fill" then
                                                            result[#result+1] = evenodd and "h f*" or "h f" -- f* = eo
                                                        elseif objecttype == "outline" then
                                                            result[#result+1] = open and "S" or "h S"
                                                        elseif objecttype == "both" then
                                                            result[#result+1] = evenodd and "h B*" or "h B" -- B* = eo -- b includes closepath
                                                        end
                                                        if transformed then
                                                            result[#result+1] = "Q"
                                                        end
                                                    end
                                                    if after then
                                                        result = pluginactions(after,result,flushfigure)
                                                    end
                                                end
                                                if object.grouped then
                                                    -- can be qQ'd so changes can end up in groups
                                                    miterlimit, linecap, linejoin, dashed, linewidth = -1, -1, -1, "", false
                                                end
                                            elseif objecttype == "start_clip" then
                                             -- local evenodd = not object.istext and object.postscript == "evenodd"
                                                local evenodd = object.postscript == "evenodd"
                                                result[#result+1] = "q"
                                                flushnormalpath(object.path,result,false,tolerance)
                                                result[#result+1] = evenodd and "W* n" or "W n"
                                            elseif objecttype == "stop_clip" then
                                                result[#result+1] = "Q"
                                                miterlimit, linecap, linejoin, dashed, linewidth = -1, -1, -1, "", false
                                            elseif objecttype == "start_bounds" or objecttype == "stop_bounds" then
                                                -- skip
                                            elseif objecttype == "start_group" then
                                                if lpdf.flushgroup then
                                                    local before, after = processplugins(object)
                                                    if before then
                                                        result[#result+1] = "q"
                                                        result = pluginactions(before,result,flushfigure)
                                                        insert(groupstack, {
                                                            after  = after,
                                                            result = result,
                                                            bbox   = toboundingbox(object.path),
                                                        })
                                                        result = { }
                                                        miterlimit, linecap, linejoin, dashed, linewidth = -1, -1, -1, "", false
                                                    else
                                                        insert(groupstack,false)
                                                    end
                                                else
                                                    insert(groupstack,false)
                                                end
                                            elseif objecttype == "stop_group" then
                                                local data = remove(groupstack)
                                                if data then
                                                    local reference = lpdf.flushgroup(concat(result,"\r"),data.bbox)
                                                    result = data.result
                                                    result[#result+1] = reference
                                                    result = pluginactions(data.after,result,flushfigure)
                                                    result[#result+1] = "Q"
                                                    miterlimit, linecap, linejoin, dashed, linewidth = -1, -1, -1, "", false
                                                end
                                         -- if objecttype == "text" then
                                         --     result[#result+1] = "q"
                                         --     local ot = object.transform -- 3,4,5,6,1,2
                                         --     result[#result+1] = f_cm(ot[3],ot[4],ot[5],ot[6],ot[1],ot[2])
                                         --     flushfigure(result) -- flush accumulated literals
                                         --     result = { }
                                         --     textfigure(object.font,object.dsize,object.text,object.width,object.height,object.depth)
                                         --     result[#result+1] = "Q"
                                         -- elseif objecttype == "special" then
                                         --     if processspecial then
                                         --         processspecial(object.prescript)
                                         --     end
                                         -- else
                                            else
                                                -- error
                                            end
                                        end
                                    end
                                end
                            end
                            result[#result+1] = "Q"
                            if tocomment then
                                result[#result+1] = tocomment("end")
                            end
                            flushfigure(result)
                        end
                        startfigure(properties.number,llx,lly,urx,ury,"begin",figure)
                        if incontext then
                            context(function() processfigure() end)
                        else
                            processfigure()
                        end
                        stopfigure("end")
                    end
                    if askedfig ~= "all" then
                        break
                    end
                end
                popproperties()
            end
            resetplugins(result) -- we should move the colorinitializer here
        end
    end
end

-- tracing:

do

    local result = { }

    local flusher = {
        startfigure = function()
            result = { }
            context.startnointerference()
        end,
        flushfigure = function(literals)
            local n = #result
            for i=1,#literals do
                result[n+i] = literals[i]
            end
        end,
        stopfigure = function()
            context.stopnointerference()
        end
    }

    local specification = {
        flusher   = flusher,
     -- incontext = true,
    }

    function metapost.pdfliterals(result)
        metapost.flush(specification,result)
        return result
    end

end

function metapost.totable(result,askedfig)
    local askedfig = askedfig or 1
    local figure   = result and result.fig and result.fig[1]
    if figure then
        local results = { }
        local objects = figure:objects()
        for o=1,#objects do
            local object = objects[o]
            local result = { }
            local fields = getfields(object) -- hm, is this the whole list, if so, we can get it once
            for f=1,#fields do
                local field   = fields[f]
                result[field] = object[field]
            end
            results[o] = result
        end
        local boundingbox = figure:boundingbox()
        return {
            boundingbox = {
                llx = boundingbox[1],
                lly = boundingbox[2],
                urx = boundingbox[3],
                ury = boundingbox[4],
            },
            objects = results
        }
    else
        return nil
    end
end