File: genmap.c

package info (click to toggle)
transfig 1%3A3.2.5.e-4%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 9,300 kB
  • sloc: ansic: 32,666; makefile: 3,255; sh: 549; csh: 25; perl: 13
file content (393 lines) | stat: -rwxr-xr-x 10,548 bytes parent folder | download | duplicates (2)
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
/*
 * TransFig: Facility for Translating Fig code
 * Copyright (c) 1999 by T. Sato
 * Parts Copyright (c) 1989-2002 by Brian V. Smith
 *
 * Any party obtaining a copy of these files is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish and/or distribute copies of
 * the Software, and to permit persons who receive copies from any such 
 * party to do so, with the only requirement being that this copyright 
 * notice remain intact.
 *
 */

/*
 * genmap.c: HTML imagemap driver for fig2dev
 *
 * T.Sato <VEF00200@nifty.ne.jp>
 *
 *
 * Description:
 *   Generate HTML imagemap from Fig file.
 *   If an object has comment like ``HREF="url" ALT="string"'',
 *   the object will become a link to the URL.
 *   Object with smaller depth will be listed before deeper ones.
 *   Figure comment will be used as the default link.
 */

#include "fig2dev.h"
#include "object.h"

#define TEXT_LENGTH  300

static int      border_margin = 0;
static char     url[TEXT_LENGTH], alt[TEXT_LENGTH];
static char     buf[2000];

struct hlink_item {
  char *url;
  char *alt;
  char *area;
  struct hlink_item* prev;
};
typedef struct hlink_item hlink;

/* The last link that was added. */
static hlink* last_link = 0;


#define fscale 15

#define XZOOM(x)   round(mag * ((x) - llx) / fscale + border_margin)
#define YZOOM(y)   round(mag * ((y) - lly) / fscale + border_margin)

#define MIN_DIST    5  /* ignore closer points when processing polyline */

#define ARC_STEP    (M_PI / 9.0)
#define ARC_EXPAND  1.02   /* make polygon slightly larger */


void
genmap_option(opt, optarg)
     char opt;
     char *optarg;
{
  switch (opt) {
  case 'b':     /* border margin */
    sscanf(optarg,"%d",&border_margin);
    break;
  case 'L':     /* ignore language and magnif. */
  case 'm':
    break;
  default:
    put_msg(Err_badarg, opt, "map");
    exit(1);
  }
}

static char *
is_link(comment)
     F_comment  *comment;
{
  char *cp1, *cp2;
  Boolean have_alt = False;
  int i;

  url[0] = '\0';
  alt[0] = '\0';

  while (comment != NULL) {
    cp1 = comment->comment;
    while (isspace(*cp1)) cp1++;  /* ignore leading spaces */

    if (strncasecmp(cp1, "HREF=", 5) == 0) {
      /* comment like "HREF=url ALT=string" */

      cp2 = cp1 + 5;
      while (isspace(*cp2)) cp2++;  /* spaces */
      i = 0;
      while (isgraph(*cp2)) url[i++] = *(cp2++);  /* URL */
      url[i] = '\0';
      while (isspace(*cp2)) cp2++;  /* spaces */
      if (*cp2 != '\0') {
        if (strncasecmp(cp2, "ALT=", 4) == 0) {
          have_alt = True;
	  cp2 = cp2 + 4;
	  while (isspace(*cp2)) cp2++;  /* spaces */
	  i = 0;
	  if (*cp2 == '"') {
	    cp2++;
	    while (*cp2 != '\0' && *cp2 != '"') alt[i++] = *(cp2++);  /* text */
	  } else {
	    while (isgraph(*cp2)) alt[i++] = *(cp2++);  /* text */
	  }
	  alt[i] = '\0';
        } else {
          fprintf(stderr, "fig2dev(map): unknown attribute: %s\n", cp2);
        }
      } 
      if (!have_alt)
        fprintf(stderr, "fig2dev(map): ALT is required in HTML 3.2: %s\n", cp1);
      return cp1;
    }
    comment = comment->next;
  }
  return NULL;
}

void
add_link(char *area)
{
    hlink* hl = (hlink*)malloc(sizeof(hlink));
    hl->url = (char *)malloc(strlen(url) + 1);
    strcpy(hl->url, url);
    hl->alt = (char *)malloc(strlen(alt) + 1);
    strcpy(hl->alt, alt);
    hl->area = (char *)malloc(strlen(area) + 1);
    strcpy(hl->area, area);
    hl->prev = last_link;
    last_link=hl;
}

void
genmap_start(objects)
     F_compound *objects;
{
  char *basename;
  char *ref;
  int i;

  if (to != NULL) {
    basename = (char *)malloc(strlen(to) + 1);
    strcpy(basename, to);
    for (i = strlen(basename) - 1; 0 < i; i--) {
      if (basename[i] == '.') {
        basename[i] = '\0';
        break;
      }
    }
  } else {
    basename = "imagemap";
  }

  fprintf(tfp, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Frameset//EN\">\n");
  fprintf(tfp, "<HTML>\n");
  fprintf(tfp, "<HEAD><TITLE>HTML imagemap generated by fig2dev</TITLE></HEAD>\n");
  fprintf(tfp, "<BODY>\n\n");
  fprintf(tfp, "<H1>HTML imagemap generated by fig2dev</H1>\n");
  fprintf(tfp, "<P>This file is generated by fig2dev.</P>\n");
  fprintf(tfp, "<P>You can copy the following lines into your HTML document.\n");
  fprintf(tfp, "You may need to edit the name of the image file in the first line.</P>\n");
  fprintf(tfp, "\n");
  fprintf(tfp, "<IMG SRC=\"%s.png\" USEMAP=\"#%s\">\n", basename, basename);
  fprintf(tfp, "<MAP NAME=\"%s\">\n", basename);

  ref = is_link(objects->comments);
  if (ref != NULL) {
    sprintf(buf, "<AREA COORDS=\"%d,%d,%d,%d\" %s>\n",
            XZOOM(llx), YZOOM(lly), XZOOM(urx), YZOOM(ury), ref);
    add_link(buf);
  }
}

int
genmap_end()
{
  hlink* l;
  int len;
  char label[TEXT_LENGTH];

  for (l = last_link; l!= 0; l=l->prev) {
    fprintf(tfp, "%s", l->area);
  }
  fprintf(tfp, "</MAP>\n");

  fprintf(tfp, "\n");
  fprintf(tfp, "<h2>Alternative Text Links</h2>\n");
  fprintf(tfp, "<P>Because not all people can use imagemaps,\n");
  fprintf(tfp, "it is recommended to prepare an alternative way for the people\n");
  fprintf(tfp, "who can't use or don't want to use them.</P>\n");
  fprintf(tfp, "<P>Here are text the links extracted from the above imagemap.\n");
  fprintf(tfp, "If the ALT attribute (it is required in HTML 3.2) was not specified\n");
  fprintf(tfp, "in the imagemap, those URLs will be displayed\n");
  fprintf(tfp, "instead of labels specified by the ALT attributes.</P>\n");
  fprintf(tfp, "\n");
  fprintf(tfp, "<P ALIGN=\"CENTER\">\n");
  len = 0;
  for (l = last_link; l!= 0; l=l->prev) {
    if (l->alt[0] != '\0') strcpy(label, l->alt);
    else sprintf(label, "<TT>%s</TT>", l->url);
    fprintf(tfp, "%s<A HREF=%s>%s</A>\n",
	    (len == 0) ? "" : " | ", l->url, label);
	    
    len = len + strlen(label) + 3;
    if (50 < len) {
      fprintf(tfp, "<BR>\n");
      len = 0;
    }
  }
  fprintf(tfp, "</P>\n");

  fprintf(tfp, "\n");
  fprintf(tfp, "</BODY>\n");
  fprintf(tfp, "</HTML>\n");

  /* all ok */
  return 0;
}

void
genmap_arc(a)
     F_arc      *a;
{
  char *ref;
  int cx, cy, sx, sy, ex, ey;
  double r;
  double sa, ea;
  double alpha;

  ref = is_link(a->comments);
  if (ref != NULL) {
    cx = XZOOM(a->center.x);
    cy = YZOOM(a->center.y);
    sx = XZOOM(a->point[0].x);
    sy = YZOOM(a->point[0].y);
    ex = XZOOM(a->point[2].x);
    ey = YZOOM(a->point[2].y);

    if (a->direction == 0) {
      sa = atan2((double)(sy - cy), (double)(sx - cx));
      ea = atan2((double)(ey - cy), (double)(ex - cx));
    } else {
      ea = atan2((double)(sy - cy), (double)(sx - cx));
      sa = atan2((double)(ey - cy), (double)(ex - cx));
    }
    if (ea < sa) ea = ea + 2 * M_PI;

    r = sqrt((double)((sx - cx) * (sx - cx)
		      + (sy - cy) * (sy - cy))) * ARC_EXPAND + 1.0;

    sprintf(buf, "<AREA SHAPE=\"poly\" COORDS=\"");
    if (a->type == T_PIE_WEDGE_ARC)
      sprintf(&buf[strlen(buf)], "%d,%d,", cx, cy);
    for (alpha = sa; alpha < (ea - ARC_STEP / 4); alpha += ARC_STEP) {
      if (alpha != sa) sprintf(&buf[strlen(buf)], ",");
      sprintf(&buf[strlen(buf)], "%d,%d",
	      round(cx + r * cos(alpha)), round(cy + r * sin(alpha)));
    }
    sprintf(&buf[strlen(buf)], ",%d,%d",
	    round(cx + r * cos(ea)), round(cy + r * sin(ea)));
    sprintf(&buf[strlen(buf)], "\" %s>\n", ref);
    add_link(buf);
  }
}

void
genmap_ellipse(e)
     F_ellipse  *e;
{
  char *ref;
  int x0, y0;
  double rx, ry;
  double angle, theta;

  ref = is_link(e->comments);
  if (ref != NULL) {
    x0 = XZOOM(e->center.x);
    y0 = YZOOM(e->center.y);
    rx = mag * e->radiuses.x / fscale;
    ry = mag * e->radiuses.y / fscale;
    angle = - e->angle;
    if (e->radiuses.x == e->radiuses.y) {
      sprintf(buf, "<AREA SHAPE=\"circle\" COORDS=\"%d,%d,%d\" %s>\n",
	      x0, y0, round(rx) + 1, ref);
      add_link(buf);
    } else {
      rx = rx * ARC_EXPAND + 1.0;
      ry = ry * ARC_EXPAND + 1.0;
      sprintf(buf, "<AREA SHAPE=\"poly\" COORDS=\"");
      for (theta = 0.0; theta < 2.0 * M_PI; theta += ARC_STEP) {
	if (theta != 0.0) sprintf(&buf[strlen(buf)], ",");
	sprintf(&buf[strlen(buf)], "%d,%d",
		round(x0 + cos(angle) * rx * cos(theta)
		      - sin(angle) * ry * sin(theta)),
		round(y0 + sin(angle) * rx * cos(theta)
		      + cos(angle) * ry * sin(theta)));
      }
      sprintf(&buf[strlen(buf)], "\" %s>\n", ref);
      add_link(buf);
    }
  }
}

void
genmap_line(l)
     F_line     *l;
{
  char *ref;
  int xmin, xmax, ymin, ymax;
  int x, y, last_x, last_y;
  F_point *p;

  ref = is_link(l->comments);
  if (ref != NULL) {
    switch (l->type) {
    case T_BOX:
    case T_ARC_BOX:
    case T_PIC_BOX:
      xmin = xmax = l->points->x;
      ymin = ymax = l->points->y;
      for (p = l->points->next; p != NULL; p = p->next) {
        if (p->x < xmin) xmin = p->x;
        if (xmax < p->x) xmax = p->x;
        if (p->y < ymin) ymin = p->y;
        if (ymax < p->y) ymax = p->y;
      }
      sprintf(buf, "<AREA COORDS=\"%d,%d,%d,%d\" %s>\n",
              XZOOM(xmin), YZOOM(ymin), XZOOM(xmax), YZOOM(ymax), ref);
      add_link(buf);
      break;
    case T_POLYLINE:
    case T_POLYGON:
      sprintf(buf, "<AREA SHAPE=\"poly\" COORDS=\"");
      for (p = l->points; (l->type==T_POLYLINE? p: p->next) != NULL; p = p->next) {
        x = XZOOM(p->x);
        y = YZOOM(p->y);
        if (p == l->points
            || MIN_DIST < abs(last_x - x) || MIN_DIST < abs(last_y - y)) {
          if (sizeof(buf) < strlen(buf) + 100) {
            fprintf(stderr, "fig2dev(map): too complex POLYLINE... ignored\n");
            return;
          } else {
            if (p != l->points) sprintf(&buf[strlen(buf)], ",");
            sprintf(&buf[strlen(buf)], "%d,%d", x, y);
            last_x = x;
            last_y = y;
          }
        }
      }
      sprintf(&buf[strlen(buf)], "\" %s>\n", ref);
      add_link(buf);
      break;
    }
  }
}

void
genmap_text(t)
     F_text     *t;
{
  char *ref;

  ref = is_link(t->comments);
  if (ref != NULL) {
    fprintf(stderr, "fig2dev(map): TEXT can't be used as link region\n");
  }
}

struct driver dev_map = {
        genmap_option,
        genmap_start,
	gendev_null,
        genmap_arc,
        genmap_ellipse,
        genmap_line,
        gendev_null,
        genmap_text,
        genmap_end,
        INCLUDE_TEXT
};