summaryrefslogtreecommitdiff
path: root/tex/context/base/lpdf-nod.lua
blob: 6b104d2fa1c1b4c0659ef79c494aacde0d3ec429 (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
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 formatters     = string.formatters

local copy_node      = node.copy
local new_node       = node.new

local nodepool       = nodes.pool
local register       = nodepool.register
local whatsitcodes   = nodes.whatsitcodes
local nodeinjections = backends.nodeinjections

local pdfliteral     = register(new_node("whatsit", whatsitcodes.pdfliteral))    pdfliteral.mode  = 1
local pdfsave        = register(new_node("whatsit", whatsitcodes.pdfsave))
local pdfrestore     = register(new_node("whatsit", whatsitcodes.pdfrestore))
local pdfsetmatrix   = register(new_node("whatsit", whatsitcodes.pdfsetmatrix))
local pdfdest        = register(new_node("whatsit", whatsitcodes.pdfdest))       pdfdest.named_id = 1 -- xyz_zoom untouched
local pdfannot       = register(new_node("whatsit", whatsitcodes.pdfannot))

local variables      = interfaces.variables

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.pdfliteral(str)
    local t = copy_node(pdfliteral)
    t.data = str
    return t
end

function nodepool.pdfdirect(str)
    local t = copy_node(pdfliteral)
    t.data = str
    t.mode = 1
    return t
end

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

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

function nodepool.pdfsetmatrix(rx,sx,sy,ry,tx,ty)
    local t = copy_node(pdfsetmatrix)
    t.data = formatters["%s %s %s %s"](rx or 0,sx or 0,sy or 0,ry or 0) -- todo: tx ty
    return t
end

function nodepool.pdfsetmatrix(rx,sx,sy,ry,tx,ty)
    local t = copy_node(pdfsetmatrix)
    if type(rx) == "string" then
        t.data = 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
                t.data = "1 0 0 1"
            else
                t.data = formatters["%0.6f 0 0 %0.6f"](rx,ry)
            end
        else
            t.data = formatters["%0.6f %0.6f %0.6f %0.6f"](rx,sx,sy,ry)
        end
    end
    return t
end

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

function nodepool.pdfannotation(w,h,d,data,n)
    local t = copy_node(pdfannot)
    if w and w ~= 0 then
        t.width = w
    end
    if h and h ~= 0 then
        t.height = h
    end
    if d and d ~= 0 then
        t.depth = d
    end
    if n then
        t.objnum = n
    end
    if data and data ~= "" then
        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.

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