Create and use images with MetaPost

The functionality to create and embed MetaPost graphics is still very new and experimental. The interface may still change or disappear altogether. In this respect, it should be used with caution.

MetaPost is a programming language that can be used to create graphics.

For example, one creates from the following program code:

beginfig(1)
    for a=0 upto 9:
      draw (0,0){dir 45}..{dir -10a}(6cm,0);
    endfor
endfig;

a graphic:

mp first

MetaPost and the speedata Publisher

The idea now is to be able to use these images in the speedata Publisher as well. To do this, you first define a graphic and then use it later in the box command:

<Layout
    xmlns="urn:speedata.de:2009/publisher/en"
    xmlns:sd="urn:speedata:2009/publisher/functions/en">

    <!-- no beginfig() ... endfig necessary -->
    <DefineGraphic name="dottedbox">
      pickup pencircle scaled 1mm;
      draw (0,0) -- (box.width,0) -- (box.width, box.height) --
        (box.width, box.height) -- (0, box.height ) -- cycle
        dashed withdots withcolor 0.7red ;
    </DefineGraphic>

    <!-- same, but uses predefined path box: -->
    <DefineGraphic name="dottedboxsimple">
        pickup pencircle scaled 1mm;
        draw box dashed withdots withcolor 0.7red ;
    </DefineGraphic>


    <Record element="data">
        <PlaceObject row="1" column="1">
            <Box height="2" width="5" graphic="dottedbox" />
        </PlaceObject>
    </Record>
</Layout>

The width of the box and the height of the box are defined with the variables box.width and box.height in MetaPost. This allows you to adjust the graphic to the defaults in the Publisher.

mp dotted
The dotted lines correspond to the specifications of the box. Raster display has been turned on to make this visible.

Coordinates

The origin of the coordinate system is in the lower left corner of the box, so positive values go in the right and up direction.

Variables

Variables for MetaPost can be set with <SetVariable>.

<SetVariable
    variable="curcol"
    type="mp:rgbcolor"
    select="'colors_mycolor'"/>

The assignment is evaluated at the beginning of the MetaPost context. Thus curcol is available at the beginning of the graphics.

Predefined values

  • All colors defined in the layout can be used in MetaPost with the prefix colors.:
    <DefineColor name="mycolor" value="#FF4E00"/>
    
    <DefineGraphic name="dots">
        pickup pencircle scaled 3mm;
        for i=0 upto 3:
            draw (i * 1cm, i * 1cm) withcolor colors.mycolor ;
        endfor;
    </DefineGraphic>
    
    <Record element="data">
        <PlaceObject row="1" column="1">
            <Box height="5" width="1" graphic="dots" />
        </PlaceObject>
    </Record>
  • CSS level 3 colors are defined in RGB colorspace.
  • The width and height of a box can be accessed via box.width and box.height
  • The box' path is saved in the variable box (see the example above).
  • Hans Hagen’s MetaFun macro package is included (parts of it).
  • In page types you can also access these variables:
    Variable Description
    page.width Page width
    page.height Page height
    page.margin.left Margin left
    page.margin.right Margin right
    page.margin.top Margin top
    page.margin.bottom Margin bottom
    page.trim Bleed

MetaPost resources

There are a number of manuals and tutorials for MetaPost: