start_file/stop_file

This commit is contained in:
Marcel Krüger 2020-07-15 04:51:59 +02:00
parent 46cada8666
commit 7bea04d470
10 changed files with 108 additions and 39 deletions

View File

@ -87,3 +87,27 @@ callback_register('handle_error_hook', function()
until false until false
return 3 return 3
end) end)
-- Now overwrite the callback functionality. Our system is based on the ssumption there there are
-- no unknown callback names, just callbacks very unlikely to ever be called. That doesn't lead to
-- good error checking, but we expect this to be overwritten by LaTeX anyway.
local callback_find = callback.find
local rawset = rawset
local callbacks = setmetatable({}, {
__index = function(cbs, name)
return callback_find(name)
end,
__newindex = function(cbs, name, new)
return callback_register(name, new) or rawset(cbs, name, new)
end,
})
function callback.register(name, new)
callbacks[name] = new
end
function callback.find(name)
return callbacks[name]
end
return callbacks

View File

@ -1,3 +1,5 @@
local readfile = require'luametalatex-readfile'
local white = (lpeg.S'\0\9\10\12\13\32' + '%' * (1 - lpeg.S'\r\n')^0)^1 local white = (lpeg.S'\0\9\10\12\13\32' + '%' * (1 - lpeg.S'\r\n')^0)^1
local regular = 1-lpeg.S'()<>[]{}/%\0\9\10\12\13\32' local regular = 1-lpeg.S'()<>[]{}/%\0\9\10\12\13\32'
local name = lpeg.C(regular^1) local name = lpeg.C(regular^1)
@ -5,8 +7,7 @@ local lname = '/' * name / 1
local namearray = lpeg.Ct('['*white^0*lpeg.Cg(lname*white^0, 0)^-1*(lname*white^0)^0*']') local namearray = lpeg.Ct('['*white^0*lpeg.Cg(lname*white^0, 0)^-1*(lname*white^0)^0*']')
local encfile = white^0*lname*white^0*namearray*white^0*'def'*white^0*-1 local encfile = white^0*lname*white^0*namearray*white^0*'def'*white^0*-1
return function(filename) return function(filename)
local file = io.open(filename, 'r') local file <close> = readfile('enc', filename, nil, 'r')
local name, encoding = encfile:match(file:read'a') local name, encoding = encfile:match(file())
file:close()
return encoding, name return encoding, name
end end

View File

@ -140,11 +140,9 @@ local function parse_maintable(offset, str)
return continue_maintable(offset, str, {}) return continue_maintable(offset, str, {})
end end
return function(filename) return function(data)
local file = io.open(filename, 'rb') local preface, private = string.unpack("<xxs4xxs4", data)
local preface, private = string.unpack("<xxs4xxs4", file:read'a')
private = decrypt(55665, 4, private) private = decrypt(55665, 4, private)
file:close()
local after = parse_maintable(1, preface .. private) local after = parse_maintable(1, preface .. private)
local lenIV = after.Private.lenIV or 4 local lenIV = after.Private.lenIV or 4
local chars = after.CharStrings local chars = after.CharStrings

View File

@ -1,3 +1,5 @@
local readfile = require'luametalatex-readfile'
local sfnt = require'luametalatex-font-sfnt' local sfnt = require'luametalatex-font-sfnt'
local stdStrings = require'luametalatex-font-cff-data' local stdStrings = require'luametalatex-font-cff-data'
local offsetfmt = ">I%i" local offsetfmt = ">I%i"
@ -576,15 +578,11 @@ function myfunc(buf, i0, fontid, usedcids, encoding, trust_widths)
end end
return require'luametalatex-font-cff'(top), bbox return require'luametalatex-font-cff'(top), bbox
end end
-- local file = io.open(arg[1])
-- local buf = file:read'a'
-- file:close()
-- io.open(arg[3], 'w'):write(myfunc(buf, 1, 1, nil, {{3}, {200}, {1000}, {1329}, {1330}, {1331}})):close()
return function(filename, fontid, encoding) return function(fontdir, usedcids) return function(filename, fontid, encoding) return function(fontdir, usedcids)
local file = io.open(filename, 'rb') local file <close> = readfile('subset', filename, nil)
local buf = file:read'a' local buf = file()
local i = 1 local i = 1
file:close()
local magic = buf:sub(1, 4) local magic = buf:sub(1, 4)
if magic == "ttcf" or magic == "OTTO" then if magic == "ttcf" or magic == "OTTO" then
-- assert(not encoding) -- nil or false -- assert(not encoding) -- nil or false
@ -598,4 +596,3 @@ return function(filename, fontid, encoding) return function(fontdir, usedcids)
fontdir.bbox = bbox fontdir.bbox = bbox
return content return content
end end end end
-- io.open(arg[3], 'w'):write(myfunc(buf, 1, 1, require'parseEnc'(arg[2]), {{string.byte'a'}, {string.byte'b'}, {string.byte'-'}})):close()

View File

@ -1,3 +1,5 @@
local readfile = require'luametalatex-readfile'
local purenumber = lpeg.R'09'^1 local purenumber = lpeg.R'09'^1
local optoperator = lpeg.C(lpeg.S'+-='^-1)*lpeg.C(lpeg.P(1)^0) local optoperator = lpeg.C(lpeg.S'+-='^-1)*lpeg.C(lpeg.P(1)^0)
local commentchar = lpeg.S' %*;#'+-1 local commentchar = lpeg.S' %*;#'+-1
@ -38,9 +40,8 @@ local function mapfile(filename, operator)
if not operator then if not operator then
operator, filename = optoperator:match(filename) operator, filename = optoperator:match(filename)
end end
local file = io.open(kpse.find_file(filename, 'map'), 'r') local file <close> = readfile('map', filename, 'map', 'r')
for line in file:lines() do mapline(line, operator) end for line in file:lines() do mapline(line, operator) end
file:close()
end end
local function reset() local function reset()
for k in next, fontmap do for k in next, fontmap do

View File

@ -1,11 +1,13 @@
local readfile = require'luametalatex-readfile'
-- Some helpers: -- Some helpers:
-- A kpse wrapper
local serialize_cff = require'luametalatex-font-cff' local serialize_cff = require'luametalatex-font-cff'
local serializet2 = require'luametalatex-font-t2' local serializet2 = require'luametalatex-font-t2'
local parseT1 = require'luametalatex-font-t1' local parseT1 = require'luametalatex-font-t1'
local t1tot2 = require'luametalatex-font-t1tot2' local t1tot2 = require'luametalatex-font-t1tot2'
return function(filename, reencode) return function(filename, reencode)
local parsed_t1 = parseT1(filename) local file <close> = readfile('subset', filename, nil)
local parsed_t1 = parseT1(file())
return function(f, usedcids) return function(f, usedcids)
f.bbox = parsed_t1.FontBBox f.bbox = parsed_t1.FontBBox
local fonttable = { local fonttable = {

View File

@ -1,3 +1,5 @@
local readfile = require'luametalatex-readfile'
local sfnt = require'luametalatex-font-sfnt' local sfnt = require'luametalatex-font-sfnt'
local stdnames = require'luametalatex-font-ttf-data' local stdnames = require'luametalatex-font-ttf-data'
local function round(x) local function round(x)
@ -15,7 +17,6 @@ local function addglyph(glyph, usedcids, cidtogid)
while offset do while offset do
local flags, component = string.unpack(">I2I2", glyph, offset) local flags, component = string.unpack(">I2I2", glyph, offset)
local gid = cidtogid[component] local gid = cidtogid[component]
print(glyph, component, gid)
if not gid then if not gid then
gid = #usedcids gid = #usedcids
usedcids[gid+1] = {component} usedcids[gid+1] = {component}
@ -68,9 +69,8 @@ local function readpostnames(buf, i, usedcids, encoding)
return newusedcids return newusedcids
end end
return function(filename, fontid, reencode) return function(filename, fontid, reencode)
local file = io.open(filename, 'rb') local file <close> = readfile('subset', filename, nil)
local buf = file:read'a' local buf = file()
file:close()
local magic, tables = sfnt.parse(buf, 1, fontid) local magic, tables = sfnt.parse(buf, 1, fontid)
if magic ~= "\0\1\0\0" then error[[Invalid TTF font]] end if magic ~= "\0\1\0\0" then error[[Invalid TTF font]] end
-- TODO: Parse post table and add reencoding support -- TODO: Parse post table and add reencoding support

View File

@ -1,3 +1,4 @@
local readfile = require'luametalatex-readfile'
local strip_floats = require'luametalatex-pdf-utils'.strip_floats local strip_floats = require'luametalatex-pdf-utils'.strip_floats
local function ignore() end local function ignore() end
@ -204,12 +205,11 @@ end
local png_functions = {} local png_functions = {}
function png_functions.scan(img) function png_functions.scan(img)
local file = io.open(img.filepath, 'rb') local file <close> = readfile('image', img.filepath, nil)
if not file then if not file then
error[[PDF image could not be opened.]] error[[PDF image could not be opened.]]
end end
local buf = file:read'a' local buf = file()
file:close()
local t = run(buf, 1, #buf, 'IDAT') local t = run(buf, 1, #buf, 'IDAT')
img.pages = 1 img.pages = 1
img.page = 1 img.page = 1
@ -228,9 +228,8 @@ local intents = {[0]=
} }
local function srgb_lookup(pfile, intent) local function srgb_lookup(pfile, intent)
if not srgb_colorspace then if not srgb_colorspace then
local f = io.open(kpse.find_file'sRGB.icc.zlib', 'rb') local file <close> = readfile('silent', 'sRGB.icc.zlib', 'other binary files')
local profile = f:read'a' local profile = file()
f:close()
local objnum = pfile:stream(nil, '/Filter/FlateDecode/N ' .. tostring(colortype & 2 == 2 and '3' or '1'), t.iCCP, nil, true) local objnum = pfile:stream(nil, '/Filter/FlateDecode/N ' .. tostring(colortype & 2 == 2 and '3' or '1'), t.iCCP, nil, true)
srgb_colorspace = string.format('[/ICCBased %i 0 R]', objnum) srgb_colorspace = string.format('[/ICCBased %i 0 R]', objnum)
end end
@ -259,12 +258,11 @@ local function rawimage(t, content)
end end
function png_functions.write(pfile, img) function png_functions.write(pfile, img)
local file = io.open(img.filepath, 'rb') local file <close> = readfile('silent', img.filepath, nil)
if not file then if not file then
error[[PDF image could not be opened.]] error[[PDF image could not be opened.]]
end end
local buf = file:read'a' local buf = file()
file:close()
local t = run(buf, 1, #buf, 'IEND') local t = run(buf, 1, #buf, 'IEND')
local colorspace local colorspace
local intent = '' local intent = ''

View File

@ -1,3 +1,5 @@
local readfile = require'luametalatex-readfile'
local format = string.format local format = string.format
local byte = string.byte local byte = string.byte
local pack = string.pack local pack = string.pack
@ -23,9 +25,8 @@ local function stream(pdf, num, dict, content, isfile, raw)
end end
pdf[num] = {offset = pdf.file:seek()} pdf[num] = {offset = pdf.file:seek()}
if isfile then if isfile then
local f = io.open(content, 'rb') local file <close> = readfile('pdf_stream', content, nil)
content = f:read'a' content = file()
f:close()
end end
local level = not raw and pdfvariable.compresslevel or 0 local level = not raw and pdfvariable.compresslevel or 0
local filter = '' local filter = ''
@ -56,9 +57,8 @@ local function indirect(pdf, num, content, isfile, objstream)
error[[Invalid object]] error[[Invalid object]]
end end
if isfile then if isfile then
local f = io.open(content, 'rb') local file <close> = readfile('pdf_dict', content, nil)
content = f:read'a' content = file()
f:close()
end end
if objstream ~= false and pdfvariable.objcompresslevel ~= 0 then if objstream ~= false and pdfvariable.objcompresslevel ~= 0 then
objstream = objstream or true objstream = objstream or true

48
luametalatex-readfile.lua Normal file
View File

@ -0,0 +1,48 @@
local io_open = io.open
local write = texio.write
local find_file = kpse.find_file
local callbacks = require'luametalatex-callbacks'
local categories = { data = 1, map = 2, image = 3, subset = 4, font = 5, enc = 6, pdf_stream = 7, pdf_stream = 8, silent = 9}
local start_categories = { [0] = '?', '(', '{', '<', '<', '<<' }
local stop_categories = { [0] = '?', ')', '}', '>', '>', '>>' }
local function stop_file(t)
local cb = callbacks.stop_file
if cb then
cb(t.category)
else
write(stop_categories[t.category] or '')
end
t.file:close()
end
local meta = {
__close = stop_file,
__call = function(t) return t.file:read'a' end,
close = stop_file,
lines = function(t, ...) return t.file:lines(...) end,
}
meta.__index = meta
return function(category, name, kpse, mode)
category = tonumber(category) or categories[category] or 0
if kpse then
name = find_file(name, kpse)
end
if not name then return name end
local f, msg = io_open(name, mode or 'rb')
if f then
local cb = callbacks.start_file
if cb then
cb(category, name)
else
local start_mark = start_categories[category]
if start_mark then
write(start_mark .. name)
end
end
end
return f and setmetatable({category = category, file = f}, meta), msg
end