summaryrefslogtreecommitdiff
path: root/support/hypertex/tanmoy/ghostview-1.5-hacked/pdf.c
blob: c50ae4baa383e41e976e98dec3952a103c5ca04c (plain)
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
/* This file is part of the hacked version of the ghostview package */
/* which is distributed under the terms of the gnu license. The */
/* modification referred to above is by Tanmoy Bhattacharya, */
/* <tanmoy@qcd.lanl.gov> on Nov 17, 1994. Neither the modification, */
/* nor the original program provides any warranty. */
#include <string.h>
#include <ctype.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include "pdf.h"
#ifdef NOMEMMOVE
#define memmove memcpy
#endif
static struct tagtable {
  double xmin, ymin, xmax, ymax;
  struct tagtable *next;
  int page;
} *tagstart=NULL, *tagcurrent=NULL;
void pdf_process(char buf[])
{char *pointer=buf;
 while (*pointer&&(pointer=strstr(pointer,"\n%%[pdfinfo:\n"))) {
   char *rectptr=strstr(pointer,"/Rect ");
   char *pageptr=strstr(pointer,"/Page ");
   char *endptr=strstr(pointer,"%%]\n");
   char *lnkptr=strstr(pointer,"\n[/LNK ");
   if (lnkptr && lnkptr<endptr && rectptr) {
     rectptr += strlen("/Rect ");
     while (isspace(*rectptr)) rectptr++;

     if (*rectptr++ == '[') {
       if(!tagstart) {
	 tagcurrent = tagstart = malloc(sizeof(struct tagtable));
	 if(!tagcurrent) {
           strcat(buf,"\nGhostview: Unable to malloc!\n");
	   } }
       else if (tagcurrent) {
	 tagcurrent->next = malloc(sizeof(struct tagtable));
	 tagcurrent = tagcurrent->next;}

       if(!tagcurrent) {
	 strcat(buf,"\nGhostview: Something seriously wrong!\n");
	 }
       else {
	 tagcurrent->next = NULL;
	 if (pageptr) {
	   pageptr += strlen("/Page ");
	   if(sscanf(pageptr,"%d",&tagcurrent->page) != 1) {
	     strcat(buf,"\nGhostview: Incorrect page specification\n");
	     tagcurrent->page = -1;
	   }
	   else tagcurrent->page--;
	 }
	 else
	   tagcurrent->page = -1;
	 if (sscanf(rectptr,"%lf %lf %lf %lf",&tagcurrent->xmin,
		    &tagcurrent->ymin, &tagcurrent->xmax, &tagcurrent->ymax)
	     != 4) {
	   strcat(buf,"\nGhostview: Incorrect rect specification\n");
	   fprintf(stderr,"error: %s\n",rectptr);
	   tagcurrent->xmin=0;
	   tagcurrent->ymin=0;
	   tagcurrent->xmax=0;
	   tagcurrent->ymax=0;
	 }
       } }
   }
   if (endptr) {
     endptr += strlen("%%]\n");
     memmove(pointer,endptr,strlen(endptr)+1);
   }
   else pointer++;
 }
}

void pdf_clear(void)
{tagcurrent=tagstart=NULL;}

int pdf_page(int x, int y)
{struct tagtable *pointer=tagstart;
 while(tagcurrent&&pointer){
   if (x<=pointer->xmax && x >=pointer->xmin &&
       y<=pointer->ymax && y >=pointer->ymin)
     return pointer->page;
   pointer = pointer->next;
 }
 return -1;
}