More regular img library

This commit is contained in:
Marcel Krüger 2020-06-11 12:44:37 +02:00
parent 5c45cbc2e3
commit 39ed349241
2 changed files with 42 additions and 39 deletions

View File

@ -24,7 +24,6 @@ local function get_pfile()
end end
return pfile return pfile
end end
pdf.__get_pfile = get_pfile
local outline local outline
local function get_outline() local function get_outline()
if not outline then if not outline then
@ -647,4 +646,14 @@ token.luacmd("pdfextension", function(_, imm)
error(string.format("Unknown PDF extension %s", token.scan_word())) error(string.format("Unknown PDF extension %s", token.scan_word()))
end end
end, "protected") end, "protected")
img = require'luametalatex-pdf-image' imglib = require'luametalatex-pdf-image'
local imglib_node = imglib.node
local imglib_write = imglib.write
local imglib_immediatewrite = imglib.immediatewrite
img = {
new = imglib.new,
scan = imglib.scan,
node = function(img, pfile) return imglib_node(pfile or get_pfile(), img) end,
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,
}

View File

@ -4,8 +4,7 @@ local nodenew = node.direct.new
local getwhd = node.direct.getwhd local getwhd = node.direct.getwhd
local setwhd = node.direct.setwhd local setwhd = node.direct.setwhd
local tonode = node.direct.tonode local tonode = node.direct.tonode
local nodewrite = node.direct.write
local reserve
local box_fallback = { local box_fallback = {
BleedBox = "CropBox", BleedBox = "CropBox",
@ -77,7 +76,7 @@ local function scan_pdf(img)
end end
local pdfe_deepcopy = require'luametalatex-pdfe-deepcopy' local pdfe_deepcopy = require'luametalatex-pdfe-deepcopy'
local function write_pdf(img, pfile) local function write_pdf(pfile, img)
local file = open_pdfe(img) local file = open_pdfe(img)
local page = pdfe.getpage(file, img.page) local page = pdfe.getpage(file, img.page)
local bbox = img.bbox local bbox = img.bbox
@ -189,15 +188,14 @@ local function scan(img)
end end
local img_by_objnum = {} local img_by_objnum = {}
local function img_from_objnum(objnum, img) -- local function img_from_objnum(objnum, img)
img = img or {} -- img = img or {}
real_images[img] = assert(img_by_objnum[objnum]) -- real_images[img] = assert(img_by_objnum[objnum])
return setmetatable(img, restricted_meta) -- return setmetatable(img, restricted_meta)
end -- end
-- Noop if already reserved -- Noop if already reserved
function reserve(img, pfile) function reserve(pfile, real)
local real = assert(real_images[img])
local obj = real.objnum or pfile:getobj() local obj = real.objnum or pfile:getobj()
real.objnum = obj real.objnum = obj
img_by_objnum[obj] = real img_by_objnum[obj] = real
@ -205,24 +203,20 @@ function reserve(img, pfile)
end end
local function write_img(pfile, img) local function write_img(pfile, img)
local objnum = reserve(img, pfile) local objnum = reserve(pfile, img)
local real = real_images[img] if not img.written then
if not real.written then img.written = true
real.written = true write_pdf(pfile, img)
write_pdf(real, pfile)
end end
end end
local function do_img(data, p, n, x, y) local function do_img(data, p, n, x, y)
local img = {} local img = assert(img_by_objnum[data >> 3], 'Invalid image ID')
img_from_objnum(data >> 3, img)
-- scan(img)
write_img(p.file, img) write_img(p.file, img)
local real = real_images[img]
local mirror = data & 4 == 4 local mirror = data & 4 == 4
local rotate = (data + img.rotation) % 8 local rotate = (data + img.rotation) & 3
local width, height, depth = getwhd(n) local width, height, depth = getwhd(n)
height = height + depth height = height + depth
local bbox = real.bbox local bbox = img.bbox
local xsize, ysize = img.xsize, img.ysize local xsize, ysize = img.xsize, img.ysize
local a, b, c, d, e, f = 1, 0, 0, 1, -bbox[1], -bbox[2] local a, b, c, d, e, f = 1, 0, 0, 1, -bbox[1], -bbox[2]
if mirror then if mirror then
@ -238,35 +232,35 @@ local function do_img(data, p, n, x, y)
a, c, e = a*xscale, c*xscale, e*xscale a, c, e = a*xscale, c*xscale, e*xscale
b, d, f = b*yscale, d*yscale, f*yscale b, d, f = b*yscale, d*yscale, f*yscale
e, f = to_bp(x + e), to_bp(y - depth + f) e, f = to_bp(x + e), to_bp(y - depth + f)
p.resources.XObject['Im' .. tostring(real.objnum)] = real.objnum p.resources.XObject['Im' .. tostring(img.objnum)] = img.objnum
pdf.write('page', string.format('q %f %f %f %f %f %f cm /Im%i Do Q', a, b, c, d, e, f, real.objnum), nil, nil, p) pdf.write('page', string.format('q %f %f %f %f %f %f cm /Im%i Do Q', a, b, c, d, e, f, img.objnum), nil, nil, p)
end end
local ruleid = node.id'rule' local ruleid = node.id'rule'
local ruletypes = node.subtypes'rule' local ruletypes = node.subtypes'rule'
local imagerule local imagerule
for n, name in next, ruletypes do for n, name in next, ruletypes do
if name == 'image' then if name == 'image' then imagerule = n break end
imagerule = n
break
end
end end
assert(imagerule) assert(imagerule)
local function node(img, pfile) local function node(pfile, img)
pfile = pfile or pdf.__get_pfile() img = scan(img)
scan(img)
local n = nodenew(ruleid, imagerule) -- image local n = nodenew(ruleid, imagerule) -- image
setdata(n, (reserve(img, pfile) << 3) | ((img.transform or 0) & 7)) setdata(n, (reserve(pfile, real_images[img]) << 3) | ((img.transform or 0) & 7))
setwhd(n, img.width or -0x40000000, img.height or -0x40000000, img.depth or -0x40000000) setwhd(n, img.width or -0x40000000, img.height or -0x40000000, img.depth or -0x40000000)
return tonode(n) return tonode(n)
end end
--[[ local function write(pfile, img)
local function write(img, immediate, pfile) img = scan(img)
pfile = pfile or pdf.__get_pfile() nodewrite(node(pfile, img))
local _, objnum = reserve(img, pfile) return img
local real = real_images[img] end
local function immediatewrite(pfile, img)
img = scan(img)
write_img(pfile, real_images[img])
return img
end end
]]
return { return {
new = new, new = new,