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