summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/lpdf-col.lua
blob: 877c01a1c24489dcf3f65790e22eab6ef7beb86b (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
if not modules then modules = { } end modules ['lpdf-col'] = {
    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"
}

local type, next, tostring, tonumber = type, next, tostring, tonumber
local char, byte, format, gsub, rep, gmatch = string.char, string.byte, string.format, string.gsub, string.rep, string.gmatch
local concat = table.concat
local round = math.round
local formatters = string.formatters

local backends, lpdf, nodes = backends, lpdf, nodes

local allocate                = utilities.storage.allocate
local formatters              = string.formatters

local nodeinjections          = backends.pdf.nodeinjections
local codeinjections          = backends.pdf.codeinjections
local registrations           = backends.pdf.registrations

local nodepool                = nodes.pool
local register                = nodepool.register
local pdfliteral              = nodepool.pdfliteral

local pdfconstant             = lpdf.constant
local pdfstring               = lpdf.string
local pdfdictionary           = lpdf.dictionary
local pdfarray                = lpdf.array
local pdfreference            = lpdf.reference
local pdfverbose              = lpdf.verbose
local pdfflushobject          = lpdf.flushobject
local pdfdelayedobject        = lpdf.delayedobject
local pdfflushstreamobject    = lpdf.flushstreamobject

local pdfshareobjectreference = lpdf.shareobjectreference

local addtopageattributes     = lpdf.addtopageattributes
local adddocumentcolorspace   = lpdf.adddocumentcolorspace
local adddocumentextgstate    = lpdf.adddocumentextgstate

local colors                  = attributes.colors
local transparencies          = attributes.transparencies
local registertransparancy    = transparencies.register
local registercolor           = colors.register
local colorsvalue             = colors.value
local transparenciesvalue     = transparencies.value
local forcedmodel             = colors.forcedmodel
local getpagecolormodel       = colors.getpagecolormodel

local c_transparency          = pdfconstant("Transparency")

local f_gray   = formatters["%.3F g %.3F G"]
local f_rgb    = formatters["%.3F %.3F %.3F rg %.3F %.3F %.3F RG"]
local f_cmyk   = formatters["%.3F %.3F %.3F %.3F k %.3F %.3F %.3F %.3F K"]
local f_spot   = formatters["/%s cs /%s CS %s SCN %s scn"]
local f_tr     = formatters["Tr%s"]
local f_cm     = formatters["q %F %F %F %F %F %F cm"]
local f_effect = formatters["%s Tc %s w %s Tr"]
local f_tr_gs  = formatters["/Tr%s gs"]
local f_num_1  = tostring
local f_num_2  = formatters["%s %s"]
local f_num_3  = formatters["%s %s %s"]
local f_num_4  = formatters["%s %s %s %s"]

local report_color = logs.reporter("colors","backend")

-- page groups (might move to lpdf-ini.lua)

local colorspaceconstants = allocate { -- v_none is ignored
    gray = pdfconstant("DeviceGray"),
    rgb  = pdfconstant("DeviceRGB"),
    cmyk = pdfconstant("DeviceCMYK"),
    all  = pdfconstant("DeviceRGB"), -- brr
}

local transparencygroups = { }

lpdf.colorspaceconstants = colorspaceconstants
lpdf.transparencygroups  = transparencygroups

table.setmetatableindex(transparencygroups, function(transparencygroups,colormodel)
    local cs = colorspaceconstants[colormodel]
    if cs then
        local d = pdfdictionary {
            S  = c_transparency,
            CS = cs,
            I  = true,
        }
     -- local g = pdfreference(pdfflushobject(tostring(d)))
        local g = pdfreference(pdfdelayedobject(tostring(d)))
        transparencygroups[colormodel] = g
        return g
    else
        transparencygroups[colormodel] = false
        return false
    end
end)

local function addpagegroup()
    local model = getpagecolormodel()
    if model then
        local g = transparencygroups[model]
        if g then
            addtopageattributes("Group",g)
        end
    end
end

lpdf.registerpagefinalizer(addpagegroup,3,"pagegroup")

-- injection code (needs a bit reordering)

-- color injection

function nodeinjections.rgbcolor(r,g,b)
    return register(pdfliteral(f_rgb(r,g,b,r,g,b)))
end

function nodeinjections.cmykcolor(c,m,y,k)
    return register(pdfliteral(f_cmyk(c,m,y,k,c,m,y,k)))
end

function nodeinjections.graycolor(s) -- caching 0/1 does not pay off
    return register(pdfliteral(f_gray(s,s)))
end

function nodeinjections.spotcolor(n,f,d,p)
    if type(p) == "string" then
        p = gsub(p,","," ") -- brr misuse of spot
    end
    return register(pdfliteral(f_spot(n,n,p,p)))
end

function nodeinjections.transparency(n)
    return register(pdfliteral(f_tr_gs(n)))
end

-- a bit weird but let's keep it here for a while

local effects = {
    normal = 0,
    inner  = 0,
    outer  = 1,
    both   = 2,
    hidden = 3,
}

local bp = number.dimenfactors.bp

function nodeinjections.effect(effect,stretch,rulethickness)
    -- always, no zero test (removed)
    rulethickness = bp * rulethickness
    effect = effects[effect] or effects['normal']
    return register(pdfliteral(f_effect(stretch,rulethickness,effect))) -- watch order
end

-- spot- and indexcolors

local pdf_separation  = pdfconstant("Separation")
local pdf_indexed     = pdfconstant("Indexed")
local pdf_device_n    = pdfconstant("DeviceN")
local pdf_device_rgb  = pdfconstant("DeviceRGB")
local pdf_device_cmyk = pdfconstant("DeviceCMYK")
local pdf_device_gray = pdfconstant("DeviceGray")
local pdf_extgstate   = pdfconstant("ExtGState")

local pdf_rbg_range  = pdfarray { 0, 1, 0, 1, 0, 1 }
local pdf_cmyk_range = pdfarray { 0, 1, 0, 1, 0, 1, 0, 1 }
local pdf_gray_range = pdfarray { 0, 1 }

local f_rgb_function  = formatters["dup %s mul exch dup %s mul exch %s mul"]
local f_cmyk_function = formatters["dup %s mul exch dup %s mul exch dup %s mul exch %s mul"]
local f_gray_function = formatters["%s mul"]

local documentcolorspaces = pdfdictionary()

local spotcolorhash       = { } -- not needed
local spotcolornames      = { }
local indexcolorhash      = { }
local delayedindexcolors  = { }

function registrations.spotcolorname(name,e)
    spotcolornames[name] = e or name
end

function registrations.getspotcolorreference(name)
    return spotcolorhash[name]
end

-- beware: xpdf/okular/evince cannot handle the spot->process shade

-- This should become delayed i.e. only flush when used; in that case we need
-- need to store the specification and then flush them when accesssomespotcolor
-- is called. At this moment we assume that splotcolors that get defined are
-- also used which keeps the overhad small anyway. Tricky for mp ...

local processcolors

local function registersomespotcolor(name,noffractions,names,p,colorspace,range,funct)
    noffractions = tonumber(noffractions) or 1 -- to be checked
    if noffractions == 0 then
        -- can't happen
    elseif noffractions == 1 then
        local dictionary = pdfdictionary {
            FunctionType = 4,
            Domain       = { 0, 1 },
            Range        = range,
        }
        local calculations = pdfflushstreamobject(format("{ %s }",funct),dictionary)
     -- local calculations = pdfobject {
     --     type      = "stream",
     --     immediate = true,
     --     string    = format("{ %s }",funct),
     --     attr      = dictionary(),
     -- }
        local array = pdfarray {
            pdf_separation,
            pdfconstant(spotcolornames[name] or name),
            colorspace,
            pdfreference(calculations),
        }
        local m = pdfflushobject(array)
        local mr = pdfreference(m)
        spotcolorhash[name] = m
        documentcolorspaces[name] = mr
        adddocumentcolorspace(name,mr)
    else
        local cnames = pdfarray()
        local domain = pdfarray()
        local colorants = pdfdictionary()
        for n in gmatch(names,"[^,]+") do
            local name = spotcolornames[n] or n
            -- the cmyk names assume that they are indeed these colors
            if n == "cyan" then
                name = "Cyan"
            elseif n == "magenta" then
                name = "Magenta"
            elseif n == "yellow" then
                name = "Yellow"
            elseif n == "black" then
                name = "Black"
            else
                local sn = spotcolorhash[name] or spotcolorhash[n]
                if not sn then
                    report_color("defining %a as colorant",name)
                    colors.definespotcolor("",name,"p=1",true)
                    sn = spotcolorhash[name] or spotcolorhash[n]
                end
                if sn then
                    colorants[name] = pdfreference(sn)
                else
                    -- maybe some day generate colorants (spot colors for multi) automatically
                    report_color("unknown colorant %a, using black instead",name or n)
                    name = "Black"
                end
            end
            cnames[#cnames+1] = pdfconstant(name)
            domain[#domain+1] = 0
            domain[#domain+1] = 1
        end
        if not processcolors then
            local specification = pdfdictionary {
                ColorSpace = pdfconstant("DeviceCMYK"),
                Components = pdfarray {
                    pdfconstant("Cyan"),
                    pdfconstant("Magenta"),
                    pdfconstant("Yellow"),
                    pdfconstant("Black")
                }
            }
            processcolors = pdfreference(pdfflushobject(specification))
        end
        local dictionary = pdfdictionary {
            FunctionType = 4,
            Domain       = domain,
            Range        = range,
        }
        local calculation = pdfflushstreamobject(format("{ %s %s }",rep("pop ",noffractions),funct),dictionary)
        local channels = pdfdictionary {
            Subtype   = pdfconstant("NChannel"),
            Colorants = colorants,
            Process   = processcolors,
        }
        local array = pdfarray {
            pdf_device_n,
            cnames,
            colorspace,
            pdfreference(calculation),
            pdfshareobjectreference(tostring(channels)), -- optional but needed for shades
        }
        local m = pdfflushobject(array)
        local mr = pdfreference(m)
        spotcolorhash[name] = m
        documentcolorspaces[name] = mr
        adddocumentcolorspace(name,mr)
    end
end

-- wrong name

local function registersomeindexcolor(name,noffractions,names,p,colorspace,range,funct)
    noffractions = tonumber(noffractions) or 1 -- to be checked
    local cnames = pdfarray()
    local domain = pdfarray()
    if names == "" then
        names = name .. ",None"
    else
        names = names .. ",None"
    end
    for n in gmatch(names,"[^,]+") do
        cnames[#cnames+1] = pdfconstant(spotcolornames[n] or n)
        domain[#domain+1] = 0
        domain[#domain+1] = 1
    end
    local dictionary = pdfdictionary {
        FunctionType = 4,
        Domain       = domain,
        Range        = range,
    }
    local n = pdfflushstreamobject(format("{ %s %s }",rep("exch pop ",noffractions),funct),dictionary) -- exch pop
    local a = pdfarray {
        pdf_device_n,
        cnames,
        colorspace,
        pdfreference(n),
    }
    if p == "" then
        p = "1"
    else
        p = p .. ",1"
    end
    local pi = { }
    for pp in gmatch(p,"[^,]+") do
        pi[#pi+1] = tonumber(pp)
    end
    local vector, set, n = { }, { }, #pi
    for i=255,0,-1 do
        for j=1,n do
            set[j] = format("%02X",round(pi[j]*i))
        end
        vector[#vector+1] = concat(set)
    end
    vector = pdfverbose { "<", concat(vector, " "), ">" }
    local n = pdfflushobject(pdfarray{ pdf_indexed, a, 255, vector })
    adddocumentcolorspace(format("%s_indexed",name),pdfreference(n))
    return n
end

-- actually, names (parent) is the hash

local function delayindexcolor(name,names,func)
    local hash = (names ~= "" and names) or name
    delayedindexcolors[hash] = func
end

local function indexcolorref(name) -- actually, names (parent) is the hash
    if not indexcolorhash[name] then
        local delayedindexcolor = delayedindexcolors[name]
        if type(delayedindexcolor) == "function" then
            indexcolorhash[name] = delayedindexcolor()
            delayedindexcolors[name] = true
        end
    end
    return indexcolorhash[name]
end

function registrations.rgbspotcolor(name,noffractions,names,p,r,g,b)
    if noffractions == 1 then
        registersomespotcolor(name,noffractions,names,p,pdf_device_rgb,pdf_rbg_range,f_rgb_function(r,g,b))
    else
        registersomespotcolor(name,noffractions,names,p,pdf_device_rgb,pdf_rbg_range,f_num_3(r,g,b))
    end
    delayindexcolor(name,names,function()
        return registersomeindexcolor(name,noffractions,names,p,pdf_device_rgb,pdf_rgb_range,f_rgb_function(r,g,b))
    end)
end

function registrations.cmykspotcolor(name,noffractions,names,p,c,m,y,k)
    if noffractions == 1 then
        registersomespotcolor(name,noffractions,names,p,pdf_device_cmyk,pdf_cmyk_range,f_cmyk_function(c,m,y,k))
    else
        registersomespotcolor(name,noffractions,names,p,pdf_device_cmyk,pdf_cmyk_range,f_num_4(c,m,y,k))
    end
    delayindexcolor(name,names,function()
        return registersomeindexcolor(name,noffractions,names,p,pdf_device_cmyk,pdf_cmyk_range,f_cmyk_function(c,m,y,k))
    end)
end

function registrations.grayspotcolor(name,noffractions,names,p,s)
    if noffractions == 1 then
        registersomespotcolor(name,noffractions,names,p,pdf_device_gray,pdf_gray_range,f_gray_function(s))
    else
        registersomespotcolor(name,noffractions,names,p,pdf_device_gray,pdf_gray_range,f_num_1(s))
    end
    delayindexcolor(name,names,function()
        return registersomeindexcolor(name,noffractions,names,p,pdf_device_gray,pdf_gray_range,f_gray_function(s))
    end)
end

function registrations.rgbindexcolor(name,noffractions,names,p,r,g,b)
    registersomeindexcolor(name,noffractions,names,p,pdf_device_rgb,pdf_rgb_range,f_rgb_function(r,g,b))
end

function registrations.cmykindexcolor(name,noffractions,names,p,c,m,y,k)
    registersomeindexcolor(name,noffractions,names,p,pdf_device_cmyk,pdf_cmyk_range,f_cmyk_function(c,m,y,k))
end

function registrations.grayindexcolor(name,noffractions,names,p,s)
    registersomeindexcolor(name,noffractions,names,p,pdf_device_gray,pdf_gray_range,f_gray_function(s))
end

function codeinjections.setfigurecolorspace(data,figure)
    local color = data.request.color
    if color then
        local ref = indexcolorref(color)
        if ref then
            figure.colorspace = ref
            data.used.color = color
        end
    end
end

-- transparency

local transparencies = { [0] =
    pdfconstant("Normal"),
    pdfconstant("Normal"),
    pdfconstant("Multiply"),
    pdfconstant("Screen"),
    pdfconstant("Overlay"),
    pdfconstant("SoftLight"),
    pdfconstant("HardLight"),
    pdfconstant("ColorDodge"),
    pdfconstant("ColorBurn"),
    pdfconstant("Darken"),
    pdfconstant("Lighten"),
    pdfconstant("Difference"),
    pdfconstant("Exclusion"),
    pdfconstant("Hue"),
    pdfconstant("Saturation"),
    pdfconstant("Color"),
    pdfconstant("Luminosity"),
    pdfconstant("Compatible"), -- obsolete; 'Normal' is used in this case
}

local documenttransparencies = { }
local transparencyhash       = { } -- share objects

local done, signaled = false, false

function registrations.transparency(n,a,t)
    if not done then
        local d = pdfdictionary {
              Type = pdf_extgstate,
              ca   = 1,
              CA   = 1,
              BM   = transparencies[1],
              AIS  = false,
            }
        local m = pdfflushobject(d)
        local mr = pdfreference(m)
        transparencyhash[0] = m
        documenttransparencies[0] = mr
        adddocumentextgstate("Tr0",mr)
        done = true
    end
    if n > 0 and not transparencyhash[n] then
        local d = pdfdictionary {
              Type = pdf_extgstate,
              ca   = tonumber(t),
              CA   = tonumber(t),
              BM   = transparencies[tonumber(a)] or transparencies[0],
              AIS  = false,
            }
        local m = pdfflushobject(d)
        local mr = pdfreference(m)
        transparencyhash[n] = m
        documenttransparencies[n] = mr
        adddocumentextgstate(f_tr(n),mr)
    end
end

statistics.register("page group warning", function()
    if done then
        local model = getpagecolormodel()
        if model and not transparencygroups[model] then
            return "transparencies are used but no pagecolormodel is set"
        end
    end
end)

-- Literals needed to inject code in the mp stream, we cannot use attributes there
-- since literals may have qQ's, much may go away once we have mplib code in place.
--
-- This module assumes that some functions are defined in the colors namespace
-- which most likely will be loaded later.

local function lpdfcolor(model,ca,default) -- todo: use gray when no color
    if colors.supported then
        local cv = colorsvalue(ca)
        if cv then
            if model == 1 then
                model = cv[1]
            end
            model = forcedmodel(model)
            if model == 2 then
                local s = cv[2]
                return f_gray(s,s)
            elseif model == 3 then
                local r, g, b = cv[3], cv[4], cv[5]
                return f_rgb(r,g,b,r,g,b)
            elseif model == 4 then
                local c, m, y, k = cv[6],cv[7],cv[8],cv[9]
                return f_cmyk(c,m,y,k,c,m,y,k)
            else
                local n,f,d,p = cv[10],cv[11],cv[12],cv[13]
                if type(p) == "string" then
                    p = gsub(p,","," ") -- brr misuse of spot
                end
                return f_spot(n,n,p,p)
            end
        else
            return f_gray(default or 0,default or 0)
        end
    else
        return ""
    end
end

lpdf.color = lpdfcolor

interfaces.implement {
    name      = "lpdf_color",
    actions   = { lpdfcolor, context },
    arguments = "integer"
}

function lpdf.colorspec(model,ca,default)
    if ca and ca > 0 then
        local cv = colors.value(ca)
        if cv then
            if model == 1 then
                model = cv[1]
            end
            if model == 2 then
                return pdfarray { cv[2] }
            elseif model == 3 then
                return pdfarray { cv[3],cv[4],cv[5] }
            elseif model == 4 then
                return pdfarray { cv[6],cv[7],cv[8],cv[9] }
            elseif model == 5 then
                return pdfarray { cv[13] }
            end
        end
    end
    if default then
        return default
    end
end

function lpdf.pdfcolor(attribute) -- bonus, for pgf and friends
    return lpdfcolor(1,attribute)
end

function lpdf.transparency(ct,default) -- kind of overlaps with transparencycode
    -- beware, we need this hack because normally transparencies are not
    -- yet registered and therefore the number is not not known ... we
    -- might use the attribute number itself in the future
    if transparencies.supported then
        local ct = transparenciesvalue(ct)
        if ct then
            return f_tr_gs(registertransparancy(nil,ct[1],ct[2],true))
        else
            return f_tr_gs(0)
        end
    else
        return ""
    end
end

function lpdf.colorvalue(model,ca,default)
    local cv = colorsvalue(ca)
    if cv then
        if model == 1 then
            model = cv[1]
        end
        model = forcedmodel(model)
        if model == 2 then
            return f_num_1(cv[2])
        elseif model == 3 then
            return f_num_3(cv[3],cv[4],cv[5])
        elseif model == 4 then
            return f_num_4(cv[6],cv[7],cv[8],cv[9])
        else
            return f_num_1(cv[13])
        end
    else
        return f_num_1(default or 0)
    end
end

function lpdf.colorvalues(model,ca,default)
    local cv = colorsvalue(ca)
    if cv then
        if model == 1 then
            model = cv[1]
        end
        model = forcedmodel(model)
        if model == 2 then
            return cv[2]
        elseif model == 3 then
            return cv[3], cv[4], cv[5]
        elseif model == 4 then
            return cv[6], cv[7], cv[8], cv[9]
        elseif model == 4 then
            return cv[13]
        end
    else
        return default or 0
    end
end

function lpdf.transparencyvalue(ta,default)
    local tv = transparenciesvalue(ta)
    if tv then
        return tv[2]
    else
        return default or 1
    end
end

function lpdf.colorspace(model,ca)
    local cv = colorsvalue(ca)
    if cv then
        if model == 1 then
            model = cv[1]
        end
        model = forcedmodel(model)
        if model == 2 then
            return "DeviceGray"
        elseif model == 3 then
            return "DeviceRGB"
        elseif model == 4 then
            return "DeviceCMYK"
        end
    end
    return "DeviceGRAY"
end

-- by registering we getconversion for free (ok, at the cost of overhead)

local intransparency = false
local pdfcolor       = lpdf.color

function lpdf.rgbcode(model,r,g,b)
    if colors.supported then
        return pdfcolor(model,registercolor(nil,'rgb',r,g,b))
    else
        return ""
    end
end

function lpdf.cmykcode(model,c,m,y,k)
    if colors.supported then
        return pdfcolor(model,registercolor(nil,'cmyk',c,m,y,k))
    else
        return ""
    end
end

function lpdf.graycode(model,s)
    if colors.supported then
        return pdfcolor(model,registercolor(nil,'gray',s))
    else
        return ""
    end
end

function lpdf.spotcode(model,n,f,d,p)
    if colors.supported then
        return pdfcolor(model,registercolor(nil,'spot',n,f,d,p)) -- incorrect
    else
        return ""
    end
end

function lpdf.transparencycode(a,t)
    if transparencies.supported then
        intransparency = true
        return f_tr_gs(registertransparancy(nil,a,t,true)) -- true forces resource
    else
        return ""
    end
end

function lpdf.finishtransparencycode()
    if transparencies.supported and intransparency then
        intransparency = false
        return f_tr_gs(0) -- we happen to know this -)
    else
        return ""
    end
end

-- this will move to lpdf-spe.lua

local f_slant = formatters["pdf: q 1 0 %F 1 0 0 cm"]

backends.pdf.tables.vfspecials = allocate { -- todo: distinguish between glyph and rule color

    red        = { "special", 'pdf: 1 0 0 rg 1 0 0 RG' },
    green      = { "special", 'pdf: 0 1 0 rg 0 1 0 RG' },
    blue       = { "special", 'pdf: 0 0 1 rg 0 0 1 RG' },
    gray       = { "special", 'pdf: .75 g .75 G' },
    black      = { "special", 'pdf: 0 g 0 G' },

    rulecolors = {
            red        = { "special", 'pdf: 1 0 0 rg' },
            green      = { "special", 'pdf: 0 1 0 rg' },
            blue       = { "special", 'pdf: 0 0 1 rg' },
            gray       = { "special", 'pdf: .5 g' },
            black      = { "special", 'pdf: 0 g' },
            palered    = { "special", 'pdf: 1 .75 .75 rg' },
            palegreen  = { "special", 'pdf: .75 1 .75 rg' },
            paleblue   = { "special", 'pdf: .75 .75 1 rg' },
            palegray   = { "special", 'pdf: .75 g' },
    },

    startslant = function(a) return { "special", f_slant(a) } end,
    stopslant  = { "special", "pdf: Q" },

}