Legacy PDF outline support

This commit is contained in:
Marcel Krüger 2020-06-07 13:10:33 +02:00
parent 7e3a4e3c74
commit f3af2599da
2 changed files with 30 additions and 7 deletions

View File

@ -572,14 +572,16 @@ token.luacmd("pdfextension", function(_, imm)
local actionobj = scan_action() local actionobj = scan_action()
action = pfile:indirect(nil, get_action_attr(pfile, actionobj)) action = pfile:indirect(nil, get_action_attr(pfile, actionobj))
end end
local outline = get_outline()
if token.scan_keyword'level' then if token.scan_keyword'level' then
local level = token.scan_int() local level = token.scan_int()
local open = token.scan_keyword'open' local open = token.scan_keyword'open'
local outline = get_outline()
local title = token.scan_string() local title = token.scan_string()
outline:add(pdf_string(title), action, level, open, attr) outline:add(pdf_string(title), action, level, open, attr)
else else
error[[Legacy outline not yet supported]] local count = token.scan_keyword'count' and token.scan_int() or 0
local title = token.scan_string()
outline:add_legacy(pdf_string(title), action, count, attr)
end end
elseif token.scan_keyword"dest" then elseif token.scan_keyword"dest" then
local id local id

View File

@ -1,8 +1,3 @@
local outline = {
{ title = "title 1", attr = "", open = true, level = 5,
{ ... },
}
}
local function add_outline(outline, title, action, level, open, attr) local function add_outline(outline, title, action, level, open, attr)
local entry = {title = title, action = action, attr = attr, open = open, level = level} local entry = {title = title, action = action, attr = attr, open = open, level = level}
-- Now find the right nesting level. We have to deal with non-continuous -- Now find the right nesting level. We have to deal with non-continuous
@ -15,6 +10,31 @@ local function add_outline(outline, title, action, level, open, attr)
until not outline or outline.level >= level until not outline or outline.level >= level
parent[#parent + 1] = entry parent[#parent + 1] = entry
end end
local function add_legacy(outline, title, action, count, attr)
local state = outline.legacy
if not state then
state = {}
outline.legacy = state
end
local level = #state
if level ~= 0 then
state[level] = state[level] - 1
end
local open
if count and count ~= 0 then
open = count > 0
if not open then
count = -count
end
state[level+1] = count
else
open = false -- Doesn't matter without children
while state[#state] == 0 do
state[#state] = nil
end
end
return outline:add(title, action, level, open, attr)
end
local function assign_objnum(pdf, outline) local function assign_objnum(pdf, outline)
local objnum = pdf:getobj() local objnum = pdf:getobj()
outline.objnum = objnum outline.objnum = objnum
@ -94,6 +114,7 @@ end
local meta = {__index = { local meta = {__index = {
write = write_outline, write = write_outline,
add = add_outline, add = add_outline,
add_legacy = add_legacy,
}} }}
return function() return function()
return setmetatable({}, meta) return setmetatable({}, meta)