[metapost] METAPOST environment taking advantage of the SVG support for previewing?

luigi scarso luigi.scarso at gmail.com
Fri Feb 17 09:00:16 CET 2017


On Thu, Feb 16, 2017 at 9:13 PM, William Adams <will.adams at frycomm.com> wrote:
> I was wondering if anyone had done this sort of thing, or given it any
> thoughts --- I think it'd be easier to set up and design and work up than
> some of the other options.
>
> I want a nice graphical tool for METAPOST editing / coding / drawing, and I
> think it's finally time I tried to just make my own if there aren't any
> other options besides METAGRAF.
Also
http://ipe.otfried.org/

There is mplib; API is  in  mplibapi.pdf at
http://tug.org/svn/texlive/trunk/Master/texmf-dist/doc/metapost/base/

This is a simple example of the Lua API; you have to put plain.mp and cmr10.tfm
in the same folder of test.lua .

--[==[ test.lua ]==]
local mplib = require('mplib')
local mp = mplib.new ({ ini_version = false,
                        mem_name    = 'plain' })

if mp then
  local l
  local tracingall=[[tracingonline:=1; showstopping:=1;
      tracingcommands:=3; tracingtitles:=1; tracingequations:=1;
      tracingcapsules:=1; tracingspecs:=2; tracingchoices:=1;
tracinglostchars:=1;
      tracingstats:=1; tracingoutput:=1; tracingmacros:=1; tracingrestores:=1;
     ]]

  l = mp:execute([[input plain;]])      -- for k,v in pairs(l) do
print('k='.. tostring(k),'v='..tostring(v)) end
--l = mp:execute(tracingall)            -- for k,v in pairs(l) do
print('k='.. tostring(k),'v='..tostring(v)) end
  l = mp:execute([[
                     beginfig(1);
                     fill fullcircle scaled 20;
                     label("Hello", (100,100) );
                     currentpicture := currentpicture scaled 20;
                      endfig;
                       ]])
--[=[ log and term out ]=]
 -- for k,v in pairs(l) do print(k,v) end

  if l and l.fig and l.fig[1] then
    local fpng = io.open("test.png",'w')
    local fps = io.open("test.ps",'w')
    local fsvg = io.open("test.svg",'w')
    --print (l.fig[1]:postscript())
    fps:write(l.fig[1]:postscript()); fps:close()
    --print (l.fig[1]:png());
    fpng:write(l.fig[1]:png()); fpng:close()
    --print (l.fig[1]:svg())
    fsvg:write(l.fig[1]:svg()); fsvg:close()
    for k1, v1 in pairs(l.fig[1]:objects()) do print(k1,v1.type) end
  end
  mp:finish();
end

The most important is objects() because it gives the complete list of
objects in  abstract way,
but one can also parse the ps backend.

mplib is embedded in luatex, so you can run the example with

$> luatex --luaonly test.lua


Be careful  that  pictures like this
picture p;
p := btex $\displaystyle\int_0^\infty e^{-x} \, \sin x \, dx $ etex;
draw p;

require a TeX engine
(basically Context does exactly this: combine objects of mplib  and
the tex typesetting engine of luatex)

Also, we are planning to drop the png and svg interface in the mplib
embedded in luatex  -- *not* in Metapost nor mplib .

-- 
luigi


More information about the metapost mailing list