Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sp-epsf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2000, Matias Atria
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 /* postscript specials */
20 
21 #include <config.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 
27 #include "mdvi.h"
28 #include "private.h"
29 
30 typedef struct {
31  double ox;
32  double oy;
33  double bw;
34  double bh;
35  double angle;
36 } EpsfBox;
37 
38 #define LLX 0
39 #define LLY 1
40 #define URX 2
41 #define URY 3
42 #define RWI 4
43 #define RHI 5
44 #define HOFF 6
45 #define VOFF 7
46 #define HSIZE 8
47 #define VSIZE 9
48 #define HSCALE 10
49 #define VSCALE 11
50 #define ANGLE 12
51 #define CLIP 13
52 
53 void epsf_special __PROTO((DviContext *dvi, char *prefix, char *arg));
54 
55 /* Note: the given strings are modified in place */
56 static char *parse_epsf_special(EpsfBox *box, char **ret,
57  char *prefix, char *arg)
58 {
59  static struct {
60  char *name;
61  int has_arg;
62  char *value;
63  } keys[] = {
64  {"llx", 1, "0"},
65  {"lly", 1, "0"},
66  {"urx", 1, "0"},
67  {"ury", 1, "0"},
68  {"rwi", 1, "0"},
69  {"rhi", 1, "0"},
70  {"hoffset", 1, "0"},
71  {"voffset", 1, "0"},
72  {"hsize", 1, "612"},
73  {"vsize", 1, "792"},
74  {"hscale", 1, "100"},
75  {"vscale", 1, "100"},
76  {"angle", 1, "0"},
77  {"clip", 0, "0"}
78  };
79 #define NKEYS (sizeof(keys) / sizeof(keys[0]))
80  char *ptr;
81  char *filename;
82  double value[NKEYS];
83  Uchar present[NKEYS];
84  Buffer buffer;
85  char *name;
86  int i;
87  double originx;
88  double originy;
89  double hsize;
90  double vsize;
91  double hscale;
92  double vscale;
93 
94  /* this special has the form
95  * ["]file.ps["] [key=valye]*
96  */
97 
98  /* scan the filename */
99  while(*arg == ' ' || *arg == '\t')
100  arg++;
101 
102  /* make a copy of the string */
103  ptr = arg;
104 
105  if(*ptr == '"')
106  for(name = ++ptr; *ptr && *ptr != '"'; ptr++);
107  else
108  for(name = ptr; *ptr && *ptr != ' ' && *ptr != '\t'; ptr++);
109  if(ptr == name)
110  return NULL;
111  *ptr++ = 0;
112  filename = name;
113 
114  /* reset values to defaults */
115  for(i = 0; i < NKEYS; i++) {
116  value[i] = atof(keys[i].value);
117  present[i] = 0;
118  }
119 
120  buff_init(&buffer);
121  buff_add(&buffer, "@beginspecial ", 0);
122 
123  while(*ptr) {
124  const char *keyname;
125  char *val;
126  char *p;
127 
128  while(*ptr == ' ' || *ptr == '\t')
129  ptr++;
130  keyname = ptr;
131 
132  /* get the whole key=value pair */
133  for(; *ptr && *ptr != ' ' && *ptr != '\t'; ptr++);
134 
135  if(*ptr) *ptr++ = 0;
136  /* now we shouldn't touch `ptr' anymore */
137 
138  /* now work on this pair */
139  p = strchr(keyname, '=');
140  if(p == NULL)
141  val = NULL;
142  else {
143  *p++ = 0;
144  if(*p == '"') {
145  val = ++p;
146  /* skip until closing quote */
147  while(*p && *p != '"')
148  p++;
149  if(*p != '"')
150  mdvi_warning(
151  _("%s: malformed value for key `%s'\n"),
152  filename, keyname);
153  } else
154  val = p;
155  }
156 
157  /* lookup the key */
158  for(i = 0; i < NKEYS; i++)
159  if(STRCEQ(keys[i].name, keyname))
160  break;
161  if(i == NKEYS) {
162  mdvi_warning(_("%s: unknown key `%s' ignored\n"),
163  filename, keyname);
164  continue;
165  }
166  if(keys[i].has_arg && val == NULL) {
167  mdvi_warning(_("%s: no argument for key `%s', using defaults\n"),
168  filename, keyname);
169  val = keys[i].value;
170  } else if(!keys[i].has_arg && val) {
171  mdvi_warning(_("%s: argument `%s' ignored for key `%s'\n"),
172  filename, val, keyname);
173  val = NULL;
174  }
175  if(val)
176  value[i] = atof(val);
177 
178  /* put the argument */
179  buff_add(&buffer, val, 0);
180  buff_add(&buffer, " @", 2);
181  buff_add(&buffer, keyname, 0);
182  buff_add(&buffer, " ", 1);
183 
184  /* remember that this option was given */
185  present[i] = 0xff;
186  }
187  buff_add(&buffer, " @setspecial", 0);
188 
189  /* now compute the bounding box (code comes from dvips) */
190  originx = 0;
191  originy = 0;
192  hscale = 1;
193  vscale = 1;
194  hsize = 0;
195  vsize = 0;
196 
197  if(present[HSIZE])
198  hsize = value[HSIZE];
199  if(present[VSIZE])
200  vsize = value[VSIZE];
201  if(present[HOFF])
202  originx = value[HOFF];
203  if(present[VOFF])
204  originy = value[VOFF];
205  if(present[HSCALE])
206  hscale = value[HSCALE] / 100.0;
207  if(present[VSCALE])
208  vscale = value[VSCALE] / 100.0;
209  if(present[URX] && present[LLX])
210  hsize = value[URX] - value[LLX];
211  if(present[URY] && present[LLY])
212  vsize = value[URY] - value[LLY];
213  if(present[RWI] || present[RHI]) {
214  if(present[RWI] && !present[RHI])
215  hscale = vscale = value[RWI] / (10.0 * hsize);
216  else if(present[RHI] && !present[RWI])
217  hscale = vscale = value[RHI] / (10.0 * vsize);
218  else {
219  hscale = value[RWI] / (10.0 * hsize);
220  vscale = value[RHI] / (10.0 * vsize);
221  }
222  }
223 
224  box->ox = originx;
225  box->oy = originy;
226  box->bw = hsize * hscale;
227  box->bh = vsize * vscale;
228  box->angle = value[ANGLE];
229 
230  *ret = buffer.data;
231 
232  return filename;
233 }
234 
235 void epsf_special(DviContext *dvi, char *prefix, char *arg)
236 {
237  char *file;
238  char *special;
239  char *psfile;
240  char *tmp;
241  EpsfBox box = {0, 0, 0, 0};
242  int x, y;
243  int w, h;
244  double xf, vf;
245  struct stat buf;
246 
247  file = parse_epsf_special(&box, &special, prefix, arg);
248  if (file != NULL)
249  mdvi_free (special);
250 
251  xf = dvi->params.dpi * dvi->params.mag / (72.0 * dvi->params.hshrink);
252  vf = dvi->params.vdpi * dvi->params.mag / (72.0 * dvi->params.vshrink);
253  w = FROUND(box.bw * xf);
254  h = FROUND(box.bh * vf);
255  x = FROUND(box.ox * xf) + dvi->pos.hh;
256  y = FROUND(box.oy * vf) + dvi->pos.vv - h + 1;
257 
258  if (!file || !dvi->device.draw_ps) {
259  dvi->device.draw_rule (dvi, x, y, w, h, 0);
260  return;
261  }
262 
263  if (file[0] == '/') { /* Absolute path */
264  if (stat (file, &buf) == 0)
265  dvi->device.draw_ps (dvi, file, x, y, w, h);
266  else
267  dvi->device.draw_rule (dvi, x, y, w, h, 0);
268  return;
269  }
270 
271  tmp = mdvi_strrstr (dvi->filename, "/");
272  if (tmp) { /* Document directory */
273  int path_len = strlen (dvi->filename) - strlen (tmp + 1);
274  int file_len = strlen (file);
275 
276  psfile = mdvi_malloc (path_len + file_len + 1);
277  psfile[0] = '\0';
278  strncat (psfile, dvi->filename, path_len);
279  strncat (psfile, file, file_len);
280 
281  if (stat (psfile, &buf) == 0) {
282  dvi->device.draw_ps (dvi, psfile, x, y, w, h);
283  mdvi_free (psfile);
284 
285  return;
286  }
287 
288  mdvi_free (psfile);
289  }
290 
291  psfile = mdvi_build_path_from_cwd (file);
292  if (stat (psfile, &buf) == 0) { /* Current working dir */
293  dvi->device.draw_ps (dvi, psfile, x, y, w, h);
294  mdvi_free (psfile);
295 
296  return;
297  }
298 
299  mdvi_free (psfile);
300 
301  psfile = kpse_find_pict (file);
302  if (psfile) { /* kpse */
303  dvi->device.draw_ps (dvi, psfile, x, y, w, h);
304  } else {
305  dvi->device.draw_rule(dvi, x, y, w, h, 0);
306  }
307 
308  free (psfile);
309 }