summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/lpdf-nod.lua
blob: 9d4bf9270d5179e62187100c436c53aba7172742 (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
if not modules then modules = { } end modules ['lpdf-nod'] = {
    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 = type

local formatters            = string.formatters

local nodecodes             = nodes.nodecodes
local whatsitcodes          = nodes.whatsitcodes

local nodeinjections        = backends.nodeinjections

local nuts                  = nodes.nuts
local tonut                 = nuts.tonut

local setfield              = nuts.setfield
local setdata               = nuts.setdata

local copy_node             = nuts.copy
local new_node              = nuts.new

local nodepool              = nuts.pool
local register              = nodepool.register

local whatsit_code          = nodecodes.whatsit

local savewhatsit_code      = whatsitcodes.save
local restorewhatsit_code   = whatsitcodes.restore
local setmatrixwhatsit_code = whatsitcodes.setmatrix
local literalwhatsit_code   = whatsitcodes.literal

local literalvalues         = nodes.literalvalues
local originliteral_code    = literalvalues.origin
local pageliteral_code      = literalvalues.page
local directliteral_code    = literalvalues.direct
local rawliteral_code       = literalvalues.raw

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)

local function 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

if CONTEXTLMTXMODE then

    local nodeproperties = nodes.properties.data

    local pdfliteral = register(new_node(whatsit_code,literalwhatsit_code))

    function nodepool.pdforiginliteral(str) local t = copy_node(pdfliteral) nodeproperties[t] = { data = str, mode = originliteral_code } return t end
    function nodepool.pdfpageliteral  (str) local t = copy_node(pdfliteral) nodeproperties[t] = { data = str, mode = pageliteral_code   } return t end
    function nodepool.pdfdirectliteral(str) local t = copy_node(pdfliteral) nodeproperties[t] = { data = str, mode = directliteral_code } return t end
    function nodepool.pdfrawliteral   (str) local t = copy_node(pdfliteral) nodeproperties[t] = { data = str, mode = rawliteral_code    } return t end

    local pdfliterals = {
        -- by number
        [originliteral_code] = originliteral_code,
        [pageliteral_code]   = pageliteral_code,
        [directliteral_code] = directliteral_code,
        [rawliteral_code]    = rawliteral_code,
        -- by name
        [literalvalues[originliteral_code]] = originliteral_code,
        [literalvalues[pageliteral_code]]   = pageliteral_code,
        [literalvalues[directliteral_code]] = directliteral_code,
        [literalvalues[rawliteral_code]]    = rawliteral_code,
    }

    function nodepool.pdfliteral(mode,str)
        local t = copy_node(pdfliteral)
        if str then
            nodeproperties[t] = { data = str, mode = pdfliterals[mode] or pageliteral_code }
        else
            nodeproperties[t] = { data = mode, mode = pageliteral_code }
        end
        return t
    end

else

    local pdforiginliteral = register(new_node(whatsit_code, literalwhatsit_code))  setfield(pdforiginliteral,"mode",originliteral_code)
    local pdfpageliteral   = register(new_node(whatsit_code, literalwhatsit_code))  setfield(pdfpageliteral,  "mode",pageliteral_code)
    local pdfdirectliteral = register(new_node(whatsit_code, literalwhatsit_code))  setfield(pdfdirectliteral,"mode",directliteral_code)
    local pdfrawliteral    = register(new_node(whatsit_code, literalwhatsit_code))  setfield(pdfrawliteral,   "mode",rawliteral_code)

    function nodepool.pdforiginliteral(str) local t = copy_node(pdforiginliteral) setdata(t,str) return t end
    function nodepool.pdfpageliteral  (str) local t = copy_node(pdfpageliteral  ) setdata(t,str) return t end
    function nodepool.pdfdirectliteral(str) local t = copy_node(pdfdirectliteral) setdata(t,str) return t end
    function nodepool.pdfrawliteral   (str) local t = copy_node(pdfrawliteral   ) setdata(t,str) return t end

    local pdfliterals = {
        -- by number
        [originliteral_code] = pdforiginliteral,
        [pageliteral_code]   = pdfpageliteral,
        [directliteral_code] = pdfdirectliteral,
        [rawliteral_code]    = pdfrawliteral,
        -- by name
        [literalvalues[originliteral_code]] = pdforiginliteral,
        [literalvalues[pageliteral_code]]   = pdfpageliteral,
        [literalvalues[directliteral_code]] = pdfdirectliteral,
        [literalvalues[rawliteral_code]]    = pdfrawliteral,
    }

    function nodepool.pdfliteral(mode,str)
        if str then
            local t = copy_node(pdfliterals[mode] or pdfpageliteral)
            setdata(t,str)
            return t
        else
            local t = copy_node(pdfpageliteral)
            setdata(t,mode)
            return t
        end
    end

end

local pdfsave      = register(new_node(whatsit_code, savewhatsit_code))
local pdfrestore   = register(new_node(whatsit_code, restorewhatsit_code))
local pdfsetmatrix = register(new_node(whatsit_code, setmatrixwhatsit_code))

function nodepool.pdfsave()
    return copy_node(pdfsave)
end

function nodepool.pdfrestore()
    return copy_node(pdfrestore)
end

if CONTEXTLMTXMODE then

    local nodeproperties = nodes.properties.data

    function nodepool.pdfsetmatrix(rx,sx,sy,ry,tx,ty)
        local t = copy_node(pdfsetmatrix)
        nodeproperties[t] = { matrix = tomatrix(rx,sx,sy,ry,tx,ty) }
        return t
    end

else

    function nodepool.pdfsetmatrix(rx,sx,sy,ry,tx,ty)
        local t = copy_node(pdfsetmatrix)
        setdata(t,tomatrix(rx,sx,sy,ry,tx,ty))
        return t
    end

end

-- best is to use a specific one: origin | page | direct | raw

nodeinjections.save      = nodepool.pdfsave
nodeinjections.restore   = nodepool.pdfrestore
nodeinjections.transform = nodepool.pdfsetmatrix

-- the next one is implemented differently, using latelua

function nodepool.pdfannotation(w,h,d,data,n)
    report("don't use node based annotations!")
    os.exit()
 -- local t = copy_node(pdfannot)
 -- if w and w ~= 0 then
 --     setfield(t,"width",w)
 -- end
 -- if h and h ~= 0 then
 --     setfield(t,"height",h)
 -- end
 -- if d and d ~= 0 then
 --     setfield(t,"depth",d)
 -- end
 -- if n then
 --     setfield(t,"objnum",n)
 -- end
 -- if data and data ~= "" then
 --     setfield(t,"data",data)
 -- end
 -- return t
end

-- (!) The next code in pdfdest.w is wrong:
--
-- case pdf_dest_xyz:
--     if (matrixused()) {
--         set_rect_dimens(pdf, p, parent_box, cur, alt_rule, pdf_dest_margin) ;
--     } else {
--         pdf_ann_left(p) = pos.h ;
--         pdf_ann_top (p) = pos.v ;
--     }
--     break ;
--
-- so we need to force a matrix.

-- local views = { -- beware, we do support the pdf keys but this is *not* official
--     xyz   = 0, [variables.standard]  = 0,
--     fit   = 1, [variables.fit]       = 1,
--     fith  = 2, [variables.width]     = 2,
--     fitv  = 3, [variables.height]    = 3,
--     fitb  = 4,
--     fitbh = 5, [variables.minwidth]  = 5,
--     fitbv = 6, [variables.minheight] = 6,
--     fitr  = 7,
-- }

function nodepool.pdfdestination(w,h,d,name,view,n)
    report("don't use node based destinations!")
    os.exit()
 -- local t = copy_node(pdfdest)
 -- local hasdimensions = false
 -- if w and w ~= 0 then
 --     setfield(t,"width",w)
 --     hasdimensions = true
 -- end
 -- if h and h ~= 0 then
 --     setfield(t,"height",h)
 --     hasdimensions = true
 -- end
 -- if d and d ~= 0 then
 --     setfield(t,"depth",d)
 --     hasdimensions = true
 -- end
 -- if n then
 --     setfield(t,"objnum",n)
 -- end
 -- view = views[view] or view or 1 -- fit is default
 -- setfield(t,"dest_id",name)
 -- setfield(t,"dest_type",view)
 -- if hasdimensions and view == 0 then -- xyz
 --     -- see (!) s -> m -> t -> r
 --     -- linked
 --     local s = copy_node(pdfsave)
 --     local m = copy_node(pdfsetmatrix)
 --     local r = copy_node(pdfrestore)
 --     setfield(m,"data","1 0 0 1")
 --     setfield(s,"next",m)
 --     setfield(m,"next",t)
 --     setfield(t,"next",r)
 --     setfield(m,"prev",s)
 --     setfield(t,"prev",m)
 --     setfield(r,"prev",t)
 --     return s -- a list
 -- else
 --     return t
 -- end
end