Graphviz  2.41.20171026.1811
gvrender_core_hpgl.c
Go to the documentation of this file.
1 /* $Id$ $Revision$ */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 /* FIXME - incomplete replacement for codegen */
15 
16 #include "config.h"
17 
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 
23 #ifdef _WIN32
24 #include <io.h>
25 #include "compat.h"
26 #endif
27 
28 #include "macros.h"
29 #include "const.h"
30 
31 #include "gvdevice.h"
32 #include "gvplugin_render.h"
33 #include "gvplugin_device.h"
34 #include "agxbuf.h"
35 #include "utils.h"
36 #include "color.h"
37 
38 /* Number of points to split splines into */
39 #define BEZIERSUBDIVISION 6
40 
41 typedef enum { FORMAT_HPGL, } format_type;
42 
43 static int Depth;
44 
45 static void hpglptarray(GVJ_t *job, pointf * A, int n, int close)
46 {
47  int i;
48  point p;
49 
50  for (i = 0; i < n; i++) {
51  PF2P(A[i],p);
52  gvprintf(job, " %d %d", p.x, p.y);
53  }
54  if (close) {
55  PF2P(A[0],p);
56  gvprintf(job, " %d %d", p.x, p.y);
57  }
58  gvputs(job, "\n");
59 }
60 
61 static char *hpgl_string(char *s)
62 {
63  static char *buf = NULL;
64  static int bufsize = 0;
65  int pos = 0;
66  char *p;
67  unsigned char c;
68 
69  if (!buf) {
70  bufsize = 64;
71  buf = malloc(bufsize * sizeof(char));
72  }
73 
74  p = buf;
75  while ((c = *s++)) {
76  if (pos > (bufsize - 8)) {
77  bufsize *= 2;
78  buf = realloc(buf, bufsize * sizeof(char));
79  p = buf + pos;
80  }
81  if (isascii(c)) {
82  if (c == '\\') {
83  *p++ = '\\';
84  pos++;
85  }
86  *p++ = c;
87  pos++;
88  } else {
89  *p++ = '\\';
90  sprintf(p, "%03o", c);
91  p += 3;
92  pos += 4;
93  }
94  }
95  *p = '\0';
96  return buf;
97 }
98 
99 static int hpglColorResolve(int *new, int r, int g, int b)
100 {
101 #define maxColors 256
102  static int top = 0;
103  static short red[maxColors], green[maxColors], blue[maxColors];
104  int c;
105  int ct = -1;
106  long rd, gd, bd, dist;
107  long mindist = 3 * 255 * 255; /* init to max poss dist */
108 
109  *new = 0; /* in case it is not a new color */
110  for (c = 0; c < top; c++) {
111  rd = (long) (red[c] - r);
112  gd = (long) (green[c] - g);
113  bd = (long) (blue[c] - b);
114  dist = rd * rd + gd * gd + bd * bd;
115  if (dist < mindist) {
116  if (dist == 0)
117  return c; /* Return exact match color */
118  mindist = dist;
119  ct = c;
120  }
121  }
122  /* no exact match. We now know closest, but first try to allocate exact */
123  if (top++ == maxColors)
124  return ct; /* Return closest available color */
125  red[c] = r;
126  green[c] = g;
127  blue[c] = b;
128  *new = 1; /* flag new color */
129  return c; /* Return newly allocated color */
130 }
131 
132 /* this table is in xfig color index order */
133 static char *hpglcolor[] = {
134  "black", "blue", "green", "cyan", "red", "magenta", "yellow", "white", (char *) NULL
135 };
136 
137 static void hpgl_resolve_color(GVJ_t *job, gvcolor_t * color)
138 {
139  int object_code = 0; /* always 0 for color */
140  int i, new;
141 
142  switch (color->type) {
143  case COLOR_STRING:
144  for (i = 0; hpglcolor[i]; i++) {
145  if (streq(hpglcolor[i], color->u.string)) {
146  color->u.index = i;
147  break;
148  }
149  }
150  break;
151  case RGBA_BYTE:
152  i = 32 + hpglColorResolve(&new,
153  color->u.rgba[0],
154  color->u.rgba[1],
155  color->u.rgba[2]);
156  if (new)
157  gvprintf(job, "%d %d #%02x%02x%02x\n",
158  object_code, i,
159  color->u.rgba[0],
160  color->u.rgba[1],
161  color->u.rgba[2]);
162  color->u.index = i;
163  break;
164  default:
165  assert(0); /* internal error */
166  }
167 
168  color->type = COLOR_INDEX;
169 }
170 
171 static void hpgl_line_style(obj_state_t *obj, int *line_style, double *style_val)
172 {
173  switch (obj->pen) {
174  case PEN_DASHED:
175  *line_style = 1;
176  *style_val = 10.;
177  break;
178  case PEN_DOTTED:
179  *line_style = 2;
180  *style_val = 10.;
181  break;
182  case PEN_SOLID:
183  default:
184  *line_style = 0;
185  *style_val = 0.;
186  break;
187  }
188 }
189 
190 static void hpgl_comment(GVJ_t *job, char *str)
191 {
192  gvprintf(job, "# %s\n", str);
193 }
194 
195 static void hpgl_begin_graph(GVJ_t * job)
196 {
197  obj_state_t *obj = job->obj;
198 
199  gvputs(job, "#FIG 3.2\n");
200  gvprintf(job, "# Generated by %s version %s (%s)\n",
201  job->common->info[0], job->common->info[1], job->common->info[2]);
202  gvprintf(job, "# Title: %s\n", obj->u.g->name);
203  gvprintf(job, "# Pages: %d\n", job->pagesArraySize.x * job->pagesArraySize.y);
204  gvputs(job, "Portrait\n"); /* orientation */
205  gvputs(job, "Center\n"); /* justification */
206  gvputs(job, "Inches\n"); /* units */
207  gvputs(job, "Letter\n"); /* papersize */
208  gvputs(job, "100.00\n"); /* magnification % */
209  gvputs(job, "Single\n"); /* multiple-page */
210  gvputs(job, "-2\n"); /* transparent color (none) */
211  gvputs(job, "1200"); /* resolution */
212  gvputs(job, " 2\n"); /* coordinate system (upper left) */
213 }
214 
215 static void hpgl_end_graph(GVJ_t * job)
216 {
217  gvputs(job, "# end of FIG file\n");
218 }
219 
220 static void hpgl_begin_page(GVJ_t * job)
221 {
222  Depth = 2;
223 }
224 
225 static void hpgl_begin_node(GVJ_t * job)
226 {
227  Depth = 1;
228 }
229 
230 static void hpgl_end_node(GVJ_t * job)
231 {
232  Depth = 2;
233 }
234 
235 static void hpgl_begin_edge(GVJ_t * job)
236 {
237  Depth = 0;
238 }
239 
240 static void hpgl_end_edge(GVJ_t * job)
241 {
242  Depth = 2;
243 }
244 
245 static void hpgl_textpara(GVJ_t * job, pointf p, textpara_t * para)
246 {
247  obj_state_t *obj = job->obj;
248 
249  int object_code = 4; /* always 4 for text */
250  int sub_type = 0; /* text justification */
251  int color = obj->pencolor.u.index;
252  int depth = Depth;
253  int pen_style = 0; /* not used */
254  int font = -1; /* init to xfig's default font */
255  double font_size = para->fontsize * job->zoom;
256  double angle = job->rotation ? (M_PI / 2.0) : 0.0;
257  int font_flags = 4; /* PostScript font */
258  double height = 0.0;
259  double length = 0.0;
260 
261  if (para->postscript_alias) /* if it is a standard postscript font */
262  font = para->postscript_alias->xfig_code;
263 
264  switch (para->just) {
265  case 'l':
266  sub_type = 0;
267  break;
268  case 'r':
269  sub_type = 2;
270  break;
271  default:
272  case 'n':
273  sub_type = 1;
274  break;
275  }
276 
277  gvprintf(job,
278  "%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %d %d %s\\001\n",
279  object_code, sub_type, color, depth, pen_style, font,
280  font_size, angle, font_flags, height, length, ROUND(p.x), ROUND(p.y),
281  hpgl_string(para->str));
282 }
283 
284 static void hpgl_ellipse(GVJ_t * job, pointf * A, int filled)
285 {
286  obj_state_t *obj = job->obj;
287 
288  int object_code = 1; /* always 1 for ellipse */
289  int sub_type = 1; /* ellipse defined by radii */
290  int line_style; /* solid, dotted, dashed */
291  int thickness = obj->penwidth;
292  int pen_color = obj->pencolor.u.index;
293  int fill_color = obj->fillcolor.u.index;
294  int depth = Depth;
295  int pen_style = 0; /* not used */
296  int area_fill = filled ? 20 : -1;
297  double style_val;
298  int direction = 0;
299  double angle = 0.0;
300  int center_x, center_y, radius_x, radius_y;
301  int start_x, start_y, end_x, end_y;
302 
303  hpgl_line_style(obj, &line_style, &style_val);
304 
305  start_x = center_x = ROUND(A[0].x);
306  start_y = center_y = ROUND(A[0].y);
307  radius_x = ROUND(A[1].x - A[0].x);
308  radius_y = ROUND(A[1].y - A[0].y);
309  end_x = ROUND(A[1].x);
310  end_y = ROUND(A[1].y);
311 
312  gvprintf(job,
313  "%d %d %d %d %d %d %d %d %d %.3f %d %.4f %d %d %d %d %d %d %d %d\n",
314  object_code, sub_type, line_style, thickness, pen_color,
315  fill_color, depth, pen_style, area_fill, style_val, direction,
316  angle, center_x, center_y, radius_x, radius_y, start_x,
317  start_y, end_x, end_y);
318 }
319 
320 static void hpgl_bezier(GVJ_t * job, pointf * A, int n, int arrow_at_start,
321  int arrow_at_end, int filled)
322 {
323  obj_state_t *obj = job->obj;
324 
325  int object_code = 3; /* always 3 for spline */
326  int sub_type;
327  int line_style; /* solid, dotted, dashed */
328  int thickness = obj->penwidth;
329  int pen_color = obj->pencolor.u.index;
330  int fill_color = obj->fillcolor.u.index;
331  int depth = Depth;
332  int pen_style = 0; /* not used */
333  int area_fill;
334  double style_val;
335  int cap_style = 0;
336  int forward_arrow = 0;
337  int backward_arrow = 0;
338  int npoints = n;
339  int i;
340 
341  pointf pf, V[4];
342  point p;
343  int j, step;
344  int count = 0;
345  int size;
346 
347  char *buffer;
348  char *buf;
349  buffer =
350  malloc((npoints + 1) * (BEZIERSUBDIVISION +
351  1) * 20 * sizeof(char));
352  buf = buffer;
353 
354  hpgl_line_style(obj, &line_style, &style_val);
355 
356  if (filled) {
357  sub_type = 5; /* closed X-spline */
358  area_fill = 20; /* fully saturated color */
359  fill_color = job->obj->fillcolor.u.index;
360  }
361  else {
362  sub_type = 4; /* opened X-spline */
363  area_fill = -1;
364  fill_color = 0;
365  }
366  V[3].x = A[0].x;
367  V[3].y = A[0].y;
368  /* Write first point in line */
369  count++;
370  PF2P(A[0], p);
371  size = sprintf(buf, " %d %d", p.x, p.y);
372  buf += size;
373  /* write subsequent points */
374  for (i = 0; i + 3 < n; i += 3) {
375  V[0] = V[3];
376  for (j = 1; j <= 3; j++) {
377  V[j].x = A[i + j].x;
378  V[j].y = A[i + j].y;
379  }
380  for (step = 1; step <= BEZIERSUBDIVISION; step++) {
381  count++;
382  pf = Bezier (V, 3, (double) step / BEZIERSUBDIVISION, NULL, NULL);
383  PF2P(pf, p);
384  size = sprintf(buf, " %d %d", p.x, p.y);
385  buf += size;
386  }
387  }
388 
389  gvprintf(job, "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d\n",
390  object_code,
391  sub_type,
392  line_style,
393  thickness,
394  pen_color,
395  fill_color,
396  depth,
397  pen_style,
398  area_fill,
399  style_val, cap_style, forward_arrow, backward_arrow, count);
400 
401  gvprintf(job, " %s\n", buffer); /* print points */
402  free(buffer);
403  for (i = 0; i < count; i++) {
404  gvprintf(job, " %d", i % (count - 1) ? 1 : 0); /* -1 on all */
405  }
406  gvputs(job, "\n");
407 }
408 
409 static void hpgl_polygon(GVJ_t * job, pointf * A, int n, int filled)
410 {
411  obj_state_t *obj = job->obj;
412 
413  int object_code = 2; /* always 2 for polyline */
414  int sub_type = 3; /* always 3 for polygon */
415  int line_style; /* solid, dotted, dashed */
416  int thickness = obj->penwidth;
417  int pen_color = obj->pencolor.u.index;
418  int fill_color = obj->fillcolor.u.index;
419  int depth = Depth;
420  int pen_style = 0; /* not used */
421  int area_fill = filled ? 20 : -1;
422  double style_val;
423  int join_style = 0;
424  int cap_style = 0;
425  int radius = 0;
426  int forward_arrow = 0;
427  int backward_arrow = 0;
428  int npoints = n + 1;
429 
430  hpgl_line_style(obj, &line_style, &style_val);
431 
432  gvprintf(job,
433  "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d %d %d\n",
434  object_code, sub_type, line_style, thickness, pen_color,
435  fill_color, depth, pen_style, area_fill, style_val, join_style,
436  cap_style, radius, forward_arrow, backward_arrow, npoints);
437  hpglptarray(job, A, n, 1); /* closed shape */
438 }
439 
440 static void hpgl_polyline(GVJ_t * job, pointf * A, int n)
441 {
442  obj_state_t *obj = job->obj;
443 
444  int object_code = 2; /* always 2 for polyline */
445  int sub_type = 1; /* always 1 for polyline */
446  int line_style; /* solid, dotted, dashed */
447  int thickness = obj->penwidth;
448  int pen_color = obj->pencolor.u.index;
449  int fill_color = 0;
450  int depth = Depth;
451  int pen_style = 0; /* not used */
452  int area_fill = 0;
453  double style_val;
454  int join_style = 0;
455  int cap_style = 0;
456  int radius = 0;
457  int forward_arrow = 0;
458  int backward_arrow = 0;
459  int npoints = n;
460 
461  hpgl_line_style(obj, &line_style, &style_val);
462 
463  gvprintf(job,
464  "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d %d %d\n",
465  object_code, sub_type, line_style, thickness, pen_color,
466  fill_color, depth, pen_style, area_fill, style_val, join_style,
467  cap_style, radius, forward_arrow, backward_arrow, npoints);
468  hpglptarray(job, A, n, 0); /* open shape */
469 }
470 
472  0, /* hpgl_begin_job */
473  0, /* hpgl_end_job */
474  hpgl_begin_graph,
475  hpgl_end_graph,
476  0, /* hpgl_begin_layer */
477  0, /* hpgl_end_layer */
478  hpgl_begin_page,
479  0, /* hpgl_end_page */
480  0, /* hpgl_begin_cluster */
481  0, /* hpgl_end_cluster */
482  0, /* hpgl_begin_nodes */
483  0, /* hpgl_end_nodes */
484  0, /* hpgl_begin_edges */
485  0, /* hpgl_end_edges */
486  hpgl_begin_node,
487  hpgl_end_node,
488  hpgl_begin_edge,
489  hpgl_end_edge,
490  0, /* hpgl_begin_anchor */
491  0, /* hpgl_end_anchor */
492  0, /* hpgl_begin_label */
493  0, /* hpgl_end_label */
494  hpgl_textpara,
495  hpgl_resolve_color,
496  hpgl_ellipse,
497  hpgl_polygon,
498  hpgl_bezier,
499  hpgl_polyline,
500  hpgl_comment,
501  0, /* hpgl_library_shape */
502 };
503 
504 static gvrender_features_t render_features_hpgl = {
505  0, /* flags */
506  4., /* default pad - graph units */
507  NULL, /* knowncolors */
508  0, /* sizeof knowncolors */
509  HSVA_DOUBLE, /* color_type */
510 };
511 
512 static gvdevice_features_t device_features_hpgl = {
513  0, /* flags */
514  {0.,0.}, /* default margin - points */
515  {0.,0.}, /* default page width, height - points */
516  {72.,72.}, /* default dpi */
517 };
518 
520  {FORMAT_HPGL, "hpgl", -1, &hpgl_engine, &render_features_hpgl},
521  {0, NULL, 0, NULL, NULL}
522 };
523 
525  {FORMAT_HPGL, "hpgl:hpgl", -1, NULL, &device_features_hpgl},
526  {0, NULL, 0, NULL, NULL}
527 };
gvplugin_installed_t gvdevice_hpgl_types[]
#define maxColors
int rotation
Definition: gvcjob.h:328
union color_s::@10 u
pen_type pen
Definition: gvcjob.h:206
int index
Definition: color.h:42
point pagesArraySize
Definition: gvcjob.h:313
#define ROUND(f)
Definition: arith.h:84
#define assert(x)
Definition: cghdr.h:47
Definition: geom.h:28
#define PF2P(pf, p)
Definition: geom.h:72
Definition: color.h:34
gvcolor_t pencolor
Definition: gvcjob.h:203
Definition: gvcjob.h:271
int x
Definition: geom.h:26
obj_state_t * obj
Definition: gvcjob.h:278
int gvputs(GVJ_t *job, const char *s)
Definition: gvdevice.c:270
color_type_t type
Definition: color.h:44
#define BEZIERSUBDIVISION
double y
Definition: geom.h:28
char * string
Definition: color.h:41
GVCOMMON_t * common
Definition: gvcjob.h:276
char ** info
Definition: gvcommon.h:22
Definition: grammar.c:79
graph_t * g
Definition: gvcjob.h:195
format_type
pointf Bezier(pointf *V, int degree, double t, pointf *Left, pointf *Right)
Definition: utils.c:221
#define NULL
Definition: logic.h:39
Definition: geom.h:26
double x
Definition: geom.h:28
#define streq(s, t)
Definition: cghdr.h:52
#define top(sp)
Definition: stack.h:35
union obj_state_s::@23 u
void(* pf)(char *, void *)
Definition: xdot.c:501
#define M_PI
Definition: arith.h:77
agxbuf * str
Definition: htmlparse.c:85
gvcolor_t fillcolor
Definition: gvcjob.h:203
double dist(Site *s, Site *t)
Definition: site.c:41
double penwidth
Definition: gvcjob.h:208
gvrender_engine_t hpgl_engine
int y
Definition: geom.h:26
unsigned char rgba[4]
Definition: color.h:38
gvplugin_installed_t gvrender_hpgl_types[]
double zoom
Definition: gvcjob.h:327
void gvprintf(GVJ_t *job, const char *format,...)
Definition: gvdevice.c:389