summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvipdfmx/src/spc_xtx.c
blob: 7369b6978ae21fbff72fb023dea2ccea06b5715e (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*  
    This is xdvipdfmx, an extended version of dvipdfmx,
    an eXtended version of dvipdfm by Mark A. Wicks.

    Copyright (c) 2006 SIL International
    Written by Jonathan Kew

    This file based on spc_pdfm.c, part of the dvipdfmx project:

    Copyright (C) 2002 by Jin-Hwan Cho and Shunsaku Hirata,
    the dvipdfmx project team <dvipdfmx@project.ktug.or.kr>
    
    Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <ctype.h>
#include <math.h>

#include "system.h"
#include "mem.h"
#include "error.h"
#include "mfileio.h"

#include "numbers.h"

#include "fontmap.h"
#include "dpxutil.h"

#include "pdfobj.h"
#include "pdfparse.h"

#include "pdfdoc.h"

#include "pdfdraw.h"
#include "pdfcolor.h"
#include "pdfdev.h"

#include "specials.h"

#include "spc_util.h"
#include "spc_xtx.h"


int
spc_xtx_at_begin_document (void)
{
  return 0;
}

int
spc_xtx_at_end_document (void)
{
  return 0;
}

int
spc_xtx_at_begin_page (void)
{
  /*pdf_dev_set_fixed_point(0, 0);*/
  return  0;
}

int
spc_xtx_at_end_page (void)
{
  return  0;
}

static int
spc_handler_xtx_do_scale (double x_user, double y_user, double valueX, double valueY)
{
  pdf_tmatrix     M = { 0, 0, 0, 0, 0, 0 };
  pdf_coord       pt;


  /* Create transformation matrix */
  M.a = valueX;
  M.d = valueY;
  M.e = ((1.0 - M.a) * x_user - M.c * y_user);
  M.f = ((1.0 - M.d) * y_user - M.b * x_user);

  pdf_dev_concat(&M);
  pdf_dev_get_fixed_point(&pt);
  pdf_dev_set_fixed_point(x_user - pt.x, y_user - pt.y);

  return  0;
}
static int
spc_handler_xtx_scale (struct spc_env *spe, struct spc_arg *args)
{
  double          values[2];

  if (spc_util_read_numbers(&values[0], 2, spe, args) < 2) {
    return -1;
  }
  args->curptr = args->endptr;

  return spc_handler_xtx_do_scale(spe->x_user, spe->y_user, values[0], values[1]);
}

/* Scaling without gsave/grestore. */
static pdf_coord *scaleFactors = 0;
static int scaleFactorCount = -1;

static int
spc_handler_xtx_bscale (struct spc_env *spe, struct spc_arg *args)
{
  double          values[2];

  if (!(++scaleFactorCount & 0x0f))
    scaleFactors = realloc(scaleFactors, (scaleFactorCount + 16) * sizeof(pdf_coord));
  if (spc_util_read_numbers(&values[0], 2, spe, args) < 2) {
    return -1;
  }
  if (fabs(values[0]) < 1.e-7 || fabs(values[1]) < 1.e-7) {
    return -1;
  }
  scaleFactors[scaleFactorCount].x = 1 / values[0];
  scaleFactors[scaleFactorCount].y = 1 / values[1];
  args->curptr = args->endptr;

  return  spc_handler_xtx_do_scale (spe->x_user, spe->y_user, values[0], values[1]);
}

static int
spc_handler_xtx_escale (struct spc_env *spe, struct spc_arg *args)
{
  pdf_coord factor = scaleFactors[scaleFactorCount--];

  args->curptr = args->endptr;

  return  spc_handler_xtx_do_scale (spe->x_user, spe->y_user, factor.x, factor.y);
}

int
spc_handler_xtx_do_rotate (double x_user, double y_user, double value)
{
  pdf_tmatrix     M = { 0, 0, 0, 0, 0, 0 };
  pdf_coord       pt;

  /* Create transformation matrix */
  M.a = cos(value * M_PI / 180);
  M.b = sin(value * M_PI / 180);
  M.c = -M.b;
  M.d = M.a;
  M.e = ((1.0 - M.a) * x_user - M.c * y_user);
  M.f = ((1.0 - M.d) * y_user - M.b * x_user);

  pdf_dev_concat(&M);
  pdf_dev_get_fixed_point(&pt);
  pdf_dev_set_fixed_point(x_user - pt.x, y_user - pt.y);

  return  0;
}

static int
spc_handler_xtx_rotate (struct spc_env *spe, struct spc_arg *args)
{
  double          value;

  if (spc_util_read_numbers(&value, 1, spe, args) < 1) {
    return -1;
  }
  args->curptr = args->endptr;

  return  spc_handler_xtx_do_rotate (spe->x_user, spe->y_user, value);
}

static int
spc_handler_xtx_gsave (struct spc_env *spe, struct spc_arg *args)
{
  pdf_dev_gsave();

  return  0;
}

static int
spc_handler_xtx_grestore (struct spc_env *spe, struct spc_arg *args)
{
  pdf_dev_grestore();

  /*
   * Unfortunately, the following line is necessary in case
   * of a font or color change inside of the save/restore pair.
   * Anything that was done there must be redone, so in effect,
   * we make no assumptions about what fonts. We act like we are
   * starting a new page.
   */
  pdf_dev_reset_fonts();
  pdf_dev_reset_color();

  return  0;
}

/* Please remove this.
 * This should be handled before processing pages!
 */
static int
spc_handler_xtx_papersize (struct spc_env *spe, struct spc_arg *args)
{
  return  0;
}

static int
spc_handler_xtx_backgroundcolor (struct spc_env *spe, struct spc_arg *args)
{
  int       error;
  pdf_color colorspec;

  error = spc_util_read_colorspec(spe, &colorspec, args, 0);
  if (error)
    spc_warn(spe, "No valid color specified?");
  else {
    pdf_doc_set_bgcolor(&colorspec);
  }

  return  error;
}

/* FIXME: xdv2pdf's x:fontmapline and x:fontmapfile may have slightly different syntax/semantics */
static int
spc_handler_xtx_fontmapline (struct spc_env *spe, struct spc_arg *ap)
{
  fontmap_rec *mrec;
  char        *map_name, opchr;
  int          error = 0;

  skip_white(&ap->curptr, ap->endptr);
  if (ap->curptr >= ap->endptr) {
    spc_warn(spe, "Empty fontmapline special?");
    return  -1;
  }

  opchr = ap->curptr[0];
  if (opchr == '-' || opchr == '+')
    ap->curptr++;

  skip_white(&ap->curptr, ap->endptr);

  switch (opchr) {
  case  '-':
    map_name = parse_ident(&ap->curptr, ap->endptr);
    if (map_name) {
      pdf_remove_fontmap_record(map_name);
      RELEASE(map_name);
    } else {
      spc_warn(spe, "Invalid fontmap line: Missing TFM name.");
      error = -1;
    }
    break;
  case  '+':
    mrec  = NEW(1, fontmap_rec);
    pdf_init_fontmap_record(mrec);
    error = pdf_read_fontmap_line(mrec, ap->curptr, (long) (ap->endptr - ap->curptr));
    if (error)
      spc_warn(spe, "Invalid fontmap line.");
    else {
      pdf_append_fontmap_record(mrec->map_name, mrec);
    }
    pdf_clear_fontmap_record(mrec);
    RELEASE(mrec);
    break;
  default:
    mrec = NEW(1, fontmap_rec);
    pdf_init_fontmap_record(mrec);
    error = pdf_read_fontmap_line(mrec, ap->curptr, (long) (ap->endptr - ap->curptr));
    if (error)
      spc_warn(spe, "Invalid fontmap line.");
    else {
      pdf_insert_fontmap_record(mrec->map_name, mrec);
    }
    pdf_clear_fontmap_record(mrec);
    RELEASE(mrec);
    break;
  }
  if (!error)
    ap->curptr = ap->endptr;

  return  0;
}

static int
spc_handler_xtx_fontmapfile (struct spc_env *spe, struct spc_arg *args)
{
  char  *mapfile;
  int    mode, error = 0;

  skip_white(&args->curptr, args->endptr);
  if (args->curptr >= args->endptr)
    return 0;

  switch (args->curptr[0]) {
  case  '-':
    mode = FONTMAP_RMODE_REMOVE;
    args->curptr++;
    break;
  case  '+':
    mode = FONTMAP_RMODE_APPEND;
    args->curptr++;
    break;
  default:
    mode = FONTMAP_RMODE_REPLACE;
    break;
  }

  mapfile = parse_val_ident(&args->curptr, args->endptr);
  if (!mapfile) {
    spc_warn(spe, "No fontmap file specified.");
    return  -1;
  } else {
    error = pdf_load_fontmap_file(mapfile, mode);
  }

  return  error;
}

static char overlay_name[256];

static int
spc_handler_xtx_initoverlay (struct spc_env *spe, struct spc_arg *args)
{
  skip_white(&args->curptr, args->endptr);
  if (args->curptr >= args->endptr)
    return -1;
  strncpy(overlay_name, args->curptr, args->endptr - args->curptr);
  overlay_name[args->endptr - args->curptr] = 0;

  args->curptr = args->endptr;
  return 0;
}

static int
spc_handler_xtx_clipoverlay (struct spc_env *spe, struct spc_arg *args)
{
  skip_white(&args->curptr, args->endptr);
  if (args->curptr >= args->endptr)
    return -1;
  pdf_dev_grestore();
  pdf_dev_gsave();
  if (strncmp(overlay_name, args->curptr, strlen(overlay_name)) != 0
   && strncmp("all", args->curptr, strlen("all")) != 0)
    pdf_doc_add_page_content(" 0 0 m W n", 10);

  args->curptr = args->endptr;
  return 0;
}

static int
spc_handler_xtx_renderingmode (struct spc_env *spe, struct spc_arg *args)
{
  double value;

  if (spc_util_read_numbers(&value, 1, spe, args) < 1) {
    return -1;
  }
  if ((int) value < 0 || (int) value > 7) {
    spc_warn(spe, "Invalid text rendering mode %d.\n", (int) value);
    return -1;
  }
  sprintf(work_buffer, " %d Tr", (int) value);
  pdf_doc_add_page_content(work_buffer, strlen(work_buffer));
  skip_white(&args->curptr, args->endptr);
  if (args->curptr < args->endptr) {
    pdf_doc_add_page_content(" ", 1);
    pdf_doc_add_page_content(args->curptr, args->endptr - args->curptr);
  }

  args->curptr = args->endptr;
  return 0;
}

static int
spc_handler_xtx_unsupportedcolor (struct spc_env *spe, struct spc_arg *args)
{
  spc_warn(spe, "xetex-style \\special{x:%s} is not supported by this driver;\n"
                "update document or driver to use \\special{color} instead.", args->command);

  args->curptr = args->endptr;

  return 0;
}

static int
spc_handler_xtx_unsupported (struct spc_env *spe, struct spc_arg *args)
{
  spc_warn(spe, "xetex-style \\special{x:%s} is not supported by this driver.", args->command);

  args->curptr = args->endptr;

  return 0;
}

static struct spc_handler xtx_handlers[] = {
  {"textcolor",       spc_handler_xtx_unsupportedcolor},
  {"textcolorpush",   spc_handler_xtx_unsupportedcolor},
  {"textcolorpop",    spc_handler_xtx_unsupportedcolor},

  {"rulecolor",       spc_handler_xtx_unsupportedcolor},
  {"rulecolorpush",   spc_handler_xtx_unsupportedcolor},
  {"rulecolorpop",    spc_handler_xtx_unsupportedcolor},

  {"papersize",       spc_handler_xtx_papersize},
  {"backgroundcolor", spc_handler_xtx_backgroundcolor},

  {"gsave",           spc_handler_xtx_gsave},
  {"grestore",        spc_handler_xtx_grestore},

  {"scale",           spc_handler_xtx_scale},
  {"bscale",           spc_handler_xtx_bscale},
  {"escale",           spc_handler_xtx_escale},
  {"rotate",          spc_handler_xtx_rotate},

  {"fontmapline",     spc_handler_xtx_fontmapline},
  {"fontmapfile",     spc_handler_xtx_fontmapfile},

  {"shadow",          spc_handler_xtx_unsupported},
  {"colorshadow",     spc_handler_xtx_unsupported},
  {"renderingmode",   spc_handler_xtx_renderingmode},

  {"initoverlay",     spc_handler_xtx_initoverlay},
  {"clipoverlay",     spc_handler_xtx_clipoverlay},
};

int
spc_xtx_check_special (const char *buf, long len)
{
  int    r = 0;
  char  *p, *endptr;

  p      = (char *) buf;
  endptr = p + len;

  skip_white(&p, endptr);
  if (p + strlen("x:") <= endptr &&
      !memcmp(p, "x:", strlen("x:"))) {
    r = 1;
  }

  return  r;
}

int
spc_xtx_setup_handler (struct spc_handler *sph,
                        struct spc_env *spe, struct spc_arg *ap)
{
  int    error = -1, i;
  char  *q;

  ASSERT(sph && spe && ap);

  skip_white(&ap->curptr, ap->endptr);
  if (ap->curptr + strlen("x:") >= ap->endptr ||
      memcmp(ap->curptr, "x:", strlen("x:"))) {
    spc_warn(spe, "Not x: special???");
    return  -1;
  }
  ap->curptr += strlen("x:");

  skip_white(&ap->curptr, ap->endptr);
  q = parse_c_ident(&ap->curptr, ap->endptr);
  if (q) {
    for (i = 0;
         i < sizeof(xtx_handlers) / sizeof(struct spc_handler); i++) {
      if (!strcmp(q, xtx_handlers[i].key)) {
        ap->command = (char *) xtx_handlers[i].key;
        sph->key   = (char *) "x:";
        sph->exec  = xtx_handlers[i].exec;
        skip_white(&ap->curptr, ap->endptr);
        error = 0;
        break;
      }
    }
    RELEASE(q);
  }

  return  error;
}