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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Details at https://graphviz.org
*************************************************************************/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <gvc/gvplugin_render.h>
#include <gvc/gvplugin_device.h>
#include <gvc/gvio.h>
#include <cgraph/cgraph.h>
#include <common/utils.h>
#include <util/agxbuf.h>
#include <util/gv_ctype.h>
#include <util/prisize_t.h>
#include "ps.h"
/* for CHAR_LATIN1 */
#include <common/const.h>
/*
* J$: added `pdfmark' URL embedding. PostScript rendered from
* dot files with URL attributes will get active PDF links
* from Adobe's Distiller.
*/
#define PDFMAX 14400 /* Maximum size of PDF page */
enum { FORMAT_PS, FORMAT_PS2, FORMAT_EPS };
static int isLatin1;
static bool setupLatin1;
static void psgen_begin_job(GVJ_t * job)
{
gvputs(job, "%!PS-Adobe-3.0");
if (job->render.id == FORMAT_EPS)
gvputs(job, " EPSF-3.0\n");
else
gvputs(job, "\n");
gvprintf(job, "%%%%Creator: %s version %s (%s)\n",
job->common->info[0], job->common->info[1], job->common->info[2]);
}
static void psgen_end_job(GVJ_t * job)
{
gvputs(job, "%%Trailer\n");
if (job->render.id != FORMAT_EPS)
gvprintf(job, "%%%%Pages: %d\n", job->common->viewNum);
if (job->common->show_boxes == NULL)
if (job->render.id != FORMAT_EPS)
gvprintf(job, "%%%%BoundingBox: %d %d %d %d\n",
job->boundingBox.LL.x, job->boundingBox.LL.y,
job->boundingBox.UR.x, job->boundingBox.UR.y);
gvputs(job, "end\nrestore\n");
gvputs(job, "%%EOF\n");
}
static void psgen_begin_graph(GVJ_t * job)
{
obj_state_t *obj = job->obj;
setupLatin1 = false;
if (job->common->viewNum == 0) {
gvprintf(job, "%%%%Title: %s\n", agnameof(obj->u.g));
if (job->render.id != FORMAT_EPS)
gvputs(job, "%%Pages: (atend)\n");
else
gvputs(job, "%%Pages: 1\n");
if (job->common->show_boxes == NULL) {
if (job->render.id != FORMAT_EPS)
gvputs(job, "%%BoundingBox: (atend)\n");
else
gvprintf(job, "%%%%BoundingBox: %d %d %d %d\n",
job->pageBoundingBox.LL.x, job->pageBoundingBox.LL.y,
job->pageBoundingBox.UR.x, job->pageBoundingBox.UR.y);
}
gvputs(job, "%%EndComments\nsave\n");
/* include shape library */
cat_libfile(job, job->common->lib, ps_txt);
/* include epsf */
epsf_define(job);
if (job->common->show_boxes) {
const char *args[2] = {job->common->show_boxes[0]};
cat_libfile(job, NULL, args);
}
}
isLatin1 = (GD_charset(obj->u.g) == CHAR_LATIN1 ? CHAR_LATIN1 : -1);
/* We always setup Latin1. The charset info is always output,
* and installing it is cheap. With it installed, we can then
* rely on ps_string to convert UTF-8 characters whose encoding
* is in the range of Latin-1 into the Latin-1 equivalent and
* get the expected PostScript output.
*/
if (!setupLatin1) {
gvputs(job, "setupLatin1\n"); /* as defined in ps header */
setupLatin1 = true;
}
/* Set base URL for relative links (for Distiller >= 3.0) */
if (obj->url) {
char *const ps = ps_string(obj->url, isLatin1);
gvprintf(job, "[ {Catalog} << /URI << /Base %s >> >>\n/PUT pdfmark\n", ps);
free(ps);
}
}
static void psgen_begin_layer(GVJ_t * job, char *layername, int layerNum, int numLayers)
{
(void)layername;
gvprintf(job, "%d %d setlayer\n", layerNum, numLayers);
}
static void psgen_begin_page(GVJ_t * job)
{
box pbr = job->pageBoundingBox;
gvprintf(job, "%%%%Page: %d %d\n",
job->common->viewNum + 1, job->common->viewNum + 1);
if (job->common->show_boxes == NULL)
gvprintf(job, "%%%%PageBoundingBox: %d %d %d %d\n",
pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
gvprintf(job, "%%%%PageOrientation: %s\n",
(job->rotation ? "Landscape" : "Portrait"));
if (job->render.id == FORMAT_PS2)
gvprintf(job, "<< /PageSize [%d %d] >> setpagedevice\n",
pbr.UR.x, pbr.UR.y);
gvprintf(job, "%d %d %d beginpage\n",
job->pagesArrayElem.x, job->pagesArrayElem.y, job->numPages);
if (job->common->show_boxes == NULL)
gvprintf(job, "gsave\n%d %d %d %d boxprim clip newpath\n",
pbr.LL.x, pbr.LL.y, pbr.UR.x-pbr.LL.x, pbr.UR.y-pbr.LL.y);
gvprintf(job, "%g %g set_scale %d rotate %g %g translate\n",
job->scale.x, job->scale.y,
job->rotation,
job->translation.x, job->translation.y);
/* Define the size of the PS canvas */
if (job->render.id == FORMAT_PS2) {
if (pbr.UR.x >= PDFMAX || pbr.UR.y >= PDFMAX)
job->common->errorfn("canvas size (%d,%d) exceeds PDF limit (%d)\n"
"\t(suggest setting a bounding box size, see dot(1))\n",
pbr.UR.x, pbr.UR.y, PDFMAX);
gvprintf(job, "[ /CropBox [%d %d %d %d] /PAGES pdfmark\n",
pbr.LL.x, pbr.LL.y, pbr.UR.x, pbr.UR.y);
}
}
static void psgen_end_page(GVJ_t * job)
{
if (job->common->show_boxes) {
gvputs(job, "0 0 0 edgecolor\n");
cat_libfile(job, NULL, job->common->show_boxes + 1);
}
/* the showpage is really a no-op, but at least one PS processor
* out there needs to see this literal token. endpage does the real work.
*/
gvputs(job, "endpage\nshowpage\ngrestore\n");
gvputs(job, "%%PageTrailer\n");
gvprintf(job, "%%%%EndPage: %d\n", job->common->viewNum);
}
static void psgen_begin_cluster(GVJ_t * job)
{
obj_state_t *obj = job->obj;
gvprintf(job, "%% %s\n", agnameof(obj->u.g));
gvputs(job, "gsave\n");
}
static void psgen_end_cluster(GVJ_t * job)
{
gvputs(job, "grestore\n");
}
static void psgen_begin_node(GVJ_t * job)
{
gvputs(job, "gsave\n");
}
static void psgen_end_node(GVJ_t * job)
{
gvputs(job, "grestore\n");
}
static void
psgen_begin_edge(GVJ_t * job)
{
gvputs(job, "gsave\n");
}
static void psgen_end_edge(GVJ_t * job)
{
gvputs(job, "grestore\n");
}
static void psgen_begin_anchor(GVJ_t *job, char *url, char *tooltip, char *target, char *id)
{
(void)tooltip;
(void)target;
(void)id;
obj_state_t *obj = job->obj;
if (url && obj->url_map_p) {
gvputs(job, "[ /Rect [ ");
gvprintpointflist(job, obj->url_map_p, 2);
gvputs(job, " ]\n");
char *const ps = ps_string(url, isLatin1);
gvprintf(job, " /Border [ 0 0 0 ]\n"
" /Action << /Subtype /URI /URI %s >>\n"
" /Subtype /Link\n"
"/ANN pdfmark\n",
ps);
free(ps);
}
}
static void
ps_set_pen_style(GVJ_t *job)
{
double penwidth = job->obj->penwidth;
char *p, *line, **s = job->obj->rawstyle;
gvprintdouble(job, penwidth);
gvputs(job," setlinewidth\n");
while (s && (p = line = *s++)) {
if (strcmp(line, "setlinewidth") == 0)
continue;
while (*p)
p++;
p++;
while (*p) {
gvprintf(job,"%s ", p);
while (*p)
p++;
p++;
}
if (strcmp(line, "invis") == 0)
job->obj->penwidth = 0;
gvprintf(job, "%s\n", line);
}
}
static void ps_set_color(GVJ_t *job, gvcolor_t *color)
{
char *objtype;
if (color) {
switch (job->obj->type) {
case ROOTGRAPH_OBJTYPE:
case CLUSTER_OBJTYPE:
objtype = "graph";
break;
case NODE_OBJTYPE:
objtype = "node";
break;
case EDGE_OBJTYPE:
objtype = "edge";
break;
default:
objtype = "sethsb";
break;
}
gvprintf(job, "%.5g %.5g %.5g %scolor\n",
color->u.HSVA[0], color->u.HSVA[1], color->u.HSVA[2], objtype);
}
}
/** check a font name abides by Adobe’s rules
*
* Adobe Technical Note #5088 “Font Naming Issues” §2.2:
*
* For compatibility with the earliest versions of PostScript interpreters and
* with the file systems in some operating systems, Adobe limits the number of
* characters in the `FontName` to 29 characters.
*
* As with any PostScript language name, a valid `FontName` must not contain
* spaces, and may only use characters from the standard ASCII character set.
*
* @param fontname Font name to check
*/
static void check_fontname(const char *fontname) {
if (strlen(fontname) > 29) {
agwarningf(
"font name %s is longer than 29 characters which may be rejected by "
"some PS viewers\n",
fontname);
}
for (const char *p = fontname; *p != '\0'; ++p) {
if (!isascii((int)*p) || *p == ' ' || gv_iscntrl(*p)) {
agwarningf(
"font name %s contains characters that may not be accepted by some "
"PS viewers\n",
fontname);
break;
}
}
}
static void psgen_textspan(GVJ_t * job, pointf p, textspan_t * span)
{
char *str;
if (job->obj->pencolor.u.HSVA[3] < .5)
return; /* skip transparent text */
ps_set_color(job, &(job->obj->pencolor));
gvprintdouble(job, span->font->size);
check_fontname(span->font->name);
gvprintf(job, " /%s set_font\n", span->font->name);
str = ps_string(span->str,isLatin1);
switch (span->just) {
case 'r':
p.x -= span->size.x;
break;
case 'l':
p.x -= 0.0;
break;
case 'n':
default:
p.x -= span->size.x / 2.0;
break;
}
p.y += span->yoffset_centerline;
gvprintpointf(job, p);
gvputs(job, " moveto ");
gvprintdouble(job, span->size.x);
gvprintf(job, " %s alignedtext\n", str);
free(str);
}
static void psgen_ellipse(GVJ_t * job, pointf * A, int filled)
{
/* A[] contains 2 points: the center and corner. */
pointf AA[2];
AA[0] = A[0];
AA[1].x = A[1].x - A[0].x;
AA[1].y = A[1].y - A[0].y;
if (filled && job->obj->fillcolor.u.HSVA[3] > .5) {
ps_set_color(job, &(job->obj->fillcolor));
gvprintpointflist(job, AA, 2);
gvputs(job, " ellipse_path fill\n");
}
if (job->obj->pencolor.u.HSVA[3] > .5) {
ps_set_pen_style(job);
ps_set_color(job, &(job->obj->pencolor));
gvprintpointflist(job, AA, 2);
gvputs(job, " ellipse_path stroke\n");
}
}
static void psgen_bezier(GVJ_t *job, pointf *A, size_t n, int filled) {
if (filled && job->obj->fillcolor.u.HSVA[3] > .5) {
ps_set_color(job, &(job->obj->fillcolor));
gvputs(job, "newpath ");
gvprintpointf(job, A[0]);
gvputs(job, " moveto\n");
for (size_t j = 1; j < n; j += 3) {
gvprintpointflist(job, &A[j], 3);
gvputs(job, " curveto\n");
}
gvputs(job, "closepath fill\n");
}
if (job->obj->pencolor.u.HSVA[3] > .5) {
ps_set_pen_style(job);
ps_set_color(job, &(job->obj->pencolor));
gvputs(job, "newpath ");
gvprintpointf(job, A[0]);
gvputs(job, " moveto\n");
for (size_t j = 1; j < n; j += 3) {
gvprintpointflist(job, &A[j], 3);
gvputs(job, " curveto\n");
}
gvputs(job, "stroke\n");
}
}
static void psgen_polygon(GVJ_t *job, pointf *A, size_t n, int filled) {
if (filled && job->obj->fillcolor.u.HSVA[3] > .5) {
ps_set_color(job, &(job->obj->fillcolor));
gvputs(job, "newpath ");
gvprintpointf(job, A[0]);
gvputs(job, " moveto\n");
for (size_t j = 1; j < n; j++) {
gvprintpointf(job, A[j]);
gvputs(job, " lineto\n");
}
gvputs(job, "closepath fill\n");
}
if (job->obj->pencolor.u.HSVA[3] > .5) {
ps_set_pen_style(job);
ps_set_color(job, &(job->obj->pencolor));
gvputs(job, "newpath ");
gvprintpointf(job, A[0]);
gvputs(job, " moveto\n");
for (size_t j = 1; j < n; j++) {
gvprintpointf(job, A[j]);
gvputs(job, " lineto\n");
}
gvputs(job, "closepath stroke\n");
}
}
static void psgen_polyline(GVJ_t *job, pointf *A, size_t n) {
if (job->obj->pencolor.u.HSVA[3] > .5) {
ps_set_pen_style(job);
ps_set_color(job, &(job->obj->pencolor));
gvputs(job, "newpath ");
gvprintpointf(job, A[0]);
gvputs(job, " moveto\n");
for (size_t j = 1; j < n; j++) {
gvprintpointf(job, A[j]);
gvputs(job, " lineto\n");
}
gvputs(job, "stroke\n");
}
}
static void psgen_comment(GVJ_t * job, char *str)
{
gvputs(job, "% ");
gvputs(job, str);
gvputs(job, "\n");
}
static void psgen_library_shape(GVJ_t *job, char *name, pointf *A, size_t n,
int filled) {
if (filled && job->obj->fillcolor.u.HSVA[3] > .5) {
ps_set_color(job, &(job->obj->fillcolor));
gvputs(job, "[ ");
gvprintpointflist(job, A, n);
gvputs(job, " ");
gvprintpointf(job, A[0]);
gvprintf(job, " ] %" PRISIZE_T " true %s\n", n, name);
}
if (job->obj->pencolor.u.HSVA[3] > .5) {
ps_set_pen_style(job);
ps_set_color(job, &(job->obj->pencolor));
gvputs(job, "[ ");
gvprintpointflist(job, A, n);
gvputs(job, " ");
gvprintpointf(job, A[0]);
gvprintf(job, " ] %" PRISIZE_T " false %s\n", n, name);
}
}
static gvrender_engine_t psgen_engine = {
psgen_begin_job,
psgen_end_job,
psgen_begin_graph,
0, /* psgen_end_graph */
psgen_begin_layer,
0, /* psgen_end_layer */
psgen_begin_page,
psgen_end_page,
psgen_begin_cluster,
psgen_end_cluster,
0, /* psgen_begin_nodes */
0, /* psgen_end_nodes */
0, /* psgen_begin_edges */
0, /* psgen_end_edges */
psgen_begin_node,
psgen_end_node,
psgen_begin_edge,
psgen_end_edge,
psgen_begin_anchor,
0, /* psgen_end_anchor */
0, /* psgen_begin_label */
0, /* psgen_end_label */
psgen_textspan,
0, /* psgen_resolve_color */
psgen_ellipse,
psgen_polygon,
psgen_bezier,
psgen_polyline,
psgen_comment,
psgen_library_shape,
};
static gvrender_features_t render_features_ps = {
GVRENDER_DOES_TRANSFORM
| GVRENDER_DOES_MAPS
| GVRENDER_NO_WHITE_BG
| GVRENDER_DOES_MAP_RECTANGLE,
4., /* default pad - graph units */
NULL, /* knowncolors */
0, /* sizeof knowncolors */
HSVA_DOUBLE, /* color_type */
};
static gvdevice_features_t device_features_ps = {
GVDEVICE_DOES_PAGES
| GVDEVICE_DOES_LAYERS, /* flags */
{36.,36.}, /* default margin - points */
{612.,792.}, /* default page width, height - points */
{72.,72.}, /* default dpi */
};
static gvdevice_features_t device_features_eps = {
0, /* flags */
{36.,36.}, /* default margin - points */
{612.,792.}, /* default page width, height - points */
{72.,72.}, /* default dpi */
};
gvplugin_installed_t gvrender_ps_types[] = {
{FORMAT_PS, "ps", 1, &psgen_engine, &render_features_ps},
{0, NULL, 0, NULL, NULL}
};
gvplugin_installed_t gvdevice_ps_types[] = {
{FORMAT_PS, "ps:ps", 1, NULL, &device_features_ps},
{FORMAT_PS2, "ps2:ps", 1, NULL, &device_features_ps},
{FORMAT_EPS, "eps:ps", 1, NULL, &device_features_eps},
{0, NULL, 0, NULL, NULL}
};
|