2 * Copyright (C) 2000, Matias Atria
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /* postscript specials */
52 void epsf_special __PROTO((DviContext *dvi, char *prefix, char *arg));
54 /* Note: the given strings are modified in place */
55 static char *parse_epsf_special(EpsfBox *box, char **ret,
56 char *prefix, char *arg)
78 #define NKEYS (sizeof(keys) / sizeof(keys[0]))
94 /* this special has the form
95 * ["]file.ps["] [key=valye]*
98 /* scan the filename */
99 while(*arg == ' ' || *arg == '\t')
102 /* make a copy of the string */
106 for(name = ++ptr; *ptr && *ptr != '"'; ptr++);
108 for(name = ptr; *ptr && *ptr != ' ' && *ptr != '\t'; ptr++);
114 /* reset values to defaults */
115 for(i = 0; i < NKEYS; i++) {
116 value[i] = atof(keys[i].value);
121 buff_add(&buffer, "@beginspecial ", 0);
129 while(*ptr == ' ' || *ptr == '\t')
133 /* get the whole key=value pair */
134 for(; *ptr && *ptr != ' ' && *ptr != '\t'; ptr++);
137 /* now we shouldn't touch `ptr' anymore */
139 /* now work on this pair */
140 p = strchr(keyname, '=');
147 /* skip until closing quote */
148 while(*p && *p != '"')
152 _("%s: malformed value for key `%s'\n"),
159 for(i = 0; i < NKEYS; i++)
160 if(STRCEQ(keys[i].name, keyname))
163 warning(_("%s: unknown key `%s' ignored\n"),
167 if(keys[i].has_arg && val == NULL) {
168 warning(_("%s: no argument for key `%s', using defaults\n"),
171 } else if(!keys[i].has_arg && val) {
172 warning(_("%s: argument `%s' ignored for key `%s'\n"),
173 filename, val, keyname);
177 value[i] = atof(val);
179 /* put the argument */
180 buff_add(&buffer, val, 0);
181 buff_add(&buffer, " @", 2);
182 buff_add(&buffer, keyname, 0);
183 buff_add(&buffer, " ", 1);
185 /* remember that this option was given */
188 buff_add(&buffer, " @setspecial", 0);
190 /* now compute the bounding box (code comes from dvips) */
199 hsize = value[HSIZE];
201 vsize = value[VSIZE];
203 originx = value[HOFF];
205 originy = value[VOFF];
207 hscale = value[HSCALE] / 100.0;
209 vscale = value[VSCALE] / 100.0;
210 if(present[URX] && present[LLX])
211 hsize = value[URX] - value[LLX];
212 if(present[URY] && present[LLY])
213 vsize = value[URY] - value[LLY];
214 if(present[RWI] || present[RHI]) {
215 if(present[RWI] && !present[RHI])
216 hscale = vscale = value[RWI] / (10.0 * hsize);
217 else if(present[RHI] && !present[RWI])
218 hscale = vscale = value[RHI] / (10.0 * vsize);
220 hscale = value[RWI] / (10.0 * hsize);
221 vscale = value[RHI] / (10.0 * vsize);
227 box->bw = hsize * hscale;
228 box->bh = vsize * vscale;
229 box->angle = value[ANGLE];
236 void epsf_special(DviContext *dvi, char *prefix, char *arg)
242 EpsfBox box = {0, 0, 0, 0};
248 file = parse_epsf_special(&box, &special, prefix, arg);
252 xf = dvi->params.dpi * dvi->params.mag / (72.0 * dvi->params.hshrink);
253 vf = dvi->params.vdpi * dvi->params.mag / (72.0 * dvi->params.vshrink);
254 w = FROUND(box.bw * xf);
255 h = FROUND(box.bh * vf);
256 x = FROUND(box.ox * xf) + dvi->pos.hh;
257 y = FROUND(box.oy * vf) + dvi->pos.vv - h + 1;
259 if (!file || !dvi->device.draw_ps) {
260 dvi->device.draw_rule (dvi, x, y, w, h, 0);
264 if (file[0] == '/') { /* Absolute path */
265 if (stat (file, &buf) == 0)
266 dvi->device.draw_ps (dvi, file, x, y, w, h);
268 dvi->device.draw_rule (dvi, x, y, w, h, 0);
272 tmp = mdvi_strrstr (dvi->filename, "/");
273 if (tmp) { /* Document directory */
274 int path_len = strlen (dvi->filename) - strlen (tmp + 1);
275 int file_len = strlen (file);
277 psfile = mdvi_malloc (path_len + file_len + 1);
279 strncat (psfile, dvi->filename, path_len);
280 strncat (psfile, file, file_len);
282 if (stat (psfile, &buf) == 0) {
283 dvi->device.draw_ps (dvi, psfile, x, y, w, h);
292 psfile = mdvi_build_path_from_cwd (file);
293 if (stat (psfile, &buf) == 0) { /* Current working dir */
294 dvi->device.draw_ps (dvi, psfile, x, y, w, h);
302 psfile = kpse_find_pict (file);
303 if (psfile) { /* kpse */
304 dvi->device.draw_ps (dvi, psfile, x, y, w, h);
306 dvi->device.draw_rule(dvi, x, y, w, h, 0);