| 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 | /*************** Read version 1.3 format ***************/ |
|---|
| 26 | /*******************************************************************/ |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | #include <sys/types.h> |
|---|
| 29 | #include <sys/stat.h> |
|---|
| 30 | #include <errno.h> |
|---|
| 31 | #include "alloc.h" |
|---|
| 32 | #include "object.h" |
|---|
| 33 | |
|---|
| 34 | /******* Fig 1.3 subtype of objects *******/ |
|---|
| 35 | #define DRAW_ELLIPSE_BY_RAD 1 |
|---|
| 36 | #define DRAW_ELLIPSE_BY_DIA 2 |
|---|
| 37 | #define DRAW_CIRCLE_BY_RAD 3 |
|---|
| 38 | #define DRAW_CIRCLE_BY_DIA 4 |
|---|
| 39 | #define DRAW_CIRCULAR_ARC 5 |
|---|
| 40 | #define DRAW_POLYLINE 6 |
|---|
| 41 | #define DRAW_BOX 7 |
|---|
| 42 | #define DRAW_POLYGON 8 |
|---|
| 43 | #define DRAW_TEXT 9 |
|---|
| 44 | #define DRAW_SPLINE 10 |
|---|
| 45 | #define DRAW_CLOSEDSPLINE 11 |
|---|
| 46 | #define DRAW_COMPOUND 13 |
|---|
| 47 | |
|---|
| 48 | extern F_arrow *forward_arrow(), *backward_arrow(); |
|---|
| 49 | extern int figure_modified; |
|---|
| 50 | extern int errno; |
|---|
| 51 | #if !defined(LINUX) && !defined(linux) |
|---|
| 52 | extern char *sys_errlist[]; |
|---|
| 53 | #endif |
|---|
| 54 | extern int sys_nerr, errno; |
|---|
| 55 | |
|---|
| 56 | static F_ellipse *read_ellipseobject(); |
|---|
| 57 | static F_line *read_lineobject(); |
|---|
| 58 | static F_text *read_textobject(); |
|---|
| 59 | static F_spline *read_splineobject(); |
|---|
| 60 | static F_arc *read_arcobject(); |
|---|
| 61 | static F_compound *read_compoundobject(); |
|---|
| 62 | |
|---|
| 63 | extern int line_no; |
|---|
| 64 | extern int num_object; |
|---|
| 65 | |
|---|
| 66 | int |
|---|
| 67 | read_1_3_objects(fp, obj) |
|---|
| 68 | FILE *fp; |
|---|
| 69 | F_compound *obj; |
|---|
| 70 | { |
|---|
| 71 | F_ellipse *e, *le = NULL; |
|---|
| 72 | F_line *l, *ll = NULL; |
|---|
| 73 | F_text *t, *lt = NULL; |
|---|
| 74 | F_spline *s, *ls = NULL; |
|---|
| 75 | F_arc *a, *la = NULL; |
|---|
| 76 | F_compound *c, *lc = NULL; |
|---|
| 77 | int n; |
|---|
| 78 | int object, pixperinch, canvaswid, canvasht, coord_sys; |
|---|
| 79 | |
|---|
| 80 | n = fscanf(fp,"%d%d%d%d\n", &pixperinch, &coord_sys, &canvaswid, &canvasht); |
|---|
| 81 | if (n != 4) { |
|---|
| 82 | put_msg("Incorrect format in the first line in input file"); |
|---|
| 83 | return(-1); |
|---|
| 84 | } |
|---|
| 85 | obj->nwcorner.x = pixperinch; |
|---|
| 86 | obj->nwcorner.y = coord_sys; |
|---|
| 87 | while (fscanf(fp, "%d", &object) == 1) { |
|---|
| 88 | switch (object) { |
|---|
| 89 | case O_POLYLINE : |
|---|
| 90 | if ((l = read_lineobject(fp)) == NULL) return(-1); |
|---|
| 91 | if (ll) |
|---|
| 92 | ll = (ll->next = l); |
|---|
| 93 | else |
|---|
| 94 | ll = obj->lines = l; |
|---|
| 95 | num_object++; |
|---|
| 96 | break; |
|---|
| 97 | case O_SPLINE : |
|---|
| 98 | if ((s = read_splineobject(fp)) == NULL) return(-1); |
|---|
| 99 | if (ls) |
|---|
| 100 | ls = (ls->next = s); |
|---|
| 101 | else |
|---|
| 102 | ls = obj->splines = s; |
|---|
| 103 | num_object++; |
|---|
| 104 | break; |
|---|
| 105 | case O_ELLIPSE : |
|---|
| 106 | if ((e = read_ellipseobject(fp)) == NULL) return(-1); |
|---|
| 107 | if (le) |
|---|
| 108 | le = (le->next = e); |
|---|
| 109 | else |
|---|
| 110 | le = obj->ellipses = e; |
|---|
| 111 | num_object++; |
|---|
| 112 | break; |
|---|
| 113 | case O_ARC : |
|---|
| 114 | if ((a = read_arcobject(fp)) == NULL) return(-1); |
|---|
| 115 | if (la) |
|---|
| 116 | la = (la->next = a); |
|---|
| 117 | else |
|---|
| 118 | la = obj->arcs = a; |
|---|
| 119 | num_object++; |
|---|
| 120 | break; |
|---|
| 121 | case O_TEXT : |
|---|
| 122 | if ((t = read_textobject(fp)) == NULL) return(-1); |
|---|
| 123 | if (lt) |
|---|
| 124 | lt = (lt->next = t); |
|---|
| 125 | else |
|---|
| 126 | lt = obj->texts = t; |
|---|
| 127 | num_object++; |
|---|
| 128 | break; |
|---|
| 129 | case O_COMPOUND : |
|---|
| 130 | if ((c = read_compoundobject(fp)) == NULL) return(-1); |
|---|
| 131 | if (lc) |
|---|
| 132 | lc = (lc->next = c); |
|---|
| 133 | else |
|---|
| 134 | lc = obj->compounds = c; |
|---|
| 135 | num_object++; |
|---|
| 136 | break; |
|---|
| 137 | default: |
|---|
| 138 | put_msg("Incorrect object code %d", object); |
|---|
| 139 | return(-1); |
|---|
| 140 | } /* switch */ |
|---|
| 141 | } /* while */ |
|---|
| 142 | if (feof(fp)) |
|---|
| 143 | return(0); |
|---|
| 144 | else |
|---|
| 145 | return(errno); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static F_arc * |
|---|
| 149 | read_arcobject(fp) |
|---|
| 150 | FILE *fp; |
|---|
| 151 | { |
|---|
| 152 | F_arc *a; |
|---|
| 153 | int f, b, h, w, n; |
|---|
| 154 | |
|---|
| 155 | Arc_malloc(a); |
|---|
| 156 | a->type = T_3_POINTS_ARC; |
|---|
| 157 | a->color = BLACK_COLOR; |
|---|
| 158 | a->depth = 0; |
|---|
| 159 | a->pen = 0; |
|---|
| 160 | a->for_arrow = NULL; |
|---|
| 161 | a->back_arrow = NULL; |
|---|
| 162 | a->next = NULL; |
|---|
| 163 | n = fscanf(fp, " %d %d %d %lf %d %d %d %d %d %lf %lf %d %d %d %d %d %d\n", |
|---|
| 164 | &a->type, &a->style, &a->thickness, |
|---|
| 165 | &a->style_val, &a->direction, &f, &b, |
|---|
| 166 | &h, &w, &a->center.x, &a->center.y, |
|---|
| 167 | &a->point[0].x, &a->point[0].y, |
|---|
| 168 | &a->point[1].x, &a->point[1].y, |
|---|
| 169 | &a->point[2].x, &a->point[2].y); |
|---|
| 170 | if (n != 17) { |
|---|
| 171 | put_msg("incomplete arc data"); |
|---|
| 172 | free((char*)a); |
|---|
| 173 | return(NULL); |
|---|
| 174 | } |
|---|
| 175 | if (f) { |
|---|
| 176 | a->for_arrow = forward_arrow(); |
|---|
| 177 | a->for_arrow->wid = w; |
|---|
| 178 | a->for_arrow->ht = h; |
|---|
| 179 | } |
|---|
| 180 | if (b) { |
|---|
| 181 | a->back_arrow = backward_arrow(); |
|---|
| 182 | a->back_arrow->wid = w; |
|---|
| 183 | a->back_arrow->ht = h; |
|---|
| 184 | } |
|---|
| 185 | return(a); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | static F_compound * |
|---|
| 189 | read_compoundobject(fp) |
|---|
| 190 | FILE *fp; |
|---|
| 191 | { |
|---|
| 192 | F_arc *a, *la = NULL; |
|---|
| 193 | F_ellipse *e, *le = NULL; |
|---|
| 194 | F_line *l, *ll = NULL; |
|---|
| 195 | F_spline *s, *ls = NULL; |
|---|
| 196 | F_text *t, *lt = NULL; |
|---|
| 197 | F_compound *com, *c, *lc = NULL; |
|---|
| 198 | int n, object; |
|---|
| 199 | |
|---|
| 200 | Compound_malloc(com); |
|---|
| 201 | com->arcs = NULL; |
|---|
| 202 | com->ellipses = NULL; |
|---|
| 203 | com->lines = NULL; |
|---|
| 204 | com->splines = NULL; |
|---|
| 205 | com->texts = NULL; |
|---|
| 206 | com->compounds = NULL; |
|---|
| 207 | com->next = NULL; |
|---|
| 208 | n = fscanf(fp, " %d %d %d %d\n", &com->nwcorner.x, &com->nwcorner.y, |
|---|
| 209 | &com->secorner.x, &com->secorner.y); |
|---|
| 210 | if (n != 4) { |
|---|
| 211 | put_msg("Incorrect compound object format"); |
|---|
| 212 | return(NULL); |
|---|
| 213 | } |
|---|
| 214 | while (fscanf(fp, "%d", &object) == 1) { |
|---|
| 215 | switch (object) { |
|---|
| 216 | case O_POLYLINE : |
|---|
| 217 | if ((l = read_lineobject(fp)) == NULL) { |
|---|
| 218 | free_line(&l); |
|---|
| 219 | return(NULL); |
|---|
| 220 | } |
|---|
| 221 | if (ll) |
|---|
| 222 | ll = (ll->next = l); |
|---|
| 223 | else |
|---|
| 224 | ll = com->lines = l; |
|---|
| 225 | break; |
|---|
| 226 | case O_SPLINE : |
|---|
| 227 | if ((s = read_splineobject(fp)) == NULL) { |
|---|
| 228 | free_spline(&s); |
|---|
| 229 | return(NULL); |
|---|
| 230 | } |
|---|
| 231 | if (ls) |
|---|
| 232 | ls = (ls->next = s); |
|---|
| 233 | else |
|---|
| 234 | ls = com->splines = s; |
|---|
| 235 | break; |
|---|
| 236 | case O_ELLIPSE : |
|---|
| 237 | if ((e = read_ellipseobject(fp)) == NULL) { |
|---|
| 238 | free_ellipse(&e); |
|---|
| 239 | return(NULL); |
|---|
| 240 | } |
|---|
| 241 | if (le) |
|---|
| 242 | le = (le->next = e); |
|---|
| 243 | else |
|---|
| 244 | le = com->ellipses = e; |
|---|
| 245 | break; |
|---|
| 246 | case O_ARC : |
|---|
| 247 | if ((a = read_arcobject(fp)) == NULL) { |
|---|
| 248 | free_arc(&a); |
|---|
| 249 | return(NULL); |
|---|
| 250 | } |
|---|
| 251 | if (la) |
|---|
| 252 | la = (la->next = a); |
|---|
| 253 | else |
|---|
| 254 | la = com->arcs = a; |
|---|
| 255 | break; |
|---|
| 256 | case O_TEXT : |
|---|
| 257 | if ((t = read_textobject(fp)) == NULL) { |
|---|
| 258 | free_text(&t); |
|---|
| 259 | return(NULL); |
|---|
| 260 | } |
|---|
| 261 | if (lt) |
|---|
| 262 | lt = (lt->next = t); |
|---|
| 263 | else |
|---|
| 264 | lt = com->texts = t; |
|---|
| 265 | break; |
|---|
| 266 | case O_COMPOUND : |
|---|
| 267 | if ((c = read_compoundobject(fp)) == NULL) { |
|---|
| 268 | free_compound(&c); |
|---|
| 269 | return(NULL); |
|---|
| 270 | } |
|---|
| 271 | if (lc) |
|---|
| 272 | lc = (lc->next = c); |
|---|
| 273 | else |
|---|
| 274 | lc = com->compounds = c; |
|---|
| 275 | break; |
|---|
| 276 | case O_END_COMPOUND : |
|---|
| 277 | return(com); |
|---|
| 278 | } /* switch */ |
|---|
| 279 | } |
|---|
| 280 | if (feof(fp)) |
|---|
| 281 | return(com); |
|---|
| 282 | else { |
|---|
| 283 | put_msg("Format error: %s", sys_errlist[errno]); |
|---|
| 284 | return(NULL); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | static F_ellipse * |
|---|
| 289 | read_ellipseobject(fp) |
|---|
| 290 | FILE *fp; |
|---|
| 291 | { |
|---|
| 292 | F_ellipse *e; |
|---|
| 293 | int n, t; |
|---|
| 294 | |
|---|
| 295 | Ellipse_malloc(e); |
|---|
| 296 | e->color = BLACK_COLOR; |
|---|
| 297 | e->angle = 0.0; |
|---|
| 298 | e->depth = 0; |
|---|
| 299 | e->pen = 0; |
|---|
| 300 | e->area_fill = 0; |
|---|
| 301 | e->next = NULL; |
|---|
| 302 | n = fscanf(fp," %d %d %d %lf %d %d %d %d %d %d %d %d %d\n", |
|---|
| 303 | &t, &e->style, |
|---|
| 304 | &e->thickness, &e->style_val, &e->direction, |
|---|
| 305 | &e->center.x, &e->center.y, |
|---|
| 306 | &e->radiuses.x, &e->radiuses.y, |
|---|
| 307 | &e->start.x, &e->start.y, |
|---|
| 308 | &e->end.x, &e->end.y); |
|---|
| 309 | if (n != 13) { |
|---|
| 310 | put_msg("incomplete ellipse data"); |
|---|
| 311 | free((char*)e); |
|---|
| 312 | return(NULL); |
|---|
| 313 | } |
|---|
| 314 | if (t == DRAW_ELLIPSE_BY_RAD) |
|---|
| 315 | e->type = T_ELLIPSE_BY_RAD; |
|---|
| 316 | else if (t == DRAW_ELLIPSE_BY_DIA) |
|---|
| 317 | e->type = T_ELLIPSE_BY_DIA; |
|---|
| 318 | else if (t == DRAW_CIRCLE_BY_RAD) |
|---|
| 319 | e->type = T_CIRCLE_BY_RAD; |
|---|
| 320 | else |
|---|
| 321 | e->type = T_CIRCLE_BY_DIA; |
|---|
| 322 | return(e); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | static F_line * |
|---|
| 326 | read_lineobject(fp) |
|---|
| 327 | FILE *fp; |
|---|
| 328 | { |
|---|
| 329 | F_line *l; |
|---|
| 330 | F_point *p, *q; |
|---|
| 331 | int f, b, h, w, n, t, x, y; |
|---|
| 332 | |
|---|
| 333 | Line_malloc(l); |
|---|
| 334 | l->color = BLACK_COLOR; |
|---|
| 335 | l->depth = 0; |
|---|
| 336 | l->pen = 0; |
|---|
| 337 | l->area_fill = 0; |
|---|
| 338 | l->for_arrow = NULL; |
|---|
| 339 | l->back_arrow = NULL; |
|---|
| 340 | l->next = NULL; |
|---|
| 341 | l->points = Point_malloc(p); |
|---|
| 342 | n = fscanf(fp, " %d %d %d %lf %d %d %d %d %d %d", &t, |
|---|
| 343 | &l->style, &l->thickness, &l->style_val, |
|---|
| 344 | &f, &b, &h, &w, &p->x, &p->y); |
|---|
| 345 | if (n != 10) { |
|---|
| 346 | put_msg("incomplete line data"); |
|---|
| 347 | free((char*)l); |
|---|
| 348 | return(NULL); |
|---|
| 349 | } |
|---|
| 350 | if (t == DRAW_POLYLINE) |
|---|
| 351 | l->type = T_POLYLINE; |
|---|
| 352 | else if (t == DRAW_POLYGON) |
|---|
| 353 | l->type = T_POLYGON; |
|---|
| 354 | else |
|---|
| 355 | l->type = T_BOX; |
|---|
| 356 | if (f) { |
|---|
| 357 | l->for_arrow = forward_arrow(); |
|---|
| 358 | l->for_arrow->wid = w; |
|---|
| 359 | l->for_arrow->ht = h; |
|---|
| 360 | } |
|---|
| 361 | if (b) { |
|---|
| 362 | l->back_arrow = backward_arrow(); |
|---|
| 363 | l->back_arrow->wid = w; |
|---|
| 364 | l->back_arrow->ht = h; |
|---|
| 365 | } |
|---|
| 366 | for (;;) { |
|---|
| 367 | if (fscanf(fp, " %d %d", &x, &y) != 2) { |
|---|
| 368 | put_msg("incomplete line object"); |
|---|
| 369 | free_linestorage(l); |
|---|
| 370 | return(NULL); |
|---|
| 371 | } |
|---|
| 372 | if (x == 9999) break; |
|---|
| 373 | Point_malloc(q); |
|---|
| 374 | q->x = x; |
|---|
| 375 | q->y = y; |
|---|
| 376 | q->next = NULL; |
|---|
| 377 | p->next = q; |
|---|
| 378 | p = q; |
|---|
| 379 | } |
|---|
| 380 | return(l); |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | static F_spline * |
|---|
| 384 | read_splineobject(fp) |
|---|
| 385 | FILE *fp; |
|---|
| 386 | { |
|---|
| 387 | F_spline *s; |
|---|
| 388 | F_point *p, *q; |
|---|
| 389 | int f, b, h, w, n, t, x, y; |
|---|
| 390 | |
|---|
| 391 | Spline_malloc(s); |
|---|
| 392 | s->color = BLACK_COLOR; |
|---|
| 393 | s->depth = 0; |
|---|
| 394 | s->pen = 0; |
|---|
| 395 | s->area_fill = 0; |
|---|
| 396 | s->for_arrow = NULL; |
|---|
| 397 | s->back_arrow = NULL; |
|---|
| 398 | s->controls = NULL; |
|---|
| 399 | s->next = NULL; |
|---|
| 400 | s->points = Point_malloc(p); |
|---|
| 401 | n = fscanf(fp, " %d %d %d %lf %d %d %d %d %d %d", |
|---|
| 402 | &t, &s->style, &s->thickness, &s->style_val, |
|---|
| 403 | &f, &b, |
|---|
| 404 | &h, &w, &p->x, &p->y); |
|---|
| 405 | if (n != 10) { |
|---|
| 406 | put_msg("incomplete spline data"); |
|---|
| 407 | free((char*)s); |
|---|
| 408 | return(NULL); |
|---|
| 409 | } |
|---|
| 410 | if (t == DRAW_CLOSEDSPLINE) |
|---|
| 411 | s->type = T_CLOSED_NORMAL; |
|---|
| 412 | else |
|---|
| 413 | s->type = T_OPEN_NORMAL; |
|---|
| 414 | if (f) { |
|---|
| 415 | s->for_arrow = forward_arrow(); |
|---|
| 416 | s->for_arrow->wid = w; |
|---|
| 417 | s->for_arrow->ht = h; |
|---|
| 418 | } |
|---|
| 419 | if (b) { |
|---|
| 420 | s->back_arrow = backward_arrow(); |
|---|
| 421 | s->back_arrow->wid = w; |
|---|
| 422 | s->back_arrow->ht = h; |
|---|
| 423 | } |
|---|
| 424 | for (;;) { |
|---|
| 425 | if (fscanf(fp, " %d %d", &x, &y) != 2) { |
|---|
| 426 | put_msg("incomplete spline object"); |
|---|
| 427 | free_splinestorage(s); |
|---|
| 428 | return(NULL); |
|---|
| 429 | }; |
|---|
| 430 | if (x == 9999) break; |
|---|
| 431 | Point_malloc(q); |
|---|
| 432 | q->x = x; |
|---|
| 433 | q->y = y; |
|---|
| 434 | q->next = NULL; |
|---|
| 435 | p->next = q; |
|---|
| 436 | p = q; |
|---|
| 437 | } |
|---|
| 438 | return(s); |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | static F_text * |
|---|
| 442 | read_textobject(fp) |
|---|
| 443 | FILE *fp; |
|---|
| 444 | { |
|---|
| 445 | F_text *t; |
|---|
| 446 | int n; |
|---|
| 447 | char buf[128]; |
|---|
| 448 | |
|---|
| 449 | Text_malloc(t); |
|---|
| 450 | t->type = T_LEFT_JUSTIFIED; |
|---|
| 451 | t->flags = 0; |
|---|
| 452 | t->color = BLACK_COLOR; |
|---|
| 453 | t->depth = 0; |
|---|
| 454 | t->pen = 0; |
|---|
| 455 | t->angle = 0.0; |
|---|
| 456 | t->next = NULL; |
|---|
| 457 | n = fscanf(fp," %d %d %d %d %d %d %d %[^\n]", &t->font, |
|---|
| 458 | &t->size, &t->flags, &t->height, &t->length, |
|---|
| 459 | &t->base_x, &t->base_y, buf); |
|---|
| 460 | if (n != 8) { |
|---|
| 461 | put_msg("incomplete text data"); |
|---|
| 462 | free((char*)t); |
|---|
| 463 | return(NULL); |
|---|
| 464 | } |
|---|
| 465 | t->cstring = (char *) calloc((unsigned)(strlen(buf)+1), sizeof(char)); |
|---|
| 466 | if (t->cstring == NULL) { |
|---|
| 467 | put_msg(Err_mem); |
|---|
| 468 | free((char*) t); |
|---|
| 469 | return(NULL); |
|---|
| 470 | } |
|---|
| 471 | (void)strcpy(t->cstring, buf); |
|---|
| 472 | if (t->size == 0) t->size = 18; |
|---|
| 473 | return(t); |
|---|
| 474 | } |
|---|