Debian Codegraph @

doc/docbook/src/graphviz_plugins.xml

raw
1 <!-- $Id: -->
2 <chapter id="ch-plugins">
3 <title>Graphviz Plugins</title>
4 <sect1 id="s1-plugins-introduction">
5 <title>Introduction</title>
6 <para>
7 Graphviz plugins are a means of extending the capabilities of the
8 graphviz tools:
9 <command>dot</command>,
10 <command>neato</command>,
11 <command>fdp</command>,
12 <command>twopi</command>, and
13 <command>circo</command>,
14 </para>
15 <para>
16 The initial motivation for the development of plugins was to support
17 alternative renders based on
18 <ulink url="http://cairo.freedesktop.org/Home">
19 <command>cairo</command>
20 </ulink>,
21 while keeping
22 the stable renderer based on
23 <ulink url="http://www.boutell.com/gd">
24 <command>gd</command>
25 </ulink>
26 available.
27 The <command>gd</command> renderer does not support anti-aliased lines
28 (first image).
29 Simply by installing the graphviz-cairo package the -Tpng renderer is
30 substituted with one based on cairo (second image).
31 </para>
32 <para>
33 Use of the newly installed plugin requires no change in the user's command.
34 These images were generated before and after installing
35 <command>graphviz-cairo</command>with the command:
36 <programlisting>
37 echo "digraph G {hello->world}" | dot -Tpng > hello.png
38 </programlisting>
39 </para>
40 <para>
41 <inlinemediaobject>
42 <imageobject role="html">
43 <imagedata format="PNG" fileref="imgs/hello_gd.png"/>
44 </imageobject>
45 <imageobject role="fo">
46 <imagedata format="PNG" fileref="imgs/hello_gd.png" scale="72" />
47 </imageobject>
48 </inlinemediaobject>
49 <inlinemediaobject>
50 <imageobject role="html">
51 <imagedata format="PNG" fileref="imgs/hello_cairo.png"/>
52 </imageobject>
53 <imageobject role="fo">
54 <imagedata format="PNG" fileref="imgs/hello_cairo.png" scale="72" />
55 </imageobject>
56 </inlinemediaobject>
57 </para>
58 <para>
59 A second motivation was to support GUI interfaces to graphs, but
60 without bloating the basic version of
61 <command>dot</command> with dependencies on
62 <command>gtk</command> or <command>xlib</command>.
63 The command:
64 <programlisting>
65 echo "digraph G {hello->world}" | dot -Txlib
66 </programlisting>
67 demand loads the xlib renderer from the graphviz-cairo plugins
68 and produces a zoomable display of the graph in a window:
69 </para>
70 <mediaobject>
71 <imageobject role="html">
72 <imagedata format="PNG" fileref="imgs/Txlib.png"/>
73 </imageobject>
74 <imageobject role="fo">
75 <imagedata format="PNG" fileref="imgs/Txlib.png" scale="72" />
76 </imageobject>
77 </mediaobject>
78 </sect1>
79 <sect1 id="s1-objectives">
80 <title>Objectives and Constraints</title>
81 <itemizedlist>
82 <listitem>
83 Support independent compilation so that plugins can be provided by
84 others separate from the base graphviz package.
85 </listitem>
86 <listitem>
87 Support independent packaging so that plugins can be installed
88 at the user's discretion, separate from the base graphviz package.
89 </listitem>
90 <listitem>
91 Support optional platform-specific functionality without adding
92 complexity to the base package.
93 </listitem>
94 <listitem>
95 Support on-demand loading of plugins so that extra capabilities
96 do not degrade performance for basic usage of the tools.
97 </listitem>
98 <listitem>
99 Unbundle some existing functionality into plugins so that the base
100 version of dot can be faster when that functionality is not needed.
101 </listitem>
102 <listitem>
103 Minimize performance cost of plugin support, particularly if no plugins
104 are demanded.
105 </listitem>
106 <listitem>
107 Must be able to configure dot with a fixed set of plugins as
108 <emphasis>builtins</emphasis> that are unconditionally loaded at program
109 invocation (linked at load time).
110 </listitem>
111 <listitem>
112 Must be able to configure dot as a static binary (linked at compile time),
113 including <emphasis>builtins</emphasis> if specified.
114 </listitem>
115 <listitem>
116 Must support old-style <emphasis>codegens</emphasis>
117 at least until completely replaced
118 by plugins that are equal or better in quality.
119 </listitem>
120 </itemizedlist>
121 </sect1>
122 <sect1 id="s1-structure">
123 <title>Structure and terminology</title>
124 <para>
125 A <emphasis>plugin-package</emphasis>,
126 such as <command>graphviz-cairo-2.6-1.i386.rpm</command>, provides one or more
127 <emphasis>plugin-libraries</emphasis>.
128 </para>
129 <para>
130 A <emphasis>plugin-library</emphasis> provides plugins that implement various
131 <emphasis>plugin-apis</emphasis>.
132 The <emphasis>plugin-library</emphasis> is the unit that is dynamically loaded.
133 </para>
134 <para>
135 There are currently five <emphasis>plugin-apis</emphasis>:
136 layout, textlayout, usershape, render, device.
137 </para>
138 <para>
139 A <emphasis>plugin-library</emphasis> provides
140 <emphasis>plugin-types</emphasis>
141 which implement <emphasis>plugin-api</emphasis>.
142 e.g. png ps pdf <emphasis>plugin-types</emphasis> implementing the renderer api.
143 </para>
144 <para>
145 Each <emphasis>plugin-type</emphasis> implementation provides a
146 <emphasis>quality</emphasis> indicator to control
147 the default selection when there are multiple implementations
148 of the same plugin-type.
149 The builtin implementations (if any) have a <emphasis>quality</emphasis>
150 of 0.
151 A positive <emphasis>quality</emphasis>
152 will choose the plugin in preference to the builtin.
153 A negative <emphasis>quality</emphasis>
154 will leave the builtin as the default.
155 </para>
156 <para>
157 The plugins are self-describing.
158 At installation time, <command>dot -c</command> is run to generate
159 <filename>/usr/lib/graphviz/config</filename> which describes the capabilities
160 of the available plugin-libraries so that they don't have
161 to be loaded at startup and can be loaded later on demand.
162 </para>
163 </sect1>
164 <sect1 id="s1-package">
165 <title>The graphviz-cairo package.</title>
166 <para>
167 The graphviz-cairo package provides multiple plugin-libraries,
168 all with some dependence on libcairo.so.
169 In general, a plugin-package can be used to localize all
170 knowledge and dependencies on some facility like cairo or
171 gtk or quartz.
172 The graphviz-cairo package alongside the graphviz base package
173 is illustrated diagrammatically here:
174 </para>
175 <mediaobject>
176 <imageobject role="html">
177 <imagedata format="PNG" fileref="imgs/framework.png"/>
178 </imageobject>
179 <imageobject role="fo">
180 <imagedata format="PNG" fileref="imgs/framework.png" scale="72" />
181 </imageobject>
182 </mediaobject>
183 <para>
184 Once graphviz-cairo is installed, and <command>dot -c</command> has
185 been run as part of the installation process,
186 the file: <filename>/usr/lib/graphviz/config</filename> contains:
187 </para>
188 <programlisting>
189 libgvplugin_gtk.so.2 gtk {
190 device {
191 gtk 0
192 }
193 }
194 libgvplugin_xlib.so.2 xlib {
195 device {
196 xlib 0
197 }
198 }
199 libgvplugin_cairo.so.2 cairo {
200 render {
201 png 10
202 ps -10
203 pdf 0
204 gtk 0
205 xlib 0
206 }
207 textlayout {
208 cairo 10
209 }
210 }
211 libgvplugin_dot_layout.so.2 dot_layout {
212 layout {
213 dot 0
214 }
215 }
216 libgvplugin_neato_layout.so.2 neato_layout {
217 layout {
218 neato 0
219 fdp 0
220 twopi 0
221 circo 0
222 }
223 }
224 </programlisting>
225 </sect1>
226 <sect1 id="s1-running">
227 <title>Running dot with plugins</title>
228 <para>
229 Dot attempts to be transparent to the user about its use of plugins.
230 By default it will always chose the highest quality plugin of the matching plugin-type.
231 Sometimes it is necessary to override the default.
232 This can be done by explicitly specifying the package to
233 take the plugin from. e.g. <command>dot -Tpng</command>
234 takes the highest quality
235 plugin currently available.
236 <command>dot -Tpng:cg</command>
237 takes the png renderer from the codegens.
238 </para>
239 <para>
240 If the package is unrecognized, the dot will return the set of available packages:
241 </para>
242 <programlisting>
243 $ dot -Tpng: hello.dot
244 Renderer type: "png:" not recognized. Use one of: png:cairo png:cg
245 </programlisting>
246 <para>
247 Also, <command>dot -v</command> will identify all
248 available plugins for all the apis.
249 </para>
250 <programlisting>
251 The plugin configuration file:
252 /home/ellson/FIX/Linux.i686/lib/graphviz/config
253 was successfully loaded.
254 render : canon cmap cmapx dia dot fig gd gd2 gif gtk
255 imap ismap jpeg jpg mif mp pcl pdf pic plain
256 plain-ext png ps ps2 svg svgz vrml vtx wbmp xdot xlib
257 layout : circo dot fdp neato twopi
258 textlayout : cairo
259 device : gtk xlib
260 usershape : png-gd
261 </programlisting>
262 </sect1>
263 <sect1 id="s1-installing">
264 <title>Installing plugins</title>
265 <para>
266 In order for dot to know what plugin capabilities are available
267 there is a file called <filename>/usr/lib/graphviz/config</filename> that is created
268 by running
269 <command>dot -c</command>
270 at install time.
271 <command>dot -c</command>
272 globs the library directory for all
273 filenames that look
274 like they might be plugins, then it goes through and actually
275 loads each of them in turn, positively verifying that they are
276 version compatible and loadable, and it then extracts from each
277 library the set of apis and types that it supports.
278 It saves this information in <filename>/usr/lib/graphviz/config</filename>.
279 When dot is run normally by a user, the single <filename>/usr/lib/graphviz/config</filename> file is
280 loaded but the plugins themselves are not loaded unless a plugin
281 is demanded from that library.
282 </para>
283 <para>
284 <command>dot -c</command> must be run, with enough privileges to write
285 <filename>/usr/lib/graphviz/config</filename>, whenever the set of plugins is changed.
286 For rpm systems
287 <command>dot -c</command>
288 is run automatically in %post and %postun
289 scripts in the rpm specfile.
290 If building from sources and installing directly,
291 <command>dot -c</command> is run
292 by the <command>make install</command> target.
293 If you are cross-compiling and cross-installing then probably the
294 best thing to do is to not use demand loaded plugins at all.
295 There is a <command>dot_static</command>
296 binary generated which as a basic set of
297 plugins already builtin.
298 </para>
299 </sect1>
300 <sect1 id="s1-developing">
301 <title>Developing new plugins</title>
302 <para>
303 One of the key goals of the plugin architecture was to allow others
304 to develop new plugins or plugin-libraries. In this section I
305 describe the structure of the key parts of the graphvz-cairo package
306 with a view to reusing or hacking them into a new package.
307 </para>
308 <sect2>
309 <title>A plugin-package</title>
310 <para>
311 The following files in the source tree for the graphviz-cairo package
312 can probably be used without change:
313 <programlisting>
314 graphviz-cairo/
315 autogen.sh
316 cpl1.0.txt
317 mkinstalldirs
318 </programlisting>
319 </para>
320 <para>
321 The following files should be changed to suit:
322 <programlisting>
323 graphviz-cairo/
324 AUTHORS
325 CHANGELOG.md
326 NEWS README
327 configure.ac
328 Makefile.am
329 graphviz-cairo.spec.in
330 </programlisting>
331 </para>
332 <para>
333 Also in graphviz-cairo are a number of GUI related files
334 specific to graphviz-cairo.
335 These can be probably not useful for any other plugin based on this code:
336 </para>
337 <programlisting>
338 graphviz-cairo/
339 dot.dot
340 dot.png
341 dot.xml
342 dotedit-splash.dot
343 dotedit.desktop.in
344 dotedit.indotedit.in
345 po/
346 </programlisting>
347 </sect2>
348 <sect2>
349 <title>A plugin library</title>
350 <para>
351 Each library in the plugin package has its own subdirectory under plugins:
352 </para>
353 <programlisting>
354 graphviz-cairo/
355 plugin/
356 Makefile.am
357 cairo/
358 Makefile.am
359 gvplugin_cairo.c
360 gtk/
361 Makefile.am
362 gvplugin_gtk.c
363 xlib/
364 Makefile.am
365 gvplugin_xlib.c
366 </programlisting>
367 <para>
368 graphviz-cairo/plugins/Makefile.am builds all the libraries as SUBDIRS.
369 </para>
370 <para>
371 In each library subdir Makefile.am and a gvplugin_xxx.c.
372 The gvplugin_cairo.c file is quite short.
373 It defines the primary entry symbol to the library
374 which is a structure defining the <emphasis>name</emphasis>
375 of the library and the address of the table of APIs that it supports.
376 </para>
377 <para>
378 The name: <emphasis>cairo</emphasis>,
379 the symbol: <emphasis>gvplugin_cairo_LTX_library</emphasis>,
380 and the filename of the library:
381 <emphasis>libgvplugin_cairo.so.2.0.0</emphasis>
382 must be kept aligned for the plugin mechanism to work.
383 </para>
384 <programlisting>
385 #include "gvplugin.h"
386
387 extern gvplugin_installed_t gvrender_cairogen_types;
388 extern gvplugin_installed_t gvtextlayout_cairogen_types;
389
390 static gvplugin_api_t apis[] = {
391 {API_render, &amp;gvrender_cairogen_types},
392 {API_textlayout, &amp;gvtextlayout_cairogen_types},
393 {(api_t)0, NULL},
394 };
395
396 gvplugin_library_t gvplugin_cairo_LTX_library = { "cairo", apis };
397 </programlisting>
398 </sect2>
399 <sect2>
400 <title>Plugin APIs</title>
401 <para>
402 The APIs are defined for the plugins by header files:
403 </para>
404 <programlisting>
405 /usr/include/graphviz/gvplugin_device.h
406 /usr/include/graphviz/gvplugin_layout.h
407 /usr/include/graphviz/gvplugin_render.h
408 /usr/include/graphviz/gvplugin_textlayout.h
409 /usr/include/graphviz/gvplugin_usershape.h
410 </programlisting>
411 <para>
412 Also, the set of APIS is defined in graphviz2/lib/gvc/gvplugin.h in:
413 #define APIS ELEM(render) ELEM(layout) ELEM(textlayout) ELEM(device) ELEM(usershape)
414 </para>
415 <para>
416 In the core of graphviz, each API is wrapped by a set of functions
417 that hide the details of the plugin mechanisms from the caller.
418 </para>
419 <programlisting>
420 graphviz2/lib/gvc/gvdevice.c
421 graphviz2/lib/gvc/gvlayout.c
422 graphviz2/lib/gvc/gvrender.c
423 graphviz2/lib/gvc/gvtextlayout.c
424 graphviz2/lib/gvc/gvusershape.c
425 </programlisting>
426 <para>
427 So, to add a new API, minimally what is required
428 is a new header file, extend the #define APIS,
429 and a new file of wrapper functions.
430 </para>
431 </sect2>
432 <sect2>
433 <title >Plugin types for an API</title>
434 <para>
435 The cairo library in particular is developing a number of new backends.
436 At some point someone will want to use one or more of these new backends.
437 </para>
438 <para>
439 (tbd)
440 </para>
441 </sect2>
442 </sect1>
443 </chapter>