Debian Codegraph @

doc/libguide/codegen.tex

raw
1 \section{Graphics renderers}
2 \label{sec:renderers}
3
4 All graph output done in \gviz\ goes through a renderer
5 with the type {\tt gvrender\_engine\_t}, used in the call
6 to {\tt gvRender}.
7 In addition to the renderers which are part of the library, an application
8 can provide its own, allowing it to specialize or control the output
9 as necessary. See Section~\ref{sec:plugin} for further details.
10
11 As in the layout phase invoked by {\tt gvLayout}, all control over
12 aspects of rendering are handled via graph attributes. For example,
13 the attribute {\tt outputorder} determines whether all edges are drawn
14 before any nodes, or all nodes are drawn before any edges.
15
16 Before describing the renderer functions in detail, it may be helpful
17 to give an overview of how output is done. Output can be viewed as a hierarchy
18 of document components. At the highest level is the job, representing
19 an output format and target. Bound to a job might be multiple graphs,
20 each embedded in some universal space. Each graph may be partitioned
21 into multiple layers as determined by a graph's {\tt layers} attribute,
22 if any. Each layer may be divided into a 2-dimensional array
23 of pages. A page will then contain nodes, edges, and clusters. Each of
24 these may contain an HTML anchor.
25 During rendering, each component
26 is reflected in paired calls to its corresponding {\tt begin\_...} and
27 {\tt end\_...} functions. The layer and anchor components are omitted if
28 there is only a single layer or the enclosing component has no browser
29 information.
30
31 Figure~\ref{fig:codegen} lists the names and type signatures of
32 the fields of {\tt gv\_render\_engine\_t}, which are used to emit the components
33 described above.\footnote{Any types mentioned in this section are
34 either described in this section or in Appendix~\ref{sec:types}.}
35 All of the functions take a {\tt GVJ\_t*} value, which
36 contains various information about the current rendering, such as the
37 output stream, if any, or the device size and resolution.
38 Section~\ref{sec:gvjfields} describes this data structure.
39
40 Most of the functions handle the nested graph structure.
41 All graphics output is handled by the {\tt textpara}, {\tt ellipse},
42 {\tt polygon}, {\tt beziercurve}, and {\tt polyline} functions.
43 The relevant drawing information such as color and pen style
44 is available through the {\tt obj} field of the
45 {\tt GVJ\_t*} parameter.
46 This is described in Section~\ref{sec:obj}.
47 Font information is passed with the text.
48
49 We note that, in \gviz, each node, edge or cluster in a graph
50 has a unique {\tt id}
51 field, which can be used as a key for storing and accessing the object.
52
53 \begin{figure*}[htb]
54 \centering
55 \begin{tabular}{|l|} \hline
56 void (*begin\_job) (GVJ\_t*); \\
57 void (*end\_job) (GVJ\_t*); \\
58 void (*begin\_graph) (GVJ\_t*); \\
59 void (*end\_graph) (GVJ\_t*); \\
60 void (*begin\_layer) (GVJ\_t*, char*, int, int); \\
61 void (*end\_layer) (GVJ\_t*); \\
62 void (*begin\_page) (GVJ\_t*); \\
63 void (*end\_page) (GVJ\_t*); \\
64 void (*begin\_cluster) (GVJ\_t*, char*, long); \\
65 void (*end\_cluster) (GVJ\_t*); \\
66 void (*begin\_nodes) (GVJ\_t*); \\
67 void (*end\_nodes) (GVJ\_t*); \\
68 void (*begin\_edges) (GVJ\_t*); \\
69 void (*end\_edges) (GVJ\_t*); \\
70 void (*begin\_node) (GVJ\_t*, char*, long); \\
71 void (*end\_node) (GVJ\_t*); \\
72 void (*begin\_edge) (GVJ\_t*, char*, bool, char*, long); \\
73 void (*end\_edge) (GVJ\_t*); \\
74 void (*begin\_anchor) (GVJ\_t*, char*, char*, char*); \\
75 void (*end\_anchor) (GVJ\_t*); \\
76 void (*textpara) (GVJ\_t*, pointf, textpara\_t*); \\
77 void (*resolve\_color) (GVJ\_t*, gvcolor\_t*); \\
78 void (*ellipse) (GVJ\_t*, pointf*, int); \\
79 void (*polygon) (GVJ\_t*, pointf*, int, int); \\
80 void (*beziercurve) (GVJ\_t*, pointf*, int, int, int, int); \\
81 void (*polyline) (GVJ\_t*, pointf*, int); \\
82 void (*comment) (GVJ\_t*, char*); \\ \hline
83 \end{tabular}
84 \caption{Interface for a renderer}
85 \label{fig:codegen}
86 \end{figure*}
87 In the following, we describe the functions in more detail, though
88 many are self-explanatory.
89 All positions and sizes are in points.
90
91 \begin{description}
92 \item[{\tt begin\_job(job)}]
93 Called at the beginning of all graphics output for a graph,
94 which may entail drawing multiple layers and multiple pages.
95 \item[{\tt end\_job(job)}]
96 Called at the end of all graphics output for graph. The output stream
97 is still open, so the renderer can append any final information
98 to the output.
99 \item[{\tt begin\_graph(job)}]
100 Called at the beginning of drawing a graph.
101 The actual graph is available as {\tt job->obj->u.g}.
102 \item[{\tt end\_graph(job)}]
103 Called when the drawing of a graph is complete.
104 \item[{\tt begin\_layer(job,layerName,n,nLayers)}]
105 Called at the beginning of each layer, only if ${\tt nLayers} > 0$.
106 The {\tt layerName} parameter is the logical layer name given in
107 the {\tt layers} attribute. The layer has index {\tt n} out of
108 {\tt nLayers}, starting from 0.
109 \item[{\tt end\_layer(job)}]
110 Called at the end of drawing the current layer.
111 \item[{\tt begin\_page(job)}]
112 Called at the beginning of a new output page.
113 A page will contain a rectangular portion of the drawing of the graph.
114 The value {\tt job->pageOffset} gives the lower left corner of the
115 rectangle in layout coordinates.
116 The point {\tt job->pagesArrayElem} is the index of the page in the array of
117 pages, with the page in the lower left corner indexed by (0,0).
118 The value {\tt job->zoom} provides a scale factor by which the drawing should
119 be scaled. The value {\tt job->rotation}, if non-zero, indicates that the
120 output should be rotated by $90^{\circ}$ counterclockwise.
121 \item[{\tt end\_page(job)}]
122 Called when the drawing of a current page is complete.
123 \item[{\tt begin\_cluster(job)}]
124 Called at the beginning of drawing a cluster subgraph.
125 The actual cluster is available as {\tt job->obj->u.sg}.
126 \item[{\tt end\_cluster(job)}]
127 Called at the end of drawing the current cluster subgraph.
128 \item[{\tt begin\_nodes(job)}]
129 Called at the beginning of drawing the nodes on the current page.
130 Only called if the graph attribute {\tt outputorder} was set to a non-default
131 value.
132 \item[{\tt end\_nodes(job)}]
133 Called when all nodes on a page have been drawn.
134 Only called if the graph attribute {\tt outputorder} was set to a non-default
135 value.
136 \item[{\tt begin\_edges(job)}]
137 Called at the beginning of drawing the edges on the current page.
138 Only called if the graph attribute {\tt outputorder} was set to a non-default
139 value.
140 \item[{\tt end\_edges()}]
141 Called when all edges on the current page are drawn.
142 Only called if the graph attribute {\tt outputorder} was set to a non-default
143 value.
144 \item[{\tt begin\_node(job)}]
145 Called at the start of drawing a node.
146 The actual node is available as {\tt job->obj->u.n}.
147 \item[{\tt end\_node(job)}]
148 Called at the end of drawing the current node.
149 \item[{\tt begin\_edge(job)}]
150 Called at the start of drawing an edge.
151 The actual edge is available as {\tt job->obj->u.e}.
152 \item[{\tt end\_edge(job)}]
153 Called at the end of drawing the current edge.
154 \item[{\tt begin\_anchor(job,href,tooltip,target)}]
155 Called at the start of an anchor context associated
156 with the current node, edge, or graph, or its label, assuming the graph
157 object or its label has a {\tt URL} or {\tt href} attribute. The
158 {\tt href} parameter gives the associated href, while
159 {\tt tooltip} and {\tt target} supply any tooltip or target information.
160 If the object has no tooltip, its label will be used.
161 If the object has no target attribute, this parameter will be {\tt NULL}.
162
163 If the anchor information is attached to a graph object,
164 the {\tt begin\_anchor} and {\tt end\_anchor} calls
165 enclose the {\tt begin\_...} and {\tt end\_...} calls on the object.
166 If the anchor information is attached to part of an object's label,
167 the {\tt begin\_anchor} and {\tt end\_anchor} calls
168 enclose the rendering of that part of the label plus any subparts.
169 \item[{\tt end\_anchor(job)}]
170 Called at the end of the current anchor context.
171 \item[{\tt textpara(job, p, txt)}]
172 Draw text at point {\tt p} using the specified font and fontsize and color.
173 The {\tt txt} argument provides the text string {\tt txt->str},
174 stored in UTF-8,
175 a calculated width of the string {\tt txt->width} and
176 the horizontal alignment {\tt txt->just} of the string in relation to {\tt p}.
177 The values {\tt txt->fontname} and {\tt txt->fontname} give the desired
178 font name and font size, the latter in points.
179
180 The base line of the text is given by {\tt p.y}. The interpretation
181 of {\tt p.x} depends upon the value of {\tt txt->just}. Basically,
182 {\tt p.x} provides the anchor point for the alignment.
183 \begin{center}
184 \begin{tabular}{ll}
185 {\tt txt->just} & {\tt p.x} \\ \hline
186 {\tt 'n'} & Center of text \\
187 {\tt 'l'} & Left edge of text \\
188 {\tt 'r'} & Right edge of text \\
189 \end{tabular}
190 \end{center}
191 The leftmost x coordinate of the text, the parameter most graphics
192 systems use for text placement, is given by
193 {\tt p.x + {\em j} * txt->width}, where {\em j} is 0.0 (-0.5,-1.0)
194 if {\tt txt->just} is {\tt 'l'}({\tt 'n'},{\tt 'r'}), respectively.
195 This representation allows the renderer to accurately compute
196 the point for text placement that is appropriate for its format, as
197 well as use its own mechanism for computing the width of the string.
198 \item[{\tt resolve\_color(job, color)}]
199 Resolve a color. The {\tt color} parameter points to a color representation
200 of some particular type. The renderer can use this information to resolve
201 the color to a representation appropriate for it. See Section~\ref{sec:color}
202 for more details.
203 \item[{\tt ellipse(job, ps, filled)}]
204 Draw an ellipse with center at {\tt ps[0]}, with horizontal and vertical
205 half-axes {\tt ps[1].x - ps[0].x} and {\tt ps[1].y - ps[0].y}
206 using the current pen color and line style.
207 If {\tt filled} is non-zero, the ellipse should be filled with the current
208 fill color.
209 \item[{\tt polygon(job, A, n, filled)}]
210 Draw a polygon with the {\tt n} vertices given in the array {\tt A},
211 using the current pen color and line style.
212 If {\tt filled} is non-zero, the polygon should be filled with the current
213 fill color.
214 \item[{\tt beziercurve(job, A, n, arrow\_at\_start, arrow\_at\_end, filled)}]
215 Draw a B-spline with the {\tt n} control points given in {\tt A}. This will
216 consist of $(n - 1)/3$ cubic Bezier curves. The spline should be drawn
217 using the current pen color and line style.
218 If the renderer has specified that it does not want to do its own arrowheads
219 (cf. Section~\ref{sec:plugin}),
220 the parameters
221 {\tt arrow\_at\_start} and {\tt arrow\_at\_end}
222 will both be 0. Otherwise, if {\tt arrow\_at\_start} ({\tt arrow\_at\_end})
223 is true, the function should draw an arrowhead at the
224 first (last) point of {\tt A}.
225 If {\tt filled} is non-zero, the bezier should be filled with the current
226 fill color.
227 \item[{\tt polyline(job,A,n)}]
228 Draw a polyline with the {\tt n} vertices given in the array {\tt A},
229 using the current pen color and line style.
230 \item[{\tt comment(job, text)}]
231 Emit text comments related to a graph object.
232 For nodes, calls will pass the node's name and any {\tt comment}
233 attribute attached to the node.
234 For edges, calls will pass a string description of the edge
235 and any {\tt comment} attribute attached to the edge.
236 For graphs and clusters, a call will pass a
237 any {\tt comment} attribute attached to the object.
238 \end{description}
239
240 Although access to the graph object being drawn is available through
241 the {\tt GVJ\_t} value, a renderer
242 can often perform its role by just implementing the basic graphics operations.
243 It need have no information about graphs or the related \gviz\ data structures.
244 Indeed, a particular renderer need not define any particular rendering
245 function, since a given entry point will only be called if non-NULL.
246
247 \subsection{The {\tt GVJ\_t} data structure}
248 \label{sec:gvjfields}
249 We now describe some of the more important fields in the {\tt GVJ\_t} structure,
250 concentrating on those regarding output. There are additional fields
251 relevant to input and GUIs.
252 \begin{description}
253 \item[{\tt common}]
254 This points to various information valid throughout the duration
255 of the application using \gviz. In particular,
256 the {\tt common->info} contains \gviz\ version
257 information, as described in Section~\ref{sec:info}.
258 \item[{\tt output\_file}]
259 The {\tt FILE*} value for an open stream on
260 which the output should be written, if relevant.
261 \item[{\tt pagesArraySize}]
262 The size of the array of pages in which the graph will be output,
263 given as a {\tt point}.
264 If {\tt pagesArraySize.x} or {\tt pagesArraySize.y} is greater than one,
265 this indicates that a
266 page size was set and the graph drawing is too large to be printed on a
267 single page.
268 Page (0,0) is the page containing the bottom, lefthand corner
269 of the graph drawing; page (1,0) will contain that part of the graph
270 drawing to the right of page (0,0); etc.
271 \item[{\tt bb}]
272 The bounding box of the layout in the universal space
273 in points. It has type {\tt boxf}.
274 \item[{\tt boundingBox}]
275 The bounding box of the layout in the device space in
276 device coordinates. It has type {\tt box}.
277 \item[{\tt layerNum}]
278 The current layer number.
279 \item[{\tt numLayers}]
280 The total number of layers.
281 \item[{\tt pagesArrayElem}]
282 The row and column of the current page.
283 \item[{\tt pageOffset}]
284 The origin of the current page in the universal space in points.
285 \item[{\tt zoom}]
286 Factor by which the output should be scaled.
287 \item[{\tt rotation}]
288 Indicates whether or not the rendering should be rotated.
289 \item[{\tt obj}]
290 Information related to the current object being rendered.
291 This is a pointer of a value of type {\tt obj\_state\_t}.
292 See Section~\ref{sec:obj} for more details.
293 \end{description}
294
295 \subsection{Inside the {\tt obj\_state\_t} data structure}
296 \label{sec:obj}
297 A value of type {\tt obj\_state\_t} encapsulates various information
298 pertaining to the current object being rendered. In particular, it
299 provides access to the current object, and provides the style
300 information for any rendering operation.
301 Figure~\ref{fig:obj} notes some of the more useful fields in the
302 structure.
303 \begin{figure*}[htb]
304 \centering
305 \begin{tabular}{|l|} \hline
306 obj\_type type; \\
307 union \{ \\
308 \multicolumn{1}{|l|}{\hspace{2em}graph\_t *g;} \\
309 \multicolumn{1}{|l|}{\hspace{2em}graph\_t *sg;} \\
310 \multicolumn{1}{|l|}{\hspace{2em}node\_t *n;} \\
311 \multicolumn{1}{|l|}{\hspace{2em}edge\_t *e;} \\
312 \} u; \\
313 gvcolor\_t pencolor; \\
314 gvcolor\_t fillcolor; \\
315 pen\_type pen; \\
316 double penwidth; \\
317 char *url; \\
318 char *tailurl; \\
319 char *headurl; \\
320 char *tooltip; \\
321 char *tailtooltip; \\
322 char *headtooltip; \\
323 char *target; \\
324 char *tailtarget; \\
325 char *headtarget; \\
326 \hline
327 \end{tabular}
328 \caption{Some fields in {\tt obj\_state\_t}}
329 \label{fig:obj}
330 \end{figure*}
331
332 \begin{description}
333 \item[{\tt type} and {\tt u}]
334 The {\tt type} field indicates what kind of graph object is currently
335 being rendered. The possible values are
336 {\tt ROOTGRAPH\_OBJTYPE}, {\tt CLUSTER\_OBJTYPE},
337 {\tt NODE\_OBJTYPE} and {\tt EDGE\_OBJTYPE},
338 indicating the root graph, a cluster subgraph, a node and an edge,
339 respectively.
340 A pointer to the actual object is available via the subfields
341 {\tt u.g}, {\tt u.sg}, {\tt u.n} and {\tt u.e}, respectively,
342 of the union {\tt u}.
343 \item[{\tt pencolor}]
344 The {\tt gvcolor\_t} value indicating the color used to draw lines,
345 curves and text.
346 \item[{\tt pen}]
347 The style of pen to be used. The possible values are
348 {\tt PEN\_NONE}, {\tt PEN\_DOTTED},
349 {\tt PEN\_DASHED} and {\tt PEN\_SOLID}.
350 \item[{\tt penwidth}]
351 The size of the pen, in points.
352 Note that, by convention, a value of 0 indicates using the smallest
353 width supported by the output format.
354 \item[{\tt fillcolor}]
355 The {\tt gvcolor\_t} value indicating the color used to fill
356 closed regions.
357 \end{description}
358 Note that font information is delivered as part of the
359 {\tt textpara\_t} value passed to the {\tt textpara} function.
360
361 As for the url, tooltip and target fields, these will point to
362 the associated attribute value of the current graph object,
363 assuming it is defined and that the renderer support map, tooltips,
364 and targets, respectively (cf. Section~\ref{sec:plugin}).
365
366 \subsection{Color information}
367 \label{sec:color}
368
369 There are five ways in which a color can be specified in \gviz:
370 RGB + alpha, HSV + alpha, CYMK, color index, and color name.
371 In addition, the RGB + alpha values can be stored as bytes, words
372 or doubles.
373
374 A color value in \gviz\ has the type {\tt gvcolor\_t}, containing
375 two fields: a union {\tt u}, containing the color data, and the
376 {\tt type} field, indicating which color representation is used in
377 the union. Table~\ref{table:color} describes the allowed color types,
378 and the associated union field.
379 \begin{table*}[htb]
380 \centering
381 \begin{tabular}[t]{|l|p{3.5in}|l|} \hline
382 \multicolumn{1}{|c|}{Type} & \multicolumn{1}{c|}{Description} & \multicolumn{1}{c|}{Field}\\ \hline
383 {\tt RGBA\_BYTE} & RGB + alpha format represented as 4 bytes from 0 to 255 & {\tt u.rgba} \\
384 {\tt RGBA\_WORD} & RGB + alpha format represented as 4 words from 0 to 65535 & {\tt u.rrggbbaa} \\
385 {\tt RGBA\_DOUBLE} & RGB + alpha format represented as 4 doubles from 0 to 1 & {\tt u.RGBA} \\
386 {\tt HSVA\_DOUBLE} & HSV + alpha format represented as 4 doubles from 0 to 1 & {\tt u.HSVA} \\
387 {\tt CYMK\_BYTE} & CYMK format represented as 4 bytes from 0 to 255 & {\tt u.cymk} \\
388 {\tt COLOR\_STRING} & text name & {\tt u.string} \\
389 {\tt COLOR\_INDEX} & integer index & {\tt u.index} \\
390 \hline
391 \end{tabular}
392 \caption{Color type representations}
393 \label{table:color}
394 \end{table*}
395
396 Before a color is used in rendering, \gviz\ will process a color description
397 provided by the input graph into a form desired by the renderer.
398 This is three step procedure. First, \gviz\ will see if the color matches
399 the renderer's known colors, if any.
400 If so, the color representation is {\tt COLOR\_STRING}. Otherwise, the
401 library will convert the input color description into the renderer's
402 preferred format. Finally, if the
403 renderer also provides a {\tt resolve\_color} function, \gviz\ will then
404 call that function, passing a pointer to the current color value. The
405 renderer then has the opportunity to adjust the value, or convert it
406 into another format. In a typical case, if a renderer uses a color map,
407 it may request RGB values as input, and then store an associated
408 color map index using the {\tt COLOR\_INDEX} format. If the renderer
409 does a conversion to another color type, it must
410 reset the {\tt type} field to indicate this.
411 It is this last representation which will be passed to the renderer's
412 drawing routines.
413 The renderer's known colors and preferred color format are
414 described in Section~\ref{sec:plugin} below.
415