| 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 <assert.h> |
| 13 |
#include <common/globals.h> |
| 14 |
#include <common/render.h> |
| 15 |
#include <common/types.h> |
| 16 |
#include <common/usershape.h> |
| 17 |
#include <common/utils.h> |
| 18 |
#include <errno.h> |
| 19 |
#include <gvc/gvcint.h> |
| 20 |
#include <gvc/gvcproc.h> |
| 21 |
#include <gvc/gvplugin.h> |
| 22 |
#include <gvc/gvplugin_loadimage.h> |
| 23 |
#include <limits.h> |
| 24 |
#include <math.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <util/agxbuf.h> |
| 30 |
#include <util/alloc.h> |
| 31 |
#include <util/gv_ctype.h> |
| 32 |
#include <util/gv_fopen.h> |
| 33 |
#include <util/optional.h> |
| 34 |
#include <util/streq.h> |
| 35 |
#include <util/strview.h> |
| 36 |
|
| 37 |
static Dict_t *ImageDict; |
| 38 |
|
| 39 |
typedef struct { |
| 40 |
char *template; |
| 41 |
size_t size; |
| 42 |
imagetype_t type; |
| 43 |
char *stringtype; |
| 44 |
} knowntype_t; |
| 45 |
|
| 46 |
#define HDRLEN 20 |
| 47 |
|
| 48 |
#define PNG_MAGIC "\x89PNG\x0D\x0A\x1A\x0A" |
| 49 |
#define PS_MAGIC "%!PS-Adobe-" |
| 50 |
#define BMP_MAGIC "BM" |
| 51 |
#define GIF_MAGIC "GIF8" |
| 52 |
#define JPEG_MAGIC "\xFF\xD8\xFF" |
| 53 |
#define PDF_MAGIC "%PDF-" |
| 54 |
#define EPS_MAGIC "\xC5\xD0\xD3\xC6" |
| 55 |
#define XML_MAGIC "<?xml" |
| 56 |
#define SVG_MAGIC "<svg" |
| 57 |
#define RIFF_MAGIC "RIFF" |
| 58 |
#define WEBP_MAGIC "WEBP" |
| 59 |
#define ICO_MAGIC "\x00\x00\x01\x00" |
| 60 |
|
| 61 |
static knowntype_t knowntypes[] = { |
| 62 |
{ |
| 63 |
PNG_MAGIC, |
| 64 |
sizeof(PNG_MAGIC) - 1, |
| 65 |
FT_PNG, |
| 66 |
"png", |
| 67 |
}, |
| 68 |
{ |
| 69 |
PS_MAGIC, |
| 70 |
sizeof(PS_MAGIC) - 1, |
| 71 |
FT_PS, |
| 72 |
"ps", |
| 73 |
}, |
| 74 |
{ |
| 75 |
BMP_MAGIC, |
| 76 |
sizeof(BMP_MAGIC) - 1, |
| 77 |
FT_BMP, |
| 78 |
"bmp", |
| 79 |
}, |
| 80 |
{ |
| 81 |
GIF_MAGIC, |
| 82 |
sizeof(GIF_MAGIC) - 1, |
| 83 |
FT_GIF, |
| 84 |
"gif", |
| 85 |
}, |
| 86 |
{ |
| 87 |
JPEG_MAGIC, |
| 88 |
sizeof(JPEG_MAGIC) - 1, |
| 89 |
FT_JPEG, |
| 90 |
"jpeg", |
| 91 |
}, |
| 92 |
{ |
| 93 |
PDF_MAGIC, |
| 94 |
sizeof(PDF_MAGIC) - 1, |
| 95 |
FT_PDF, |
| 96 |
"pdf", |
| 97 |
}, |
| 98 |
{ |
| 99 |
EPS_MAGIC, |
| 100 |
sizeof(EPS_MAGIC) - 1, |
| 101 |
FT_EPS, |
| 102 |
"eps", |
| 103 |
}, |
| 104 |
{ |
| 105 |
XML_MAGIC, |
| 106 |
sizeof(XML_MAGIC) - 1, |
| 107 |
FT_XML, |
| 108 |
"xml", |
| 109 |
}, |
| 110 |
{ |
| 111 |
RIFF_MAGIC, |
| 112 |
sizeof(RIFF_MAGIC) - 1, |
| 113 |
FT_RIFF, |
| 114 |
"riff", |
| 115 |
}, |
| 116 |
{ |
| 117 |
ICO_MAGIC, |
| 118 |
sizeof(ICO_MAGIC) - 1, |
| 119 |
FT_ICO, |
| 120 |
"ico", |
| 121 |
}, |
| 122 |
}; |
| 123 |
|
| 124 |
static imagetype_t imagetype(usershape_t *us) { |
| 125 |
char header[HDRLEN] = {0}; |
| 126 |
|
| 127 |
if (us->f && fread(header, 1, HDRLEN, us->f) == HDRLEN) { |
| 128 |
for (size_t i = 0; i < sizeof(knowntypes) / sizeof(knowntype_t); i++) { |
| 129 |
if (!memcmp(header, knowntypes[i].template, knowntypes[i].size)) { |
| 130 |
us->stringtype = knowntypes[i].stringtype; |
| 131 |
us->type = knowntypes[i].type; |
| 132 |
if (us->type == FT_XML) { |
| 133 |
// if we did not see the closing of the XML declaration, scan for it |
| 134 |
if (memchr(header, '>', HDRLEN) == NULL) { |
| 135 |
while (true) { |
| 136 |
int c = fgetc(us->f); |
| 137 |
if (c == EOF) { |
| 138 |
return us->type; |
| 139 |
} else if (c == '>') { |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
/* check for SVG in case of XML */ |
| 145 |
char tag[sizeof(SVG_MAGIC) - 1] = {0}; |
| 146 |
if (fread(tag, 1, sizeof(tag), us->f) != sizeof(tag)) { |
| 147 |
return us->type; |
| 148 |
} |
| 149 |
while (true) { |
| 150 |
if (memcmp(tag, SVG_MAGIC, sizeof(SVG_MAGIC) - 1) == 0) { |
| 151 |
us->stringtype = "svg"; |
| 152 |
return (us->type = FT_SVG); |
| 153 |
} |
| 154 |
int c = fgetc(us->f); |
| 155 |
if (c == EOF) { |
| 156 |
return us->type; |
| 157 |
} |
| 158 |
memmove(&tag[0], &tag[1], sizeof(tag) - 1); |
| 159 |
tag[sizeof(tag) - 1] = (char)c; |
| 160 |
} |
| 161 |
} else if (us->type == FT_RIFF) { |
| 162 |
/* check for WEBP in case of RIFF */ |
| 163 |
if (!memcmp(header + 8, WEBP_MAGIC, sizeof(WEBP_MAGIC) - 1)) { |
| 164 |
us->stringtype = "webp"; |
| 165 |
return (us->type = FT_WEBP); |
| 166 |
} |
| 167 |
} |
| 168 |
return us->type; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
us->stringtype = "(lib)"; |
| 174 |
us->type = FT_NULL; |
| 175 |
|
| 176 |
return FT_NULL; |
| 177 |
} |
| 178 |
|
| 179 |
static bool get_int_lsb_first(FILE *f, size_t sz, int *val) { |
| 180 |
unsigned value = 0; |
| 181 |
for (size_t i = 0; i < sz; i++) { |
| 182 |
const int ch = fgetc(f); |
| 183 |
if (feof(f)) |
| 184 |
return false; |
| 185 |
value |= (unsigned)ch << 8 * i; |
| 186 |
} |
| 187 |
if (value > INT_MAX) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
*val = (int)value; |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
static bool get_int_msb_first(FILE *f, size_t sz, int *val) { |
| 195 |
unsigned value = 0; |
| 196 |
for (size_t i = 0; i < sz; i++) { |
| 197 |
const int ch = fgetc(f); |
| 198 |
if (feof(f)) |
| 199 |
return false; |
| 200 |
value <<= 8; |
| 201 |
value |= (unsigned)ch; |
| 202 |
} |
| 203 |
if (value > INT_MAX) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
*val = (int)value; |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
static double svg_units_convert(double n, char *u) { |
| 211 |
if (streq(u, "in")) |
| 212 |
return round(n * POINTS_PER_INCH); |
| 213 |
if (streq(u, "px")) |
| 214 |
return round(n * POINTS_PER_INCH / 96); |
| 215 |
if (streq(u, "pc")) |
| 216 |
return round(n * POINTS_PER_INCH / 6); |
| 217 |
if (streq(u, "pt") || streq(u, "\"")) /* ugly!! - if there are no inits then |
| 218 |
the %2s get the trailing '"' */ |
| 219 |
return round(n); |
| 220 |
if (streq(u, "cm")) |
| 221 |
return round(n * POINTS_PER_CM); |
| 222 |
if (streq(u, "mm")) |
| 223 |
return round(n * POINTS_PER_MM); |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
typedef struct { |
| 228 |
strview_t key; |
| 229 |
strview_t value; |
| 230 |
} match_t; |
| 231 |
|
| 232 |
static int find_attribute(const char *s, match_t *result) { |
| 233 |
|
| 234 |
// look for an attribute string matching ([a-z][a-zA-Z]*)="([^"]*)" |
| 235 |
for (size_t i = 0; s[i] != '\0';) { |
| 236 |
if (gv_islower(s[i])) { |
| 237 |
result->key.data = &s[i]; |
| 238 |
result->key.size = 1; |
| 239 |
++i; |
| 240 |
while (gv_isalpha(s[i])) { |
| 241 |
++i; |
| 242 |
++result->key.size; |
| 243 |
} |
| 244 |
if (s[i] == '=' && s[i + 1] == '"') { |
| 245 |
i += 2; |
| 246 |
result->value.data = &s[i]; |
| 247 |
result->value.size = 0; |
| 248 |
while (s[i] != '"' && s[i] != '\0') { |
| 249 |
++i; |
| 250 |
++result->value.size; |
| 251 |
} |
| 252 |
if (s[i] == '"') { |
| 253 |
// found a valid attribute |
| 254 |
return 0; |
| 255 |
} |
| 256 |
} |
| 257 |
} else { |
| 258 |
++i; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// no attribute found |
| 263 |
return -1; |
| 264 |
} |
| 265 |
|
| 266 |
static void svg_size(usershape_t *us) { |
| 267 |
double n; |
| 268 |
char u[3]; |
| 269 |
agxbuf line = {0}; |
| 270 |
bool eof = false; |
| 271 |
|
| 272 |
// authoritative constraints we learned from `height` and `width` |
| 273 |
optional_double_t hard_height = {0}; |
| 274 |
optional_double_t hard_width = {0}; |
| 275 |
|
| 276 |
// fallback constraints we learned from `viewBox` |
| 277 |
optional_double_t soft_height = {0}; |
| 278 |
optional_double_t soft_width = {0}; |
| 279 |
|
| 280 |
rewind(us->f); |
| 281 |
while (!eof && (!hard_width.has_value || !hard_height.has_value)) { |
| 282 |
// read next line |
| 283 |
while (true) { |
| 284 |
int c = fgetc(us->f); |
| 285 |
if (c == EOF) { |
| 286 |
eof = true; |
| 287 |
break; |
| 288 |
} else if (c == '\n') { |
| 289 |
break; |
| 290 |
} |
| 291 |
agxbputc(&line, (char)c); |
| 292 |
} |
| 293 |
|
| 294 |
const char *re_string = agxbuse(&line); |
| 295 |
match_t match; |
| 296 |
while (find_attribute(re_string, &match) == 0) { |
| 297 |
re_string = match.value.data + match.value.size + 1; |
| 298 |
|
| 299 |
if (strview_str_eq(match.key, "width")) { |
| 300 |
char *value = strview_str(match.value); |
| 301 |
if (sscanf(value, "%lf%2s", &n, u) == 2) { |
| 302 |
optional_double_set(&hard_width, svg_units_convert(n, u)); |
| 303 |
} else if (sscanf(value, "%lf", &n) == 1) { |
| 304 |
optional_double_set(&hard_width, svg_units_convert(n, "pt")); |
| 305 |
} |
| 306 |
free(value); |
| 307 |
if (hard_height.has_value) |
| 308 |
break; |
| 309 |
} else if (strview_str_eq(match.key, "height")) { |
| 310 |
char *value = strview_str(match.value); |
| 311 |
if (sscanf(value, "%lf%2s", &n, u) == 2) { |
| 312 |
optional_double_set(&hard_height, svg_units_convert(n, u)); |
| 313 |
} else if (sscanf(value, "%lf", &n) == 1) { |
| 314 |
optional_double_set(&hard_height, svg_units_convert(n, "pt")); |
| 315 |
} |
| 316 |
free(value); |
| 317 |
if (hard_width.has_value) |
| 318 |
break; |
| 319 |
} else if (strview_str_eq(match.key, "viewBox")) { |
| 320 |
char *value = strview_str(match.value); |
| 321 |
double w, h; |
| 322 |
if (sscanf(value, "%*f %*f %lf %lf", &w, &h) == 2) { |
| 323 |
optional_double_set(&soft_width, w); |
| 324 |
optional_double_set(&soft_height, h); |
| 325 |
} |
| 326 |
free(value); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
// if we have reached the end of a line and have seen `viewBox` but not |
| 331 |
// `height` and/or `width`, let `viewBox` determine the dimensions |
| 332 |
if (soft_height.has_value && soft_width.has_value) { |
| 333 |
if (!hard_height.has_value) { |
| 334 |
optional_double_set(&hard_height, soft_height.value); |
| 335 |
} |
| 336 |
if (!hard_width.has_value) { |
| 337 |
optional_double_set(&hard_width, soft_width.value); |
| 338 |
} |
| 339 |
break; |
| 340 |
} |
| 341 |
} |
| 342 |
us->dpi = 0; |
| 343 |
const double h = optional_double_value_or(hard_height, 0); |
| 344 |
const double w = optional_double_value_or(hard_width, 0); |
| 345 |
assert(w >= 0 && w <= INT_MAX); |
| 346 |
us->w = (int)w; |
| 347 |
assert(h >= 0 && h <= INT_MAX); |
| 348 |
us->h = (int)h; |
| 349 |
agxbfree(&line); |
| 350 |
} |
| 351 |
|
| 352 |
static void png_size(usershape_t *us) { |
| 353 |
int w, h; |
| 354 |
|
| 355 |
us->dpi = 0; |
| 356 |
fseek(us->f, 16, SEEK_SET); |
| 357 |
if (get_int_msb_first(us->f, 4, &w) && get_int_msb_first(us->f, 4, &h)) { |
| 358 |
us->w = w; |
| 359 |
us->h = h; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
static void ico_size(usershape_t *us) { |
| 364 |
int w, h; |
| 365 |
|
| 366 |
us->dpi = 0; |
| 367 |
fseek(us->f, 6, SEEK_SET); |
| 368 |
if (get_int_msb_first(us->f, 1, &w) && get_int_msb_first(us->f, 1, &h)) { |
| 369 |
us->w = w; |
| 370 |
us->h = h; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
static void webp_size(usershape_t *us) { |
| 375 |
int w, h; |
| 376 |
|
| 377 |
us->dpi = 0; |
| 378 |
fseek(us->f, 15, SEEK_SET); |
| 379 |
if (fgetc(us->f) == 'X') { // VP8X |
| 380 |
fseek(us->f, 24, SEEK_SET); |
| 381 |
if (get_int_lsb_first(us->f, 4, &w) && get_int_lsb_first(us->f, 4, &h)) { |
| 382 |
us->w = w; |
| 383 |
us->h = h; |
| 384 |
} |
| 385 |
} else { // VP8 |
| 386 |
fseek(us->f, 26, SEEK_SET); |
| 387 |
if (get_int_lsb_first(us->f, 2, &w) && get_int_lsb_first(us->f, 2, &h)) { |
| 388 |
us->w = w; |
| 389 |
us->h = h; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
static void gif_size(usershape_t *us) { |
| 395 |
int w, h; |
| 396 |
|
| 397 |
us->dpi = 0; |
| 398 |
fseek(us->f, 6, SEEK_SET); |
| 399 |
if (get_int_lsb_first(us->f, 2, &w) && get_int_lsb_first(us->f, 2, &h)) { |
| 400 |
us->w = w; |
| 401 |
us->h = h; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
static void bmp_size(usershape_t *us) { |
| 406 |
int size_x_msw, size_x_lsw, size_y_msw, size_y_lsw; |
| 407 |
|
| 408 |
us->dpi = 0; |
| 409 |
fseek(us->f, 16, SEEK_SET); |
| 410 |
if (get_int_lsb_first(us->f, 2, &size_x_msw) && |
| 411 |
get_int_lsb_first(us->f, 2, &size_x_lsw) && |
| 412 |
get_int_lsb_first(us->f, 2, &size_y_msw) && |
| 413 |
get_int_lsb_first(us->f, 2, &size_y_lsw)) { |
| 414 |
us->w = size_x_msw << 16 | size_x_lsw; |
| 415 |
us->h = size_y_msw << 16 | size_y_lsw; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
static void jpeg_size(usershape_t *us) { |
| 420 |
int marker, length, size_x, size_y; |
| 421 |
|
| 422 |
/* These are the markers that follow 0xff in the file. |
| 423 |
* Other markers implicitly have a 2-byte length field that follows. |
| 424 |
*/ |
| 425 |
static const unsigned char standalone_markers[] = { |
| 426 |
0x01, /* Temporary */ |
| 427 |
0xd0, 0xd1, 0xd2, 0xd3, /* Reset */ |
| 428 |
0xd4, 0xd5, 0xd6, 0xd7, 0xd8, /* Start of image */ |
| 429 |
0xd9, /* End of image */ |
| 430 |
}; |
| 431 |
|
| 432 |
us->dpi = 0; |
| 433 |
rewind(us->f); |
| 434 |
while (true) { |
| 435 |
/* Now we must be at a 0xff or at a series of 0xff's. |
| 436 |
* If that is not the case, or if we're at EOF, then there's |
| 437 |
* a parsing error. |
| 438 |
*/ |
| 439 |
if (!get_int_msb_first(us->f, 1, &marker)) { |
| 440 |
agwarningf("Parsing of \"%s\" failed\n", us->name); |
| 441 |
return; |
| 442 |
} |
| 443 |
|
| 444 |
if (marker == 0xff) |
| 445 |
continue; |
| 446 |
|
| 447 |
/* Ok.. marker now read. If it is not a stand-alone marker, |
| 448 |
* then continue. If it's a Start Of Frame (0xc?), then we're there. |
| 449 |
* If it's another marker with a length field, then skip ahead |
| 450 |
* over that length field. |
| 451 |
*/ |
| 452 |
|
| 453 |
/* A stand-alone... */ |
| 454 |
if (memchr(standalone_markers, marker, sizeof(standalone_markers))) |
| 455 |
continue; |
| 456 |
|
| 457 |
/* Incase of a 0xc0 marker: */ |
| 458 |
if (marker == 0xc0) { |
| 459 |
/* Skip length and 2 lengths. */ |
| 460 |
if (fseek(us->f, 3, SEEK_CUR) == 0 && |
| 461 |
get_int_msb_first(us->f, 2, &size_y) && |
| 462 |
get_int_msb_first(us->f, 2, &size_x)) { |
| 463 |
|
| 464 |
/* Store length. */ |
| 465 |
us->h = size_y; |
| 466 |
us->w = size_x; |
| 467 |
} |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
/* Incase of a 0xc2 marker: */ |
| 472 |
if (marker == 0xc2) { |
| 473 |
/* Skip length and one more byte */ |
| 474 |
if (fseek(us->f, 3, SEEK_CUR) != 0) |
| 475 |
return; |
| 476 |
|
| 477 |
/* Get length and store. */ |
| 478 |
if (get_int_msb_first(us->f, 2, &size_y) && |
| 479 |
get_int_msb_first(us->f, 2, &size_x)) { |
| 480 |
us->h = size_y; |
| 481 |
us->w = size_x; |
| 482 |
} |
| 483 |
return; |
| 484 |
} |
| 485 |
|
| 486 |
/* Any other marker is assumed to be followed by 2 bytes length. */ |
| 487 |
if (!get_int_msb_first(us->f, 2, &length)) |
| 488 |
return; |
| 489 |
|
| 490 |
fseek(us->f, length - 2, SEEK_CUR); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
static void ps_size(usershape_t *us) { |
| 495 |
char line[BUFSIZ]; |
| 496 |
int lx, ly, ux, uy; |
| 497 |
char *linep; |
| 498 |
|
| 499 |
us->dpi = 72; |
| 500 |
rewind(us->f); |
| 501 |
bool saw_bb = false; |
| 502 |
while (fgets(line, sizeof(line), us->f)) { |
| 503 |
/* PostScript accepts \r as EOL, so using fgets () and looking for a |
| 504 |
* bounding box comment at the beginning doesn't work in this case. |
| 505 |
* As a heuristic, we first search for a bounding box comment in line. |
| 506 |
* This obviously fails if not all of the numbers make it into the |
| 507 |
* current buffer. This shouldn't be a problem, as the comment is |
| 508 |
* typically near the beginning, and so should be read within the first |
| 509 |
* BUFSIZ bytes (even on Windows where this is 512). |
| 510 |
*/ |
| 511 |
if (!(linep = strstr(line, "%%BoundingBox:"))) |
| 512 |
continue; |
| 513 |
if (sscanf(linep, "%%%%BoundingBox: %d %d %d %d", &lx, &ly, &ux, &uy) == |
| 514 |
4) { |
| 515 |
saw_bb = true; |
| 516 |
break; |
| 517 |
} |
| 518 |
} |
| 519 |
if (saw_bb) { |
| 520 |
us->x = lx; |
| 521 |
us->y = ly; |
| 522 |
us->w = ux - lx; |
| 523 |
us->h = uy - ly; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
typedef struct { |
| 528 |
FILE *fp; |
| 529 |
agxbuf scratch; |
| 530 |
} stream_t; |
| 531 |
|
| 532 |
static void skipWS(stream_t *str) { |
| 533 |
while (true) { |
| 534 |
const int c = getc(str->fp); |
| 535 |
if (gv_isspace(c)) { |
| 536 |
continue; |
| 537 |
} |
| 538 |
if (c != EOF) { |
| 539 |
(void)ungetc(c, str->fp); |
| 540 |
} |
| 541 |
break; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
static int scanNum(char *tok, double *dp) { |
| 546 |
char *endp; |
| 547 |
double d = strtod(tok, &endp); |
| 548 |
|
| 549 |
if (tok == endp) |
| 550 |
return 1; |
| 551 |
*dp = d; |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
static char *getNum(stream_t *str) { |
| 556 |
skipWS(str); |
| 557 |
while (true) { |
| 558 |
const int c = getc(str->fp); |
| 559 |
if (gv_isdigit(c) || c == '.') { |
| 560 |
agxbputc(&str->scratch, (char)c); |
| 561 |
continue; |
| 562 |
} |
| 563 |
if (c != EOF) { |
| 564 |
(void)ungetc(c, str->fp); |
| 565 |
} |
| 566 |
break; |
| 567 |
} |
| 568 |
return agxbuse(&str->scratch); |
| 569 |
} |
| 570 |
|
| 571 |
static int boxof(stream_t *str, boxf *bp) { |
| 572 |
skipWS(str); |
| 573 |
if (getc(str->fp) != '[') |
| 574 |
return 1; |
| 575 |
char *tok = getNum(str); |
| 576 |
if (scanNum(tok, &bp->LL.x)) |
| 577 |
return 1; |
| 578 |
tok = getNum(str); |
| 579 |
if (scanNum(tok, &bp->LL.y)) |
| 580 |
return 1; |
| 581 |
tok = getNum(str); |
| 582 |
if (scanNum(tok, &bp->UR.x)) |
| 583 |
return 1; |
| 584 |
tok = getNum(str); |
| 585 |
if (scanNum(tok, &bp->UR.y)) |
| 586 |
return 1; |
| 587 |
return 0; |
| 588 |
} |
| 589 |
|
| 590 |
/// scan a file until a string is found |
| 591 |
/// |
| 592 |
/// This is essentially `strstr`, but taking a `FILE *` as the haystack instead |
| 593 |
/// of a `char *`. Note that the position of `f` will be immediately after the |
| 594 |
/// given string if this function returns `true`. |
| 595 |
/// |
| 596 |
/// @param f File to seek |
| 597 |
/// @param needle Substring to look for |
| 598 |
/// @return True if the substring was found |
| 599 |
static bool fstr(FILE *f, const char *needle) { |
| 600 |
assert(f != NULL); |
| 601 |
assert(needle != NULL); |
| 602 |
|
| 603 |
// the algorithm in this function only works if the needle’s characters are |
| 604 |
// distinct |
| 605 |
for (size_t i = 0; needle[i] != '\0'; ++i) { |
| 606 |
for (size_t j = i + 1; needle[j] != '\0'; ++j) { |
| 607 |
assert(needle[i] != needle[j]); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
for (size_t offset = 0;;) { |
| 612 |
if (needle[offset] == '\0') { |
| 613 |
return true; |
| 614 |
} |
| 615 |
const int c = getc(f); |
| 616 |
if (c == EOF) { |
| 617 |
break; |
| 618 |
} |
| 619 |
if (needle[offset] == c) { |
| 620 |
++offset; |
| 621 |
} else if (needle[0] == c) { |
| 622 |
offset = 1; |
| 623 |
} else { |
| 624 |
offset = 0; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
return false; |
| 629 |
} |
| 630 |
|
| 631 |
static int bboxPDF(FILE *fp, boxf *bp) { |
| 632 |
static const char KEY[] = "/MediaBox"; |
| 633 |
if (fstr(fp, KEY)) { |
| 634 |
stream_t str = {.fp = fp}; |
| 635 |
const int rc = boxof(&str, bp); |
| 636 |
agxbfree(&str.scratch); |
| 637 |
return rc; |
| 638 |
} |
| 639 |
|
| 640 |
return 1; |
| 641 |
} |
| 642 |
|
| 643 |
static void pdf_size(usershape_t *us) { |
| 644 |
boxf bb; |
| 645 |
|
| 646 |
us->dpi = 0; |
| 647 |
rewind(us->f); |
| 648 |
if (!bboxPDF(us->f, &bb)) { |
| 649 |
us->x = bb.LL.x; |
| 650 |
us->y = bb.LL.y; |
| 651 |
us->w = bb.UR.x - bb.LL.x; |
| 652 |
us->h = bb.UR.y - bb.LL.y; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
static void usershape_close(void *p) { |
| 657 |
usershape_t *us = p; |
| 658 |
|
| 659 |
if (us->f) |
| 660 |
fclose(us->f); |
| 661 |
if (us->data && us->datafree) |
| 662 |
us->datafree(us); |
| 663 |
free(us); |
| 664 |
} |
| 665 |
|
| 666 |
static Dtdisc_t ImageDictDisc = { |
| 667 |
.key = offsetof(usershape_t, name), |
| 668 |
.size = -1, |
| 669 |
.freef = usershape_close, |
| 670 |
}; |
| 671 |
|
| 672 |
usershape_t *gvusershape_find(const char *name) { |
| 673 |
assert(name); |
| 674 |
assert(name[0]); |
| 675 |
|
| 676 |
if (!ImageDict) |
| 677 |
return NULL; |
| 678 |
|
| 679 |
return dtmatch(ImageDict, name); |
| 680 |
} |
| 681 |
|
| 682 |
#define MAX_USERSHAPE_FILES_OPEN 50 |
| 683 |
bool gvusershape_file_access(usershape_t *us) { |
| 684 |
static int usershape_files_open_cnt; |
| 685 |
const char *fn; |
| 686 |
|
| 687 |
assert(us); |
| 688 |
assert(us->name); |
| 689 |
assert(us->name[0]); |
| 690 |
|
| 691 |
if (us->f) |
| 692 |
rewind(us->f); |
| 693 |
else { |
| 694 |
if (!(fn = safefile(us->name))) { |
| 695 |
agwarningf("Filename \"%s\" is unsafe\n", us->name); |
| 696 |
return false; |
| 697 |
} |
| 698 |
us->f = gv_fopen(fn, "rb"); |
| 699 |
if (us->f == NULL) { |
| 700 |
agwarningf("%s while opening %s\n", strerror(errno), fn); |
| 701 |
return false; |
| 702 |
} |
| 703 |
if (usershape_files_open_cnt >= MAX_USERSHAPE_FILES_OPEN) |
| 704 |
us->nocache = true; |
| 705 |
else |
| 706 |
usershape_files_open_cnt++; |
| 707 |
} |
| 708 |
assert(us->f); |
| 709 |
return true; |
| 710 |
} |
| 711 |
|
| 712 |
void gvusershape_file_release(usershape_t *us) { |
| 713 |
if (us->nocache) { |
| 714 |
if (us->f) { |
| 715 |
fclose(us->f); |
| 716 |
us->f = NULL; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
static void freeUsershape(usershape_t *us) { |
| 722 |
if (us->name) |
| 723 |
agstrfree(0, us->name, false); |
| 724 |
free(us); |
| 725 |
} |
| 726 |
|
| 727 |
static usershape_t *gvusershape_open(const char *name) { |
| 728 |
usershape_t *us; |
| 729 |
|
| 730 |
assert(name); |
| 731 |
|
| 732 |
if (!ImageDict) |
| 733 |
ImageDict = dtopen(&ImageDictDisc, Dttree); |
| 734 |
|
| 735 |
if (!(us = gvusershape_find(name))) { |
| 736 |
us = gv_alloc(sizeof(usershape_t)); |
| 737 |
|
| 738 |
us->name = agstrdup(0, name); |
| 739 |
if (!gvusershape_file_access(us)) { |
| 740 |
freeUsershape(us); |
| 741 |
return NULL; |
| 742 |
} |
| 743 |
|
| 744 |
assert(us->f); |
| 745 |
|
| 746 |
switch (imagetype(us)) { |
| 747 |
case FT_NULL: |
| 748 |
if (!(us->data = find_user_shape(us->name))) { |
| 749 |
agwarningf( |
| 750 |
"\"%s\" was not found as a file or as a shape library member\n", |
| 751 |
us->name); |
| 752 |
freeUsershape(us); |
| 753 |
return NULL; |
| 754 |
} |
| 755 |
break; |
| 756 |
case FT_GIF: |
| 757 |
gif_size(us); |
| 758 |
break; |
| 759 |
case FT_PNG: |
| 760 |
png_size(us); |
| 761 |
break; |
| 762 |
case FT_BMP: |
| 763 |
bmp_size(us); |
| 764 |
break; |
| 765 |
case FT_JPEG: |
| 766 |
jpeg_size(us); |
| 767 |
break; |
| 768 |
case FT_PS: |
| 769 |
ps_size(us); |
| 770 |
break; |
| 771 |
case FT_WEBP: |
| 772 |
webp_size(us); |
| 773 |
break; |
| 774 |
case FT_SVG: |
| 775 |
svg_size(us); |
| 776 |
break; |
| 777 |
case FT_PDF: |
| 778 |
pdf_size(us); |
| 779 |
break; |
| 780 |
case FT_ICO: |
| 781 |
ico_size(us); |
| 782 |
break; |
| 783 |
case FT_EPS: /* no eps_size code available */ |
| 784 |
default: |
| 785 |
break; |
| 786 |
} |
| 787 |
gvusershape_file_release(us); |
| 788 |
dtinsert(ImageDict, us); |
| 789 |
return us; |
| 790 |
} |
| 791 |
gvusershape_file_release(us); |
| 792 |
return us; |
| 793 |
} |
| 794 |
|
| 795 |
/* gvusershape_size_dpi: |
| 796 |
* Return image size in points. |
| 797 |
*/ |
| 798 |
point gvusershape_size_dpi(usershape_t *us, pointf dpi) { |
| 799 |
if (!us) { |
| 800 |
return (point){.x = -1, .y = -1}; |
| 801 |
} |
| 802 |
if (us->dpi != 0) { |
| 803 |
dpi.x = dpi.y = us->dpi; |
| 804 |
} |
| 805 |
return (point){.x = (int)(us->w * POINTS_PER_INCH / dpi.x), |
| 806 |
.y = (int)(us->h * POINTS_PER_INCH / dpi.y)}; |
| 807 |
} |
| 808 |
|
| 809 |
/* gvusershape_size: |
| 810 |
* Loads user image from file name if not already loaded. |
| 811 |
* Return image size in points. |
| 812 |
*/ |
| 813 |
point gvusershape_size(graph_t *g, char *name) { |
| 814 |
pointf dpi; |
| 815 |
static char *oldpath; |
| 816 |
|
| 817 |
/* no shape file, no shape size */ |
| 818 |
if (!name || (*name == '\0')) { |
| 819 |
return (point){.x = -1, .y = -1}; |
| 820 |
} |
| 821 |
|
| 822 |
if (!HTTPServerEnVar && (oldpath != Gvimagepath)) { |
| 823 |
oldpath = Gvimagepath; |
| 824 |
if (ImageDict) { |
| 825 |
dtclose(ImageDict); |
| 826 |
ImageDict = NULL; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
if ((dpi.y = GD_drawing(g)->dpi) >= 1.0) |
| 831 |
dpi.x = dpi.y; |
| 832 |
else |
| 833 |
dpi.x = dpi.y = DEFAULT_DPI; |
| 834 |
|
| 835 |
usershape_t *const us = gvusershape_open(name); |
| 836 |
return gvusershape_size_dpi(us, dpi); |
| 837 |
} |