| 1 | /* |
|---|
| 2 | * TransFig: Facility for Translating Fig code |
|---|
| 3 | * Copyright (c) 1985 Supoj Sutantavibul |
|---|
| 4 | * Copyright (c) 1991 Micah Beck |
|---|
| 5 | * |
|---|
| 6 | * Permission to use, copy, modify, distribute, and sell this software and its |
|---|
| 7 | * documentation for any purpose is hereby granted without fee, provided that |
|---|
| 8 | * the above copyright notice appear in all copies and that both that |
|---|
| 9 | * copyright notice and this permission notice appear in supporting |
|---|
| 10 | * documentation. The authors make no representations about the suitability |
|---|
| 11 | * of this software for any purpose. It is provided "as is" without express |
|---|
| 12 | * or implied warranty. |
|---|
| 13 | * |
|---|
| 14 | * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
|---|
| 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
|---|
| 16 | * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
|---|
| 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
|---|
| 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|---|
| 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|---|
| 20 | * PERFORMANCE OF THIS SOFTWARE. |
|---|
| 21 | * |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /* |
|---|
| 25 | * FIG : Facility for Interactive Generation of figures |
|---|
| 26 | * |
|---|
| 27 | * Copyright (c) 1985, 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU) |
|---|
| 28 | * January 1985. |
|---|
| 29 | * 1st revision : August 1985. |
|---|
| 30 | * 2nd revision : March 1988. |
|---|
| 31 | * |
|---|
| 32 | * %W% %G% |
|---|
| 33 | */ |
|---|
| 34 | #include <stdio.h> |
|---|
| 35 | #include <ctype.h> |
|---|
| 36 | #include <errno.h> |
|---|
| 37 | #include "alloc.h" |
|---|
| 38 | #include "object.h" |
|---|
| 39 | |
|---|
| 40 | #if defined(hpux) || defined(SYSV) |
|---|
| 41 | #define bzero(s,n) memset((s),'\0',(n)) |
|---|
| 42 | #endif |
|---|
| 43 | |
|---|
| 44 | extern int errno; |
|---|
| 45 | |
|---|
| 46 | extern F_arrow *make_arrow(); |
|---|
| 47 | extern char *calloc(); |
|---|
| 48 | extern double floor(), ceil(); |
|---|
| 49 | |
|---|
| 50 | static F_ellipse *read_ellipseobject(); |
|---|
| 51 | static F_line *read_lineobject(); |
|---|
| 52 | static F_text *read_textobject(); |
|---|
| 53 | static F_spline *read_splineobject(); |
|---|
| 54 | static F_arc *read_arcobject(); |
|---|
| 55 | static F_compound *read_compoundobject(); |
|---|
| 56 | |
|---|
| 57 | #define FILL_CONVERT(f) \ |
|---|
| 58 | ((v2_flag || (f) < WHITE_FILL) \ |
|---|
| 59 | ? (f) : 21 - ((f)-1)*5) |
|---|
| 60 | |
|---|
| 61 | #define BUF_SIZE 1024 |
|---|
| 62 | |
|---|
| 63 | char buf[BUF_SIZE]; |
|---|
| 64 | int line_no = 0; |
|---|
| 65 | int num_object; |
|---|
| 66 | int v2_flag; /* Protocol V2.0 or V2.1 */ |
|---|
| 67 | int v21_flag; /* Protocol V2.1 */ |
|---|
| 68 | |
|---|
| 69 | read_fail_message(file, err) |
|---|
| 70 | char *file; |
|---|
| 71 | int err; |
|---|
| 72 | { |
|---|
| 73 | #if !defined(LINUX) && !defined(linux) |
|---|
| 74 | extern char *sys_errlist[]; |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | if (err == 0) /* Successful read */ |
|---|
| 78 | return; |
|---|
| 79 | #if !defined(hpux) && !defined(SYSV) |
|---|
| 80 | else if (err == ENAMETOOLONG) |
|---|
| 81 | put_msg("File name \"%s\" is too long", file); |
|---|
| 82 | #endif |
|---|
| 83 | else if (err == ENOENT) |
|---|
| 84 | put_msg("File \"%s\" does not exist", file); |
|---|
| 85 | else if (err == ENOTDIR) |
|---|
| 86 | put_msg("A name in the path \"%s\" is not a directory", file); |
|---|
| 87 | else if (err == EACCES) |
|---|
| 88 | put_msg("Read access to file \"%s\" is blocked", file); |
|---|
| 89 | else if (err == EISDIR) |
|---|
| 90 | put_msg("File \"%s\" is a directory", file); |
|---|
| 91 | else if (err == -2) { |
|---|
| 92 | put_msg("File \"%s\" is empty", file); |
|---|
| 93 | } |
|---|
| 94 | else if (err == -1) { |
|---|
| 95 | /* Format error; relevant error message is already delivered */ |
|---|
| 96 | } |
|---|
| 97 | else |
|---|
| 98 | put_msg("File \"%s\" is not accessable; %s", file, sys_errlist[err]); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /********************************************************** |
|---|
| 102 | Read_fig returns : |
|---|
| 103 | |
|---|
| 104 | 0 : successful read. |
|---|
| 105 | -1 : File is in incorrect format |
|---|
| 106 | -2 : File is empty |
|---|
| 107 | err_no : if file can not be read for various reasons |
|---|
| 108 | |
|---|
| 109 | The resolution (ppi) and the cooridnate system (coord_sys) are |
|---|
| 110 | stored in obj->nwcorner.x and obj->nwcorner.x respectively. |
|---|
| 111 | **********************************************************/ |
|---|
| 112 | |
|---|
| 113 | read_fig(file_name, obj) |
|---|
| 114 | char *file_name; |
|---|
| 115 | F_compound *obj; |
|---|
| 116 | { |
|---|
| 117 | FILE *fp; |
|---|
| 118 | |
|---|
| 119 | if ((fp = fopen(file_name, "r")) == NULL) |
|---|
| 120 | return(errno); |
|---|
| 121 | else |
|---|
| 122 | return(readfp_fig(fp, obj)); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | readfp_fig(fp, obj) |
|---|
| 126 | FILE *fp; |
|---|
| 127 | F_compound *obj; |
|---|
| 128 | { |
|---|
| 129 | char c; |
|---|
| 130 | int status; |
|---|
| 131 | |
|---|
| 132 | num_object = 0; |
|---|
| 133 | c = fgetc(fp); |
|---|
| 134 | if (feof(fp)) return(-2); |
|---|
| 135 | ungetc(c, fp); |
|---|
| 136 | bzero((char*)obj, COMOBJ_SIZE); |
|---|
| 137 | if (c == '#') |
|---|
| 138 | status = read_objects(fp, obj); |
|---|
| 139 | else |
|---|
| 140 | status = read_1_3_objects(fp, obj); |
|---|
| 141 | (void)fclose(fp); |
|---|
| 142 | return(status); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | int |
|---|
| 146 | read_objects(fp, obj) |
|---|
| 147 | FILE *fp; |
|---|
| 148 | F_compound *obj; |
|---|
| 149 | { |
|---|
| 150 | F_ellipse *e, *le = NULL; |
|---|
| 151 | F_line *l, *ll = NULL; |
|---|
| 152 | F_text *t, *lt = NULL; |
|---|
| 153 | F_spline *s, *ls = NULL; |
|---|
| 154 | F_arc *a, *la = NULL; |
|---|
| 155 | F_compound *c, *lc = NULL; |
|---|
| 156 | int object, ppi, coord_sys; |
|---|
| 157 | |
|---|
| 158 | bzero((char*)obj, COMOBJ_SIZE); |
|---|
| 159 | (void)fgets(buf, BUF_SIZE, fp); /* get the version line */ |
|---|
| 160 | |
|---|
| 161 | v2_flag = (!strncmp(buf, "#FIG 2", 6)); |
|---|
| 162 | v21_flag = (!strncmp(buf, "#FIG 2.1", 8)); |
|---|
| 163 | |
|---|
| 164 | line_no++; |
|---|
| 165 | if (get_line(fp) < 0) { |
|---|
| 166 | put_msg("File is truncated"); |
|---|
| 167 | return(-1); |
|---|
| 168 | } |
|---|
| 169 | if (2 != sscanf(buf,"%d%d\n", &ppi, &coord_sys)) { |
|---|
| 170 | put_msg("Incomplete data at line %d", line_no); |
|---|
| 171 | return(-1); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | obj->nwcorner.x = ppi; |
|---|
| 175 | obj->nwcorner.y = coord_sys; |
|---|
| 176 | while (get_line(fp) > 0) { |
|---|
| 177 | if (1 != sscanf(buf, "%d", &object)) { |
|---|
| 178 | put_msg("Incorrect format at line %d", line_no); |
|---|
| 179 | return(-1); |
|---|
| 180 | } |
|---|
| 181 | switch (object) { |
|---|
| 182 | case O_POLYLINE : |
|---|
| 183 | if ((l = read_lineobject(fp)) == NULL) return(-1); |
|---|
| 184 | if (ll) |
|---|
| 185 | ll = (ll->next = l); |
|---|
| 186 | else |
|---|
| 187 | ll = obj->lines = l; |
|---|
| 188 | num_object++; |
|---|
| 189 | break; |
|---|
| 190 | case O_SPLINE : |
|---|
| 191 | if ((s = read_splineobject(fp)) == NULL) return(-1); |
|---|
| 192 | if (ls) |
|---|
| 193 | ls = (ls->next = s); |
|---|
| 194 | else |
|---|
| 195 | ls = obj->splines = s; |
|---|
| 196 | num_object++; |
|---|
| 197 | break; |
|---|
| 198 | case O_ELLIPSE : |
|---|
| 199 | if ((e = read_ellipseobject()) == NULL) return(-1); |
|---|
| 200 | if (le) |
|---|
| 201 | le = (le->next = e); |
|---|
| 202 | else |
|---|
| 203 | le = obj->ellipses = e; |
|---|
| 204 | num_object++; |
|---|
| 205 | break; |
|---|
| 206 | case O_ARC : |
|---|
| 207 | if ((a = read_arcobject(fp)) == NULL) return(-1); |
|---|
| 208 | if (la) |
|---|
| 209 | la = (la->next = a); |
|---|
| 210 | else |
|---|
| 211 | la = obj->arcs = a; |
|---|
| 212 | num_object++; |
|---|
| 213 | break; |
|---|
| 214 | case O_TEXT : |
|---|
| 215 | if ((t = read_textobject(fp)) == NULL) return(-1); |
|---|
| 216 | if (lt) |
|---|
| 217 | lt = (lt->next = t); |
|---|
| 218 | else |
|---|
| 219 | lt = obj->texts = t; |
|---|
| 220 | num_object++; |
|---|
| 221 | break; |
|---|
| 222 | case O_COMPOUND : |
|---|
| 223 | if ((c = read_compoundobject(fp)) == NULL) return(-1); |
|---|
| 224 | if (lc) |
|---|
| 225 | lc = (lc->next = c); |
|---|
| 226 | else |
|---|
| 227 | lc = obj->compounds = c; |
|---|
| 228 | num_object++; |
|---|
| 229 | break; |
|---|
| 230 | default : |
|---|
| 231 | put_msg("Incorrect object code at line %d", line_no); |
|---|
| 232 | return(-1); |
|---|
| 233 | } /* switch */ |
|---|
| 234 | } /* while */ |
|---|
| 235 | if (feof(fp)) |
|---|
| 236 | return(0); |
|---|
| 237 | else |
|---|
| 238 | return(errno); |
|---|
| 239 | } /* read_objects */ |
|---|
| 240 | |
|---|
| 241 | static F_arc * |
|---|
| 242 | read_arcobject(fp) |
|---|
| 243 | FILE *fp; |
|---|
| 244 | { |
|---|
| 245 | F_arc *a; |
|---|
| 246 | int n, fa, ba; |
|---|
| 247 | int type, style; |
|---|
| 248 | double thickness, wid, ht; |
|---|
| 249 | |
|---|
| 250 | if (NULL == (Arc_malloc(a))) { |
|---|
| 251 | put_msg(Err_mem); |
|---|
| 252 | return(NULL); |
|---|
| 253 | } |
|---|
| 254 | a->pen = 0; |
|---|
| 255 | a->area_fill = 0; |
|---|
| 256 | a->for_arrow = NULL; |
|---|
| 257 | a->back_arrow = NULL; |
|---|
| 258 | a->next = NULL; |
|---|
| 259 | n = sscanf(buf, "%*d%d%d%d%d%d%d%d%lf%d%d%d%lf%lf%d%d%d%d%d%d\n", |
|---|
| 260 | &a->type, &a->style, &a->thickness, |
|---|
| 261 | &a->color, &a->depth, &a->pen, &a->area_fill, |
|---|
| 262 | &a->style_val, &a->direction, &fa, &ba, |
|---|
| 263 | &a->center.x, &a->center.y, |
|---|
| 264 | &a->point[0].x, &a->point[0].y, |
|---|
| 265 | &a->point[1].x, &a->point[1].y, |
|---|
| 266 | &a->point[2].x, &a->point[2].y); |
|---|
| 267 | if (n != 19) { |
|---|
| 268 | put_msg(Err_incomp, "arc", line_no); |
|---|
| 269 | free((char*)a); |
|---|
| 270 | return(NULL); |
|---|
| 271 | } |
|---|
| 272 | a->area_fill = FILL_CONVERT(a->area_fill); |
|---|
| 273 | skip_comment(fp); |
|---|
| 274 | if (fa) { |
|---|
| 275 | line_no++; |
|---|
| 276 | if (5 != fscanf(fp, "%d%d%lf%lf%lf", &type, &style, &thickness, &wid, &ht)) { |
|---|
| 277 | fprintf(stderr, Err_incomp, "arc", line_no); |
|---|
| 278 | return(NULL); |
|---|
| 279 | } |
|---|
| 280 | skip_line(fp); |
|---|
| 281 | a->for_arrow = make_arrow(type, style, thickness, wid, ht); |
|---|
| 282 | skip_comment(fp); |
|---|
| 283 | } |
|---|
| 284 | skip_comment(fp); |
|---|
| 285 | if (ba) { |
|---|
| 286 | line_no++; |
|---|
| 287 | if (5 != fscanf(fp, "%d%d%lf%lf%lf", &type, &style, &thickness, &wid, &ht)) { |
|---|
| 288 | fprintf(stderr, Err_incomp, "arc", line_no); |
|---|
| 289 | return(NULL); |
|---|
| 290 | } |
|---|
| 291 | skip_line(fp); |
|---|
| 292 | a->back_arrow = make_arrow(type, style, thickness, wid, ht); |
|---|
| 293 | } |
|---|
| 294 | return(a); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | static F_compound * |
|---|
| 298 | read_compoundobject(fp) |
|---|
| 299 | FILE *fp; |
|---|
| 300 | { |
|---|
| 301 | F_arc *a, *la = NULL; |
|---|
| 302 | F_ellipse *e, *le = NULL; |
|---|
| 303 | F_line *l, *ll = NULL; |
|---|
| 304 | F_spline *s, *ls = NULL; |
|---|
| 305 | F_text *t, *lt = NULL; |
|---|
| 306 | F_compound *com, *c, *lc = NULL; |
|---|
| 307 | int n, object; |
|---|
| 308 | |
|---|
| 309 | Compound_malloc(com); |
|---|
| 310 | com->arcs = NULL; |
|---|
| 311 | com->ellipses = NULL; |
|---|
| 312 | com->lines = NULL; |
|---|
| 313 | com->splines = NULL; |
|---|
| 314 | com->texts = NULL; |
|---|
| 315 | com->compounds = NULL; |
|---|
| 316 | com->next = NULL; |
|---|
| 317 | n = sscanf(buf, "%*d%d%d%d%d\n", &com->nwcorner.x, &com->nwcorner.y, |
|---|
| 318 | &com->secorner.x, &com->secorner.y); |
|---|
| 319 | if (4 != n) { |
|---|
| 320 | put_msg(Err_incomp, "compound", line_no); |
|---|
| 321 | free((char*)com); |
|---|
| 322 | return(NULL); |
|---|
| 323 | } |
|---|
| 324 | while (get_line(fp) > 0) { |
|---|
| 325 | if (1 != sscanf(buf, "%d", &object)) { |
|---|
| 326 | put_msg(Err_incomp, "compound", line_no); |
|---|
| 327 | free_compound(&com); |
|---|
| 328 | return(NULL); |
|---|
| 329 | } |
|---|
| 330 | switch (object) { |
|---|
| 331 | case O_POLYLINE : |
|---|
| 332 | if ((l = read_lineobject(fp)) == NULL) { |
|---|
| 333 | free_line(&l); |
|---|
| 334 | return(NULL); |
|---|
| 335 | } |
|---|
| 336 | if (ll) |
|---|
| 337 | ll = (ll->next = l); |
|---|
| 338 | else |
|---|
| 339 | ll = com->lines = l; |
|---|
| 340 | break; |
|---|
| 341 | case O_SPLINE : |
|---|
| 342 | if ((s = read_splineobject(fp)) == NULL) { |
|---|
| 343 | free_spline(&s); |
|---|
| 344 | return(NULL); |
|---|
| 345 | } |
|---|
| 346 | if (ls) |
|---|
| 347 | ls = (ls->next = s); |
|---|
| 348 | else |
|---|
| 349 | ls = com->splines = s; |
|---|
| 350 | break; |
|---|
| 351 | case O_ELLIPSE : |
|---|
| 352 | if ((e = read_ellipseobject()) == NULL) { |
|---|
| 353 | free_ellipse(&e); |
|---|
| 354 | return(NULL); |
|---|
| 355 | } |
|---|
| 356 | if (le) |
|---|
| 357 | le = (le->next = e); |
|---|
| 358 | else |
|---|
| 359 | le = com->ellipses = e; |
|---|
| 360 | break; |
|---|
| 361 | case O_ARC : |
|---|
| 362 | if ((a = read_arcobject(fp)) == NULL) { |
|---|
| 363 | free_arc(&a); |
|---|
| 364 | return(NULL); |
|---|
| 365 | } |
|---|
| 366 | if (la) |
|---|
| 367 | la = (la->next = a); |
|---|
| 368 | else |
|---|
| 369 | la = com->arcs = a; |
|---|
| 370 | break; |
|---|
| 371 | case O_TEXT : |
|---|
| 372 | if ((t = read_textobject(fp)) == NULL) { |
|---|
| 373 | free_text(&t); |
|---|
| 374 | return(NULL); |
|---|
| 375 | } |
|---|
| 376 | if (lt) |
|---|
| 377 | lt = (lt->next = t); |
|---|
| 378 | else |
|---|
| 379 | lt = com->texts = t; |
|---|
| 380 | break; |
|---|
| 381 | case O_COMPOUND : |
|---|
| 382 | if ((c = read_compoundobject(fp)) == NULL) { |
|---|
| 383 | free_compound(&c); |
|---|
| 384 | return(NULL); |
|---|
| 385 | } |
|---|
| 386 | if (lc) |
|---|
| 387 | lc = (lc->next = c); |
|---|
| 388 | else |
|---|
| 389 | lc = com->compounds = c; |
|---|
| 390 | break; |
|---|
| 391 | case O_END_COMPOUND : |
|---|
| 392 | return(com); |
|---|
| 393 | default : |
|---|
| 394 | put_msg("Wrong object code at line %d", line_no); |
|---|
| 395 | return(NULL); |
|---|
| 396 | } /* switch */ |
|---|
| 397 | } |
|---|
| 398 | if (feof(fp)) |
|---|
| 399 | return(com); |
|---|
| 400 | else |
|---|
| 401 | return(NULL); |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | static F_ellipse * |
|---|
| 405 | read_ellipseobject() |
|---|
| 406 | { |
|---|
| 407 | F_ellipse *e; |
|---|
| 408 | int n; |
|---|
| 409 | |
|---|
| 410 | Ellipse_malloc(e); |
|---|
| 411 | e->area_fill = 0; |
|---|
| 412 | e->pen = 0; |
|---|
| 413 | e->next = NULL; |
|---|
| 414 | n = sscanf(buf, "%*d%d%d%d%d%d%d%d%lf%d%lf%d%d%d%d%d%d%d%d\n", |
|---|
| 415 | &e->type, &e->style, &e->thickness, |
|---|
| 416 | &e->color, &e->depth, &e->pen, &e->area_fill, |
|---|
| 417 | &e->style_val, &e->direction, &e->angle, |
|---|
| 418 | &e->center.x, &e->center.y, |
|---|
| 419 | &e->radiuses.x, &e->radiuses.y, |
|---|
| 420 | &e->start.x, &e->start.y, |
|---|
| 421 | &e->end.x, &e->end.y); |
|---|
| 422 | if (n != 18) { |
|---|
| 423 | put_msg(Err_incomp, "ellipse", line_no); |
|---|
| 424 | free((char*)e); |
|---|
| 425 | return(NULL); |
|---|
| 426 | } |
|---|
| 427 | e->area_fill = FILL_CONVERT(e->area_fill); |
|---|
| 428 | return(e); |
|---|
| 429 | } |
|---|
| 430 | |
|---|
| 431 | static F_line * |
|---|
| 432 | read_lineobject(fp) |
|---|
| 433 | FILE *fp; |
|---|
| 434 | { |
|---|
| 435 | F_line *l; |
|---|
| 436 | F_point *p, *q; |
|---|
| 437 | int n, x, y, fa, ba; |
|---|
| 438 | int type, style, radius_flag; |
|---|
| 439 | double thickness, wid, ht; |
|---|
| 440 | |
|---|
| 441 | Line_malloc(l); |
|---|
| 442 | l->points = NULL; |
|---|
| 443 | l->pen = 0; |
|---|
| 444 | l->area_fill = 0; |
|---|
| 445 | l->for_arrow = NULL; |
|---|
| 446 | l->back_arrow = NULL; |
|---|
| 447 | l->next = NULL; |
|---|
| 448 | |
|---|
| 449 | sscanf(buf,"%*d%d",&l->type); /* get the line type */ |
|---|
| 450 | |
|---|
| 451 | radius_flag = v21_flag || (v2_flag && l->type == T_ARC_BOX); |
|---|
| 452 | if (radius_flag) |
|---|
| 453 | { |
|---|
| 454 | n = sscanf(buf, "%*d%d%d%d%d%d%d%d%lf%d%d%d", |
|---|
| 455 | &l->type, &l->style, &l->thickness, &l->color, |
|---|
| 456 | &l->depth, &l->pen, &l->area_fill, &l->style_val, &l->radius, &fa, &ba); |
|---|
| 457 | } |
|---|
| 458 | /* old format uses pen for radius of arc-box corners */ |
|---|
| 459 | else |
|---|
| 460 | { |
|---|
| 461 | n = sscanf(buf, "%*d%d%d%d%d%d%d%d%lf%d%d", |
|---|
| 462 | &l->type, &l->style, &l->thickness, &l->color, |
|---|
| 463 | &l->depth, &l->pen, &l->area_fill, &l->style_val, &fa, &ba); |
|---|
| 464 | if (l->type == T_ARC_BOX) |
|---|
| 465 | { |
|---|
| 466 | l->radius = (int) l->pen; |
|---|
| 467 | l->pen = 0; |
|---|
| 468 | } |
|---|
| 469 | else |
|---|
| 470 | l->radius = 0; |
|---|
| 471 | } |
|---|
| 472 | if ((!radius_flag && n!=10) || (radius_flag && n!=11)) { |
|---|
| 473 | put_msg(Err_incomp, "line", line_no); |
|---|
| 474 | free((char*)l); |
|---|
| 475 | return(NULL); |
|---|
| 476 | } |
|---|
| 477 | l->area_fill = FILL_CONVERT(l->area_fill); |
|---|
| 478 | skip_comment(fp); |
|---|
| 479 | if (fa) { |
|---|
| 480 | line_no++; |
|---|
| 481 | if (5 != fscanf(fp, "%d%d%lf%lf%lf", &type, &style, &thickness, &wid, &ht)) { |
|---|
| 482 | fprintf(stderr, Err_incomp, "line", line_no); |
|---|
| 483 | return(NULL); |
|---|
| 484 | } |
|---|
| 485 | skip_line(fp); |
|---|
| 486 | l->for_arrow = make_arrow(type, style, thickness, wid, ht); |
|---|
| 487 | skip_comment(fp); |
|---|
| 488 | } |
|---|
| 489 | if (ba) { |
|---|
| 490 | line_no++; |
|---|
| 491 | if (5 != fscanf(fp, "%d%d%lf%lf%lf", &type, &style, &thickness, &wid, &ht)) { |
|---|
| 492 | fprintf(stderr, Err_incomp, "line", line_no); |
|---|
| 493 | return(NULL); |
|---|
| 494 | } |
|---|
| 495 | skip_line(fp); |
|---|
| 496 | l->back_arrow = make_arrow(type, style, thickness, wid, ht); |
|---|
| 497 | skip_comment(fp); |
|---|
| 498 | } |
|---|
| 499 | if (l->type == T_EPS_BOX) { |
|---|
| 500 | line_no++; |
|---|
| 501 | Eps_malloc(l->eps); |
|---|
| 502 | if (l->eps == NULL) { |
|---|
| 503 | free((char *) l); |
|---|
| 504 | return (NULL); |
|---|
| 505 | } |
|---|
| 506 | if (2 != fscanf(fp, "%d %s", &l->eps->flipped, l->eps->file)) { |
|---|
| 507 | put_msg(Err_incomp, |
|---|
| 508 | "Encapsulated Postscript", line_no); |
|---|
| 509 | fprintf(stderr, Err_incomp, |
|---|
| 510 | "Encapsulated Postscript", line_no); |
|---|
| 511 | return (NULL); |
|---|
| 512 | } |
|---|
| 513 | read_epsf(l->eps); |
|---|
| 514 | } else |
|---|
| 515 | l->eps = NULL; |
|---|
| 516 | |
|---|
| 517 | if (NULL == (l->points = Point_malloc(p))) { |
|---|
| 518 | put_msg(Err_mem); |
|---|
| 519 | return(NULL); |
|---|
| 520 | } |
|---|
| 521 | p->next = NULL; |
|---|
| 522 | if (fscanf(fp, "%d%d", &p->x, &p->y) != 2) { |
|---|
| 523 | put_msg(Err_incomp, "line", line_no); |
|---|
| 524 | free_linestorage(l); |
|---|
| 525 | return(NULL); |
|---|
| 526 | } |
|---|
| 527 | for (;;) { |
|---|
| 528 | if (fscanf(fp, "%d%d", &x, &y) != 2) { |
|---|
| 529 | put_msg(Err_incomp, "line", line_no); |
|---|
| 530 | free_linestorage(l); |
|---|
| 531 | return(NULL); |
|---|
| 532 | } |
|---|
| 533 | if (x == 9999) break; |
|---|
| 534 | if (NULL == (Point_malloc(q))) { |
|---|
| 535 | put_msg(Err_mem); |
|---|
| 536 | free_linestorage(l); |
|---|
| 537 | return(NULL); |
|---|
| 538 | } |
|---|
| 539 | q->x = x; |
|---|
| 540 | q->y = y; |
|---|
| 541 | q->next = NULL; |
|---|
| 542 | p->next = q; |
|---|
| 543 | p = q; |
|---|
| 544 | } |
|---|
| 545 | skip_line(fp); |
|---|
| 546 | return(l); |
|---|
| 547 | } |
|---|
| 548 | |
|---|
| 549 | static F_spline * |
|---|
| 550 | read_splineobject(fp) |
|---|
| 551 | FILE *fp; |
|---|
| 552 | { |
|---|
| 553 | F_spline *s; |
|---|
| 554 | F_point *p, *q; |
|---|
| 555 | F_control *cp, *cq; |
|---|
| 556 | int c, n, x, y, fa, ba; |
|---|
| 557 | int type, style; |
|---|
| 558 | double thickness, wid, ht; |
|---|
| 559 | double lx, ly, rx, ry; |
|---|
| 560 | |
|---|
| 561 | Spline_malloc(s); |
|---|
| 562 | s->points = NULL; |
|---|
| 563 | s->controls = NULL; |
|---|
| 564 | s->pen = 0; |
|---|
| 565 | s->area_fill = 0; |
|---|
| 566 | s->for_arrow = NULL; |
|---|
| 567 | s->back_arrow = NULL; |
|---|
| 568 | s->next = NULL; |
|---|
| 569 | |
|---|
| 570 | n = sscanf(buf, "%*d%d%d%d%d%d%d%d%lf%d%d", |
|---|
| 571 | &s->type, &s->style, &s->thickness, &s->color, |
|---|
| 572 | &s->depth, &s->pen, &s->area_fill, &s->style_val, &fa, &ba); |
|---|
| 573 | if (n != 10) { |
|---|
| 574 | put_msg(Err_incomp, "spline", line_no); |
|---|
| 575 | free((char*)s); |
|---|
| 576 | return(NULL); |
|---|
| 577 | } |
|---|
| 578 | s->area_fill = FILL_CONVERT(s->area_fill); |
|---|
| 579 | skip_comment(fp); |
|---|
| 580 | if (fa) { |
|---|
| 581 | line_no++; |
|---|
| 582 | if (5 != fscanf(fp, "%d%d%lf%lf%lf", &type, &style, &thickness, &wid, &ht)) { |
|---|
| 583 | fprintf(stderr, Err_incomp, "spline", line_no); |
|---|
| 584 | return(NULL); |
|---|
| 585 | } |
|---|
| 586 | skip_line(fp); |
|---|
| 587 | s->for_arrow = make_arrow(type, style, thickness, wid, ht); |
|---|
| 588 | skip_comment(fp); |
|---|
| 589 | } |
|---|
| 590 | if (ba) { |
|---|
| 591 | line_no++; |
|---|
| 592 | if (5 != fscanf(fp, "%d%d%lf%lf%lf", &type, &style, &thickness, &wid, &ht)) { |
|---|
| 593 | fprintf(stderr, Err_incomp, "spline", line_no); |
|---|
| 594 | return(NULL); |
|---|
| 595 | } |
|---|
| 596 | skip_line(fp); |
|---|
| 597 | s->back_arrow = make_arrow(type, style, thickness, wid, ht); |
|---|
| 598 | skip_comment(fp); |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | /* Read points */ |
|---|
| 602 | if ((n = fscanf(fp, "%d%d", &x, &y)) != 2) { |
|---|
| 603 | put_msg(Err_incomp, "spline", line_no); |
|---|
| 604 | free_splinestorage(s); |
|---|
| 605 | return(NULL); |
|---|
| 606 | }; |
|---|
| 607 | if (NULL == (s->points = Point_malloc(p))) { |
|---|
| 608 | put_msg(Err_mem); |
|---|
| 609 | free_splinestorage(s); |
|---|
| 610 | return(NULL); |
|---|
| 611 | } |
|---|
| 612 | p->x = x; p->y = y; |
|---|
| 613 | for (c = 1;;) { |
|---|
| 614 | if (fscanf(fp, "%d%d", &x, &y) != 2) { |
|---|
| 615 | put_msg(Err_incomp, "spline", line_no); |
|---|
| 616 | p->next = NULL; |
|---|
| 617 | free_splinestorage(s); |
|---|
| 618 | return(NULL); |
|---|
| 619 | }; |
|---|
| 620 | if (x == 9999) break; |
|---|
| 621 | if (NULL == (Point_malloc(q))) { |
|---|
| 622 | put_msg(Err_mem); |
|---|
| 623 | free_splinestorage(s); |
|---|
| 624 | return(NULL); |
|---|
| 625 | } |
|---|
| 626 | q->x = x; |
|---|
| 627 | q->y = y; |
|---|
| 628 | p->next = q; |
|---|
| 629 | p = q; |
|---|
| 630 | c++; |
|---|
| 631 | } |
|---|
| 632 | p->next = NULL; |
|---|
| 633 | skip_line(fp); |
|---|
| 634 | |
|---|
| 635 | if (normal_spline(s)) return(s); |
|---|
| 636 | |
|---|
| 637 | skip_comment(fp); |
|---|
| 638 | /* Read controls */ |
|---|
| 639 | if ((n = fscanf(fp, "%lf%lf%lf%lf", &lx, &ly, &rx, &ry)) != 4) { |
|---|
| 640 | put_msg(Err_incomp, "spline", line_no); |
|---|
| 641 | free_splinestorage(s); |
|---|
| 642 | return(NULL); |
|---|
| 643 | }; |
|---|
| 644 | if (NULL == (s->controls = Control_malloc(cp))) { |
|---|
| 645 | put_msg(Err_mem); |
|---|
| 646 | free_splinestorage(s); |
|---|
| 647 | return(NULL); |
|---|
| 648 | } |
|---|
| 649 | cp->lx = lx; cp->ly = ly; |
|---|
| 650 | cp->rx = rx; cp->ry = ry; |
|---|
| 651 | while (--c) { |
|---|
| 652 | if (fscanf(fp, "%lf%lf%lf%lf", &lx, &ly, &rx, &ry) != 4) { |
|---|
| 653 | put_msg(Err_incomp, "spline", line_no); |
|---|
| 654 | cp->next = NULL; |
|---|
| 655 | free_splinestorage(s); |
|---|
| 656 | return(NULL); |
|---|
| 657 | }; |
|---|
| 658 | if (NULL == (Control_malloc(cq))) { |
|---|
| 659 | put_msg(Err_mem); |
|---|
| 660 | cp->next = NULL; |
|---|
| 661 | free_splinestorage(s); |
|---|
| 662 | return(NULL); |
|---|
| 663 | } |
|---|
| 664 | cq->lx = lx; cq->ly = ly; |
|---|
| 665 | cq->rx = rx; cq->ry = ry; |
|---|
| 666 | cp->next = cq; |
|---|
| 667 | cp = cq; |
|---|
| 668 | } |
|---|
| 669 | cp->next = NULL; |
|---|
| 670 | |
|---|
| 671 | skip_line(fp); |
|---|
| 672 | return(s); |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | static F_text * |
|---|
| 676 | read_textobject(fp) |
|---|
| 677 | FILE *fp; |
|---|
| 678 | { |
|---|
| 679 | F_text *t; |
|---|
| 680 | int n, ignore = 0; |
|---|
| 681 | char s[BUF_SIZE], s_temp[BUF_SIZE], junk[2]; |
|---|
| 682 | |
|---|
| 683 | Text_malloc(t); |
|---|
| 684 | t->font = 0; |
|---|
| 685 | t->size = 0.0; |
|---|
| 686 | t->next = NULL; |
|---|
| 687 | /* The text object is terminated by a CONTROL-A, so we read |
|---|
| 688 | everything up to the CONTROL-A and then read that character. |
|---|
| 689 | If we do not find the CONTROL-A on this line then this must |
|---|
| 690 | be a multi-line text object and we will have to read more. */ |
|---|
| 691 | n = sscanf(buf,"%*d%d%d%lf%d%d%d%lf%d%lf%lf%d%d%[^\1]%[\1]", |
|---|
| 692 | &t->type, &t->font, &t->size, &t->pen, |
|---|
| 693 | &t->color, &t->depth, &t->angle, |
|---|
| 694 | &t->flags, &t->height, &t->length, |
|---|
| 695 | &t->base_x, &t->base_y, s, junk); |
|---|
| 696 | if ((n != 14) && (n != 13)) { |
|---|
| 697 | put_msg(Err_incomp, "text", line_no); |
|---|
| 698 | free((char*)t); |
|---|
| 699 | /* return(NULL); */ |
|---|
| 700 | } |
|---|
| 701 | if (n == 13) { |
|---|
| 702 | /* Read in the remainder of the text object. */ |
|---|
| 703 | do { |
|---|
| 704 | fgets(buf, BUF_SIZE, fp); |
|---|
| 705 | line_no++; /* As is done in get_line */ |
|---|
| 706 | n = sscanf(buf,"%[^\1]%[\1]", s_temp, junk); |
|---|
| 707 | /* Safety check */ |
|---|
| 708 | if (strlen(s)+1 + strlen(s_temp)+1 > BUF_SIZE) { |
|---|
| 709 | /* Too many characters. Ignore the rest. */ |
|---|
| 710 | ignore = 1; |
|---|
| 711 | } |
|---|
| 712 | if (!ignore) |
|---|
| 713 | strcat(s, s_temp); |
|---|
| 714 | } while (n == 1); |
|---|
| 715 | } |
|---|
| 716 | if (strlen(s) == 0) (void)strcpy(s, " "); |
|---|
| 717 | t->cstring = (char*)calloc((unsigned)(strlen(s)), sizeof(char)); |
|---|
| 718 | if (NULL == t->cstring) { |
|---|
| 719 | put_msg(Err_mem); |
|---|
| 720 | free((char*)t); |
|---|
| 721 | return(NULL); |
|---|
| 722 | } |
|---|
| 723 | (void)strcpy(t->cstring, s+1); |
|---|
| 724 | |
|---|
| 725 | if (!v21_flag && (t->font == 0 || t->font == DEFAULT)) |
|---|
| 726 | t->flags = ((t->flags != DEFAULT) ? t->flags : 0) |
|---|
| 727 | | SPECIAL_TEXT; |
|---|
| 728 | |
|---|
| 729 | if (v2_flag && !v21_flag && !special_text(t)) |
|---|
| 730 | t->flags = ((t->flags != DEFAULT) ? t->flags : 0) |
|---|
| 731 | | PSFONT_TEXT; |
|---|
| 732 | |
|---|
| 733 | return(t); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | get_line(fp) |
|---|
| 737 | FILE *fp; |
|---|
| 738 | { |
|---|
| 739 | while (1) { |
|---|
| 740 | if (NULL == fgets(buf, BUF_SIZE, fp)) { |
|---|
| 741 | return(-1); |
|---|
| 742 | } |
|---|
| 743 | line_no++; |
|---|
| 744 | if (*buf != '\n' && *buf != '#') return(1); |
|---|
| 745 | /* Skip empty and comment lines */ |
|---|
| 746 | } |
|---|
| 747 | } |
|---|
| 748 | |
|---|
| 749 | skip_comment(fp) |
|---|
| 750 | FILE *fp; |
|---|
| 751 | { |
|---|
| 752 | char c; |
|---|
| 753 | |
|---|
| 754 | while ((c = fgetc(fp)) == '#') skip_line(fp); |
|---|
| 755 | if (c != '#') ungetc(c, fp); |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | skip_line(fp) |
|---|
| 759 | FILE *fp; |
|---|
| 760 | { |
|---|
| 761 | while (fgetc(fp) != '\n') { |
|---|
| 762 | if (feof(fp)) return; |
|---|
| 763 | } |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | read_epsf(eps) |
|---|
| 767 | F_eps *eps; |
|---|
| 768 | { |
|---|
| 769 | int nbitmap; |
|---|
| 770 | int bitmapz; |
|---|
| 771 | char *cp; |
|---|
| 772 | unsigned char *mp; |
|---|
| 773 | int n; |
|---|
| 774 | int flag; |
|---|
| 775 | char buf[300]; |
|---|
| 776 | int llx, lly, urx, ury; |
|---|
| 777 | FILE *epsf; |
|---|
| 778 | register unsigned char *last; |
|---|
| 779 | double fllx, flly, furx, fury; |
|---|
| 780 | |
|---|
| 781 | epsf = fopen(eps->file, "r"); |
|---|
| 782 | if (epsf == NULL) { |
|---|
| 783 | put_msg("Cannot open file: %s", eps->file); |
|---|
| 784 | return 0; |
|---|
| 785 | } |
|---|
| 786 | while (fgets(buf, 300, epsf) != NULL) { |
|---|
| 787 | lower(buf); |
|---|
| 788 | if (!strncmp(buf, "%%boundingbox", 13)) { |
|---|
| 789 | if (sscanf(buf, "%%%%boundingbox: %lf %lf %lf %lf", |
|---|
| 790 | &fllx, &flly, &furx, &fury) < 4) { |
|---|
| 791 | put_msg("Bad EPS bitmap file: %s", eps->file); |
|---|
| 792 | fclose(epsf); |
|---|
| 793 | return 0; |
|---|
| 794 | } |
|---|
| 795 | llx= floor(fllx); |
|---|
| 796 | lly= floor(flly); |
|---|
| 797 | urx= ceil(furx); |
|---|
| 798 | ury= ceil(fury); |
|---|
| 799 | break; |
|---|
| 800 | } |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | eps->hw_ratio = (float) (ury - lly) / (float) (urx - llx); |
|---|
| 804 | |
|---|
| 805 | eps->bitmap = NULL; |
|---|
| 806 | eps->bit_size.x = 0; |
|---|
| 807 | eps->bit_size.y = 0; |
|---|
| 808 | |
|---|
| 809 | eps->pix_rotation = 0; |
|---|
| 810 | eps->pix_width = 0; |
|---|
| 811 | eps->pix_height = 0; |
|---|
| 812 | |
|---|
| 813 | if (ury - lly <= 0 || urx - llx <= 0) { |
|---|
| 814 | put_msg("Bad values in EPS bitmap bounding box"); |
|---|
| 815 | } |
|---|
| 816 | bitmapz = 0; |
|---|
| 817 | |
|---|
| 818 | /* look for a preview bitmap */ |
|---|
| 819 | while (fgets(buf, 300, epsf) != NULL) { |
|---|
| 820 | lower(buf); |
|---|
| 821 | if (!strncmp(buf, "%%beginpreview", 14)) { |
|---|
| 822 | sscanf(buf, "%%%%beginpreview: %d %d %d", |
|---|
| 823 | &eps->bit_size.x, &eps->bit_size.y, &bitmapz); |
|---|
| 824 | break; |
|---|
| 825 | } |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | if (eps->bit_size.x > 0 && eps->bit_size.y > 0 && bitmapz == 1) { |
|---|
| 829 | nbitmap = (eps->bit_size.x + 7) / 8 * eps->bit_size.y; |
|---|
| 830 | eps->bitmap = (unsigned char *) malloc(nbitmap); |
|---|
| 831 | if (eps->bitmap == NULL) |
|---|
| 832 | fprintf(stderr, "could not allocate %d bytes of memory\n", nbitmap); |
|---|
| 833 | } |
|---|
| 834 | /* read for a preview bitmap */ |
|---|
| 835 | if (eps->bitmap != NULL) { |
|---|
| 836 | mp = eps->bitmap; |
|---|
| 837 | bzero(mp, nbitmap); /* init bitmap to zero */ |
|---|
| 838 | last = eps->bitmap + nbitmap; |
|---|
| 839 | flag = 1; |
|---|
| 840 | while (fgets(buf, 300, epsf) != NULL && mp < last) { |
|---|
| 841 | lower(buf); |
|---|
| 842 | if (!strncmp(buf, "%%endpreview", 12) || |
|---|
| 843 | !strncmp(buf, "%%endimage", 10)) |
|---|
| 844 | break; |
|---|
| 845 | cp = buf; |
|---|
| 846 | if (*cp != '%') |
|---|
| 847 | break; |
|---|
| 848 | cp++; |
|---|
| 849 | while (*cp != '\0') { |
|---|
| 850 | if (isxdigit(*cp)) { |
|---|
| 851 | n = hex(*cp); |
|---|
| 852 | if (flag) { |
|---|
| 853 | flag = 0; |
|---|
| 854 | *mp = n << 4; |
|---|
| 855 | } else { |
|---|
| 856 | flag = 1; |
|---|
| 857 | *mp = *mp + n; |
|---|
| 858 | mp++; |
|---|
| 859 | if (mp >= last) |
|---|
| 860 | break; |
|---|
| 861 | } |
|---|
| 862 | } |
|---|
| 863 | cp++; |
|---|
| 864 | } |
|---|
| 865 | } |
|---|
| 866 | } |
|---|
| 867 | fclose(epsf); |
|---|
| 868 | return 1; |
|---|
| 869 | } |
|---|
| 870 | |
|---|
| 871 | int |
|---|
| 872 | hex(c) |
|---|
| 873 | char c; |
|---|
| 874 | { |
|---|
| 875 | if (isdigit(c)) |
|---|
| 876 | return (c - 48); |
|---|
| 877 | else |
|---|
| 878 | return (c - 87); |
|---|
| 879 | } |
|---|
| 880 | |
|---|
| 881 | lower(buf) |
|---|
| 882 | char *buf; |
|---|
| 883 | { |
|---|
| 884 | while (*buf) { |
|---|
| 885 | if (isupper(*buf)) |
|---|
| 886 | *buf = (char) tolower(*buf); |
|---|
| 887 | buf++; |
|---|
| 888 | } |
|---|
| 889 | } |
|---|