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:

ad 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">

    <DefineGraphic name="dottedbox">
      beginfig(1);
      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 ;
      endfig;
    </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.

ad 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 color_:

<DefineColor name="mycolor" value="#FF4E00"/>

<DefineGraphic name="dots">
    beginfig(1);
    pickup pencircle scaled 3mm;
    for i=0 upto 3:
        draw (i * 1cm, i * 1cm) withcolor colors_mycolor ;
    endfor;
    endfig;
</DefineGraphic>

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

MetaPost resources

There are a number of manuals and tutorials for MetaPost: