Graphviz  2.39.20141217.0545
gvrender_core_fig.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 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
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 "gvplugin_render.h"
32 #include "gvplugin_device.h"
33 #include "gvio.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_FIG, } format_type;
42 
43 static int Depth;
44 
45 static void figptarray(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 *fig_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 figColorResolve(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 *figcolor[] = {
134  "black", "blue", "green", "cyan", "red", "magenta", "yellow", "white", (char *) NULL
135 };
136 
137 static void fig_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; figcolor[i]; i++) {
145  if (streq(figcolor[i], color->u.string)) {
146  color->u.index = i;
147  break;
148  }
149  }
150  break;
151  case RGBA_BYTE:
152  i = 32 + figColorResolve(&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 fig_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 fig_comment(GVJ_t *job, char *str)
191 {
192  gvprintf(job, "# %s\n", str);
193 }
194 
195 static void fig_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", agnameof(obj->u.g));
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 fig_end_graph(GVJ_t * job)
216 {
217  gvputs(job, "# end of FIG file\n");
218 }
219 
220 static void fig_begin_page(GVJ_t * job)
221 {
222  Depth = 2;
223 }
224 
225 static void fig_begin_node(GVJ_t * job)
226 {
227  Depth = 1;
228 }
229 
230 static void fig_end_node(GVJ_t * job)
231 {
232  Depth = 2;
233 }
234 
235 static void fig_begin_edge(GVJ_t * job)
236 {
237  Depth = 0;
238 }
239 
240 static void fig_end_edge(GVJ_t * job)
241 {
242  Depth = 2;
243 }
244 
245 static void fig_textspan(GVJ_t * job, pointf p, textspan_t * span)
246 {
247  obj_state_t *obj = job->obj;
248  PostscriptAlias *pA;
249 
250  int object_code = 4; /* always 4 for text */
251  int sub_type = 0; /* text justification */
252  int color = obj->pencolor.u.index;
253  int depth = Depth;
254  int pen_style = 0; /* not used */
255  int font = -1; /* init to xfig's default font */
256  double font_size = span->font->size * job->zoom;
257  double angle = job->rotation ? (M_PI / 2.0) : 0.0;
258  int font_flags = 6; /* PostScript font + Special text */
259 /* Special text indicates that latex markup may exist
260  * in the output - but note that dot knows nothing about latex,
261  * so the node sizes may be wrong.
262  */
263  double height = 0.0;
264  double length = 0.0;
265 
266  pA = span->font->postscript_alias;
267  if (pA) /* if it is a standard postscript font */
268  font = pA->xfig_code;
269 
270  switch (span->just) {
271  case 'l':
272  sub_type = 0;
273  break;
274  case 'r':
275  sub_type = 2;
276  break;
277  default:
278  case 'n':
279  sub_type = 1;
280  break;
281  }
282 
283  gvprintf(job,
284  "%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %d %d %s\\001\n",
285  object_code, sub_type, color, depth, pen_style, font,
286  font_size, angle, font_flags, height, length, ROUND(p.x), ROUND(p.y),
287  fig_string(span->str));
288 }
289 
290 static void fig_ellipse(GVJ_t * job, pointf * A, int filled)
291 {
292  obj_state_t *obj = job->obj;
293 
294  int object_code = 1; /* always 1 for ellipse */
295  int sub_type = 1; /* ellipse defined by radii */
296  int line_style; /* solid, dotted, dashed */
297  int thickness = obj->penwidth;
298  int pen_color = obj->pencolor.u.index;
299  int fill_color = obj->fillcolor.u.index;
300  int depth = Depth;
301  int pen_style = 0; /* not used */
302  int area_fill = filled ? 20 : -1;
303  double style_val;
304  int direction = 0;
305  double angle = 0.0;
306  int center_x, center_y, radius_x, radius_y;
307  int start_x, start_y, end_x, end_y;
308 
309  fig_line_style(obj, &line_style, &style_val);
310 
311  start_x = center_x = ROUND(A[0].x);
312  start_y = center_y = ROUND(A[0].y);
313  radius_x = ROUND(A[1].x - A[0].x);
314  radius_y = ROUND(A[1].y - A[0].y);
315  end_x = ROUND(A[1].x);
316  end_y = ROUND(A[1].y);
317 
318  gvprintf(job,
319  "%d %d %d %d %d %d %d %d %d %.3f %d %.4f %d %d %d %d %d %d %d %d\n",
320  object_code, sub_type, line_style, thickness, pen_color,
321  fill_color, depth, pen_style, area_fill, style_val, direction,
322  angle, center_x, center_y, radius_x, radius_y, start_x,
323  start_y, end_x, end_y);
324 }
325 
326 static void fig_bezier(GVJ_t * job, pointf * A, int n, int arrow_at_start,
327  int arrow_at_end, int filled)
328 {
329  obj_state_t *obj = job->obj;
330 
331  int object_code = 3; /* always 3 for spline */
332  int sub_type;
333  int line_style; /* solid, dotted, dashed */
334  int thickness = obj->penwidth;
335  int pen_color = obj->pencolor.u.index;
336  int fill_color = obj->fillcolor.u.index;
337  int depth = Depth;
338  int pen_style = 0; /* not used */
339  int area_fill;
340  double style_val;
341  int cap_style = 0;
342  int forward_arrow = 0;
343  int backward_arrow = 0;
344  int npoints = n;
345  int i;
346 
347 
348  pointf pf, V[4];
349  point p;
350  int j, step;
351  int count = 0;
352  int size;
353 
354  char *buffer;
355  char *buf;
356  assert (n >= 4);
357 
358  buffer =
359  malloc((npoints + 1) * (BEZIERSUBDIVISION +
360  1) * 20 * sizeof(char));
361  buf = buffer;
362 
363  fig_line_style(obj, &line_style, &style_val);
364 
365  if (filled) {
366  sub_type = 5; /* closed X-spline */
367  area_fill = 20; /* fully saturated color */
368  fill_color = job->obj->fillcolor.u.index;
369  }
370  else {
371  sub_type = 4; /* opened X-spline */
372  area_fill = -1;
373  fill_color = 0;
374  }
375  V[3].x = A[0].x;
376  V[3].y = A[0].y;
377  /* Write first point in line */
378  count++;
379  PF2P(A[0], p);
380  size = sprintf(buf, " %d %d", p.x, p.y);
381  buf += size;
382  /* write subsequent points */
383  for (i = 0; i + 3 < n; i += 3) {
384  V[0] = V[3];
385  for (j = 1; j <= 3; j++) {
386  V[j].x = A[i + j].x;
387  V[j].y = A[i + j].y;
388  }
389  for (step = 1; step <= BEZIERSUBDIVISION; step++) {
390  count++;
391  pf = Bezier (V, 3, (double) step / BEZIERSUBDIVISION, NULL, NULL);
392  PF2P(pf, p);
393  size = sprintf(buf, " %d %d", p.x, p.y);
394  buf += size;
395  }
396  }
397 
398  gvprintf(job, "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d\n",
399  object_code,
400  sub_type,
401  line_style,
402  thickness,
403  pen_color,
404  fill_color,
405  depth,
406  pen_style,
407  area_fill,
408  style_val, cap_style, forward_arrow, backward_arrow, count);
409 
410  gvprintf(job, " %s\n", buffer); /* print points */
411  free(buffer);
412  for (i = 0; i < count; i++) {
413  gvprintf(job, " %d", i % (count - 1) ? 1 : 0); /* -1 on all */
414  }
415  gvputs(job, "\n");
416 }
417 
418 static void fig_polygon(GVJ_t * job, pointf * A, int n, int filled)
419 {
420  obj_state_t *obj = job->obj;
421 
422  int object_code = 2; /* always 2 for polyline */
423  int sub_type = 3; /* always 3 for polygon */
424  int line_style; /* solid, dotted, dashed */
425  int thickness = obj->penwidth;
426  int pen_color = obj->pencolor.u.index;
427  int fill_color = obj->fillcolor.u.index;
428  int depth = Depth;
429  int pen_style = 0; /* not used */
430  int area_fill = filled ? 20 : -1;
431  double style_val;
432  int join_style = 0;
433  int cap_style = 0;
434  int radius = 0;
435  int forward_arrow = 0;
436  int backward_arrow = 0;
437  int npoints = n + 1;
438 
439  fig_line_style(obj, &line_style, &style_val);
440 
441  gvprintf(job,
442  "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d %d %d\n",
443  object_code, sub_type, line_style, thickness, pen_color,
444  fill_color, depth, pen_style, area_fill, style_val, join_style,
445  cap_style, radius, forward_arrow, backward_arrow, npoints);
446  figptarray(job, A, n, 1); /* closed shape */
447 }
448 
449 static void fig_polyline(GVJ_t * job, pointf * A, int n)
450 {
451  obj_state_t *obj = job->obj;
452 
453  int object_code = 2; /* always 2 for polyline */
454  int sub_type = 1; /* always 1 for polyline */
455  int line_style; /* solid, dotted, dashed */
456  int thickness = obj->penwidth;
457  int pen_color = obj->pencolor.u.index;
458  int fill_color = 0;
459  int depth = Depth;
460  int pen_style = 0; /* not used */
461  int area_fill = 0;
462  double style_val;
463  int join_style = 0;
464  int cap_style = 0;
465  int radius = 0;
466  int forward_arrow = 0;
467  int backward_arrow = 0;
468  int npoints = n;
469 
470  fig_line_style(obj, &line_style, &style_val);
471 
472  gvprintf(job,
473  "%d %d %d %d %d %d %d %d %d %.1f %d %d %d %d %d %d\n",
474  object_code, sub_type, line_style, thickness, pen_color,
475  fill_color, depth, pen_style, area_fill, style_val, join_style,
476  cap_style, radius, forward_arrow, backward_arrow, npoints);
477  figptarray(job, A, n, 0); /* open shape */
478 }
479 
481  0, /* fig_begin_job */
482  0, /* fig_end_job */
483  fig_begin_graph,
484  fig_end_graph,
485  0, /* fig_begin_layer */
486  0, /* fig_end_layer */
487  fig_begin_page,
488  0, /* fig_end_page */
489  0, /* fig_begin_cluster */
490  0, /* fig_end_cluster */
491  0, /* fig_begin_nodes */
492  0, /* fig_end_nodes */
493  0, /* fig_begin_edges */
494  0, /* fig_end_edges */
495  fig_begin_node,
496  fig_end_node,
497  fig_begin_edge,
498  fig_end_edge,
499  0, /* fig_begin_anchor */
500  0, /* fig_end_anchor */
501  0, /* fig_begin_label */
502  0, /* fig_end_label */
503  fig_textspan,
504  fig_resolve_color,
505  fig_ellipse,
506  fig_polygon,
507  fig_bezier,
508  fig_polyline,
509  fig_comment,
510  0, /* fig_library_shape */
511 };
512 
513 
514 /* NB. List must be LANG_C sorted */
515 static char *fig_knowncolors[] = {
516  "black", "blue", "cyan", "green", "magenta", "red", "white", "yellow",
517 };
518 
519 
522  | GVRENDER_Y_GOES_DOWN, /* flags */
523  4., /* default pad - graph units */
524  fig_knowncolors, /* knowncolors */
525  sizeof(fig_knowncolors) / sizeof(char *), /* sizeof knowncolors */
526  RGBA_BYTE, /* color_type */
527 };
528 
531  | GVRENDER_Y_GOES_DOWN, /* flags */
532  {0.,0.}, /* default margin - points */
533  {0.,0.}, /* default page width, height - points */
534  {1440.,1440.}, /* default dpi */
535  /* FIXME - this default dpi is a very strange number!!!
536  * It was picked to make .png usershapes the right size on my screen.
537  * It happens to be 1.2 * 1200, but I can't explain the 1.2.
538  * (I was expecting 1.3333 which is 96/72, but thats too big.)
539  * Also 1200 is hardcoded in fig_begin_graph() instead of using job->dpi
540  */
541 
542  /* It may be TWIPS, i.e. 20 * POINT_PER_INCH
543  * but that doesn't explain what the 1200 is? */
544 };
545 
547  {FORMAT_FIG, "fig", 1, &fig_engine, &render_features_fig},
548  {0, NULL, 0, NULL, NULL}
549 };
550 
552  {FORMAT_FIG, "fig:fig", 1, NULL, &device_features_fig},
553  {0, NULL, 0, NULL, NULL}
554 };
gvdevice_features_t device_features_fig
int rotation
Definition: gvcjob.h:328
union color_s::@10 u
gvrender_features_t render_features_fig
pen_type pen
Definition: gvcjob.h:206
int index
Definition: color.h:42
double size
Definition: textspan.h:52
format_type
point pagesArraySize
Definition: gvcjob.h:313
#define ROUND(f)
Definition: arith.h:87
#define assert(x)
Definition: cghdr.h:48
Definition: geom.h:30
Definition: color.h:34
#define maxColors
gvcolor_t pencolor
Definition: gvcjob.h:203
Definition: gvcjob.h:271
int x
Definition: geom.h:28
obj_state_t * obj
Definition: gvcjob.h:278
#define GVRENDER_Y_GOES_DOWN
Definition: gvcjob.h:96
int gvputs(GVJ_t *job, const char *s)
Definition: gvdevice.c:274
char * malloc()
char * str
Definition: textspan.h:59
color_type_t type
Definition: color.h:44
void free()
gvplugin_installed_t gvrender_fig_types[]
int i
Definition: gvdevice.c:448
char * realloc()
double y
Definition: geom.h:30
gvrender_engine_t fig_engine
#define BEZIERSUBDIVISION
char * string
Definition: color.h:41
GVCOMMON_t * common
Definition: gvcjob.h:276
char ** info
Definition: gvcommon.h:22
#define EMIT_COLORS
Definition: gvcjob.h:85
PostscriptAlias * postscript_alias
Definition: textspan.h:51
gvplugin_installed_t gvdevice_fig_types[]
Definition: grammar.c:79
graph_t * g
Definition: gvcjob.h:195
pointf Bezier(pointf *V, int degree, double t, pointf *Left, pointf *Right)
Definition: utils.c:219
#define NULL
Definition: logic.h:50
Definition: geom.h:28
double x
Definition: geom.h:30
#define streq(s, t)
Definition: cghdr.h:58
#define top(sp)
Definition: stack.h:35
char * agnameof(void *)
Definition: id.c:143
char just
Definition: textspan.h:65
union obj_state_s::@23 u
void(* pf)(char *, void *)
Definition: xdot.c:501
#define M_PI
Definition: arith.h:80
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
int y
Definition: geom.h:28
unsigned char rgba[4]
Definition: color.h:38
double zoom
Definition: gvcjob.h:327
void gvprintf(GVJ_t *job, const char *format,...)
Definition: gvdevice.c:393
textfont_t * font
Definition: textspan.h:60