From f3af2599da855edcead7e48f828438e4c705c484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sun, 7 Jun 2020 13:10:33 +0200 Subject: [PATCH] Legacy PDF outline support --- luametalatex-back-pdf.lua | 6 ++++-- luametalatex-pdf-outline.lua | 31 ++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/luametalatex-back-pdf.lua b/luametalatex-back-pdf.lua index ea39983..7e03f8a 100644 --- a/luametalatex-back-pdf.lua +++ b/luametalatex-back-pdf.lua @@ -572,14 +572,16 @@ token.luacmd("pdfextension", function(_, imm) local actionobj = scan_action() action = pfile:indirect(nil, get_action_attr(pfile, actionobj)) end + local outline = get_outline() if token.scan_keyword'level' then local level = token.scan_int() local open = token.scan_keyword'open' - local outline = get_outline() local title = token.scan_string() outline:add(pdf_string(title), action, level, open, attr) 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 elseif token.scan_keyword"dest" then local id diff --git a/luametalatex-pdf-outline.lua b/luametalatex-pdf-outline.lua index 62b6f69..fe252c6 100644 --- a/luametalatex-pdf-outline.lua +++ b/luametalatex-pdf-outline.lua @@ -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 entry = {title = title, action = action, attr = attr, open = open, level = level} -- 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 parent[#parent + 1] = entry 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 objnum = pdf:getobj() outline.objnum = objnum @@ -94,6 +114,7 @@ end local meta = {__index = { write = write_outline, add = add_outline, + add_legacy = add_legacy, }} return function() return setmetatable({}, meta)