●●●● ● ● ● ●●●●●
Figure 5: The PostScriptTrace() function breaks a path into a series of locations on the
boundary of the path. This image shows how the curved petal shape from Table1 can be
converted into a set of points describing the outline of the petal shape.
colour to be used to ﬁll the shape and then a series of <move> and <line> elements describe
the outline of the shape itself. A <summary> element provides information on how many paths
there are in the image, plus bounding box information.
One detail to notice is that the curveto in the PostScript ﬁle has become a series of <line>
elements in the RGML ﬁle. We will discuss this issue further in Section3. The main point
to focus on for now is that the image has become a set of (x, y) locations that describe the
outline of the shape in the image, as illustrated in Figure5.
One reason for choosing PostScript as the original format to focus on is that it is a sophis-
ticated graphics language. PostScript has commands to draw a wide variety of shapes and
PostScript provides advanced facilities to control the placement of shapes and to control
such things as the colors and line styles for ﬁlling and stroking the shapes. This means that
PostScript is capable of describing very complex images; by focusing on PostScript we should
be able to import virtually any vector image no matter how complicated it is.
This is not to say that PostScript is the most sophisticated graphics language—PDF and SVG
are also sophisticated graphics languages with various strengths and weaknesses compared to
PostScript. The point is that, amongst graphics formats, PostScript is one of the sophisticated
PostScript is also a complete programming language. As a simple demonstration of this,
Table3 shows a ﬁle, flower.ps, that contains PostScript code for drawing a simple “ﬂower”
shape, which is shown to the right of the code in Table3.
The important feature of this PostScript code is that it deﬁnes a“macro”that describes how
to draw a petal, then it runs this macro ﬁve times (at ﬁve diﬀerent angles) to produce the
This complexity presents an imposing challenge for us. How can we convert PostScript code
when the code can be extremely complicated? The approach taken by the grImport package
is to use the power of PostScript against itself.
The ﬁrst part of the solution is based on the fact that it is possible to write PostScript code
that runs other PostScript code. The basis for the conversion from an original PostScript ﬁle
to an RGML ﬁle is a set of additional PostScript code that processes the original PostScript
The other part of the solution is based on the fact that it is possible to redeﬁne some of
the core PostScript commands. For example, the grImport PostScript code redeﬁnes the
meaning of the PostScript commands stroke and fill so that, instead of drawing shapes,
these commands print out information about the shapes that would have been drawn.
Table 4 shows a very simpliﬁed version of how the grImport PostScript conversion code
works. This code ﬁrst deﬁnes a macro, printtwo that prints out two values. It also deﬁnes