| 1 |
This directory holds the source files for a C/C++ 2-D vector graphics |
| 2 |
library: GNU libplot/libplotter. It is distributed under the GNU GPL |
| 3 |
(see the file ../COPYING). |
| 4 |
|
| 5 |
The library provides a uniform interface to numerous display devices and |
| 6 |
output formats. The interface presents each output device as a opaque |
| 7 |
`Plotter' object: a virtual pen plotter. Each of libplot's output drivers |
| 8 |
is accessed at run time through an Plotter object of the appropriate type. |
| 9 |
Many types of Plotter are supported: Metafile, Tektronix, ReGIS, HP-GL/2, |
| 10 |
PCL 5, xfig, CGM (by default, WebCGM), idraw-editable Postscript, Adobe |
| 11 |
Illustrator, SVG, GIF, PNM (i.e. PBM/PGM/PPM), PNG, X Drawable, and X. All |
| 12 |
but the final two write graphic output to an output stream. The final two |
| 13 |
draw graphics by calling X routines. The X Drawable driver draws graphics |
| 14 |
in one or two `drawables' (windows or pixmaps), which must be passed to it |
| 15 |
via pointers, as Plotter parameters. The X driver pops up a window |
| 16 |
(actually, a Label widget) on an X display, and draws graphics in it. |
| 17 |
|
| 18 |
The names of the source files include as prefix a letter indicating which |
| 19 |
driver they include code for. For example, the file h_openpl.c is part of |
| 20 |
the HP-GL/2 driver, and contains low-level code which is called when the |
| 21 |
API function pl_openpl_r() is invoked on an HP-GL/2 or PCL 5 Plotter. |
| 22 |
Namely, it contains the _pl_h_begin_page() function. As prefixes, |
| 23 |
m=Metafile, t=Tektronix, r=ReGIS, h=HP-GL[/2] and PCL 5, f=Fig, c=CGM, |
| 24 |
p=Postscript, a=Adobe Illustrator, s=SVG, i=GIF, n=PNM, z=PNG, x=XDrawable, |
| 25 |
and y=X. (Actually, XPlotters use mostly XDrawablePlotter methods.) The |
| 26 |
many files whose names begin with `g' (e.g., g_relative.c) contain generic |
| 27 |
code, i.e. code used by the base Plotter class from which all other classes |
| 28 |
are derived. Similarly, `b' files contain code for a generic bitmap |
| 29 |
Plotter class, from which the PNM and PNG Plotter classes are derived. |
| 30 |
|
| 31 |
The library comes in two versions: libplot, which provides two C APIs (an |
| 32 |
old non-thread-safe API, and a new thread-safe API), and libplotter, which |
| 33 |
is a full-fledged C++ class library. They are compiled in separate |
| 34 |
directories (respectively, in this directory and in ../libplotter). |
| 35 |
However, the same source files are used in both libraries. That is a bit |
| 36 |
remarkable. It is arranged in the the two key header files |
| 37 |
../include/plotter.h and ./extern.h. If LIBPLOTTER is defined and a C++ |
| 38 |
compiler is used, a C++ class library results. Otherwise, a conventional C |
| 39 |
function library results. There are a lot of conditionalizations in those |
| 40 |
two header files. |
| 41 |
|
| 42 |
In libplot, a Plotter is implemented as a C-style struct. Each such struct |
| 43 |
includes approximately 20 function pointers: one function pointer for each |
| 44 |
of the low-level functions that define the semantics of the Plotter. It is |
| 45 |
by dereferencing these function pointers that polymorphism is implemented |
| 46 |
in libplot. The initialization of the `function pointer part' of each |
| 47 |
Plotter is contained in the corresponding ?_defplot.c file. At present, |
| 48 |
the low-level functions include |
| 49 |
|
| 50 |
initialize |
| 51 |
terminate |
| 52 |
begin_page |
| 53 |
erase_page |
| 54 |
end_page |
| 55 |
push_state |
| 56 |
pop_state |
| 57 |
paint_path |
| 58 |
paint_paths |
| 59 |
path_is_flushable |
| 60 |
maybe_prepaint_segments |
| 61 |
paint_marker |
| 62 |
paint_point |
| 63 |
paint_text_string_with_escapes |
| 64 |
paint_text_string |
| 65 |
get_text_width |
| 66 |
retrieve_font |
| 67 |
flush_output |
| 68 |
warning |
| 69 |
error |
| 70 |
|
| 71 |
For an explanation of the functionality of each of the above, see the |
| 72 |
comments in ../include/plotter.h. |
| 73 |
|
| 74 |
Here are some more details on how polymorphism is implemented. In libplot, |
| 75 |
each API function or internal Plotter method takes a pointer to a Plotter |
| 76 |
struct as its first argument. In the code, this pointer argument is always |
| 77 |
written as `_plotter'. It's the function pointers in `_plotter' that are |
| 78 |
dereferenced, when performing any low-level operation. For example, in the |
| 79 |
libplot code you may see a function invocation like |
| 80 |
`_plotter->begin_page(...)'. This will (1) dereference the `_plotter' |
| 81 |
pointer to get a Plotter, and (2) dereference the function pointer in the |
| 82 |
Plotter to find the Plotter-specific implementation of the low-level |
| 83 |
operation `begin_page()'. This would be _pl_h_begin_page() if `_plotter' |
| 84 |
points to an HP-GL/2 or PCL Plotter, for example. |
| 85 |
|
| 86 |
The two special files apinewc.c and apioldc.c, which alone among the source |
| 87 |
files are included in libplot but not libplotter, define the libplot API. |
| 88 |
Each function in the new (thread-safe) API takes as first argument a |
| 89 |
pointer to a Plotter struct to which the Plotter operations should be |
| 90 |
applied. It is this pointer which is passed down to the low-level code as |
| 91 |
`_plotter'. Each function in the old (non-thread-safe) API acts on a |
| 92 |
Plotter which is globally selected by calling the function pl_selectpl(). |
| 93 |
Both old and new C APIs include functions like pl_newpl[_r]() and |
| 94 |
pl_deletepl[_r](), which create and destroy Plotters. |
| 95 |
|
| 96 |
The C++ binding, i.e. the libplotter C++ class library, is easier to |
| 97 |
understand. There is a generic Plotter class and derived classes that are |
| 98 |
declared in ../include/plotter.h. These classes include as public members |
| 99 |
the API functions, such as openpl(), and low-level device-specific |
| 100 |
functions, such as begin_page(), which are protected and virtual. All this |
| 101 |
is arranged by a *lot* of conditional #defines in ../include/extern.h. For |
| 102 |
example, if LIBPLOTTER is defined then the internal function _h_begin_page |
| 103 |
is redefined to be HPGLPlotter::begin_page, which overrides the generic |
| 104 |
begin_page method, which is a no-op. |
| 105 |
|
| 106 |
Also, if LIBPLOTTER is defined then the Plotter methods don't all have a |
| 107 |
special first argument called `_plotter'. By the magic of the C/C++ |
| 108 |
preprocessor, the first argument disappears. And in the code for each |
| 109 |
method, `_plotter' is redefined to be `this', i.e. a pointer to the Plotter |
| 110 |
whose operation is being invoked. Believe it or not, it works: the same |
| 111 |
source files can be used in the compilation of both libplot and libplotter. |
| 112 |
|
| 113 |
A further implementation note: in libplot, any Plotter contains a `tag |
| 114 |
field', identifying its type (X11, PS, PCL5, etc.). This tag field is used |
| 115 |
only in libplot, and only by a very few forwarding functions, so if |
| 116 |
-DLIBPLOTTER is used, it's #ifdef'd out. (See ../include/plotter.h.) |
| 117 |
Other than that, the data members are essentially the same in the libplot |
| 118 |
Plotter struct as they are in the libplotter Plotter class. |
| 119 |
|
| 120 |
Actually, it's a bit more complicated. In libplot, each Plotter struct, no |
| 121 |
matter what its type, contains essentially the same data members. But |
| 122 |
there are a lot of data members, some of them which are relevant to all |
| 123 |
Plotters (e.g., `line_type') and some of which are specific to individual |
| 124 |
types of Plotter (e.g., `hpgl_line_type', which keeps track of an HP-GL |
| 125 |
display device's internal state). That means there's a lot of redundancy |
| 126 |
(an XPlotter struct, for example, contains all the private data members |
| 127 |
that an HPGLPlotter uses, but never uses them). In libplotter, the design |
| 128 |
is cleaner: each of the many data members which in libplot are contained in |
| 129 |
every Plotter struct is moved to the appropriate derived class. So |
| 130 |
`hpgl_line_type' is a data member of the HPGLPlotter class, but not of the |
| 131 |
base (generic) Plotter class, or any of its other subclasses. That's |
| 132 |
arranged by extensive #ifdef's in ../include/plotter.h. It's a bit awkward |
| 133 |
though: each data member needs to be declared twice in that file. |
| 134 |
|
| 135 |
Adding support for a new type of display device or output file format, |
| 136 |
given the object-oriented way that libplot/libplotter is structured, is |
| 137 |
easy. A new type of Plotter, derived from the base Plotter class, would be |
| 138 |
declared in ../include/plotter.h. If it needs any private data members, |
| 139 |
which it probably will, they would be declared in two places in that file, |
| 140 |
as just mentioned. Also, conditional #defines for the methods used by the |
| 141 |
new Plotter (to distinguish libplot from libplotter) would be added at the |
| 142 |
end of ./extern.h. |
| 143 |
|
| 144 |
A `defplot' file for the new Plotter type would need to be written too. |
| 145 |
Besides low-level initialize() and terminate() routines for the new Plotter |
| 146 |
type, it would include, for the benefit of libplot, an initialization |
| 147 |
routine for the part of the Plotter struct that contains low-level |
| 148 |
device-specific functions. (See, for example, the initializing structure |
| 149 |
_pl_h_default_plotter in the file h_defplot.c, as well as the routines |
| 150 |
_pl_h_initialize and _pl_h_terminate.) The new Plotter initialization |
| 151 |
would need to be added to the _plotter_data[] table in apinewc.c, which is |
| 152 |
specific to libplot. No such initialization is required for libplotter, |
| 153 |
since the C++ compiler itself initializes any instance of a derived Plotter |
| 154 |
class. |
| 155 |
|
| 156 |
To see how easy it is to add or remove support for a Plotter type, search |
| 157 |
for the symbol X_DISPLAY_MISSING. If this is defined, support for X |
| 158 |
Drawable Plotters and X Plotters will be dropped at compile time. The only |
| 159 |
occurrences in the code of tests like `#ifdef X_DISPLAY_MISSING' are in the |
| 160 |
header files ../include/plotter.h and ./extern.h, in apinewc.c, and in |
| 161 |
g_defstate.c. And the only reason for the appearance of X_DISPLAY_MISSING |
| 162 |
in g_defstate.c is that the drawing state structure initialization located |
| 163 |
in that file contains some X-specific fields, which need to be omitted if X |
| 164 |
support is dropped. That's because they rely on symbols defined in X11 |
| 165 |
header files. |
| 166 |
|
| 167 |
Up to now we haven't mentioned drawing states, but every Plotter includes a |
| 168 |
pointer to a stack of them. Drawing states, which are structs in both |
| 169 |
libplot and libplotter, contain numerous fields that are specific to |
| 170 |
individual types of Plotter. The reason device-specific information is |
| 171 |
kept in the drawing state struct is that it's convenient. For example, |
| 172 |
when the user-frame line width is changed, the device-frame line width |
| 173 |
changes too. Rather than compute the device-frame line width each time a |
| 174 |
graphical object is drawn, it's easier to store it in the drawing state. |
| 175 |
There are many other such examples (see the declaration of the drawing |
| 176 |
state structure as the `plDrawstate' typedef in ../include/plotter.h). |