1 /* This file is part of the GNU plotutils package. Copyright (C) 1995,
2 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
3
4 The GNU plotutils package is free software. You may redistribute it
5 and/or modify it under the terms of the GNU General Public License as
6 published by the Free Software foundation; either version 2, or (at your
7 option) any later version.
8
9 The GNU plotutils package is distributed in the hope that it will be
10 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with the GNU plotutils package; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
17 Boston, MA 02110-1301, USA. */
18
19 /* This file defines the initialization for any GIFPlotter object,
20 including both private data and public methods. There is a one-to-one
21 correspondence between public methods and user-callable functions in the
22 C API. */
23
24 #include "sys-defines.h"
25 #include "extern.h"
26 #include "xmi.h" /* use libxmi scan conversion module */
27
28 /* forward references */
29 static bool parse_bitmap_size (const char *bitmap_size_s, int *width, int *height);
30
31 #ifndef LIBPLOTTER
32 /* In libplot, this is the initialization for the function-pointer part of
33 a GIFPlotter struct. */
34 const Plotter _pl_i_default_plotter =
35 {
36 /* initialization (after creation) and termination (before deletion) */
37 _pl_i_initialize, _pl_i_terminate,
38 /* page manipulation */
39 _pl_i_begin_page, _pl_i_erase_page, _pl_i_end_page,
40 /* drawing state manipulation */
41 _pl_g_push_state, _pl_g_pop_state,
42 /* internal path-painting methods (endpath() is a wrapper for the first) */
43 _pl_i_paint_path, _pl_i_paint_paths, _pl_g_path_is_flushable, _pl_g_maybe_prepaint_segments,
44 /* internal methods for drawing of markers and points */
45 _pl_g_paint_marker, _pl_i_paint_point,
46 /* internal methods that plot strings in Hershey, non-Hershey fonts */
47 _pl_g_paint_text_string_with_escapes, _pl_g_paint_text_string,
48 _pl_g_get_text_width,
49 /* private low-level `retrieve font' method */
50 _pl_g_retrieve_font,
51 /* `flush output' method, called only if Plotter handles its own output */
52 _pl_g_flush_output,
53 /* error handlers */
54 _pl_g_warning,
55 _pl_g_error,
56 };
57 #endif /* not LIBPLOTTER */
58
59 /* The private `initialize' method, which is invoked when a Plotter is
60 created. It is used for such things as initializing capability flags
61 from the values of class variables, allocating storage, etc. When this
62 is invoked, _plotter points to the Plotter that has just been
63 created. */
64
65 void
_pl_i_initialize(S___ (Plotter * _plotter))66 _pl_i_initialize (S___(Plotter *_plotter))
67 {
68 #ifndef LIBPLOTTER
69 /* in libplot, manually invoke superclass initialization method */
70 _pl_g_initialize (S___(_plotter));
71 #endif
72
73 /* override superclass initializations, as necessary */
74
75 #ifndef LIBPLOTTER
76 /* tag field, differs in derived classes */
77 _plotter->data->type = PL_GIF;
78 #endif
79
80 /* output model */
81 _plotter->data->output_model = PL_OUTPUT_VIA_CUSTOM_ROUTINES;
82
83 /* user-queryable capabilities: 0/1/2 = no/yes/maybe */
84 _plotter->data->have_wide_lines = 1;
85 _plotter->data->have_dash_array = 1;
86 _plotter->data->have_solid_fill = 1;
87 _plotter->data->have_odd_winding_fill = 1;
88 _plotter->data->have_nonzero_winding_fill = 1;
89 _plotter->data->have_settable_bg = 1;
90 _plotter->data->have_escaped_string_support = 0;
91 _plotter->data->have_ps_fonts = 0;
92 _plotter->data->have_pcl_fonts = 0;
93 _plotter->data->have_stick_fonts = 0;
94 _plotter->data->have_extra_stick_fonts = 0;
95 _plotter->data->have_other_fonts = 0;
96
97 /* text and font-related parameters (internal, not queryable by user);
98 note that we don't set kern_stick_fonts, because it was set by the
99 superclass initialization (and it's irrelevant for this Plotter type,
100 anyway) */
101 _plotter->data->default_font_type = PL_F_HERSHEY;
102 _plotter->data->pcl_before_ps = false;
103 _plotter->data->have_horizontal_justification = false;
104 _plotter->data->have_vertical_justification = false;
105 _plotter->data->issue_font_warning = true;
106
107 /* path-related parameters (also internal); note that we
108 don't set max_unfilled_path_length, because it was set by the
109 superclass initialization */
110 _plotter->data->have_mixed_paths = false;
111 _plotter->data->allowed_arc_scaling = AS_AXES_PRESERVED;
112 _plotter->data->allowed_ellarc_scaling = AS_AXES_PRESERVED;
113 _plotter->data->allowed_quad_scaling = AS_NONE;
114 _plotter->data->allowed_cubic_scaling = AS_NONE;
115 _plotter->data->allowed_box_scaling = AS_NONE;
116 _plotter->data->allowed_circle_scaling = AS_NONE;
117 _plotter->data->allowed_ellipse_scaling = AS_AXES_PRESERVED;
118
119 /* dimensions */
120 _plotter->data->display_model_type = (int)DISP_MODEL_VIRTUAL;
121 _plotter->data->display_coors_type = (int)DISP_DEVICE_COORS_INTEGER_LIBXMI;
122 _plotter->data->flipped_y = true;
123 _plotter->data->imin = 0;
124 _plotter->data->imax = 569;
125 _plotter->data->jmin = 569;
126 _plotter->data->jmax = 0;
127 _plotter->data->xmin = 0.0;
128 _plotter->data->xmax = 0.0;
129 _plotter->data->ymin = 0.0;
130 _plotter->data->ymax = 0.0;
131 _plotter->data->page_data = (plPageData *)NULL;
132
133 /* initialize data members specific to this derived class */
134 /* parameters */
135 _plotter->i_xn = _plotter->data->imax + 1;
136 _plotter->i_yn = _plotter->data->jmin + 1;
137 _plotter->i_num_pixels = (_plotter->i_xn) * (_plotter->i_yn);
138 _plotter->i_animation = true; /* default, can be turned off */
139 _plotter->i_iterations = 0;
140 _plotter->i_delay = 0;
141 _plotter->i_interlace = false;
142 _plotter->i_transparent = false;
143 _plotter->i_transparent_color.red = 255; /* dummy */
144 _plotter->i_transparent_color.green = 255; /* dummy */
145 _plotter->i_transparent_color.blue = 255; /* dummy */
146 _plotter->i_transparent_index = 0; /* dummy */
147 /* storage used by libxmi's reentrant miDrawArcs_r() function for
148 cacheing rasterized ellipses */
149 _plotter->i_arc_cache_data = (void *)miNewEllipseCache ();
150 /* dynamic variables */
151 _plotter->i_painted_set = (void *)NULL;
152 _plotter->i_canvas = (void *)NULL;
153 /* N.B. _plotter->i_colormap is initialized in i_openpl.c */
154 _plotter->i_num_color_indices = 0;
155 _plotter->i_bit_depth = 0;
156 _plotter->i_frame_nonempty = false;
157 _plotter->i_pixels_scanned = 0;
158 _plotter->i_pass = 0;
159 _plotter->i_hot.x = 0;
160 _plotter->i_hot.y = 0;
161 /* N.B. _plotter->i_global_colormap, i_num_global_color_indices are
162 copied into later */
163 _plotter->i_header_written = false;
164
165 /* initialize certain data members from device driver parameters */
166
167 /* is there a user-specified transparent color? */
168 {
169 const char *transparent_name_s;
170 plColor color;
171
172 transparent_name_s = (const char *)_get_plot_param (_plotter->data, "TRANSPARENT_COLOR");
173 if (transparent_name_s
174 && _string_to_color (transparent_name_s, &color, _plotter->data->color_name_cache))
175 /* have 24-bit RGB */
176 {
177 _plotter->i_transparent = true;
178 _plotter->i_transparent_color = color;
179 }
180 }
181
182 /* produce an interlaced GIF? */
183 {
184 const char *interlace_s;
185
186 interlace_s = (const char *)_get_plot_param (_plotter->data, "INTERLACE" );
187 if (strcasecmp (interlace_s, "yes") == 0)
188 _plotter->i_interlace = true;
189 }
190
191 /* turn off animation? */
192 {
193 const char *animate_s;
194
195 animate_s = (const char *)_get_plot_param (_plotter->data, "GIF_ANIMATION" );
196 if (strcasecmp (animate_s, "no") == 0)
197 _plotter->i_animation = false;
198 }
199
200 /* determine number of iterations to be used (if animating) */
201 {
202 const char *iteration_s;
203 int num_iterations;
204
205 iteration_s = (const char *)_get_plot_param (_plotter->data, "GIF_ITERATIONS" );
206 if (sscanf (iteration_s, "%d", &num_iterations) > 0
207 && num_iterations >= 0 && num_iterations <= 65535)
208 _plotter->i_iterations = num_iterations;
209 }
210
211 /* determine delay after each frame, in 1/100 sec units */
212 {
213 const char *delay_s;
214 int delay;
215
216 delay_s = (const char *)_get_plot_param (_plotter->data, "GIF_DELAY" );
217 if (sscanf (delay_s, "%d", &delay) > 0
218 && delay >= 0 && delay <= 65535)
219 _plotter->i_delay = delay;
220 }
221
222 /* determine the range of device coordinates over which the graphics
223 display will extend (and hence the transformation from user to device
224 coordinates). */
225 {
226 const char *bitmap_size_s;
227 int width = 1, height = 1;
228
229 bitmap_size_s = (const char *)_get_plot_param (_plotter->data, "BITMAPSIZE");
230 if (bitmap_size_s && parse_bitmap_size (bitmap_size_s, &width, &height)
231 /* insist on range of 1..65535 for GIF format */
232 && width >= 1 && height >= 1
233 && width <= 65535 && height <= 65535)
234 /* override defaults above */
235 {
236 _plotter->data->imax = width - 1;
237 _plotter->data->jmin = height - 1;
238 _plotter->i_xn = width;
239 _plotter->i_yn = height;
240 _plotter->i_num_pixels = width * height;
241 }
242 }
243
244 /* compute the NDC to device-frame affine map, set it in Plotter */
245 _compute_ndc_to_device_map (_plotter->data);
246 }
247
248 static bool
parse_bitmap_size(const char * bitmap_size_s,int * width,int * height)249 parse_bitmap_size (const char *bitmap_size_s, int *width, int *height)
250 {
251 int local_width = 1, local_height = 1;
252
253 if (bitmap_size_s
254 /* should parse this better */
255 && sscanf (bitmap_size_s, "%dx%d", &local_width, &local_height) == 2
256 && local_width > 0 && local_height > 0)
257 {
258 *width = local_width;
259 *height = local_height;
260 return true;
261 }
262 else
263 return false;
264 }
265
266 /* The private `terminate' method, which is invoked when a Plotter is
267 deleted. It may do such things as write to an output stream from
268 internal storage, deallocate storage, etc. When this is invoked,
269 _plotter points (temporarily) to the Plotter that is about to be
270 deleted. */
271
272 void
_pl_i_terminate(S___ (Plotter * _plotter))273 _pl_i_terminate (S___(Plotter *_plotter))
274 {
275 /* free storage used by libxmi's reentrant miDrawArcs_r() function */
276 miDeleteEllipseCache ((miEllipseCache *)_plotter->i_arc_cache_data);
277
278 #ifndef LIBPLOTTER
279 /* in libplot, manually invoke superclass termination method */
280 _pl_g_terminate (S___(_plotter));
281 #endif
282 }
283
284 #ifdef LIBPLOTTER
GIFPlotter(FILE * infile,FILE * outfile,FILE * errfile)285 GIFPlotter::GIFPlotter (FILE *infile, FILE *outfile, FILE *errfile)
286 :Plotter (infile, outfile, errfile)
287 {
288 _pl_i_initialize ();
289 }
290
GIFPlotter(FILE * outfile)291 GIFPlotter::GIFPlotter (FILE *outfile)
292 :Plotter (outfile)
293 {
294 _pl_i_initialize ();
295 }
296
GIFPlotter(istream & in,ostream & out,ostream & err)297 GIFPlotter::GIFPlotter (istream& in, ostream& out, ostream& err)
298 : Plotter (in, out, err)
299 {
300 _pl_i_initialize ();
301 }
302
GIFPlotter(ostream & out)303 GIFPlotter::GIFPlotter (ostream& out)
304 : Plotter (out)
305 {
306 _pl_i_initialize ();
307 }
308
GIFPlotter()309 GIFPlotter::GIFPlotter ()
310 {
311 _pl_i_initialize ();
312 }
313
GIFPlotter(FILE * infile,FILE * outfile,FILE * errfile,PlotterParams & parameters)314 GIFPlotter::GIFPlotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams ¶meters)
315 :Plotter (infile, outfile, errfile, parameters)
316 {
317 _pl_i_initialize ();
318 }
319
GIFPlotter(FILE * outfile,PlotterParams & parameters)320 GIFPlotter::GIFPlotter (FILE *outfile, PlotterParams ¶meters)
321 :Plotter (outfile, parameters)
322 {
323 _pl_i_initialize ();
324 }
325
GIFPlotter(istream & in,ostream & out,ostream & err,PlotterParams & parameters)326 GIFPlotter::GIFPlotter (istream& in, ostream& out, ostream& err, PlotterParams ¶meters)
327 : Plotter (in, out, err, parameters)
328 {
329 _pl_i_initialize ();
330 }
331
GIFPlotter(ostream & out,PlotterParams & parameters)332 GIFPlotter::GIFPlotter (ostream& out, PlotterParams ¶meters)
333 : Plotter (out, parameters)
334 {
335 _pl_i_initialize ();
336 }
337
GIFPlotter(PlotterParams & parameters)338 GIFPlotter::GIFPlotter (PlotterParams ¶meters)
339 : Plotter (parameters)
340 {
341 _pl_i_initialize ();
342 }
343
~GIFPlotter()344 GIFPlotter::~GIFPlotter ()
345 {
346 /* if luser left the Plotter open, close it */
347 if (_plotter->data->open)
348 _API_closepl ();
349
350 _pl_i_terminate ();
351 }
352 #endif
353