From b376473f1703ed8458cc5d58dd742dec09fba6f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Fabian=20Kr=C3=BCger?= Date: Sat, 13 Jun 2020 02:53:08 +0200 Subject: [PATCH] Image TeX interface, part I --- luametalatex-back-pdf.lua | 34 ++++++++++++++++++++++++++++++++++ luametalatex-pdf-image.lua | 12 ++++++++++++ 2 files changed, 46 insertions(+) diff --git a/luametalatex-back-pdf.lua b/luametalatex-back-pdf.lua index 2106dd9..0d6b2c5 100644 --- a/luametalatex-back-pdf.lua +++ b/luametalatex-back-pdf.lua @@ -159,6 +159,7 @@ end) local lastobj = -1 local lastannot = -1 +local lastimage = -1 function pdf.newcolorstack(default, mode, page) local idx = #colorstacks @@ -657,3 +658,36 @@ img = { write = function(img, pfile) return imglib_write(pfile or get_pfile(), img) end, immediatewrite = function(img, pfile) return imglib_immediatewrite(pfile or get_pfile(), img) end, } + +-- These are very minimal right now but LaTeX isn't using the scaling etc. stuff anyway. +token.luacmd("saveimageresource", function(imm) + local attr = token.scan_keyword'attr' and token.scan_string() or nil + local page = token.scan_keyword'page' and token.scan_int() or nil + local userpassword = token.scan_keyword'userpassword' and token.scan_string() or nil + local ownerpassword = token.scan_keyword'ownerpassword' and token.scan_string() or nil + -- local colorspace = token.scan_keyword'colorspace' and token.scan_int() or nil -- Doesn't make sense for PDF + local pagebox = token.scan_keyword'mediabox' and 'media' + or token.scan_keyword'cropbox' and 'crop' + or token.scan_keyword'bleedbox' and 'bleed' + or token.scan_keyword'trimbox' and 'trim' + or token.scan_keyword'artbox' and 'art' + or nil + local filename = token.scan_string() + local img = imglib.scan{ + attr = attr, + page = page, + userpassword = userpassword, + ownerpassword = ownerpassword, + pagebox = pagebox, + } + local pfile = get_pfile() + lastimage = imglib.get_num(pfile, img) + if imm == 'immediate' then + imglib_immediatewrite(pfile, img) + end +end, "protected") +token.luacmd("useimageresource", function() + local pfile = get_pfile() + local img = assert(imglib.from_num(token.scan_int())) + imglib_write(pfile, img) +end, "protected") diff --git a/luametalatex-pdf-image.lua b/luametalatex-pdf-image.lua index 19b0fe7..dd9bd57 100644 --- a/luametalatex-pdf-image.lua +++ b/luametalatex-pdf-image.lua @@ -102,6 +102,10 @@ local function write_pdf(pfile, img) else content = '' end + local attr = img.attr + if attr then + dict = dict .. attr + end pfile:stream(img.objnum, dict, content, nil, raw) end @@ -267,5 +271,13 @@ return { scan = scan, write = write, node = node, + from_num = function(i) + local img = {} + real_images[img] = assert(img_by_objnum[i]) + return setmetatable(img, restricted_meta) + end, + get_num = function(pfile, img) + return reserve(pfile, real_images[img]) + end, ship = do_img, }