Compare commits

...

2 Commits

4 changed files with 76 additions and 27 deletions

View File

@ -3,12 +3,11 @@ local newpdf = require'luametalatex-pdf'
local pfile = newpdf.open(tex.jobname .. '.pdf')
local fontdirs = setmetatable({}, {__index=function(t, k)t[k] = pfile:getobj() return t[k] end})
local usedglyphs = {}
print(token, token.luacmd)
token.luacmd("shipout", function()
local voff = node.new'kern'
voff.kern = tex.voffset + tex.sp'1in'
voff.kern = tex.voffset + pdf.variable.horigin
voff.next = token.scan_list()
voff.next.shift = tex.hoffset + tex.sp'1in'
voff.next.shift = tex.hoffset + pdf.variable.vorigin
local list = node.vpack(voff)
list.height = tex.pageheight
list.width = tex.pagewidth
@ -34,3 +33,14 @@ callback.register("stop_run", function()
pfile:indirect(pfile.root, string.format([[<</Type/Catalog/Version/1.7/Pages %i 0 R>>]], pfile:writepages()))
pfile:close()
end, "Finish PDF file")
token.luacmd("pdfvariable", function()
for n, t in pairs(pdf.variable_tokens) do
if token.scan_keyword(n) then
token.put_next(t)
return
end
end
-- The following error message gobbles the next word as a side effect.
-- This is intentional to make error-recovery easier.
error(string.format("Unknown PDF variable %s", token.scan_string()))
end)

View File

@ -2,6 +2,11 @@
\catcode`\^^^^ffff=11
\catcode`\@=11
\toks0{%
do
local function frozen(s)
local t = token.create(s)
return token.new(t.mode, t.command)
end
local dimen_cmd = token.command_id'assign_dimen'
local tex_params = {}
local texmeta = getmetatable(tex)
@ -27,11 +32,35 @@
return texmetaoldnewindex(t, k, v)
end
end
local pdf_params = {}
pdf.variable_tokens = pdf_params
pdf.variable = setmetatable({}, {
__index = function(t, k)
local v = pdf_params[k]
if v then
if v.command == dimen_cmd then
return tex.dimen[v.index]
end
else
return texmetaoldindex(t, k)
end
end,
__newindex = function(t, k, v)
local p = pdf_params[k]
if p then
if p.command == dimen_cmd then
tex.dimen[p.index] = v
end
else
return texmetaoldnewindex(t, k, v)
end
end,
})
}
\def\InternalAlloc#1#2#3#4#5{%
\csname new#3\endcsname#1%
\global#1=#5\relax
\etoksapp0{#2_params["\luaescapestring{#4}"] = token.create"\luaescapestring{\csstring#1}"}
\etoksapp0{#2_params["\luaescapestring{#4}"] = frozen"\luaescapestring{\csstring#1}"}
}
\def\internalAlloc#1#2#3{%
\expandafter\InternalAlloc\csname ^^^^fffe#3@#1\endcsname{#1}{#2}{#3}%
@ -39,11 +68,16 @@
\def\texAlloc#1#2{%
\expandafter\InternalAlloc\csname #2\endcsname{tex}{#1}{#2}%
}
\def\pdfAlloc{%
\internalAlloc{pdf}%
}
\texAlloc{dimen}{pageheight}{11in}
\texAlloc{dimen}{pagewidth}{8.5in}
% \internalAlloc{tex}{dimen}{pagewidth}
\pdfAlloc{dimen}{horigin}{1in}
\pdfAlloc{dimen}{vorigin}{1in}
\directlua{
lua.prepared_code[\csstring#lua.prepared_code+1] = tex.toks[0]
lua.prepared_code[\csstring#lua.prepared_code+1] = tex.toks[0] .. "end"
\the\toks0
end
}
\endgroup

View File

@ -4,10 +4,10 @@ local functions = lua.getfunctionstable()
function lua.get_functions_table() return functions end
local set_lua = token.set_lua
-- local new_luafunction = luatexbase.new_luafunction
local i=12342
local predefined_luafunctions = 0
local function new_luafunction(name)
i = i+1
return i
predefined_luafunctions = predefined_luafunctions + 1
return predefined_luafunctions
end
function token.luacmd(name, func, ...)
local idx = new_luafunction(name)
@ -101,7 +101,7 @@ local function do_openout(p)
end
end
end
functions[39] = function(_, immediate) -- \openout
token.luacmd("openout", function(_, immediate) -- \openout
local file = token.scan_int()
token.scan_keyword'='
local name = scan_filename()
@ -113,14 +113,14 @@ functions[39] = function(_, immediate) -- \openout
properties[whatsit] = props
node.write(whatsit)
end
end
end, "protected")
local function do_closeout(p)
if ofiles[p.file] then
ofiles[p.file]:close()
ofiles[p.file] = nil
end
end
functions[40] = function(_, immediate) -- \closeout
token.luacmd("closeout", function(_, immediate) -- \closeout
local file = token.scan_int()
local props = {file = file, handle = do_closeout}
if immediate == "immediate" then
@ -130,7 +130,7 @@ functions[40] = function(_, immediate) -- \closeout
properties[whatsit] = props
node.write(whatsit)
end
end
end, "protected")
local function do_write(p)
local content = token.to_string(p.data) .. '\n'
local file = ofiles[p.file]
@ -140,7 +140,7 @@ local function do_write(p)
texio.write_nl(p.file < 0 and "log" or "term and log", content)
end
end
functions[41] = function(_, immediate) -- \write
token.luacmd("write", function(_, immediate) -- \write
local file = token.scan_int()
local content = token.scan_tokenlist()
local props = {file = file, data = content, handle = do_write}
@ -151,29 +151,31 @@ functions[41] = function(_, immediate) -- \write
properties[whatsit] = props
node.write(whatsit)
end
end
end, "protected")
local lua_call_cmd = token.command_id'lua_call'
functions[42] = function() -- \immediate
token.luacmd("immediate", function() -- \immediate
local next_tok = token.scan_token()
if next_tok.command ~= lua_call_cmd then
return token.put_next(next_tok)
end
local function_id = next_tok.index
functions[function_id](function_id, 'immediate')
end
functions[43] = function() -- \pdfvariable
local name = token.scan_string()
print('Missing \\pdf' .. name)
end
token.set_lua("openout", 39, "protected")
token.set_lua("closeout", 40, "protected")
token.set_lua("write", 41, "protected")
write_tok = token.create'write'
token.set_lua("immediate", 42, "protected")
token.set_lua("pdfvariable", 43)
end, "protected")
-- functions[43] = function() -- \pdfvariable
-- local name = token.scan_string()
-- print('Missing \\pdf' .. name)
-- end
local prepared_code = lua.bytecode[1]
if prepared_code then
prepared_code()
prepared_code, lua.bytecode[1] = nil, nil
function fixupluafunctions()
new_luafunction = luatexbase.new_luafunction
fixupluafunctions = nil
end
else
function fixupluafunctions()
tex.setcount("global", "e@alloc@luafunction@count", predefined_luafunctions)
end
end
require'luametalatex-back-pdf'

View File

@ -30,5 +30,8 @@
\input luametalatex-baseregisters
\let\dump\savedversionofdump
\let\savedversionofdump\undefined
\directlua{fixupluafunctions()}%
\everyjob\expandafter{\the\everyjob\directlua{fixupluafunctions()}}%
\directlua{fixupluafunctions()}%
\dump
\endinput