Debian Codegraph @

doc/libguide/plugins.tex

raw
1 \section{Adding Plug-ins}
2 \label{sec:pluginmain}
3
4 The \gviz\ framework allows the programmer to use plug-ins to
5 extend the system in several ways. For example, the programmer can add new
6 graph layout engines along with new renderers and their related
7 functions. Table~\ref{table:apis} describes the plug-in APIs
8 supported by \gviz.
9 \begin{table*}[htbp]\footnotesize
10 \centering
11 \begin{tabular}[t]{|l|l|c|p{2.0in}|} \hline
12 \multicolumn{1}{|c|}{Kind} & \multicolumn{1}{|c|}{Functions} & \multicolumn{1}{|c|}{Features} & \multicolumn{1}{c|}{Description} \\ \hline
13 {\tt API\_render} & {\tt gvrender\_engine\_t} & {\tt gvrender\_features\_t} & Functions for rendering a graph \\
14 {\tt API\_device} & {\tt gvdevice\_engine\_t} & - & Functions for initializing and terminating a device \\
15 {\tt API\_loadimage} & {\tt gvloadimage\_engine\_t} & - & Functions for converting from one image format to another \\
16 {\tt API\_layout} & {\tt gvlayout\_engine\_t} & {\tt gvlayout\_features\_t} & Functions for laying out a graph \\
17 {\tt API\_textlayout} & {\tt gvtextlayout\_engine\_t} & - & Functions for resolving font names and text size \\
18 \hline
19 \end{tabular}
20 \caption{Plug-in API types}
21 \label{table:apis}
22 \end{table*}
23 Each plug-in is defined by an engine structure containing its function
24 entry points, and a features structure specifying features supported
25 by the plug-in. Thus, a renderer is defined by values of type
26 {\tt gvrender\_engine\_t} and {\tt gvrender\_features\_t}.
27
28 Once all of the plug-ins of a given kind are defined, they should be
29 gathered into a 0-terminated array of element type {\tt gvplugin\_installed\_t},
30 whose fields are shown in Figure~\ref{fig:installed}.
31 \begin{figure*}[htb]
32 \centering
33 \begin{tabular}{|l|} \hline
34 int id; \\
35 char *type; \\
36 int quality; \\
37 void *engine; \\
38 void *features; \\
39 \hline
40 \end{tabular}
41 \caption{Plug-in fields}
42 \label{fig:installed}
43 \end{figure*}
44 The fields have the following meanings.
45 \begin{description}
46 \item[{\tt id}]
47 Identifier for a given plug-in within a given package and with
48 a given API kind. Note that the {\tt id} need only be unique within
49 its plug-in package, as these packages are assumed to be independent.
50 \item[{\tt type}]
51 Name for a given plug-in, used during plug-in lookup.
52 \item[{\tt quality}]
53 An arbitrary integer used for ordering plug-ins with the same {\tt type}.
54 Plug-ins with larger values will be chosen before plug-ins with smaller
55 values.
56 \item[{\tt engine}]
57 Points to the related engine structure.
58 \item[{\tt features}]
59 Points to the related features structure.
60 \end{description}
61
62 As an example, suppose we wish to add various renderers for
63 bitmap output. A collection of these might be combined as follows.
64 \begin{verbatim}
65 gvplugin_installed_t render_bitmap_types[] = {
66 {0, "jpg", 1, &jpg_engine, &jpg_features},
67 {0, "jpeg", 1, &jpg_engine, &jpg_features},
68 {1, "png", 1, &png_engine, &png_features},
69 {2, "gif", 1, &gif_engine, &gif_features},
70 {0, NULL, 0, NULL, NULL}
71 };
72 \end{verbatim}
73 Note that this allows {\tt "jpg"} and {\tt "jpeg"} to refer to the
74 same renderers.
75 For the plug-in kinds without a features structure, the feature pointer in its
76 {\tt gvplugin\_installed\_t} should be NULL.
77
78 All of the plug-ins of all API kinds should then be
79 gathered into a 0-terminated array of element type {\tt gvplugin\_api\_t}.
80 For each element,
81 the first field indicates the kind of API, and the second points to the
82 array of plug-ins described above ({\tt gvplugin\_installed\_t}).
83
84 Continuing our example, if we have supplied, in addition
85 to the bitmap rendering plug-ins, plug-ins to render VRML, and
86 plug-ins to load images, we would define
87 \begin{verbatim}
88 gvplugin_api_t apis[] = {
89 {API_render, &render_bitmap_types},
90 {API_render, &render_vrml_types},
91 {API_loadimage, &loadimage_bitmap_types},
92 {0, 0},
93 };
94 \end{verbatim}
95 Here {\tt render\_vrml\_types} and {\tt render\_vrml\_types}
96 are also 0-terminated arrays of element type {\tt gvplugin\_installed\_t}.
97 Note that there can be multiple items of the same API kind.
98
99 A final definition is used to attach a name to the package of
100 all the plug-ins. This is done using a {\tt gvplugin\_library\_t}
101 structure. Its first field is a {\tt char*} giving the name of the
102 package. The second field is a {\tt gvplugin\_api\_t*} pointing to
103 the array described above. The structure itself must be named
104 {\tt gvplugin\_{\em name}\_LTX\_library}, where {\em name} is the
105 name of the package as defined in the first field.
106
107 For example, if we have decided to call our package {\tt "bitmap"},
108 we could use the following definition:
109 \begin{verbatim}
110 gvplugin_library_t gvplugin_bitmap_LTX_library = { "bitmap", apis };
111 \end{verbatim}
112
113 To finish the installation of the package, it is necessary to create
114 a dynamic library containing the {\tt gvplugin\_library\_t} value and
115 all of the functions and data referred by it, either directly or
116 indirectly. The library must be named {\tt gvplugin\_{\em name}},
117 where again {\em name} is the name of the package. The actual filename
118 of the library will be system-dependent. For example, on Linux systems,
119 our library {\tt gvplugin\_bitmap} would have filename
120 {\tt libgvplugin\_bitmap.so.3}.
121
122 In most cases, \gviz\ is built with
123 a plug-in version number. This number must be included in the library's
124 filename, following any system-dependent conventions.
125 The number is given as the value of {\tt plugins} in the file
126 {\tt libgvc.pc}, which can be found in the directory {\tt lib/pkgconfig}
127 where \gviz\ was installed.
128 In our example, the ``3'' in the library's filename gives the version number.
129
130 Finally, the library must be installed in the \gviz\ library directory,
131 and {\tt dot -c} must be run to add the package to the \gviz\ configuration.
132 Note that both of these steps typically assume that one has installer
133 privileges.\footnote{
134 Normally, for builds intended for local installation {\tt dot -c} is run
135 during {\tt make install}.
136 It may be necessary to run this manually if cross-compiling or otherwise
137 manually moving binaries to a different system.
138 }
139
140 In the remainder of this section, we shall look at the first
141 three types of plug-in APIs in more detail.
142
143 \subsection{Writing a renderer plug-in}
144 \label{sec:plugin}
145 A renderer plug-in has two parts.
146 The first consists of a structure of type {\tt gvrender\_engine\_t}
147 defining the renderer's actions, as described in Section~\ref{sec:renderers}.
148 Recall that any field may contain a NULL pointer.
149
150 For the second part, the programmer must provide a structure of type
151 {\tt gvrender\_features\_t}. This record provides \gviz\ with
152 information about the renderer.
153 Figure~\ref{fig:features} list the fields involved.
154 \begin{figure*}[htbp]
155 \centering
156 \begin{tabular}{|l|} \hline
157 int flags; \\
158 double default\_margin; \\
159 double default\_pad; \\
160 pointf default\_pagesize; \\
161 pointf default\_dpi; \\
162 char **knowncolors; \\
163 int sz\_knowncolors; \\
164 color\_type\_t color\_type; \\
165 char *device; \\
166 char *loadimage\_target; \\
167 \hline
168 \end{tabular}
169 \caption{Features of a renderer}
170 \label{fig:features}
171 \end{figure*}
172 Some of the default values may be overridden by the input graph.
173
174 We now describe the fields in detail.
175 \begin{description}
176 \item[{\tt flags}]
177 Bit-wise of {\tt or} flags indicating properties of the renderer.
178 These flags are described in Table~\ref{table:renderflags}.
179 \item[{\tt default\_margin}]
180 Default margin size in points. This is the amount
181 of space left around the drawing.
182 \item[{\tt default\_pad}]
183 Default pad size in points. This is the amount
184 by which the graph is inset within the drawing region. Note that the
185 drawing region may be filled with a background color.
186 \item[{\tt default\_pagesize}]
187 Default page size size in points. For example, an 8.5 by 11 inch letter-sized
188 page would have a {\tt default\_pagesize} of 612 by 792.
189 \item[{\tt default\_dpi}]
190 Default resolution, in pixels per inch. Note that the x and y values
191 may be different to support non-square pixels.
192 \item[{\tt knowncolors}]
193 An array of character pointers giving a lexicographically ordered
194 \footnote{The ordering must be done byte-wise using
195 the {\tt LANG=C} locale for
196 byte comparison.} list of the color names supported by the renderer.
197 \item[{\tt sz\_knowncolors}]
198 The number of items in the {\tt knowncolors} array.
199 \item[{\tt color\_type}]
200 The preferred representation for colors. See Section~\ref{sec:color}.
201 \item[{\tt device}]
202 The name of a device, if any, associated with the renderer. For example,
203 a renderer using GTK for output might specify {\tt "gtk"} as its device.
204 If a name is given, the library will look for a plug-in of type API\_device
205 with that name, and use the associated functions to initialize and terminate
206 the device. See Section~\ref{sec:device}.
207 \item[{\tt loadimage\_target}]
208 The name of the preferred type of image format for the renderer.
209 When a user-supplied image is given, the library will attempt to find
210 a function that will convert the image from its original format to
211 the renderer's preferred one. A user-defined renderer may need to
212 provide, as additional plug-ins, its own functions for handling the conversion.
213 \end{description}
214
215 \begin{table*}[htbp]\footnotesize
216 \centering
217 \begin{tabular}[t]{|l|p{3.5in}|} \hline
218 \multicolumn{1}{|c|}{Flag} & \multicolumn{1}{c|}{Description} \\ \hline
219 {\tt GVRENDER\_DOES\_ARROWS} & Built-in arrowheads on splines \\
220 {\tt GVRENDER\_DOES\_LAYERS} & Supports graph layers \\
221 {\tt GVRENDER\_Y\_GOES\_DOWN} & Output coordinate system has the origin in
222 the upper left corner \\
223 {\tt GVRENDER\_DOES\_TRANSFORM} & Can handle transformation (scaling,
224 translation, rotation) from universal
225 to device coordinates. If false, the library will do the transformation
226 before passing any coordinates to the renderer \\
227 {\tt GVRENDER\_DOES\_LABELS} & Wants an object's label, if any, provided
228 as text during rendering \\
229 {\tt GVRENDER\_DOES\_MAPS} & Supports regions to which URLs can be attached.
230 If true, URLs are provided to the renderer,
231 either as part of the {\tt job->obj} or via the renderer's {\tt begin\_anchor}
232 function \\
233 {\tt GVRENDER\_DOES\_MAP\_RECTANGLE} & Rectangular regions can be mapped \\
234 {\tt GVRENDER\_DOES\_MAP\_CIRCLE} & Circular regions can be mapped \\
235 {\tt GVRENDER\_DOES\_MAP\_POLYGON} & Polygons can be mapped \\
236 {\tt GVRENDER\_DOES\_MAP\_ELLIPSE} & Ellipses can be mapped \\
237 {\tt GVRENDER\_DOES\_MAP\_BSPLINE} & B-splines can be mapped \\
238 {\tt GVRENDER\_DOES\_TOOLTIPS} & If true, tooltips are provided to the renderer,
239 either as part of the {\tt job->obj} or via the renderer's {\tt begin\_anchor}
240 function \\
241 {\tt GVRENDER\_DOES\_TARGETS} & If true, targets are provided to the renderer,
242 either as part of the {\tt job->obj} or via the renderer's {\tt begin\_anchor}
243 function \\
244 {\tt GVRENDER\_DOES\_Z} & Uses a 3D output model \\
245 {\tt GVRENDER\_NO\_WHITE\_BG} & Does not paint white background, assumes white
246 paper -Tps \\
247 \hline
248 \end{tabular}
249 \caption{Renderer properties}
250 \label{table:renderflags}
251 \end{table*}
252
253 \subsection{Writing a device plug-in}
254 \label{sec:device}
255
256 A device plug-in provides hooks for \gviz\ to handle any
257 device-specific operations needed before and after rendering.
258 The related engine of type {\tt gvdevice\_engine\_t} has 2 entry points:
259 \begin{verbatim}
260 void (*initialize) (GVJ_t*);
261 void (*finalize) (GVJ_t*);
262 \end{verbatim}
263 which are called at the beginning and end of rendering each job.
264 The initialize routine might open a canvas on window system, or
265 set up a new page for printing;
266 the finalize routine might go into an event loop after which it could close
267 the output device.
268
269 \subsection{Writing an image loading plug-in}
270 \label{sec:imageload}
271
272 A image loading plug-in has engine type
273 {\tt gvimageload\_engine\_t} and provides a single entry point which can be
274 used to read in an image,
275 convert the image from one format to another, and write the result.
276 Since the function actually does rendering, it is usually closely tied
277 to a specific renderer plug-in.
278 \begin{verbatim}
279 void (*loadimage) (GVJ_t *job, usershape_t *us, boxf b, bool filled);
280 \end{verbatim}
281 When called, {\tt loadimage} is given the current job, a pointer to the
282 input image {\tt us}, and the bounding box {\tt b} in device coordinates
283 where the image should be written. The boolean {\tt filled} value indicates
284 whether the bounding box should first be filled.
285
286 The {\tt type} value for an image loading plug-in's
287 {\tt gvplugin\_installed\_t} entry should specify the input and output
288 formats it handles. Thus, a plug-in converting JPEG to GIF would be
289 called {\tt "jpeg2gif"}. Since an image loader may well want to read in
290 an image in some format, and then render the image using the same format,
291 it is quite reasonable for the input and output formats to be identical,
292 e.g. {\tt "gif2gif"}.
293
294 Concerning the type {\tt usershape\_t}, its most important fields
295 are shown in Figure~\ref{fig:usershape}.
296 \begin{figure*}[htbp]
297 \centering
298 \begin{tabular}{|l|} \hline
299 char *name; \\
300 FILE *f; \\
301 imagetype\_t type; \\
302 unsigned int x, y; \\
303 unsigned int w, h; \\
304 unsigned int dpi; \\
305 void *data; \\
306 size\_t datasize; \\
307 void (*datafree)(usershape\_t *us); \\
308 \hline
309 \end{tabular}
310 \caption{Fields in {\tt usershape\_t}}
311 \label{fig:usershape}
312 \end{figure*}
313 These fields have the following meanings:
314 \begin{description}
315 \item[{\tt name}]
316 The name of the image.
317 \item[{\tt f}]
318 An open input stream to the image's data. Since the image might
319 be processed multiple times, the application should use a function
320 such as {\tt fseek} to make sure the file pointer points to the
321 beginning of the file.
322 \item[{\tt type}]
323 The format of the image. The formats supported in \gviz\ are
324 {\tt FT\_BMP}, {\tt FT\_GIF}, {\tt FT\_PNG}, {\tt FT\_JPEG}, {\tt FT\_PDF},
325 {\tt FT\_PS} and {\tt FT\_EPS}. The value {\tt FT\_NULL} indicates an
326 unknown image type.
327 \item[{\tt x} and {\tt y}]
328 The coordinates of the lower-left corner of image in image units.
329 This is usually the origin but some images such as those in PostScript
330 format may be translated away from the origin.
331 \item[{\tt w} and {\tt h}]
332 The width and height of image in image units
333 \item[{\tt dpi}]
334 The number of image units per inch
335 \item[{\tt data}, {\tt datasize}, {\tt datafree}]
336 These fields can be used to cache the converted image data so
337 that the file I/O and conversion need only be done once. The
338 data can be stored via {\tt data}, with {\tt datasize} giving the
339 number of bytes used. In this case, the image loading code should
340 store a clean-up handler in {\tt datafree}, which can be called
341 to release any memory allocated.
342
343 If {\tt loadimage} does caching, it can check if {\tt us->data}
344 is NULL. If so, it can read and cache the image. If not, it should
345 check that the {\tt us->datafree} value points to its own {\tt datafree}
346 routing. If not, then some other image loader has cached data there.
347 The {\tt loadimage} function must them call the current {\tt us->datafree}
348 function before caching its own version of the image.
349
350 The code template in Figure~\ref{fig:caching}
351 indicates how caching should be handled.
352 \end{description}
353 \begin{figure}[hbt]
354 \begin{verbatim}
355 if (us->data) {
356 if (us->datafree != my_datafree) {
357 us->datafree(us); /* free incompatible cache data */
358 us->data = NULL;
359 us->datafree = NULL;
360 us->datasize = 0;
361 }
362 }
363
364 if (!us->data) {
365 /* read image data from us->f and convert it;
366 * store the image data into memory pointed to by us->data;
367 * set us->datasize and us->datafree to the appropriate values.
368 */
369 }
370
371 if (us->data) {
372 /* emit the image data in us->data */
373 }
374 \end{verbatim}
375 \caption{Caching converted images}
376 \label{fig:caching}
377 \end{figure}
378