c_defplot.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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 CGMPlotter 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. /* localtime_r() is currently not used, because there is apparently _no_
  22. universal way of ensuring that it is declared. On some systems
  23. (e.g. Red Hat Linux), `#define _POSIX_SOURCE' will do it. But on other
  24. systems, doing `#define _POSIX_SOURCE' **removes** the declaration! */
  25. #ifdef HAVE_LOCALTIME_R
  26. #undef HAVE_LOCALTIME_R
  27. #endif
  28. #ifdef MSDOS
  29. #include <unistd.h> /* for fsync() */
  30. #endif
  31. /* song and dance to define time_t, and declare both time() and localtime() */
  32. #ifdef HAVE_SYS_TYPES_H
  33. #include <sys/types.h> /* for time_t on some pre-ANSI Unix systems */
  34. #endif
  35. #ifdef TIME_WITH_SYS_TIME
  36. #include <sys/time.h> /* for time() on some pre-ANSI Unix systems */
  37. #include <time.h> /* for localtime() */
  38. #else /* not TIME_WITH_SYS_TIME, include only one (prefer <sys/time.h>) */
  39. #ifdef HAVE_SYS_TIME_H
  40. #include <sys/time.h>
  41. #else /* not HAVE_SYS_TIME_H */
  42. #include <time.h>
  43. #endif /* not HAVE_SYS_TIME_H */
  44. #endif /* not TIME_WITH_SYS_TIME */
  45. /* forward references */
  46. static void build_sdr_from_index (plOutbuf *sdr_buffer, int cgm_encoding, int x);
  47. static void build_sdr_from_string (plOutbuf *sdr_buffer, int cgm_encoding, const char *s, int string_length, bool use_double_quotes);
  48. static void build_sdr_from_ui8s (plOutbuf *sdr_buffer, int cgm_encoding, const int *x, int n);
  49. #ifndef LIBPLOTTER
  50. /* In libplot, this is the initialization for the function-pointer part of
  51. a CGMPlotter struct. */
  52. const Plotter _pl_c_default_plotter =
  53. {
  54. /* initialization (after creation) and termination (before deletion) */
  55. _pl_c_initialize, _pl_c_terminate,
  56. /* page manipulation */
  57. _pl_c_begin_page, _pl_c_erase_page, _pl_c_end_page,
  58. /* drawing state manipulation */
  59. _pl_g_push_state, _pl_g_pop_state,
  60. /* internal path-painting methods (endpath() is a wrapper for the first) */
  61. _pl_c_paint_path, _pl_c_paint_paths, _pl_g_path_is_flushable, _pl_g_maybe_prepaint_segments,
  62. /* internal methods for drawing of markers and points */
  63. _pl_c_paint_marker, _pl_c_paint_point,
  64. /* internal methods that plot strings in Hershey, non-Hershey fonts */
  65. _pl_g_paint_text_string_with_escapes, _pl_c_paint_text_string,
  66. _pl_g_get_text_width,
  67. /* private low-level `retrieve font' method */
  68. _pl_g_retrieve_font,
  69. /* `flush output' method, called only if Plotter handles its own output */
  70. _pl_g_flush_output,
  71. /* error handlers */
  72. _pl_g_warning,
  73. _pl_g_error,
  74. };
  75. #endif /* not LIBPLOTTER */
  76. /* The private `initialize' method, which is invoked when a Plotter is
  77. created. It is used for such things as initializing capability flags
  78. from the values of class variables, allocating storage, etc. When this
  79. is invoked, _plotter points to the Plotter that has just been
  80. created. */
  81. void
  82. _pl_c_initialize (S___(Plotter *_plotter))
  83. {
  84. #ifndef LIBPLOTTER
  85. /* in libplot, manually invoke superclass initialization method */
  86. _pl_g_initialize (S___(_plotter));
  87. #endif
  88. /* override generic initializations (which are appropriate to the base
  89. Plotter class), as necessary */
  90. #ifndef LIBPLOTTER
  91. /* tag field, differs in derived classes */
  92. _plotter->data->type = PL_CGM;
  93. #endif
  94. /* output model */
  95. _plotter->data->output_model = PL_OUTPUT_PAGES_ALL_AT_ONCE;
  96. /* user-queryable capabilities: 0/1/2 = no/yes/maybe */
  97. _plotter->data->have_wide_lines = 1;
  98. _plotter->data->have_dash_array = 0;
  99. _plotter->data->have_solid_fill = 1;
  100. _plotter->data->have_odd_winding_fill = 1;
  101. _plotter->data->have_nonzero_winding_fill = 0;
  102. _plotter->data->have_settable_bg = 1;
  103. _plotter->data->have_escaped_string_support = 0;
  104. _plotter->data->have_ps_fonts = 1;
  105. _plotter->data->have_pcl_fonts = 0;
  106. _plotter->data->have_stick_fonts = 0;
  107. _plotter->data->have_extra_stick_fonts = 0;
  108. _plotter->data->have_other_fonts = 0;
  109. /* text and font-related parameters (internal, not queryable by user);
  110. note that we don't set kern_stick_fonts, because it was set by the
  111. superclass initialization (and it's irrelevant for this Plotter type,
  112. anyway) */
  113. _plotter->data->default_font_type = PL_F_POSTSCRIPT;
  114. _plotter->data->pcl_before_ps = false;
  115. _plotter->data->have_horizontal_justification = true;
  116. _plotter->data->have_vertical_justification = true;
  117. _plotter->data->issue_font_warning = true;
  118. /* path-related parameters (also internal); note that we
  119. don't set max_unfilled_path_length, because it was set by the
  120. superclass initialization */
  121. _plotter->data->have_mixed_paths = false;
  122. _plotter->data->allowed_arc_scaling = AS_NONE;
  123. _plotter->data->allowed_ellarc_scaling = AS_NONE;
  124. _plotter->data->allowed_quad_scaling = AS_NONE;
  125. _plotter->data->allowed_cubic_scaling = AS_NONE;
  126. _plotter->data->allowed_box_scaling = AS_AXES_PRESERVED;
  127. _plotter->data->allowed_circle_scaling = AS_UNIFORM;
  128. _plotter->data->allowed_ellipse_scaling = AS_ANY;
  129. /* dimensions */
  130. _plotter->data->display_model_type = (int)DISP_MODEL_VIRTUAL;
  131. _plotter->data->display_coors_type = (int)DISP_DEVICE_COORS_INTEGER_NON_LIBXMI;
  132. _plotter->data->flipped_y = false;
  133. /* we choose viewport coor range to be 1/4 of the integer range */
  134. _plotter->data->imin = - ((1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) - 1);
  135. _plotter->data->imax = (1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) - 1;
  136. _plotter->data->jmin = - ((1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) - 1);
  137. _plotter->data->jmax = (1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) - 1;
  138. _plotter->data->xmin = 0.0;
  139. _plotter->data->xmax = 0.0;
  140. _plotter->data->ymin = 0.0;
  141. _plotter->data->ymax = 0.0;
  142. _plotter->data->page_data = (plPageData *)NULL;
  143. /* initialize data members specific to this derived class */
  144. /* parameters */
  145. _plotter->cgm_encoding = CGM_ENCODING_BINARY;
  146. _plotter->cgm_max_version = 4;
  147. /* most important dynamic variables (global) */
  148. _plotter->cgm_version = 1;
  149. _plotter->cgm_profile = CGM_PROFILE_WEB;
  150. _plotter->cgm_need_color = false;
  151. /* corresponding dynamic variables (page-specific, i.e. picture-specific) */
  152. _plotter->cgm_page_version = 1;
  153. _plotter->cgm_page_profile = CGM_PROFILE_WEB;
  154. _plotter->cgm_page_need_color = false;
  155. /* colors (24-bit or 48-bit, initialized to nonphysical or dummy values) */
  156. _plotter->cgm_line_color.red = -1;
  157. _plotter->cgm_line_color.green = -1;
  158. _plotter->cgm_line_color.blue = -1;
  159. _plotter->cgm_edge_color.red = -1;
  160. _plotter->cgm_edge_color.green = -1;
  161. _plotter->cgm_edge_color.blue = -1;
  162. _plotter->cgm_fillcolor.red = -1;
  163. _plotter->cgm_fillcolor.green = -1;
  164. _plotter->cgm_fillcolor.blue = -1;
  165. _plotter->cgm_marker_color.red = -1;
  166. _plotter->cgm_marker_color.green = -1;
  167. _plotter->cgm_marker_color.blue = -1;
  168. _plotter->cgm_text_color.red = -1;
  169. _plotter->cgm_text_color.green = -1;
  170. _plotter->cgm_text_color.blue = -1;
  171. _plotter->cgm_bgcolor.red = -1; /* set in c_begin_page() */
  172. _plotter->cgm_bgcolor.green = -1;
  173. _plotter->cgm_bgcolor.blue = -1;
  174. /* other dynamic variables */
  175. _plotter->cgm_line_type = CGM_L_SOLID;
  176. _plotter->cgm_dash_offset = 0.0;
  177. _plotter->cgm_join_style = CGM_JOIN_UNSPEC;
  178. _plotter->cgm_cap_style = CGM_CAP_UNSPEC;
  179. _plotter->cgm_dash_cap_style = CGM_CAP_UNSPEC;
  180. /* CGM's default line width: 1/1000 times the max VDC dimension */
  181. _plotter->cgm_line_width = (1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) / 500;
  182. _plotter->cgm_interior_style = CGM_INT_STYLE_HOLLOW;
  183. _plotter->cgm_edge_type = CGM_L_SOLID;
  184. _plotter->cgm_edge_dash_offset = 0.0;
  185. _plotter->cgm_edge_join_style = CGM_JOIN_UNSPEC;
  186. _plotter->cgm_edge_cap_style = CGM_CAP_UNSPEC;
  187. _plotter->cgm_edge_dash_cap_style = CGM_CAP_UNSPEC;
  188. /* CGM's default edge width: 1/1000 times the max VDC dimension */
  189. _plotter->cgm_edge_width = (1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) / 500;
  190. _plotter->cgm_edge_is_visible = false;
  191. _plotter->cgm_miter_limit = 32767.0;
  192. _plotter->cgm_marker_type = CGM_M_ASTERISK;
  193. /* CGM's default marker size: 1/1000 times the max VDC dimension */
  194. _plotter->cgm_marker_size = (1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) /500;
  195. /* label-related variables */
  196. _plotter->cgm_char_height = -1; /* impossible (dummy) value */
  197. _plotter->cgm_char_base_vector_x = 1;
  198. _plotter->cgm_char_base_vector_y = 0;
  199. _plotter->cgm_char_up_vector_x = 0;
  200. _plotter->cgm_char_up_vector_y = 1;
  201. _plotter->cgm_horizontal_text_alignment = CGM_ALIGN_NORMAL_HORIZONTAL;
  202. _plotter->cgm_vertical_text_alignment = CGM_ALIGN_NORMAL_VERTICAL;
  203. _plotter->cgm_font_id = -1; /* impossible (dummy) value */
  204. _plotter->cgm_charset_lower = 0; /* dummy value (we use values 1..4) */
  205. _plotter->cgm_charset_upper = 0; /* dummy value (we use values 1..4) */
  206. _plotter->cgm_restricted_text_type = CGM_RESTRICTED_TEXT_TYPE_BASIC;
  207. /* initialize certain data members from device driver parameters */
  208. /* determine page type, and viewport size and location */
  209. _set_page_type (_plotter->data);
  210. /* user may have specified a viewport aspect ratio other than 1:1, so
  211. carefully compute device-space coordinate ranges (i.e. don't use above
  212. default values for imin,imax,jmin,jmax, which are appropriate only for
  213. a square viewport) */
  214. {
  215. /* our choice: the larger side of the viewport will essentially be 1/4
  216. times the maximum range for integer device coordinates, i.e., half
  217. the larger side will be 1/8 times the maximum range */
  218. int half_side = (1 << (8*CGM_BINARY_BYTES_PER_INTEGER - 3)) - 1;
  219. int half_other_side;
  220. double xsize = _plotter->data->viewport_xsize;
  221. double ysize = _plotter->data->viewport_ysize;
  222. int xsign = xsize < 0.0 ? -1 : 1;
  223. int ysign = ysize < 0.0 ? -1 : 1;
  224. double fraction;
  225. /* There are two cases, plus a degenerate case. For each,
  226. `scaling_factor' is the conversion factor from virtual to physical
  227. units. */
  228. if (xsize == 0.0 && ysize == 0.0)
  229. /* degenerate case, scaling_factor = 0 (or anything else :-)) */
  230. {
  231. _plotter->data->imin = 0;
  232. _plotter->data->imax = 0;
  233. _plotter->data->jmin = 0;
  234. _plotter->data->jmax = 0;
  235. }
  236. else if (FABS(ysize) > FABS(xsize))
  237. /* scaling_factor = FABS(ysize) / (2*half_side) */
  238. {
  239. fraction = FABS(xsize) / FABS(ysize);
  240. half_other_side = IROUND(half_side * fraction);
  241. _plotter->data->imin = - xsign * half_other_side;
  242. _plotter->data->imax = xsign * half_other_side;
  243. _plotter->data->jmin = - ysign * half_side;
  244. _plotter->data->jmax = ysign * half_side;
  245. }
  246. else /* FABS(ysize) <= FABS(xsize), which is nonzero */
  247. /* scaling_factor = FABS(xsize) / (2*half_side) */
  248. {
  249. fraction = FABS(ysize) / FABS(xsize);
  250. half_other_side = IROUND(half_side * fraction);
  251. _plotter->data->imin = - xsign * half_side;
  252. _plotter->data->imax = xsign * half_side;
  253. _plotter->data->jmin = - ysign * half_other_side;
  254. _plotter->data->jmax = ysign * half_other_side;
  255. }
  256. }
  257. /* compute the NDC to device-frame affine map, set it in Plotter */
  258. _compute_ndc_to_device_map (_plotter->data);
  259. /* determine CGM encoding */
  260. {
  261. const char* cgm_encoding_type;
  262. cgm_encoding_type =
  263. (const char *)_get_plot_param (_plotter->data, "CGM_ENCODING");
  264. if (cgm_encoding_type != NULL)
  265. {
  266. if (strcmp (cgm_encoding_type, "binary") == 0)
  267. _plotter->cgm_encoding = CGM_ENCODING_BINARY;
  268. else if (strcmp (cgm_encoding_type, "clear text") == 0
  269. || (strcmp (cgm_encoding_type, "cleartext") == 0)
  270. || (strcmp (cgm_encoding_type, "clear_text") == 0))
  271. _plotter->cgm_encoding = CGM_ENCODING_CLEAR_TEXT;
  272. else /* we don't support the character encoding */
  273. _plotter->cgm_encoding = CGM_ENCODING_BINARY;
  274. }
  275. else
  276. _plotter->cgm_encoding = CGM_ENCODING_BINARY; /* default value */
  277. }
  278. /* determine upper bound on CGM version number */
  279. {
  280. const char* cgm_max_version_type;
  281. cgm_max_version_type =
  282. (const char *)_get_plot_param (_plotter->data, "CGM_MAX_VERSION");
  283. if (cgm_max_version_type != NULL)
  284. {
  285. if (strcmp (cgm_max_version_type, "1") == 0)
  286. _plotter->cgm_max_version = 1;
  287. else if (strcmp (cgm_max_version_type, "2") == 0)
  288. _plotter->cgm_max_version = 2;
  289. else if (strcmp (cgm_max_version_type, "3") == 0)
  290. _plotter->cgm_max_version = 3;
  291. else if (strcmp (cgm_max_version_type, "4") == 0)
  292. _plotter->cgm_max_version = 4;
  293. else /* use default */
  294. _plotter->cgm_max_version = 4;
  295. }
  296. else
  297. _plotter->cgm_max_version = 4; /* use default */
  298. }
  299. /* If the maximum CGM version number is greater than 1, relax the
  300. constraints on what path segments can be stored in libplot's path
  301. buffer. By default, we allow only line segments. */
  302. /* Counterclockwise circular arcs have been in the CGM standard since
  303. version 1, but clockwise circular arcs were only added in version 2.
  304. To include a circular arc we insist on a uniform map from user to
  305. device coordinates, since otherwise it wouldn't be mapped to a
  306. circle.
  307. Similarly, we don't allow elliptic arcs into the arc buffer unless the
  308. version is 2 or higher. Elliptic arcs have been in the standard since
  309. version 1, but the `closed figure' construction that we use to fill
  310. single elliptic (and circular!) arcs was only added in version 2. */
  311. if (_plotter->cgm_max_version >= 2)
  312. {
  313. _plotter->data->allowed_arc_scaling = AS_UNIFORM;
  314. _plotter->data->allowed_ellarc_scaling = AS_ANY;
  315. }
  316. /* Bezier cubics were added to the standard in version 3. Closed mixed
  317. paths (`closed figures' in CGM jargon) have been in the standard since
  318. version 2, but open mixed paths (`compound lines' in CGM jargon) were
  319. only added in version 3. */
  320. if (_plotter->cgm_max_version >= 3)
  321. {
  322. _plotter->data->allowed_cubic_scaling = AS_ANY;
  323. _plotter->data->have_mixed_paths = true;
  324. }
  325. /* Beginning in version 3 CGM's, user can define line types, by
  326. specifying a precise dashing style. */
  327. if (_plotter->cgm_max_version >= 3)
  328. _plotter->data->have_dash_array = 1;
  329. }
  330. /* Lists of metafile elements that we use, indexed by the CGM version less
  331. unity, i.e., by 0,1,2,3 for CGM versions 1,2,3,4. We use a standard
  332. shorthand: element class -1, and element id 0, 1, 2, etc., specifies
  333. certain sets of metafile elements. E.g., class=-1 and id=1 specifies
  334. all the version-1 metafile elements; in the clear text encoding this is
  335. written as "DRAWINGPLUS". */
  336. #define MAX_CGM_ELEMENT_LIST_LENGTH 1
  337. typedef struct
  338. {
  339. const char *text_string;
  340. int length; /* number of genuine entries in list */
  341. int class_id[MAX_CGM_ELEMENT_LIST_LENGTH];
  342. int element_id[MAX_CGM_ELEMENT_LIST_LENGTH];
  343. }
  344. plCGMElementList;
  345. static const plCGMElementList _metafile_element_list[4] =
  346. {
  347. /* version 1 */
  348. { "DRAWINGPLUS", 1, {-1}, {1} },
  349. /* version 2 */
  350. { "VERSION2", 1, {-1}, {2} },
  351. /* version 3 */
  352. { "VERSION3", 1, {-1}, {5} },
  353. /* version 4 */
  354. { "VERSION4", 1, {-1}, {6} }
  355. };
  356. /* CGM character sets, for upper and lower halves of both ISO-Latin-1 and
  357. Symbol fonts. We use standard 8-bit encoding when writing text strings
  358. in either sort of font, but the character set used in each font half
  359. needs to be specified explicitly. Supported types of character set
  360. include "standard 94-character set" and "standard 96-character set".
  361. The character set is further specified by the "tail" string. */
  362. typedef struct
  363. {
  364. int type; /* a CGM enumerative */
  365. const char *type_string; /* its string representation, for cleartext */
  366. const char *tail; /* the `designation sequence tail' */
  367. }
  368. plCGMCharset;
  369. static const plCGMCharset _iso_latin_1_cgm_charset[2] =
  370. {
  371. { 0, "std94", "4/2" }, /* ISO 8859-1 LH, tail is "A" */
  372. { 1, "std96", "4/1" } /* ISO 8859-1 RH, tail is "B" */
  373. };
  374. static const plCGMCharset _symbol_cgm_charset[2] =
  375. {
  376. { 0, "std94", "2/10 3/10" }, /* Symbol LH, tail is "*:" */
  377. { 0, "std94", "2/6 3/10" } /* Symbol RH, tail is "&:" */
  378. };
  379. /* The private `terminate' method, which is invoked when a Plotter is
  380. deleted. It may do such things as write to an output stream from
  381. internal storage, deallocate storage, etc. When this is invoked,
  382. _plotter points to the Plotter that is about to be deleted. */
  383. /* This version is for CGM Plotters...
  384. (CGM Plotters differ from most other plotters that do not plot in real
  385. time in that they emit output only after all pages have pages have been
  386. drawn, rather than at the end of each page. This is necessary in order
  387. to produce the correct header lines.)
  388. When this is called, the CGM code for the body of each page is stored in
  389. a plOutbuf, and the page plOutbufs form a linked list. */
  390. void
  391. _pl_c_terminate (S___(Plotter *_plotter))
  392. {
  393. int i;
  394. plOutbuf *current_page;
  395. bool ps_font_used_in_doc[PL_NUM_PS_FONTS];
  396. bool symbol_font_used_in_doc;
  397. bool cgm_font_id_used_in_doc[PL_NUM_PS_FONTS];
  398. bool doc_uses_fonts;
  399. int max_cgm_font_id;
  400. /* if no pages of graphics (i.e. Plotter was never opened), CGM file
  401. won't contain any pictures, and won't satisfy any standard profile */
  402. if (_plotter->data->first_page == (plOutbuf *)NULL)
  403. _plotter->cgm_profile =
  404. IMAX(_plotter->cgm_profile, CGM_PROFILE_NONE);
  405. /* COMMENTED OUT BECAUSE USERS WOULD FIND THIS TOO CONFUSING! */
  406. #if 0
  407. /* only the binary encoding satisfies the WebCGM profile */
  408. if (_plotter->cgm_encoding != CGM_ENCODING_BINARY)
  409. _plotter->cgm_profile =
  410. IMAX(_plotter->cgm_profile, CGM_PROFILE_MODEL);
  411. #endif
  412. #ifdef LIBPLOTTER
  413. if (_plotter->data->outfp || _plotter->data->outstream)
  414. #else
  415. if (_plotter->data->outfp)
  416. #endif
  417. /* have an output stream, will emit CGM commands */
  418. {
  419. plOutbuf *doc_header, *doc_trailer;
  420. int byte_count, data_byte_count, data_len, string_length;
  421. doc_header = _new_outbuf ();
  422. /* emit "BEGIN METAFILE" command */
  423. {
  424. const char *string_param;
  425. string_param = "CGM plot";
  426. string_length = strlen (string_param);
  427. data_len = CGM_BINARY_BYTES_PER_STRING(string_length);
  428. byte_count = data_byte_count = 0;
  429. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  430. CGM_DELIMITER_ELEMENT, 1,
  431. data_len, &byte_count,
  432. "BEGMF");
  433. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  434. string_param,
  435. string_length, true,
  436. data_len, &data_byte_count, &byte_count);
  437. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  438. &byte_count);
  439. }
  440. /* emit "METAFILE VERSION" command */
  441. {
  442. data_len = CGM_BINARY_BYTES_PER_INTEGER;
  443. byte_count = data_byte_count = 0;
  444. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  445. CGM_METAFILE_DESCRIPTOR_ELEMENT, 1,
  446. data_len, &byte_count,
  447. "MFVERSION");
  448. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  449. _plotter->cgm_version,
  450. data_len, &data_byte_count, &byte_count);
  451. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  452. &byte_count);
  453. }
  454. /* emit "METAFILE ELEMENT LIST" command; this is encoding-dependent */
  455. {
  456. const plCGMElementList *element_list =
  457. &(_metafile_element_list[_plotter->cgm_version - 1]);
  458. int length = element_list->length;
  459. int k;
  460. /* 1 integer, plus `length' pairs of 2-byte indices */
  461. data_len = CGM_BINARY_BYTES_PER_INTEGER + 2 * 2 * length;
  462. byte_count = data_byte_count = 0;
  463. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  464. CGM_METAFILE_DESCRIPTOR_ELEMENT, 11,
  465. data_len, &byte_count,
  466. "MFELEMLIST");
  467. switch (_plotter->cgm_encoding)
  468. {
  469. case CGM_ENCODING_BINARY:
  470. default:
  471. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  472. length,
  473. data_len, &data_byte_count, &byte_count);
  474. for (k = 0; k < length; k++)
  475. {
  476. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  477. element_list->class_id[k],
  478. data_len, &data_byte_count, &byte_count);
  479. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  480. element_list->element_id[k],
  481. data_len, &data_byte_count, &byte_count);
  482. }
  483. break;
  484. case CGM_ENCODING_CHARACTER: /* not supported */
  485. break;
  486. case CGM_ENCODING_CLEAR_TEXT:
  487. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  488. element_list->text_string,
  489. (int) strlen (element_list->text_string),
  490. true,
  491. data_len, &data_byte_count, &byte_count);
  492. break;
  493. }
  494. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  495. &byte_count);
  496. }
  497. /* emit "METAFILE DESCRIPTION" command, including profile string etc. */
  498. {
  499. time_t clock;
  500. const char *profile_string, *profile_edition_string;
  501. char string_param[254];
  502. struct tm *local_time_struct_ptr;
  503. #ifdef HAVE_LOCALTIME_R
  504. struct tm local_time_struct;
  505. #endif
  506. /* Work out ASCII specification of profile */
  507. switch (_plotter->cgm_profile)
  508. {
  509. case CGM_PROFILE_WEB:
  510. profile_string = "WebCGM";
  511. profile_edition_string = "1.0";
  512. break;
  513. case CGM_PROFILE_MODEL:
  514. profile_string = "Model-Profile";
  515. profile_edition_string = "1";
  516. break;
  517. case CGM_PROFILE_NONE:
  518. default:
  519. profile_string = "None";
  520. profile_edition_string = "0.0"; /* waggish */
  521. break;
  522. }
  523. /* Compute an ASCII representation of the current time, in a
  524. reentrant way if we're supporting pthreads (i.e. by using
  525. localtime_r if it's available). */
  526. time (&clock);
  527. #ifdef PTHREAD_SUPPORT
  528. #ifdef HAVE_PTHREAD_H
  529. #ifdef HAVE_LOCALTIME_R
  530. localtime_r (&clock, &local_time_struct);
  531. local_time_struct_ptr = &local_time_struct;
  532. #else
  533. local_time_struct_ptr = localtime (&clock);
  534. #endif
  535. #else /* not HAVE_PTHREAD_H */
  536. local_time_struct_ptr = localtime (&clock);
  537. #endif /* not HAVE_PTHREAD_H */
  538. #else /* not PTHREAD_SUPPORT */
  539. local_time_struct_ptr = localtime (&clock);
  540. #endif /* not PTHREAD_SUPPORT */
  541. sprintf (string_param,
  542. "\"ProfileId:%s\" \"ProfileEd:%s\" \"ColourClass:%s\" \"Source:GNU libplot %s\" \"Date:%04d%02d%02d\"",
  543. profile_string, profile_edition_string,
  544. _plotter->cgm_need_color ? "colour" : "monochrome",
  545. PL_LIBPLOT_VER_STRING,
  546. 1900 + local_time_struct_ptr->tm_year,
  547. 1 + local_time_struct_ptr->tm_mon,
  548. local_time_struct_ptr->tm_mday);
  549. string_length = strlen (string_param);
  550. data_len = CGM_BINARY_BYTES_PER_STRING(string_length);
  551. byte_count = data_byte_count = 0;
  552. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  553. CGM_METAFILE_DESCRIPTOR_ELEMENT, 2,
  554. data_len, &byte_count,
  555. "MFDESC");
  556. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  557. string_param,
  558. string_length, false, /* delimit by single quotes */
  559. data_len, &data_byte_count, &byte_count);
  560. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  561. &byte_count);
  562. }
  563. /* emit "VDC TYPE" command, selecting integer VDC's for the metafile */
  564. {
  565. data_len = 2; /* 2 bytes per enum */
  566. byte_count = data_byte_count = 0;
  567. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  568. CGM_METAFILE_DESCRIPTOR_ELEMENT, 3,
  569. data_len, &byte_count,
  570. "VDCTYPE");
  571. _cgm_emit_enum (doc_header, false, _plotter->cgm_encoding,
  572. 0,
  573. data_len, &data_byte_count, &byte_count,
  574. "integer");
  575. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  576. &byte_count);
  577. }
  578. /* Emit "INTEGER PRECISION" command. Parameters are
  579. encoding-dependent: in the binary encoding, the number of bits k,
  580. and in clear text, a pair of integers: the minimum and maximum
  581. integers representable in CGM format, i.e. -(2^(k-1) - 1) and
  582. (2^(k-1) - 1), where k=8*CGM_BINARY_BYTES_PER_INTEGER. */
  583. {
  584. int j, max_int;
  585. data_len = 2; /* in binary, 16 bits of data; see comment */
  586. byte_count = data_byte_count = 0;
  587. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  588. CGM_METAFILE_DESCRIPTOR_ELEMENT, 4,
  589. data_len, &byte_count,
  590. "INTEGERPREC");
  591. switch (_plotter->cgm_encoding)
  592. {
  593. case CGM_ENCODING_BINARY:
  594. default:
  595. /* The integer precision, in terms of bits, should be encoded
  596. as an integer at the current precision (the default, which
  597. is 16 bits), not the eventual precision. So we don't call
  598. _cgm_emit_integer; we call _cgm_emit_index instead. We
  599. always represent indices by 16 bits (the default). */
  600. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  601. 8 * CGM_BINARY_BYTES_PER_INTEGER,
  602. data_len, &data_byte_count, &byte_count);
  603. break;
  604. case CGM_ENCODING_CHARACTER: /* not supported */
  605. break;
  606. case CGM_ENCODING_CLEAR_TEXT:
  607. max_int = 0;
  608. for (j = 0; j < (8 * CGM_BINARY_BYTES_PER_INTEGER - 1); j++)
  609. max_int += (1 << j);
  610. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  611. -max_int,
  612. data_len, &data_byte_count, &byte_count);
  613. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  614. max_int,
  615. data_len, &data_byte_count, &byte_count);
  616. break;
  617. }
  618. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  619. &byte_count);
  620. }
  621. /* Emit "REAL PRECISION" command, selecting default precision.
  622. Parameters are encoding-dependent. In the clear text encoding,
  623. three numbers: the minimum real, the maximum real, and the number
  624. of significant decimal digits (an integer). Typical choices are
  625. (-32767.0, 32767.0, 4) [the default]. In the binary encoding,
  626. three objects: a 2-octet enumerative specifying the encoding of
  627. reals (0=floating, 1=fixed), and two integers at current intege
  628. precision specifying the size of each piece of the encoded real.
  629. Typical choices are (1,16,16) [the default] or (0,9,23). */
  630. {
  631. data_len = 2 + 2 * CGM_BINARY_BYTES_PER_INTEGER;
  632. byte_count = data_byte_count = 0;
  633. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  634. CGM_METAFILE_DESCRIPTOR_ELEMENT, 5,
  635. data_len, &byte_count,
  636. "REALPREC");
  637. switch (_plotter->cgm_encoding)
  638. {
  639. case CGM_ENCODING_BINARY:
  640. default:
  641. _cgm_emit_enum (doc_header, false, _plotter->cgm_encoding,
  642. 1,
  643. data_len, &data_byte_count, &byte_count,
  644. "DUMMY");
  645. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  646. 16,
  647. data_len, &data_byte_count, &byte_count);
  648. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  649. 16,
  650. data_len, &data_byte_count, &byte_count);
  651. break;
  652. case CGM_ENCODING_CHARACTER: /* not supported */
  653. break;
  654. case CGM_ENCODING_CLEAR_TEXT:
  655. _cgm_emit_real_fixed_point (doc_header, false, _plotter->cgm_encoding,
  656. -32767.0,
  657. data_len, &data_byte_count, &byte_count);
  658. _cgm_emit_real_fixed_point (doc_header, false, _plotter->cgm_encoding,
  659. 32767.0,
  660. data_len, &data_byte_count, &byte_count);
  661. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  662. 4,
  663. data_len, &data_byte_count, &byte_count);
  664. break;
  665. }
  666. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  667. &byte_count);
  668. }
  669. /* Emit "COLOR PRECISION" command. Parameters are
  670. encoding-dependent: in the binary encoding, an integer specifying
  671. the number of bits k, and in clear text, the maximum possible
  672. color component value, i.e. (2^k - 1), where
  673. k=8*CGM_BINARY_BYTES_PER_COLOR_COMPONENT. */
  674. {
  675. int j;
  676. unsigned int max_component;
  677. data_len = CGM_BINARY_BYTES_PER_INTEGER;
  678. byte_count = data_byte_count = 0;
  679. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  680. CGM_METAFILE_DESCRIPTOR_ELEMENT, 7,
  681. data_len, &byte_count,
  682. "COLRPREC");
  683. switch (_plotter->cgm_encoding)
  684. {
  685. case CGM_ENCODING_BINARY:
  686. default:
  687. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  688. 8 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT,
  689. data_len, &data_byte_count, &byte_count);
  690. break;
  691. case CGM_ENCODING_CHARACTER: /* not supported */
  692. break;
  693. case CGM_ENCODING_CLEAR_TEXT:
  694. max_component = 0;
  695. for (j = 0; j < (8 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT); j++)
  696. max_component += (1 << j);
  697. _cgm_emit_unsigned_integer (doc_header, false, _plotter->cgm_encoding,
  698. max_component,
  699. data_len, &data_byte_count, &byte_count);
  700. break;
  701. }
  702. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  703. &byte_count);
  704. }
  705. /* emit "COLOR VALUE EXTENT" command, duplicating the information
  706. we just supplied (this is necessary) */
  707. {
  708. int j;
  709. unsigned int max_component;
  710. data_len = 6 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
  711. byte_count = data_byte_count = 0;
  712. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  713. CGM_METAFILE_DESCRIPTOR_ELEMENT, 10,
  714. data_len, &byte_count,
  715. "COLRVALUEEXT");
  716. _cgm_emit_color_component (doc_header, false, _plotter->cgm_encoding,
  717. (unsigned int)0,
  718. data_len, &data_byte_count, &byte_count);
  719. _cgm_emit_color_component (doc_header, false, _plotter->cgm_encoding,
  720. (unsigned int)0,
  721. data_len, &data_byte_count, &byte_count);
  722. _cgm_emit_color_component (doc_header, false, _plotter->cgm_encoding,
  723. (unsigned int)0,
  724. data_len, &data_byte_count, &byte_count);
  725. max_component = 0;
  726. for (j = 0; j < (8 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT); j++)
  727. max_component += (1 << j);
  728. _cgm_emit_color_component (doc_header, false, _plotter->cgm_encoding,
  729. max_component,
  730. data_len, &data_byte_count, &byte_count);
  731. _cgm_emit_color_component (doc_header, false, _plotter->cgm_encoding,
  732. max_component,
  733. data_len, &data_byte_count, &byte_count);
  734. _cgm_emit_color_component (doc_header, false, _plotter->cgm_encoding,
  735. max_component,
  736. data_len, &data_byte_count, &byte_count);
  737. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  738. &byte_count);
  739. }
  740. /* determine fonts needed by document, by examining all pages */
  741. {
  742. current_page = _plotter->data->first_page;
  743. for (i = 0; i < PL_NUM_PS_FONTS; i++)
  744. ps_font_used_in_doc[i] = false;
  745. while (current_page)
  746. {
  747. for (i = 0; i < PL_NUM_PS_FONTS; i++)
  748. if (current_page->ps_font_used[i])
  749. ps_font_used_in_doc[i] = true;
  750. current_page = current_page->next;
  751. }
  752. }
  753. /* Map our internal indexing of PS fonts to the indexing we use in a
  754. CGM file (which may be different, because we want the traditional
  755. `Adobe 13' to come first, out of the `Adobe 35'). Also work out
  756. whether Symbol font, which has its own character sets, is used. */
  757. symbol_font_used_in_doc = false;
  758. for (i = 0; i < PL_NUM_PS_FONTS; i++)
  759. {
  760. cgm_font_id_used_in_doc[_pl_g_ps_font_to_cgm_font_id[i]] = ps_font_used_in_doc[i];
  761. if (ps_font_used_in_doc[i]
  762. && strcmp (_pl_g_ps_font_info[i].ps_name, "Symbol") == 0)
  763. symbol_font_used_in_doc = true;
  764. }
  765. /* compute maximum used font id, if any */
  766. max_cgm_font_id = 0;
  767. doc_uses_fonts = false;
  768. for (i = 0; i < PL_NUM_PS_FONTS; i++)
  769. {
  770. if (cgm_font_id_used_in_doc[i] == true)
  771. {
  772. doc_uses_fonts = true;
  773. max_cgm_font_id = i;
  774. }
  775. }
  776. if (doc_uses_fonts)
  777. {
  778. /* emit "FONT LIST" command */
  779. /* command will include encoded strings, which are the names of
  780. fonts in range 0..max_cgm_font_id; later in the CGM file,
  781. they'll be referred to as 1..max_cgm_font_id+1 */
  782. data_len = 0;
  783. for (i = 0; i <= max_cgm_font_id; i++)
  784. {
  785. int ps_font_index;
  786. int font_name_length, encoded_font_name_length;
  787. ps_font_index = _pl_g_cgm_font_id_to_ps_font[i];
  788. font_name_length = (int) strlen (_pl_g_ps_font_info[ps_font_index].ps_name);
  789. encoded_font_name_length =
  790. CGM_BINARY_BYTES_PER_STRING(font_name_length);
  791. data_len += encoded_font_name_length;
  792. }
  793. byte_count = data_byte_count = 0;
  794. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  795. CGM_METAFILE_DESCRIPTOR_ELEMENT, 13,
  796. data_len, &byte_count,
  797. "FONTLIST");
  798. for (i = 0; i <= max_cgm_font_id; i++)
  799. {
  800. int ps_font_index;
  801. ps_font_index = _pl_g_cgm_font_id_to_ps_font[i];
  802. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  803. _pl_g_ps_font_info[ps_font_index].ps_name,
  804. (int) strlen (_pl_g_ps_font_info[ps_font_index].ps_name),
  805. true,
  806. data_len, &data_byte_count, &byte_count);
  807. }
  808. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  809. &byte_count);
  810. if (_plotter->cgm_version >= 3)
  811. /* emit version-3 "FONT PROPERTIES" commands; note that if
  812. fonts are used and CGM_MAX_VERSION is >=3, then cgm_version
  813. was previously bumped up to 3 in c_closepl.c */
  814. {
  815. /* For each font in the font list, we specify 7 properties.
  816. Each "FONT PROPERTIES" command refers to a single font.
  817. Its argument list is a sequence of 3-tuples, each being of
  818. the form (property type [an index], priority [an integer],
  819. value), where `value' is an SDR (structured data record).
  820. One of the supported property types is
  821. CGM_FONT_PROP_INDEX, the value of which is an index into
  822. the font list. For any invocation of the command, this
  823. property type and its value must be supplied.
  824. An SDR is a string-encoded structure, and each `run' of a
  825. single CGM datatype within an SDR is encoded as (A) an
  826. identifier for the datatype [an index], (B) a count of the
  827. number of occurrences [an integer], and (C) the
  828. occurrences of the datatype, themselves. So in the binary
  829. encoding, bytes per SDR equals
  830. 2 + bytes_per_integer + data_bytes, since we always encode
  831. CGM indices as 2 bytes.
  832. The only SDR's that occur in this context are
  833. (1) single CGM indices [used for 5 of the 7 font properties],
  834. (2) single CGM strings [used for the `family' property], and
  835. (3) three 8-bit unsigned integers [used for the font's
  836. `design group'].
  837. Because we always encode CGM indices as 2 bytes, bytes
  838. per SDR, in the binary encoding, in these 3 cases are:
  839. (1) 1+ CGM_BINARY_BYTES_PER_INTEGER + 4,
  840. (2) 1+ CGM_BINARY_BYTES_PER_INTEGER + 2 + CGM_BYTES_PER_STRING
  841. (3) 1+ CGM_BINARY_BYTES_PER_INTEGER + 5.
  842. (This takes account of the initial byte used for the
  843. string encoding of the SDR.)
  844. And bytes per 3-tuple in these three cases are:
  845. (1) 2*CGM_BINARY_BYTES_PER_INTEGER + 7,
  846. (2) 2*CGM_BINARY_BYTES_PER_INTEGER + 5 + CGM_BYTES_PER_STRING
  847. (3) 2*CGM_BINARY_BYTES_PER_INTEGER + 8.
  848. Since for every included font, we emit 5 3-tuples of type 1,
  849. 1 of type 2, and 1 of type 3, bytes per emitted font equals
  850. 14*CGM_BINARY_BYTES_PER_INTEGER + 48 + CGM_BYTES_PER_STRING().
  851. In the binary encoding, this is the length, in bytes, of
  852. the argument list of each "FONT PROPERTIES" command. Here
  853. CGM_BYTES_PER_STRING() stands for the string-encoded
  854. length of the family name of the font. */
  855. for (i = 0; i <= max_cgm_font_id; i++)
  856. {
  857. int family_length;
  858. plOutbuf *sdr_buffer;
  859. family_length = strlen(_pl_g_cgm_font_properties[i].family);
  860. data_len = (14 * CGM_BINARY_BYTES_PER_INTEGER
  861. + 48 /* hardcoded constants; see above */
  862. + CGM_BINARY_BYTES_PER_STRING(family_length));
  863. byte_count = data_byte_count = 0;
  864. sdr_buffer = _new_outbuf ();
  865. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  866. CGM_METAFILE_DESCRIPTOR_ELEMENT, 21,
  867. data_len, &byte_count,
  868. "FONTPROP");
  869. /* now emit a sequence of 3-tuples: (index, integer, SDR);
  870. for each 2nd element (a priority), we just specify `1' */
  871. /* specify index of font in table (beginning with 1, not
  872. with 0) */
  873. {
  874. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  875. CGM_FONT_PROP_INDEX,
  876. data_len, &data_byte_count, &byte_count);
  877. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  878. 1, /* priority */
  879. data_len, &data_byte_count, &byte_count);
  880. build_sdr_from_index (sdr_buffer, _plotter->cgm_encoding,
  881. i + 1); /* add 1 to index */
  882. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  883. sdr_buffer->base,
  884. (int)(sdr_buffer->contents),
  885. false,
  886. data_len, &data_byte_count, &byte_count);
  887. _reset_outbuf (sdr_buffer);
  888. }
  889. /* specify font family */
  890. {
  891. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  892. CGM_FONT_PROP_FAMILY,
  893. data_len, &data_byte_count, &byte_count);
  894. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  895. 1,
  896. data_len, &data_byte_count, &byte_count);
  897. build_sdr_from_string (sdr_buffer, _plotter->cgm_encoding,
  898. _pl_g_cgm_font_properties[i].family,
  899. (int)(strlen (_pl_g_cgm_font_properties[i].family)),
  900. true); /* use double quotes */
  901. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  902. sdr_buffer->base,
  903. (int)(sdr_buffer->contents),
  904. false,
  905. data_len, &data_byte_count, &byte_count);
  906. _reset_outbuf (sdr_buffer);
  907. }
  908. /* specify font posture */
  909. {
  910. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  911. CGM_FONT_PROP_POSTURE,
  912. data_len, &data_byte_count, &byte_count);
  913. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  914. 1,
  915. data_len, &data_byte_count, &byte_count);
  916. build_sdr_from_index (sdr_buffer, _plotter->cgm_encoding,
  917. _pl_g_cgm_font_properties[i].posture);
  918. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  919. sdr_buffer->base,
  920. (int)(sdr_buffer->contents),
  921. false,
  922. data_len, &data_byte_count, &byte_count);
  923. _reset_outbuf (sdr_buffer);
  924. }
  925. /* specify font weight */
  926. {
  927. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  928. CGM_FONT_PROP_WEIGHT,
  929. data_len, &data_byte_count, &byte_count);
  930. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  931. 1,
  932. data_len, &data_byte_count, &byte_count);
  933. build_sdr_from_index (sdr_buffer, _plotter->cgm_encoding,
  934. _pl_g_cgm_font_properties[i].weight);
  935. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  936. sdr_buffer->base,
  937. (int)(sdr_buffer->contents),
  938. false,
  939. data_len, &data_byte_count, &byte_count);
  940. _reset_outbuf (sdr_buffer);
  941. }
  942. /* specify font width */
  943. {
  944. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  945. CGM_FONT_PROP_WIDTH,
  946. data_len, &data_byte_count, &byte_count);
  947. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  948. 1,
  949. data_len, &data_byte_count, &byte_count);
  950. build_sdr_from_index (sdr_buffer, _plotter->cgm_encoding,
  951. _pl_g_cgm_font_properties[i].proportionate_width);
  952. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  953. sdr_buffer->base,
  954. (int)(sdr_buffer->contents),
  955. false,
  956. data_len, &data_byte_count, &byte_count);
  957. _reset_outbuf (sdr_buffer);
  958. }
  959. /* specify font design group */
  960. {
  961. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  962. CGM_FONT_PROP_DESIGN_GROUP,
  963. data_len, &data_byte_count, &byte_count);
  964. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  965. 1,
  966. data_len, &data_byte_count, &byte_count);
  967. build_sdr_from_ui8s (sdr_buffer, _plotter->cgm_encoding,
  968. _pl_g_cgm_font_properties[i].design_group,
  969. 3);
  970. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  971. sdr_buffer->base,
  972. (int)(sdr_buffer->contents),
  973. false,
  974. data_len, &data_byte_count, &byte_count);
  975. _reset_outbuf (sdr_buffer);
  976. }
  977. /* specify font structure */
  978. {
  979. _cgm_emit_index (doc_header, false, _plotter->cgm_encoding,
  980. CGM_FONT_PROP_STRUCTURE,
  981. data_len, &data_byte_count, &byte_count);
  982. _cgm_emit_integer (doc_header, false, _plotter->cgm_encoding,
  983. 1,
  984. data_len, &data_byte_count, &byte_count);
  985. build_sdr_from_index (sdr_buffer, _plotter->cgm_encoding,
  986. _pl_g_cgm_font_properties[i].structure);
  987. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  988. sdr_buffer->base,
  989. (int)(sdr_buffer->contents),
  990. false,
  991. data_len, &data_byte_count, &byte_count);
  992. _reset_outbuf (sdr_buffer);
  993. }
  994. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  995. &byte_count);
  996. _delete_outbuf (sdr_buffer);
  997. }
  998. }
  999. /* Emit a "CHARACTER SET LIST" command. Argument list is a
  1000. sequence of character sets, with each character set being
  1001. expressed both as a CGM enumerative and a CGM string (the
  1002. `designation sequence tail').
  1003. We include the 2 character sets used in the 8-bit ISO-Latin-1
  1004. encoding, and, if we're using the Symbol font, the two
  1005. character sets that Symbol uses, also. So internally, we
  1006. index these character sets by 1,2,3,4 (the latter two may not
  1007. be present). */
  1008. data_len = 0;
  1009. for (i = 0; i < 2; i++)
  1010. {
  1011. int tail_length, encoded_tail_length;
  1012. data_len += 2; /* 2 bytes per enum */
  1013. tail_length = strlen (_iso_latin_1_cgm_charset[i].tail);
  1014. encoded_tail_length =
  1015. CGM_BINARY_BYTES_PER_STRING(tail_length);
  1016. data_len += encoded_tail_length;
  1017. }
  1018. if (symbol_font_used_in_doc)
  1019. for (i = 0; i < 2; i++)
  1020. {
  1021. int tail_length, encoded_tail_length;
  1022. data_len += 2; /* 2 bytes per enum */
  1023. tail_length = (int) strlen (_symbol_cgm_charset[i].tail);
  1024. encoded_tail_length =
  1025. CGM_BINARY_BYTES_PER_STRING(tail_length);
  1026. data_len += encoded_tail_length;
  1027. }
  1028. byte_count = data_byte_count = 0;
  1029. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  1030. CGM_METAFILE_DESCRIPTOR_ELEMENT, 14,
  1031. data_len, &byte_count,
  1032. "CHARSETLIST");
  1033. for (i = 0; i < 2; i++)
  1034. {
  1035. _cgm_emit_enum (doc_header, false, _plotter->cgm_encoding,
  1036. _iso_latin_1_cgm_charset[i].type,
  1037. data_len, &data_byte_count, &byte_count,
  1038. _iso_latin_1_cgm_charset[i].type_string);
  1039. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  1040. _iso_latin_1_cgm_charset[i].tail,
  1041. (int) strlen (_iso_latin_1_cgm_charset[i].tail),
  1042. true,
  1043. data_len, &data_byte_count, &byte_count);
  1044. }
  1045. if (symbol_font_used_in_doc)
  1046. for (i = 0; i < 2; i++)
  1047. {
  1048. _cgm_emit_enum (doc_header, false, _plotter->cgm_encoding,
  1049. _symbol_cgm_charset[i].type,
  1050. data_len, &data_byte_count, &byte_count,
  1051. _symbol_cgm_charset[i].type_string);
  1052. _cgm_emit_string (doc_header, false, _plotter->cgm_encoding,
  1053. _symbol_cgm_charset[i].tail,
  1054. (int) strlen (_symbol_cgm_charset[i].tail),
  1055. true,
  1056. data_len, &data_byte_count, &byte_count);
  1057. }
  1058. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  1059. &byte_count);
  1060. }
  1061. /* emit "CHARACTER CODING ANNOUNCER" command, selecting 8-bit
  1062. character codes (no switching between font halves for us!) */
  1063. {
  1064. data_len = 2; /* 2 bytes per enum */
  1065. byte_count = data_byte_count = 0;
  1066. _cgm_emit_command_header (doc_header, _plotter->cgm_encoding,
  1067. CGM_METAFILE_DESCRIPTOR_ELEMENT, 15,
  1068. data_len, &byte_count,
  1069. "CHARCODING");
  1070. _cgm_emit_enum (doc_header, false, _plotter->cgm_encoding,
  1071. 1,
  1072. data_len, &data_byte_count, &byte_count,
  1073. "basic8bit");
  1074. _cgm_emit_command_terminator (doc_header, _plotter->cgm_encoding,
  1075. &byte_count);
  1076. }
  1077. /* WRITE DOCUMENT HEADER */
  1078. _write_bytes (_plotter->data,
  1079. (int)(doc_header->contents),
  1080. (unsigned char *)doc_header->base);
  1081. _delete_outbuf (doc_header);
  1082. /* loop over plOutbufs in which successive pages of graphics are
  1083. stored; emit each page as a CGM picture, and delete each plOutbuf
  1084. as we finish with it */
  1085. current_page = _plotter->data->first_page;
  1086. i = 1;
  1087. while (current_page)
  1088. {
  1089. plOutbuf *next_page;
  1090. plOutbuf *current_page_header, *current_page_trailer;
  1091. /* prepare a page header */
  1092. current_page_header = _new_outbuf ();
  1093. /* emit "BEGIN PICTURE" command */
  1094. {
  1095. char picture[32];
  1096. const char *string_param;
  1097. sprintf (picture, "picture_%d", i);
  1098. string_param = picture;
  1099. string_length = strlen (string_param);
  1100. data_len = CGM_BINARY_BYTES_PER_STRING(string_length);
  1101. byte_count = data_byte_count = 0;
  1102. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1103. CGM_DELIMITER_ELEMENT, 3,
  1104. data_len, &byte_count,
  1105. "BEGPIC");
  1106. _cgm_emit_string (current_page_header, false, _plotter->cgm_encoding,
  1107. string_param,
  1108. string_length,
  1109. true,
  1110. data_len, &data_byte_count, &byte_count);
  1111. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1112. &byte_count);
  1113. }
  1114. /* emit "VDC EXTENT" command [specify virtual device coor ranges] */
  1115. {
  1116. int imin_true, imax_true, jmin_true, jmax_true;
  1117. data_len = 2 * 2 * 2;
  1118. byte_count = data_byte_count = 0;
  1119. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1120. CGM_PICTURE_DESCRIPTOR_ELEMENT, 6,
  1121. data_len, &byte_count,
  1122. "VDCEXT");
  1123. /* To see how we set extent values, see the initialize()
  1124. routine. In that routine, we choose device-coordinates
  1125. ranges imin,imax,jmin,jmax so that the length of the longer
  1126. side of the viewport is one-fourth the maximum integer
  1127. range. The other side will be reduced if the user specifies
  1128. (via PAGESIZE) an aspect ratio other than 1:1. With this
  1129. scheme, if the user specifies xsize=ysize > 0, the same
  1130. user->device coordinate map will result, no matter what
  1131. xsize and ysize equal.
  1132. However, if the user specifies (via PAGESIZE) a negative
  1133. xsize or ysize, we flip the sign of imax-imin or jmax-jmin,
  1134. thereby greatly modifying the user->device coordinate map.
  1135. Which means we must flip it back, right here, before
  1136. emitting the VDC extents. The reason for this sign-flipping
  1137. is that we don't trust CGM viewers to handle negative VDC
  1138. extents. */
  1139. if (_plotter->data->imax < _plotter->data->imin)
  1140. {
  1141. imin_true = _plotter->data->imax;
  1142. imax_true = _plotter->data->imin;
  1143. }
  1144. else
  1145. {
  1146. imin_true = _plotter->data->imin;
  1147. imax_true = _plotter->data->imax;
  1148. }
  1149. if (_plotter->data->jmax < _plotter->data->jmin)
  1150. {
  1151. jmin_true = _plotter->data->jmax;
  1152. jmax_true = _plotter->data->jmin;
  1153. }
  1154. else
  1155. {
  1156. jmin_true = _plotter->data->jmin;
  1157. jmax_true = _plotter->data->jmax;
  1158. }
  1159. /* In binary, we write each of these four coordinates as a
  1160. 16-bit `index' i.e. integer, because we haven't yet changed
  1161. the VDC integer precision to our desired value (we can't do
  1162. that until the beginning of the picture). */
  1163. _cgm_emit_index (current_page_header, false, _plotter->cgm_encoding,
  1164. imin_true,
  1165. data_len, &data_byte_count, &byte_count);
  1166. _cgm_emit_index (current_page_header, false, _plotter->cgm_encoding,
  1167. jmin_true,
  1168. data_len, &data_byte_count, &byte_count);
  1169. _cgm_emit_index (current_page_header, false, _plotter->cgm_encoding,
  1170. imax_true,
  1171. data_len, &data_byte_count, &byte_count);
  1172. _cgm_emit_index (current_page_header, false, _plotter->cgm_encoding,
  1173. jmax_true,
  1174. data_len, &data_byte_count, &byte_count);
  1175. _cgm_emit_command_terminator (current_page_header,
  1176. _plotter->cgm_encoding,
  1177. &byte_count);
  1178. }
  1179. /* emit "SCALING MODE" command. Specify metric scaling (required
  1180. by WebCGM profile). The argument is the number of millimeters
  1181. per VDC unit; it must be a floating-point real. */
  1182. {
  1183. int irange, jrange;
  1184. double scaling_factor;
  1185. data_len = 6; /* 2 bytes per enum, 4 per floating-pt. real */
  1186. byte_count = data_byte_count = 0;
  1187. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1188. CGM_PICTURE_DESCRIPTOR_ELEMENT, 1,
  1189. data_len, &byte_count,
  1190. "SCALEMODE");
  1191. _cgm_emit_enum (current_page_header, false, _plotter->cgm_encoding,
  1192. 1,
  1193. data_len, &data_byte_count, &byte_count,
  1194. "metric");
  1195. /* Compute a metric scaling factor from the criterion that the
  1196. nominal physical width and height of VDC space be the
  1197. viewport xsize and ysize, as determined by the PAGESIZE
  1198. parameter.
  1199. We can get this scaling factor easily, by computing a
  1200. quotient, because our scheme for setting imin,imax,jmin,jmax
  1201. in the initialize() method preserves aspect ratio, and
  1202. signs, as well (see comment immediately above). But we must
  1203. be careful not to divide by zero, since zero-width and
  1204. zero-height viewports are allowed. */
  1205. irange = _plotter->data->imax - _plotter->data->imin;
  1206. jrange = _plotter->data->jmax - _plotter->data->jmin;
  1207. if (irange != 0)
  1208. scaling_factor =
  1209. (25.4 * _plotter->data->viewport_xsize) / irange;
  1210. else if (jrange != 0)
  1211. scaling_factor =
  1212. (25.4 * _plotter->data->viewport_ysize) / jrange;
  1213. else
  1214. /* degenerate case, viewport has zero size */
  1215. scaling_factor = 0.0;
  1216. /* yes, this needs to be a floating-point real, not fixed-point! */
  1217. _cgm_emit_real_floating_point (current_page_header, false, _plotter->cgm_encoding,
  1218. scaling_factor,
  1219. data_len, &data_byte_count, &byte_count);
  1220. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1221. &byte_count);
  1222. }
  1223. /* emit "LINE WIDTH SPECIFICATION MODE" command [specify
  1224. absolute coordinates] */
  1225. {
  1226. data_len = 2; /* 2 bytes per enum */
  1227. byte_count = data_byte_count = 0;
  1228. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1229. CGM_PICTURE_DESCRIPTOR_ELEMENT, 3,
  1230. data_len, &byte_count,
  1231. "LINEWIDTHMODE");
  1232. _cgm_emit_enum (current_page_header, false, _plotter->cgm_encoding,
  1233. 0,
  1234. data_len, &data_byte_count, &byte_count,
  1235. "abs");
  1236. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1237. &byte_count);
  1238. }
  1239. /* emit "EDGE WIDTH SPECIFICATION MODE" command [specify absolute
  1240. coordinates] */
  1241. {
  1242. data_len = 2; /* 2 bytes per enum */
  1243. byte_count = data_byte_count = 0;
  1244. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1245. CGM_PICTURE_DESCRIPTOR_ELEMENT, 5,
  1246. data_len, &byte_count,
  1247. "EDGEWIDTHMODE");
  1248. _cgm_emit_enum (current_page_header, false, _plotter->cgm_encoding,
  1249. 0,
  1250. data_len, &data_byte_count, &byte_count,
  1251. "abs");
  1252. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1253. &byte_count);
  1254. }
  1255. /* emit "MARKER SIZE SPECIFICATION MODE" command [specify
  1256. absolute coordinates] */
  1257. {
  1258. data_len = 2; /* 2 bytes per enum */
  1259. byte_count = data_byte_count = 0;
  1260. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1261. CGM_PICTURE_DESCRIPTOR_ELEMENT, 4,
  1262. data_len, &byte_count,
  1263. "MARKERSIZEMODE");
  1264. _cgm_emit_enum (current_page_header, false, _plotter->cgm_encoding,
  1265. 0,
  1266. data_len, &data_byte_count, &byte_count,
  1267. "abs");
  1268. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1269. &byte_count);
  1270. }
  1271. /* emit "COLOR SELECTION MODE" command [specify direct color,
  1272. not indexed color] */
  1273. {
  1274. data_len = 2; /* 2 bytes per enum */
  1275. byte_count = data_byte_count = 0;
  1276. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1277. CGM_PICTURE_DESCRIPTOR_ELEMENT, 2,
  1278. data_len, &byte_count,
  1279. "COLRMODE");
  1280. _cgm_emit_enum (current_page_header, false, _plotter->cgm_encoding,
  1281. 1,
  1282. data_len, &data_byte_count, &byte_count,
  1283. "direct");
  1284. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1285. &byte_count);
  1286. }
  1287. if (current_page->bg_color_suppressed == false)
  1288. /* user didn't specify "none" as background color, so emit
  1289. "BACKGROUND COLOR" command. (Note that in a CGM file,
  1290. background color is always a direct color specified by color
  1291. components, never an indexed color.) The background color
  1292. for any page is stored in the `bg_color' element of its
  1293. plOutbuf at the time the page is closed; see g_closepl.c. */
  1294. {
  1295. data_len = 3 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
  1296. byte_count = data_byte_count = 0;
  1297. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1298. CGM_PICTURE_DESCRIPTOR_ELEMENT, 7,
  1299. data_len, &byte_count,
  1300. "BACKCOLR");
  1301. _cgm_emit_color_component (current_page_header, false, _plotter->cgm_encoding,
  1302. (unsigned int)current_page->bg_color.red,
  1303. data_len, &data_byte_count, &byte_count);
  1304. _cgm_emit_color_component (current_page_header, false, _plotter->cgm_encoding,
  1305. (unsigned int)current_page->bg_color.green,
  1306. data_len, &data_byte_count, &byte_count);
  1307. _cgm_emit_color_component (current_page_header, false, _plotter->cgm_encoding,
  1308. (unsigned int)current_page->bg_color.blue,
  1309. data_len, &data_byte_count, &byte_count);
  1310. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1311. &byte_count);
  1312. }
  1313. /* if user defined any line types, emit a sequence of "LINE AND
  1314. EDGE TYPE DEFINITION" commands */
  1315. {
  1316. plCGMCustomLineType *linetype_ptr = (plCGMCustomLineType *)current_page->extra;
  1317. int linetype = 0;
  1318. while (linetype_ptr)
  1319. {
  1320. int k, cycle_length, dash_array_len, *dash_array;
  1321. linetype--; /* user-defined ones are -1,-2,-3,... */
  1322. dash_array_len = linetype_ptr->dash_array_len;
  1323. dash_array = linetype_ptr->dashes;
  1324. cycle_length = 0;
  1325. for (k = 0; k < dash_array_len; k++)
  1326. cycle_length += dash_array[k];
  1327. /* data: a 2-byte index, the cycle length, and the array of
  1328. dash lengths (all integers) */
  1329. data_len = 2 + (1 + dash_array_len) * CGM_BINARY_BYTES_PER_INTEGER;
  1330. byte_count = data_byte_count = 0;
  1331. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1332. CGM_PICTURE_DESCRIPTOR_ELEMENT, 17,
  1333. data_len, &byte_count,
  1334. "LINEEDGETYPEDEF");
  1335. _cgm_emit_index (current_page_header, false, _plotter->cgm_encoding,
  1336. linetype,
  1337. data_len, &data_byte_count, &byte_count);
  1338. _cgm_emit_integer (current_page_header, false, _plotter->cgm_encoding,
  1339. cycle_length,
  1340. data_len, &data_byte_count, &byte_count);
  1341. for (k = 0; k < dash_array_len; k++)
  1342. _cgm_emit_integer (current_page_header, false, _plotter->cgm_encoding,
  1343. dash_array[k],
  1344. data_len, &data_byte_count, &byte_count);
  1345. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1346. &byte_count);
  1347. /* on to next user-defined line type */
  1348. linetype_ptr = linetype_ptr->next;
  1349. }
  1350. }
  1351. /* emit "BEGIN PICTURE BODY" command */
  1352. {
  1353. data_len = 0;
  1354. byte_count = data_byte_count = 0;
  1355. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1356. CGM_DELIMITER_ELEMENT, 4,
  1357. data_len, &byte_count,
  1358. "BEGPICBODY");
  1359. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1360. &byte_count);
  1361. }
  1362. /* Emit "VDC INTEGER PRECISION" command. Very similar to the
  1363. "INTEGER PRECISION" command, except we emit this at the start
  1364. of each picture. */
  1365. {
  1366. int j, max_int;
  1367. data_len = CGM_BINARY_BYTES_PER_INTEGER;
  1368. byte_count = data_byte_count = 0;
  1369. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1370. CGM_CONTROL_ELEMENT, 1,
  1371. data_len, &byte_count,
  1372. "VDCINTEGERPREC");
  1373. switch (_plotter->cgm_encoding)
  1374. {
  1375. case CGM_ENCODING_BINARY:
  1376. default:
  1377. _cgm_emit_integer (current_page_header, false, _plotter->cgm_encoding,
  1378. 8 * CGM_BINARY_BYTES_PER_INTEGER,
  1379. data_len, &data_byte_count, &byte_count);
  1380. break;
  1381. case CGM_ENCODING_CHARACTER: /* not supported */
  1382. break;
  1383. case CGM_ENCODING_CLEAR_TEXT:
  1384. max_int = 0;
  1385. for (j = 0; j < (8 * CGM_BINARY_BYTES_PER_INTEGER - 1); j++)
  1386. max_int += (1 << j);
  1387. _cgm_emit_integer (current_page_header, false, _plotter->cgm_encoding,
  1388. -max_int,
  1389. data_len, &data_byte_count, &byte_count);
  1390. _cgm_emit_integer (current_page_header, false, _plotter->cgm_encoding,
  1391. max_int,
  1392. data_len, &data_byte_count, &byte_count);
  1393. break;
  1394. }
  1395. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1396. &byte_count);
  1397. }
  1398. if (doc_uses_fonts)
  1399. /* emit "TEXT PRECISION" command */
  1400. {
  1401. data_len = 2; /* 2 bytes per enum */
  1402. byte_count = data_byte_count = 0;
  1403. _cgm_emit_command_header (current_page_header, _plotter->cgm_encoding,
  1404. CGM_ATTRIBUTE_ELEMENT, 11,
  1405. data_len, &byte_count,
  1406. "TEXTPREC");
  1407. _cgm_emit_enum (current_page_header, false, _plotter->cgm_encoding,
  1408. 2,
  1409. data_len, &data_byte_count, &byte_count,
  1410. "stroke");
  1411. _cgm_emit_command_terminator (current_page_header, _plotter->cgm_encoding,
  1412. &byte_count);
  1413. }
  1414. /* write the page header */
  1415. _write_bytes (_plotter->data,
  1416. (int)(current_page_header->contents),
  1417. (unsigned char *)current_page_header->base);
  1418. _delete_outbuf (current_page_header);
  1419. /* WRITE THE PICTURE */
  1420. _write_bytes (_plotter->data,
  1421. (int)(current_page->contents),
  1422. (unsigned char *)current_page->base);
  1423. /* prepare a page trailer */
  1424. current_page_trailer = _new_outbuf ();
  1425. /* emit "END PICTURE" command (no parameters) */
  1426. {
  1427. data_len = 0;
  1428. byte_count = data_byte_count = 0;
  1429. _cgm_emit_command_header (current_page_trailer, _plotter->cgm_encoding,
  1430. CGM_DELIMITER_ELEMENT, 5,
  1431. data_len, &byte_count,
  1432. "ENDPIC");
  1433. _cgm_emit_command_terminator (current_page_trailer, _plotter->cgm_encoding,
  1434. &byte_count);
  1435. }
  1436. /* write page trailer */
  1437. _write_bytes (_plotter->data,
  1438. (int)(current_page_trailer->contents),
  1439. (unsigned char *)current_page_trailer->base);
  1440. _delete_outbuf (current_page_trailer);
  1441. /* on to next page (if any) */
  1442. next_page = current_page->next;
  1443. current_page = next_page;
  1444. i++;
  1445. }
  1446. /* prepare a document trailer */
  1447. doc_trailer = _new_outbuf ();
  1448. /* emit "END METAFILE" command (no parameters) */
  1449. {
  1450. data_len = 0;
  1451. byte_count = data_byte_count = 0;
  1452. _cgm_emit_command_header (doc_trailer, _plotter->cgm_encoding,
  1453. CGM_DELIMITER_ELEMENT, 2,
  1454. data_len, &byte_count,
  1455. "ENDMF");
  1456. _cgm_emit_command_terminator (doc_trailer, _plotter->cgm_encoding,
  1457. &byte_count);
  1458. }
  1459. /* WRITE DOCUMENT TRAILER */
  1460. _write_bytes (_plotter->data,
  1461. (int)(doc_trailer->contents),
  1462. (unsigned char *)doc_trailer->base);
  1463. _delete_outbuf (doc_trailer);
  1464. }
  1465. /* delete all plOutbufs in which document pages are stored */
  1466. current_page = _plotter->data->first_page;
  1467. while (current_page)
  1468. {
  1469. plOutbuf *next_page;
  1470. next_page = current_page->next;
  1471. /* deallocate page-specific table of user-specified line types,
  1472. if any */
  1473. if (current_page->extra)
  1474. {
  1475. plCGMCustomLineType *linetype_ptr = (plCGMCustomLineType *)current_page->extra;
  1476. plCGMCustomLineType *old_linetype_ptr;
  1477. while (linetype_ptr)
  1478. {
  1479. if (linetype_ptr->dash_array_len > 0 /* paranoia */
  1480. && linetype_ptr->dashes)
  1481. free (linetype_ptr->dashes);
  1482. old_linetype_ptr = linetype_ptr;
  1483. linetype_ptr = linetype_ptr->next;
  1484. free (old_linetype_ptr);
  1485. }
  1486. _plotter->data->page->extra = (void *)NULL;
  1487. }
  1488. _delete_outbuf (current_page);
  1489. current_page = next_page;
  1490. }
  1491. /* flush output stream if any */
  1492. if (_plotter->data->outfp)
  1493. {
  1494. if (fflush(_plotter->data->outfp) < 0
  1495. #ifdef MSDOS
  1496. /* data can be caught in DOS buffers, so do an fsync() too */
  1497. || fsync (_plotter->data->outfp) < 0
  1498. #endif
  1499. )
  1500. _plotter->error (R___(_plotter) "the output stream is jammed");
  1501. }
  1502. #ifdef LIBPLOTTER
  1503. else if (_plotter->data->outstream)
  1504. {
  1505. _plotter->data->outstream->flush ();
  1506. if (!(*(_plotter->data->outstream)))
  1507. _plotter->error (R___(_plotter) "the output stream is jammed");
  1508. }
  1509. #endif
  1510. #ifndef LIBPLOTTER
  1511. /* in libplot, manually invoke superclass termination method */
  1512. _pl_g_terminate (S___(_plotter));
  1513. #endif
  1514. }
  1515. static void
  1516. build_sdr_from_index (plOutbuf *sdr_buffer, int cgm_encoding, int x)
  1517. {
  1518. int dummy_data_len, dummy_data_byte_count, dummy_byte_count;
  1519. dummy_data_len = dummy_data_byte_count = dummy_byte_count = 0;
  1520. _cgm_emit_index (sdr_buffer, true, cgm_encoding,
  1521. CGM_SDR_DATATYPE_INDEX,
  1522. dummy_data_len, &dummy_data_byte_count,
  1523. &dummy_byte_count);
  1524. _cgm_emit_integer (sdr_buffer, true, cgm_encoding,
  1525. 1,
  1526. dummy_data_len, &dummy_data_byte_count,
  1527. &dummy_byte_count);
  1528. _cgm_emit_index (sdr_buffer, true, cgm_encoding,
  1529. x,
  1530. dummy_data_len, &dummy_data_byte_count,
  1531. &dummy_byte_count);
  1532. }
  1533. static void
  1534. build_sdr_from_string (plOutbuf *sdr_buffer, int cgm_encoding, const char *s, int string_length, bool use_double_quotes)
  1535. {
  1536. int dummy_data_len, dummy_data_byte_count, dummy_byte_count;
  1537. dummy_data_len = dummy_data_byte_count = dummy_byte_count = 0;
  1538. _cgm_emit_index (sdr_buffer, true, cgm_encoding,
  1539. CGM_SDR_DATATYPE_STRING_FIXED,
  1540. dummy_data_len, &dummy_data_byte_count,
  1541. &dummy_byte_count);
  1542. _cgm_emit_integer (sdr_buffer, true, cgm_encoding,
  1543. 1,
  1544. dummy_data_len, &dummy_data_byte_count,
  1545. &dummy_byte_count);
  1546. _cgm_emit_string (sdr_buffer, true, cgm_encoding,
  1547. s, string_length, use_double_quotes,
  1548. dummy_data_len, &dummy_data_byte_count,
  1549. &dummy_byte_count);
  1550. }
  1551. static void
  1552. build_sdr_from_ui8s (plOutbuf *sdr_buffer, int cgm_encoding, const int *x, int n)
  1553. {
  1554. int i, dummy_data_len, dummy_data_byte_count, dummy_byte_count;
  1555. dummy_data_len = dummy_data_byte_count = dummy_byte_count = 0;
  1556. _cgm_emit_index (sdr_buffer, true, cgm_encoding,
  1557. CGM_SDR_DATATYPE_UNSIGNED_INTEGER_8BIT,
  1558. dummy_data_len, &dummy_data_byte_count,
  1559. &dummy_byte_count);
  1560. _cgm_emit_integer (sdr_buffer, true, cgm_encoding,
  1561. n,
  1562. dummy_data_len, &dummy_data_byte_count,
  1563. &dummy_byte_count);
  1564. for (i = 0; i < n; i++)
  1565. _cgm_emit_unsigned_integer_8bit (sdr_buffer, true, cgm_encoding,
  1566. (unsigned int)(x[i]),
  1567. dummy_data_len, &dummy_data_byte_count,
  1568. &dummy_byte_count);
  1569. }
  1570. #ifdef LIBPLOTTER
  1571. CGMPlotter::CGMPlotter (FILE *infile, FILE *outfile, FILE *errfile)
  1572. :Plotter (infile, outfile, errfile)
  1573. {
  1574. _pl_c_initialize ();
  1575. }
  1576. CGMPlotter::CGMPlotter (FILE *outfile)
  1577. :Plotter (outfile)
  1578. {
  1579. _pl_c_initialize ();
  1580. }
  1581. CGMPlotter::CGMPlotter (istream& in, ostream& out, ostream& err)
  1582. : Plotter (in, out, err)
  1583. {
  1584. _pl_c_initialize ();
  1585. }
  1586. CGMPlotter::CGMPlotter (ostream& out)
  1587. : Plotter (out)
  1588. {
  1589. _pl_c_initialize ();
  1590. }
  1591. CGMPlotter::CGMPlotter ()
  1592. {
  1593. _pl_c_initialize ();
  1594. }
  1595. CGMPlotter::CGMPlotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &parameters)
  1596. :Plotter (infile, outfile, errfile, parameters)
  1597. {
  1598. _pl_c_initialize ();
  1599. }
  1600. CGMPlotter::CGMPlotter (FILE *outfile, PlotterParams &parameters)
  1601. :Plotter (outfile, parameters)
  1602. {
  1603. _pl_c_initialize ();
  1604. }
  1605. CGMPlotter::CGMPlotter (istream& in, ostream& out, ostream& err, PlotterParams &parameters)
  1606. : Plotter (in, out, err, parameters)
  1607. {
  1608. _pl_c_initialize ();
  1609. }
  1610. CGMPlotter::CGMPlotter (ostream& out, PlotterParams &parameters)
  1611. : Plotter (out, parameters)
  1612. {
  1613. _pl_c_initialize ();
  1614. }
  1615. CGMPlotter::CGMPlotter (PlotterParams &parameters)
  1616. : Plotter (parameters)
  1617. {
  1618. _pl_c_initialize ();
  1619. }
  1620. CGMPlotter::~CGMPlotter ()
  1621. {
  1622. /* if luser left the Plotter open, close it */
  1623. if (_plotter->data->open)
  1624. _API_closepl ();
  1625. _pl_c_terminate ();
  1626. }
  1627. #endif