summaryrefslogtreecommitdiff
path: root/tex/context/base/lpdf-epd.lua
blob: 8a17aeb6b9da076dfb4ba74b6c6a85657f24b4b9 (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
if not modules then modules = { } end modules ['lpdf-epd'] = {
    version   = 1.001,
    comment   = "companion to lpdf-epa.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- This is an experimental layer around the epdf library. Because that
-- library is not yet finished and will get a clear api (independent of
-- the underlying pdf library which has an instable api) it will take
-- a while before this module is completed. Also, some integration with
-- other lpdf code might happen (i.e. we might generate lpdf objects).

local setmetatable, rawset = setmetatable, rawset

-- used:
--
-- arrayGet arrayGetNF dictLookup getTypeName arrayGetLength
-- getNum getString getBool getName getRef
-- getResourceDict getMediaBox getCropBox getBleedBox getTrimBox getArtBox
-- getPageRef getKindName findDestgetNumPages getDests getPage getCatalog getAnnots
--
-- needed:
--
-- add accessor methods to the resource dict
-- a function to mark objects as to be included

local lpdf = lpdf

-- -- -- helpers -- -- --

local cache_lookups = false

local checked_access

local array_access = {
    __index = function(t,k)
        local d = t.__data__
        if tonumber(k) then
            return checked_access(t,k,d:arrayGetNF(k))
        elseif k == "all" then
            local result = { }
            for i=1,t.size do
                result[i] = checked_access(t,k,d:arrayGetNF(i))
            end
            return result
        elseif k == "width" then
            return checked_access(t,k,d:arrayGetNF(3)) - checked_access(t,k,d:arrayGetNF(1))
        elseif k == "height" then
            return checked_access(t,k,d:arrayGetNF(4)) - checked_access(t,k,d:arrayGetNF(2))
        end
    end,
}

local dictionary_access = {
    __index = function(t,k)
        return checked_access(t,k,t.__data__:dictLookup(k))
    end
}

checked_access = function(tab,key,v)
    local n = v:getTypeName()
--~ print("!!!!!!!!!!!!!!",n)
    if n == "array" then
        local t = { __data__ = v, size = v:arrayGetLength() }
        setmetatable(t,array_access)
        if cache_lookups then rawset(tab,key,t) end
        return t
    elseif n == "dictionary" then
        local t = { __data__ = v, }
        setmetatable(t,dictionary_access)
        if cache_lookups then rawset(tab,key,t) end
        return t
    elseif n == "real" or n == "integer" then
        return v:getNum()
    elseif n == "string" then
        return v:getString()
    elseif n == "boolean" then
        return v:getBool()
    elseif n == "name" then
        return v:getName()
    elseif n == "ref" then
        return v:getRef(v.num,v.gen)
    else
        return v
    end
end

local basic_annots_access = {
    __index = function(t,k)
        local a = {
            __data__ = t.__data__:arrayGet(k),
        }
        setmetatable(a,dictionary_access)
        if cache_lookups then rawset(t,k,a) end
        return a
    end
}

local basic_resources_access = { -- == dictionary_access
    __index = function(t,k)
--~ local d = t.__data__
--~ print(d)
--~ print(d:getTypeName())
        return checked_access(t,k,t.__data__:dictLookup(k))
    end
}

local basic_box_access = { -- here it makes sense to do the rawset
    __index = function(t,k)
        local d = t.__data__
        if     k == "all"           then return { d.x1, d.y1, d.x2, d.y2 }
        elseif k == "width"         then return d.x2 - d.x1
        elseif k == "height"        then return d.y2 - d.y1
        elseif k == 1 or k == "llx" then return d.x1
        elseif k == 2 or k == "lly" then return d.y1
        elseif k == 3 or k == "urx" then return d.x2
        elseif k == 4 or k == "lly" then return d.y2
        else                        return 0 end
    end
}

-- -- -- pages -- -- --

local page_access = {
    __index = function(t,k)
        local d = t.__data__
        if k == "Annots" then
            local annots = d:getAnnots()
            local a = {
                __data__ = annots,
                size = annots:arrayGetLength()
            }
            setmetatable(a,basic_annots_access)
            rawset(t,k,a)
            return a
        elseif k == "Resources" then
            local r = {
                __data__ = d:getResourceDict(),
            }
            setmetatable(r,basic_resources_access)
            rawset(t,k,r)
            return r
        elseif k == "MediaBox" or k == "TrimBox" or k == "CropBox" or k == "ArtBox" or k == "BleedBox" then
            local b = {
             -- __data__ = d:getMediaBox(),
                __data__ = d["get"..k](d),
            }
            setmetatable(b,basic_box_access)
            rawset(t,k,b)
            return b
        end
    end
}

-- -- -- catalog -- -- --

local destination_access = {
    __index = function(t,k)
        if k == "D" then
            local d = t.__data__
            local p = {
                d:getPageRef(k), d:getKindName(k)
            }
            if cache_lookups then rawset(t,k,p) end -- not needed
            return p
        end
    end
}

local destinations_access = {
    __index = function(t,k)
        local d = t.__catalog__
        local p = {
            __data__  = d:findDest(k),
        }
        setmetatable(p,destination_access)
        if cache_lookups then rawset(t,k,p) end
        return p
    end
}

local catalog_access = {
    __index = function(t,k)
        local c = t.__catalog__
        if k == "Pages" then
            local s = c:getNumPages()
            local r = {
            }
            local p = {
                __catalog__ = c,
                size        = s,
                references  = r,
            }
         -- we load all pages as we need to resolve refs
            for i=1,s do
                local di, ri = c:getPage(i), c:getPageRef(i)
                local pi = {
                    __data__  = di,
                    reference = ri,
                    number    = i,
                }
                setmetatable(pi,page_access)
                p[i], r[ri.num] = pi, i
            end
         -- setmetatable(p,pages_access)
            rawset(t,k,p)
            return p
        elseif k == "Destinations" or k == "Dest" then
            local d = c:getDests()
            local p = {
                __catalog__ = c,
            }
            setmetatable(p,destinations_access)
            rawset(t,k,p)
            return p
        elseif k == "Metadata" then
            local m = c:readMetadata()
            local p = { -- we fake a stream dictionary
                __catalog__ = c,
                stream      = m,
                Type        = "Metadata",
                Subtype     = "XML",
                Length      = #m,
            }
         -- rawset(t,k,p)
            return p
        end
    end
}

local document_access = {
    __index = function(t,k)
        if k == "Catalog" then
            local c = {
                __catalog__ = t.__root__:getCatalog(),
            }
            setmetatable(c,catalog_access)
            rawset(t,k,c)
            return c
        end
    end
}

function lpdf.load(filename)
    local document = {
        __root__ = epdf.open(filename),
        filename = filename,
    }
    setmetatable(document,document_access)
    return document
end