i_defplot.c 11 KB

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