diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/mlib-pdf.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/mlib-pdf.lua | 138 |
1 files changed, 66 insertions, 72 deletions
diff --git a/Master/texmf-dist/tex/context/base/mlib-pdf.lua b/Master/texmf-dist/tex/context/base/mlib-pdf.lua index 7a23ba94772..96330995105 100644 --- a/Master/texmf-dist/tex/context/base/mlib-pdf.lua +++ b/Master/texmf-dist/tex/context/base/mlib-pdf.lua @@ -6,31 +6,49 @@ if not modules then modules = { } end modules ['mlib-pdf'] = { license = "see context related readme files", } +-- maybe %s is better than %f + local format, concat, gsub = string.format, table.concat, string.gsub local abs, sqrt, round = math.abs, math.sqrt, math.round local setmetatable = setmetatable local Cf, C, Cg, Ct, P, S, lpegmatch = lpeg.Cf, lpeg.C, lpeg.Cg, lpeg.Ct, lpeg.P, lpeg.S, lpeg.match - -local allocate = utilities.storage.allocate +local formatters = string.formatters local report_metapost = logs.reporter("metapost") local mplib, context = mplib, context -local copy_node = node.copy -local write_node = node.write +local allocate = utilities.storage.allocate + +local copy_node = node.copy +local write_node = node.write + +metapost = metapost or { } +local metapost = metapost + +metapost.flushers = metapost.flushers or { } +local pdfflusher = { } +metapost.flushers.pdf = pdfflusher + +metapost.multipass = false +metapost.n = 0 +metapost.optimize = true -- false + +local experiment = true -- uses context(node) that already does delayed nodes -metapost = metapost or { } -local metapost = metapost +local savedliterals = nil -- needs checking +local mpsliteral = nodes.pool.register(node.new("whatsit",nodes.whatsitcodes.pdfliteral)) -- pdfliteral.mode = 1 -metapost.multipass = false -metapost.n = 0 -metapost.optimize = true -- false +local pdfliteral = function(s) + local literal = copy_node(mpsliteral) + literal.data = s + return literal +end ---~ Because in MKiV we always have two passes, we save the objects. When an extra ---~ mp run is done (due to for instance texts identifier in the parse pass), we ---~ get a new result table and the stored objects are forgotten. Otherwise they ---~ are reused. +-- Because in MKiV we always have two passes, we save the objects. When an extra +-- mp run is done (due to for instance texts identifier in the parse pass), we +-- get a new result table and the stored objects are forgotten. Otherwise they +-- are reused. local function getobjects(result,figure,f) if metapost.optimize then @@ -64,30 +82,13 @@ function metapost.convert(result, trialrun, flusher, multipass, askedfig) return true -- done end -metapost.flushers = { } -metapost.flushers.pdf = { } - --- \def\MPLIBtoPDF#1{\ctxlua{metapost.flushliteral(#1)}} - -local savedliterals = nil -- needs checking - -local mpsliteral = nodes.pool.register(node.new("whatsit",8)) -- pdfliteral - -local pdfliteral = function(s) - local literal = copy_node(mpsliteral) - literal.data = s - return literal -end - -local experiment = true -- uses context(node) that already does delayed nodes - function metapost.flushliteral(d) if savedliterals then local literal = copy_node(mpsliteral) literal.data = savedliterals[d] write_node(literal) else - report_metapost("problem flushing literal %s",d) + report_metapost("problem flushing literal %a",d) end end @@ -95,9 +96,9 @@ function metapost.flushreset() -- will become obsolete and internal savedliterals = nil end -function metapost.flushers.pdf.comment(message) +function pdfflusher.comment(message) if message then - message = format("%% mps graphic %s: %s", metapost.n, message) + message = formatters["%% mps graphic %s: %s"](metapost.n,message) if experiment then context(pdfliteral(message)) else @@ -113,20 +114,20 @@ function metapost.flushers.pdf.comment(message) end end -function metapost.flushers.pdf.startfigure(n,llx,lly,urx,ury,message) +function pdfflusher.startfigure(n,llx,lly,urx,ury,message) savedliterals = nil metapost.n = metapost.n + 1 context.startMPLIBtoPDF(llx,lly,urx,ury) - if message then metapost.flushers.pdf.comment(message) end + if message then pdfflusher.comment(message) end end -function metapost.flushers.pdf.stopfigure(message) - if message then metapost.flushers.pdf.comment(message) end +function pdfflusher.stopfigure(message) + if message then pdfflusher.comment(message) end context.stopMPLIBtoPDF() context.MPLIBflushreset() -- maybe just at the beginning end -function metapost.flushers.pdf.flushfigure(pdfliterals) -- table +function pdfflusher.flushfigure(pdfliterals) -- table if #pdfliterals > 0 then pdfliterals = concat(pdfliterals,"\n") if experiment then @@ -144,7 +145,7 @@ function metapost.flushers.pdf.flushfigure(pdfliterals) -- table end end -function metapost.flushers.pdf.textfigure(font,size,text,width,height,depth) -- we could save the factor +function pdfflusher.textfigure(font,size,text,width,height,depth) -- we could save the factor text = gsub(text,".","\\hbox{%1}") -- kerning happens in metapost (i have to check if this is true for mplib) context.MPtextext(font,size,text,0,-number.dimenfactors.bp*depth) end @@ -189,11 +190,11 @@ local function flushnormalpath(path, t, open) nt = nt + 1 pth = path[i] if not ith then - t[nt] = format("%f %f m",pth.x_coord,pth.y_coord) + t[nt] = formatters["%f %f m"](pth.x_coord,pth.y_coord) elseif curved(ith,pth) then - t[nt] = format("%f %f %f %f %f %f c",ith.right_x,ith.right_y,pth.left_x,pth.left_y,pth.x_coord,pth.y_coord) + t[nt] = formatters["%f %f %f %f %f %f c"](ith.right_x,ith.right_y,pth.left_x,pth.left_y,pth.x_coord,pth.y_coord) else - t[nt] = format("%f %f l",pth.x_coord,pth.y_coord) + t[nt] = formatters["%f %f l"](pth.x_coord,pth.y_coord) end ith = pth end @@ -201,15 +202,15 @@ local function flushnormalpath(path, t, open) nt = nt + 1 local one = path[1] if curved(pth,one) then - t[nt] = format("%f %f %f %f %f %f c",pth.right_x,pth.right_y,one.left_x,one.left_y,one.x_coord,one.y_coord ) + t[nt] = formatters["%f %f %f %f %f %f c"](pth.right_x,pth.right_y,one.left_x,one.left_y,one.x_coord,one.y_coord ) else - t[nt] = format("%f %f l",one.x_coord,one.y_coord) + t[nt] = formatters["%f %f l"](one.x_coord,one.y_coord) end elseif #path == 1 then -- special case .. draw point local one = path[1] nt = nt + 1 - t[nt] = format("%f %f l",one.x_coord,one.y_coord) + t[nt] = formatters["%f %f l"](one.x_coord,one.y_coord) end return t end @@ -223,18 +224,18 @@ local function flushconcatpath(path, t, open) nt = 0 end nt = nt + 1 - t[nt] = format("%f %f %f %f %f %f cm", sx, rx, ry, sy, tx ,ty) + t[nt] = formatters["%f %f %f %f %f %f cm"](sx,rx,ry,sy,tx,ty) for i=1,#path do nt = nt + 1 pth = path[i] if not ith then - t[nt] = format("%f %f m",mpconcat(pth.x_coord,pth.y_coord)) + t[nt] = formatters["%f %f m"](mpconcat(pth.x_coord,pth.y_coord)) elseif curved(ith,pth) then local a, b = mpconcat(ith.right_x,ith.right_y) local c, d = mpconcat(pth.left_x,pth.left_y) - t[nt] = format("%f %f %f %f %f %f c",a,b,c,d,mpconcat(pth.x_coord,pth.y_coord)) + t[nt] = formatters["%f %f %f %f %f %f c"](a,b,c,d,mpconcat(pth.x_coord,pth.y_coord)) else - t[nt] = format("%f %f l",mpconcat(pth.x_coord, pth.y_coord)) + t[nt] = formatters["%f %f l"](mpconcat(pth.x_coord, pth.y_coord)) end ith = pth end @@ -244,15 +245,15 @@ local function flushconcatpath(path, t, open) if curved(pth,one) then local a, b = mpconcat(pth.right_x,pth.right_y) local c, d = mpconcat(one.left_x,one.left_y) - t[nt] = format("%f %f %f %f %f %f c",a,b,c,d,mpconcat(one.x_coord, one.y_coord)) + t[nt] = formatters["%f %f %f %f %f %f c"](a,b,c,d,mpconcat(one.x_coord, one.y_coord)) else - t[nt] = format("%f %f l",mpconcat(one.x_coord,one.y_coord)) + t[nt] = formatters["%f %f l"](mpconcat(one.x_coord,one.y_coord)) end elseif #path == 1 then -- special case .. draw point nt = nt + 1 local one = path[1] - t[nt] = format("%f %f l",mpconcat(one.x_coord,one.y_coord)) + t[nt] = formatters["%f %f l"](mpconcat(one.x_coord,one.y_coord)) end return t end @@ -273,7 +274,7 @@ function metapost.flush(result,flusher,askedfig) if result then local figures = result.fig if figures then - flusher = flusher or metapost.flushers.pdf + flusher = flusher or pdfflusher local resetplugins = metapost.resetplugins or ignore -- before figure local processplugins = metapost.processplugins or ignore -- each object local synchronizeplugins = metapost.synchronizeplugins or ignore @@ -290,7 +291,7 @@ function metapost.flush(result,flusher,askedfig) local t = { } local miterlimit, linecap, linejoin, dashed = -1, -1, -1, false local bbox = figure:boundingbox() - local llx, lly, urx, ury = bbox[1], bbox[2], bbox[3], bbox[4] -- faster than unpack + local llx, lly, urx, ury = bbox[1], bbox[2], bbox[3], bbox[4] metapost.llx = llx metapost.lly = lly metapost.urx = urx @@ -319,7 +320,7 @@ function metapost.flush(result,flusher,askedfig) elseif objecttype == "text" then t[#t+1] = "q" local ot = object.transform -- 3,4,5,6,1,2 - t[#t+1] = format("%f %f %f %f %f %f cm",ot[3],ot[4],ot[5],ot[6],ot[1],ot[2]) -- TH: format("%f %f m %f %f %f %f 0 0 cm",unpack(ot)) + t[#t+1] = formatters["%f %f %f %f %f %f cm"](ot[3],ot[4],ot[5],ot[6],ot[1],ot[2]) -- TH: formatters["%f %f m %f %f %f %f 0 0 cm"](unpack(ot)) flushfigure(t) -- flush accumulated literals t = { } textfigure(object.font,object.dsize,object.text,object.width,object.height,object.depth) @@ -344,21 +345,21 @@ function metapost.flush(result,flusher,askedfig) local ml = object.miterlimit if ml and ml ~= miterlimit then miterlimit = ml - t[#t+1] = format("%f M",ml) + t[#t+1] = formatters["%f M"](ml) end local lj = object.linejoin if lj and lj ~= linejoin then linejoin = lj - t[#t+1] = format("%i j",lj) + t[#t+1] = formatters["%i j"](lj) end local lc = object.linecap if lc and lc ~= linecap then linecap = lc - t[#t+1] = format("%i J",lc) + t[#t+1] = formatters["%i J"](lc) end local dl = object.dash if dl then - local d = format("[%s] %i d",concat(dl.dashes or {}," "),dl.offset) + local d = formatters["[%s] %f d"](concat(dl.dashes or {}," "),dl.offset) if d ~= dashed then dashed = d t[#t+1] = dashed @@ -374,7 +375,7 @@ function metapost.flush(result,flusher,askedfig) if pen then if pen.type == 'elliptical' then transformed, penwidth = pen_characteristics(original) -- boolean, value - t[#t+1] = format("%f w",penwidth) -- todo: only if changed + t[#t+1] = formatters["%f w"](penwidth) -- todo: only if changed if objecttype == 'fill' then objecttype = 'both' end @@ -451,24 +452,17 @@ function metapost.parse(result,askedfig) local figures = result.fig if figures then local analyzeplugins = metapost.analyzeplugins -- each object - for f=1, #figures do + for f=1,#figures do local figure = figures[f] local fignum = figure:charcode() or 0 if askedfig == "direct" or askedfig == "all" or askedfig == fignum then local bbox = figure:boundingbox() - local llx, lly, urx, ury = bbox[1], bbox[2], bbox[3], bbox[4] -- faster than unpack - metapost.llx = llx - metapost.lly = lly - metapost.urx = urx - metapost.ury = ury + metapost.llx = bbox[1] + metapost.lly = bbox[2] + metapost.urx = bbox[3] + metapost.ury = bbox[4] local objects = getobjects(result,figure,f) if objects then - -- for o=1,#objects do - -- local object = objects[o] - -- local prescript = object.prescript - -- if prescript then - -- analyzeplugins(object) - -- end for o=1,#objects do analyzeplugins(objects[o]) end |