Debian Codegraph @

texk/dvipsk/hps.c

raw
1 /* This is the main file for hacking dvips to do HyperPostScript
2 * Written by Mark D. Doyle 11/94. It is (C) Copyright 1994 by Mark D. Doyle
3 * and the University of California. You may modify and use this program to
4 * your heart's content.
5 */
6 #include "dvips.h"
7
8 /*
9 * The external declarations:
10 */
11 #include "protos.h"
12
13 #ifdef HPS
14 #include <stdlib.h>
15 #ifdef KPATHSEA
16 #include <kpathsea/c-ctype.h>
17 #else
18 #define TOLOWER Tolower
19 #endif
20 #ifndef FALSE
21 #define FALSE 0
22 #endif
23 #ifndef TRUE
24 #define TRUE 1
25 #endif
26 #ifndef Tolower
27 #define Tolower tolower
28 #endif
29 #define NAME 0
30 #define HREF 1
31 #define skip_space(s) for(; *s == ' '; s++);
32
33 Boolean inHTMLregion = 0;
34 Boolean NO_ERROR = 1;
35 Boolean HPS_ERROR = 0;
36 integer HREF_COUNT;
37 Boolean ISHREF = 0;
38 Boolean POPPED = 0;
39 Boolean PUSHED = 0;
40
41 char *current_name;
42 int current_type;
43 int current_pushcount;
44
45
46 #define GoTo 0
47 #define GoToR 1
48 #define Launch 2
49
50 typedef struct rectangle {
51 double llx; /* lower left x coor */
52 double lly; /* lower left y coor */
53 double urx; /* upper right x coor */
54 double ury; /* upper right y coor */
55 } dvipsRectangle;
56
57 typedef struct hps_link {
58 int action; /* GoTo, GoToR, or Launch */
59 char *file; /* PDF-style system-independent filename - unused now */
60 dvipsRectangle rect; /* rectangle for location of link */
61 int border[5]; /* Border info --- with dashes*/
62 int srcpg; /* Page of location */
63 int page; /* Page of destination --- left out is same as srcpg */
64 int vert_dest; /* Vertical position of destination on page */
65 double color[3]; /* color: length of array gives colorspace (3 <=> RGB)*/
66 char *title; /* Title of link */
67 } Hps_link;
68
69 struct nlist { /* hashtable entry */
70 struct nlist *next; /* next entry in chain */
71 char *name; /* defined name */
72 Hps_link *defn; /* Link associated with name */
73 };
74
75 typedef struct rect_list { /* linked list of rectangles */
76 struct rect_list *next; /* next rectangle in the chain */
77 dvipsRectangle rect;
78 } Rect_list;
79
80 Rect_list *current_rect_list;
81
82 #define HASHSIZE 1223
83 static struct nlist *link_targets[HASHSIZE]; /* names .... */
84 static struct nlist *link_sources[HASHSIZE]; /* hrefs .... */
85
86 #ifdef KPATHSEA
87 #define dup_str xstrdup
88 #else
89 char *dup_str(); /* duplicate a string */
90 #endif
91
92 Boolean noprocset; /* Leave out BeginProc and EndProc comments */
93 /* This was hardcoded to letter size. The denominator converts scaled
94 points to big points (65536*72.27/72). */
95 #undef PAGESIZE /* HP-UX 10 defines for itself. */
96 #define PAGESIZE ((int) (vpapersize/65781.76))
97 #define MARGIN 72
98 #define VERTICAL 0
99 #define HORIZONTAL 1
100 #define FUDGE 2.0
101
102 /* For later use
103 * static char *dest_key[9] = {"Fit", "FitB", "FitW", "FitH", "FitBH"
104 "FitR", "FitV", "FitBV", "XYZ"};
105 */
106
107 char *hs = NULL; /* html string to be handled */
108 char *url_name = NULL; /* url between double quotes */
109
110 static int href_or_name(void);
111 static int parseref(void);
112 static int get_string(void);
113 static int do_link(char *s, int type);
114 static struct nlist *lookup_link(char *s, int type);
115 static struct nlist *install_link(char *name, struct hps_link *defn, int type);
116 static Hps_link *link_dup(struct hps_link *s);
117 static double dvi_to_hps_conv(int i, int dir);
118 static int href_name_match(char *h, char *n);
119 static void stamp_hps(struct hps_link *pl);
120 static void stamp_external(char *s, struct hps_link *pl);
121 static void print_rect_list(void);
122
123 /* parse anchor into link */
124 void
125 do_html(char *s)
126 {
127 Hps_link *nl;
128 url_name = (char *)malloc(strlen(s)+1);
129 hs = s;
130 HREF_COUNT = 0;
131 skip_space(hs); /* skip spaces */
132 if ( TOLOWER(*hs) == 'a') { /* valid start */
133 POPPED = FALSE;
134 if (inHTMLregion) {
135 error("previous html not closed with /A");
136 return;
137 } else {
138 inHTMLregion = TRUE;
139 current_rect_list = NULL;
140 }
141 hs++;
142 skip_space(hs);
143 while(*hs != '\0') {
144 /* parse for names and href */
145 if (!href_or_name()) {
146 error("Bad HMTL:");
147 error(s);
148 error("!");
149 break;
150 }
151 skip_space(hs);
152 }
153 } else if ( (*hs == '/') && (TOLOWER(hs[1]) == 'a') ) {
154 if (!inHTMLregion) {
155 error("In html, /A found without previous A");
156 return;
157 } else {
158 inHTMLregion = FALSE;
159 }
160 if (current_type == HREF && current_name[0] != '#') {
161 if ((nl = lookup_link(current_name, current_type)->defn)) {
162 nl->rect.urx = dvi_to_hps_conv(hh, HORIZONTAL);
163 nl->rect.ury = dvi_to_hps_conv(vv, VERTICAL)-FUDGE+12.0;
164 stamp_external(current_name,nl); /* Broken lines ? */
165 } else {
166 error("!Null lookup");
167 }
168 } else {
169 if ((nl = lookup_link(current_name, current_type)->defn)) {
170 nl->rect.urx = dvi_to_hps_conv(hh, HORIZONTAL);
171 nl->rect.ury = dvi_to_hps_conv(vv, VERTICAL)-FUDGE+12.0;
172 if (current_type) {
173 stamp_hps(nl); /* Put link info right where special is */
174 print_rect_list(); /* print out rectangles */
175 }
176 } else {
177 error("!Null lookup");
178 }
179 }
180 }
181 else {
182 error( "No A in html special");
183 error(s);
184 /*error("!");*/
185 }
186
187 return;
188 }
189
190 static int
191 href_or_name(void) {
192 if ((strncmp(hs, "href", 4) == 0) || (strncmp(hs, "HREF", 4) == 0)) {
193 ISHREF = TRUE;
194 } else if ((strncmp(hs, "name", 4) == 0)
195 || (strncmp(hs, "NAME", 4) == 0)) {
196 ISHREF = FALSE;
197 } else {
198 error("Not valid href or name html reference");
199 return(HPS_ERROR);
200 }
201 if(!parseref()) return(HPS_ERROR);
202 if (url_name) {
203 if( ISHREF ) {
204 do_link(url_name, HREF);
205 } else do_link(url_name, NAME);
206 } else {
207 error("No string in qoutes ");
208 return(HPS_ERROR);
209 }
210 return(NO_ERROR);
211 }
212
213 static int
214 parseref(void) {
215 int i = 0;
216 for(i=0; i++ < 4; hs++); /* skip href or name in html string */
217 skip_space(hs);
218 if (*hs == '=' ) {
219 hs++;
220 } else {
221 error("No equal sign");
222 return(HPS_ERROR);
223 }
224 if(!get_string()) return(HPS_ERROR);
225 return(NO_ERROR); /* extract stuff between double quotes */
226 }
227
228 static int
229 get_string(void) {
230 char *v = url_name;
231
232 skip_space(hs);
233 /* hash_name(); */
234 if (*hs == '"') {
235 for(hs++; *hs != '"' && *hs != '\0'; *v++ = *hs++); /* need to esc " */
236 if(*hs == '"') {
237 hs++;
238 *v++ = '\0';
239 return(NO_ERROR);
240 } else {
241 error("Improper href. Missing closing double quote");
242 return(HPS_ERROR);
243 }
244 } else {
245 error("Improper href. Missing opening double quote");
246 return(HPS_ERROR);
247 }
248 }
249
250 static int
251 do_link(char *s, int type)
252 {
253
254 Hps_link *p;
255
256 if (HREF_COUNT++ > 0) {
257 error("!HTML string contains more than one href");
258 return(HPS_ERROR);
259 }
260 p = (Hps_link *)malloc(sizeof(*p));
261 p->action = GoTo;
262 p->title = (char *)malloc(strlen(s)+1);
263 p->title = s;
264 p->srcpg = pagecounter;
265 p->rect.llx = dvi_to_hps_conv(hh, HORIZONTAL);
266 p->rect.lly = dvi_to_hps_conv(vv, VERTICAL)-FUDGE;
267 p->rect.urx = -1.0;
268 p->rect.ury = -1.0;
269 p->vert_dest = -1;
270 p->page = -1;
271 p->color[0] = 0;
272 p->color[1] = 0; /* Blue links */
273 p->color[2] = 1;
274 p->border[0] = 1; /* horizontal corner radius */
275 p->border[1] = 1; /* vertical corner radius */
276 p->border[2] = 1; /* box line width */
277 p->border[3] = 3; /* dash on size */
278 p->border[4] = 3; /* dash off size */
279
280 current_name = (char *)malloc(strlen(s)+1);
281 current_name = s;
282 current_type = type;
283 current_pushcount = pushcount;
284 install_link(s, p, type);
285 return(NO_ERROR);
286 }
287
288 static unsigned int
289 hash_string(char *s)
290 {
291 unsigned hashval;
292 for (hashval = 0; *s != '\0'; s++)
293 hashval = *s + 31 * hashval;
294 return hashval % HASHSIZE;
295 }
296
297 /* lookup a hashed name */
298
299 static struct nlist *
300 lookup_link(char *s, int type)
301 {
302 struct nlist *np;
303
304 for(np = type ? link_sources[hash_string(s)] : link_targets[hash_string(s)];
305 np != NULL; np = np -> next)
306 if (strcmp(s, np->name) == 0)
307 return np; /* found */
308 return NULL; /* not found */
309 }
310
311 static struct nlist *
312 install_link(char *name, Hps_link *defn, int type)
313 {
314 struct nlist *np;
315 unsigned hashval;
316 np = (struct nlist *)malloc(sizeof(*np));
317 if (np == NULL || (np->name = dup_str(name)) == NULL)
318 return NULL;
319 hashval = hash_string(name);
320 np->next = type ? link_sources[hashval] : link_targets[hashval];
321 (type ? link_sources : link_targets)[hashval] = np;
322 if ((np->defn = link_dup(defn)) == NULL)
323 return NULL;
324 return np;
325 }
326
327 #ifndef KPATHSEA
328 char *
329 dup_str(char *w) /* make a duplicate of s */
330 {
331 char *p;
332 p = (char *)malloc(strlen(w)+1);
333 if (p != NULL)
334 strcpy(p,w);
335 return p;
336 }
337 #endif
338
339 static Hps_link *
340 link_dup(Hps_link *s) /* make a duplicate link */
341 {
342 Hps_link *p;
343
344 p = (Hps_link *) malloc(sizeof(*p));
345 if (p != NULL)
346 p = s;
347 return p;
348 }
349
350 static double
351 dvi_to_hps_conv(int i, int dir)
352 {
353 double hps_coor;
354 /* Convert dvi integers into proper hps coordinates
355 Take into account magnification and resolution that dvi file was
356 produced at */
357 hps_coor = dir ? (((i * 72.0) / vactualdpi) +MARGIN) : (PAGESIZE - ((i * 72.0) / (vactualdpi)) - MARGIN );
358 return(hps_coor);
359 }
360
361 static int
362 vert_loc(int i)
363 {
364 int return_value;
365 return_value = (int) (i + (PAGESIZE / 4) + FUDGE);
366 if ( return_value > PAGESIZE) {
367 return((int)PAGESIZE);
368 } else if (return_value < (PAGESIZE / 4.0)) {
369 return((PAGESIZE / 4));
370 } else return(return_value);
371 }
372
373 static Hps_link *
374 dest_link(char *s)
375 {
376 /* Assume for now that only one entry with same NAME, i.e.
377 Should be true for all legitimate papers.
378 Also, assume prepending of # for names. */
379
380 struct nlist *np;
381
382 s++; /* get rid of hashmark */
383 for(np = link_targets[hash_string(s)]; np != NULL; np = np -> next) {
384 if ( href_name_match(s, np->name)) {
385 return (np->defn); /* found */
386 }
387 }
388 error("Oh no, link not found in target hashtable");
389 error(s);
390 error("!");
391 return NULL; /* not found */
392 }
393
394 static int
395 count_targets(void) {
396 int count=0;
397 int i;
398 struct nlist *np;
399
400 for (i = 0; i < HASHSIZE; i++)
401 for(np = link_targets[i]; np != NULL; np = np -> next)
402 count++;
403 return count;
404 }
405
406 static void
407 do_targets(void) {
408
409 struct nlist *np;
410 int i;
411 Hps_link *dest;
412
413 for (i = 0; i < HASHSIZE; i++)
414 for(np = link_sources[i]; np != NULL; np = np -> next) {
415 if (np->name[0] == '#') {
416 dest = dest_link(np->name);
417 np->defn->page = dest->srcpg;
418 np->defn->vert_dest = vert_loc((int) dest->rect.lly);
419 }
420 }
421 }
422
423 static void
424 do_target_dict(void)
425 {
426 struct nlist *np;
427 int i;
428 (void) fprintf(bitfile, "HPSdict begin\n");
429 fprintf(bitfile, "/TargetAnchors\n");
430 fprintf(bitfile, "%i dict dup begin\n",count_targets());
431
432 for (i = 0; i < HASHSIZE; i++)
433 for(np = link_targets[i]; np != NULL; np = np -> next)
434 fprintf(bitfile, "(%s) [%i [%.0f %.0f %.0f %.0f] %i] def\n",
435 np->defn->title, np->defn->srcpg,
436 np->defn->rect.llx, np->defn->rect.lly,
437 np->defn->rect.urx, np->defn->rect.ury,
438 vert_loc((int) np->defn->rect.lly));
439 fprintf(bitfile,"end targetdump-hook def end\n");
440 }
441
442 static int
443 href_name_match(char *h, char *n)
444 {
445 int count = 0;
446 int name_length = strlen(n);
447 while((*h == *n) & (*h != '\0') ) {
448 count++;
449 h++;
450 n++;
451 }
452 if (name_length == count) {
453 return 1;
454 } else {
455 /* printf(" count: %i \n", count); */
456 return 0;
457 }
458 }
459
460 static void
461 stamp_hps(Hps_link *pl)
462 {
463 char *tmpbuf;
464 unsigned tmpsize;
465
466 if (pl == NULL) {
467 error("stamp_hps: null pl pointer, oh no!");
468 return;
469 }
470 if (pl->title == NULL) {
471 error("stamp_hps: null pl->title pointer, oh no!");
472 return;
473 }
474
475 tmpsize = strlen(pl->title) + 500;
476 tmpbuf = xmalloc(tmpsize);
477
478 /* print out the proper pdfm with local page info only
479 * target info will be in the target dictionary */
480 snprintf(tmpbuf, tmpsize,
481 " (%s) [[%.0f %.0f %.0f %.0f] [%i %i %i [%i %i]] [%.0f %.0f %.0f]] pdfm ",
482 pl->title, pl->rect.llx, pl->rect.lly, pl->rect.urx, pl->rect.ury,
483 pl->border[0], pl->border[1], pl->border[2], pl->border[3],pl->border[4],
484 pl->color[0], pl->color[1], pl->color[2]);
485 cmdout(tmpbuf);
486 free(tmpbuf);
487
488
489 }
490
491 /* For external URL's, we just pass them through as a string. The hyperps
492 * interpreter can then do what is wants with them.
493 */
494 static void
495 stamp_external(char *s, Hps_link *pl)
496 {
497 char *tmpbuf;
498 unsigned tmpsize;
499 if (pl == NULL) {
500 error("stamp_external: null pl pointer, oh no!");
501 return;
502 }
503 if (s == NULL) {
504 error("stamp_external: null s pointer, oh no!");
505 return;
506 }
507
508 tmpsize = strlen(s) + 500;
509 tmpbuf = xmalloc(tmpsize);
510
511 /* print out the proper pdfm with local page info only
512 * target info will be in the target dictionary */
513 snprintf(tmpbuf, tmpsize,
514 " [[%.0f %.0f %.0f %.0f] [%i %i %i [%i %i]] [%.0f %.0f %.0f]] (%s) pdfm ",
515 pl->rect.llx, pl->rect.lly, pl->rect.urx, pl->rect.ury,
516 pl->border[0], pl->border[1], pl->border[2], pl->border[3],pl->border[4],
517 pl->color[0], pl->color[1], pl->color[2], s);
518 cmdout(tmpbuf);
519 free(tmpbuf);
520 }
521
522 void
523 finish_hps(void) {
524 fclose(bitfile);
525 set_bitfile("head.tmp",1);
526 do_targets();
527 do_target_dict();
528 fprintf(bitfile, "TeXDict begin\n");
529 fprintf(bitfile, "%%%%EndSetup\n");
530 fclose(bitfile);
531 open_output();
532 noprocset = 1;
533 removecomments = 0;
534 copyfile("head.tmp");
535 copyfile("body.tmp");
536 if (dvips_debug_flag == 0 && debug_flag == 0) {
537 unlink("head.tmp");
538 unlink("body.tmp");
539 }
540 }
541
542 void
543 set_bitfile(const char *s, int mode)
544 {
545 if ((bitfile=fopen(s, mode ? FOPEN_ABIN_MODE : FOPEN_WBIN_MODE))==NULL) {
546 error(s);
547 error("!couldn't open file");
548 }
549 linepos = 0;
550 }
551
552 void
553 vertical_in_hps(void) {
554 Rect_list *rl;
555 /*printf("in vertical_in_hps"); */
556 if (current_type == NAME) return; /* Handle this case later */
557 if (!current_rect_list) {
558 current_rect_list = (Rect_list *)malloc(sizeof(Rect_list));
559 current_rect_list->next = NULL;
560 }
561 else {
562 rl = (Rect_list *)malloc(sizeof(Rect_list));
563 rl->next = current_rect_list;
564 current_rect_list = rl;
565 }
566 current_rect_list->rect.llx = dvi_to_hps_conv(hh, HORIZONTAL);
567 current_rect_list->rect.lly = dvi_to_hps_conv(vv, VERTICAL)-FUDGE;
568 current_rect_list->rect.urx = dvi_to_hps_conv(hhmem, HORIZONTAL);
569 current_rect_list->rect.ury = dvi_to_hps_conv(vvmem, VERTICAL)-FUDGE;
570
571 if (POPPED) start_new_box();
572 }
573
574 static void
575 print_rect_list(void) {
576 Rect_list *rl, *rln;
577
578 for(rl = current_rect_list; rl != NULL; rl = rln) {
579 /* printf("Rectangle is %.0f, %.0f, %.0f, %.0f\n", rl->rect.llx, rl->rect.lly,
580 rl->rect.urx, rl->rect.ury); */
581 rln = rl -> next;
582 free(rl);
583 }
584 }
585
586 void
587 end_current_box(void) {
588 Hps_link *nl;
589
590 POPPED = TRUE;
591 HREF_COUNT--;
592 if (current_type == HREF && current_name[0] != '#') {
593 if ((nl = lookup_link(current_name, current_type)->defn)) {
594 nl->rect.urx = dvi_to_hps_conv(hhmem, HORIZONTAL);
595 nl->rect.ury = dvi_to_hps_conv(vvmem, VERTICAL)-FUDGE+12.0;
596 stamp_external(current_name,nl); /* Broken lines ? */
597 } else {
598 error("!Null lookup");
599 }
600 } else {
601 if ((nl = lookup_link(current_name, current_type)->defn)) {
602 nl->rect.urx = dvi_to_hps_conv(hhmem, HORIZONTAL);
603 nl->rect.ury = dvi_to_hps_conv(vvmem, VERTICAL)-FUDGE+12.0;
604 if (current_type) {
605 stamp_hps(nl); /* Put link info right where special is */
606 }
607 } else {
608 error("!Null lookup");
609 }
610 /* printf("Ended box %.0f, %.0f, %.0f, %.0f\n", nl->rect.llx, \
611 nl->rect.lly,nl->rect.urx, nl->rect.ury); */
612 }
613 }
614
615 void
616 start_new_box(void) {
617 POPPED = FALSE;
618 do_link(current_name, current_type);
619 }
620 #else
621 int no_hypertex; /* prevent an empty file */
622 #endif