Debian Codegraph @

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