g_outbuf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 contains routines for creating, manipulating, and deleting a
  16. special sort of output buffer: a plOutbuf. They are invoked by drawing
  17. methods for Plotters that do not do real-time output. Such Plotters
  18. store the device code for each page of graphics in a plOutbuf. Along
  19. with a page of device code, a plOutbuf optionally stores bounding box
  20. information for the page.
  21. plOutbufs are resized when they are too full. The strange resizing
  22. method (_UPDATE_BUFFER) is needed because on many systems, sprintf()
  23. does not return the number of characters it writes. _UPDATE_BUFFER must
  24. be called after each call to sprintf(); it is not invoked automatically.
  25. Output buffers of this sort are a bit of a kludge. They may eventually
  26. be replaced or supplemented by an in-core object hierarchy, which
  27. deletepl() will scan.
  28. When erase() is invoked on the Plotter, _RESET_OUTBUF is called, to
  29. remove all graphics from the plOutbuf. There is provision for keeping a
  30. section of initialization code in the plOutbuf, untouched. This is
  31. arranged by a previous call to _FREEZE_OUTBUF. Anything in the plOutbuf
  32. at that time will be untouched by a later call to _RESET_OUTBUF. */
  33. #include "sys-defines.h"
  34. #include "extern.h"
  35. /* Initial length for a plOutbuf (should be large enough to handle any
  36. single one of our sprintf's or strcpy's [including final NUL], or other
  37. write operations, without overflow). Note: in p_defplot.c we write long
  38. blocks of Postscript initialization code (see p_header.h) into a
  39. plOutbuf, so this should be quite large. We may also write large
  40. substrings into a plOutbuf in c_emit.c. */
  41. #define INITIAL_OUTBUF_LEN 8192
  42. /* New (larger) length of a plOutbuf, as function of the old; used when
  43. reallocating due to exhaustion of storage. */
  44. #define NEW_OUTBUF_LEN(old_outbuf_len) ((old_outbuf_len) < 10000000 ? 2 * (old_outbuf_len) : (old_outbuf_len) + 10000000)
  45. plOutbuf *
  46. _new_outbuf (void)
  47. {
  48. plOutbuf *bufp;
  49. bufp = (plOutbuf *)_pl_xmalloc(sizeof(plOutbuf));
  50. bufp->header = (plOutbuf *)NULL;
  51. bufp->trailer = (plOutbuf *)NULL;
  52. bufp->base = (char *)_pl_xmalloc(INITIAL_OUTBUF_LEN * sizeof(char));
  53. bufp->len = (unsigned long)INITIAL_OUTBUF_LEN;
  54. bufp->next = NULL;
  55. bufp->reset_point = bufp->base;
  56. bufp->reset_contents = (unsigned long)0L;
  57. _reset_outbuf (bufp);
  58. return bufp;
  59. }
  60. void
  61. _reset_outbuf (plOutbuf *bufp)
  62. {
  63. int i;
  64. *(bufp->reset_point) = '\0';
  65. bufp->point = bufp->reset_point;
  66. bufp->contents = bufp->reset_contents;
  67. /* also initialize elements used by some drivers */
  68. /* initialize bounding box to an empty (self-contradictory) box */
  69. bufp->xrange_min = DBL_MAX;
  70. bufp->xrange_max = -(DBL_MAX);
  71. bufp->yrange_min = DBL_MAX;
  72. bufp->yrange_max = -(DBL_MAX);
  73. /* initialize `font used' arrays for the page */
  74. for (i = 0; i < PL_NUM_PS_FONTS; i++)
  75. bufp->ps_font_used[i] = false;
  76. for (i = 0; i < PL_NUM_PCL_FONTS; i++)
  77. bufp->pcl_font_used[i] = false;
  78. }
  79. void
  80. _freeze_outbuf (plOutbuf *bufp)
  81. {
  82. bufp->reset_point = bufp->point;
  83. bufp->reset_contents = bufp->contents;
  84. }
  85. void
  86. _delete_outbuf (plOutbuf *bufp)
  87. {
  88. if (bufp)
  89. {
  90. free (bufp->base);
  91. free (bufp);
  92. }
  93. }
  94. /* UPDATE_BUFFER is called manually, after each sprintf() and other object
  95. write operation. It assumes that the buffer is always a null-terminated
  96. string, so that strlen() can be used, to determine how many additional
  97. characters were added. */
  98. void
  99. _update_buffer (plOutbuf *bufp)
  100. {
  101. int additional;
  102. /* determine how many add'l chars were added */
  103. additional = strlen (bufp->point);
  104. bufp->point += additional;
  105. bufp->contents += additional;
  106. if (bufp->contents + 1 > bufp->len) /* need room for NUL */
  107. /* shouldn't happen! */
  108. {
  109. fprintf (stderr, "libplot: output buffer overrun\n");
  110. exit (EXIT_FAILURE);
  111. }
  112. if (bufp->contents > (bufp->len >> 1))
  113. /* expand buffer */
  114. {
  115. unsigned long oldlen, newlen;
  116. oldlen = bufp->len;
  117. newlen = NEW_OUTBUF_LEN(oldlen);
  118. bufp->base =
  119. (char *)_pl_xrealloc (bufp->base, newlen * sizeof(char));
  120. bufp->len = newlen;
  121. bufp->point = bufp->base + bufp->contents;
  122. bufp->reset_point = bufp->base + bufp->reset_contents;
  123. }
  124. }
  125. /* A variant of _UPDATE_BUFFER in which the caller specifies how many bytes
  126. have been added. Used in cases when the buffer contains something other
  127. than a null-terminated string (e.g., raw binary bytes). */
  128. void
  129. _update_buffer_by_added_bytes (plOutbuf *bufp, int additional)
  130. {
  131. bufp->point += additional;
  132. bufp->contents += additional;
  133. if (bufp->contents + 1 > bufp->len) /* need room for NUL */
  134. /* shouldn't happen! */
  135. {
  136. fprintf (stderr, "libplot: output buffer overrun\n");
  137. exit (EXIT_FAILURE);
  138. }
  139. if (bufp->contents > (bufp->len >> 1))
  140. /* expand buffer */
  141. {
  142. unsigned long oldlen, newlen;
  143. oldlen = bufp->len;
  144. newlen = NEW_OUTBUF_LEN(oldlen);
  145. bufp->base =
  146. (char *)_pl_xrealloc (bufp->base, newlen * sizeof(char));
  147. bufp->len = newlen;
  148. bufp->point = bufp->base + bufp->contents;
  149. bufp->reset_point = bufp->base + bufp->reset_contents;
  150. }
  151. }
  152. /* update bounding box information for a plOutbuf, to take account of a
  153. point being plotted on the associated page */
  154. void
  155. _update_bbox (plOutbuf *bufp, double x, double y)
  156. {
  157. if (x > bufp->xrange_max) bufp->xrange_max = x;
  158. if (x < bufp->xrange_min) bufp->xrange_min = x;
  159. if (y > bufp->yrange_max) bufp->yrange_max = y;
  160. if (y < bufp->yrange_min) bufp->yrange_min = y;
  161. }
  162. /* return bounding box information for a plOutbuf */
  163. void
  164. _bbox_of_outbuf (plOutbuf *bufp, double *xmin, double *xmax, double *ymin, double *ymax)
  165. {
  166. double page_x_min = DBL_MAX;
  167. double page_y_min = DBL_MAX;
  168. double page_x_max = -(DBL_MAX);
  169. double page_y_max = -(DBL_MAX);
  170. if (bufp)
  171. {
  172. page_x_max = bufp->xrange_max;
  173. page_x_min = bufp->xrange_min;
  174. page_y_max = bufp->yrange_max;
  175. page_y_min = bufp->yrange_min;
  176. }
  177. *xmin = page_x_min;
  178. *ymin = page_y_min;
  179. *xmax = page_x_max;
  180. *ymax = page_y_max;
  181. }
  182. /* compute bounding box information for a linked list of plOutbufs
  183. (i.e. pages), starting with a specified plOutbuf (i.e., page) */
  184. void
  185. _bbox_of_outbufs (plOutbuf *bufp, double *xmin, double *xmax, double *ymin, double *ymax)
  186. {
  187. double doc_x_min = DBL_MAX;
  188. double doc_y_min = DBL_MAX;
  189. double doc_x_max = -(DBL_MAX);
  190. double doc_y_max = -(DBL_MAX);
  191. double page_x_min, page_x_max, page_y_min, page_y_max;
  192. plOutbuf *page = bufp;
  193. while (page)
  194. {
  195. page_x_max = page->xrange_max;
  196. page_x_min = page->xrange_min;
  197. page_y_max = page->yrange_max;
  198. page_y_min = page->yrange_min;
  199. if (!((page_x_max < page_x_min || page_y_max < page_y_min)))
  200. /* nonempty page */
  201. {
  202. if (page_x_max > doc_x_max) doc_x_max = page_x_max;
  203. if (page_y_max > doc_y_max) doc_y_max = page_y_max;
  204. if (page_x_min < doc_x_min) doc_x_min = page_x_min;
  205. if (page_y_min < doc_y_min) doc_y_min = page_y_min;
  206. }
  207. page = page->next;
  208. }
  209. *xmin = doc_x_min;
  210. *ymin = doc_y_min;
  211. *xmax = doc_x_max;
  212. *ymax = doc_y_max;
  213. }