Graphviz 12.0.1~dev.20240715.2254
Loading...
Searching...
No Matches
gvrender_core_pic.c
Go to the documentation of this file.
1/*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12#include <math.h>
13#include <stdbool.h>
14#include <stdint.h>
15#include <stdlib.h>
16#include <string.h>
17#include <stdarg.h>
18
19#include <gvc/gvplugin_render.h>
20#include <gvc/gvplugin_device.h>
21#include <gvc/gvio.h>
22#include <cgraph/agxbuf.h>
23#include <cgraph/strview.h>
24#include <common/utils.h>
25#include <common/color.h>
26#include <common/colorprocs.h>
27
28#include <common/const.h>
29
30/* Number of points to split splines into */
31#define BEZIERSUBDIVISION 6
32
34
35static bool onetime = true;
36static double Fontscale;
37
38/* There are a couple of ways to generate output:
39 1. generate for whatever size is given by the bounding box
40 - the drawing at its "natural" size might not fit on a physical page
41 ~ dot size specification can be used to scale the drawing
42 ~ and it's not difficult for user to scale the pic output to fit (multiply 4 (3 distinct) numbers on 3 lines by a scale factor)
43 - some troff implementations may clip large graphs
44 ~ handle by scaling to manageable size
45 - give explicit width and height as parameters to .PS
46 - pic scale variable is reset to 1.0
47 - fonts are printed as size specified by caller, modified by user scaling
48 2. scale to fit on a physical page
49 - requires an assumption of page size (GNU pic assumes 8.5x11.0 inches)
50 ~ any assumption is bound to be wrong more often than right
51 - requires separate scaling of font point sizes since pic's scale variable doesn't affect text
52 ~ possible, as above
53 - likewise for line thickness
54 - GNU pic does this (except for fonts) if .PS is used without explicit width or height; DWB pic does not
55 ~ pic variants likely to cause trouble
56 The first approach is used here.
57*/
58
59static const char pic_comments[] = "# "; /* PIC comment */
60static const char troff_comments[] = ".\\\" "; /* troff comment */
61static const char picgen_msghdr[] = "dot pic plugin: ";
62
63static void unsupported(char *s)
64{
65 agwarningf("%s%s unsupported\n", picgen_msghdr, s);
66}
67
68/* troff font mapping */
69typedef struct {
70 char trname[3], *psname;
71} fontinfo;
72
73static const fontinfo fonttab[] = {
74 {"AB", "AvantGarde-Demi"},
75 {"AI", "AvantGarde-BookOblique"},
76 {"AR", "AvantGarde-Book"},
77 {"AX", "AvantGarde-DemiOblique"},
78 {"B ", "Times-Bold"},
79 {"BI", "Times-BoldItalic"},
80 {"CB", "Courier-Bold"},
81 {"CO", "Courier"},
82 {"CX", "Courier-BoldOblique"},
83 {"H ", "Helvetica"},
84 {"HB", "Helvetica-Bold"},
85 {"HI", "Helvetica-Oblique"},
86 {"HX", "Helvetica-BoldOblique"},
87 {"Hb", "Helvetica-Narrow-Bold"},
88 {"Hi", "Helvetica-Narrow-Oblique"},
89 {"Hr", "Helvetica-Narrow"},
90 {"Hx", "Helvetica-Narrow-BoldOblique"},
91 {"I ", "Times-Italic"},
92 {"KB", "Bookman-Demi"},
93 {"KI", "Bookman-LightItalic"},
94 {"KR", "Bookman-Light"},
95 {"KX", "Bookman-DemiItalic"},
96 {"NB", "NewCenturySchlbk-Bold"},
97 {"NI", "NewCenturySchlbk-Italic"},
98 {"NR", "NewCenturySchlbk-Roman"},
99 {"NX", "NewCenturySchlbk-BoldItalic"},
100 {"PA", "Palatino-Roman"},
101 {"PB", "Palatino-Bold"},
102 {"PI", "Palatino-Italic"},
103 {"PX", "Palatino-BoldItalic"},
104 {"R ", "Times-Roman"},
105 {"S ", "Symbol"},
106 {"ZD", "ZapfDingbats"},
107};
108static const size_t fonttab_size = sizeof(fonttab) / sizeof(fonttab[0]);
109
110#ifdef HAVE_MEMRCHR
111void *memrchr(const void *s, int c, size_t n);
112#else
113static const void *memrchr(const void *s, int c, size_t n) {
114 const char *str = s;
115 for (size_t i = n - 1; i != SIZE_MAX; --i) {
116 if (str[i] == c) {
117 return &str[i];
118 }
119 }
120 return NULL;
121}
122#endif
123
124static const char *picfontname(strview_t psname) {
125 for (size_t i = 0; i < fonttab_size; ++i)
126 if (strview_str_eq(psname, fonttab[i].psname))
127 return fonttab[i].trname;
128 agerrorf("%s%.*s is not a troff font\n", picgen_msghdr,
129 (int)psname.size, psname.data);
130 /* try base font names, e.g. Helvetica-Outline-Oblique -> Helvetica-Outline -> Helvetica */
131 const char *dash = memrchr(psname.data, '-', psname.size);
132 if (dash != NULL) {
133 strview_t prefix = {.data = psname.data,
134 .size = (size_t)(dash - psname.data)};
135 return picfontname(prefix);
136 }
137 return "R";
138}
139
140static void picptarray(GVJ_t *job, pointf *A, size_t n, int close) {
141 for (size_t i = 0; i < n; i++) {
142 if (i == 0) {
143 gvprintf(job, "move to (%.0f, %.0f)", A[i].x, A[i].y);
144 } else {
145 gvprintf(job, "; line to (%.0f, %.0f)", A[i].x, A[i].y);
146 }
147 }
148 if (close) {
149 gvprintf(job, "; line to (%.0f, %.0f)", A[0].x, A[0].y);
150 }
151 gvputs(job, "\n");
152}
153
154static void pic_comment(GVJ_t *job, char *str)
155{
156 gvprintf(job, "%s %s\n", pic_comments, str);
157}
158
159static void pic_begin_graph(GVJ_t * job)
160{
161 obj_state_t *obj = job->obj;
162
163 gvprintf(job, "%s Creator: %s version %s (%s)\n",
164 troff_comments, job->common->info[0], job->common->info[1], job->common->info[2]);
165 gvprintf(job, "%s Title: %s\n", troff_comments, agnameof(obj->u.g));
166 gvprintf(job,
167 "%s save point size and font\n.nr .S \\n(.s\n.nr DF \\n(.f\n",
169}
170
171static void pic_end_graph(GVJ_t * job)
172{
173 gvprintf(job,
174 "%s restore point size and font\n.ps \\n(.S\n.ft \\n(DF\n",
176}
177
178static void pic_begin_page(GVJ_t * job)
179{
180 box pbr = job->pageBoundingBox;
181
182 if (onetime && job->rotation && job->rotation != 90) {
183 unsupported("rotation");
184 onetime = false;
185 }
186 double height = PS2INCH((double)pbr.UR.y - (double)pbr.LL.y);
187 double width = PS2INCH((double)pbr.UR.x - (double)pbr.LL.x);
188 if (job->rotation == 90) {
189 double temp = width;
190 width = height;
191 height = temp;
192 }
193 gvprintf(job, ".PS %.5f %.5f\n", width, height);
194 gvprintf(job,
195 "%s to change drawing size, multiply the width and height on the .PS line above and the number on the two lines below (rounded to the nearest integer) by a scale factor\n",
197 if (width > 0.0) {
198 Fontscale = log10(width);
199 Fontscale += 3.0 - (int) Fontscale; /* between 3.0 and 4.0 */
200 } else
201 Fontscale = 3.0;
202 Fontscale = pow(10.0, Fontscale); /* a power of 10 times width, between 1000 and 10000 */
203 gvprintf(job, ".nr SF %.0f\nscalethickness = %.0f\n", Fontscale,
204 Fontscale);
205 gvprintf(job,
206 "%s don't change anything below this line in this drawing\n",
208 gvprintf(job,
209 "%s non-fatal run-time pic version determination, version 2\n",
211 gvprintf(job,
212 "boxrad=2.0 %s will be reset to 0.0 by gpic only\n",
214 gvprintf(job, "scale=1.0 %s required for comparisons\n",
216 gvprintf(job,
217 "%s boxrad is now 0.0 in gpic, else it remains 2.0\n",
219 gvprintf(job,
220 "%s dashwid is 0.1 in 10th Edition, 0.05 in DWB 2 and in gpic\n",
222 gvprintf(job,
223 "%s fillval is 0.3 in 10th Edition (fill 0 means black), 0.5 in gpic (fill 0 means white), undefined in DWB 2\n",
225 gvprintf(job,
226 "%s fill has no meaning in DWB 2, gpic can use fill or filled, 10th Edition uses fill only\n",
228 gvprintf(job,
229 "%s DWB 2 doesn't use fill and doesn't define fillval\n",
231 gvprintf(job,
232 "%s reset works in gpic and 10th edition, but isn't defined in DWB 2\n",
234 gvprintf(job, "%s DWB 2 compatibility definitions\n",
236 gvprintf(job,
237 "if boxrad > 1.0 && dashwid < 0.075 then X\n\tfillval = 1;\n\tdefine fill Y Y;\n\tdefine solid Y Y;\n\tdefine reset Y scale=1.0 Y;\nX\n");
238 gvprintf(job, "reset %s set to known state\n", pic_comments);
239 gvprintf(job, "%s GNU pic vs. 10th Edition d\\(e'tente\n",
241 gvprintf(job,
242 "if fillval > 0.4 then X\n\tdefine setfillval Y fillval = 1 - Y;\n\tdefine bold Y thickness 2 Y;\n");
243 gvprintf(job,
244 "\t%s if you use gpic and it barfs on encountering \"solid\",\n",
246 gvprintf(job,
247 "\t%s\tinstall a more recent version of gpic or switch to DWB or 10th Edition pic;\n",
249 gvprintf(job,
250 "\t%s\tsorry, the groff folks changed gpic; send any complaint to them;\n",
252 gvprintf(job,
253 "X else Z\n\tdefine setfillval Y fillval = Y;\n\tdefine bold Y Y;\n\tdefine filled Y fill Y;\nZ\n");
254 gvprintf(job,
255 "%s arrowhead has no meaning in DWB 2, arrowhead = 7 makes filled arrowheads in gpic and in 10th Edition\n",
257 gvprintf(job,
258 "%s arrowhead is undefined in DWB 2, initially 1 in gpic, 2 in 10th Edition\n",
260 gvprintf(job, "arrowhead = 7 %s not used by graphviz\n",
262 gvprintf(job,
263 "%s GNU pic supports a boxrad variable to draw boxes with rounded corners; DWB and 10th Ed. do not\n",
265 gvprintf(job, "boxrad = 0 %s no rounded corners in graphviz\n",
267 gvprintf(job,
268 "%s GNU pic supports a linethick variable to set line thickness; DWB and 10th Ed. do not\n",
270 gvprintf(job, "linethick = 0; oldlinethick = linethick\n");
271 gvprintf(job,
272 "%s .PS w/o args causes GNU pic to scale drawing to fit 8.5x11 paper; DWB does not\n",
274 gvprintf(job,
275 "%s maxpsht and maxpswid have no meaning in DWB 2.0, set page boundaries in gpic and in 10th Edition\n",
277 gvprintf(job,
278 "%s maxpsht and maxpswid are predefined to 11.0 and 8.5 in gpic\n",
280 gvprintf(job, "maxpsht = %f\nmaxpswid = %f\n", height, width);
281 gvprintf(job, "Dot: [\n");
282 gvprintf(job,
283 "define attrs0 %% %%; define unfilled %% %%; define rounded %% %%; define diagonals %% %%\n");
284}
285
286static void pic_end_page(GVJ_t * job)
287{
288 gvprintf(job,
289 "]\n.PE\n");
290}
291
292static void pic_textspan(GVJ_t * job, pointf p, textspan_t * span)
293{
294 static char *lastname;
295 static double lastsize;
296
297 switch (span->just) {
298 case 'l':
299 break;
300 case 'r':
301 p.x -= span->size.x;
302 break;
303 default:
304 case 'n':
305 p.x -= span->size.x / 2;
306 break;
307 }
308 /* Why on earth would we do this. But it works. SCN 2/26/2002 */
309 p.y += span->font->size / (3.0 * POINTS_PER_INCH);
310 p.x += span->size.x / (2.0 * POINTS_PER_INCH);
311
312 if (span->font->name && (!lastname || strcmp(lastname, span->font->name))) {
313 gvprintf(job, ".ft %s\n", picfontname(strview(span->font->name, '\0')));
314 lastname = span->font->name;
315 }
316 double sz = fmax(span->font->size, 1);
317 if (fabs(sz - lastsize) > 0.5) {
318 gvprintf(job, ".ps %.0f*\\n(SFu/%.0fu\n", sz, Fontscale);
319 lastsize = sz;
320 }
321 gvputc(job, '"');
322 gvputs_nonascii(job, span->str);
323 gvprintf(job, "\" at (%.5f,%.5f);\n", p.x, p.y);
324}
325
326static void pic_ellipse(GVJ_t * job, pointf * A, int filled)
327{
328 /* A[] contains 2 points: the center and corner. */
329
330 gvprintf(job,
331 "ellipse attrs0 %swid %.5f ht %.5f at (%.5f,%.5f);\n",
332 filled ? "fill " : "",
333 PS2INCH(2*(A[1].x - A[0].x)),
334 PS2INCH(2*(A[1].y - A[0].y)),
335 PS2INCH(A[0].x),
336 PS2INCH(A[0].y));
337}
338
339static void pic_bezier(GVJ_t *job, pointf *A, size_t n, int filled) {
340 (void)filled;
341
342 pointf V[4];
343
344 V[3].x = A[0].x;
345 V[3].y = A[0].y;
346 /* Write first point in line */
347 gvprintf(job, "move to (%.0f, %.0f)", A[0].x, A[0].y);
348 /* write subsequent points */
349 for (size_t i = 0; i + 3 < n; i += 3) {
350 V[0] = V[3];
351 for (size_t j = 1; j <= 3; j++) {
352 V[j].x = A[i + j].x;
353 V[j].y = A[i + j].y;
354 }
355 for (int step = 1; step <= BEZIERSUBDIVISION; step++) {
356 pointf pf = Bezier(V, (double)step / BEZIERSUBDIVISION, NULL, NULL);
357 gvprintf(job, "; spline to (%.0f, %.0f)", pf.x, pf.y);
358 }
359 }
360
361 gvputs(job, "\n");
362}
363
364static void pic_polygon(GVJ_t *job, pointf *A, size_t n, int filled) {
365 (void)filled;
366 picptarray(job, A, n, 1); // closed shape
367}
368
369static void pic_polyline(GVJ_t *job, pointf *A, size_t n) {
370 picptarray(job, A, n, 0); // open shape
371}
372
374 0, /* pic_begin_job */
375 0, /* pic_end_job */
378 0, /* pic_begin_layer */
379 0, /* pic_end_layer */
382 0, /* pic_begin_cluster */
383 0, /* pic_end_cluster */
384 0, /* pic_begin_nodes */
385 0, /* pic_end_nodes */
386 0, /* pic_begin_edges */
387 0, /* pic_end_edges */
388 0, /* pic_begin_node */
389 0, /* pic_end_node */
390 0, /* pic_begin_edge */
391 0, /* pic_end_edge */
392 0, /* pic_begin_anchor */
393 0, /* pic_end_anchor */
394 0, /* pic_begin_label */
395 0, /* pic_end_label */
397 0, /* pic_resolve_color */
403 0, /* pic_library_shape */
404};
405
406
408 0, /* flags */
409 4., /* default pad - graph units */
410 NULL, /* knowncolors */
411 0, /* sizeof knowncolors */
412 HSVA_DOUBLE, /* color_type */
413};
414
416 0, /* flags */
417 {0.,0.}, /* default margin - points */
418 {0.,0.}, /* default page width, height - points */
419 {72.,72.}, /* default dpi */
420};
421
426
428 {FORMAT_PIC, "pic:pic", -1, NULL, &device_features_pic},
429 {0, NULL, 0, NULL, NULL}
430};
@ HSVA_DOUBLE
Definition color.h:26
pointf Bezier(pointf *V, double t, pointf *Left, pointf *Right)
Definition utils.c:169
#define A(n, t)
Definition expr.h:76
#define V
Definition gdefs.h:5
#define PS2INCH(a_points)
Definition geom.h:70
#define POINTS_PER_INCH
Definition geom.h:64
require define api prefix
Definition gmlparse.y:17
#define SIZE_MAX
Definition gmlscan.c:347
node NULL
Definition grammar.y:149
void agwarningf(const char *fmt,...)
Definition agerror.c:173
void agerrorf(const char *fmt,...)
Definition agerror.c:165
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
int gvputc(GVJ_t *job, int c)
Definition gvdevice.c:290
void gvputs_nonascii(GVJ_t *job, const char *s)
Definition gvdevice.c:278
int gvputs(GVJ_t *job, const char *s)
Definition gvdevice.c:263
void gvprintf(GVJ_t *job, const char *format,...)
Definition gvdevice.c:394
static void pic_bezier(GVJ_t *job, pointf *A, size_t n, int filled)
static const char * picfontname(strview_t psname)
static void pic_end_graph(GVJ_t *job)
gvplugin_installed_t gvdevice_pic_types[]
static void picptarray(GVJ_t *job, pointf *A, size_t n, int close)
@ FORMAT_PIC
static void pic_polygon(GVJ_t *job, pointf *A, size_t n, int filled)
static void pic_begin_graph(GVJ_t *job)
static double Fontscale
gvrender_engine_t pic_engine
static const char troff_comments[]
static gvrender_features_t render_features_pic
static const char picgen_msghdr[]
static const size_t fonttab_size
static void pic_textspan(GVJ_t *job, pointf p, textspan_t *span)
static void pic_ellipse(GVJ_t *job, pointf *A, int filled)
static bool onetime
gvplugin_installed_t gvrender_pic_types[]
static const fontinfo fonttab[]
static const void * memrchr(const void *s, int c, size_t n)
static void pic_comment(GVJ_t *job, char *str)
static void pic_end_page(GVJ_t *job)
static void pic_begin_page(GVJ_t *job)
static const char pic_comments[]
static void pic_polyline(GVJ_t *job, pointf *A, size_t n)
#define BEZIERSUBDIVISION
static gvdevice_features_t device_features_pic
static void unsupported(char *s)
agxbuf * str
Definition htmlparse.c:97
char ** info
Definition gvcommon.h:20
int rotation
Definition gvcjob.h:319
obj_state_t * obj
Definition gvcjob.h:269
GVCOMMON_t * common
Definition gvcjob.h:267
box pageBoundingBox
Definition gvcjob.h:329
Definition geom.h:39
point LL
Definition geom.h:39
point UR
Definition geom.h:39
ingroup plugin_api
Definition gvplugin.h:35
union obj_state_s::@92 u
graph_t * g
Definition gvcjob.h:186
int y
Definition geom.h:27
int x
Definition geom.h:27
double x
Definition geom.h:29
double y
Definition geom.h:29
a non-owning string reference
Definition strview.h:20
const char * data
start of the pointed to string
Definition strview.h:21
size_t size
extent of the string in bytes
Definition strview.h:22
char * name
Definition textspan.h:54
double size
Definition textspan.h:57
char * str
Definition textspan.h:65
char just
'l' 'n' 'r'
Definition textspan.h:71
pointf size
Definition textspan.h:70
textfont_t * font
Definition textspan.h:66
Non-owning string references.
static bool strview_str_eq(strview_t a, const char *b)
compare a string reference to a string for equality
Definition strview.h:98
static strview_t strview(const char *referent, char terminator)
create a string reference
Definition strview.h:26
Definition grammar.c:93
int(* pf)(void *, char *,...)
Definition xdot.c:405