2020-07-12 02:50:27 +02:00
|
|
|
local pdf = pdf
|
2019-07-17 21:14:34 +02:00
|
|
|
local min = math.min
|
|
|
|
local format = string.format
|
|
|
|
local concat = table.concat
|
2020-07-01 14:10:30 +02:00
|
|
|
local pdfvariable = pdf.variable
|
2020-07-12 02:50:27 +02:00
|
|
|
local function write(pfile, tree, total, max)
|
|
|
|
tree = tree or pfile.pages
|
2019-07-17 21:14:34 +02:00
|
|
|
if #tree == 0 then
|
2020-07-12 02:50:27 +02:00
|
|
|
local id = pfile:getobj()
|
|
|
|
pfile:indirect(id, '<</Type/Pages/Kids[]/Count 0>>')
|
2019-07-17 21:14:34 +02:00
|
|
|
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
|
2020-07-12 02:50:27 +02:00
|
|
|
local parentid = pfile:getobj()
|
2019-07-17 21:14:34 +02:00
|
|
|
newtree[-(i//6)] = parentid
|
|
|
|
parent = format("/Parent %i 0 R", parentid)
|
2020-07-01 14:10:30 +02:00
|
|
|
elseif #tree <= 6 then
|
2020-07-12 02:50:27 +02:00
|
|
|
parent = pdfvariable.pagesattr .. pdf.pagesattributes
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-07-12 02:50:27 +02:00
|
|
|
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)))
|
2019-07-17 21:14:34 +02:00
|
|
|
remaining = remaining - max
|
|
|
|
end
|
2020-07-01 14:10:30 +02:00
|
|
|
if newtree[0] then
|
2020-07-12 02:50:27 +02:00
|
|
|
return write(pfile, newtree, total, max*6)
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
|
|
|
return newtree[1]
|
|
|
|
end
|
2020-07-12 02:50:27 +02:00
|
|
|
local function newpage(pfile)
|
|
|
|
local pages = pfile.pages
|
2020-07-02 11:02:38 +02:00
|
|
|
local pagenumber = #pages+1
|
2020-07-02 14:18:12 +02:00
|
|
|
local pageid = pages.reserved and pages.reserved[pagenumber]
|
|
|
|
if pageid then
|
|
|
|
pages.reserved[pagenumber] = nil
|
|
|
|
else
|
2020-07-12 02:50:27 +02:00
|
|
|
pageid = pfile:getobj()
|
2020-07-02 14:18:12 +02:00
|
|
|
end
|
2020-07-02 11:02:38 +02:00
|
|
|
pages[pagenumber] = pageid
|
|
|
|
if 1 == pagenumber % 6 then
|
2020-07-12 02:50:27 +02:00
|
|
|
pages[-((pagenumber-1)//6)] = pfile:getobj()
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-07-02 11:02:38 +02:00
|
|
|
return pageid, pages[-((pagenumber-1)//6)]
|
|
|
|
end
|
2020-07-12 02:50:27 +02:00
|
|
|
local function reservepage(pfile, num)
|
|
|
|
local pages = pfile.pages
|
2020-07-02 11:02:38 +02:00
|
|
|
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
|
2020-07-12 02:50:27 +02:00
|
|
|
reserved[num] = pfile:getobj()
|
2020-07-02 11:02:38 +02:00
|
|
|
return reserved[num]
|
2019-07-17 21:14:34 +02:00
|
|
|
end
|
2020-06-06 04:09:56 +02:00
|
|
|
return {
|
|
|
|
write = write,
|
|
|
|
newpage = newpage,
|
2020-07-02 11:02:38 +02:00
|
|
|
reservepage = reservepage,
|
2020-06-06 04:09:56 +02:00
|
|
|
}
|