8 .. sectionauthor:: Jörg Lehmann <joergl@users.sourceforge.net>
12 contains the class ``canvas.clip`` which allows clipping of the output.
18 This is the basic class of the canvas module. Instances of this class collect
19 visual elements like paths, other canvases, TeX or LaTeX elements. A canvas may
20 also be embedded in another one using its ``insert`` method. This may be useful
21 when you want to apply a transformation on a whole set of operations.
23 .. class:: canvas(attrs=[], texrunner=None)
25 Construct a new canvas, applying the given *attrs*, which can be instances of
26 :class:`trafo.trafo`, :class:`canvas.clip`, :class:`style.strokestyle` or
27 :class:`style.fillstyle`. The *texrunner* argument can be used to specify the
28 texrunner instance used for the :meth:`text` method of the canvas. If not
29 specified, it defaults to *text.defaulttexrunner*.
31 Paths can be drawn on the canvas using one of the following methods:
34 .. method:: canvas.draw(path, attrs)
36 Draws *path* on the canvas applying the given *attrs*. Depending on the
37 *attrs* the path will be filled, stroked, ornamented, or a combination
38 thereof. For the common first two cases the following two convenience
39 functions are provided.
42 .. method:: canvas.fill(path, attrs=[])
44 Fills the given *path* on the canvas applying the given *attrs*.
47 .. method:: canvas.stroke(path, attrs=[])
49 Strokes the given *path* on the canvas applying the given *attrs*.
51 Arbitrary allowed elements like other :class:`canvas` instances can be inserted
55 .. method:: canvas.insert(item, attrs=[])
57 Inserts an instance of :class:`base.canvasitem` into the canvas. If *attrs* are
58 present, *item* is inserted into a new :class:`canvas` instance with *attrs*
59 as arguments passed to its constructor. Then this :class:`canvas` instance
60 is inserted itself into the canvas.
62 Text output on the canvas is possible using
65 .. method:: canvas.text(x, y, text, attrs=[])
67 Inserts *text* at position (*x*, *y*) into the canvas applying *attrs*. This is
68 a shortcut for ``insert(texrunner.text(x, y, text, attrs))``.
70 To group drawing operations, layers can be used:
73 .. method:: canvas.layer( name, above=None, below=None)
75 This method creates or gets a layer with name *name*.
77 A layer is a canvas itself and can be used to combine drawing operations for
78 ordering purposes, i.e., what is above and below each other. The layer name
79 *name* is a dotted string, where dots are used to form a hierarchy of layer
80 groups. When inserting a layer, it is put on top of its layer group except
81 when another layer instance of this group is specified by means of the
82 parameters *above* or *below*.
85 The :class:`canvas` class provides access to the total geometrical size of its
89 .. method:: canvas.bbox()
91 Returns the bounding box enclosing all elements of the canvas (see Sect. :mod:`bbox`).
93 A canvas also allows to set its TeX runner:
96 .. method:: canvas.settexrunner(texrunner)
98 Sets a new *texrunner* for the canvas.
100 The contents of the canvas can be written to a file using the following
101 convenience methods, which wrap the canvas into a single page document.
104 .. method:: canvas.writeEPSfile(file, *args, **kwargs)
106 Writes the canvas to *file* using the EPS format. *file* either has to provide a
107 write method or it is used as a string containing the filename (the extension
108 ``.eps`` is appended automatically, if it is not present). This method
109 constructs a single page document, passing *args* and *kwargs* to the
110 :class:`document.page` constructor and calls the :meth:`writeEPSfile` method
111 of this :class:`document.document` instance passing the *file*.
114 .. method:: canvas.writePSfile(file, *args, **kwargs)
116 Similar to :meth:`writeEPSfile` but using the PS format.
119 .. method:: canvas.writePDFfile(file, *args, **kwargs)
121 Similar to :meth:`writeEPSfile` but using the PDF format.
124 .. method:: canvas.writetofile(filename, *args, **kwargs)
126 Determine the file type (EPS, PS, or PDF) from the file extension of *filename*
127 and call the corresponding write method with the given arguments *arg* and
131 .. method:: canvas.pipeGS(device, resolution=100, gs="gs", gsoptions=[], textalphabits=4, graphicsalphabits=4, ciecolor=False, input="eps", **kwargs)
133 This method pipes the content of a canvas to the ghostscript interpreter
134 to generate other output formats. The output is returned by means of a
135 python BytesIO object. *device* specifies a ghostscript output device by
136 a string. Depending on the ghostscript configuration ``"png16"``,
137 ``"png16m"``, ``"png256"``, ``"png48"``, ``"pngalpha"``, ``"pnggray"``,
138 ``"pngmono"``, ``"jpeg"``, and ``"jpeggray"`` might be available among
139 others. See the output of ``gs --help`` and the ghostscript documentation
140 for more information.
142 *resolution* specifies the resolution in dpi (dots per inch). *gs* is the
143 name of the ghostscript executable. *gsoptions* is a list of additional
144 options passed to the ghostscript interpreter. *textalphabits* and
145 *graphicsalphabits* are convenient parameters to set the ``TextAlphaBits``
146 and ``GraphicsAlphaBits`` options of ghostscript. The addition of these
147 options can be skipped by setting their values to ``None``. *ciecolor* adds
148 the ``-dUseCIEColor`` flag to improve the CMYK to RGB color conversion.
149 *input* can be either ``"eps"`` or ``"pdf"`` to select the input type to be
150 passed to ghostscript (note slightly different features available in the
151 different input types regarding e.g. :mod:`epsfile` inclusion and
154 *kwargs* are passed to the :meth:`writeEPSfile` method (not counting the *file*
155 parameter), which is used to generate the input for ghostscript. By that you
156 gain access to the :class:`document.page` constructor arguments.
158 .. method:: canvas.writeGSfile(filename=None, device=None, **kwargs)
160 This method is similar to pipeGS, but the content is written into the file
161 *filename*. If filename is None it is auto-guessed from the script name. If
162 filename is "-", the output is written to stdout. In both cases, a device
163 needs to be specified to define the format (and the file suffix in case the
164 filename is created from the script name).
166 If device is None, but a filename with suffix is given, PNG files will
167 be written using the png16m device and JPG files using the jpeg device.
169 All other arguments are identical to those of the :meth:`canvas.pipeGS`.
171 For more information about the possible arguments of the :class:`document.page`
172 constructor, we refer to Sect. :mod:`document`.
176 ---------------------
178 In addition the canvas module contains the class ``canvas.clip`` which allows for
179 clipping of the output by passing a clipping instance to the attrs parameter of
180 the canvas constructor.