24 #define GLOB_NOSPACE 1
25 #define GLOB_ABORTED 2
26 #define GLOB_NOMATCH 3
28 #define DMKEY "Software\\Microsoft" //key to look for library dir
56 #define PNG_MAGIC "\x89PNG\x0D\x0A\x1A\x0A"
57 #define PS_MAGIC "%!PS-Adobe-"
58 #define BMP_MAGIC "BM"
59 #define GIF_MAGIC "GIF8"
60 #define JPEG_MAGIC "\xFF\xD8\xFF\xE0"
61 #define PDF_MAGIC "%PDF-"
62 #define EPS_MAGIC "\xC5\xD0\xD3\xC6"
63 #define XML_MAGIC "<?xml"
64 #define SVG_MAGIC "<svg"
65 #define RIFF_MAGIC "RIFF"
66 #define WEBP_MAGIC "WEBP"
68 #define ICO_MAGIC "\x00\x00\x01\x00"
92 for (i = 0; i <
sizeof(knowntypes) /
sizeof(
knowntype_t); i++) {
93 if (!memcmp (header, knowntypes[i].
template, knowntypes[i].size)) {
98 while (fgets(line,
sizeof(line), us->
f) !=
NULL) {
123 static boolean get_int_lsb_first (FILE *f,
unsigned int sz,
unsigned int *val)
128 for (i = 0; i < sz; i++) {
137 static boolean get_int_msb_first (FILE *f,
unsigned int sz,
unsigned int *val)
142 for (i = 0; i < sz; i++) {
152 static unsigned int svg_units_convert(
double n,
char *u)
154 if (strcmp(u,
"in") == 0)
156 if (strcmp(u,
"px") == 0)
157 return ROUND(n * POINTS_PER_INCH / 96);
158 if (strcmp(u,
"pc") == 0)
159 return ROUND(n * POINTS_PER_INCH / 6);
160 if (strcmp(u,
"pt") == 0 || strcmp(u,
"\"") == 0)
162 if (strcmp(u,
"cm") == 0)
164 if (strcmp(u,
"mm") == 0)
169 static char* svg_attr_value_re =
"([a-z][a-zA-Z]*)=\"([^\"]*)\"";
170 static regex_t
re, *pre =
NULL;
174 unsigned int w = 0, h = 0;
175 double n, x0, y0, x1, y1;
177 char *attribute, *value, *re_string;
185 if (regcomp(&re, svg_attr_value_re, REG_EXTENDED) != 0) {
186 agerr(
AGERR,
"cannot compile regular expression %s", svg_attr_value_re);
191 fseek(us->
f, 0, SEEK_SET);
192 while (fgets(line,
sizeof(line), us->
f) !=
NULL && (!wFlag || !hFlag)) {
194 while (regexec(&re, re_string,
RE_NMATCH, re_pmatch, 0) == 0) {
195 re_string[re_pmatch[1].rm_eo] =
'\0';
196 re_string[re_pmatch[2].rm_eo] =
'\0';
197 attribute = re_string + re_pmatch[1].rm_so;
198 value = re_string + re_pmatch[2].rm_so;
199 re_string += re_pmatch[0].rm_eo + 1;
201 if (strcmp(attribute,
"width") == 0) {
202 if (sscanf(value,
"%lf%2s", &n, u) == 2) {
203 w = svg_units_convert(n, u);
206 else if (sscanf(value,
"%lf", &n) == 1) {
207 w = svg_units_convert(n,
"pt");
213 else if (strcmp(attribute,
"height") == 0) {
214 if (sscanf(value,
"%lf%2s", &n, u) == 2) {
215 h = svg_units_convert(n, u);
218 else if (sscanf(value,
"%lf", &n) == 1) {
219 h = svg_units_convert(n,
"pt");
225 else if (strcmp(attribute,
"viewBox") == 0
226 && sscanf(value,
"%lf %lf %lf %lf", &x0,&y0,&x1,&y1) == 4) {
245 fseek(us->
f, 16, SEEK_SET);
246 if (get_int_msb_first(us->
f, 4, &w) && get_int_msb_first(us->
f, 4, &h)) {
257 fseek(us->
f, 6, SEEK_SET);
258 if (get_int_msb_first(us->
f, 1, &w) && get_int_msb_first(us->
f, 1, &h)) {
272 fseek(us->
f, 6, SEEK_SET);
273 if (get_int_msb_first(us->
f, 1, &w) && get_int_msb_first(us->
f, 1, &h)) {
285 fseek(us->
f, 15, SEEK_SET);
286 if (fgetc(us->
f) ==
'X') {
287 fseek(us->
f, 24, SEEK_SET);
288 if (get_int_lsb_first(us->
f, 4, &w) && get_int_lsb_first(us->
f, 4, &h)) {
294 fseek(us->
f, 26, SEEK_SET);
295 if (get_int_lsb_first(us->
f, 2, &w) && get_int_lsb_first(us->
f, 2, &h)) {
307 fseek(us->
f, 6, SEEK_SET);
308 if (get_int_lsb_first(us->
f, 2, &w) && get_int_lsb_first(us->
f, 2, &h)) {
315 unsigned int size_x_msw, size_x_lsw, size_y_msw, size_y_lsw;
318 fseek (us->
f, 16, SEEK_SET);
319 if ( get_int_lsb_first (us->
f, 2, &size_x_msw) &&
320 get_int_lsb_first (us->
f, 2, &size_x_lsw) &&
321 get_int_lsb_first (us->
f, 2, &size_y_msw) &&
322 get_int_lsb_first (us->
f, 2, &size_y_lsw) ) {
323 us->
w = size_x_msw << 16 | size_x_lsw;
324 us->
h = size_y_msw << 16 | size_y_lsw;
329 unsigned int marker, length, size_x, size_y, junk;
334 static unsigned char standalone_markers [] = {
336 0xd0, 0xd1, 0xd2, 0xd3,
350 if (! get_int_msb_first (us->
f, 1, &marker))
363 if (strchr ((
char*)standalone_markers, marker))
367 if (marker == 0xc0) {
369 if ( get_int_msb_first (us->
f, 3, &junk) &&
370 get_int_msb_first (us->
f, 2, &size_x) &&
371 get_int_msb_first (us->
f, 2, &size_y) ) {
381 if (marker == 0xc2) {
383 if (! get_int_msb_first (us->
f, 3, &junk))
387 if ( get_int_msb_first (us->
f, 2, &size_x) &&
388 get_int_msb_first (us->
f, 2, &size_y) ) {
396 if (! get_int_msb_first (us->
f, 2, &length))
399 fseek (us->
f, length - 2, SEEK_CUR);
411 fseek(us->
f, 0, SEEK_SET);
413 while (fgets(line,
sizeof(line), us->
f)) {
422 if (!(linep = strstr (line,
"%%BoundingBox:")))
424 if (sscanf (linep,
"%%%%BoundingBox: %d %d %d %d", &lx, &ly, &ux, &uy) == 4) {
437 #define KEY "/MediaBox"
448 if (fgets(str->
buf, BUFSIZ, str->
fp)) {
456 #define strc(x) (*(x->s)?*(x->s):nxtc(x))
457 #define stradv(x) (x->s++)
463 while ((c =
strc(str))) {
464 if (isspace(c))
stradv(str);
470 scanNum (
char* tok,
double* dp)
473 double d = strtod(tok, &endp);
475 if (tok == endp)
return 1;
486 while ((c =
strc(str)) && (isdigit(c) || (c ==
'.'))) {
489 if (len == BUFSIZ-1)
break;
502 if (
strc(str) !=
'[')
return 1;
505 if (scanNum(tok,&bp->
LL.
x))
return 1;
507 if (scanNum(tok,&bp->
LL.
y))
return 1;
509 if (scanNum(tok,&bp->
UR.
x))
return 1;
511 if (scanNum(tok,&bp->
UR.
y))
return 1;
516 bboxPDF (FILE* fp,
boxf* bp)
521 while (fgets(buf, BUFSIZ, fp)) {
522 if ((s = strstr(buf,
KEY))) {
524 str.
s = s+(
sizeof(
KEY)-1);
526 return boxof(&str,bp);
537 fseek(us->
f, 0, SEEK_SET);
538 if ( ! bboxPDF (us->
f, &bb)) {
546 static void usershape_close (
Dict_t * dict,
void * p,
Dtdisc_t * disc)
583 #define MAX_USERSHAPE_FILES_OPEN 50
586 static int usershape_files_open_cnt;
594 fseek(us->
f, 0, SEEK_SET);
601 us->
f = fopen(fn,
"r");
603 us->
f = fopen(fn,
"rb");
606 agerr(
AGWARN,
"%s while opening %s\n", strerror(errno), fn);
612 usershape_files_open_cnt++;
634 static usershape_t *gvusershape_open (
const char *name)
655 switch(imagetype(us)) {
658 agerr(
AGWARN,
"\"%s\" was not found as a file or as a shape library member\n", us->
name);
718 dpi.
x = dpi.
y = us->
dpi;
734 static char* oldpath;
738 if (!name || (*name ==
'\0')) {
754 dpi.
x = dpi.
y = (double)DEFAULT_DPI;
756 us = gvusershape_open (name);
void * zmalloc(size_t nbytes)
int(* Dtcompar_f)(Dt_t *, void *, void *, Dtdisc_t *)
unsigned int(* Dthash_f)(Dt_t *, void *, Dtdisc_t *)
shape_desc * find_user_shape(const char *)
EXTERN char * Gvimagepath
void(* datafree)(usershape_t *us)
CDT_API int dtclose(Dt_t *)
void *(* Dtmake_f)(Dt_t *, void *, Dtdisc_t *)
EXTERN char * HTTPServerEnVar
int agerr(agerrlevel_t level, const char *fmt,...)
CDT_API Dt_t * dtopen(Dtdisc_t *, Dtmethod_t *)
point gvusershape_size(graph_t *g, char *name)
boolean gvusershape_file_access(usershape_t *us)
void *(* Dtmemory_f)(Dt_t *, void *, size_t, Dtdisc_t *)
CGRAPH_API int agstrfree(Agraph_t *, char *)
CGRAPH_API char * agstrdup(Agraph_t *, char *)
void gvusershape_file_release(usershape_t *us)
usershape_t * gvusershape_find(char *name)
const char * safefile(const char *filename)
#define MAX_USERSHAPE_FILES_OPEN
CDT_API Dtmethod_t * Dttree
int(* Dtevent_f)(Dt_t *, int, void *, Dtdisc_t *)
point gvusershape_size_dpi(usershape_t *us, pointf dpi)