MetaFun - Pragma ADE

MetaFun - Pragma ADE MetaFun - Pragma ADE

pragma.ade.com
from pragma.ade.com More from this publisher
29.11.2012 Views

68 4 5 pickup pencircle scaled .5cm ; filldraw unitsquare xscaled 8cm yscaled 1cm withcolor .625white ; path bb ; bboxmargin := 0pt ; bb := bbox currentpicture ; draw bb withpen pencircle scaled 1pt withcolor .625red ; draw origin withpen pencircle scaled 5pt withcolor .625yellow ; The resulting graphic has the bounding box of the fill. Note how the path, because it is stroked with a .5cm pen, extends beyond the border of the previous bounding box. The way this image shows up depends on the viewer (settings) you use to render the graphic. For example, in GHOSTVIEW, if you disable clipping to the bounding box, only the positive quadrant of the graphic is shown. 4 From the previous examples, you can conclude that the following alternative results in a proper bounding box: pickup pencircle scaled .5cm ; path p ; p := unitsquare xscaled 8cm yscaled 1cm ; fill p withcolor .625white ; draw p withcolor .625white ; path bb ; bboxmargin := 0pt ; bb := bbox currentpicture ; draw bb withpen pencircle scaled 1pt withcolor .625red ; draw origin withpen pencircle scaled 5pt withcolor .625yellow ; The CONTEXT distribution comes with a set of METAPOST modules, one of which contains the drawfill macro, which provides the outer bounding box. 5 Next we demonstrate its use in another, more complicated example. picture finalpicture ; finalpicture := nullpicture ; numeric n ; n := 0 ; bboxmargin := 0pt ; pickup pencircle scaled .5cm ; def shape = unitsquare scaled 2cm withcolor .625white ; draw bbox currentpicture withpen pencircle scaled .5mm withcolor .625red ; addto finalpicture also currentpicture shifted(n*3cm,0) ; currentpicture := nullpicture ; n := n+1 ; enddef ; Old versions of METAPOST calculated the boundingbox differently for a filldraw: through the middle of the penpath. Starting from version 1.0 METAPOST calculates the boundingbox differently and the distinction between drawfill and filldraw is gone. We keep them around both for compatibility. A few more details Bounding boxes

fill shape ; draw shape ; filldraw shape ; drawfill shape ; currentpicture := finalpicture ; Here we introduce a macro definition, shape. In METAPOST, the start of a macro definition is indicated with the keyword def. Thereafter, you can insert other variables and commands, even other macro definitions. The keyword enddef signals the end of the macro definition. The result is shown in figure 2.1; watch the bounding boxes. Close reading of the macro will reveal that the fill, draw, filldraw and drawfill macros are applied to the first unitsquare path in the macro. Figure 2.1 A fill, draw, filldraw and drawfill applied to the same square. In this macro, bbox calls a macro that returns the enlarged bounding box of a path. By setting bboxmargin we can influence how much the bounding box is enlarged. Since this is an existing variable, we don't have to allocate it, like we do with numeric n. Unless you take special precautions, variables are global by nature and persistent outside macros. picture finalpicture ; finalpicture := nullpicture ; Just as numeric allocates an integer variable, the picture primitive allocates a picture data structure. We explicitly have to set this picture to nothing using the built--in primitive nullpicture. Later on, we will add the drawn paths as accumulated in currentpicture to this finalpicture in the following manner. addto finalpicture also currentpicture shifted(n*3cm,0) ; Since we want to add a few more and don't want them to overlap, we shift them. Therefore we have to erase the current picture as well as increment the shift counter. currentpicture := nullpicture ; n := n+1 ; The drawfill macro is one of the METAFUN macros. Another handy macro is boundingbox. When used instead of bbox, you don't have to set the margin to zero. Figure 2.2 The influence of pens on fill. Bounding boxes A few more details 69

fill shape ; draw shape ; filldraw shape ; drawfill shape ;<br />

currentpicture := finalpicture ;<br />

Here we introduce a macro definition, shape. In METAPOST, the start of a macro definition is indicated<br />

with the keyword def. Thereafter, you can insert other variables and commands, even<br />

other macro definitions. The keyword enddef signals the end of the macro definition. The result<br />

is shown in figure 2.1; watch the bounding boxes. Close reading of the macro will reveal that the<br />

fill, draw, filldraw and drawfill macros are applied to the first unitsquare path in the macro.<br />

Figure 2.1 A fill, draw, filldraw and<br />

drawfill applied to the same square.<br />

In this macro, bbox calls a macro that returns the enlarged bounding box of a path. By setting<br />

bboxmargin we can influence how much the bounding box is enlarged. Since this is an existing<br />

variable, we don't have to allocate it, like we do with numeric n. Unless you take special precautions,<br />

variables are global by nature and persistent outside macros.<br />

picture finalpicture ; finalpicture := nullpicture ;<br />

Just as numeric allocates an integer variable, the picture primitive allocates a picture data structure.<br />

We explicitly have to set this picture to nothing using the built--in primitive nullpicture.<br />

Later on, we will add the drawn paths as accumulated in currentpicture to this finalpicture<br />

in the following manner.<br />

addto finalpicture also currentpicture shifted(n*3cm,0) ;<br />

Since we want to add a few more and don't want them to overlap, we shift them. Therefore we<br />

have to erase the current picture as well as increment the shift counter.<br />

currentpicture := nullpicture ; n := n+1 ;<br />

The drawfill macro is one of the METAFUN macros. Another handy macro is boundingbox. When<br />

used instead of bbox, you don't have to set the margin to zero.<br />

Figure 2.2 The influence of pens on fill.<br />

Bounding boxes A few more details<br />

69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!