diff options
author | Karl Berry <karl@freefriends.org> | 2014-03-09 21:30:34 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2014-03-09 21:30:34 +0000 |
commit | 630961707af41c344d014c8589d4b793069a98fc (patch) | |
tree | 418a08b198a3cf6e6f55acd6bb36066c3bf8f8db /Master/texmf-dist/source/luatex/luamplib | |
parent | 287d865990e5823a75263a98624f9f4bd89192a4 (diff) |
luamplib (9mar14)
git-svn-id: svn://tug.org/texlive/trunk@33135 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/source/luatex/luamplib')
-rw-r--r-- | Master/texmf-dist/source/luatex/luamplib/luamplib.dtx | 216 |
1 files changed, 132 insertions, 84 deletions
diff --git a/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx b/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx index 0024de422f3..860f3f8c5ce 100644 --- a/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx +++ b/Master/texmf-dist/source/luatex/luamplib/luamplib.dtx @@ -85,7 +85,7 @@ See source file '\inFileName' for licencing and contact information. %<*driver> \NeedsTeXFormat{LaTeX2e} \ProvidesFile{luamplib.drv}% - [2014/03/01 v2.5.3 Interface for using the mplib library]% + [2014/03/08 v2.6.0 Interface for using the mplib library]% \documentclass{ltxdoc} \usepackage{metalogo,multicol,mdwlist,fancyvrb,xspace} \usepackage[x11names]{xcolor} @@ -154,7 +154,7 @@ See source file '\inFileName' for licencing and contact information. % \author{Hans Hagen, Taco Hoekwater, Elie Roux, Philipp Gesang and Kim Dohyun\\ % Maintainer: LuaLaTeX Maintainers --- % Support: \email{lualatex-dev@tug.org}} -% \date{2014/03/01 v2.5.3} +% \date{2014/03/08 v2.6.0} % % \maketitle % @@ -187,7 +187,7 @@ See source file '\inFileName' for licencing and contact information. % % \begin{itemize} % \item a \LaTeX\ environment -% \item all \TeX\ macros start by \texttt{mplib} +% \item all \TeX\ macros start by |mplib| % \item use of luatexbase for errors, warnings and declaration % \item possibility to use |btex ... etex| to typeset \TeX\ code. % |textext()| is a more versatile macro equivalent to |TEX()| from TEX.mp. @@ -215,6 +215,9 @@ See source file '\inFileName' for licencing and contact information. % |verbatimtex ... etex|. % \item Notice that, after each figure is processed, macro \cs{MPwidth} stores % the width value of latest figure; \cs{MPheight}, the height value. +% Incidentally, also note that \cs{MPllx}, \cs{MPlly}, \cs{MPurx}, and +% \cs{MPury} store the bounding box information of latest figure +% without the unit |bp|. % \item Since v2.3, new macros \cs{everymplib} and \cs{everyendmplib} redefine % token lists \cs{everymplibtoks} and \cs{everyendmplibtoks} respectively, % which will @@ -253,7 +256,7 @@ See source file '\inFileName' for licencing and contact information. % \item To support |btex ... etex| in external |.mp| files, \textsf{luamplib} % inspects the content of each and every |.mp| input files and makes caches % if nececcsary, before returning their paths to \LuaTeX's mplib library. -% This would make the compile time longer wastefully, as most |.mp| files +% This would make the compilation time longer wastefully, as most |.mp| files % do not contain |btex ... etex| command. So \textsf{luamplib} provides % macros as follows, so that users can give instruction about files % that do not require this functionality. @@ -270,9 +273,18 @@ See source file '\inFileName' for licencing and contact information. % as the user's home directory (on windows machines as well). % As backslashes (|\|) should be escaped by users, it is easier to use % slashes (|/|) instead. +% \item Starting with v2.6, |\mplibtextextlabel{enable}| enables +% string labels typeset via |textext()| instead of |infont| operator. +% So, |label("my text",origin)| thereafter is exactly the same as +% |label(textext("my text"),origin)|. \textsc{n.b.} In the background, +% \textsf{luamplib} redefines |infont| operator so that the right side +% argument (the font part) is totally ignored. Every string label +% therefore will be typeset with current \TeX\ font. +% Also take care of |char| operator in the left side argument, +% as this might bring unpermitted characters into \TeX. % \item At the end of package loading, \textsf{luamplib} searches % |luamplib.cfg| and, if found, reads the file in automatically. -% Frequently used settings such as |\everymplib| or |\mplibcachedir| +% Frequently used settings such as \cs{everymplib} or \cs{mplibcachedir} % are suitable for going into this file. % % \end{itemize} @@ -307,8 +319,8 @@ luamplib.lastlog = "" local err, warn, info, log = luatexbase.provides_module({ name = "luamplib", - version = "2.5.3", - date = "2014/03/01", + version = "2.6.0", + date = "2014/03/08", description = "Lua package to typeset Metapost with LuaTeX's MPLib.", }) @@ -325,6 +337,7 @@ local stringgsub = string.gsub local stringfind = string.find local stringmatch = string.match local stringgmatch = string.gmatch +local stringexplode = string.explode local tableconcat = table.concat local texsprint = tex.sprint @@ -474,8 +487,8 @@ local function replaceinputmpfile (name,file) local data = fh:read("*all"); fh:close() data = stringgsub(data, "\"[^\n]-\"", function(str) - str = stringgsub(str,"%%","*****PERCENT*****") - str = stringgsub(str,"([bem])tex%f[^A-Z_a-z]","%1***T***E***X***") + str = stringgsub(str,"%%","!!!!!PERCENT!!!!!") + str = stringgsub(str,"([bem])tex%f[^A-Z_a-z]","%1!!!T!!!E!!!X!!!") return str end) data = stringgsub(data,"%%.-\n","") @@ -501,14 +514,16 @@ local function replaceinputmpfile (name,file) end return file end - data = stringgsub(data,"([bem])***T***E***X***","%1tex") - data = stringgsub(data,"*****PERCENT*****","%%") + data = stringgsub(data,"([bem])!!!T!!!E!!!X!!!","%1tex") + data = stringgsub(data,"!!!!!PERCENT!!!!!","%%") fh = ioopen(newfile,"w") if not fh then return file end fh:write(data); fh:close() lfstouch(newfile,currenttime,ofmodify) return newfile end + +local randomseed = nil % \end{macrocode} % 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 @@ -617,6 +632,7 @@ else % See \url{https://github.com/lualatex/luamplib/issues/21}. % \begin{macrocode} math_mode = luamplib.numbersystem, + random_seed = randomseed, } local result if not mpx then @@ -832,72 +848,68 @@ local further_split_keys = { } local function script2table(s) - local t = {} - for i in stringgmatch(s,"[^\13]+") do - local k,v = stringmatch(i,"(.-)=(.+)") -- v may contain =. - if k and v then - local vv = {} - if further_split_keys[k] then - for j in stringgmatch(v,"[^:]+") do - vv[#vv+1] = j - end - end - if #vv > 0 then - t[k] = vv - else - t[k] = v - end - end + local t = {} + for _,i in ipairs(stringexplode(s,"\13+")) do + local k,v = stringmatch(i,"(.-)=(.*)") -- v may contain = or empty. + if k and v and k ~= "" then + if further_split_keys[k] then + t[k] = stringexplode(v,":") + else + t[k] = v + end end - return t + end + return t end local mplibcodepreamble = [[ vardef rawtextext (expr t) = - if unknown TEXBOX_: - image( special "MPlibmkTEXbox="&t; ) + if unknown TEXBOX_: + image( special "MPlibmkTEXbox="&t; ) + else: + TEXBOX_ := TEXBOX_ + 1; + if known TEXBOX_wd_[TEXBOX_]: + image ( addto currentpicture doublepath unitsquare + xscaled TEXBOX_wd_[TEXBOX_] + yscaled (TEXBOX_ht_[TEXBOX_] + TEXBOX_dp_[TEXBOX_]) + shifted (0, -TEXBOX_dp_[TEXBOX_]) + withprescript "MPlibTEXboxID=" & + decimal TEXBOX_ & ":" & + decimal TEXBOX_wd_[TEXBOX_] & ":" & + decimal(TEXBOX_ht_[TEXBOX_]+TEXBOX_dp_[TEXBOX_]); ) else: - TEXBOX_ := TEXBOX_ + 1; - image ( - addto currentpicture doublepath unitsquare - xscaled TEXBOX_wd[TEXBOX_] - yscaled (TEXBOX_ht[TEXBOX_] + TEXBOX_dp[TEXBOX_]) - shifted (0, -TEXBOX_dp[TEXBOX_]) - withprescript "MPlibTEXboxID=" & - decimal TEXBOX_ & ":" & - decimal TEXBOX_wd[TEXBOX_] & ":" & - decimal(TEXBOX_ht[TEXBOX_]+TEXBOX_dp[TEXBOX_]); - ) + image( special "MPlibTEXError=1"; ) fi + fi enddef; if known context_mlib: - defaultfont := "cmtt10"; - let infont = normalinfont; - let fontsize = normalfontsize; - vardef thelabel@#(expr p,z) = - if string p : - thelabel@#(p infont defaultfont scaled defaultscale,z) - else : - p shifted (z + labeloffset*mfun_laboff@# - - (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + - (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) - fi - enddef; - def graphictext primary filename = - if (readfrom filename = EOF): - errmessage "Please prepare '"&filename&"' in advance with command"& - " 'pstoedit -ssp -dt -f mpost yourfile.ps "&filename&"'"; - fi - closefrom filename; - def data_mpy_file = filename enddef; - mfun_do_graphic_text (filename) - enddef; - if unknown TEXBOX_: def mfun_do_graphic_text text t = enddef; fi + defaultfont := "cmtt10"; + let infont = normalinfont; + let fontsize = normalfontsize; + vardef thelabel@#(expr p,z) = + if string p : + thelabel@#(p infont defaultfont scaled defaultscale,z) + else : + p shifted (z + labeloffset*mfun_laboff@# - + (mfun_labxf@#*lrcorner p + mfun_labyf@#*ulcorner p + + (1-mfun_labxf@#-mfun_labyf@#)*llcorner p)) + fi + enddef; + def graphictext primary filename = + if (readfrom filename = EOF): + errmessage "Please prepare '"&filename&"' in advance with"& + " 'pstoedit -ssp -dt -f mpost yourfile.ps "&filename&"'"; + fi + closefrom filename; + def data_mpy_file = filename enddef; + mfun_do_graphic_text (filename) + enddef; + if unknown TEXBOX_: def mfun_do_graphic_text text t = enddef; fi else: - vardef textext@# (text t) = rawtextext (t) enddef; + vardef textext@# (text t) = rawtextext (t) enddef; fi def externalfigure primary filename = - draw rawtextext("\includegraphics{"& filename &"}") + draw rawtextext("\includegraphics{"& filename &"}") enddef; def TEX = textext enddef; def fontmapfile primary filename = enddef; @@ -908,6 +920,20 @@ extra_beginfig := extra_beginfig & " let VerbatimTeX = ignoreVerbatimTeX;" ; extra_endfig := extra_endfig & " let VerbatimTeX = specialVerbatimTeX;" ; ]] +local textextlabelpreamble = [[ +primarydef s infont f = rawtextext(s) enddef; +let normalinfont = infont; +def fontsize expr f = + begingroup + save size,pic; numeric size; picture pic; + pic := rawtextext("\hskip\pdffontsize\font"); + size := xpart urcorner pic - xpart llcorner pic; + if size = 0: 10pt else: size fi + endgroup +enddef; +let normalfontsize = fontsize; +]] + local function protecttextext(data) local everymplib = tex.toks['everymplibtoks'] or '' local everyendmplib = tex.toks['everyendmplibtoks'] or '' @@ -915,8 +941,8 @@ local function protecttextext(data) data = stringgsub(data,"\r","\n") data = stringgsub(data, "\"[^\n]-\"", function(str) - str = stringgsub(str,"%%","****PERCENT****") - str = stringgsub(str,"([bem])tex%f[^A-Z_a-z]","%1***T***E***X***") + str = stringgsub(str,"%%","!!!!!PERCENT!!!!!") + str = stringgsub(str,"([bem])tex%f[^A-Z_a-z]","%1!!!T!!!E!!!X!!!") return str end) data = stringgsub(data,"%%.-\n","") @@ -936,11 +962,11 @@ local function protecttextext(data) end) data = stringgsub(data, "\"[^\n]-\"", function(str) - str = stringgsub(str,"([bem])***T***E***X***","%1tex") - str = stringgsub(str,"{", "****LEFTBRCE****") - str = stringgsub(str,"}", "****RGHTBRCE****") - str = stringgsub(str,"#", "****SHARPE****") - return format("\\unexpanded{%s}",str) + str = stringgsub(str,"([bem])!!!T!!!E!!!X!!!","%1tex") + str = stringgsub(str,"{", "!!!!!LEFTBRCE!!!!!") + str = stringgsub(str,"}", "!!!!!RGHTBRCE!!!!!") + str = stringgsub(str,"#", "!!!!!SHARPE!!!!!") + return format("\\detokenize{%s}",str) end) texsprint(data) end @@ -950,7 +976,7 @@ luamplib.protecttextext = protecttextext local TeX_code_t = {} local function domakeTEXboxes (data) - local num = tex.count[14] -- newbox register + local num = 255 -- output box if data and data.fig then local figures = data.fig for f=1, #figures do @@ -983,13 +1009,18 @@ end local function makeTEXboxes (data) data = stringgsub(data, "##", "#") -- restore # doubled in input string - data = stringgsub(data, "****PERCENT****", "%%") - data = stringgsub(data, "****LEFTBRCE****","{") - data = stringgsub(data, "****RGHTBRCE****","}") - data = stringgsub(data, "****SHARPE****", "#" ) + data = stringgsub(data, "!!!!!PERCENT!!!!!", "%%") + data = stringgsub(data, "!!!!!LEFTBRCE!!!!!","{") + data = stringgsub(data, "!!!!!RGHTBRCE!!!!!","}") + data = stringgsub(data, "!!!!!SHARPE!!!!!", "#" ) + local preamble = mplibcodepreamble + if luamplib.textextlabel then + preamble = textextlabelpreamble .. preamble + end + randomseed = math.random(65535) local mpx = luamplib.load(currentformat) if mpx and data then - local result = mpx:execute(mplibcodepreamble .. data) + local result = mpx:execute(preamble .. data) domakeTEXboxes(result) end return data @@ -1000,18 +1031,22 @@ luamplib.makeTEXboxes = makeTEXboxes local factor = 65536*(7227/7200) local function processwithTEXboxes (data) - local num = tex.count[14] -- the same newbox register + local num = 255 -- output box local prepreamble = "TEXBOX_ := "..num..";\n" while true do num = num + 1 local box = tex.box[num] if not box then break end prepreamble = prepreamble .. - "TEXBOX_wd["..num.."] := "..box.width /factor..";\n".. - "TEXBOX_ht["..num.."] := "..box.height/factor..";\n".. - "TEXBOX_dp["..num.."] := "..box.depth /factor..";\n" + "TEXBOX_wd_["..num.."] := "..box.width /factor..";\n".. + "TEXBOX_ht_["..num.."] := "..box.height/factor..";\n".. + "TEXBOX_dp_["..num.."] := "..box.depth /factor..";\n" + end + local preamble = prepreamble .. mplibcodepreamble + if luamplib.textextlabel then + preamble = textextlabelpreamble .. preamble end - process(prepreamble .. mplibcodepreamble .. data) + process(preamble .. data) end luamplib.processwithTEXboxes = processwithTEXboxes @@ -1229,6 +1264,9 @@ local function flush(result,flusher) miterlimit, linecap, linejoin, dashed = -1, -1, -1, false elseif objecttype == "special" then -- not supported + if prescript and prescript.MPlibTEXError then + warn("textext() anomaly. Try disabling \\mplibtextextlabel.") + end elseif objecttype == "text" then local ot = object.transform -- 3,4,5,6,1,2 pdf_literalcode("q %f %f %f %f %f %f cm",ot[3],ot[4],ot[5],ot[6],ot[1],ot[2]) @@ -1383,7 +1421,7 @@ luamplib.colorconverter = colorconverter \else \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{luamplib} - [2014/03/01 v2.5.3 mplib package for LuaTeX] + [2014/03/08 v2.6.0 mplib package for LuaTeX] \RequirePackage{luatexbase-modutils} \RequirePackage{pdftexcmds} \fi @@ -1551,6 +1589,16 @@ luamplib.colorconverter = colorconverter \fi } \def\mplibcachedir#1{\directlua{luamplib.getcachedir("\unexpanded{#1}")}} +\def\mplibtextextlabel#1{% + \begingroup + \def\tempa{enable}\def\tempb{#1}% + \ifx\tempa\tempb + \directlua{luamplib.textextlabel = true}% + \else + \directlua{luamplib.textextlabel = false}% + \fi + \endgroup +} % \end{macrocode} % % We use a dedicated scratchbox. |