-
Notifications
You must be signed in to change notification settings - Fork 3
/
luametalatex-pdf-pagetree.lua
70 lines (70 loc) · 1.92 KB
/
luametalatex-pdf-pagetree.lua
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
local pdf = pdf
local min = math.min
local format = string.format
local concat = table.concat
local pdfvariable = pdf.variable
local function write(pfile, tree, total, max)
tree = tree or pfile.pages
if #tree == 0 then
local id = pfile:getobj()
pfile:indirect(id, '<</Type/Pages/Kids[]/Count 0>>')
return id
end
max = max or 6 -- These defaults only work on the lowest level
total = total or #tree
local remaining = total
-- if #tree == 1 then
-- retur
local newtree = {}
local parent = ""
for i=0,(#tree-1)//6 do
local id = tree[-i]
newtree[i+1] = id
if 0 == i % 6 and #tree > 6 then
local parentid = pfile:getobj()
newtree[-(i//6)] = parentid
parent = format("/Parent %i 0 R", parentid)
elseif #tree <= 6 then
parent = pdfvariable.pagesattr .. pdf.pagesattributes
end
pfile:indirect(id, format('<</Type/Pages%s/Kids[%s 0 R]/Count %i>>', parent, concat(tree, ' 0 R ', 6*i+1, min(#tree, 6*i+6)), min(remaining, max)))
remaining = remaining - max
end
if newtree[0] then
return write(pfile, newtree, total, max*6)
end
return newtree[1]
end
local function newpage(pfile)
local pages = pfile.pages
local pagenumber = #pages+1
local pageid = pages.reserved and pages.reserved[pagenumber]
if pageid then
pages.reserved[pagenumber] = nil
else
pageid = pfile:getobj()
end
pages[pagenumber] = pageid
if 1 == pagenumber % 6 then
pages[-((pagenumber-1)//6)] = pfile:getobj()
end
return pageid, pages[-((pagenumber-1)//6)]
end
local function reservepage(pfile, num)
local pages = pfile.pages
if pages[num] then return pages[num] end
local reserved = pages.reserved
if reserved then
if reserved[num] then return reserved[num] end
else
reserved = {}
pages.reserved = reserved
end
reserved[num] = pfile:getobj()
return reserved[num]
end
return {
write = write,
newpage = newpage,
reservepage = reservepage,
}