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
|
/*************************************************************************
* 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 <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <common/macros.h>
#include <common/const.h>
#include <gvc/gvplugin_render.h>
#include <gvc/gvplugin_device.h>
#include <gvc/gvio.h>
#include <common/utils.h>
#include <common/color.h>
#include <util/agxbuf.h>
#include <util/prisize_t.h>
#include <util/streq.h>
#include <util/unreachable.h>
/* Number of points to split splines into */
#define BEZIERSUBDIVISION 6
enum { FORMAT_FIG, };
static int Depth;
static void figptarray(GVJ_t *job, pointf *A, size_t n, int close) {
for (size_t i = 0; i < n; i++) {
gvprintf(job, " %.0f %.0f", A[i].x, A[i].y);
}
if (close) {
gvprintf(job, " %.0f %.0f", A[0].x, A[0].y);
}
gvputs(job, "\n");
}
static int figColorResolve(bool *new, unsigned char r, unsigned char g,
unsigned char b)
{
#define maxColors 512
static int top = 0;
static short red[maxColors], green[maxColors], blue[maxColors];
int c;
int ct = -1;
long rd, gd, bd, dist;
long mindist = 3 * 255 * 255; /* init to max poss dist */
*new = false; // in case it is not a new color
for (c = 0; c < top; c++) {
rd = (long) (red[c] - r);
gd = (long) (green[c] - g);
bd = (long) (blue[c] - b);
dist = rd * rd + gd * gd + bd * bd;
if (dist < mindist) {
if (dist == 0)
return c; /* Return exact match color */
mindist = dist;
ct = c;
}
}
/* no exact match. We now know closest, but first try to allocate exact */
if (top == maxColors)
return ct; /* Return closest available color */
++top;
red[c] = r;
green[c] = g;
blue[c] = b;
*new = true; // flag new color
return c; /* Return newly allocated color */
}
/* this table is in xfig color index order */
static char *figcolor[] = {"black", "blue", "green", "cyan", "red",
"magenta", "yellow", "white", NULL};
static void fig_resolve_color(GVJ_t *job, gvcolor_t * color)
{
int object_code = 0; /* always 0 for color */
int i;
switch (color->type) {
case COLOR_STRING:
for (i = 0; figcolor[i]; i++) {
if (streq(figcolor[i], color->u.string)) {
color->u.index = i;
break;
}
}
break;
case RGBA_BYTE: {
bool new;
i = 32 + figColorResolve(&new,
color->u.rgba[0],
color->u.rgba[1],
color->u.rgba[2]);
if (new)
gvprintf(job, "%d %d #%02x%02x%02x\n",
object_code, i,
color->u.rgba[0],
color->u.rgba[1],
color->u.rgba[2]);
color->u.index = i;
break;
}
default:
UNREACHABLE(); // internal error
}
color->type = COLOR_INDEX;
}
static void fig_line_style(obj_state_t *obj, int *line_style, double *style_val)
{
switch (obj->pen) {
case PEN_DASHED:
*line_style = 1;
*style_val = 10.;
break;
case PEN_DOTTED:
*line_style = 2;
*style_val = 10.;
break;
case PEN_SOLID:
default:
*line_style = 0;
*style_val = 0.;
break;
}
}
static void fig_comment(GVJ_t *job, char *str)
{
gvprintf(job, "# %s\n", str);
}
static void fig_begin_graph(GVJ_t * job)
{
obj_state_t *obj = job->obj;
gvputs(job, "#FIG 3.2\n");
gvprintf(job, "# Generated by %s version %s (%s)\n",
job->common->info[0], job->common->info[1], job->common->info[2]);
gvprintf(job, "# Title: %s\n", agnameof(obj->u.g));
gvprintf(job, "# Pages: %d\n", job->pagesArraySize.x * job->pagesArraySize.y);
gvputs(job, "Portrait\n"); /* orientation */
gvputs(job, "Center\n"); /* justification */
gvputs(job, "Inches\n"); /* units */
gvputs(job, "Letter\n"); /* papersize */
gvputs(job, "100.00\n"); /* magnification % */
gvputs(job, "Single\n"); /* multiple-page */
gvputs(job, "-2\n"); /* transparent color (none) */
gvputs(job, "1200"); /* resolution */
gvputs(job, " 2\n"); /* coordinate system (upper left) */
}
static void fig_end_graph(GVJ_t * job)
{
gvputs(job, "# end of FIG file\n");
}
static void fig_begin_page(GVJ_t * job)
{
(void)job;
Depth = 2;
}
static void fig_begin_node(GVJ_t * job)
{
(void)job;
Depth = 1;
}
static void fig_end_node(GVJ_t * job)
{
(void)job;
Depth = 2;
}
static void fig_begin_edge(GVJ_t * job)
{
(void)job;
Depth = 0;
}
static void fig_end_edge(GVJ_t * job)
{
(void)job;
Depth = 2;
}
static void fig_textspan(GVJ_t * job, pointf p, textspan_t * span)
{
obj_state_t *obj = job->obj;
PostscriptAlias *pA;
int object_code = 4; /* always 4 for text */
int sub_type = 0; /* text justification */
int color = obj->pencolor.u.index;
int depth = Depth;
int pen_style = 0; /* not used */
int font = -1; /* init to xfig's default font */
double font_size = span->font->size * job->zoom;
double angle = job->rotation ? (M_PI / 2.0) : 0.0;
int font_flags = 6; /* PostScript font + Special text */
/* Special text indicates that latex markup may exist
* in the output - but note that dot knows nothing about latex,
* so the node sizes may be wrong.
*/
double height = font_size;
const size_t span_length = strlen(span->str);
const double length = 2.0 * font_size / 3.0 * (double)span_length / 2.0;
pA = span->font->postscript_alias;
if (pA) /* if it is a standard postscript font */
font = pA->xfig_code;
switch (span->just) {
case 'l':
sub_type = 0;
break;
case 'r':
sub_type = 2;
break;
default:
case 'n':
sub_type = 1;
break;
}
/* object_code sub_type color depth pen_style font
4 1 0 1 0 0
font_size angle font_flags height length ROUND(p.x) ROUND(p.y),
14.0 0.0000 6 14.0 51.3 1237 570
$A \\in M_0$\001
*/
gvprintf(job,
"%d %d %d %d %d %d %.1f %.4f %d %.1f %.1f %.0f %.0f ",
object_code, sub_type, color, depth, pen_style, font,
font_size, angle, font_flags, height, length, round(p.x),
round(p.y - 72.0));
gvputs_nonascii(job, span->str);
gvputs(job, "\\001\n");
}
static void fig_ellipse(GVJ_t * job, pointf * A, int filled)
{
obj_state_t *obj = job->obj;
int object_code = 1; /* always 1 for ellipse */
int sub_type = 1; /* ellipse defined by radii */
int line_style; /* solid, dotted, dashed */
double thickness = round(obj->penwidth);
int pen_color = obj->pencolor.u.index;
int fill_color = obj->fillcolor.u.index;
int depth = Depth;
int pen_style = 0; /* not used */
int area_fill = filled ? 20 : -1;
double style_val;
int direction = 0;
double angle = 0.0;
double center_x, center_y;
fig_line_style(obj, &line_style, &style_val);
const double start_x = center_x = round(A[0].x);
const double start_y = center_y = round(A[0].y);
const double radius_x = round(A[1].x - A[0].x);
const double radius_y = round(A[1].y - A[0].y);
const double end_x = round(A[1].x);
const double end_y = round(A[1].y);
gvprintf(job,
"%d %d %d %.0f %d %d %d %d %d %.3f %d %.4f %.0f %.0f %.0f %.0f "
"%.0f %.0f %.0f %.0f\n",
object_code, sub_type, line_style, thickness, pen_color,
fill_color, depth, pen_style, area_fill, style_val, direction,
angle, center_x, center_y, radius_x, radius_y, start_x,
start_y, end_x, end_y);
}
static void fig_bezier(GVJ_t *job, pointf *A, size_t n, int filled) {
obj_state_t *obj = job->obj;
int object_code = 3; /* always 3 for spline */
int sub_type;
int line_style; /* solid, dotted, dashed */
double thickness = round(obj->penwidth);
int pen_color = obj->pencolor.u.index;
int fill_color = obj->fillcolor.u.index;
int depth = Depth;
int pen_style = 0; /* not used */
int area_fill;
double style_val;
int cap_style = 0;
int forward_arrow = 0;
int backward_arrow = 0;
pointf pf, V[4];
int step;
int count = 0;
agxbuf buf = {0};
assert (n >= 4);
fig_line_style(obj, &line_style, &style_val);
if (filled) {
sub_type = 5; /* closed X-spline */
area_fill = 20; /* fully saturated color */
fill_color = job->obj->fillcolor.u.index;
}
else {
sub_type = 4; /* opened X-spline */
area_fill = -1;
fill_color = 0;
}
V[3].x = A[0].x;
V[3].y = A[0].y;
/* Write first point in line */
count++;
agxbprint(&buf, " %.0f %.0f", A[0].x, A[0].y);
/* write subsequent points */
for (size_t i = 0; i + 3 < n; i += 3) {
V[0] = V[3];
for (size_t j = 1; j <= 3; j++) {
V[j].x = A[i + j].x;
V[j].y = A[i + j].y;
}
for (step = 1; step <= BEZIERSUBDIVISION; step++) {
count++;
pf = Bezier(V, (double)step / BEZIERSUBDIVISION, NULL, NULL);
agxbprint(&buf, " %.0f %.0f", pf.x, pf.y);
}
}
gvprintf(job, "%d %d %d %.0f %d %d %d %d %d %.1f %d %d %d %d\n",
object_code,
sub_type,
line_style,
thickness,
pen_color,
fill_color,
depth,
pen_style,
area_fill,
style_val, cap_style, forward_arrow, backward_arrow, count);
gvprintf(job, " %s\n", agxbuse(&buf)); /* print points */
agxbfree(&buf);
for (int i = 0; i < count; i++) {
gvprintf(job, " %d", i % (count - 1) ? 1 : 0); /* -1 on all */
}
gvputs(job, "\n");
}
static void fig_polygon(GVJ_t *job, pointf *A, size_t n, int filled) {
obj_state_t *obj = job->obj;
int object_code = 2; /* always 2 for polyline */
int sub_type = 3; /* always 3 for polygon */
int line_style; /* solid, dotted, dashed */
double thickness = round(obj->penwidth);
int pen_color = obj->pencolor.u.index;
int fill_color = obj->fillcolor.u.index;
int depth = Depth;
int pen_style = 0; /* not used */
int area_fill = filled ? 20 : -1;
double style_val;
int join_style = 0;
int cap_style = 0;
int radius = 0;
int forward_arrow = 0;
int backward_arrow = 0;
const size_t npoints = n + 1;
fig_line_style(obj, &line_style, &style_val);
gvprintf(job,
"%d %d %d %.0f %d %d %d %d %d %.1f %d %d %d %d %d %" PRISIZE_T "\n",
object_code, sub_type, line_style, thickness, pen_color,
fill_color, depth, pen_style, area_fill, style_val, join_style,
cap_style, radius, forward_arrow, backward_arrow, npoints);
figptarray(job, A, n, 1); // closed shape
}
static void fig_polyline(GVJ_t *job, pointf *A, size_t n) {
obj_state_t *obj = job->obj;
int object_code = 2; /* always 2 for polyline */
int sub_type = 1; /* always 1 for polyline */
int line_style; /* solid, dotted, dashed */
double thickness = round(obj->penwidth);
int pen_color = obj->pencolor.u.index;
int fill_color = 0;
int depth = Depth;
int pen_style = 0; /* not used */
int area_fill = 0;
double style_val;
int join_style = 0;
int cap_style = 0;
int radius = 0;
int forward_arrow = 0;
int backward_arrow = 0;
const size_t npoints = n;
fig_line_style(obj, &line_style, &style_val);
gvprintf(job,
"%d %d %d %.0f %d %d %d %d %d %.1f %d %d %d %d %d %" PRISIZE_T "\n",
object_code, sub_type, line_style, thickness, pen_color,
fill_color, depth, pen_style, area_fill, style_val, join_style,
cap_style, radius, forward_arrow, backward_arrow, npoints);
figptarray(job, A, n, 0); // open shape
}
static gvrender_engine_t fig_engine = {
0, /* fig_begin_job */
0, /* fig_end_job */
fig_begin_graph,
fig_end_graph,
0, /* fig_begin_layer */
0, /* fig_end_layer */
fig_begin_page,
0, /* fig_end_page */
0, /* fig_begin_cluster */
0, /* fig_end_cluster */
0, /* fig_begin_nodes */
0, /* fig_end_nodes */
0, /* fig_begin_edges */
0, /* fig_end_edges */
fig_begin_node,
fig_end_node,
fig_begin_edge,
fig_end_edge,
0, /* fig_begin_anchor */
0, /* fig_end_anchor */
0, /* fig_begin_label */
0, /* fig_end_label */
fig_textspan,
fig_resolve_color,
fig_ellipse,
fig_polygon,
fig_bezier,
fig_polyline,
fig_comment,
0, /* fig_library_shape */
};
/* NB. List must be LANG_C sorted */
static char *fig_knowncolors[] = {
"black", "blue", "cyan", "green", "magenta", "red", "white", "yellow",
};
static gvrender_features_t render_features_fig = {
EMIT_COLORS
| GVRENDER_Y_GOES_DOWN, /* flags */
4., /* default pad - graph units */
fig_knowncolors, /* knowncolors */
sizeof(fig_knowncolors) / sizeof(char *), /* sizeof knowncolors */
RGBA_BYTE, /* color_type */
};
static gvdevice_features_t device_features_fig = {
EMIT_COLORS
| GVRENDER_Y_GOES_DOWN, /* flags */
{0.,0.}, /* default margin - points */
{0.,0.}, /* default page width, height - points */
{1440.,1440.}, /* default dpi */
/* FIXME - this default dpi is a very strange number!!!
* It was picked to make .png usershapes the right size on my screen.
* It happens to be 1.2 * 1200, but I can't explain the 1.2.
* (I was expecting 1.3333 which is 96/72, but thats too big.)
* Also 1200 is hardcoded in fig_begin_graph() instead of using job->dpi
*/
/* It may be TWIPS, i.e. 20 * POINT_PER_INCH
* but that doesn't explain what the 1200 is? */
};
gvplugin_installed_t gvrender_fig_types[] = {
{FORMAT_FIG, "fig", 1, &fig_engine, &render_features_fig},
{0, NULL, 0, NULL, NULL}
};
gvplugin_installed_t gvdevice_fig_types[] = {
{FORMAT_FIG, "fig:fig", 1, NULL, &device_features_fig},
{0, NULL, 0, NULL, NULL}
};
|