diff options
Diffstat (limited to 'Master/texmf-dist/tex/luatex/luamplib/luamplib.lua')
-rw-r--r-- | Master/texmf-dist/tex/luatex/luamplib/luamplib.lua | 234 |
1 files changed, 160 insertions, 74 deletions
diff --git a/Master/texmf-dist/tex/luatex/luamplib/luamplib.lua b/Master/texmf-dist/tex/luatex/luamplib/luamplib.lua index 897dd456737..575e030539b 100644 --- a/Master/texmf-dist/tex/luatex/luamplib/luamplib.lua +++ b/Master/texmf-dist/tex/luatex/luamplib/luamplib.lua @@ -18,8 +18,8 @@ luamplib.lastlog = "" local err, warn, info, log = luatexbase.provides_module({ name = "luamplib", - version = "2.5.1", - date = "2014/02/19", + version = "2.5.3", + date = "2014/03/01", description = "Lua package to typeset Metapost with LuaTeX's MPLib.", }) @@ -35,6 +35,12 @@ local texsprint = tex.sprint local mplib = require ('mplib') local kpse = require ('kpse') +local lfs = require ('lfs') + +local lfsattributes = lfs.attributes +local lfsisdir = lfs.isdir +local lfstouch = lfs.touch +local ioopen = io.open local file = file if not file then @@ -51,49 +57,44 @@ if not file then end end -local mpreplacedabove = {} +local luamplibtime = kpse.find_file("luamplib.lua") +luamplibtime = luamplibtime and lfsattributes(luamplibtime,"modification") -local function replaceinputmpfile (file) - local fh = io.open(file,"r") - if not fh then return file end - local data = fh:read("*all"); fh:close() - data = stringgsub(data, "\".-\"", - function(str) - str = stringgsub(str,"%%","*****PERCENT*****") - str = stringgsub(str,"([bm])tex%f[^A-Z_a-z]","%1***T***E***X***") - return str - end) - data = stringgsub(data,"%%.-\n","") - local count,cnt = 0,0 - data,cnt = stringgsub(data, - "%f[A-Z_a-z]btex%f[^A-Z_a-z]%s*(.-)%s*%f[A-Z_a-z]etex%f[^A-Z_a-z]", - function(str) - str = stringgsub(str,"[\n\r]%s*"," ") - str = stringgsub(str,'"','"&ditto&"') - return format("rawtextext(\"%s\")",str) - end) - count = count + cnt - data,cnt = stringgsub(data, - "%f[A-Z_a-z]verbatimtex%f[^A-Z_a-z]%s*(.-)%s*%f[A-Z_a-z]etex%f[^A-Z_a-z]", - "") - count = count + cnt - if count == 0 then - mpreplacedabove[file] = file - return file +local currenttime = os.time() + +local outputdir = "." +for _,v in ipairs(arg) do + local t = stringmatch(v,"%-output%-directory=(.+)") + if t then + outputdir = t + break + end +end + +function luamplib.getcachedir(dir) + dir = stringgsub(dir,"##","#") + dir = stringgsub(dir,"^~", + os.type == "windows" and os.getenv("UserProfile") or os.getenv("HOME")) + if lfstouch and dir then + if lfsisdir(dir) then + local tmp = dir.."/_luam_plib_temp_file_" + local fh = ioopen(tmp,"w") + if fh then + fh:close(fh) + os.remove(tmp) + luamplib.cachedir = dir + else + warn("Directory '"..dir.."' is not writable!") + end + else + warn("Directory '"..dir.."' does not exist!") end - data = stringgsub(data,"([bm])***T***E***X***","%1tex") - data = stringgsub(data,"*****PERCENT*****","%%") - local newfile = stringgsub(file,".*/","luamplib_input_") - fh = io.open(newfile,"w") - if not fh then return file end - fh:write(data); fh:close() - mpreplacedabove[file] = newfile - return newfile + end end local noneedtoreplace = { ["boxes.mp"] = true, - ["format.mp"] = true, +-- ["format.mp"] = true, ["graph.mp"] = true, ["marith.mp"] = true, ["mfplain.mp"] = true, @@ -128,6 +129,80 @@ local noneedtoreplace = { ["mp-text.mpiv"] = true, ["mp-tool.mpiv"] = true, } +luamplib.noneedtoreplace = noneedtoreplace + +local function replaceformatmp(file,newfile,ofmodify) + local fh = ioopen(file,"r") + if not fh then return file end + local data = fh:read("*all"); fh:close() + fh = ioopen(newfile,"w") + if not fh then return file end + fh:write( + "let normalinfont = infont;\n", + "primarydef str infont name = rawtextext(str) enddef;\n", + data, + "vardef Fmant_(expr x) = rawtextext(decimal abs x) enddef;\n", + "vardef Fexp_(expr x) = rawtextext(\"$^{\"&decimal x&\"}$\") enddef;\n", + "let infont = normalinfont;\n" + ); fh:close() + lfstouch(newfile,currenttime,ofmodify) + return newfile +end + +local function replaceinputmpfile (name,file) + local ofmodify = lfsattributes(file,"modification") + if not ofmodify then return file end + local cachedir = luamplib.cachedir or outputdir + local newfile = stringgsub(name,"%W","_") + newfile = cachedir .."/luamplib_input_"..newfile + if newfile and luamplibtime then + local nf = lfsattributes(newfile) + if nf and nf.mode == "file" and ofmodify == nf.modification and luamplibtime < nf.access then + return nf.size == 0 and file or newfile + end + end + if name == "format.mp" then return replaceformatmp(file,newfile,ofmodify) end + + local fh = ioopen(file,"r") + if not fh then return file end + 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***") + return str + end) + data = stringgsub(data,"%%.-\n","") + local count,cnt = 0,0 + data,cnt = stringgsub(data, + "%f[A-Z_a-z]btex%f[^A-Z_a-z]%s*(.-)%s*%f[A-Z_a-z]etex%f[^A-Z_a-z]", + function(str) + str = stringgsub(str,"[\n\r]%s*"," ") + str = stringgsub(str,'"','"&ditto&"') + return format("rawtextext(\"%s\")",str) + end) + count = count + cnt + data,cnt = stringgsub(data, + "%f[A-Z_a-z]verbatimtex%f[^A-Z_a-z]%s*.-%s*%f[A-Z_a-z]etex%f[^A-Z_a-z]", + "") + count = count + cnt + if count == 0 then + noneedtoreplace[name] = true + fh = ioopen(newfile,"w"); + if fh then + fh:close() + lfstouch(newfile,currenttime,ofmodify) + end + return file + end + 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 mpkpse = kpse.new("luatex", "mpost") @@ -137,9 +212,10 @@ local function finder(name, mode, ftype) else local file = mpkpse:find_file(name,ftype) if file then - if ftype ~= "mp" or noneedtoreplace[name] then return file end - if mpreplacedabove[file] then return mpreplacedabove[file] end - return replaceinputmpfile(file) + if not lfstouch or ftype ~= "mp" or noneedtoreplace[name] then + return file + end + return replaceinputmpfile(name,file) end return mpkpse:find_file(name,stringmatch(name,"[a-zA-Z]+$")) end @@ -205,7 +281,6 @@ else end function luamplib.load(name) - mpreplacedabove = {} -- reset local mpx = mplib.new { ini_version = true, find_file = luamplib.finder, @@ -485,54 +560,42 @@ extra_endfig := extra_endfig & " let VerbatimTeX = specialVerbatimTeX;" ; local function protecttextext(data) local everymplib = tex.toks['everymplibtoks'] or '' local everyendmplib = tex.toks['everyendmplibtoks'] or '' - data = " " .. everymplib .. data .. everyendmplib + data = "\n" .. everymplib .."\n".. data .."\n".. everyendmplib + 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***") + return str + end) + data = stringgsub(data,"%%.-\n","") data = stringgsub(data, "%f[A-Z_a-z]btex%f[^A-Z_a-z]%s*(.-)%s*%f[A-Z_a-z]etex%f[^A-Z_a-z]", function(str) str = stringgsub(str,'"','"&ditto&"') - return format("rawtextext(\\unexpanded{\"%s\"})",str) + str = stringgsub(str,"\n%s*"," ") + return format("rawtextext(\"%s\")",str) end) data = stringgsub(data, "%f[A-Z_a-z]verbatimtex%f[^A-Z_a-z]%s*(.-)%s*%f[A-Z_a-z]etex%f[^A-Z_a-z]", function(str) str = stringgsub(str,'"','"&ditto&"') - return format("VerbatimTeX(\\unexpanded{\"%s\"})",str) + str = stringgsub(str,"\n%s*"," ") + return format("VerbatimTeX(\"%s\")",str) end) - data = stringgsub(data, "\".-\"", -- hack for parentheses inside quotes + data = stringgsub(data, "\"[^\n]-\"", function(str) - str = stringgsub(str,"%(","%%%%LEFTPAREN%%%%") - str = stringgsub(str,"%)","%%%%RGHTPAREN%%%%") - return 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) end) - data = stringgsub(data, "%f[A-Z_a-z]TEX%s*%b()", "\\unexpanded{%1}") - data = stringgsub(data, "%f[A-Z_a-z]textext%s*%b()", "\\unexpanded{%1}") - data = stringgsub(data, "%f[A-Z_a-z]textext%.[_a-z]+%s*%b()", "\\unexpanded{%1}") - data = stringgsub(data, "%%%%LEFTPAREN%%%%", "(") -- restore - data = stringgsub(data, "%%%%RGHTPAREN%%%%", ")") -- restore texsprint(data) end luamplib.protecttextext = protecttextext -local factor = 65536*(7227/7200) - -local function putTEXboxes (object,prescript) - local box = prescript.MPlibTEXboxID - local n,tw,th = box[1],box[2],box[3] - if n and tw and th then - local op = object.path - local first, second, fourth = op[1], op[2], op[4] - local tx, ty = first.x_coord, first.y_coord - local sx, sy = (second.x_coord - tx)/tw, (fourth.y_coord - ty)/th - local rx, ry = (second.y_coord - ty)/tw, (fourth.x_coord - tx)/th - if sx == 0 then sx = 0.00001 end - if sy == 0 then sy = 0.00001 end - pdf_literalcode("q %f %f %f %f %f %f cm",sx,rx,ry,sy,tx,ty) - texsprint(format("\\mplibputtextbox{%i}",n)) - pdf_literalcode("Q") - end -end - local TeX_code_t = {} local function domakeTEXboxes (data) @@ -565,6 +628,10 @@ 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****", "#" ) local mpx = luamplib.load(currentformat) if mpx and data then local result = mpx:execute(mplibcodepreamble .. data) @@ -575,6 +642,8 @@ end luamplib.makeTEXboxes = makeTEXboxes +local factor = 65536*(7227/7200) + local function processwithTEXboxes (data) local num = tex.count[14] -- the same newbox register local prepreamble = "TEXBOX_ := "..num..";\n" @@ -592,6 +661,23 @@ end luamplib.processwithTEXboxes = processwithTEXboxes +local function putTEXboxes (object,prescript) + local box = prescript.MPlibTEXboxID + local n,tw,th = box[1],box[2],box[3] + if n and tw and th then + local op = object.path + local first, second, fourth = op[1], op[2], op[4] + local tx, ty = first.x_coord, first.y_coord + local sx, sy = (second.x_coord - tx)/tw, (fourth.y_coord - ty)/th + local rx, ry = (second.y_coord - ty)/tw, (fourth.x_coord - tx)/th + if sx == 0 then sx = 0.00001 end + if sy == 0 then sy = 0.00001 end + pdf_literalcode("q %f %f %f %f %f %f cm",sx,rx,ry,sy,tx,ty) + texsprint(format("\\mplibputtextbox{%i}",n)) + pdf_literalcode("Q") + end +end + local pdf_objs = {} -- objstr <string> => obj <number>, new <boolean> |