diff options
Diffstat (limited to 'Master/texmf-dist/source/luatex/luamplib/luamplib.dtx')
-rw-r--r-- | Master/texmf-dist/source/luatex/luamplib/luamplib.dtx | 466 |
1 files changed, 142 insertions, 324 deletions
diff --git a/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx b/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx index ed2680ef509..d6aa0c705d2 100644 --- a/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx +++ b/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx @@ -1,29 +1,22 @@ -% \iffalse meta-comment +% \iffalse meta-comment -- by the way, this file contains UTF-8 % -% Copyright (C) 2008-2009 by Hans Hagen, Taco Hoekwater and Elie Roux -% <elie.roux@telecom-bretagne.eu>. (Support: <lualatex-dev@tug.org>.) +% Copyright (C) 2008-2010 by Hans Hagen, Taco Hoekwater, Elie Roux, and +% Manuel Pégourié-Gonnard. +% Currently maintained by Manuel Pégourié-Gonnard <mpg@elzevir.fr> +% Support: <lualatex-dev@tug.org> % % This work is under the CC0 license. % -% This Current Maintainer of this work is Elie Roux. -% % This work consists of the main source file luamplib.dtx % and the derived files -% luamplib.sty, luamplib.lua, luamplib-createmem.lua and luamplib.pdf. -% +% luamplib.sty, luamplib.lua and luamplib.pdf. % % Unpacking: % tex luamplib.dtx -% +% % Documentation: % pdflatex luamplib.dtx % -% The class ltxdoc loads the configuration file ltxdoc.cfg -% if available. Here you can specify further options, e.g. -% use A4 as paper format: -% \PassOptionsToClass{a4paper}{article} -% -% %<*ignore> \begingroup \def\x{LaTeX2e}% @@ -46,18 +39,8 @@ \let\MetaPrefix\relax \preamble -This is a generated file. -Copyright (C) 2008-2009 by Hans Hagen, Taco Hoekwater and Elie Roux -<elie.roux@telecom-bretagne.eu>. (Support: <lualatex-dev@tug.org>.) - -This work is under the CC0 license. - -This Current Maintainer of this work is Elie Roux. - -This work consists of the main source file luamplib.dtx -and the derived files - luamplib.sty, luamplib.lua, luamplib-createmem.lua and luamplib.pdf. +See source file '\inFileName' for licencing and contact information. \endpreamble @@ -68,9 +51,6 @@ and the derived files \file{luamplib.sty}{\from{luamplib.dtx}{package}}% } -% The following hacks are to generate a lua file with lua comments starting by -% -- instead of %% - \def\MetaPrefix{-- } \def\luapostamble{% @@ -83,7 +63,6 @@ and the derived files \generate{% \usedir{tex/luatex/mplib}% \file{luamplib.lua}{\from{luamplib.dtx}{lua}}% - \file{luamplib-createmem.lua}{\from{luamplib.dtx}{gen-lua}}% } \obeyspaces @@ -104,19 +83,29 @@ and the derived files \fi %</ignore> %<*driver> -\NeedsTeXFormat{LaTeX2e} -\ProvidesFile{luamplib.drv}% - [2010/10/17 v1.06 mplib package for LuaTeX]% \documentclass{ltxdoc} -\usepackage{hyperref} -\newcommand*\email[1]{\href{mailto:#1}{#1}} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{textcomp} +\usepackage{lmodern} +\usepackage{fixltx2e} + +\usepackage[a4paper]{geometry} +\usepackage[english]{babel} +\usepackage[colorlinks]{hyperref} +\usepackage{bookmark} + +\newcommand*\email [1] {<\href{mailto:#1}{#1}>} +\newcommand \file {\nolinkurl} +\newcommand \pk {\textsf} + \begin{document} \DocInput{luamplib.dtx}% \end{document} %</driver> % \fi % -% \CheckSum{2} +% \CheckSum{0} % % \CharacterTable % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z @@ -135,9 +124,10 @@ and the derived files % Right brace \} Tilde \~} % % \title{The \textsf{luamplib} package} -% \date{2010/10/17 v1.06} % \author{Hans Hagen, Taco Hoekwater and Elie Roux \\ +% Maintainer: Manuel Pégourié-Gonnard --- % Support: \email{lualatex-dev@tug.org}} +% \date{2010/12/28 v1.07} % % \maketitle % @@ -153,7 +143,7 @@ and the derived files % wrapper (in Lua) for the Lua \texttt{mplib} functions and some \TeX\ % functions to have the output of the \texttt{mplib} functions in the pdf. % -% The package need to be in PDF mode in order to output something, as PDF +% The package needs to be in PDF mode in order to output something, as PDF % specials are not supported by the DVI format and tools. % % The metapost figures are put in a \TeX\ \texttt{hbox} with dimensions @@ -166,248 +156,153 @@ and the derived files % \begin{itemize} % \item a \LaTeX\ environment % \item all \TeX\ macros start by \texttt{mplib} -% \item use of the luatexbase printing and module functions -% \item adapted warning, error and log messages +% \item use of luatexbase for errors, warnings and declaration % \end{itemize} % % Using this package is easy: in Plain, type your metapost code between the % macros \texttt{mplibcode} and \texttt{endmplibcode}, and in \LaTeX\ in the % \texttt{mplibcode} environment. % -% In order to use metapost, some \texttt{.mem} files are needed. These files -% must be generated with the same version of \texttt{mplib} as the version of -% Lua\TeX . These files names can be changed, they are by default -% \texttt{mpost.mem} and \texttt{mpfun.mem}. If this package is -% to be included in a distribution, some values may have to be changed in the -% file \texttt{luamplib.lua}, see comments. -% -% If your distribution does not provide valid \texttt{.mem} files (\TeX Live -% 2009 will be the first), you'll have to generate and install them by hand, -% with the script \texttt{luamplib-createmem.lua} included in this package -% if you want them. +% There are (basically) two formats for metapost: \emph{plain} and +% \emph{mpfun}. By default, the \emph{plain} format is used, but you can set +% the format to be used by future figures at any time using +% \cs{mplibsetformat}\marg{format name}. % -% These .mem files are not mandatory though. If this package doesn't find the -% mem files, it will just input the \texttt{.mp} file, and work without problem; -% the only difference is that it may be a bit slower but this is not noticeable -% on a modern computer. +% \section{Implementation} % -% \section{Files} -% -% This package contains three files: -% \begin{itemize} -% \item \texttt{luamplib.lua} containing the lua code that calls mplib -% \item \texttt{luamplib.sty} containing the macros for \LaTeX\ and Plain -% \item \texttt{create-mem.lua}, a standalone mem generation script -% \end{itemize} +% \subsection{Lua module} % -% \iffalse +% \begin{macrocode} %<*lua> -% \fi -% -% \subsection{\texttt{luamplib.lua}} +% \end{macrocode} % -% First the \texttt{luamplib} module is registered as a Lua\TeX\ module, -% with some informations. Here we can't name it \texttt{mplib}, as the name -% is already taken. +% Use the |luamplib| namespace, since |mplib| is for the metapost library +% itself. % % \begin{macrocode} - module('luamplib', package.seeall) - -luamplib.module = { +% \end{macrocode} +% +% Identification, and additional reporting function, for compatibility with +% the original |report()| function and associated data structure. +% +% \begin{macrocode} +local err, warn, info, log = luatexbase.provides_module({ name = "luamplib", - version = 1.06, - date = "2010/10/17", + version = 1.07, + date = "2010/12/28", description = "Lua package to typeset Metapost with LuaTeX's MPLib.", - author = "Hans Hagen, Taco Hoekwater & Elie Roux", - copyright = "ConTeXt Development Team & Elie Roux", - license = "CC0", -} +}) -luatexbase.provides_module(luamplib.module) +local function term(...) + texio.write_nl('term', 'luamplib: ' .. string.format(...)) +end % \end{macrocode} +% % This module is a stripped down version of libraries that are used by -% Con\TeX t. +% Con\TeX t. Provide a few ``shortcuts'' expected by the imported code. +% % \begin{macrocode} - local format, concat, abs = string.format, table.concat, math.abs % \end{macrocode} -% -% The \texttt{mem} file and the format name are hardcoded, and they can be -% set with \TeX\ if it's useful. The \TeX\ distributions should change -% these values if necessary. +% +% This is a small trick for \LaTeX . In \LaTeX\ we read the metapost code +% line by line, but it needs to be passed entirely to |process()|, so we +% simply add the lines in |data| and at the end we call |process(data)|. % % \begin{macrocode} +local data = "" -luamplib.currentformat = "plain" -luamplib.currentmem = "mplib-luatex" - -local currentformat = luamplib.currentformat -local currentmem = luamplib.currentmem - -function luamplib.setformat (name) - luamplib.currentformat = name +local function resetdata() + data = "" end - -function luamplib.setmemfile(name) - luamplib.currentmem = name +function addline(line) + data = data .. '\n' .. line +end +function processlines() + process(data) + resetdata() end % \end{macrocode} % -% We use the \texttt{kpse} library and make it behave like when used -% with Metapost. To find the .mem files with kpse, we have to make a -% small hack... that might be a little bug. +% As the finder function for |mplib|, use the |kpse| library and +% make it behave like as if MetaPost was used (or almost, since the engine +% name is not set this way---not sure if this is a problem). % % \begin{macrocode} +local mpkpse = kpse.new("luatex", "mpost") -local mpkpse = kpse.new("luatex","mpost") - -function luamplib.finder(name, mode, ftype) +local function finder(name, mode, ftype) if mode == "w" then return name else - local result = mpkpse:find_file(name,ftype) - if not result and ftype == "mem" then - result = mpkpse:find_file("metapost/"..name,ftype) - end - return result + return mpkpse:find_file(name,ftype) end end -function luamplib.info (...) - luatexbase.module_info('luamplib', format(...)) -end - -function luamplib.log (...) - luatexbase.module_log('luamplib', format(...)) -end - -function luamplib.term (...) - luatexbase.module_term('luamplib', format(...)) -end - -function luamplib.warning (...) - luatexbase.module_warning('luamplib', format(...)) -end - -function luamplib.error (...) - luatexbase.module_error('luamplib', format(...)) -end - % \end{macrocode} % -% This is a small hack for \LaTeX . In \LaTeX\ we read the metapost code -% line by line, but it needs to be passed entirely to -% \texttt{luamplib.process}, so we simply add the lines in -% \texttt{luamplib.data} and at the end we call \texttt{luamplib.process} -% on \texttt{luamplib.data}. +% Default format name, and a public function to change it. % % \begin{macrocode} +local currentformat = "plain" -luamplib.data = "" - -function luamplib.resetdata() - luamplib.data = "" -end - -function luamplib.addline(line) - luamplib.data = luamplib.data .. '\n' .. line -end - -function luamplib.processlines() - luamplib.process(luamplib.data) - luamplib.resetdata() +function setformat (name) + currentformat = name end % \end{macrocode} % -% -% \begin{macro}{luamplib.input mp} -% -% This function creates a new mplib object and inputs the -% good .mp file. It's less efficient than loading the .mem -% file, but it works more safely, as the .mp file does not -% depend on the version of the mpost program. +% Create a new mplib object and input the correct .mp file as given by +% |currentformat|. With older versions of MetaPost, using a |mem| file may +% be more efficient, but newer versions don't support it any more so for +% the sake of simplicity, don't even try to use a |mem| file. +% |ini_version| is ignored by new versions but is useful to prevent old +% ones from trying to load a |mem| file. % % \begin{macrocode} - -function luamplib.input_mp(name) +function load() local mpx = mplib.new { + find_file = finder, ini_version = true, - find_file = luamplib.finder, - job_name = name, - } - mpx:execute(format("input %s ;",name)) - return mpx -end - -% \end{macrocode} -% -% \end{macro} -% -% \begin{macro}{luamplib.load} -% -% This function is the one loading the metapost format we want. It uses the -% \texttt{luamplib.currentformat} and \texttt{luamplib.currentmem} to -% determine the format and the mem file to use. -% -% The rest of this module is not documented. More info can be found in the -% Lua\TeX\ manual, articles in user group journals and the files that ship -% with Con\TeX t. -% -% \begin{macrocode} - -function luamplib.load() - local mpx = mplib.new { - ini_version = false, - mem_name = currentmem, - find_file = luamplib.finder } - if mpx then - luamplib.log("using mem file %s", luamplib.finder(currentmem, 'r', 'mem')) - else - mpx = luamplib.input_mp(currentformat) - if mpx then - luamplib.log("using mp file %s", luamplib.finder(currentformat, 'r', 'mp')) - else - luamplib.error("unable to load the metapost format.") - end - end + mpx:execute(format("input %s ;", currentformat)) return mpx end % \end{macrocode} % -% \end{macro} +% The rest of this module is not documented. More information can be found +% in the Lua\TeX\ manual, articles in user group journals and the files +% that ship with Con\TeX t. % % \begin{macrocode} - -function luamplib.report(result) +function report(result) if not result then - luamplib.error("no result object") + err("no result object") elseif result.status > 0 then local t, e, l, f = result.term, result.error, result.log if l then - luamplib.log(l) + log(l) end if t then - luamplib.term(t) + term(t) end if e then if result.status == 1 then - luamplib.warning(e) + warn(e) else - luamplib.error(e) + err(e) end end if not t and not e and not l then if result.status == 1 then - luamplib.warning("unknown error, no error, terminal or log messages, maybe missing beginfig/endfig") + warn("unknown error, no error, terminal or log messages, maybe missing beginfig/endfig") else - luamplib.error("unknown error, no error, terminal or log messages, maybe missing beginfig/endfig") + err("unknown error, no error, terminal or log messages, maybe missing beginfig/endfig") end end else @@ -416,20 +311,20 @@ function luamplib.report(result) return false end -function luamplib.process(data) +function process(data) local converted, result = false, {} - local mpx = luamplib.load() + local mpx = load() if mpx and data then local result = mpx:execute(data) - if luamplib.report(result) then + if report(result) then if result.fig then - converted = luamplib.convert(result) + converted = convert(result) else - luamplib.warning("no figure output") + warn("no figure output") end end else - luamplib.error("Mem file unloadable. Maybe generated with a different version of mplib?") + err("Mem file unloadable. Maybe generated with a different version of mplib?") end return converted, result end @@ -438,8 +333,8 @@ local function getobjects(result,figure,f) return figure:objects() end -function luamplib.convert(result, flusher) - luamplib.flush(result, flusher) +function convert(result, flusher) + flush(result, flusher) return true -- done end @@ -557,13 +452,12 @@ end % Support for specials in DVI has been removed. % % \begin{macrocode} - -function luamplib.flush(result,flusher) +function flush(result,flusher) if result then local figures = result.fig if figures then for f=1, #figures do - luamplib.log("flushing figure %s",f) + log("flushing figure %s",f) local figure = figures[f] local objects = getobjects(result,figure,f) local fignum = tonumber((figure:filename()):match("([%d]+)$") or figure:charcode() or 0) @@ -600,7 +494,7 @@ function luamplib.flush(result,flusher) else local cs = object.color if cs and #cs > 0 then - pdf_literalcode(luamplib.colorconverter(cs)) + pdf_literalcode(colorconverter(cs)) end local ml = object.miterlimit if ml and ml ~= miterlimit then @@ -698,7 +592,7 @@ function luamplib.flush(result,flusher) end end -function luamplib.colorconverter(cr) +function colorconverter(cr) local n = #cr if n == 4 then local c, m, y, k = cr[1], cr[2], cr[3], cr[4] @@ -713,63 +607,48 @@ function luamplib.colorconverter(cr) end % \end{macrocode} % -% \iffalse +% \begin{macrocode} %</lua> -% \fi +% \end{macrocode} % -% \subsection{\texttt{luamplib.sty}} +% \subsection{\texorpdfstring{\TeX}{TeX} package} % -% \iffalse +% \begin{macrocode} %<*package> -% \fi -% +% \end{macrocode} % % First we need to load fancyvrb, to define the environment mplibcode. % % \begin{macrocode} - \bgroup\expandafter\expandafter\expandafter\egroup \expandafter\ifx\csname ProvidesPackage\endcsname\relax \input luatexbase-modutils.sty \else \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{luamplib} - [2010/10/17 v1.06 mplib package for LuaTeX] + [2010/12/28 v1.07 mplib package for LuaTeX] \RequirePackage{luatexbase-modutils} \RequirePackage{fancyvrb} \fi - % \end{macrocode} -% Loading of lua code. +% +% Loading of lua code. +% % \begin{macrocode} - \RequireLuaModule{luamplib} - % \end{macrocode} % -% -% -% There are (basically) two formats for metapost: \emph{plain} and -% \emph{mpfun}. The corresponding \texttt{.mem} files are -% \texttt{mplib-luatex.mem} in \TeX Live 2010. -% With these functions you can set the format and the mem files that will -% be used by this package. Warning: the package never generates the mem -% files, you have to do it by hand, with \texttt{create-mem.lua}. +% Set the format for metapost. % % \begin{macrocode} - -\def\mplibsetformat#1{\directlua{luamplib.setformat([[#1]])}} - -\def\mplibsetmemfile#1{\directlua{luamplib.setmemfile([[#1]])}} - +\def\mplibsetformat#1{% + \directlua{luamplib.setformat("\luatexluaescapestring{#1}")}} % \end{macrocode} % -% % MPLib only works in PDF mode, we don't do anything if we are in DVI mode, % and we output a warning. % % \begin{macrocode} - \ifnum\pdfoutput>0 \let\mplibtoPDF\pdfliteral \else @@ -783,43 +662,35 @@ end \PackageWarning{mplib}{MPLib only works in PDF mode, no figure will be output.} \fi \fi - % \end{macrocode} % % The Plain-specific stuff. % % \begin{macrocode} - \bgroup\expandafter\expandafter\expandafter\egroup \expandafter\ifx\csname ProvidesPackage\endcsname\relax - \def\mplibsetupcatcodes{ \catcode`\{=12 \catcode`\}=12 \catcode`\#=12 \catcode`\^=12 \catcode`\~=12 - \catcode`\_=12 \catcode`\%=12 \catcode`\&=12 \catcode`\$=12 + \catcode`\_=12 \catcode`\%=12 \catcode`\&=12 \catcode`\$=12 } - \def\mplibcode{% \bgroup % \mplibsetupcatcodes % \mplibdocode % } - \long\def\mplibdocode#1\endmplibcode{% \egroup % \mplibprocess{#1}% } - \long\def\mplibprocess#1{% - \directlua{luamplib.process([[#1]])}% + \directlua{luamplib.process("\luatexluaescapestring{#1}")}% } - \else - % \end{macrocode} +% % The \LaTeX-specific parts. First a Hack for the catcodes in \LaTeX . +% % \begin{macrocode} - -\makeatletter \begingroup \catcode`\,=13 \catcode`\-=13 @@ -828,28 +699,26 @@ end \def-{\string-}% } \endgroup - % \end{macrocode} +% % In \LaTeX\ (it's not the case in plain\TeX ), we get the metapost code % line by line, here is the function handling a line. +% % \begin{macrocode} - \newcommand\mplibaddlines[1]{% \begingroup % \FV@hack % \def\FV@ProcessLine##1{% - \directlua{luamplib.addline([[##1]])}% + \directlua{luamplib.addline("\luatexluaescapestring{##1}")}% }% \csname FV@SV@#1\endcsname % \endgroup % } - -\makeatother - % \end{macrocode} +% % The \LaTeX\ environment is a modified \texttt{verbatim} environment. +% % \begin{macrocode} - \newenvironment{mplibcode}{% \VerbatimEnvironment % \begin{SaveVerbatim}{memoire}% @@ -860,17 +729,17 @@ end } \fi - % \end{macrocode} +% % We use a dedicated scratchbox. +% % \begin{macrocode} - \ifx\mplibscratchbox\undefined \newbox\mplibscratchbox \fi - % \end{macrocode} +% % We encapsulate the litterals. +% % \begin{macrocode} - \def\mplibstarttoPDF#1#2#3#4{ \hbox\bgroup \xdef\MPllx{#1}\xdef\MPlly{#2}% @@ -884,7 +753,9 @@ end \setbox\mplibscratchbox\vbox\bgroup \noindent } - +% \end{macrocode} +% +% \begin{macrocode} \def\mplibstoptoPDF{% \egroup % \setbox\mplibscratchbox\hbox % @@ -903,11 +774,11 @@ end \box\mplibscratchbox \egroup } - % \end{macrocode} +% % Text items have a special handler. +% % \begin{macrocode} - \def\mplibtextext#1#2#3#4#5{% \begingroup \setbox\mplibscratchbox\hbox @@ -924,67 +795,14 @@ end \box\mplibscratchbox \endgroup } - % \end{macrocode} % -% \iffalse -%</package> -% \fi -% -% \iffalse -%<*gen-lua> -% \fi -% -% \subsection{\texttt{luamplib-createmem.lua}} -% -% Finally a small standalone file to call with \texttt{texlua} that -% generates \texttt{mplib-luatex.mem} in the current directory. To generate -% other formats in other names, simply change the last line. After the -% \texttt{mem} generation, you'll have to install it in a directory -% searchable by \TeX . +% That's all folks! % % \begin{macrocode} - -kpse.set_program_name("kpsewhich") - -function finder (name, mode, ftype) - if mode == "w" then - return name - else - local result = kpse.find_file(name,ftype) - return result - end -end - -local preamble = [[ -input %s ; dump ; -]] - +%</package> % \end{macrocode} % -% \begin{macro}{makeformat} -% \begin{macrocode} - -makeformat = function (name, mem_name) - local mpx = mplib.new { - ini_version = true, - find_file = finder, - job_name = mem_name, - } - if mpx then - local result - result = mpx:execute(string.format(preamble,name)) - print(string.format("dumping format %s in %s", name, mem_name)) - mpx:finish() - end -end - -makeformat("plain", "mplib-luatex.mem") - -% \end{macrocode} -% \end{macro} -% \iffalse -%</gen-lua> -% \fi +% % \Finale \endinput |