diff --git a/luametalatex-back-pdf.lua b/luametalatex-back-pdf.lua index 5418785..0616823 100644 --- a/luametalatex-back-pdf.lua +++ b/luametalatex-back-pdf.lua @@ -1,4 +1,5 @@ local pdf = pdf +local pdfvariable = pdf.variable local writer = require'luametalatex-nodewriter' local newpdf = require'luametalatex-pdf' local nametree = require'luametalatex-pdf-nametree' @@ -35,9 +36,9 @@ local properties = node.direct.properties token.luacmd("shipout", function() local pfile = get_pfile() local voff = node.new'kern' - voff.kern = tex.voffset + pdf.variable.vorigin + voff.kern = tex.voffset + pdfvariable.vorigin voff.next = token.scan_list() - voff.next.shift = tex.hoffset + pdf.variable.horigin + voff.next.shift = tex.hoffset + pdfvariable.horigin local list = node.direct.tonode(node.direct.vpack(node.direct.todirect(voff))) list.height = tex.pageheight list.width = tex.pagewidth @@ -94,7 +95,7 @@ callback.register("stop_run", function() pfile:indirect(id, require'luametalatex-pdf-font'(pfile, f, sorted)) end pfile.root = pfile:getobj() - pfile.version = string.format("%i.%i", pdf.variable.majorversion, pdf.variable.minorversion) + pfile.version = string.format("%i.%i", pdfvariable.majorversion, pdfvariable.minorversion) local destnames = {} for k,obj in next, dests do if pfile:written(obj) then @@ -235,7 +236,7 @@ local function write_link(p, link) end local function addlinkpoint(p, link, x, y, list, kind) local quads = link.quads - local off = pdf.variable.linkmargin + local off = pdfvariable.linkmargin x = kind == 'start' and x-off or x+off if link.annots and link.annots ~= p.annots then -- We started on another page, let's finish that before starting the new page write_link(p, link) diff --git a/luametalatex-baseregisters.tex b/luametalatex-baseregisters.tex index 5b5b8d9..cd6a01c 100644 --- a/luametalatex-baseregisters.tex +++ b/luametalatex-baseregisters.tex @@ -46,7 +46,7 @@ end local pdf_params = {} pdf.variable_tokens = pdf_params - pdf.variable = setmetatable({}, { + setmetatable(pdf.variable, { __index = function(t, k) local v = pdf_params[k] if v then @@ -99,8 +99,8 @@ \pdfAlloc{dimen}{linkmargin}{0pt} \pdfAlloc{count}{majorversion}{1} \pdfAlloc{count}{minorversion}{7} -\pdfAlloc{count}{compresslevel}{0} % 0 is actually the only supported value right now, so this is basically ignored -\pdfAlloc{count}{objcompresslevel}{0} % see above +\pdfAlloc{count}{compresslevel}{0} +\pdfAlloc{count}{objcompresslevel}{0} % 0 is actually the only supported value right now, so this is basically ignored \pdfAlloc{toks}{pageresources}{{}} \texAlloc{count}{bodydirection}{0} diff --git a/luametalatex-init.lua b/luametalatex-init.lua index f663e17..a094390 100644 --- a/luametalatex-init.lua +++ b/luametalatex-init.lua @@ -45,6 +45,7 @@ pdf = { getfontname = function(id) -- No font sharing return id end, + variable = {}, } local olddefinefont = font.define function font.define(f) diff --git a/luametalatex-pdf.lua b/luametalatex-pdf.lua index 281f963..3741637 100644 --- a/luametalatex-pdf.lua +++ b/luametalatex-pdf.lua @@ -7,6 +7,8 @@ local pairs = pairs local setmetatable = setmetatable local assigned = {} local delayed = {} +local compress = xzip.compress +local pdfvariable = pdf.variable -- slightly tricky interface: No/nil return means that the objects content -- isn't known yet, while false indicates a delayed object. local function written(pdf, num) @@ -26,7 +28,16 @@ local function stream(pdf, num, dict, content, isfile, raw) content = f:read'a' f:close() end - pdf.file:write(format('%i 0 obj\n<<%s/Length %i>>stream\n', num, dict, #content)) + local level = not raw and pdfvariable.compresslevel or 0 + local filter = '' + if level > 0 then + local compressed = compress(content, level) + if #compressed < #content + 19 then -- Filter has some overhead + filter = "/Filter/FlateDecode" + content = compressed + end + end + pdf.file:write(format('%i 0 obj\n<<%s%s/Length %i>>stream\n', num, dict, filter, #content)) pdf.file:write(content) pdf.file:write'\nendstream\nendobj\n' return num