File: g_outbuf.c

package info (click to toggle)
plotutils 2.4.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,676 kB
  • ctags: 6,967
  • sloc: ansic: 76,305; sh: 15,172; cpp: 12,403; yacc: 2,604; makefile: 888; lex: 144
file content (282 lines) | stat: -rw-r--r-- 8,074 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* This file contains routines for creating, manipulating, and deleting a
   special sort of output buffer: a plOutbuf.  They are invoked by drawing
   methods for Plotters that do not do real-time output.  Such Plotters
   store the device code for each page of graphics in a plOutbuf.  Along
   with a page of device code, a plOutbuf optionally stores bounding box
   information for the page.

   plOutbufs are resized when they are too full.  The strange resizing
   method (_UPDATE_BUFFER) is needed because on many systems, sprintf()
   does not return the number of characters it writes.  _UPDATE_BUFFER must
   be called after each call to sprintf(); it is not invoked automatically.
   
   Output buffers of this sort are a bit of a kludge.  They may eventually
   be replaced or supplemented by an in-core object hierarchy, which
   deletepl() will scan.

   When erase() is invoked on the Plotter, _RESET_OUTBUF is called, to
   remove all graphics from the plOutbuf.  There is provision for keeping a
   section of initialization code in the plOutbuf, untouched.  This is
   arranged by a previous call to _FREEZE_OUTBUF.  Anything in the plOutbuf
   at that time will be untouched by a later call to _RESET_OUTBUF. */

#include "sys-defines.h"
#include "extern.h"

/* Initial length for a plOutbuf (should be large enough to handle any
   single one of our sprintf's or strcpy's [including final NUL], or other
   write operations, without overflow).  Note: in p_defplot.c we write long
   blocks of Postscript initialization code (see p_header.h) into a
   plOutbuf, so this should be quite large.  We may also write large
   substrings into a plOutbuf in c_emit.c. */
#define INITIAL_OUTBUF_LEN 8192

/* New (larger) length of a plOutbuf, as function of the old; used when
   reallocating due to exhaustion of storage. */
#define NEW_OUTBUF_LEN(old_outbuf_len) ((old_outbuf_len) < 10000000 ? 2 * (old_outbuf_len) : (old_outbuf_len) + 10000000)

plOutbuf *
#ifdef _HAVE_PROTOS
_new_outbuf (void)
#else
_new_outbuf ()
#endif
{
  plOutbuf *bufp;

  bufp = (plOutbuf *)_plot_xmalloc(sizeof(plOutbuf));
  bufp->header = (plOutbuf *)NULL;
  bufp->trailer = (plOutbuf *)NULL;
  bufp->base = (char *)_plot_xmalloc(INITIAL_OUTBUF_LEN * sizeof(char));
  bufp->len = (unsigned long)INITIAL_OUTBUF_LEN;
  bufp->next = NULL;
  bufp->reset_point = bufp->base;
  bufp->reset_contents = (unsigned long)0L;
  _reset_outbuf (bufp);

  return bufp;
}

void
#ifdef _HAVE_PROTOS
_reset_outbuf (plOutbuf *bufp)
#else
_reset_outbuf (bufp)
     plOutbuf *bufp;
#endif
{
  int i;

  *(bufp->reset_point) = '\0';
  bufp->point = bufp->reset_point;
  bufp->contents = bufp->reset_contents;

  /* also initialize elements used by some drivers */

  /* initialize bounding box to an empty (self-contradictory) box */
  bufp->xrange_min = DBL_MAX;
  bufp->xrange_max = -(DBL_MAX);
  bufp->yrange_min = DBL_MAX;
  bufp->yrange_max = -(DBL_MAX);

  /* initialize `font used' arrays for the page */
  for (i = 0; i < NUM_PS_FONTS; i++)
    bufp->ps_font_used[i] = false;
  for (i = 0; i < NUM_PCL_FONTS; i++)
    bufp->pcl_font_used[i] = false;
}

void
#ifdef _HAVE_PROTOS
_freeze_outbuf (plOutbuf *bufp)
#else
_freeze_outbuf (bufp)
     plOutbuf *bufp;
#endif
{
  bufp->reset_point = bufp->point;
  bufp->reset_contents = bufp->contents;
}

void
#ifdef _HAVE_PROTOS
_delete_outbuf (plOutbuf *bufp)
#else
_delete_outbuf (bufp)
     plOutbuf *bufp;
#endif
{
  if (bufp)
    {
      free (bufp->base);
      free (bufp);
    }
}

/* UPDATE_BUFFER is called manually, after each sprintf() and other object
   write operation.  It assumes that the buffer is always a null-terminated
   string, so that strlen() can be used, to determine how many additional
   characters were added.  */

void
#ifdef _HAVE_PROTOS
_update_buffer (plOutbuf *bufp)
#else
_update_buffer (bufp)
     plOutbuf *bufp;
#endif
{
  int additional;

  /* determine how many add'l chars were added */
  additional = strlen (bufp->point);
  bufp->point += additional;
  bufp->contents += additional;
  
  if (bufp->contents + 1 > bufp->len) /* need room for NUL */
    /* shouldn't happen! */
    {
      fprintf (stderr, "libplot: output buffer overrun\n");
      exit (EXIT_FAILURE);
    }
  if (bufp->contents > (bufp->len >> 1))
    /* expand buffer */
    {
      unsigned long oldlen, newlen;

      oldlen = bufp->len;
      newlen = NEW_OUTBUF_LEN(oldlen);

      bufp->base = 
	(char *)_plot_xrealloc (bufp->base, newlen * sizeof(char));
      bufp->len = newlen;
      bufp->point = bufp->base + bufp->contents;
      bufp->reset_point = bufp->base + bufp->reset_contents;
    }      
}

/* A variant of _UPDATE_BUFFER in which the caller specifies how many bytes
   have been added.  Used in cases when the buffer contains something other
   than a null-terminated string (e.g., raw binary bytes). */

void
#ifdef _HAVE_PROTOS
_update_buffer_by_added_bytes (plOutbuf *bufp, int additional)
#else
_update_buffer_by_added_bytes (bufp, additional)
     plOutbuf *bufp;
     int additional;
#endif
{
  bufp->point += additional;
  bufp->contents += additional;
  
  if (bufp->contents + 1 > bufp->len) /* need room for NUL */
    /* shouldn't happen! */
    {
      fprintf (stderr, "libplot: output buffer overrun\n");
      exit (EXIT_FAILURE);
    }
  if (bufp->contents > (bufp->len >> 1))
    /* expand buffer */
    {
      unsigned long oldlen, newlen;

      oldlen = bufp->len;
      newlen = NEW_OUTBUF_LEN(oldlen);

      bufp->base = 
	(char *)_plot_xrealloc (bufp->base, newlen * sizeof(char));
      bufp->len = newlen;
      bufp->point = bufp->base + bufp->contents;
      bufp->reset_point = bufp->base + bufp->reset_contents;
    }      
}

/* update bounding box information for a plOutbuf, to take account of a
   point being plotted on the associated page */
void 
#ifdef _HAVE_PROTOS
_update_bbox (plOutbuf *bufp, double x, double y)
#else
_update_bbox (bufp, x, y)
     plOutbuf *bufp;
     double x, y;
#endif
{
  if (x > bufp->xrange_max) bufp->xrange_max = x;
  if (x < bufp->xrange_min) bufp->xrange_min = x;
  if (y > bufp->yrange_max) bufp->yrange_max = y;
  if (y < bufp->yrange_min) bufp->yrange_min = y;
}

/* return bounding box information for a plOutbuf */
void 
#ifdef _HAVE_PROTOS
_bbox_of_outbuf (plOutbuf *bufp, double *xmin, double *xmax, double *ymin, double *ymax)
#else
_bbox_of_outbuf (bufp, xmin, xmax, ymin, ymax)
     plOutbuf *bufp;
     double *xmin, *xmax, *ymin, *ymax;
#endif
{
  double page_x_min = DBL_MAX;
  double page_y_min = DBL_MAX;  
  double page_x_max = -(DBL_MAX);
  double page_y_max = -(DBL_MAX);  

  if (bufp)
    {
      page_x_max = bufp->xrange_max;
      page_x_min = bufp->xrange_min;
      page_y_max = bufp->yrange_max;
      page_y_min = bufp->yrange_min;
    }

  *xmin = page_x_min;
  *ymin = page_y_min;
  *xmax = page_x_max;
  *ymax = page_y_max;
}

/* compute bounding box information for a linked list of plOutbufs
   (i.e. pages), starting with a specified plOutbuf (i.e., page) */
void 
#ifdef _HAVE_PROTOS
_bbox_of_outbufs (plOutbuf *bufp, double *xmin, double *xmax, double *ymin, double *ymax)
#else
_bbox_of_outbufs (bufp, xmin, xmax, ymin, ymax)
     plOutbuf *bufp;
     double *xmin, *xmax, *ymin, *ymax;
#endif
{
  double doc_x_min = DBL_MAX;
  double doc_y_min = DBL_MAX;  
  double doc_x_max = -(DBL_MAX);
  double doc_y_max = -(DBL_MAX);  
  double page_x_min, page_x_max, page_y_min, page_y_max;
  plOutbuf *page = bufp;

  while (page)
    {
      page_x_max = page->xrange_max;
      page_x_min = page->xrange_min;
      page_y_max = page->yrange_max;
      page_y_min = page->yrange_min;

      if (!((page_x_max < page_x_min || page_y_max < page_y_min)))
	/* nonempty page */
	{
	  if (page_x_max > doc_x_max) doc_x_max = page_x_max;
	  if (page_y_max > doc_y_max) doc_y_max = page_y_max;
	  if (page_x_min < doc_x_min) doc_x_min = page_x_min;
	  if (page_y_min < doc_y_min) doc_y_min = page_y_min;
	}
      page = page->next;
    }

  *xmin = doc_x_min;
  *ymin = doc_y_min;
  *xmax = doc_x_max;
  *ymax = doc_y_max;
}