x_defplot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 XDrawablePlotter 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. #ifndef LIBPLOTTER
  22. /* In libplot, this is the initialization for the function-pointer part of
  23. an XDrawablePlotter struct. */
  24. const Plotter _pl_x_default_plotter =
  25. {
  26. /* initialization (after creation) and termination (before deletion) */
  27. _pl_x_initialize, _pl_x_terminate,
  28. /* page manipulation */
  29. _pl_x_begin_page, _pl_x_erase_page, _pl_x_end_page,
  30. /* drawing state manipulation */
  31. _pl_x_push_state, _pl_x_pop_state,
  32. /* internal path-painting methods (endpath() is a wrapper for the first) */
  33. _pl_x_paint_path, _pl_x_paint_paths, _pl_x_path_is_flushable, _pl_x_maybe_prepaint_segments,
  34. /* internal methods for drawing of markers and points */
  35. _pl_g_paint_marker, _pl_x_paint_point,
  36. /* internal methods that plot strings in Hershey, non-Hershey fonts */
  37. _pl_g_paint_text_string_with_escapes, _pl_x_paint_text_string,
  38. _pl_x_get_text_width,
  39. /* private low-level `retrieve font' method */
  40. _pl_x_retrieve_font,
  41. /* `flush output' method, called only if Plotter handles its own output */
  42. _pl_x_flush_output,
  43. /* internal error handlers */
  44. _pl_g_warning,
  45. _pl_g_error,
  46. };
  47. #endif /* not LIBPLOTTER */
  48. /* The private `initialize' method, which is invoked when a Plotter is
  49. created. It is used for such things as initializing capability flags
  50. from the values of class variables, allocating storage, etc. When this
  51. is invoked, _plotter points to the Plotter that has just been
  52. created. */
  53. void
  54. _pl_x_initialize (S___(Plotter *_plotter))
  55. {
  56. Colormap *x_cmap_ptr;
  57. Drawable *drawable_p1, *drawable_p2;
  58. #ifndef LIBPLOTTER
  59. /* in libplot, manually invoke superclass initialization method */
  60. _pl_g_initialize (S___(_plotter));
  61. #endif
  62. /* override superclass initializations, as necessary */
  63. #ifndef LIBPLOTTER
  64. /* tag field, differs in derived classes */
  65. _plotter->data->type = PL_X11_DRAWABLE;
  66. #endif
  67. /* output model */
  68. _plotter->data->output_model = PL_OUTPUT_VIA_CUSTOM_ROUTINES_TO_NON_STREAM;
  69. /* user-queryable capabilities: 0/1/2 = no/yes/maybe */
  70. _plotter->data->have_wide_lines = 1;
  71. _plotter->data->have_dash_array = 1;
  72. _plotter->data->have_solid_fill = 1;
  73. _plotter->data->have_odd_winding_fill = 1;
  74. _plotter->data->have_nonzero_winding_fill = 1;
  75. _plotter->data->have_settable_bg = 1;
  76. _plotter->data->have_escaped_string_support = 0;
  77. _plotter->data->have_ps_fonts = 1;
  78. #ifdef USE_LJ_FONTS_IN_X
  79. _plotter->data->have_pcl_fonts = 1;
  80. #else
  81. _plotter->data->have_pcl_fonts = 0;
  82. #endif
  83. _plotter->data->have_stick_fonts = 0;
  84. _plotter->data->have_extra_stick_fonts = 0;
  85. _plotter->data->have_other_fonts = 1;
  86. /* text and font-related parameters (internal, not queryable by user);
  87. note that we don't set kern_stick_fonts, because it was set by the
  88. superclass initialization (and it's irrelevant for this Plotter type,
  89. anyway) */
  90. _plotter->data->default_font_type = PL_F_POSTSCRIPT;
  91. _plotter->data->pcl_before_ps = false;
  92. _plotter->data->have_horizontal_justification = false;
  93. _plotter->data->have_vertical_justification = false;
  94. _plotter->data->issue_font_warning = true;
  95. /* path-related parameters (also internal); note that we
  96. don't set max_unfilled_path_length, because it was set by the
  97. superclass initialization */
  98. _plotter->data->have_mixed_paths = false;
  99. _plotter->data->allowed_arc_scaling = AS_AXES_PRESERVED;
  100. _plotter->data->allowed_ellarc_scaling = AS_AXES_PRESERVED;
  101. _plotter->data->allowed_quad_scaling = AS_NONE;
  102. _plotter->data->allowed_cubic_scaling = AS_NONE;
  103. _plotter->data->allowed_box_scaling = AS_NONE;
  104. _plotter->data->allowed_circle_scaling = AS_NONE;
  105. _plotter->data->allowed_ellipse_scaling = AS_AXES_PRESERVED;
  106. /* dimensions */
  107. _plotter->data->display_model_type = (int)DISP_MODEL_VIRTUAL;
  108. _plotter->data->display_coors_type = (int)DISP_DEVICE_COORS_INTEGER_LIBXMI; /* X != NeWS, alas */
  109. _plotter->data->flipped_y = true;
  110. _plotter->data->imin = 0;
  111. _plotter->data->imax = 569;
  112. _plotter->data->jmin = 569;
  113. _plotter->data->jmax = 0; /* flipped y */
  114. _plotter->data->xmin = 0.0;
  115. _plotter->data->xmax = 0.0;
  116. _plotter->data->ymin = 0.0;
  117. _plotter->data->ymax = 0.0;
  118. _plotter->data->page_data = (plPageData *)NULL;
  119. /* initialize data members specific to this derived class */
  120. _plotter->x_dpy = (Display *)NULL;
  121. _plotter->x_visual = (Visual *)NULL;
  122. _plotter->x_drawable1 = (Drawable)0;
  123. _plotter->x_drawable2 = (Drawable)0;
  124. _plotter->x_drawable3 = (Drawable)0;
  125. _plotter->x_double_buffering = X_DBL_BUF_NONE;
  126. _plotter->x_max_polyline_len = INT_MAX; /* reduced in openpl() */
  127. _plotter->x_fontlist = (plXFontRecord *)NULL;
  128. _plotter->x_colorlist = (plColorRecord *)NULL;
  129. _plotter->x_cmap = (Colormap)0;
  130. _plotter->x_cmap_type = X_CMAP_ORIG;
  131. _plotter->x_colormap_warning_issued = false;
  132. _plotter->x_bg_color_warning_issued = false;
  133. _plotter->x_paint_pixel_count = 0;
  134. /* initialize certain data members from device driver parameters */
  135. /* if this is NULL, won't be able to open Plotter */
  136. _plotter->x_dpy = (Display *)_get_plot_param (_plotter->data, "XDRAWABLE_DISPLAY");
  137. /* we allow the visual to be NULL, i.e., not set, since we use it only
  138. for determining the visual class of the colormap (see below); since if
  139. it's Truecolor, that means we can avoid calling XAllocColor() */
  140. _plotter->x_visual = (Visual *)_get_plot_param (_plotter->data, "XDRAWABLE_VISUAL");
  141. /* we allow either or both of the drawables to be NULL, i.e. not set */
  142. drawable_p1 = (Drawable *)_get_plot_param (_plotter->data, "XDRAWABLE_DRAWABLE1");
  143. drawable_p2 = (Drawable *)_get_plot_param (_plotter->data, "XDRAWABLE_DRAWABLE2");
  144. _plotter->x_drawable1 = drawable_p1 ? *drawable_p1 : 0;
  145. _plotter->x_drawable2 = drawable_p2 ? *drawable_p2 : 0;
  146. /* allow user to specify a non-default colormap */
  147. x_cmap_ptr = (Colormap *)_get_plot_param (_plotter->data, "XDRAWABLE_COLORMAP");
  148. if (x_cmap_ptr != NULL)
  149. /* user-specified colormap */
  150. {
  151. _plotter->x_cmap = *x_cmap_ptr;
  152. if (_plotter->x_dpy)
  153. /* have a display, so is this the default colormap? */
  154. {
  155. int screen; /* screen number */
  156. Screen *screen_struct; /* screen structure */
  157. screen = DefaultScreen (_plotter->x_dpy);
  158. screen_struct = ScreenOfDisplay (_plotter->x_dpy, screen);
  159. if (_plotter->x_cmap == DefaultColormapOfScreen (screen_struct))
  160. /* it is, so as visual, use visual of default screen */
  161. _plotter->x_visual = DefaultVisualOfScreen (screen_struct);
  162. }
  163. }
  164. else
  165. /* default colormap */
  166. {
  167. if (_plotter->x_dpy)
  168. /* have a display, so as default, use colormap of its default screen */
  169. {
  170. int screen; /* screen number */
  171. Screen *screen_struct; /* screen structure */
  172. screen = DefaultScreen (_plotter->x_dpy);
  173. screen_struct = ScreenOfDisplay (_plotter->x_dpy, screen);
  174. _plotter->x_cmap = DefaultColormapOfScreen (screen_struct);
  175. /* also, as visual, use visual of its default screen */
  176. _plotter->x_visual = DefaultVisualOfScreen (screen_struct);
  177. }
  178. }
  179. /* colormap type will always be `original' (unlike XPlotters, XDrawable
  180. Plotters never switch to a private colormap) */
  181. _plotter->x_cmap_type = X_CMAP_ORIG;
  182. }
  183. /* The private `terminate' method, which is invoked when a Plotter is
  184. deleted. It may do such things as write to an output stream from
  185. internal storage, deallocate storage, etc. When this is invoked,
  186. _plotter points to the Plotter that is about to be deleted. */
  187. void
  188. _pl_x_terminate (S___(Plotter *_plotter))
  189. {
  190. plXFontRecord *fptr = _plotter->x_fontlist, *fptr_next;
  191. /* Free entire cache of retrieved core X fonts (a linked list). One of
  192. these is the `current font', i.e., _plotter->x_font_struct, so we
  193. don't free that data member separately. */
  194. while (fptr)
  195. {
  196. fptr_next = fptr->next;
  197. free (fptr->x_font_name);
  198. if (fptr->x_font_struct)
  199. /* non-NULL, indicating a successful font retrieval */
  200. XFreeFont (_plotter->x_dpy, fptr->x_font_struct);
  201. fptr = fptr->next;
  202. }
  203. #ifndef LIBPLOTTER
  204. /* in libplot, manually invoke superclass termination method */
  205. _pl_g_terminate (S___(_plotter));
  206. #endif
  207. }
  208. #ifdef LIBPLOTTER
  209. XDrawablePlotter::XDrawablePlotter (FILE *infile, FILE *outfile, FILE *errfile)
  210. :Plotter (infile, outfile, errfile)
  211. {
  212. _pl_x_initialize ();
  213. }
  214. XDrawablePlotter::XDrawablePlotter (FILE *outfile)
  215. :Plotter (outfile)
  216. {
  217. _pl_x_initialize ();
  218. }
  219. XDrawablePlotter::XDrawablePlotter (istream& in, ostream& out, ostream& err)
  220. : Plotter (in, out, err)
  221. {
  222. _pl_x_initialize ();
  223. }
  224. XDrawablePlotter::XDrawablePlotter (ostream& out)
  225. : Plotter (out)
  226. {
  227. _pl_x_initialize ();
  228. }
  229. XDrawablePlotter::XDrawablePlotter ()
  230. {
  231. _pl_x_initialize ();
  232. }
  233. XDrawablePlotter::XDrawablePlotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &parameters)
  234. :Plotter (infile, outfile, errfile, parameters)
  235. {
  236. _pl_x_initialize ();
  237. }
  238. XDrawablePlotter::XDrawablePlotter (FILE *outfile, PlotterParams &parameters)
  239. :Plotter (outfile, parameters)
  240. {
  241. _pl_x_initialize ();
  242. }
  243. XDrawablePlotter::XDrawablePlotter (istream& in, ostream& out, ostream& err, PlotterParams &parameters)
  244. : Plotter (in, out, err, parameters)
  245. {
  246. _pl_x_initialize ();
  247. }
  248. XDrawablePlotter::XDrawablePlotter (ostream& out, PlotterParams &parameters)
  249. : Plotter (out, parameters)
  250. {
  251. _pl_x_initialize ();
  252. }
  253. XDrawablePlotter::XDrawablePlotter (PlotterParams &parameters)
  254. : Plotter (parameters)
  255. {
  256. _pl_x_initialize ();
  257. }
  258. XDrawablePlotter::~XDrawablePlotter ()
  259. {
  260. /* if luser left the Plotter open, close it */
  261. if (_plotter->data->open)
  262. _API_closepl ();
  263. _pl_x_terminate ();
  264. }
  265. #endif
  266. #ifndef LIBPLOTTER
  267. /* The following forwarding functions provide special support in libplot
  268. for deriving the XPlotter class from the XDrawablePlotter class. In
  269. libplotter, forwarding is implemented by a virtual function; see
  270. plotter.h. */
  271. /* Forwarding function called by any XDrawablePlotter/XPlotter in
  272. x_color.c, if the original colormap fills up. See x_openpl.c and
  273. y_openpl.c for the two forwarded-to functions
  274. _pl_x_maybe_get_new_colormap() and _pl_y_maybe_get_new_colormap(),
  275. respectively. The former is a no-op, but the latter tries to switch to
  276. a new colormap. */
  277. void
  278. _maybe_get_new_colormap (Plotter *_plotter)
  279. {
  280. switch ((int)_plotter->data->type)
  281. {
  282. case (int)PL_X11_DRAWABLE:
  283. default:
  284. _pl_x_maybe_get_new_colormap (_plotter); /* no-op */
  285. break;
  286. case (int)PL_X11:
  287. _pl_y_maybe_get_new_colormap (_plotter);
  288. break;
  289. }
  290. }
  291. /* Forwarding function called by any XDrawablePlotter at the conclusion of
  292. most drawing operations. See x_openpl.c and y_openpl.c for the two
  293. forwarded-to functions _pl_x_maybe_handle_x_events() and
  294. _pl_y_maybe_handle_x_events(), respectively. The former is a no-op, but
  295. the latter is processes pending X events. */
  296. void
  297. _maybe_handle_x_events (Plotter *_plotter)
  298. {
  299. switch ((int)_plotter->data->type)
  300. {
  301. case (int)PL_X11_DRAWABLE:
  302. default:
  303. _pl_x_maybe_handle_x_events (_plotter); /* no-op */
  304. break;
  305. case (int)PL_X11:
  306. _pl_y_maybe_handle_x_events (_plotter);
  307. break;
  308. }
  309. }
  310. #endif /* not LIBPLOTTER */