Debian Codegraph @

texk/dvipdfm-x/spc_xtx.c

raw
1 /* This is xdvipdfmx, an extended version of dvipdfmx,
2 an eXtended version of dvipdfm by Mark A. Wicks.
3
4 Copyright (C) 2013-2020 by the dvipdfmx project team.
5
6 Copyright (c) 2006 SIL International
7 Originally written by Jonathan Kew
8
9 This file based on spc_pdfm.c, part of the dvipdfmx project:
10
11 Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata.
12
13 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <ctype.h>
35 #include <math.h>
36
37 #include "system.h"
38 #include "mem.h"
39 #include "error.h"
40 #include "mfileio.h"
41
42 #include "numbers.h"
43
44 #include "fontmap.h"
45 #include "dpxutil.h"
46
47 #include "pdfobj.h"
48 #include "pdfparse.h"
49
50 #include "pdfdoc.h"
51
52 #include "pdfdraw.h"
53 #include "pdfcolor.h"
54 #include "pdfdev.h"
55
56 #include "specials.h"
57
58 #include "spc_util.h"
59 #include "spc_xtx.h"
60
61
62 int
63 spc_handler_xtx_do_transform (struct spc_env *spe, double x_user, double y_user, double a, double b, double c, double d, double e, double f)
64 {
65 pdf_tmatrix M = { 0, 0, 0, 0, 0, 0 };
66 pdf_coord pt;
67
68 /* Create transformation matrix */
69 M.a = a;
70 M.b = b;
71 M.c = c;
72 M.d = d;
73 M.e = ((1.0 - M.a) * x_user - M.c * y_user) + e;
74 M.f = ((1.0 - M.d) * y_user - M.b * x_user) + f;
75
76 pdf_dev_concat(&M);
77 spc_get_fixed_point(spe, &pt.x, &pt.y);
78 spc_set_fixed_point(spe, x_user - pt.x, y_user - pt.y);
79
80 return 0;
81 }
82
83 static int
84 spc_handler_xtx_scale (struct spc_env *spe, struct spc_arg *args)
85 {
86 double values[2];
87
88 if (spc_util_read_numbers(&values[0], 2, args) < 2) {
89 return -1;
90 }
91 args->curptr = args->endptr;
92
93 return spc_handler_xtx_do_transform(spe, spe->x_user, spe->y_user, values[0], 0, 0, values[1], 0, 0);
94 }
95
96 /* Scaling without gsave/grestore. */
97 static pdf_coord *scaleFactors = 0;
98 static int scaleFactorCount = -1;
99
100 static int
101 spc_handler_xtx_bscale (struct spc_env *spe, struct spc_arg *args)
102 {
103 double values[2];
104
105 if (!(++scaleFactorCount & 0x0f))
106 scaleFactors = realloc(scaleFactors, (scaleFactorCount + 16) * sizeof(pdf_coord));
107 if (spc_util_read_numbers(&values[0], 2, args) < 2) {
108 return -1;
109 }
110 if (fabs(values[0]) < 1.e-7 || fabs(values[1]) < 1.e-7) {
111 return -1;
112 }
113 scaleFactors[scaleFactorCount].x = 1 / values[0];
114 scaleFactors[scaleFactorCount].y = 1 / values[1];
115 args->curptr = args->endptr;
116
117 return spc_handler_xtx_do_transform (spe, spe->x_user, spe->y_user, values[0], 0, 0, values[1], 0, 0);
118 }
119
120 static int
121 spc_handler_xtx_escale (struct spc_env *spe, struct spc_arg *args)
122 {
123 pdf_coord factor = scaleFactors[scaleFactorCount--];
124
125 args->curptr = args->endptr;
126
127 return spc_handler_xtx_do_transform (spe, spe->x_user, spe->y_user, factor.x, 0, 0, factor.y, 0, 0);
128 }
129
130 static int
131 spc_handler_xtx_rotate (struct spc_env *spe, struct spc_arg *args)
132 {
133 double value;
134
135 if (spc_util_read_numbers(&value, 1, args) < 1) {
136 return -1;
137 }
138 args->curptr = args->endptr;
139
140 return spc_handler_xtx_do_transform (spe, spe->x_user, spe->y_user,
141 cos(value * M_PI / 180), sin(value * M_PI / 180),
142 -sin(value * M_PI / 180), cos(value * M_PI / 180),
143 0, 0);
144 }
145
146 int
147 spc_handler_xtx_gsave (struct spc_env *spe, struct spc_arg *args)
148 {
149 pdf_dev_gsave();
150 spc_dup_fixed_point(spe);
151
152 return 0;
153 }
154
155 int
156 spc_handler_xtx_grestore (struct spc_env *spe, struct spc_arg *args)
157 {
158 pdf_dev_grestore();
159 spc_pop_fixed_point(spe);
160
161 /*
162 * Unfortunately, the following line is necessary in case
163 * of a font or color change inside of the save/restore pair.
164 * Anything that was done there must be redone, so in effect,
165 * we make no assumptions about what fonts. We act like we are
166 * starting a new page.
167 */
168 pdf_dev_reset_fonts(0);
169 pdf_dev_reset_color(0);
170 pdf_dev_reset_xgstate(0);
171
172 return 0;
173 }
174
175 /* Please remove this.
176 * This should be handled before processing pages!
177 */
178 static int
179 spc_handler_xtx_papersize (struct spc_env *spe, struct spc_arg *args)
180 {
181 return 0;
182 }
183
184 static int
185 spc_handler_xtx_backgroundcolor (struct spc_env *spe, struct spc_arg *args)
186 {
187 int error;
188 pdf_color colorspec;
189
190 error = spc_util_read_colorspec(spe, &colorspec, args, 0);
191 if (error)
192 spc_warn(spe, "No valid color specified?");
193 else {
194 pdf_doc_set_bgcolor(&colorspec);
195 }
196
197 return error;
198 }
199
200 /* FIXME: xdv2pdf's x:fontmapline and x:fontmapfile may have slightly different syntax/semantics */
201 #define THEBUFFLENGTH 1024
202 static int
203 spc_handler_xtx_fontmapline (struct spc_env *spe, struct spc_arg *ap)
204 {
205 fontmap_rec *mrec;
206 char *map_name, opchr;
207 int error = 0;
208 static char buffer[THEBUFFLENGTH];
209 const char *p;
210 char *q;
211 int count;
212
213 skip_white(&ap->curptr, ap->endptr);
214 if (ap->curptr >= ap->endptr) {
215 spc_warn(spe, "Empty fontmapline special?");
216 return -1;
217 }
218
219 opchr = ap->curptr[0];
220 if (opchr == '-' || opchr == '+')
221 ap->curptr++;
222
223 skip_white(&ap->curptr, ap->endptr);
224
225 switch (opchr) {
226 case '-':
227 map_name = parse_ident(&ap->curptr, ap->endptr);
228 if (map_name) {
229 pdf_remove_fontmap_record(map_name);
230 RELEASE(map_name);
231 } else {
232 spc_warn(spe, "Invalid fontmap line: Missing TFM name.");
233 error = -1;
234 }
235 break;
236 default:
237 p = ap->curptr;
238 q = buffer;
239 count = 0;
240 while (p < ap->endptr && count < THEBUFFLENGTH - 1) {
241 *q++ = *p++;
242 count++;
243 }
244 if (count == THEBUFFLENGTH - 1) {
245 spc_warn(spe, "Invalid fontmap line: Too long a line.");
246 *q = 0;
247 return -1;
248 }
249 *q = '\0';
250 mrec = NEW(1, fontmap_rec);
251 pdf_init_fontmap_record(mrec);
252 error = pdf_read_fontmap_line(mrec, buffer, (int) (ap->endptr - ap->curptr), is_pdfm_mapline(buffer));
253 if (error)
254 spc_warn(spe, "Invalid fontmap line.");
255 else if (opchr == '+')
256 pdf_append_fontmap_record(mrec->map_name, mrec);
257 else
258 pdf_insert_fontmap_record(mrec->map_name, mrec);
259 pdf_clear_fontmap_record(mrec);
260 RELEASE(mrec);
261 break;
262 }
263 if (!error)
264 ap->curptr = ap->endptr;
265
266 return 0;
267 }
268
269 static int
270 spc_handler_xtx_fontmapfile (struct spc_env *spe, struct spc_arg *args)
271 {
272 char *mapfile;
273 int mode, error = 0;
274
275 skip_white(&args->curptr, args->endptr);
276 if (args->curptr >= args->endptr)
277 return 0;
278
279 switch (args->curptr[0]) {
280 case '-':
281 mode = FONTMAP_RMODE_REMOVE;
282 args->curptr++;
283 break;
284 case '+':
285 mode = FONTMAP_RMODE_APPEND;
286 args->curptr++;
287 break;
288 default:
289 mode = FONTMAP_RMODE_REPLACE;
290 break;
291 }
292
293 mapfile = parse_val_ident(&args->curptr, args->endptr);
294 if (!mapfile) {
295 spc_warn(spe, "No fontmap file specified.");
296 return -1;
297 } else {
298 error = pdf_load_fontmap_file(mapfile, mode);
299 }
300
301 return error;
302 }
303
304 static char overlay_name[256];
305
306 static int
307 spc_handler_xtx_initoverlay (struct spc_env *spe, struct spc_arg *args)
308 {
309 skip_white(&args->curptr, args->endptr);
310 if (args->curptr >= args->endptr)
311 return -1;
312 strncpy(overlay_name, args->curptr, args->endptr - args->curptr);
313 overlay_name[args->endptr - args->curptr] = 0;
314
315 args->curptr = args->endptr;
316 return 0;
317 }
318
319 static int
320 spc_handler_xtx_clipoverlay (struct spc_env *spe, struct spc_arg *args)
321 {
322 skip_white(&args->curptr, args->endptr);
323 if (args->curptr >= args->endptr)
324 return -1;
325 pdf_dev_grestore();
326 pdf_dev_gsave();
327 if (strncmp(overlay_name, args->curptr, strlen(overlay_name)) != 0
328 && strncmp("all", args->curptr, strlen("all")) != 0)
329 pdf_doc_add_page_content(" 0 0 m W n", 10);
330
331 args->curptr = args->endptr;
332 return 0;
333 }
334
335 static int
336 spc_handler_xtx_renderingmode (struct spc_env *spe, struct spc_arg *args)
337 {
338 double value;
339
340 if (spc_util_read_numbers(&value, 1, args) < 1) {
341 return -1;
342 }
343 if ((int) value < 0 || (int) value > 7) {
344 spc_warn(spe, "Invalid text rendering mode %d.\n", (int) value);
345 return -1;
346 }
347 sprintf(work_buffer, " %d Tr", (int) value);
348 pdf_doc_add_page_content(work_buffer, strlen(work_buffer));
349 skip_white(&args->curptr, args->endptr);
350 if (args->curptr < args->endptr) {
351 pdf_doc_add_page_content(" ", 1);
352 pdf_doc_add_page_content(args->curptr, args->endptr - args->curptr);
353 }
354
355 args->curptr = args->endptr;
356 return 0;
357 }
358
359 static int
360 spc_handler_xtx_unsupportedcolor (struct spc_env *spe, struct spc_arg *args)
361 {
362 spc_warn(spe, "xetex-style \\special{x:%s} is not supported by this driver;\n"
363 "update document or driver to use \\special{color} instead.", args->command);
364
365 args->curptr = args->endptr;
366
367 return 0;
368 }
369
370 static int
371 spc_handler_xtx_unsupported (struct spc_env *spe, struct spc_arg *args)
372 {
373 spc_warn(spe, "xetex-style \\special{x:%s} is not supported by this driver.", args->command);
374
375 args->curptr = args->endptr;
376
377 return 0;
378 }
379
380 static struct spc_handler xtx_handlers[] = {
381 {"textcolor", spc_handler_xtx_unsupportedcolor},
382 {"textcolorpush", spc_handler_xtx_unsupportedcolor},
383 {"textcolorpop", spc_handler_xtx_unsupportedcolor},
384
385 {"rulecolor", spc_handler_xtx_unsupportedcolor},
386 {"rulecolorpush", spc_handler_xtx_unsupportedcolor},
387 {"rulecolorpop", spc_handler_xtx_unsupportedcolor},
388
389 {"papersize", spc_handler_xtx_papersize},
390 {"backgroundcolor", spc_handler_xtx_backgroundcolor},
391
392 {"gsave", spc_handler_xtx_gsave},
393 {"grestore", spc_handler_xtx_grestore},
394
395 {"scale", spc_handler_xtx_scale},
396 {"bscale", spc_handler_xtx_bscale},
397 {"escale", spc_handler_xtx_escale},
398 {"rotate", spc_handler_xtx_rotate},
399
400 {"fontmapline", spc_handler_xtx_fontmapline},
401 {"fontmapfile", spc_handler_xtx_fontmapfile},
402
403 {"shadow", spc_handler_xtx_unsupported},
404 {"colorshadow", spc_handler_xtx_unsupported},
405 {"renderingmode", spc_handler_xtx_renderingmode},
406
407 {"initoverlay", spc_handler_xtx_initoverlay},
408 {"clipoverlay", spc_handler_xtx_clipoverlay},
409 };
410
411 int
412 spc_xtx_check_special (const char *buf, int len)
413 {
414 int r = 0;
415 const char *p, *endptr;
416
417 p = buf;
418 endptr = p + len;
419
420 skip_white(&p, endptr);
421 if (p + strlen("x:") <= endptr &&
422 !memcmp(p, "x:", strlen("x:"))) {
423 r = 1;
424 }
425
426 return r;
427 }
428
429 int
430 spc_xtx_setup_handler (struct spc_handler *sph,
431 struct spc_env *spe, struct spc_arg *ap)
432 {
433 int error = -1, i;
434 char *q;
435
436 ASSERT(sph && spe && ap);
437
438 skip_white(&ap->curptr, ap->endptr);
439 if (ap->curptr + strlen("x:") >= ap->endptr ||
440 memcmp(ap->curptr, "x:", strlen("x:"))) {
441 spc_warn(spe, "Not x: special???");
442 return -1;
443 }
444 ap->curptr += strlen("x:");
445
446 skip_white(&ap->curptr, ap->endptr);
447 q = parse_c_ident(&ap->curptr, ap->endptr);
448 if (q) {
449 for (i = 0;
450 i < sizeof(xtx_handlers) / sizeof(struct spc_handler); i++) {
451 if (!strcmp(q, xtx_handlers[i].key)) {
452 ap->command = xtx_handlers[i].key;
453 sph->key = "x:";
454 sph->exec = xtx_handlers[i].exec;
455 skip_white(&ap->curptr, ap->endptr);
456 error = 0;
457 break;
458 }
459 }
460 RELEASE(q);
461 }
462
463 return error;
464 }
465