Implement pdf.[gs]etpage{resources,s?attributes}

This commit is contained in:
Marcel Krüger 2020-07-12 02:50:27 +02:00
parent 5262457cd8
commit 697f874089
2 changed files with 27 additions and 16 deletions

View File

@ -84,7 +84,7 @@ token.luacmd("shipout", function()
local out, resources, annots = writer(pfile, list, fontdirs, usedglyphs, colorstacks) local out, resources, annots = writer(pfile, list, fontdirs, usedglyphs, colorstacks)
cur_page = nil cur_page = nil
local content = pfile:stream(nil, '', out) local content = pfile:stream(nil, '', out)
pfile:indirect(page, string.format([[<</Type/Page/Parent %i 0 R/Contents %i 0 R/MediaBox[0 %i %i %i]/Resources%s%s%s>>]], parent, content, -math.ceil(to_bp(list.depth)), math.ceil(to_bp(list.width)), math.ceil(to_bp(list.height)), resources(pdfvariable.pageresources), annots, pdfvariable.pageattr)) pfile:indirect(page, string.format([[<</Type/Page/Parent %i 0 R/Contents %i 0 R/MediaBox[0 %i %i %i]/Resources%s%s%s%s>>]], parent, content, -math.ceil(to_bp(list.depth)), math.ceil(to_bp(list.width)), math.ceil(to_bp(list.height)), resources(pdfvariable.pageresources .. pdf.pageresources), annots, pdfvariable.pageattr, pdf.pageattributes))
node.flush_list(list) node.flush_list(list)
token.put_next(reset_deadcycles) token.put_next(reset_deadcycles)
token.scan_token() token.scan_token()
@ -924,3 +924,13 @@ end, "protected", "value")
token.luacmd("lastypos", function() token.luacmd("lastypos", function()
return integer_code, (saved_pos_y+.5)//1 return integer_code, (saved_pos_y+.5)//1
end, "protected", "value") end, "protected", "value")
local function pdf_register_funcs(name)
pdf[name] = ""
pdf['get' .. name] = function() return pdf[name] end
pdf['set' .. name] = function(s) pdf[name] = assert(s) end
end
pdf_register_funcs'pageattributes'
pdf_register_funcs'pageresources'
pdf_register_funcs'pagesattributes'

View File

@ -1,12 +1,13 @@
local pdf = pdf
local min = math.min local min = math.min
local format = string.format local format = string.format
local concat = table.concat local concat = table.concat
local pdfvariable = pdf.variable local pdfvariable = pdf.variable
local function write(pdf, tree, total, max) local function write(pfile, tree, total, max)
tree = tree or pdf.pages tree = tree or pfile.pages
if #tree == 0 then if #tree == 0 then
local id = pdf:getobj() local id = pfile:getobj()
pdf:indirect(id, '<</Type/Pages/Kids[]/Count 0>>') pfile:indirect(id, '<</Type/Pages/Kids[]/Count 0>>')
return id return id
end end
max = max or 6 -- These defaults only work on the lowest level max = max or 6 -- These defaults only work on the lowest level
@ -20,37 +21,37 @@ local function write(pdf, tree, total, max)
local id = tree[-i] local id = tree[-i]
newtree[i+1] = id newtree[i+1] = id
if 0 == i % 6 and #tree > 6 then if 0 == i % 6 and #tree > 6 then
local parentid = pdf:getobj() local parentid = pfile:getobj()
newtree[-(i//6)] = parentid newtree[-(i//6)] = parentid
parent = format("/Parent %i 0 R", parentid) parent = format("/Parent %i 0 R", parentid)
elseif #tree <= 6 then elseif #tree <= 6 then
parent = pdfvariable.pagesattr parent = pdfvariable.pagesattr .. pdf.pagesattributes
end end
pdf: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))) 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 remaining = remaining - max
end end
if newtree[0] then if newtree[0] then
return write(pdf, newtree, total, max*6) return write(pfile, newtree, total, max*6)
end end
return newtree[1] return newtree[1]
end end
local function newpage(pdf) local function newpage(pfile)
local pages = pdf.pages local pages = pfile.pages
local pagenumber = #pages+1 local pagenumber = #pages+1
local pageid = pages.reserved and pages.reserved[pagenumber] local pageid = pages.reserved and pages.reserved[pagenumber]
if pageid then if pageid then
pages.reserved[pagenumber] = nil pages.reserved[pagenumber] = nil
else else
pageid = pdf:getobj() pageid = pfile:getobj()
end end
pages[pagenumber] = pageid pages[pagenumber] = pageid
if 1 == pagenumber % 6 then if 1 == pagenumber % 6 then
pages[-((pagenumber-1)//6)] = pdf:getobj() pages[-((pagenumber-1)//6)] = pfile:getobj()
end end
return pageid, pages[-((pagenumber-1)//6)] return pageid, pages[-((pagenumber-1)//6)]
end end
local function reservepage(pdf, num) local function reservepage(pfile, num)
local pages = pdf.pages local pages = pfile.pages
if pages[num] then return pages[num] end if pages[num] then return pages[num] end
local reserved = pages.reserved local reserved = pages.reserved
if reserved then if reserved then
@ -59,7 +60,7 @@ local function reservepage(pdf, num)
reserved = {} reserved = {}
pages.reserved = reserved pages.reserved = reserved
end end
reserved[num] = pdf:getobj() reserved[num] = pfile:getobj()
return reserved[num] return reserved[num]
end end
return { return {