Hi Olivier,
Olivier wrote:
Btw: could you add in the doc a simple example of how to set up an mplib
instance
(loading the format file) and typesetting a minimal graphic, and then
use it from within plain TeX for instance; this is transparent to
conTeXt users since there are files like mlib-run.lua but not easy for
plain TeX users :P
Running mplib is easy enough to document, but that is only a part of
the work: you also have to convert the image to pdf literals, and
that is more work than I would like to put in the manual. Anyway,
attached is a bunch of definitions I stripped out of context that
will allow you to run mplib in plain tex provided that you also have
context installed (you need mlib-pdf.lua for the PDF conversion)
Best wishes,
Taco
metapost = (metapost or {})
function metapost.report (a)
print ("\n" .. a)
end
function metapost.finder (name, mode, ftype)
return (mode=="w" and name) or kpse.find_file(name, ftype)
end
function metapost.make(name, target, finder)
local status = 2 -- higher numbers are errors
finder = (finder or metapost.finder)
local mpx = mplib.new ( {
hash_size = 100000,
main_memory = 2000000,
max_in_open = 50,
param_size = 100000,
ini_version = true,
find_file = finder,
job_name = target,
} )
if mpx then
local result = mpx:execute("input " .. name .. "; dump;")
if result then status = result.status end
mpx:finish()
end
if status < 2 then
metapost.report('mp message: created mem file ' .. target )
else
metapost.report('mp error: mem file generation failed')
end
end
function metapost.load(name, finder)
finder = (finder or metapost.finder)
local memname = finder("mplib-" .. name,"r","mem")
if not memname then
memname = finder("mplib-" .. name, "w", "mem")
metapost.make(name, memname, finder)
end
metapost.report('mp message: using mem file ' .. memname)
local mpx = mplib.new ( {
ini_version = false,
mem_name = memname,
find_file = finder,
} )
if not mpx then
metapost.report('mp error: mem loading failed')
end
return mpx
end
function metapost.process(mpx, data)
local result
if mpx and data then
result = mpx:execute(data)
-- todo: error message
if not result then
metapost.report("mp error: no result object returned")
elseif result.status > 0 then
metapost.report("mp error:" .. (result.term or "no-term") .. "\n" ..
(result.error or "no-error"))
end
end
return result
end
\pdfoutput = 1
\pdfcompresslevel=0
\directlua0{ dofile ("mplib.lua") }
\directlua0{ dofile (kpse.find_file('mlib-pdf.lua')) }
% some things that are normally pre-defined in context
\newdimen\onebasepoint
\onebasepoint = 1bp
\newbox\scratchbox
{\catcode`\% =12 \gdef\letterpercent{%}}
\directlua0{ tex.ctxcatcodes = 0 }
% the three macros output by the context converter:
\def\startMPLIBtoPDF#1#2#3#4% watch the transparency reset
{\hbox\bgroup
\xdef\MPllx{#1}%
\xdef\MPlly{#2}%
\xdef\MPurx{#3}%
\xdef\MPury{#4}%
\xdef\MPwidth {\the\dimexpr#3\onebasepoint-#1\onebasepoint\relax}%
\xdef\MPheight{\the\dimexpr#4\onebasepoint-#2\onebasepoint\relax}%
\setbox\scratchbox\vbox\bgroup
\noindent % this is really needed in order to force tex into proper cm's
}
\let\MPLIBtoPDF\pdfliteral
\def\stopMPLIBtoPDF % watch the transparency reset
{\egroup
\setbox\scratchbox\hbox\bgroup
\hskip-\MPllx\onebasepoint
\raise-\MPlly\onebasepoint
\box\scratchbox
\egroup
\setbox\scratchbox\vbox to \MPheight\bgroup
\vfill
\hsize\MPwidth
\ht\scratchbox=0pt
\dp\scratchbox=0pt
\box\scratchbox
\egroup
\wd\scratchbox\MPwidth
\ht\scratchbox\MPheight
\box\scratchbox
\egroup }
% our own command
\def\mp#1{\directlua0{
if not mpx then mpx = metapost.load('plain') end
local f = metapost.process(mpx,'#1')
if f then
print (f.term)
if f.fig then
metapost.convert(f)
end
end
}}
\mp{beginfig(1); draw fullcircle scaled 20 withcolor (1,0,0); endfig;}
\bye
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the
Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage : http://www.pragma-ade.nl / http://tex.aanhet.net
archive : https://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___________________________________________________________________________________