s_closepl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include "sys-defines.h"
  16. #include "extern.h"
  17. /* forward references */
  18. static void write_svg_transform (plOutbuf *outbuf, const double m[6]);
  19. bool
  20. _pl_s_end_page (S___(Plotter *_plotter))
  21. {
  22. plOutbuf *svg_header, *svg_trailer;
  23. /* SVG files contain only one page of graphics so this is a sanity check */
  24. if (_plotter->data->page_number != 1)
  25. return true;
  26. /* prepare SVG header (i.e. page header), write it to a plOutbuf */
  27. svg_header = _new_outbuf ();
  28. /* start with DTD */
  29. sprintf (svg_header->point, "\
  30. <?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>\n\
  31. <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
  32. _update_buffer (svg_header);
  33. /* Emit nominal physical size of the device-frame viewport (and specify
  34. that in the device-frame coordinates we use, it's a unit square).
  35. viewport_{x,y}size are set from the PAGESIZE Plotter parameter, via
  36. the xsize and ysize options, and either or both may be negative. If
  37. they are, we flipped the NDC_frame->device_frame map to compensate
  38. (see s_defplot.c). Which is why we can take absolute values here. */
  39. if (_plotter->data->page_data->metric)
  40. sprintf (svg_header->point,
  41. "<svg version=\"1.1\" baseProfile=\"full\" id=\"body\" width=\"%.5gcm\" height=\"%.5gcm\" ",
  42. 2.54 * FABS(_plotter->data->viewport_xsize),
  43. 2.54 * FABS(_plotter->data->viewport_ysize));
  44. else
  45. sprintf (svg_header->point,
  46. "<svg version=\"1.1\" baseProfile=\"full\" id=\"body\" width=\"%.5gin\" height=\"%.5gin\" ",
  47. FABS(_plotter->data->viewport_xsize),
  48. FABS(_plotter->data->viewport_ysize));
  49. _update_buffer (svg_header);
  50. sprintf (svg_header->point,
  51. "%s %s %s %s %s>\n",
  52. "viewBox=\"0 0 1 1\"",
  53. "preserveAspectRatio=\"none\"",
  54. /* bind SVG namespace */
  55. "xmlns=\"http://www.w3.org/2000/svg\"",
  56. /* bind XLink and XML Events namespaces for good measure */
  57. "xmlns:xlink=\"http://www.w3.org/1999/xlink\"",
  58. "xmlns:ev=\"http://www.w3.org/2001/xml-events\"");
  59. _update_buffer (svg_header);
  60. sprintf (svg_header->point, "<title>SVG drawing</title>\n");
  61. _update_buffer (svg_header);
  62. sprintf (svg_header->point, "<desc>This was produced by version %s of GNU libplot, a free library for exporting 2-D vector graphics.</desc>\n",
  63. PL_LIBPLOT_VER_STRING);
  64. _update_buffer (svg_header);
  65. if (_plotter->s_bgcolor_suppressed == false)
  66. /* place a background rectangle behind, covering entire viewport */
  67. {
  68. char color_buf[8]; /* enough room for "#ffffff", incl. NUL */
  69. sprintf (svg_header->point,
  70. "<rect id=\"background\" x=\"0\" y=\"0\" width=\"1\" height=\"1\" stroke=\"none\" fill=\"%s\"/>\n",
  71. _libplot_color_to_svg_color (_plotter->s_bgcolor, color_buf));
  72. _update_buffer (svg_header);
  73. }
  74. /* enclose everything else in a container */
  75. sprintf (svg_header->point, "<g id=\"content\" ");
  76. _update_buffer (svg_header);
  77. if (_plotter->s_matrix_is_unknown == false
  78. && _plotter->s_matrix_is_bogus == false)
  79. /* Place a transform in the container: this page's default
  80. transformation matrix, which is simply the transformation matrix
  81. attribute of the very first graphical object plotted on the page.
  82. In libplot, `transformation matrix attribute' refers to the affine
  83. map from user space to NDC space. So we're careful to multiply by
  84. `m_ndc_to_device', which transforms NDC space to device space.
  85. Because SVG uses a flipped-y convention, `m_ndc_to_device' flips the
  86. y coordinate. (There will be additional flipping if the
  87. user-specified xsize, ysize are negative; see s_defplot.c. Also, if
  88. the ROTATION Plotter parameter is specified by the user, it may
  89. rotate.) */
  90. {
  91. double product[6];
  92. _matrix_product (_plotter->s_matrix, _plotter->data->m_ndc_to_device,
  93. product);
  94. write_svg_transform (svg_header, product);
  95. }
  96. /* turn off SVG's default [unfortunate] XML-inherited treatment of spaces */
  97. sprintf (svg_header->point, "xml:space=\"preserve\" ");
  98. _update_buffer (svg_header);
  99. /* specify style properties (all libplot defaults) */
  100. sprintf (svg_header->point, "stroke=\"%s\" ",
  101. "black");
  102. _update_buffer (svg_header);
  103. sprintf (svg_header->point, "stroke-linecap=\"%s\" ",
  104. "butt");
  105. _update_buffer (svg_header);
  106. sprintf (svg_header->point, "stroke-linejoin=\"%s\" ",
  107. "miter");
  108. _update_buffer (svg_header);
  109. sprintf (svg_header->point, "stroke-miterlimit=\"%.5g\" ",
  110. PL_DEFAULT_MITER_LIMIT);
  111. _update_buffer (svg_header);
  112. sprintf (svg_header->point, "stroke-dasharray=\"%s\" ",
  113. "none");
  114. _update_buffer (svg_header);
  115. /* should use `px' here to specify user units, per the SVG Authoring
  116. Guide, but ImageMagick objects to that */
  117. sprintf (svg_header->point, "stroke-dashoffset=\"%.5g\" ",
  118. 0.0);
  119. _update_buffer (svg_header);
  120. sprintf (svg_header->point, "stroke-opacity=\"%.5g\" ",
  121. 1.0);
  122. _update_buffer (svg_header);
  123. sprintf (svg_header->point, "fill=\"%s\" ",
  124. "none");
  125. _update_buffer (svg_header);
  126. sprintf (svg_header->point, "fill-rule=\"%s\" ",
  127. "evenodd");
  128. _update_buffer (svg_header);
  129. sprintf (svg_header->point, "fill-opacity=\"%.5g\" ",
  130. 1.0);
  131. _update_buffer (svg_header);
  132. sprintf (svg_header->point, "font-style=\"%s\" ",
  133. "normal");
  134. _update_buffer (svg_header);
  135. sprintf (svg_header->point, "font-variant=\"%s\" ",
  136. "normal");
  137. _update_buffer (svg_header);
  138. sprintf (svg_header->point, "font-weight=\"%s\" ",
  139. "normal");
  140. _update_buffer (svg_header);
  141. sprintf (svg_header->point, "font-stretch=\"%s\" ",
  142. "normal");
  143. _update_buffer (svg_header);
  144. sprintf (svg_header->point, "font-size-adjust=\"%s\" ",
  145. "none");
  146. _update_buffer (svg_header);
  147. sprintf (svg_header->point, "letter-spacing=\"%s\" ",
  148. "normal");
  149. _update_buffer (svg_header);
  150. sprintf (svg_header->point, "word-spacing=\"%s\" ",
  151. "normal");
  152. _update_buffer (svg_header);
  153. sprintf (svg_header->point, "text-anchor=\"%s\"",
  154. "start");
  155. _update_buffer (svg_header);
  156. sprintf (svg_header->point, ">\n");
  157. _update_buffer (svg_header);
  158. /* place SVG header in this page's plOutbuf */
  159. _plotter->data->page->header = svg_header;
  160. /* prepare SVG trailer too, write it to a plOutbuf */
  161. svg_trailer = _new_outbuf ();
  162. sprintf (svg_trailer->point, "</g>\n");
  163. _update_buffer (svg_trailer);
  164. sprintf (svg_trailer->point, "</svg>\n");
  165. _update_buffer (svg_trailer);
  166. /* place SVG trailer in this page's plOutbuf */
  167. _plotter->data->page->trailer = svg_trailer;
  168. return true;
  169. }
  170. /* This function is invoked while writing any graphical object on a page to
  171. the page's output buffer. It emits the string "transform=\"...\" ",
  172. where the "\"...\"" is computed from a transformation matrix attribute
  173. of the object, which is passed. I.e., it transforms a per-object
  174. transformation matrix to an SVG-style transformation matrix, and emits
  175. the latter as an SVG element attribute. The per-object transformation
  176. matrix is always the identity, except for rotated text strings and
  177. ellipses.
  178. This code evaluates the SVG transformation matrix as the composition of
  179. two transformations: the local transformation, which acts first (in user
  180. space), which is passed as an argument; and a 2nd transformation, which
  181. is the current transformation from user to NDC coordinates. Typically,
  182. it's the 1st which this code emits as the value of the `transform'
  183. attribute. That's because when this is called for the first time on a
  184. page (or newly erased page), the 2nd is stored in `s_matrix', the global
  185. transformation matrix for the page, which will later be written at the
  186. head of the SVG code for the page when closepl() is invoked (see above).
  187. This separation of the two distinct transformations will of course work
  188. only if the 2nd doesn't change from object to object on the page. For
  189. this reason, what's actually emitted as the value of the SVG transform
  190. attribute is a composite transformation, made up in succession of
  191. (1) the passed per-object transformation
  192. (2) the current value of the transformation from user to NDC coordinates
  193. (3) the inverse of s_matrix.
  194. If the user space -> NDC space map is the same for all objects on the
  195. page, then (2) and (3) will cancel each other out for all objects on
  196. the page.
  197. Note that in this code we flag `s_matrix' as bogus if it's singular. If
  198. it's bogus, it won't be written out when closepl() is invoked, and the
  199. global transformation matrix of the page will effectively be the
  200. identity (i.e., we'll punt). */
  201. void
  202. _pl_s_set_matrix (R___(Plotter *_plotter) const double m_local[6])
  203. {
  204. double m_base[6], m[6];
  205. const double *m_emitted = (const double *)NULL; /* keep compiler happy */
  206. bool need_transform_attribute = false;
  207. int i;
  208. for (i = 0; i < 6; i++)
  209. m_base[i] = _plotter->drawstate->transform.m_user_to_ndc[i];
  210. /* if this is the first time this function is invoked on a page (or newly
  211. erased page), store the current user-to-NDC matrix for later use as
  212. the global transformation matrix for the page */
  213. if (_plotter->s_matrix_is_unknown)
  214. {
  215. for (i = 0; i < 6; i++)
  216. _plotter->s_matrix[i] = m_base[i];
  217. _plotter->s_matrix_is_unknown = false;
  218. if (m_base[0] * m_base[3] - m_base[1] * m_base[2] == 0.0)
  219. /* singular, won't be used even though stored */
  220. _plotter->s_matrix_is_bogus = true;
  221. }
  222. /* compute product: current transformation matrix (in the transformation
  223. from user to NDC coors, local acts first, then base) */
  224. _matrix_product (m_local, m_base, m);
  225. /* determine whether current matrix is different from the global one that
  226. will be wrapped around the entire page (if there is one) */
  227. if (_plotter->s_matrix_is_bogus == false)
  228. /* have a global page-specific transformation matrix that will be
  229. applied, so object's transform attribute may need to compensate */
  230. {
  231. for (i = 0; i < 6; i++)
  232. {
  233. if (m[i] != _plotter->s_matrix[i])
  234. /* different, so need to compensate */
  235. {
  236. need_transform_attribute = true;
  237. break;
  238. }
  239. }
  240. if (need_transform_attribute)
  241. {
  242. double inverse_of_global[6], product[6];
  243. _matrix_inverse (_plotter->s_matrix, inverse_of_global);
  244. /* emitted transform attribute of object will be a product of
  245. three matrices: (1) the passed matrix, (2) the current
  246. user-to-NDC transformation matrix, and (3) the inverse of the
  247. global transformation matrix */
  248. _matrix_product (m, inverse_of_global, product);
  249. m_emitted = product;
  250. }
  251. }
  252. else
  253. /* no global transformation matrix for this page (no doubt because of
  254. the abovementioned non-invertibility problem), so object's transform
  255. attribute will simply be the current matrix */
  256. {
  257. need_transform_attribute = true;
  258. m_emitted = m;
  259. }
  260. /* emit object's transform attribute if it's not the identity */
  261. if (need_transform_attribute)
  262. write_svg_transform (_plotter->data->page, m_emitted);
  263. }
  264. /* Internal function for writing out a PS-style affine transformation as a
  265. SVG-style affine transformation. If matrix is the identity, nothing is
  266. written.
  267. In SVG format, the value of the `transform' attribute is a sequence of
  268. transformations such as `rotate', `scale', and `translate', where the
  269. sequence (as a composite transformation from user space to device [NDC]
  270. space) is read from right to left. This is the opposite of the PS
  271. convention. SVG documentation uses column vectors, while PS
  272. documentation uses row vectors.
  273. Presumably the SVG convention arose from a desire to make the
  274. `nestedness' of the transform attribute, implemented as the computation
  275. of a composite transformation, more intuitive. */
  276. static void
  277. write_svg_transform (plOutbuf *outbuf, const double m[6])
  278. {
  279. double mm[6];
  280. double max_value = 0.0;
  281. int i;
  282. int type = 0; /* default */
  283. /* compensate for possible roundoff error: treat very small elements of
  284. linear transformation (if any) as zero */
  285. #define VERY_SMALL_FACTOR 1e-10
  286. for (i = 0; i < 4; i++)
  287. max_value = DMAX(max_value, FABS(m[i]));
  288. for (i = 0; i < 6; i++)
  289. if (i < 4 && FABS(m[i]) < VERY_SMALL_FACTOR * max_value)
  290. mm[i] = 0;
  291. else
  292. mm[i] = m[i];
  293. if (mm[0] == 1.0 && mm[1] == 0.0 && mm[2] == 0.0 && mm[3] == 1.0
  294. && mm[4] == 0.0 && mm[5] == 0.0)
  295. /* identity matrix, unnecessary to write it */
  296. return;
  297. /* treat several types of affine transformation specially */
  298. if (mm[1] == 0.0 && mm[2] == 0.0)
  299. type = 1; /* scale + translation */
  300. else if (mm[0] == 0.0 && mm[1] == 1.0 && mm[2] == -1.0 && mm[3] == 0.0)
  301. type = 2; /* rotation by 90 + translation */
  302. else if (mm[0] == 0.0 && mm[1] == -1.0 && mm[2] == 1.0 && mm[3] == 0.0)
  303. type = 3; /* rotation by 270 + translation */
  304. else if (mm[0] == 0.0 && mm[1] == 1.0 && mm[2] == 1.0 && mm[3] == 0.0)
  305. type = 4; /* y-flip + rotation by 90 + translation */
  306. else if (mm[0] == 0.0 && mm[1] == -1.0 && mm[2] == -1.0 && mm[3] == 0.0)
  307. type = 5; /* y-flip + rotation by 270 + translation */
  308. sprintf (outbuf->point, "transform=\"");
  309. _update_buffer (outbuf);
  310. if (type != 0)
  311. {
  312. /* emit translation if any (SVG will perform it last, since SVG uses
  313. opposite order from PS for multiplying matrices) */
  314. if (mm[4] != 0.0 || mm[5] != 0.0)
  315. {
  316. if (mm[5] == 0.0)
  317. sprintf (outbuf->point, "translate(%.5g) ",
  318. mm[4]);
  319. else
  320. sprintf (outbuf->point, "translate(%.5g,%.5g) ",
  321. mm[4], mm[5]);
  322. _update_buffer (outbuf);
  323. }
  324. switch (type)
  325. {
  326. case 1:
  327. if (mm[0] != 1.0 || mm[3] != 1.0)
  328. {
  329. if (mm[3] == mm[0])
  330. sprintf (outbuf->point, "scale(%.5g) ",
  331. mm[0]);
  332. else if (mm[3] == -mm[0])
  333. {
  334. if (mm[0] != 1.0)
  335. sprintf (outbuf->point, "scale(1,-1) scale(%.5g) ",
  336. mm[0]);
  337. else
  338. sprintf (outbuf->point, "scale(1,-1) ");
  339. }
  340. else
  341. sprintf (outbuf->point, "scale(%.5g,%.5g) ",
  342. mm[0], mm[3]);
  343. _update_buffer (outbuf);
  344. }
  345. break;
  346. case 2:
  347. sprintf (outbuf->point, "rotate(90) ");
  348. _update_buffer (outbuf);
  349. break;
  350. case 3:
  351. sprintf (outbuf->point, "rotate(270) ");
  352. _update_buffer (outbuf);
  353. break;
  354. case 4:
  355. sprintf (outbuf->point, "rotate(90) scale(1,-1) ");
  356. _update_buffer (outbuf);
  357. break;
  358. case 5:
  359. sprintf (outbuf->point, "rotate(270) scale(1,-1) ");
  360. _update_buffer (outbuf);
  361. break;
  362. default: /* shouldn't happen */
  363. break;
  364. }
  365. }
  366. else
  367. /* general affine transformation */
  368. {
  369. sprintf (outbuf->point, "matrix(%.5g %.5g %.5g %.5g %.5g %.5g) ",
  370. mm[0], mm[1], mm[2], mm[3], mm[4], mm[5]);
  371. _update_buffer (outbuf);
  372. }
  373. sprintf (outbuf->point, "\" ");
  374. _update_buffer (outbuf);
  375. }