Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xpdf-4.06/xpdf/Gfx.cc
Line
Count
Source
1
//========================================================================
2
//
3
// Gfx.cc
4
//
5
// Copyright 1996-2016 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <stdlib.h>
12
#include <stdio.h>
13
#include <stddef.h>
14
#include <string.h>
15
#include <math.h>
16
#include "gmem.h"
17
#include "gmempp.h"
18
#include "GString.h"
19
#include "GList.h"
20
#include "Trace.h"
21
#include "GlobalParams.h"
22
#include "CharTypes.h"
23
#include "Object.h"
24
#include "PDFDoc.h"
25
#include "Array.h"
26
#include "Dict.h"
27
#include "Stream.h"
28
#include "Lexer.h"
29
#include "Parser.h"
30
#include "GfxFont.h"
31
#include "GfxState.h"
32
#include "OutputDev.h"
33
#include "Page.h"
34
#include "Annot.h"
35
#include "OptionalContent.h"
36
#include "Error.h"
37
#include "TextString.h"
38
#include "Gfx.h"
39
40
// the MSVC math.h doesn't define this
41
#ifndef M_PI
42
#define M_PI 3.14159265358979323846
43
#endif
44
45
//------------------------------------------------------------------------
46
// constants
47
//------------------------------------------------------------------------
48
49
// Max recursive depth for a function shading fill.
50
0
#define functionMaxDepth 6
51
52
// Max delta allowed in any color component for a function shading fill.
53
0
#define functionColorDelta (dblToCol(1 / 256.0))
54
55
// Number of splits along the t axis for an axial shading fill.
56
0
#define axialSplits 256
57
58
// Max delta allowed in any color component for an axial shading fill.
59
0
#define axialColorDelta (dblToCol(1 / 256.0))
60
61
// Max number of splits along the t axis for a radial shading fill.
62
0
#define radialMaxSplits 256
63
64
// Max delta allowed in any color component for a radial shading fill.
65
0
#define radialColorDelta (dblToCol(1 / 256.0))
66
67
// Max recursive depth for a Gouraud triangle shading fill.
68
0
#define gouraudMaxDepth 6
69
70
// Max delta allowed in any color component for a Gouraud triangle
71
// shading fill.
72
0
#define gouraudColorDelta (dblToCol(1 / 256.0))
73
74
// Max recursive depth for a patch mesh shading fill.
75
0
#define patchMaxDepth 6
76
77
// Max delta allowed in any color component for a patch mesh shading
78
// fill.
79
0
#define patchColorDelta (dblToCol(1 / 256.0))
80
81
// Max errors (undefined operator, wrong number of args) allowed before
82
// giving up on a content stream.
83
10.5M
#define contentStreamErrorLimit 500
84
85
//------------------------------------------------------------------------
86
// Operator table
87
//------------------------------------------------------------------------
88
89
#ifdef _WIN32 // this works around a bug in the VC7 compiler
90
#  pragma optimize("",off)
91
#endif
92
93
Operator Gfx::opTab[] = {
94
  {"\"",  3, {tchkNum,    tchkNum,    tchkString},
95
          &Gfx::opMoveSetShowText},
96
  {"'",   1, {tchkString},
97
          &Gfx::opMoveShowText},
98
  {"B",   0, {tchkNone},
99
          &Gfx::opFillStroke},
100
  {"B*",  0, {tchkNone},
101
          &Gfx::opEOFillStroke},
102
  {"BDC", 2, {tchkName,   tchkProps},
103
          &Gfx::opBeginMarkedContent},
104
  {"BI",  0, {tchkNone},
105
          &Gfx::opBeginImage},
106
  {"BMC", 1, {tchkName},
107
          &Gfx::opBeginMarkedContent},
108
  {"BT",  0, {tchkNone},
109
          &Gfx::opBeginText},
110
  {"BX",  0, {tchkNone},
111
          &Gfx::opBeginIgnoreUndef},
112
  {"CS",  1, {tchkName},
113
          &Gfx::opSetStrokeColorSpace},
114
  {"DP",  2, {tchkName,   tchkProps},
115
          &Gfx::opMarkPoint},
116
  {"Do",  1, {tchkName},
117
          &Gfx::opXObject},
118
  {"EI",  0, {tchkNone},
119
          &Gfx::opEndImage},
120
  {"EMC", 0, {tchkNone},
121
          &Gfx::opEndMarkedContent},
122
  {"ET",  0, {tchkNone},
123
          &Gfx::opEndText},
124
  {"EX",  0, {tchkNone},
125
          &Gfx::opEndIgnoreUndef},
126
  {"F",   0, {tchkNone},
127
          &Gfx::opFill},
128
  {"G",   1, {tchkNum},
129
          &Gfx::opSetStrokeGray},
130
  {"ID",  0, {tchkNone},
131
          &Gfx::opImageData},
132
  {"J",   1, {tchkInt},
133
          &Gfx::opSetLineCap},
134
  {"K",   4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
135
          &Gfx::opSetStrokeCMYKColor},
136
  {"M",   1, {tchkNum},
137
          &Gfx::opSetMiterLimit},
138
  {"MP",  1, {tchkName},
139
          &Gfx::opMarkPoint},
140
  {"Q",   0, {tchkNone},
141
          &Gfx::opRestore},
142
  {"RG",  3, {tchkNum,    tchkNum,    tchkNum},
143
          &Gfx::opSetStrokeRGBColor},
144
  {"S",   0, {tchkNone},
145
          &Gfx::opStroke},
146
  {"SC",  -4, {tchkNum,   tchkNum,    tchkNum,    tchkNum,
147
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
148
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
149
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
150
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
151
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
152
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
153
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
154
         tchkNum},
155
          &Gfx::opSetStrokeColor},
156
  {"SCN", -33, {tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
157
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
158
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
159
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
160
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
161
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
162
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
163
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
164
          tchkSCN},
165
          &Gfx::opSetStrokeColorN},
166
  {"T*",  0, {tchkNone},
167
          &Gfx::opTextNextLine},
168
  {"TD",  2, {tchkNum,    tchkNum},
169
          &Gfx::opTextMoveSet},
170
  {"TJ",  1, {tchkArray},
171
          &Gfx::opShowSpaceText},
172
  {"TL",  1, {tchkNum},
173
          &Gfx::opSetTextLeading},
174
  {"Tc",  1, {tchkNum},
175
          &Gfx::opSetCharSpacing},
176
  {"Td",  2, {tchkNum,    tchkNum},
177
          &Gfx::opTextMove},
178
  {"Tf",  2, {tchkName,   tchkNum},
179
          &Gfx::opSetFont},
180
  {"Tj",  1, {tchkString},
181
          &Gfx::opShowText},
182
  {"Tm",  6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
183
        tchkNum,    tchkNum},
184
          &Gfx::opSetTextMatrix},
185
  {"Tr",  1, {tchkInt},
186
          &Gfx::opSetTextRender},
187
  {"Ts",  1, {tchkNum},
188
          &Gfx::opSetTextRise},
189
  {"Tw",  1, {tchkNum},
190
          &Gfx::opSetWordSpacing},
191
  {"Tz",  1, {tchkNum},
192
          &Gfx::opSetHorizScaling},
193
  {"W",   0, {tchkNone},
194
          &Gfx::opClip},
195
  {"W*",  0, {tchkNone},
196
          &Gfx::opEOClip},
197
  {"b",   0, {tchkNone},
198
          &Gfx::opCloseFillStroke},
199
  {"b*",  0, {tchkNone},
200
          &Gfx::opCloseEOFillStroke},
201
  {"c",   6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
202
        tchkNum,    tchkNum},
203
          &Gfx::opCurveTo},
204
  {"cm",  6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
205
        tchkNum,    tchkNum},
206
          &Gfx::opConcat},
207
  {"cs",  1, {tchkName},
208
          &Gfx::opSetFillColorSpace},
209
  {"d",   2, {tchkArray,  tchkNum},
210
          &Gfx::opSetDash},
211
  {"d0",  2, {tchkNum,    tchkNum},
212
          &Gfx::opSetCharWidth},
213
  {"d1",  6, {tchkNum,    tchkNum,    tchkNum,    tchkNum,
214
        tchkNum,    tchkNum},
215
          &Gfx::opSetCacheDevice},
216
  {"f",   0, {tchkNone},
217
          &Gfx::opFill},
218
  {"f*",  0, {tchkNone},
219
          &Gfx::opEOFill},
220
  {"g",   1, {tchkNum},
221
          &Gfx::opSetFillGray},
222
  {"gs",  1, {tchkName},
223
          &Gfx::opSetExtGState},
224
  {"h",   0, {tchkNone},
225
          &Gfx::opClosePath},
226
  {"i",   1, {tchkNum},
227
          &Gfx::opSetFlat},
228
  {"j",   1, {tchkInt},
229
          &Gfx::opSetLineJoin},
230
  {"k",   4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
231
          &Gfx::opSetFillCMYKColor},
232
  {"l",   2, {tchkNum,    tchkNum},
233
          &Gfx::opLineTo},
234
  {"m",   2, {tchkNum,    tchkNum},
235
          &Gfx::opMoveTo},
236
  {"n",   0, {tchkNone},
237
          &Gfx::opEndPath},
238
  {"q",   0, {tchkNone},
239
          &Gfx::opSave},
240
  {"re",  4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
241
          &Gfx::opRectangle},
242
  {"rg",  3, {tchkNum,    tchkNum,    tchkNum},
243
          &Gfx::opSetFillRGBColor},
244
  {"ri",  1, {tchkName},
245
          &Gfx::opSetRenderingIntent},
246
  {"s",   0, {tchkNone},
247
          &Gfx::opCloseStroke},
248
  {"sc",  -4, {tchkNum,   tchkNum,    tchkNum,    tchkNum,
249
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
250
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
251
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
252
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
253
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
254
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
255
         tchkNum,   tchkNum,    tchkNum,    tchkNum,
256
         tchkNum},
257
          &Gfx::opSetFillColor},
258
  {"scn", -33, {tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
259
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
260
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
261
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
262
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
263
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
264
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
265
          tchkSCN,   tchkSCN,    tchkSCN,    tchkSCN,
266
          tchkSCN},
267
          &Gfx::opSetFillColorN},
268
  {"sh",  1, {tchkName},
269
          &Gfx::opShFill},
270
  {"v",   4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
271
          &Gfx::opCurveTo1},
272
  {"w",   1, {tchkNum},
273
          &Gfx::opSetLineWidth},
274
  {"y",   4, {tchkNum,    tchkNum,    tchkNum,    tchkNum},
275
          &Gfx::opCurveTo2},
276
};
277
278
#ifdef _WIN32 // this works around a bug in the VC7 compiler
279
#  pragma optimize("",on)
280
#endif
281
282
10.5M
#define numOps (sizeof(opTab) / sizeof(Operator))
283
284
//------------------------------------------------------------------------
285
// GfxResources
286
//------------------------------------------------------------------------
287
288
2.96M
GfxResources::GfxResources(XRef *xref, Dict *resDict, GfxResources *nextA) {
289
2.96M
  Object obj1, obj2;
290
2.96M
  Ref r;
291
292
2.96M
  if (resDict) {
293
2.89M
    valid = gTrue;
294
295
    // build font dictionary
296
2.89M
    fonts = NULL;
297
2.89M
    resDict->lookupNF("Font", &obj1);
298
2.89M
    if (obj1.isRef()) {
299
2.91k
      obj1.fetch(xref, &obj2);
300
2.91k
      if (obj2.isDict()) {
301
324
  r = obj1.getRef();
302
324
  fonts = new GfxFontDict(xref, &r, obj2.getDict());
303
324
      }
304
2.91k
      obj2.free();
305
2.89M
    } else if (obj1.isDict()) {
306
115k
      fonts = new GfxFontDict(xref, NULL, obj1.getDict());
307
115k
    }
308
2.89M
    obj1.free();
309
310
    // get XObject dictionary
311
2.89M
    resDict->lookup("XObject", &xObjDict);
312
313
    // get color space dictionary
314
2.89M
    resDict->lookup("ColorSpace", &colorSpaceDict);
315
316
    // get pattern dictionary
317
2.89M
    resDict->lookup("Pattern", &patternDict);
318
319
    // get shading dictionary
320
2.89M
    resDict->lookup("Shading", &shadingDict);
321
322
    // get graphics state parameter dictionary
323
2.89M
    resDict->lookup("ExtGState", &gStateDict);
324
325
    // get properties dictionary
326
2.89M
    resDict->lookup("Properties", &propsDict);
327
328
2.89M
  } else {
329
70.9k
    valid = gFalse;
330
70.9k
    fonts = NULL;
331
70.9k
    xObjDict.initNull();
332
70.9k
    colorSpaceDict.initNull();
333
70.9k
    patternDict.initNull();
334
70.9k
    shadingDict.initNull();
335
70.9k
    gStateDict.initNull();
336
70.9k
    propsDict.initNull();
337
70.9k
  }
338
339
2.96M
  next = nextA;
340
2.96M
}
341
342
2.96M
GfxResources::~GfxResources() {
343
2.96M
  if (fonts) {
344
115k
    delete fonts;
345
115k
  }
346
2.96M
  xObjDict.free();
347
2.96M
  colorSpaceDict.free();
348
2.96M
  patternDict.free();
349
2.96M
  shadingDict.free();
350
2.96M
  gStateDict.free();
351
2.96M
  propsDict.free();
352
2.96M
}
353
354
135k
GfxFont *GfxResources::lookupFont(char *name) {
355
135k
  GfxFont *font;
356
135k
  GfxResources *resPtr;
357
358
212k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
359
158k
    if (resPtr->fonts) {
360
137k
      if ((font = resPtr->fonts->lookup(name))) {
361
81.0k
  return font;
362
81.0k
      }
363
137k
    }
364
158k
  }
365
54.5k
  error(errSyntaxError, -1, "Unknown font tag '{0:s}'", name);
366
54.5k
  return NULL;
367
135k
}
368
369
0
GfxFont *GfxResources::lookupFontByRef(Ref ref) {
370
0
  GfxFont *font;
371
0
  GfxResources *resPtr;
372
373
0
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
374
0
    if (resPtr->fonts) {
375
0
      if ((font = resPtr->fonts->lookupByRef(ref))) {
376
0
  return font;
377
0
      }
378
0
    }
379
0
  }
380
0
  error(errSyntaxError, -1, "Unknown font ref {0:d}.{1:d}", ref.num, ref.gen);
381
0
  return NULL;
382
0
}
383
384
0
GBool GfxResources::lookupXObject(const char *name, Object *obj) {
385
0
  GfxResources *resPtr;
386
387
0
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
388
0
    if (resPtr->xObjDict.isDict()) {
389
0
      if (!resPtr->xObjDict.dictLookup(name, obj)->isNull())
390
0
  return gTrue;
391
0
      obj->free();
392
0
    }
393
0
  }
394
0
  error(errSyntaxError, -1, "XObject '{0:s}' is unknown", name);
395
0
  return gFalse;
396
0
}
397
398
96.4k
GBool GfxResources::lookupXObjectNF(const char *name, Object *obj) {
399
96.4k
  GfxResources *resPtr;
400
401
193k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
402
180k
    if (resPtr->xObjDict.isDict()) {
403
172k
      if (!resPtr->xObjDict.dictLookupNF(name, obj)->isNull())
404
83.9k
  return gTrue;
405
88.3k
      obj->free();
406
88.3k
    }
407
180k
  }
408
12.4k
  error(errSyntaxError, -1, "XObject '{0:s}' is unknown", name);
409
12.4k
  return gFalse;
410
96.4k
}
411
412
void GfxResources::lookupColorSpace(const char *name, Object *obj,
413
65.9k
            GBool inherit) {
414
65.9k
  GfxResources *resPtr;
415
416
  //~ should also test for G, RGB, and CMYK - but only in inline images (?)
417
65.9k
  if (!strcmp(name, "DeviceGray") ||
418
52.0k
      !strcmp(name, "DeviceRGB") ||
419
46.1k
      !strcmp(name, "DeviceCMYK")) {
420
19.9k
    obj->initNull();
421
19.9k
    return;
422
19.9k
  }
423
118k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
424
85.0k
    if (resPtr->colorSpaceDict.isDict()) {
425
19.1k
      if (!resPtr->colorSpaceDict.dictLookup(name, obj)->isNull()) {
426
13.0k
  return;
427
13.0k
      }
428
6.15k
      obj->free();
429
6.15k
    }
430
71.9k
    if (!inherit && valid) {
431
0
      break;
432
0
    }
433
71.9k
  }
434
32.9k
  obj->initNull();
435
32.9k
}
436
437
GfxPattern *GfxResources::lookupPattern(const char *name
438
14.7k
          ) {
439
14.7k
  GfxResources *resPtr;
440
14.7k
  GfxPattern *pattern;
441
14.7k
  Object objRef, obj;
442
443
22.3k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
444
15.9k
    if (resPtr->patternDict.isDict()) {
445
12.0k
      if (!resPtr->patternDict.dictLookup(name, &obj)->isNull()) {
446
8.33k
  resPtr->patternDict.dictLookupNF(name, &objRef);
447
8.33k
  pattern = GfxPattern::parse(&objRef, &obj
448
8.33k
            );
449
8.33k
  objRef.free();
450
8.33k
  obj.free();
451
8.33k
  return pattern;
452
8.33k
      }
453
3.74k
      obj.free();
454
3.74k
    }
455
15.9k
  }
456
6.38k
  error(errSyntaxError, -1, "Unknown pattern '{0:s}'", name);
457
6.38k
  return NULL;
458
14.7k
}
459
460
GfxShading *GfxResources::lookupShading(const char *name
461
24.9k
          ) {
462
24.9k
  GfxResources *resPtr;
463
24.9k
  GfxShading *shading;
464
24.9k
  Object obj;
465
466
54.8k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
467
38.1k
    if (resPtr->shadingDict.isDict()) {
468
9.66k
      if (!resPtr->shadingDict.dictLookup(name, &obj)->isNull()) {
469
8.21k
  shading = GfxShading::parse(&obj
470
8.21k
            );
471
8.21k
  obj.free();
472
8.21k
  return shading;
473
8.21k
      }
474
1.45k
      obj.free();
475
1.45k
    }
476
38.1k
  }
477
16.7k
  error(errSyntaxError, -1, "Unknown shading '{0:s}'", name);
478
16.7k
  return NULL;
479
24.9k
}
480
481
33.4k
GBool GfxResources::lookupGState(const char *name, Object *obj) {
482
33.4k
  GfxResources *resPtr;
483
484
74.8k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
485
58.6k
    if (resPtr->gStateDict.isDict()) {
486
27.0k
      if (!resPtr->gStateDict.dictLookup(name, obj)->isNull()) {
487
17.2k
  return gTrue;
488
17.2k
      }
489
9.81k
      obj->free();
490
9.81k
    }
491
58.6k
  }
492
16.1k
  error(errSyntaxError, -1, "ExtGState '{0:s}' is unknown", name);
493
16.1k
  return gFalse;
494
33.4k
}
495
496
10.2k
GBool GfxResources::lookupPropertiesNF(const char *name, Object *obj) {
497
10.2k
  GfxResources *resPtr;
498
499
14.6k
  for (resPtr = this; resPtr; resPtr = resPtr->next) {
500
10.2k
    if (resPtr->propsDict.isDict()) {
501
9.88k
      if (!resPtr->propsDict.dictLookupNF(name, obj)->isNull()) {
502
5.83k
  return gTrue;
503
5.83k
      }
504
4.04k
      obj->free();
505
4.04k
    }
506
10.2k
  }
507
4.38k
  error(errSyntaxError, -1, "Properties '{0:s}' is unknown", name);
508
4.38k
  return gFalse;
509
10.2k
}
510
511
//------------------------------------------------------------------------
512
// Gfx
513
//------------------------------------------------------------------------
514
515
Gfx::Gfx(PDFDoc *docA, OutputDev *outA, LocalParams *localParams,
516
   int pageNum, Dict *resDict,
517
   double hDPI, double vDPI, PDFRectangle *box,
518
   PDFRectangle *cropBox, int rotate,
519
   GBool (*abortCheckCbkA)(void *data),
520
149k
   void *abortCheckCbkDataA) {
521
149k
  int i;
522
523
149k
  doc = docA;
524
149k
  xref = doc->getXRef();
525
149k
  subPage = gFalse;
526
149k
  printCommands = globalParams->getPrintCommands();
527
149k
  defaultFont = NULL;
528
529
  // start the resource stack
530
149k
  res = new GfxResources(xref, resDict, NULL);
531
532
  // initialize
533
149k
  out = outA;
534
149k
  state = new GfxState(localParams, hDPI, vDPI,
535
149k
           box, rotate, out->upsideDown());
536
149k
  fontChanged = gFalse;
537
149k
  haveSavedClipPath = gFalse;
538
149k
  clip = clipNone;
539
149k
  ignoreUndef = 0;
540
149k
  out->startPage(pageNum, state);
541
149k
  out->setDefaultCTM(state->getCTM());
542
149k
  out->updateAll(state);
543
1.04M
  for (i = 0; i < 6; ++i) {
544
894k
    baseMatrix[i] = state->getCTM()[i];
545
894k
  }
546
149k
  formDepth = 0;
547
149k
  markedContentStack = new GList();
548
149k
  ocState = gTrue;
549
149k
  parser = NULL;
550
149k
  contentStreamStack = new GList();
551
149k
  abortCheckCbk = abortCheckCbkA;
552
149k
  abortCheckCbkData = abortCheckCbkDataA;
553
554
  // set crop box
555
149k
  if (cropBox) {
556
12.6k
    state->moveTo(cropBox->x1, cropBox->y1);
557
12.6k
    state->lineTo(cropBox->x2, cropBox->y1);
558
12.6k
    state->lineTo(cropBox->x2, cropBox->y2);
559
12.6k
    state->lineTo(cropBox->x1, cropBox->y2);
560
12.6k
    state->closePath();
561
12.6k
    state->clip();
562
12.6k
    out->clip(state);
563
12.6k
    state->clearPath();
564
12.6k
  }
565
149k
}
566
567
Gfx::Gfx(PDFDoc *docA, OutputDev *outA, LocalParams *localParams,
568
   Dict *resDict, PDFRectangle *box, PDFRectangle *cropBox,
569
   GBool (*abortCheckCbkA)(void *data),
570
0
   void *abortCheckCbkDataA) {
571
0
  int i;
572
573
0
  doc = docA;
574
0
  xref = doc->getXRef();
575
0
  subPage = gTrue;
576
0
  printCommands = globalParams->getPrintCommands();
577
0
  defaultFont = NULL;
578
579
  // start the resource stack
580
0
  res = new GfxResources(xref, resDict, NULL);
581
582
  // initialize
583
0
  out = outA;
584
0
  state = new GfxState(localParams, 72, 72, box, 0, gFalse);
585
0
  fontChanged = gFalse;
586
0
  haveSavedClipPath = gFalse;
587
0
  clip = clipNone;
588
0
  ignoreUndef = 0;
589
0
  for (i = 0; i < 6; ++i) {
590
0
    baseMatrix[i] = state->getCTM()[i];
591
0
  }
592
0
  formDepth = 0;
593
0
  markedContentStack = new GList();
594
0
  ocState = gTrue;
595
0
  parser = NULL;
596
0
  contentStreamStack = new GList();
597
0
  abortCheckCbk = abortCheckCbkA;
598
0
  abortCheckCbkData = abortCheckCbkDataA;
599
600
  // set crop box
601
0
  if (cropBox) {
602
0
    state->moveTo(cropBox->x1, cropBox->y1);
603
0
    state->lineTo(cropBox->x2, cropBox->y1);
604
0
    state->lineTo(cropBox->x2, cropBox->y2);
605
0
    state->lineTo(cropBox->x1, cropBox->y2);
606
0
    state->closePath();
607
0
    state->clip();
608
0
    out->clip(state);
609
0
    state->clearPath();
610
0
  }
611
0
}
612
613
148k
Gfx::~Gfx() {
614
148k
  if (defaultFont) {
615
11.3k
    delete defaultFont;
616
11.3k
  }
617
148k
  if (!subPage) {
618
148k
    out->endPage();
619
148k
  }
620
148k
  while (state->hasSaves()) {
621
0
    restoreState();
622
0
  }
623
148k
  delete state;
624
297k
  while (res) {
625
148k
    popResources();
626
148k
  }
627
148k
  deleteGList(markedContentStack, GfxMarkedContent);
628
148k
  delete contentStreamStack;
629
148k
}
630
631
257k
void Gfx::display(Object *objRef, GBool topLevel) {
632
257k
  Object obj1, obj2;
633
257k
  int i;
634
635
257k
  objRef->fetch(xref, &obj1);
636
257k
  if (obj1.isArray()) {
637
3.16k
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
638
2.43k
      obj1.arrayGetNF(i, &obj2);
639
2.43k
      if (checkForContentStreamLoop(&obj2)) {
640
0
  obj2.free();
641
0
  obj1.free();
642
0
  return;
643
0
      }
644
2.43k
      obj2.free();
645
2.43k
    }
646
2.41k
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
647
1.76k
      obj1.arrayGet(i, &obj2);
648
1.76k
      if (!obj2.isStream()) {
649
89
  error(errSyntaxError, -1, "Invalid object type for content stream");
650
89
  obj2.free();
651
89
  obj1.free();
652
89
  return;
653
89
      }
654
1.67k
      obj2.free();
655
1.67k
    }
656
645
    contentStreamStack->append(&obj1);
657
256k
  } else if (obj1.isStream()) {
658
255k
    if (checkForContentStreamLoop(objRef)) {
659
107k
      obj1.free();
660
107k
      return;
661
107k
    }
662
148k
    contentStreamStack->append(objRef);
663
148k
  } else {
664
582
    error(errSyntaxError, -1, "Invalid object type for content stream");
665
582
    obj1.free();
666
582
    return;
667
582
  }
668
148k
  parser = new Parser(xref, new Lexer(xref, &obj1), gFalse);
669
148k
  go(topLevel);
670
148k
  delete parser;
671
148k
  parser = NULL;
672
148k
  contentStreamStack->del(contentStreamStack->getLength() - 1);
673
148k
  obj1.free();
674
148k
}
675
676
// If <ref> is already on contentStreamStack, i.e., if there is a loop
677
// in the content streams, report an error, and return true.
678
258k
GBool Gfx::checkForContentStreamLoop(Object *ref) {
679
258k
  Object *objPtr;
680
258k
  Object obj1;
681
258k
  int i, j;
682
683
258k
  if (ref->isRef()) {
684
605k
    for (i = 0; i < contentStreamStack->getLength(); ++i) {
685
459k
      objPtr = (Object *)contentStreamStack->get(i);
686
459k
      if (objPtr->isRef()) {
687
454k
  if (ref->getRefNum() == objPtr->getRefNum() &&
688
107k
      ref->getRefGen() == objPtr->getRefGen()) {
689
107k
    error(errSyntaxError, -1, "Loop in content streams");
690
107k
    return gTrue;
691
107k
  }
692
454k
      } else if (objPtr->isArray()) {
693
10.0k
  for (j = 0; j < objPtr->arrayGetLength(); ++j) {
694
5.16k
    objPtr->arrayGetNF(j, &obj1);
695
5.16k
    if (obj1.isRef()) {
696
5.16k
      if (ref->getRefNum() == obj1.getRefNum() &&
697
0
    ref->getRefGen() == obj1.getRefGen()) {
698
0
        error(errSyntaxError, -1, "Loop in content streams");
699
0
        obj1.free();
700
0
        return gTrue;
701
0
      }
702
5.16k
    }
703
5.16k
    obj1.free();
704
5.16k
  }
705
4.89k
      }
706
459k
    }
707
254k
  }
708
150k
  return gFalse;
709
258k
}
710
711
148k
void Gfx::go(GBool topLevel) {
712
148k
  Object obj;
713
148k
  Object args[maxArgs];
714
148k
  GBool aborted;
715
148k
  int numArgs, i;
716
148k
  int errCount;
717
718
  // scan a sequence of objects
719
148k
  opCounter = 0;
720
148k
  aborted = gFalse;
721
148k
  errCount = 0;
722
148k
  numArgs = 0;
723
148k
  getContentObj(&obj);
724
35.1M
  while (!obj.isEOF()) {
725
726
    // check for an abort
727
34.9M
    ++opCounter;
728
34.9M
    if (abortCheckCbk && opCounter > 100) {
729
0
      if ((*abortCheckCbk)(abortCheckCbkData)) {
730
0
  aborted = gTrue;
731
0
  break;
732
0
      }
733
0
      opCounter = 0;
734
0
    }
735
736
    // got a command - execute it
737
34.9M
    if (obj.isCmd()) {
738
10.5M
      if (printCommands) {
739
0
  obj.print(stdout);
740
0
  for (i = 0; i < numArgs; ++i) {
741
0
    printf(" ");
742
0
    args[i].print(stdout);
743
0
  }
744
0
  printf("\n");
745
0
  fflush(stdout);
746
0
      }
747
10.5M
      if (!execOp(&obj, args, numArgs)) {
748
5.43M
  ++errCount;
749
5.43M
      }
750
10.5M
      obj.free();
751
33.4M
      for (i = 0; i < numArgs; ++i)
752
22.8M
  args[i].free();
753
10.5M
      numArgs = 0;
754
755
      // check for too many errors
756
10.5M
      if (errCount > contentStreamErrorLimit) {
757
1.97k
  error(errSyntaxError, -1,
758
1.97k
        "Too many errors - giving up on this content stream");
759
1.97k
  break;
760
1.97k
      }
761
762
    // got an argument - save it
763
24.4M
    } else if (numArgs < maxArgs) {
764
22.9M
      args[numArgs++] = obj;
765
766
    // too many arguments - something is wrong
767
22.9M
    } else {
768
1.50M
      error(errSyntaxError, getPos(), "Too many args in content stream");
769
1.50M
      if (printCommands) {
770
0
  printf("throwing away arg: ");
771
0
  obj.print(stdout);
772
0
  printf("\n");
773
0
  fflush(stdout);
774
0
      }
775
1.50M
      obj.free();
776
1.50M
    }
777
778
    // grab the next object
779
34.9M
    getContentObj(&obj);
780
34.9M
  }
781
148k
  obj.free();
782
783
  // args at end with no command
784
148k
  if (numArgs > 0) {
785
39.3k
    if (!aborted) {
786
39.3k
      error(errSyntaxError, getPos(), "Leftover args in content stream");
787
39.3k
      if (printCommands) {
788
0
  printf("%d leftovers:", numArgs);
789
0
  for (i = 0; i < numArgs; ++i) {
790
0
    printf(" ");
791
0
    args[i].print(stdout);
792
0
  }
793
0
  printf("\n");
794
0
  fflush(stdout);
795
0
      }
796
39.3k
    }
797
112k
    for (i = 0; i < numArgs; ++i) {
798
73.3k
      args[i].free();
799
73.3k
    }
800
39.3k
  }
801
148k
}
802
803
37.1M
void Gfx::getContentObj(Object *obj) {
804
37.1M
  parser->getObj(obj);
805
37.1M
  if (obj->isRef()) {
806
23.4k
    error(errSyntaxError, getPos(), "Indirect reference in content stream");
807
23.4k
    obj->free();
808
23.4k
    obj->initError();
809
23.4k
  }
810
37.1M
}
811
812
// Returns true if successful, false on error.
813
10.5M
GBool Gfx::execOp(Object *cmd, Object args[], int numArgs) {
814
10.5M
  Operator *op;
815
10.5M
  char *name;
816
10.5M
  Object *argPtr;
817
10.5M
  int i;
818
819
  // find operator
820
10.5M
  name = cmd->getCmd();
821
10.5M
  if (!(op = findOp(name))) {
822
4.49M
    if (ignoreUndef > 0) {
823
124k
      return gTrue;
824
124k
    }
825
4.36M
    error(errSyntaxError, getPos(), "Unknown operator '{0:s}'", name);
826
4.36M
    return gFalse;
827
4.49M
  }
828
829
  // type check args
830
6.04M
  argPtr = args;
831
6.04M
  if (op->numArgs >= 0) {
832
6.00M
    if (numArgs < op->numArgs) {
833
908k
      error(errSyntaxError, getPos(),
834
908k
      "Too few ({0:d}) args to '{1:s}' operator", numArgs, name);
835
908k
      return gFalse;
836
908k
    }
837
5.09M
    if (numArgs > op->numArgs) {
838
#if 0
839
      error(errSyntaxWarning, getPos(),
840
      "Too many ({0:d}) args to '{1:s}' operator", numArgs, name);
841
#endif
842
1.37M
      argPtr += numArgs - op->numArgs;
843
1.37M
      numArgs = op->numArgs;
844
1.37M
    }
845
5.09M
  } else {
846
37.4k
    if (numArgs > -op->numArgs) {
847
1.23k
      error(errSyntaxWarning, getPos(),
848
1.23k
      "Too many ({0:d}) args to '{1:s}' operator",
849
1.23k
      numArgs, name);
850
1.23k
    }
851
37.4k
  }
852
10.9M
  for (i = 0; i < numArgs; ++i) {
853
5.97M
    if (!checkArg(&argPtr[i], op->tchk[i])) {
854
162k
      error(errSyntaxError, getPos(),
855
162k
      "Arg #{0:d} to '{1:s}' operator is wrong type ({2:s})",
856
162k
      i, name, argPtr[i].getTypeName());
857
162k
      return gFalse;
858
162k
    }
859
5.97M
  }
860
861
  // do it
862
4.97M
  (this->*op->func)(argPtr, numArgs);
863
864
4.97M
  return gTrue;
865
5.13M
}
866
867
10.5M
Operator *Gfx::findOp(char *name) {
868
10.5M
  int a, b, m, cmp;
869
870
10.5M
  a = -1;
871
10.5M
  b = numOps;
872
10.5M
  cmp = 0; // make gcc happy
873
  // invariant: opTab[a] < name < opTab[b]
874
71.7M
  while (b - a > 1) {
875
61.2M
    m = (a + b) / 2;
876
61.2M
    cmp = strcmp(opTab[m].name, name);
877
61.2M
    if (cmp < 0)
878
33.6M
      a = m;
879
27.6M
    else if (cmp > 0)
880
21.6M
      b = m;
881
6.04M
    else
882
6.04M
      a = b = m;
883
61.2M
  }
884
10.5M
  if (cmp != 0)
885
4.49M
    return NULL;
886
6.04M
  return &opTab[a];
887
10.5M
}
888
889
5.97M
GBool Gfx::checkArg(Object *arg, TchkType type) {
890
5.97M
  switch (type) {
891
0
  case tchkBool:   return arg->isBool();
892
66.6k
  case tchkInt:    return arg->isInt();
893
4.98M
  case tchkNum:    return arg->isNum();
894
425k
  case tchkString: return arg->isString();
895
387k
  case tchkName:   return arg->isName();
896
73.1k
  case tchkArray:  return arg->isArray();
897
16.0k
  case tchkProps:  return arg->isDict() || arg->isName();
898
25.7k
  case tchkSCN:    return arg->isNum() || arg->isName();
899
0
  case tchkNone:   return gFalse;
900
5.97M
  }
901
0
  return gFalse;
902
5.97M
}
903
904
11.6M
GFileOffset Gfx::getPos() {
905
11.6M
  return parser ? parser->getPos() : -1;
906
11.6M
}
907
908
//------------------------------------------------------------------------
909
// graphics state operators
910
//------------------------------------------------------------------------
911
912
357k
void Gfx::opSave(Object args[], int numArgs) {
913
357k
  saveState();
914
357k
}
915
916
372k
void Gfx::opRestore(Object args[], int numArgs) {
917
372k
  restoreState();
918
372k
}
919
920
159k
void Gfx::opConcat(Object args[], int numArgs) {
921
159k
  state->concatCTM(args[0].getNum(), args[1].getNum(),
922
159k
       args[2].getNum(), args[3].getNum(),
923
159k
       args[4].getNum(), args[5].getNum());
924
159k
  out->updateCTM(state, args[0].getNum(), args[1].getNum(),
925
159k
     args[2].getNum(), args[3].getNum(),
926
159k
     args[4].getNum(), args[5].getNum());
927
159k
  fontChanged = gTrue;
928
159k
}
929
930
8.12k
void Gfx::opSetDash(Object args[], int numArgs) {
931
8.12k
  Array *a;
932
8.12k
  int length;
933
8.12k
  Object obj;
934
8.12k
  double *dash;
935
8.12k
  int i;
936
937
8.12k
  a = args[0].getArray();
938
8.12k
  length = a->getLength();
939
8.12k
  if (length == 0) {
940
6.55k
    dash = NULL;
941
6.55k
  } else {
942
1.56k
    dash = (double *)gmallocn(length, sizeof(double));
943
40.7k
    for (i = 0; i < length; ++i) {
944
39.1k
      dash[i] = a->get(i, &obj)->getNum();
945
39.1k
      obj.free();
946
39.1k
    }
947
1.56k
  }
948
8.12k
  state->setLineDash(dash, length, args[1].getNum());
949
8.12k
  out->updateLineDash(state);
950
8.12k
}
951
952
103k
void Gfx::opSetFlat(Object args[], int numArgs) {
953
103k
  state->setFlatness((int)args[0].getNum());
954
103k
  out->updateFlatness(state);
955
103k
}
956
957
12.0k
void Gfx::opSetLineJoin(Object args[], int numArgs) {
958
12.0k
  int lineJoin;
959
960
12.0k
  lineJoin = args[0].getInt();
961
12.0k
  if (lineJoin < 0 || lineJoin > 2) {
962
4.77k
    lineJoin = 0;
963
4.77k
  }
964
12.0k
  state->setLineJoin(lineJoin);
965
12.0k
  out->updateLineJoin(state);
966
12.0k
}
967
968
37.9k
void Gfx::opSetLineCap(Object args[], int numArgs) {
969
37.9k
  int lineCap;
970
971
37.9k
  lineCap = args[0].getInt();
972
37.9k
  if (lineCap < 0 || lineCap > 2) {
973
15.1k
    lineCap = 0;
974
15.1k
  }
975
37.9k
  state->setLineCap(lineCap);
976
37.9k
  out->updateLineCap(state);
977
37.9k
}
978
979
22.2k
void Gfx::opSetMiterLimit(Object args[], int numArgs) {
980
22.2k
  state->setMiterLimit(args[0].getNum());
981
22.2k
  out->updateMiterLimit(state);
982
22.2k
}
983
984
78.1k
void Gfx::opSetLineWidth(Object args[], int numArgs) {
985
78.1k
  state->setLineWidth(args[0].getNum());
986
78.1k
  out->updateLineWidth(state);
987
78.1k
}
988
989
33.4k
void Gfx::opSetExtGState(Object args[], int numArgs) {
990
33.4k
  Object obj1, obj2, obj3, objRef3, obj4, obj5, backdropColorObj;
991
33.4k
  Object args2[2];
992
33.4k
  GfxBlendMode mode;
993
33.4k
  GBool haveFillOP;
994
33.4k
  Function *funcs[4];
995
33.4k
  GBool alpha, knockout;
996
33.4k
  double opac;
997
33.4k
  int i;
998
999
33.4k
  if (!res->lookupGState(args[0].getName(), &obj1)) {
1000
16.1k
    return;
1001
16.1k
  }
1002
17.2k
  if (!obj1.isDict()) {
1003
3.70k
    error(errSyntaxError, getPos(),
1004
3.70k
    "ExtGState '{0:s}' is wrong type", args[0].getName());
1005
3.70k
    obj1.free();
1006
3.70k
    return;
1007
3.70k
  }
1008
13.5k
  if (printCommands) {
1009
0
    printf("  gfx state dict: ");
1010
0
    obj1.print();
1011
0
    printf("\n");
1012
0
  }
1013
1014
  // parameters that are also set by individual PDF operators
1015
13.5k
  if (obj1.dictLookup("LW", &obj2)->isNum()) {
1016
208
    opSetLineWidth(&obj2, 1);
1017
208
  }
1018
13.5k
  obj2.free();
1019
13.5k
  if (obj1.dictLookup("LC", &obj2)->isInt()) {
1020
208
    opSetLineCap(&obj2, 1);
1021
208
  }
1022
13.5k
  obj2.free();
1023
13.5k
  if (obj1.dictLookup("LJ", &obj2)->isInt()) {
1024
208
    opSetLineJoin(&obj2, 1);
1025
208
  }
1026
13.5k
  obj2.free();
1027
13.5k
  if (obj1.dictLookup("ML", &obj2)->isNum()) {
1028
208
    opSetMiterLimit(&obj2, 1);
1029
208
  }
1030
13.5k
  obj2.free();
1031
13.5k
  if (obj1.dictLookup("D", &obj2)->isArray() &&
1032
97
      obj2.arrayGetLength() == 2) {
1033
97
    obj2.arrayGet(0, &args2[0]);
1034
97
    obj2.arrayGet(1, &args2[1]);
1035
97
    if (args2[0].isArray() && args2[1].isNum()) {
1036
0
      opSetDash(args2, 2);
1037
0
    }
1038
97
    args2[0].free();
1039
97
    args2[1].free();
1040
97
  }
1041
13.5k
  obj2.free();
1042
13.5k
  if (obj1.dictLookup("FL", &obj2)->isNum()) {
1043
0
    opSetFlat(&obj2, 1);
1044
0
  }
1045
13.5k
  obj2.free();
1046
13.5k
  if (obj1.dictLookup("RI", &obj2)->isName()) {
1047
0
    opSetRenderingIntent(&obj2, 1);
1048
0
  }
1049
13.5k
  obj2.free();
1050
1051
  // font
1052
13.5k
  if (obj1.dictLookup("Font", &obj2)->isArray() &&
1053
0
      obj2.arrayGetLength() == 2) {
1054
0
    obj2.arrayGetNF(0, &obj3);
1055
0
    obj2.arrayGetNF(1, &obj4);
1056
0
    if (obj3.isRef() && obj4.isNum()) {
1057
0
      doSetFont(res->lookupFontByRef(obj3.getRef()), obj4.getNum());
1058
0
    }
1059
0
    obj3.free();
1060
0
    obj4.free();
1061
0
  }
1062
13.5k
  obj2.free();
1063
1064
  // transparency support: blend mode, fill/stroke opacity
1065
13.5k
  if (!obj1.dictLookup("BM", &obj2)->isNull()) {
1066
6.78k
    if (state->parseBlendMode(&obj2, &mode)) {
1067
6.18k
      state->setBlendMode(mode);
1068
6.18k
      out->updateBlendMode(state);
1069
6.18k
    } else {
1070
606
      error(errSyntaxError, getPos(), "Invalid blend mode in ExtGState");
1071
606
    }
1072
6.78k
  }
1073
13.5k
  obj2.free();
1074
13.5k
  if (obj1.dictLookup("ca", &obj2)->isNum()) {
1075
6.87k
    opac = obj2.getNum();
1076
6.87k
    state->setFillOpacity(opac < 0 ? 0 : opac > 1 ? 1 : opac);
1077
6.87k
    out->updateFillOpacity(state);
1078
6.87k
  }
1079
13.5k
  obj2.free();
1080
13.5k
  if (obj1.dictLookup("CA", &obj2)->isNum()) {
1081
6.95k
    opac = obj2.getNum();
1082
6.95k
    state->setStrokeOpacity(opac < 0 ? 0 : opac > 1 ? 1 : opac);
1083
6.95k
    out->updateStrokeOpacity(state);
1084
6.95k
  }
1085
13.5k
  obj2.free();
1086
1087
  // fill/stroke overprint, overprint mode
1088
13.5k
  if ((haveFillOP = (obj1.dictLookup("op", &obj2)->isBool()))) {
1089
3.48k
    if (!state->getIgnoreColorOps()) {
1090
3.38k
      state->setFillOverprint(obj2.getBool());
1091
3.38k
      out->updateFillOverprint(state);
1092
3.38k
    } else {
1093
105
      error(errSyntaxWarning, getPos(), "Ignoring overprint setting"
1094
105
      " in uncolored Type 3 char or tiling pattern");
1095
105
    }
1096
3.48k
  }
1097
13.5k
  obj2.free();
1098
13.5k
  if (obj1.dictLookup("OP", &obj2)->isBool()) {
1099
3.41k
    if (!state->getIgnoreColorOps()) {
1100
3.30k
      state->setStrokeOverprint(obj2.getBool());
1101
3.30k
      out->updateStrokeOverprint(state);
1102
3.30k
      if (!haveFillOP) {
1103
644
  state->setFillOverprint(obj2.getBool());
1104
644
  out->updateFillOverprint(state);
1105
644
      }
1106
3.30k
    } else {
1107
106
      error(errSyntaxWarning, getPos(), "Ignoring overprint setting"
1108
106
      " in uncolored Type 3 char or tiling pattern");
1109
106
    }
1110
3.41k
  }
1111
13.5k
  obj2.free();
1112
13.5k
  if (obj1.dictLookup("OPM", &obj2)->isInt()) {
1113
3.15k
    if (!state->getIgnoreColorOps()) {
1114
3.06k
      state->setOverprintMode(obj2.getInt());
1115
3.06k
      out->updateOverprintMode(state);
1116
3.06k
    } else {
1117
94
      error(errSyntaxWarning, getPos(), "Ignoring overprint setting"
1118
94
      " in uncolored Type 3 char or tiling pattern");
1119
94
    }
1120
3.15k
  }
1121
13.5k
  obj2.free();
1122
1123
  // stroke adjust
1124
13.5k
  if (obj1.dictLookup("SA", &obj2)->isBool()) {
1125
3.62k
    state->setStrokeAdjust(obj2.getBool());
1126
3.62k
    out->updateStrokeAdjust(state);
1127
3.62k
  }
1128
13.5k
  obj2.free();
1129
1130
  // transfer function
1131
13.5k
  if (obj1.dictLookup("TR2", &obj2)->isNull()) {
1132
13.3k
    obj2.free();
1133
13.3k
    obj1.dictLookup("TR", &obj2);
1134
13.3k
  }
1135
13.5k
  if (!obj2.isNull()) {
1136
2.56k
    if (!state->getIgnoreColorOps()) {
1137
2.56k
      if (obj2.isName("Default") ||
1138
2.39k
    obj2.isName("Identity")) {
1139
689
  funcs[0] = funcs[1] = funcs[2] = funcs[3] = NULL;
1140
689
  state->setTransfer(funcs);
1141
689
  out->updateTransfer(state);
1142
1.87k
      } else if (obj2.isArray() && obj2.arrayGetLength() == 4) {
1143
141
  for (i = 0; i < 4; ++i) {
1144
130
    obj2.arrayGet(i, &obj3);
1145
130
    funcs[i] = Function::parse(&obj3, 1, 1);
1146
130
    obj3.free();
1147
130
    if (!funcs[i]) {
1148
46
      break;
1149
46
    }
1150
130
  }
1151
57
  if (i == 4) {
1152
11
    state->setTransfer(funcs);
1153
11
    out->updateTransfer(state);
1154
11
  }
1155
1.81k
      } else if (obj2.isName() || obj2.isDict() || obj2.isStream()) {
1156
1.61k
  if ((funcs[0] = Function::parse(&obj2, 1, 1))) {
1157
228
    funcs[1] = funcs[2] = funcs[3] = NULL;
1158
228
    state->setTransfer(funcs);
1159
228
    out->updateTransfer(state);
1160
228
  }
1161
1.61k
      } else {
1162
200
  error(errSyntaxError, getPos(),
1163
200
        "Invalid transfer function in ExtGState");
1164
200
      }
1165
2.56k
    } else {
1166
0
      error(errSyntaxWarning, getPos(), "Ignoring transfer function setting"
1167
0
      " in uncolored Type 3 char or tiling pattern");
1168
0
    }
1169
2.56k
  }
1170
13.5k
  obj2.free();
1171
1172
  // alpha is shape
1173
13.5k
  if (obj1.dictLookup("AIS", &obj2)->isBool()) {
1174
5.00k
    state->setAlphaIsShape(obj2.getBool());
1175
5.00k
    out->updateAlphaIsShape(state);
1176
5.00k
  }
1177
13.5k
  obj2.free();
1178
1179
  // soft mask
1180
13.5k
  if (!obj1.dictLookup("SMask", &obj2)->isNull()) {
1181
6.12k
    if (obj2.isName("None")) {
1182
1.31k
      out->clearSoftMask(state);
1183
4.80k
    } else if (obj2.isDict()) {
1184
4.44k
      obj2.dictLookup("S", &obj3);
1185
4.44k
      if (obj3.isName("Alpha")) {
1186
1.73k
  alpha = gTrue;
1187
2.70k
      } else if (obj3.isName("Luminosity")) {
1188
1.57k
  alpha = gFalse;
1189
1.57k
      } else {
1190
1.12k
  error(errSyntaxError, getPos(),
1191
1.12k
        "Missing S (subtype) entry in soft mask");
1192
1.12k
  alpha = gFalse;
1193
1.12k
      }
1194
4.44k
      obj3.free();
1195
4.44k
      funcs[0] = NULL;
1196
4.44k
      if (!obj2.dictLookup("TR", &obj3)->isNull()) {
1197
2.58k
  if (obj3.isName("Default") ||
1198
2.58k
      obj3.isName("Identity")) {
1199
0
    funcs[0] = NULL;
1200
2.58k
  } else {
1201
2.58k
    if (!(funcs[0] = Function::parse(&obj3, 1, 1))) {
1202
2.38k
      error(errSyntaxError, getPos(),
1203
2.38k
      "Invalid transfer function in soft mask in ExtGState");
1204
2.38k
      delete funcs[0];
1205
2.38k
      funcs[0] = NULL;
1206
2.38k
    }
1207
2.58k
  }
1208
2.58k
      }
1209
4.44k
      obj3.free();
1210
4.44k
      obj2.dictLookup("BC", &backdropColorObj);
1211
4.44k
      if (obj2.dictLookup("G", &obj3)->isStream()) {
1212
2.65k
  if (obj3.streamGetDict()->lookup("Group", &obj4)->isDict()) {
1213
2.29k
    knockout = gFalse;
1214
2.29k
    if (obj4.dictLookup("K", &obj5)->isBool()) {
1215
1.50k
      knockout = obj5.getBool();
1216
1.50k
    }
1217
2.29k
    obj5.free();
1218
2.29k
    obj2.dictLookupNF("G", &objRef3);
1219
    // it doesn't make sense for softmasks to be non-isolated,
1220
    // because they're blended with a backdrop color, rather
1221
    // than the original backdrop
1222
2.29k
    doSoftMask(&obj3, &objRef3, alpha, gTrue, knockout, funcs[0],
1223
2.29k
         &backdropColorObj);
1224
2.29k
    objRef3.free();
1225
2.29k
    if (funcs[0]) {
1226
141
      delete funcs[0];
1227
141
    }
1228
2.29k
  } else {
1229
351
    error(errSyntaxError, getPos(),
1230
351
    "Invalid soft mask in ExtGState - missing group");
1231
351
  }
1232
2.65k
  obj4.free();
1233
2.65k
      } else {
1234
1.79k
  error(errSyntaxError, getPos(),
1235
1.79k
        "Invalid soft mask in ExtGState - missing group");
1236
1.79k
      }
1237
4.44k
      obj3.free();
1238
4.44k
      backdropColorObj.free();
1239
4.44k
    } else if (!obj2.isNull()) {
1240
362
      error(errSyntaxError, getPos(), "Invalid soft mask in ExtGState");
1241
362
    }
1242
6.12k
  }
1243
13.5k
  obj2.free();
1244
1245
13.5k
  obj1.free();
1246
13.5k
}
1247
1248
void Gfx::doSoftMask(Object *str, Object *strRef, GBool alpha,
1249
         GBool isolated, GBool knockout,
1250
2.29k
         Function *transferFunc, Object *backdropColorObj) {
1251
2.29k
  Dict *dict, *resDict;
1252
2.29k
  double m[6], bbox[4];
1253
2.29k
  Object obj1, obj2;
1254
2.29k
  int i;
1255
1256
  // get stream dict
1257
2.29k
  dict = str->streamGetDict();
1258
1259
  // check form type
1260
2.29k
  dict->lookup("FormType", &obj1);
1261
2.29k
  if (!(obj1.isNull() || (obj1.isInt() && obj1.getInt() == 1))) {
1262
289
    error(errSyntaxError, getPos(), "Unknown form type");
1263
289
  }
1264
2.29k
  obj1.free();
1265
1266
  // get bounding box
1267
2.29k
  dict->lookup("BBox", &obj1);
1268
2.29k
  if (!obj1.isArray()) {
1269
228
    obj1.free();
1270
228
    error(errSyntaxError, getPos(), "Bad form bounding box");
1271
228
    return;
1272
228
  }
1273
10.3k
  for (i = 0; i < 4; ++i) {
1274
8.28k
    obj1.arrayGet(i, &obj2);
1275
8.28k
    bbox[i] = obj2.getNum();
1276
8.28k
    obj2.free();
1277
8.28k
  }
1278
2.07k
  obj1.free();
1279
1280
  // get matrix
1281
2.07k
  dict->lookup("Matrix", &obj1);
1282
2.07k
  if (obj1.isArray()) {
1283
5.20k
    for (i = 0; i < 6; ++i) {
1284
4.46k
      obj1.arrayGet(i, &obj2);
1285
4.46k
      m[i] = obj2.getNum();
1286
4.46k
      obj2.free();
1287
4.46k
    }
1288
1.32k
  } else {
1289
1.32k
    m[0] = 1; m[1] = 0;
1290
1.32k
    m[2] = 0; m[3] = 1;
1291
1.32k
    m[4] = 0; m[5] = 0;
1292
1.32k
  }
1293
2.07k
  obj1.free();
1294
1295
  // get resources
1296
2.07k
  dict->lookup("Resources", &obj1);
1297
2.07k
  resDict = obj1.isDict() ? obj1.getDict() : (Dict *)NULL;
1298
1299
  // draw it
1300
2.07k
  drawForm(strRef, resDict, m, bbox, gTrue, gTrue, isolated, knockout,
1301
2.07k
     alpha, transferFunc, backdropColorObj);
1302
1303
2.07k
  obj1.free();
1304
2.07k
}
1305
1306
4.58k
void Gfx::opSetRenderingIntent(Object args[], int numArgs) {
1307
4.58k
  GfxRenderingIntent ri;
1308
1309
4.58k
  if (state->getIgnoreColorOps()) {
1310
65
    error(errSyntaxWarning, getPos(), "Ignoring rendering intent setting"
1311
65
    " in uncolored Type 3 char or tiling pattern");
1312
65
    return;
1313
65
  }
1314
4.52k
  ri = parseRenderingIntent(args[0].getName());
1315
4.52k
  state->setRenderingIntent(ri);
1316
4.52k
  out->updateRenderingIntent(state);
1317
4.52k
}
1318
1319
4.52k
GfxRenderingIntent Gfx::parseRenderingIntent(const char *name) {
1320
4.52k
  if (!strcmp(name, "AbsoluteColorimetric")) {
1321
0
    return gfxRenderingIntentAbsoluteColorimetric;
1322
0
  }
1323
4.52k
  if (!strcmp(name, "Saturation")) {
1324
0
    return gfxRenderingIntentSaturation;
1325
0
  }
1326
4.52k
  if (!strcmp(name, "Perceptual")) {
1327
1.63k
    return gfxRenderingIntentPerceptual;
1328
1.63k
  }
1329
2.88k
  return gfxRenderingIntentRelativeColorimetric;
1330
4.52k
}
1331
1332
//------------------------------------------------------------------------
1333
// color operators
1334
//------------------------------------------------------------------------
1335
1336
45.2k
void Gfx::opSetFillGray(Object args[], int numArgs) {
1337
45.2k
  GfxColor color;
1338
1339
45.2k
  if (state->getIgnoreColorOps()) {
1340
3.56k
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1341
3.56k
    " in uncolored Type 3 char or tiling pattern");
1342
3.56k
    return;
1343
3.56k
  }
1344
41.6k
  state->setFillPattern(NULL);
1345
41.6k
  state->setFillColorSpace(GfxColorSpace::create(csDeviceGray));
1346
41.6k
  out->updateFillColorSpace(state);
1347
41.6k
  color.c[0] = dblToCol(args[0].getNum());
1348
41.6k
  state->setFillColor(&color);
1349
41.6k
  out->updateFillColor(state);
1350
41.6k
}
1351
1352
67.7k
void Gfx::opSetStrokeGray(Object args[], int numArgs) {
1353
67.7k
  GfxColor color;
1354
1355
67.7k
  if (state->getIgnoreColorOps()) {
1356
3.27k
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1357
3.27k
    " in uncolored Type 3 char or tiling pattern");
1358
3.27k
    return;
1359
3.27k
  }
1360
64.4k
  state->setStrokePattern(NULL);
1361
64.4k
  state->setStrokeColorSpace(GfxColorSpace::create(csDeviceGray));
1362
64.4k
  out->updateStrokeColorSpace(state);
1363
64.4k
  color.c[0] = dblToCol(args[0].getNum());
1364
64.4k
  state->setStrokeColor(&color);
1365
64.4k
  out->updateStrokeColor(state);
1366
64.4k
}
1367
1368
9.46k
void Gfx::opSetFillCMYKColor(Object args[], int numArgs) {
1369
9.46k
  GfxColor color;
1370
9.46k
  int i;
1371
1372
9.46k
  if (state->getIgnoreColorOps()) {
1373
450
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1374
450
    " in uncolored Type 3 char or tiling pattern");
1375
450
    return;
1376
450
  }
1377
9.01k
  state->setFillPattern(NULL);
1378
9.01k
  state->setFillColorSpace(GfxColorSpace::create(csDeviceCMYK));
1379
9.01k
  out->updateFillColorSpace(state);
1380
45.0k
  for (i = 0; i < 4; ++i) {
1381
36.0k
    color.c[i] = dblToCol(args[i].getNum());
1382
36.0k
  }
1383
9.01k
  state->setFillColor(&color);
1384
9.01k
  out->updateFillColor(state);
1385
9.01k
}
1386
1387
3.47k
void Gfx::opSetStrokeCMYKColor(Object args[], int numArgs) {
1388
3.47k
  GfxColor color;
1389
3.47k
  int i;
1390
1391
3.47k
  if (state->getIgnoreColorOps()) {
1392
318
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1393
318
    " in uncolored Type 3 char or tiling pattern");
1394
318
    return;
1395
318
  }
1396
3.15k
  state->setStrokePattern(NULL);
1397
3.15k
  state->setStrokeColorSpace(GfxColorSpace::create(csDeviceCMYK));
1398
3.15k
  out->updateStrokeColorSpace(state);
1399
15.7k
  for (i = 0; i < 4; ++i) {
1400
12.6k
    color.c[i] = dblToCol(args[i].getNum());
1401
12.6k
  }
1402
3.15k
  state->setStrokeColor(&color);
1403
3.15k
  out->updateStrokeColor(state);
1404
3.15k
}
1405
1406
81.2k
void Gfx::opSetFillRGBColor(Object args[], int numArgs) {
1407
81.2k
  GfxColor color;
1408
81.2k
  int i;
1409
1410
81.2k
  if (state->getIgnoreColorOps()) {
1411
963
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1412
963
    " in uncolored Type 3 char or tiling pattern");
1413
963
    return;
1414
963
  }
1415
80.3k
  state->setFillPattern(NULL);
1416
80.3k
  state->setFillColorSpace(GfxColorSpace::create(csDeviceRGB));
1417
80.3k
  out->updateFillColorSpace(state);
1418
321k
  for (i = 0; i < 3; ++i) {
1419
240k
    color.c[i] = dblToCol(args[i].getNum());
1420
240k
  }
1421
80.3k
  state->setFillColor(&color);
1422
80.3k
  out->updateFillColor(state);
1423
80.3k
}
1424
1425
26.0k
void Gfx::opSetStrokeRGBColor(Object args[], int numArgs) {
1426
26.0k
  GfxColor color;
1427
26.0k
  int i;
1428
1429
26.0k
  if (state->getIgnoreColorOps()) {
1430
2.07k
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1431
2.07k
    " in uncolored Type 3 char or tiling pattern");
1432
2.07k
    return;
1433
2.07k
  }
1434
23.9k
  state->setStrokePattern(NULL);
1435
23.9k
  state->setStrokeColorSpace(GfxColorSpace::create(csDeviceRGB));
1436
23.9k
  out->updateStrokeColorSpace(state);
1437
95.8k
  for (i = 0; i < 3; ++i) {
1438
71.9k
    color.c[i] = dblToCol(args[i].getNum());
1439
71.9k
  }
1440
23.9k
  state->setStrokeColor(&color);
1441
23.9k
  out->updateStrokeColor(state);
1442
23.9k
}
1443
1444
32.1k
void Gfx::opSetFillColorSpace(Object args[], int numArgs) {
1445
32.1k
  Object obj;
1446
32.1k
  GfxColorSpace *colorSpace;
1447
32.1k
  GfxColor color;
1448
1449
32.1k
  if (state->getIgnoreColorOps()) {
1450
24
    error(errSyntaxWarning, getPos(), "Ignoring color space setting"
1451
24
    " in uncolored Type 3 char or tiling pattern");
1452
24
    return;
1453
24
  }
1454
32.1k
  state->setFillPattern(NULL);
1455
32.1k
  res->lookupColorSpace(args[0].getName(), &obj);
1456
32.1k
  if (obj.isNull()) {
1457
19.3k
    colorSpace = GfxColorSpace::parse(&args[0]
1458
19.3k
              );
1459
19.3k
  } else {
1460
12.8k
    colorSpace = GfxColorSpace::parse(&obj
1461
12.8k
              );
1462
12.8k
  }
1463
32.1k
  obj.free();
1464
32.1k
  if (colorSpace) {
1465
20.7k
    state->setFillColorSpace(colorSpace);
1466
20.7k
    out->updateFillColorSpace(state);
1467
20.7k
    colorSpace->getDefaultColor(&color);
1468
20.7k
    state->setFillColor(&color);
1469
20.7k
    out->updateFillColor(state);
1470
20.7k
  } else {
1471
11.3k
    error(errSyntaxError, getPos(), "Bad color space (fill)");
1472
11.3k
  }
1473
32.1k
}
1474
1475
1.14k
void Gfx::opSetStrokeColorSpace(Object args[], int numArgs) {
1476
1.14k
  Object obj;
1477
1.14k
  GfxColorSpace *colorSpace;
1478
1.14k
  GfxColor color;
1479
1480
1.14k
  if (state->getIgnoreColorOps()) {
1481
39
    error(errSyntaxWarning, getPos(), "Ignoring color space setting"
1482
39
    " in uncolored Type 3 char or tiling pattern");
1483
39
    return;
1484
39
  }
1485
1.10k
  state->setStrokePattern(NULL);
1486
1.10k
  res->lookupColorSpace(args[0].getName(), &obj);
1487
1.10k
  if (obj.isNull()) {
1488
873
    colorSpace = GfxColorSpace::parse(&args[0]
1489
873
              );
1490
873
  } else {
1491
228
    colorSpace = GfxColorSpace::parse(&obj
1492
228
              );
1493
228
  }
1494
1.10k
  obj.free();
1495
1.10k
  if (colorSpace) {
1496
674
    state->setStrokeColorSpace(colorSpace);
1497
674
    out->updateStrokeColorSpace(state);
1498
674
    colorSpace->getDefaultColor(&color);
1499
674
    state->setStrokeColor(&color);
1500
674
    out->updateStrokeColor(state);
1501
674
  } else {
1502
427
    error(errSyntaxError, getPos(), "Bad color space (stroke)");
1503
427
  }
1504
1.10k
}
1505
1506
// Technically, per the PDF spec, the 'sc' operator can only be used
1507
// with device, CIE, and Indexed color spaces.  Some buggy PDF
1508
// generators use it for DeviceN, so we also allow that (same as
1509
// 'scn', minus the optional pattern name argument).
1510
14.1k
void Gfx::opSetFillColor(Object args[], int numArgs) {
1511
14.1k
  GfxColor color;
1512
14.1k
  int i;
1513
1514
14.1k
  if (state->getIgnoreColorOps()) {
1515
270
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1516
270
    " in uncolored Type 3 char or tiling pattern");
1517
270
    return;
1518
270
  }
1519
13.8k
  if (numArgs != state->getFillColorSpace()->getNComps()) {
1520
7.98k
    error(errSyntaxError, getPos(),
1521
7.98k
    "Incorrect number of arguments in 'sc' command");
1522
7.98k
    return;
1523
7.98k
  }
1524
5.86k
  state->setFillPattern(NULL);
1525
11.5k
  for (i = 0; i < numArgs; ++i) {
1526
5.73k
    color.c[i] = dblToCol(args[i].getNum());
1527
5.73k
  }
1528
5.86k
  state->setFillColor(&color);
1529
5.86k
  out->updateFillColor(state);
1530
5.86k
}
1531
1532
// Technically, per the PDF spec, the 'SC' operator can only be used
1533
// with device, CIE, and Indexed color spaces.  Some buggy PDF
1534
// generators use it for DeviceN, so we also allow that (same as
1535
// 'SCN', minus the optional pattern name argument).
1536
1.43k
void Gfx::opSetStrokeColor(Object args[], int numArgs) {
1537
1.43k
  GfxColor color;
1538
1.43k
  int i;
1539
1540
1.43k
  if (numArgs != state->getStrokeColorSpace()->getNComps()) {
1541
921
    error(errSyntaxError, getPos(),
1542
921
    "Incorrect number of arguments in 'SC' command");
1543
921
    return;
1544
921
  }
1545
512
  state->setStrokePattern(NULL);
1546
1.94k
  for (i = 0; i < numArgs; ++i) {
1547
1.42k
    color.c[i] = dblToCol(args[i].getNum());
1548
1.42k
  }
1549
512
  state->setStrokeColor(&color);
1550
512
  out->updateStrokeColor(state);
1551
512
}
1552
1553
19.2k
void Gfx::opSetFillColorN(Object args[], int numArgs) {
1554
19.2k
  GfxColor color;
1555
19.2k
  GfxPattern *pattern;
1556
19.2k
  int i;
1557
1558
19.2k
  if (state->getIgnoreColorOps()) {
1559
155
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1560
155
    " in uncolored Type 3 char or tiling pattern");
1561
155
    return;
1562
155
  }
1563
19.1k
  if (state->getFillColorSpace()->getMode() == csPattern) {
1564
15.2k
    if (numArgs == 0 || !args[numArgs-1].isName()) {
1565
985
      error(errSyntaxError, getPos(),
1566
985
      "Invalid arguments in 'scn' command");
1567
985
      return;
1568
985
    }
1569
14.2k
    if (numArgs > 1) {
1570
241
      if (!((GfxPatternColorSpace *)state->getFillColorSpace())->getUnder() ||
1571
205
    numArgs - 1 != ((GfxPatternColorSpace *)state->getFillColorSpace())
1572
205
                       ->getUnder()->getNComps()) {
1573
114
  error(errSyntaxError, getPos(),
1574
114
        "Incorrect number of arguments in 'scn' command");
1575
114
  return;
1576
114
      }
1577
508
      for (i = 0; i < numArgs - 1 && i < gfxColorMaxComps; ++i) {
1578
381
  if (args[i].isNum()) {
1579
331
    color.c[i] = dblToCol(args[i].getNum());
1580
331
  }
1581
381
      }
1582
127
      state->setFillColor(&color);
1583
127
      out->updateFillColor(state);
1584
127
    }
1585
14.1k
    if ((pattern = res->lookupPattern(args[numArgs-1].getName()
1586
14.1k
              ))) {
1587
5.87k
      state->setFillPattern(pattern);
1588
5.87k
    }
1589
1590
14.1k
  } else {
1591
3.85k
    if (numArgs != state->getFillColorSpace()->getNComps()) {
1592
2.07k
      error(errSyntaxError, getPos(),
1593
2.07k
      "Incorrect number of arguments in 'scn' command");
1594
2.07k
      return;
1595
2.07k
    }
1596
1.77k
    state->setFillPattern(NULL);
1597
3.91k
    for (i = 0; i < numArgs && i < gfxColorMaxComps; ++i) {
1598
2.13k
      if (args[i].isNum()) {
1599
1.46k
  color.c[i] = dblToCol(args[i].getNum());
1600
1.46k
      }
1601
2.13k
    }
1602
1.77k
    state->setFillColor(&color);
1603
1.77k
    out->updateFillColor(state);
1604
1.77k
  }
1605
19.1k
}
1606
1607
1.63k
void Gfx::opSetStrokeColorN(Object args[], int numArgs) {
1608
1.63k
  GfxColor color;
1609
1.63k
  GfxPattern *pattern;
1610
1.63k
  int i;
1611
1612
1.63k
  if (state->getIgnoreColorOps()) {
1613
153
    error(errSyntaxWarning, getPos(), "Ignoring color setting"
1614
153
    " in uncolored Type 3 char or tiling pattern");
1615
153
    return;
1616
153
  }
1617
1.48k
  if (state->getStrokeColorSpace()->getMode() == csPattern) {
1618
749
    if (numArgs == 0 || !args[numArgs-1].isName()) {
1619
94
      error(errSyntaxError, getPos(),
1620
94
      "Invalid arguments in 'SCN' command");
1621
94
      return;
1622
94
    }
1623
655
    if (numArgs > 1) {
1624
84
      if (!((GfxPatternColorSpace *)state->getStrokeColorSpace())
1625
84
         ->getUnder() ||
1626
0
    numArgs - 1 != ((GfxPatternColorSpace *)state->getStrokeColorSpace())
1627
84
                       ->getUnder()->getNComps()) {
1628
84
  error(errSyntaxError, getPos(),
1629
84
        "Incorrect number of arguments in 'SCN' command");
1630
84
  return;
1631
84
      }
1632
0
      for (i = 0; i < numArgs - 1 && i < gfxColorMaxComps; ++i) {
1633
0
  if (args[i].isNum()) {
1634
0
    color.c[i] = dblToCol(args[i].getNum());
1635
0
  }
1636
0
      }
1637
0
      state->setStrokeColor(&color);
1638
0
      out->updateStrokeColor(state);
1639
0
    }
1640
571
    if ((pattern = res->lookupPattern(args[numArgs-1].getName()
1641
571
              ))) {
1642
422
      state->setStrokePattern(pattern);
1643
422
    }
1644
1645
733
  } else {
1646
733
    if (numArgs != state->getStrokeColorSpace()->getNComps()) {
1647
551
      error(errSyntaxError, getPos(),
1648
551
      "Incorrect number of arguments in 'SCN' command");
1649
551
      return;
1650
551
    }
1651
182
    state->setStrokePattern(NULL);
1652
424
    for (i = 0; i < numArgs && i < gfxColorMaxComps; ++i) {
1653
242
      if (args[i].isNum()) {
1654
155
  color.c[i] = dblToCol(args[i].getNum());
1655
155
      }
1656
242
    }
1657
182
    state->setStrokeColor(&color);
1658
182
    out->updateStrokeColor(state);
1659
182
  }
1660
1.48k
}
1661
1662
//------------------------------------------------------------------------
1663
// path segment operators
1664
//------------------------------------------------------------------------
1665
1666
129k
void Gfx::opMoveTo(Object args[], int numArgs) {
1667
129k
  state->moveTo(args[0].getNum(), args[1].getNum());
1668
129k
}
1669
1670
215k
void Gfx::opLineTo(Object args[], int numArgs) {
1671
215k
  if (!state->isCurPt()) {
1672
110k
    error(errSyntaxError, getPos(), "No current point in lineto");
1673
110k
    return;
1674
110k
  }
1675
104k
  state->lineTo(args[0].getNum(), args[1].getNum());
1676
104k
}
1677
1678
66.7k
void Gfx::opCurveTo(Object args[], int numArgs) {
1679
66.7k
  double x1, y1, x2, y2, x3, y3;
1680
1681
66.7k
  if (!state->isCurPt()) {
1682
24.8k
    error(errSyntaxError, getPos(), "No current point in curveto");
1683
24.8k
    return;
1684
24.8k
  }
1685
41.8k
  x1 = args[0].getNum();
1686
41.8k
  y1 = args[1].getNum();
1687
41.8k
  x2 = args[2].getNum();
1688
41.8k
  y2 = args[3].getNum();
1689
41.8k
  x3 = args[4].getNum();
1690
41.8k
  y3 = args[5].getNum();
1691
41.8k
  state->curveTo(x1, y1, x2, y2, x3, y3);
1692
41.8k
}
1693
1694
10.2k
void Gfx::opCurveTo1(Object args[], int numArgs) {
1695
10.2k
  double x1, y1, x2, y2, x3, y3;
1696
1697
10.2k
  if (!state->isCurPt()) {
1698
8.79k
    error(errSyntaxError, getPos(), "No current point in curveto1");
1699
8.79k
    return;
1700
8.79k
  }
1701
1.46k
  x1 = state->getCurX();
1702
1.46k
  y1 = state->getCurY();
1703
1.46k
  x2 = args[0].getNum();
1704
1.46k
  y2 = args[1].getNum();
1705
1.46k
  x3 = args[2].getNum();
1706
1.46k
  y3 = args[3].getNum();
1707
1.46k
  state->curveTo(x1, y1, x2, y2, x3, y3);
1708
1.46k
}
1709
1710
8.58k
void Gfx::opCurveTo2(Object args[], int numArgs) {
1711
8.58k
  double x1, y1, x2, y2, x3, y3;
1712
1713
8.58k
  if (!state->isCurPt()) {
1714
7.03k
    error(errSyntaxError, getPos(), "No current point in curveto2");
1715
7.03k
    return;
1716
7.03k
  }
1717
1.55k
  x1 = args[0].getNum();
1718
1.55k
  y1 = args[1].getNum();
1719
1.55k
  x2 = args[2].getNum();
1720
1.55k
  y2 = args[3].getNum();
1721
1.55k
  x3 = x2;
1722
1.55k
  y3 = y2;
1723
1.55k
  state->curveTo(x1, y1, x2, y2, x3, y3);
1724
1.55k
}
1725
1726
106k
void Gfx::opRectangle(Object args[], int numArgs) {
1727
106k
  double x, y, w, h;
1728
1729
106k
  x = args[0].getNum();
1730
106k
  y = args[1].getNum();
1731
106k
  w = args[2].getNum();
1732
106k
  h = args[3].getNum();
1733
106k
  state->moveTo(x, y);
1734
106k
  state->lineTo(x + w, y);
1735
106k
  state->lineTo(x + w, y + h);
1736
106k
  state->lineTo(x, y + h);
1737
106k
  state->closePath();
1738
106k
}
1739
1740
49.0k
void Gfx::opClosePath(Object args[], int numArgs) {
1741
49.0k
  if (!state->isCurPt()) {
1742
26.3k
    error(errSyntaxError, getPos(), "No current point in closepath");
1743
26.3k
    return;
1744
26.3k
  }
1745
22.7k
  state->closePath();
1746
22.7k
}
1747
1748
//------------------------------------------------------------------------
1749
// path painting operators
1750
//------------------------------------------------------------------------
1751
1752
269k
void Gfx::opEndPath(Object args[], int numArgs) {
1753
269k
  doEndPath();
1754
269k
}
1755
1756
84.2k
void Gfx::opStroke(Object args[], int numArgs) {
1757
84.2k
  if (!state->isCurPt()) {
1758
    //error(errSyntaxError, getPos(), "No path in stroke");
1759
65.0k
    return;
1760
65.0k
  }
1761
19.2k
  if (state->isPath()) {
1762
14.3k
    if (ocState) {
1763
14.3k
      if (state->getStrokeColorSpace()->getMode() == csPattern) {
1764
622
  doPatternStroke();
1765
13.7k
      } else {
1766
13.7k
  out->stroke(state);
1767
13.7k
      }
1768
14.3k
    }
1769
14.3k
  }
1770
19.2k
  doEndPath();
1771
19.2k
}
1772
1773
39.0k
void Gfx::opCloseStroke(Object args[], int numArgs) {
1774
39.0k
  if (!state->isCurPt()) {
1775
    //error(errSyntaxError, getPos(), "No path in closepath/stroke");
1776
27.8k
    return;
1777
27.8k
  }
1778
11.2k
  if (state->isPath()) {
1779
7.50k
    state->closePath();
1780
7.50k
    if (ocState) {
1781
7.44k
      if (state->getStrokeColorSpace()->getMode() == csPattern) {
1782
10
  doPatternStroke();
1783
7.43k
      } else {
1784
7.43k
  out->stroke(state);
1785
7.43k
      }
1786
7.44k
    }
1787
7.50k
  }
1788
11.2k
  doEndPath();
1789
11.2k
}
1790
1791
139k
void Gfx::opFill(Object args[], int numArgs) {
1792
139k
  if (!state->isCurPt()) {
1793
    //error(errSyntaxError, getPos(), "No path in fill");
1794
62.7k
    return;
1795
62.7k
  }
1796
76.3k
  if (state->isPath()) {
1797
63.9k
    if (ocState) {
1798
62.9k
      if (state->getFillColorSpace()->getMode() == csPattern) {
1799
5.11k
  doPatternFill(gFalse);
1800
57.8k
      } else {
1801
57.8k
  out->fill(state);
1802
57.8k
      }
1803
62.9k
    }
1804
63.9k
  }
1805
76.3k
  doEndPath();
1806
76.3k
}
1807
1808
6.06k
void Gfx::opEOFill(Object args[], int numArgs) {
1809
6.06k
  if (!state->isCurPt()) {
1810
    //error(errSyntaxError, getPos(), "No path in eofill");
1811
1.85k
    return;
1812
1.85k
  }
1813
4.21k
  if (state->isPath()) {
1814
4.09k
    if (ocState) {
1815
4.09k
      if (state->getFillColorSpace()->getMode() == csPattern) {
1816
3.41k
  doPatternFill(gTrue);
1817
3.41k
      } else {
1818
675
  out->eoFill(state);
1819
675
      }
1820
4.09k
    }
1821
4.09k
  }
1822
4.21k
  doEndPath();
1823
4.21k
}
1824
1825
237k
void Gfx::opFillStroke(Object args[], int numArgs) {
1826
237k
  if (!state->isCurPt()) {
1827
    //error(errSyntaxError, getPos(), "No path in fill/stroke");
1828
220k
    return;
1829
220k
  }
1830
17.4k
  if (state->isPath()) {
1831
10.2k
    if (ocState) {
1832
10.1k
      if (state->getFillColorSpace()->getMode() == csPattern ||
1833
7.16k
    state->getStrokeColorSpace()->getMode() == csPattern) {
1834
2.94k
  if (state->getFillColorSpace()->getMode() == csPattern) {
1835
2.94k
    doPatternFill(gFalse);
1836
2.94k
  } else {
1837
0
    out->fill(state);
1838
0
  }
1839
2.94k
  if (state->getStrokeColorSpace()->getMode() == csPattern) {
1840
0
    doPatternStroke();
1841
2.94k
  } else {
1842
2.94k
    out->stroke(state);
1843
2.94k
  }
1844
7.16k
      } else {
1845
7.16k
  out->fillStroke(state, gFalse);
1846
7.16k
      }
1847
10.1k
    }
1848
10.2k
  }
1849
17.4k
  doEndPath();
1850
17.4k
}
1851
1852
39.6k
void Gfx::opCloseFillStroke(Object args[], int numArgs) {
1853
39.6k
  if (!state->isCurPt()) {
1854
    //error(errSyntaxError, getPos(), "No path in closepath/fill/stroke");
1855
27.3k
    return;
1856
27.3k
  }
1857
12.2k
  if (state->isPath()) {
1858
5.72k
    state->closePath();
1859
5.72k
    if (ocState) {
1860
5.50k
      if (state->getFillColorSpace()->getMode() == csPattern ||
1861
2.92k
    state->getStrokeColorSpace()->getMode() == csPattern) {
1862
2.92k
  if (state->getFillColorSpace()->getMode() == csPattern) {
1863
2.92k
    doPatternFill(gFalse);
1864
2.92k
  } else {
1865
0
    out->fill(state);
1866
0
  }
1867
2.92k
  if (state->getStrokeColorSpace()->getMode() == csPattern) {
1868
0
    doPatternStroke();
1869
2.92k
  } else {
1870
2.92k
    out->stroke(state);
1871
2.92k
  }
1872
2.92k
      } else {
1873
2.57k
  out->fillStroke(state, gFalse);
1874
2.57k
      }
1875
5.50k
    }
1876
5.72k
  }
1877
12.2k
  doEndPath();
1878
12.2k
}
1879
1880
7.38k
void Gfx::opEOFillStroke(Object args[], int numArgs) {
1881
7.38k
  if (!state->isCurPt()) {
1882
    //error(errSyntaxError, getPos(), "No path in eofill/stroke");
1883
213
    return;
1884
213
  }
1885
7.17k
  if (state->isPath()) {
1886
6.64k
    if (ocState) {
1887
6.64k
      if (state->getFillColorSpace()->getMode() == csPattern ||
1888
3.97k
    state->getStrokeColorSpace()->getMode() == csPattern) {
1889
2.66k
  if (state->getFillColorSpace()->getMode() == csPattern) {
1890
2.66k
    doPatternFill(gTrue);
1891
2.66k
  } else {
1892
0
    out->eoFill(state);
1893
0
  }
1894
2.66k
  if (state->getStrokeColorSpace()->getMode() == csPattern) {
1895
0
    doPatternStroke();
1896
2.66k
  } else {
1897
2.66k
    out->stroke(state);
1898
2.66k
  }
1899
3.97k
      } else {
1900
3.97k
  out->fillStroke(state, gTrue);
1901
3.97k
      }
1902
6.64k
    }
1903
6.64k
  }
1904
7.17k
  doEndPath();
1905
7.17k
}
1906
1907
228
void Gfx::opCloseEOFillStroke(Object args[], int numArgs) {
1908
228
  if (!state->isCurPt()) {
1909
    //error(errSyntaxError, getPos(), "No path in closepath/eofill/stroke");
1910
144
    return;
1911
144
  }
1912
84
  if (state->isPath()) {
1913
72
    state->closePath();
1914
72
    if (ocState) {
1915
68
      if (state->getFillColorSpace()->getMode() == csPattern ||
1916
57
    state->getStrokeColorSpace()->getMode() == csPattern) {
1917
11
  if (state->getFillColorSpace()->getMode() == csPattern) {
1918
11
    doPatternFill(gTrue);
1919
11
  } else {
1920
0
    out->eoFill(state);
1921
0
  }
1922
11
  if (state->getStrokeColorSpace()->getMode() == csPattern) {
1923
0
    doPatternStroke();
1924
11
  } else {
1925
11
    out->stroke(state);
1926
11
  }
1927
57
      } else {
1928
57
  out->fillStroke(state, gTrue);
1929
57
      }
1930
68
    }
1931
72
  }
1932
84
  doEndPath();
1933
84
}
1934
1935
17.2k
void Gfx::doPatternFill(GBool eoFill) {
1936
17.2k
  GfxPattern *pattern;
1937
1938
  // this is a bit of a kludge -- patterns can be really slow, so we
1939
  // skip them if we're only doing text extraction, since they almost
1940
  // certainly don't contain any text
1941
17.2k
  if (!out->needNonText()) {
1942
0
    return;
1943
0
  }
1944
1945
17.2k
  if (!(pattern = state->getFillPattern())) {
1946
15.0k
    return;
1947
15.0k
  }
1948
2.20k
  switch (pattern->getType()) {
1949
1.25k
  case 1:
1950
1.25k
    doTilingPatternFill((GfxTilingPattern *)pattern, gFalse, eoFill, gFalse);
1951
1.25k
    break;
1952
945
  case 2:
1953
945
    doShadingPatternFill((GfxShadingPattern *)pattern, gFalse, eoFill, gFalse);
1954
945
    break;
1955
0
  default:
1956
0
    error(errSyntaxError, getPos(), "Unknown pattern type ({0:d}) in fill",
1957
0
    pattern->getType());
1958
0
    break;
1959
2.20k
  }
1960
2.20k
}
1961
1962
632
void Gfx::doPatternStroke() {
1963
632
  GfxPattern *pattern;
1964
1965
  // this is a bit of a kludge -- patterns can be really slow, so we
1966
  // skip them if we're only doing text extraction, since they almost
1967
  // certainly don't contain any text
1968
632
  if (!out->needNonText()) {
1969
0
    return;
1970
0
  }
1971
1972
632
  if (!(pattern = state->getStrokePattern())) {
1973
92
    return;
1974
92
  }
1975
540
  switch (pattern->getType()) {
1976
540
  case 1:
1977
540
    doTilingPatternFill((GfxTilingPattern *)pattern, gTrue, gFalse, gFalse);
1978
540
    break;
1979
0
  case 2:
1980
0
    doShadingPatternFill((GfxShadingPattern *)pattern, gTrue, gFalse, gFalse);
1981
0
    break;
1982
0
  default:
1983
0
    error(errSyntaxError, getPos(), "Unknown pattern type ({0:d}) in stroke",
1984
0
    pattern->getType());
1985
0
    break;
1986
540
  }
1987
540
}
1988
1989
23.8k
void Gfx::doPatternText(GBool stroke) {
1990
23.8k
  GfxPattern *pattern;
1991
1992
  // this is a bit of a kludge -- patterns can be really slow, so we
1993
  // skip them if we're only doing text extraction, since they almost
1994
  // certainly don't contain any text
1995
23.8k
  if (!out->needNonText()) {
1996
0
    return;
1997
0
  }
1998
1999
23.8k
  pattern = stroke ? state->getStrokePattern() : state->getFillPattern();
2000
23.8k
  if (!pattern) {
2001
22.4k
    return;
2002
22.4k
  }
2003
1.43k
  switch (pattern->getType()) {
2004
1.33k
  case 1:
2005
1.33k
    doTilingPatternFill((GfxTilingPattern *)pattern, gFalse, gFalse, gTrue);
2006
1.33k
    break;
2007
95
  case 2:
2008
95
    doShadingPatternFill((GfxShadingPattern *)pattern, gFalse, gFalse, gTrue);
2009
95
    break;
2010
0
  default:
2011
0
    error(errSyntaxError, getPos(), "Unknown pattern type ({0:d}) in fill",
2012
0
    pattern->getType());
2013
0
    break;
2014
1.43k
  }
2015
1.43k
}
2016
2017
void Gfx::doPatternImageMask(Object *ref, Stream *str, int width, int height,
2018
152
           GBool invert, GBool inlineImg, GBool interpolate) {
2019
152
  saveState();
2020
2021
152
  out->setSoftMaskFromImageMask(state, ref, str,
2022
152
        width, height, invert, inlineImg, interpolate);
2023
2024
152
  state->clearPath();
2025
152
  state->moveTo(0, 0);
2026
152
  state->lineTo(1, 0);
2027
152
  state->lineTo(1, 1);
2028
152
  state->lineTo(0, 1);
2029
152
  state->closePath();
2030
152
  doPatternFill(gTrue);
2031
152
  state->clearPath();
2032
2033
152
  restoreState();
2034
152
}
2035
2036
void Gfx::doTilingPatternFill(GfxTilingPattern *tPat,
2037
3.13k
            GBool stroke, GBool eoFill, GBool text) {
2038
3.13k
  GfxPatternColorSpace *patCS;
2039
3.13k
  GfxColorSpace *cs;
2040
3.13k
  GfxColor color;
2041
3.13k
  GfxState *savedState;
2042
3.13k
  double xMin, yMin, xMax, yMax, x, y, x1, y1, t;
2043
3.13k
  double cxMin, cyMin, cxMax, cyMax;
2044
3.13k
  int xi0, yi0, xi1, yi1, xi, yi;
2045
3.13k
  double *ctm, *btm, *ptm;
2046
3.13k
  double bbox[4], m[6], ictm[6], m1[6], imb[6];
2047
3.13k
  double det;
2048
3.13k
  double xstep, ystep;
2049
3.13k
  int abortCheckCounter, i;
2050
2051
  // get color space
2052
3.13k
  patCS = (GfxPatternColorSpace *)(stroke ? state->getStrokeColorSpace()
2053
3.13k
                  : state->getFillColorSpace());
2054
2055
  // construct a (pattern space) -> (current space) transform matrix
2056
3.13k
  ctm = state->getCTM();
2057
3.13k
  btm = baseMatrix;
2058
3.13k
  ptm = tPat->getMatrix();
2059
  // iCTM = invert CTM
2060
3.13k
  det = ctm[0] * ctm[3] - ctm[1] * ctm[2];
2061
3.13k
  if (fabs(det) <= 1e-10) {
2062
237
    error(errSyntaxError, getPos(), "Singular matrix in tiling pattern fill");
2063
237
    return;
2064
237
  }
2065
2.89k
  det = 1 / det;
2066
2.89k
  ictm[0] = ctm[3] * det;
2067
2.89k
  ictm[1] = -ctm[1] * det;
2068
2.89k
  ictm[2] = -ctm[2] * det;
2069
2.89k
  ictm[3] = ctm[0] * det;
2070
2.89k
  ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
2071
2.89k
  ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
2072
  // m1 = PTM * BTM = PTM * base transform matrix
2073
2.89k
  m1[0] = ptm[0] * btm[0] + ptm[1] * btm[2];
2074
2.89k
  m1[1] = ptm[0] * btm[1] + ptm[1] * btm[3];
2075
2.89k
  m1[2] = ptm[2] * btm[0] + ptm[3] * btm[2];
2076
2.89k
  m1[3] = ptm[2] * btm[1] + ptm[3] * btm[3];
2077
2.89k
  m1[4] = ptm[4] * btm[0] + ptm[5] * btm[2] + btm[4];
2078
2.89k
  m1[5] = ptm[4] * btm[1] + ptm[5] * btm[3] + btm[5];
2079
  // m = m1 * iCTM = (PTM * BTM) * (iCTM)
2080
2.89k
  m[0] = m1[0] * ictm[0] + m1[1] * ictm[2];
2081
2.89k
  m[1] = m1[0] * ictm[1] + m1[1] * ictm[3];
2082
2.89k
  m[2] = m1[2] * ictm[0] + m1[3] * ictm[2];
2083
2.89k
  m[3] = m1[2] * ictm[1] + m1[3] * ictm[3];
2084
2.89k
  m[4] = m1[4] * ictm[0] + m1[5] * ictm[2] + ictm[4];
2085
2.89k
  m[5] = m1[4] * ictm[1] + m1[5] * ictm[3] + ictm[5];
2086
2087
  // construct a (device space) -> (pattern space) transform matrix
2088
2.89k
  det = m1[0] * m1[3] - m1[1] * m1[2];
2089
2.89k
  if (fabs(det) <= 1e-10) {
2090
80
    error(errSyntaxError, getPos(), "Singular matrix in tiling pattern fill");
2091
80
    return;
2092
80
  }
2093
2.81k
  det = 1 / det;
2094
2.81k
  imb[0] = m1[3] * det;
2095
2.81k
  imb[1] = -m1[1] * det;
2096
2.81k
  imb[2] = -m1[2] * det;
2097
2.81k
  imb[3] = m1[0] * det;
2098
2.81k
  imb[4] = (m1[2] * m1[5] - m1[3] * m1[4]) * det;
2099
2.81k
  imb[5] = (m1[1] * m1[4] - m1[0] * m1[5]) * det;
2100
2101
  // save current graphics state
2102
2.81k
  savedState = saveStateStack();
2103
2104
  // set underlying color space (for uncolored tiling patterns); set
2105
  // various other parameters (stroke color, line width) to match
2106
  // Adobe's behavior
2107
2.81k
  state->setFillPattern(NULL);
2108
2.81k
  state->setStrokePattern(NULL);
2109
2.81k
  state->setFillOverprint(gFalse);
2110
2.81k
  state->setStrokeOverprint(gFalse);
2111
2.81k
  state->setOverprintMode(0);
2112
2.81k
  if (tPat->getPaintType() == 2 && (cs = patCS->getUnder())) {
2113
45
    state->setFillColorSpace(cs->copy());
2114
45
    out->updateFillColorSpace(state);
2115
45
    state->setStrokeColorSpace(cs->copy());
2116
45
    out->updateStrokeColorSpace(state);
2117
45
    if (stroke) {
2118
0
      state->setFillColor(state->getStrokeColor());
2119
45
    } else {
2120
45
      state->setStrokeColor(state->getFillColor());
2121
45
    }
2122
45
    out->updateFillColor(state);
2123
45
    out->updateStrokeColor(state);
2124
45
    state->setIgnoreColorOps(gTrue);
2125
2.77k
  } else {
2126
2.77k
    state->setFillColorSpace(GfxColorSpace::create(csDeviceGray));
2127
2.77k
    out->updateFillColorSpace(state);
2128
2.77k
    state->getFillColorSpace()->getDefaultColor(&color);
2129
2.77k
    state->setFillColor(&color);
2130
2.77k
    out->updateFillColor(state);
2131
2.77k
    state->setStrokeColorSpace(GfxColorSpace::create(csDeviceGray));
2132
2.77k
    out->updateStrokeColorSpace(state);
2133
2.77k
    state->getStrokeColorSpace()->getDefaultColor(&color);
2134
2.77k
    state->setStrokeColor(&color);
2135
2.77k
    out->updateStrokeColor(state);
2136
2.77k
  }
2137
2.81k
  if (!stroke) {
2138
2.28k
    state->setLineWidth(0);
2139
2.28k
    out->updateLineWidth(state);
2140
2.28k
    state->setLineDash(NULL, 0, 0);
2141
2.28k
    out->updateLineDash(state);
2142
2.28k
  }
2143
2144
  // clip to current path
2145
2.81k
  if (stroke) {
2146
538
    state->clipToStrokePath();
2147
538
    out->clipToStrokePath(state);
2148
2.28k
  } else if (!text) {
2149
1.24k
    state->clip();
2150
1.24k
    if (eoFill) {
2151
128
      out->eoClip(state);
2152
1.12k
    } else {
2153
1.12k
      out->clip(state);
2154
1.12k
    }
2155
1.24k
  }
2156
2.81k
  state->clearPath();
2157
2158
  // get the clip region, check for empty
2159
2.81k
  state->getClipBBox(&cxMin, &cyMin, &cxMax, &cyMax);
2160
2.81k
  if (cxMin > cxMax || cyMin > cyMax) {
2161
769
    goto err;
2162
769
  }
2163
2164
  // transform clip region bbox to pattern space
2165
2.04k
  xMin = xMax = cxMin * imb[0] + cyMin * imb[2] + imb[4];
2166
2.04k
  yMin = yMax = cxMin * imb[1] + cyMin * imb[3] + imb[5];
2167
2.04k
  x1 = cxMin * imb[0] + cyMax * imb[2] + imb[4];
2168
2.04k
  y1 = cxMin * imb[1] + cyMax * imb[3] + imb[5];
2169
2.04k
  if (x1 < xMin) {
2170
0
    xMin = x1;
2171
2.04k
  } else if (x1 > xMax) {
2172
0
    xMax = x1;
2173
0
  }
2174
2.04k
  if (y1 < yMin) {
2175
221
    yMin = y1;
2176
1.82k
  } else if (y1 > yMax) {
2177
0
    yMax = y1;
2178
0
  }
2179
2.04k
  x1 = cxMax * imb[0] + cyMin * imb[2] + imb[4];
2180
2.04k
  y1 = cxMax * imb[1] + cyMin * imb[3] + imb[5];
2181
2.04k
  if (x1 < xMin) {
2182
0
    xMin = x1;
2183
2.04k
  } else if (x1 > xMax) {
2184
226
    xMax = x1;
2185
226
  }
2186
2.04k
  if (y1 < yMin) {
2187
0
    yMin = y1;
2188
2.04k
  } else if (y1 > yMax) {
2189
0
    yMax = y1;
2190
0
  }
2191
2.04k
  x1 = cxMax * imb[0] + cyMax * imb[2] + imb[4];
2192
2.04k
  y1 = cxMax * imb[1] + cyMax * imb[3] + imb[5];
2193
2.04k
  if (x1 < xMin) {
2194
0
    xMin = x1;
2195
2.04k
  } else if (x1 > xMax) {
2196
0
    xMax = x1;
2197
0
  }
2198
2.04k
  if (y1 < yMin) {
2199
0
    yMin = y1;
2200
2.04k
  } else if (y1 > yMax) {
2201
0
    yMax = y1;
2202
0
  }
2203
2204
  // draw the pattern
2205
  //~ this should treat negative steps differently -- start at right/top
2206
  //~ edge instead of left/bottom (?)
2207
2.04k
  bbox[0] = tPat->getBBox()[0];
2208
2.04k
  bbox[1] = tPat->getBBox()[1];
2209
2.04k
  bbox[2] = tPat->getBBox()[2];
2210
2.04k
  bbox[3] = tPat->getBBox()[3];
2211
2.04k
  if (bbox[0] > bbox[2]) {
2212
149
    t = bbox[0]; bbox[0] = bbox[2]; bbox[2] = t;
2213
149
  }
2214
2.04k
  if (bbox[1] > bbox[3]) {
2215
2
    t = bbox[1]; bbox[1] = bbox[3]; bbox[3] = t;
2216
2
  }
2217
2.04k
  xstep = tPat->getXStep();
2218
2.04k
  ystep = tPat->getYStep();
2219
2.04k
  if (xstep == 0 || ystep == 0) {
2220
27
    error(errSyntaxError, getPos(), "Zero x or y step in tiling pattern fill");
2221
27
    goto err;
2222
27
  }
2223
2.02k
  if (xstep > 0) {
2224
2.02k
    xi0 = (int)ceil((xMin - bbox[2]) / xstep);
2225
2.02k
    xi1 = (int)floor((xMax - bbox[0]) / xstep) + 1;
2226
2.02k
  } else {
2227
0
    xi0 = (int)ceil((xMax - bbox[0]) / xstep);
2228
0
    xi1 = (int)floor((xMin - bbox[2]) / xstep) + 1;
2229
0
  }
2230
2.02k
  if (ystep > 0) {
2231
2.02k
    yi0 = (int)ceil((yMin - bbox[3]) / ystep);
2232
2.02k
    yi1 = (int)floor((yMax - bbox[1]) / ystep) + 1;
2233
2.02k
  } else {
2234
0
    yi0 = (int)ceil((yMax - bbox[1]) / ystep);
2235
0
    yi1 = (int)floor((yMin - bbox[3]) / ystep) + 1;
2236
0
  }
2237
10.1k
  for (i = 0; i < 4; ++i) {
2238
8.08k
    m1[i] = m[i];
2239
8.08k
  }
2240
2.02k
  if (out->useTilingPatternFill() && !tPat->usesBlendMode(xref)) {
2241
2.02k
    m1[4] = m[4];
2242
2.02k
    m1[5] = m[5];
2243
2.02k
    out->tilingPatternFill(state, this, tPat->getContentStreamRef(),
2244
2.02k
         tPat->getPaintType(), tPat->getTilingType(),
2245
2.02k
         tPat->getResDict(),
2246
2.02k
         m1, bbox,
2247
2.02k
         xi0, yi0, xi1, yi1, xstep, ystep);
2248
2.02k
  } else {
2249
0
    abortCheckCounter = 0;
2250
0
    for (yi = yi0; yi < yi1; ++yi) {
2251
0
      for (xi = xi0; xi < xi1; ++xi) {
2252
0
  if (abortCheckCbk) {
2253
0
    ++abortCheckCounter;
2254
0
    if (abortCheckCounter > 100) {
2255
0
      if ((*abortCheckCbk)(abortCheckCbkData)) {
2256
0
        goto err;
2257
0
      }
2258
0
      abortCheckCounter = 0;
2259
0
    }
2260
0
  }
2261
0
  x = xi * xstep;
2262
0
  y = yi * ystep;
2263
0
  m1[4] = x * m[0] + y * m[2] + m[4];
2264
0
  m1[5] = x * m[1] + y * m[3] + m[5];
2265
0
  drawForm(tPat->getContentStreamRef(), tPat->getResDict(),
2266
0
     m1, bbox);
2267
0
      }
2268
0
    }
2269
0
  }
2270
2271
  // restore graphics state
2272
2.81k
 err:
2273
2.81k
  restoreStateStack(savedState);
2274
2.81k
}
2275
2276
void Gfx::doShadingPatternFill(GfxShadingPattern *sPat,
2277
1.04k
             GBool stroke, GBool eoFill, GBool text) {
2278
1.04k
  GfxShading *shading;
2279
1.04k
  GfxState *savedState;
2280
1.04k
  double *ctm, *btm, *ptm;
2281
1.04k
  double m[6], ictm[6], m1[6];
2282
1.04k
  double xMin, yMin, xMax, yMax;
2283
1.04k
  double det;
2284
2285
1.04k
  shading = sPat->getShading();
2286
2287
  // save current graphics state
2288
1.04k
  savedState = saveStateStack();
2289
2290
  // clip to current path
2291
1.04k
  if (stroke) {
2292
0
    state->clipToStrokePath();
2293
0
    out->clipToStrokePath(state);
2294
0
    state->setFillOverprint(state->getStrokeOverprint());
2295
1.04k
  } else if (!text) {
2296
945
    state->clip();
2297
945
    if (eoFill) {
2298
406
      out->eoClip(state);
2299
539
    } else {
2300
539
      out->clip(state);
2301
539
    }
2302
945
  }
2303
1.04k
  state->clearPath();
2304
2305
  // construct a (pattern space) -> (current space) transform matrix
2306
1.04k
  ctm = state->getCTM();
2307
1.04k
  btm = baseMatrix;
2308
1.04k
  ptm = sPat->getMatrix();
2309
  // iCTM = invert CTM
2310
1.04k
  det = ctm[0] * ctm[3] - ctm[1] * ctm[2];
2311
1.04k
  if (fabs(det) <= 1e-10) {
2312
54
    error(errSyntaxError, getPos(), "Singular matrix in shading pattern fill");
2313
54
    restoreStateStack(savedState);
2314
54
    return;
2315
54
  }
2316
986
  det = 1 / det;
2317
986
  ictm[0] = ctm[3] * det;
2318
986
  ictm[1] = -ctm[1] * det;
2319
986
  ictm[2] = -ctm[2] * det;
2320
986
  ictm[3] = ctm[0] * det;
2321
986
  ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
2322
986
  ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
2323
  // m1 = PTM * BTM = PTM * base transform matrix
2324
986
  m1[0] = ptm[0] * btm[0] + ptm[1] * btm[2];
2325
986
  m1[1] = ptm[0] * btm[1] + ptm[1] * btm[3];
2326
986
  m1[2] = ptm[2] * btm[0] + ptm[3] * btm[2];
2327
986
  m1[3] = ptm[2] * btm[1] + ptm[3] * btm[3];
2328
986
  m1[4] = ptm[4] * btm[0] + ptm[5] * btm[2] + btm[4];
2329
986
  m1[5] = ptm[4] * btm[1] + ptm[5] * btm[3] + btm[5];
2330
  // m = m1 * iCTM = (PTM * BTM) * (iCTM)
2331
986
  m[0] = m1[0] * ictm[0] + m1[1] * ictm[2];
2332
986
  m[1] = m1[0] * ictm[1] + m1[1] * ictm[3];
2333
986
  m[2] = m1[2] * ictm[0] + m1[3] * ictm[2];
2334
986
  m[3] = m1[2] * ictm[1] + m1[3] * ictm[3];
2335
986
  m[4] = m1[4] * ictm[0] + m1[5] * ictm[2] + ictm[4];
2336
986
  m[5] = m1[4] * ictm[1] + m1[5] * ictm[3] + ictm[5];
2337
2338
  // set the new matrix
2339
986
  state->concatCTM(m[0], m[1], m[2], m[3], m[4], m[5]);
2340
986
  out->updateCTM(state, m[0], m[1], m[2], m[3], m[4], m[5]);
2341
2342
  // clip to bbox
2343
986
  if (shading->getHasBBox()) {
2344
0
    shading->getBBox(&xMin, &yMin, &xMax, &yMax);
2345
0
    state->moveTo(xMin, yMin);
2346
0
    state->lineTo(xMax, yMin);
2347
0
    state->lineTo(xMax, yMax);
2348
0
    state->lineTo(xMin, yMax);
2349
0
    state->closePath();
2350
0
    state->clip();
2351
0
    out->clip(state);
2352
0
    state->clearPath();
2353
0
  }
2354
2355
  // set the color space
2356
986
  state->setFillColorSpace(shading->getColorSpace()->copy());
2357
986
  out->updateFillColorSpace(state);
2358
2359
  // background color fill
2360
986
  if (shading->getHasBackground()) {
2361
309
    state->setFillColor(shading->getBackground());
2362
309
    out->updateFillColor(state);
2363
309
    state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
2364
309
    state->moveTo(xMin, yMin);
2365
309
    state->lineTo(xMax, yMin);
2366
309
    state->lineTo(xMax, yMax);
2367
309
    state->lineTo(xMin, yMax);
2368
309
    state->closePath();
2369
309
    out->fill(state);
2370
309
    state->clearPath();
2371
309
  }
2372
2373
  // perform the fill
2374
986
  doShFill(shading);
2375
2376
  // restore graphics state
2377
986
  restoreStateStack(savedState);
2378
986
}
2379
2380
26.4k
void Gfx::opShFill(Object args[], int numArgs) {
2381
26.4k
  GfxShading *shading;
2382
26.4k
  GfxState *savedState;
2383
26.4k
  double xMin, yMin, xMax, yMax;
2384
2385
26.4k
  if (state->getIgnoreColorOps()) {
2386
1.51k
    error(errSyntaxWarning, getPos(), "Ignoring shaded fill"
2387
1.51k
    " in uncolored Type 3 char or tiling pattern");
2388
1.51k
    return;
2389
1.51k
  }
2390
2391
24.9k
  if (!out->needNonText()) {
2392
0
    return;
2393
0
  }
2394
2395
24.9k
  if (!ocState) {
2396
0
    return;
2397
0
  }
2398
2399
24.9k
  if (!(shading = res->lookupShading(args[0].getName()
2400
24.9k
             ))) {
2401
21.5k
    return;
2402
21.5k
  }
2403
2404
  // save current graphics state
2405
3.39k
  savedState = saveStateStack();
2406
2407
  // clip to bbox
2408
3.39k
  if (shading->getHasBBox()) {
2409
59
    shading->getBBox(&xMin, &yMin, &xMax, &yMax);
2410
59
    state->moveTo(xMin, yMin);
2411
59
    state->lineTo(xMax, yMin);
2412
59
    state->lineTo(xMax, yMax);
2413
59
    state->lineTo(xMin, yMax);
2414
59
    state->closePath();
2415
59
    state->clip();
2416
59
    out->clip(state);
2417
59
    state->clearPath();
2418
59
  }
2419
2420
  // set the color space
2421
3.39k
  state->setFillColorSpace(shading->getColorSpace()->copy());
2422
3.39k
  out->updateFillColorSpace(state);
2423
2424
  // perform the fill
2425
3.39k
  doShFill(shading);
2426
2427
  // restore graphics state
2428
3.39k
  restoreStateStack(savedState);
2429
2430
3.39k
  delete shading;
2431
3.39k
}
2432
2433
4.37k
void Gfx::doShFill(GfxShading *shading) {
2434
4.37k
  if (out->shadedFill(state, shading)) {
2435
4.33k
    return;
2436
4.33k
  }
2437
2438
  // do shading type-specific operations
2439
39
  switch (shading->getType()) {
2440
0
  case 1:
2441
0
    doFunctionShFill((GfxFunctionShading *)shading);
2442
0
    break;
2443
0
  case 2:
2444
0
    doAxialShFill((GfxAxialShading *)shading);
2445
0
    break;
2446
0
  case 3:
2447
0
    doRadialShFill((GfxRadialShading *)shading);
2448
0
    break;
2449
0
  case 4:
2450
0
  case 5:
2451
0
    doGouraudTriangleShFill((GfxGouraudTriangleShading *)shading);
2452
0
    break;
2453
0
  case 6:
2454
0
  case 7:
2455
0
    doPatchMeshShFill((GfxPatchMeshShading *)shading);
2456
0
    break;
2457
39
  }
2458
39
}
2459
2460
0
void Gfx::doFunctionShFill(GfxFunctionShading *shading) {
2461
0
  double x0, y0, x1, y1;
2462
0
  GfxColor colors[4];
2463
2464
0
  shading->getDomain(&x0, &y0, &x1, &y1);
2465
0
  shading->getColor(x0, y0, &colors[0]);
2466
0
  shading->getColor(x0, y1, &colors[1]);
2467
0
  shading->getColor(x1, y0, &colors[2]);
2468
0
  shading->getColor(x1, y1, &colors[3]);
2469
0
  doFunctionShFill1(shading, x0, y0, x1, y1, colors, 0);
2470
0
}
2471
2472
void Gfx::doFunctionShFill1(GfxFunctionShading *shading,
2473
          double x0, double y0,
2474
          double x1, double y1,
2475
0
          GfxColor *colors, int depth) {
2476
0
  GfxColor fillColor;
2477
0
  GfxColor color0M, color1M, colorM0, colorM1, colorMM;
2478
0
  GfxColor colors2[4];
2479
0
  double *matrix;
2480
0
  double xM, yM;
2481
0
  int nComps, i, j;
2482
2483
0
  nComps = shading->getColorSpace()->getNComps();
2484
0
  matrix = shading->getMatrix();
2485
2486
  // compare the four corner colors
2487
0
  for (i = 0; i < 4; ++i) {
2488
0
    for (j = 0; j < nComps; ++j) {
2489
0
      if (abs(colors[i].c[j] - colors[(i+1)&3].c[j]) > functionColorDelta) {
2490
0
  break;
2491
0
      }
2492
0
    }
2493
0
    if (j < nComps) {
2494
0
      break;
2495
0
    }
2496
0
  }
2497
2498
  // center of the rectangle
2499
0
  xM = 0.5 * (x0 + x1);
2500
0
  yM = 0.5 * (y0 + y1);
2501
2502
  // the four corner colors are close (or we hit the recursive limit)
2503
  // -- fill the rectangle; but require at least one subdivision
2504
  // (depth==0) to avoid problems when the four outer corners of the
2505
  // shaded region are the same color
2506
0
  if ((i == 4 && depth > 0) || depth == functionMaxDepth) {
2507
2508
    // use the center color
2509
0
    shading->getColor(xM, yM, &fillColor);
2510
0
    state->setFillColor(&fillColor);
2511
0
    out->updateFillColor(state);
2512
2513
    // fill the rectangle
2514
0
    state->moveTo(x0 * matrix[0] + y0 * matrix[2] + matrix[4],
2515
0
      x0 * matrix[1] + y0 * matrix[3] + matrix[5]);
2516
0
    state->lineTo(x1 * matrix[0] + y0 * matrix[2] + matrix[4],
2517
0
      x1 * matrix[1] + y0 * matrix[3] + matrix[5]);
2518
0
    state->lineTo(x1 * matrix[0] + y1 * matrix[2] + matrix[4],
2519
0
      x1 * matrix[1] + y1 * matrix[3] + matrix[5]);
2520
0
    state->lineTo(x0 * matrix[0] + y1 * matrix[2] + matrix[4],
2521
0
      x0 * matrix[1] + y1 * matrix[3] + matrix[5]);
2522
0
    state->closePath();
2523
0
    out->fill(state);
2524
0
    state->clearPath();
2525
2526
  // the four corner colors are not close enough -- subdivide the
2527
  // rectangle
2528
0
  } else {
2529
2530
    // colors[0]       colorM0       colors[2]
2531
    //   (x0,y0)       (xM,y0)       (x1,y0)
2532
    //         +----------+----------+
2533
    //         |          |          |
2534
    //         |    UL    |    UR    |
2535
    // color0M |       colorMM       | color1M
2536
    // (x0,yM) +----------+----------+ (x1,yM)
2537
    //         |       (xM,yM)       |
2538
    //         |    LL    |    LR    |
2539
    //         |          |          |
2540
    //         +----------+----------+
2541
    // colors[1]       colorM1       colors[3]
2542
    //   (x0,y1)       (xM,y1)       (x1,y1)
2543
2544
0
    shading->getColor(x0, yM, &color0M);
2545
0
    shading->getColor(x1, yM, &color1M);
2546
0
    shading->getColor(xM, y0, &colorM0);
2547
0
    shading->getColor(xM, y1, &colorM1);
2548
0
    shading->getColor(xM, yM, &colorMM);
2549
2550
    // upper-left sub-rectangle
2551
0
    colors2[0] = colors[0];
2552
0
    colors2[1] = color0M;
2553
0
    colors2[2] = colorM0;
2554
0
    colors2[3] = colorMM;
2555
0
    doFunctionShFill1(shading, x0, y0, xM, yM, colors2, depth + 1);
2556
    
2557
    // lower-left sub-rectangle
2558
0
    colors2[0] = color0M;
2559
0
    colors2[1] = colors[1];
2560
0
    colors2[2] = colorMM;
2561
0
    colors2[3] = colorM1;
2562
0
    doFunctionShFill1(shading, x0, yM, xM, y1, colors2, depth + 1);
2563
    
2564
    // upper-right sub-rectangle
2565
0
    colors2[0] = colorM0;
2566
0
    colors2[1] = colorMM;
2567
0
    colors2[2] = colors[2];
2568
0
    colors2[3] = color1M;
2569
0
    doFunctionShFill1(shading, xM, y0, x1, yM, colors2, depth + 1);
2570
2571
    // lower-right sub-rectangle
2572
0
    colors2[0] = colorMM;
2573
0
    colors2[1] = colorM1;
2574
0
    colors2[2] = color1M;
2575
0
    colors2[3] = colors[3];
2576
0
    doFunctionShFill1(shading, xM, yM, x1, y1, colors2, depth + 1);
2577
0
  }
2578
0
}
2579
2580
0
void Gfx::doAxialShFill(GfxAxialShading *shading) {
2581
0
  double xMin, yMin, xMax, yMax;
2582
0
  double x0, y0, x1, y1;
2583
0
  double dx, dy, mul;
2584
0
  GBool dxdyZero, horiz;
2585
0
  double tMin, tMax, tMinExt, tMaxExt, t, tx, ty;
2586
0
  double sMin, sMax, tmp;
2587
0
  double ux0, uy0, ux1, uy1, vx0, vy0, vx1, vy1;
2588
0
  double t0, t1, tt;
2589
0
  GfxColor colors[axialSplits];
2590
0
  int abortCheckCounter, nComps, i, j, k;
2591
2592
  // get the clip region bbox
2593
0
  state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
2594
2595
  // compute min and max t values, based on the four corners of the
2596
  // clip region bbox
2597
0
  shading->getCoords(&x0, &y0, &x1, &y1);
2598
0
  dx = x1 - x0;
2599
0
  dy = y1 - y0;
2600
0
  dxdyZero = fabs(dx) < 0.0001 && fabs(dy) < 0.0001;
2601
0
  horiz = fabs(dy) < fabs(dx);
2602
0
  if (dxdyZero) {
2603
0
    tMinExt = tMaxExt = 0;
2604
0
    tMin = tMax = 0;
2605
0
  } else {
2606
0
    mul = 1 / (dx * dx + dy * dy);
2607
0
    tMinExt = tMaxExt = ((xMin - x0) * dx + (yMin - y0) * dy) * mul;
2608
0
    t = ((xMin - x0) * dx + (yMax - y0) * dy) * mul;
2609
0
    if (t < tMinExt) {
2610
0
      tMinExt = t;
2611
0
    } else if (t > tMaxExt) {
2612
0
      tMaxExt = t;
2613
0
    }
2614
0
    t = ((xMax - x0) * dx + (yMin - y0) * dy) * mul;
2615
0
    if (t < tMinExt) {
2616
0
      tMinExt = t;
2617
0
    } else if (t > tMaxExt) {
2618
0
      tMaxExt = t;
2619
0
    }
2620
0
    t = ((xMax - x0) * dx + (yMax - y0) * dy) * mul;
2621
0
    if (t < tMinExt) {
2622
0
      tMinExt = t;
2623
0
    } else if (t > tMaxExt) {
2624
0
      tMaxExt = t;
2625
0
    }
2626
0
    if ((tMin = tMinExt) < 0) {
2627
0
      tMin = 0;
2628
0
    }
2629
0
    if (!shading->getExtend0()) {
2630
0
      tMinExt = tMin;
2631
0
    }
2632
0
    if ((tMax = tMaxExt) > 1) {
2633
0
      tMax = 1;
2634
0
    }
2635
0
    if (!shading->getExtend1()) {
2636
0
      tMaxExt = tMax;
2637
0
    }
2638
0
  }
2639
2640
  // get the function domain
2641
0
  t0 = shading->getDomain0();
2642
0
  t1 = shading->getDomain1();
2643
2644
  // Traverse the t axis and do the shading.
2645
  //
2646
  // For each point (tx, ty) on the t axis, consider a line through
2647
  // that point perpendicular to the t axis:
2648
  //
2649
  //     x(s) = tx + s * -dy   -->   s = (x - tx) / -dy
2650
  //     y(s) = ty + s * dx    -->   s = (y - ty) / dx
2651
  //
2652
  // Then look at the intersection of this line with the bounding box
2653
  // (xMin, yMin, xMax, yMax).  For -1 < |dy/dx| < 1, look at the
2654
  // intersection with yMin, yMax:
2655
  //
2656
  //     s0 = (yMin - ty) / dx
2657
  //     s1 = (yMax - ty) / dx
2658
  //
2659
  // else look at the intersection with xMin, xMax:
2660
  //
2661
  //     s0 = (xMin - tx) / -dy
2662
  //     s1 = (xMax - tx) / -dy
2663
  //
2664
  // Each filled polygon is bounded by two of these line segments
2665
  // perpdendicular to the t axis.
2666
  //
2667
  // The t axis is bisected into smaller regions until the color
2668
  // difference across a region is small enough, and then the region
2669
  // is painted with a single color.
2670
2671
  // compute the coordinates of the point on the t axis at t = tMin;
2672
  // then compute the intersection of the perpendicular line with the
2673
  // bounding box
2674
0
  tx = x0 + tMin * dx;
2675
0
  ty = y0 + tMin * dy;
2676
0
  if (dxdyZero) {
2677
0
    sMin = sMax = 0;
2678
0
  } else {
2679
0
    if (horiz) {
2680
0
      sMin = (yMin - ty) / dx;
2681
0
      sMax = (yMax - ty) / dx;
2682
0
    } else {
2683
0
      sMin = (xMin - tx) / -dy;
2684
0
      sMax = (xMax - tx) / -dy;
2685
0
    }
2686
0
    if (sMin > sMax) {
2687
0
      tmp = sMin; sMin = sMax; sMax = tmp;
2688
0
    }
2689
0
  }
2690
0
  ux0 = tx - sMin * dy;
2691
0
  uy0 = ty + sMin * dx;
2692
0
  vx0 = tx - sMax * dy;
2693
0
  vy0 = ty + sMax * dx;
2694
2695
  // fill the extension at t0
2696
0
  if (shading->getExtend0() && tMinExt < tMin) {
2697
2698
    // compute the color at t0
2699
0
    shading->getColor(t0, &colors[0]);
2700
2701
    // compute the coordinates of the point on the t axis at t =
2702
    // tMinExt; then compute the intersection of the perpendicular
2703
    // line with the bounding box
2704
0
    tx = x0 + tMinExt * dx;
2705
0
    ty = y0 + tMinExt * dy;
2706
0
    if (dxdyZero) {
2707
0
      sMin = sMax = 0;
2708
0
    } else {
2709
0
      if (horiz) {
2710
0
  sMin = (yMin - ty) / dx;
2711
0
  sMax = (yMax - ty) / dx;
2712
0
      } else {
2713
0
  sMin = (xMin - tx) / -dy;
2714
0
  sMax = (xMax - tx) / -dy;
2715
0
      }
2716
0
      if (sMin > sMax) {
2717
0
  tmp = sMin; sMin = sMax; sMax = tmp;
2718
0
      }
2719
0
    }
2720
0
    ux1 = tx - sMin * dy;
2721
0
    uy1 = ty + sMin * dx;
2722
0
    vx1 = tx - sMax * dy;
2723
0
    vy1 = ty + sMax * dx;
2724
2725
    // set the color
2726
0
    state->setFillColor(&colors[0]);
2727
0
    out->updateFillColor(state);
2728
2729
    // fill the region
2730
0
    state->moveTo(ux1, uy1);
2731
0
    state->lineTo(vx1, vy1);
2732
0
    state->lineTo(vx0, vy0);
2733
0
    state->lineTo(ux0, uy0);
2734
0
    state->closePath();
2735
0
    out->fill(state);
2736
0
    state->clearPath();
2737
0
  }
2738
2739
  // traverse the t axis, splitting [tMin, tMax] into axialSplits regions
2740
2741
  // compute the color in the center of each region
2742
0
  for (i = 0; i < axialSplits; ++i) {
2743
0
    t = tMin + (tMax - tMin) * (i + 0.5) / axialSplits;
2744
0
    tt = t0 + (t1 - t0) * t;
2745
0
    shading->getColor(tt, &colors[i]);
2746
0
  }
2747
2748
  // each iteration draws one or more regions, starting at i --
2749
  // if the colors are similar, it will combine regions i, i+1, ...
2750
0
  nComps = shading->getColorSpace()->getNComps();
2751
0
  abortCheckCounter = 0;
2752
0
  i = 0;
2753
0
  while (i < axialSplits) {
2754
2755
0
    if (abortCheckCbk) {
2756
0
      ++abortCheckCounter;
2757
0
      if (abortCheckCounter > 100) {
2758
0
        if ((*abortCheckCbk)(abortCheckCbkData)) {
2759
0
          break;
2760
0
        }
2761
0
        abortCheckCounter = 0;
2762
0
      }
2763
0
    }
2764
2765
    // check for similar colors
2766
0
    for (j = i + 1; j < axialSplits; ++j) {
2767
0
      for (k = 0; k < nComps; ++k) {
2768
0
  if (abs(colors[j].c[k] - colors[i].c[k]) > axialColorDelta) {
2769
0
    break;
2770
0
  }
2771
0
      }
2772
0
      if (k < nComps) {
2773
0
  break;
2774
0
      }
2775
0
    }
2776
2777
    // compute the coordinates of the point on the t axis; then
2778
    // compute the intersection of the perpendicular line with the
2779
    // bounding box
2780
0
    t = tMin + (tMax - tMin) * (double)j / axialSplits;
2781
0
    tx = x0 + t * dx;
2782
0
    ty = y0 + t * dy;
2783
0
    if (dxdyZero) {
2784
0
      sMin = sMax = 0;
2785
0
    } else {
2786
0
      if (horiz) {
2787
0
  sMin = (yMin - ty) / dx;
2788
0
  sMax = (yMax - ty) / dx;
2789
0
      } else {
2790
0
  sMin = (xMin - tx) / -dy;
2791
0
  sMax = (xMax - tx) / -dy;
2792
0
      }
2793
0
      if (sMin > sMax) {
2794
0
  tmp = sMin; sMin = sMax; sMax = tmp;
2795
0
      }
2796
0
    }
2797
0
    ux1 = tx - sMin * dy;
2798
0
    uy1 = ty + sMin * dx;
2799
0
    vx1 = tx - sMax * dy;
2800
0
    vy1 = ty + sMax * dx;
2801
2802
    // set the color
2803
0
    state->setFillColor(&colors[i]);
2804
0
    out->updateFillColor(state);
2805
2806
    // fill the region
2807
0
    state->moveTo(ux0, uy0);
2808
0
    state->lineTo(vx0, vy0);
2809
0
    state->lineTo(vx1, vy1);
2810
0
    state->lineTo(ux1, uy1);
2811
0
    state->closePath();
2812
0
    out->fill(state);
2813
0
    state->clearPath();
2814
2815
    // set up for next region
2816
0
    ux0 = ux1;
2817
0
    uy0 = uy1;
2818
0
    vx0 = vx1;
2819
0
    vy0 = vy1;
2820
2821
0
    i = j;
2822
0
  }
2823
2824
  // fill the extension at t1
2825
0
  if (shading->getExtend1() && tMaxExt > tMax) {
2826
2827
    // compute the color at t1
2828
0
    shading->getColor(t1, &colors[0]);
2829
2830
    // compute the coordinates of the point on the t axis at t =
2831
    // tMaxExt; then compute the intersection of the perpendicular
2832
    // line with the bounding box
2833
0
    tx = x0 + tMaxExt * dx;
2834
0
    ty = y0 + tMaxExt * dy;
2835
0
    if (dxdyZero) {
2836
0
      sMin = sMax = 0;
2837
0
    } else {
2838
0
      if (horiz) {
2839
0
  sMin = (yMin - ty) / dx;
2840
0
  sMax = (yMax - ty) / dx;
2841
0
      } else {
2842
0
  sMin = (xMin - tx) / -dy;
2843
0
  sMax = (xMax - tx) / -dy;
2844
0
      }
2845
0
      if (sMin > sMax) {
2846
0
  tmp = sMin; sMin = sMax; sMax = tmp;
2847
0
      }
2848
0
    }
2849
0
    ux1 = tx - sMin * dy;
2850
0
    uy1 = ty + sMin * dx;
2851
0
    vx1 = tx - sMax * dy;
2852
0
    vy1 = ty + sMax * dx;
2853
2854
    // set the color
2855
0
    state->setFillColor(&colors[0]);
2856
0
    out->updateFillColor(state);
2857
2858
    // fill the region
2859
0
    state->moveTo(ux0, uy0);
2860
0
    state->lineTo(vx0, vy0);
2861
0
    state->lineTo(vx1, vy1);
2862
0
    state->lineTo(ux1, uy1);
2863
0
    state->closePath();
2864
0
    out->fill(state);
2865
0
    state->clearPath();
2866
0
  }
2867
0
}
2868
2869
#if defined(__GNUC__) && !defined(__clang__)
2870
// this function makes a lot of sin()/cos() calls, which are slow
2871
// with glibc 2.16 and newer on x86; accuracy isn't terribly
2872
// important here, so tell gcc to use the fast version
2873
#pragma GCC optimize ("fast-math")
2874
#endif
2875
2876
0
void Gfx::doRadialShFill(GfxRadialShading *shading) {
2877
0
  double xMin, yMin, xMax, yMax;
2878
0
  double x0, y0, r0, x1, y1, r1, t0, t1;
2879
0
  int nComps;
2880
0
  GfxColor colorA, colorB;
2881
0
  double xa, ya, xb, yb, ra, rb;
2882
0
  double ta, tb, sa, sb;
2883
0
  double sMin, sMax, h;
2884
0
  double sLeft, sRight, sTop, sBottom, sZero, sDiag;
2885
0
  GBool haveSLeft, haveSRight, haveSTop, haveSBottom, haveSZero;
2886
0
  GBool haveSMin, haveSMax;
2887
0
  GBool enclosed;
2888
0
  double *ctm;
2889
0
  double theta, alpha, angle, t;
2890
0
  int abortCheckCounter, ia, ib, k, n;
2891
2892
  // get the shading info
2893
0
  shading->getCoords(&x0, &y0, &r0, &x1, &y1, &r1);
2894
0
  t0 = shading->getDomain0();
2895
0
  t1 = shading->getDomain1();
2896
0
  nComps = shading->getColorSpace()->getNComps();
2897
2898
  // Compute the point at which r(s) = 0; check for the enclosed
2899
  // circles case; and compute the angles for the tangent lines.
2900
0
  h = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
2901
0
  if (h == 0) {
2902
0
    enclosed = gTrue;
2903
0
    theta = 0; // make gcc happy
2904
0
  } else if (r1 - r0 == 0) {
2905
0
    enclosed = gFalse;
2906
0
    theta = 0;
2907
0
  } else if (fabs(r1 - r0) >= h - 0.0001) {
2908
0
    enclosed = gTrue;
2909
0
    theta = 0; // make gcc happy
2910
0
  } else {
2911
0
    enclosed = gFalse;
2912
0
    theta = asin((r1 - r0) / h);
2913
0
  }
2914
0
  if (enclosed) {
2915
0
    alpha = 0;
2916
0
  } else {
2917
0
    alpha = atan2(y1 - y0, x1 - x0);
2918
0
  }
2919
2920
  // compute the (possibly extended) s range
2921
0
  state->getUserClipBBox(&xMin, &yMin, &xMax, &yMax);
2922
0
  if (enclosed) {
2923
0
    sMin = 0;
2924
0
    sMax = 1;
2925
0
  } else {
2926
    // solve x(sLeft) + r(sLeft) = xMin
2927
0
    if ((haveSLeft = fabs((x1 + r1) - (x0 + r0)) > 0.000001)) {
2928
0
      sLeft = (xMin - (x0 + r0)) / ((x1 + r1) - (x0 + r0));
2929
0
    } else {
2930
0
      sLeft = 0; // make gcc happy
2931
0
    }
2932
    // solve x(sRight) - r(sRight) = xMax
2933
0
    if ((haveSRight = fabs((x1 - r1) - (x0 - r0)) > 0.000001)) {
2934
0
      sRight = (xMax - (x0 - r0)) / ((x1 - r1) - (x0 - r0));
2935
0
    } else {
2936
0
      sRight = 0; // make gcc happy
2937
0
    }
2938
    // solve y(sBottom) + r(sBottom) = yMin
2939
0
    if ((haveSBottom = fabs((y1 + r1) - (y0 + r0)) > 0.000001)) {
2940
0
      sBottom = (yMin - (y0 + r0)) / ((y1 + r1) - (y0 + r0));
2941
0
    } else {
2942
0
      sBottom = 0; // make gcc happy
2943
0
    }
2944
    // solve y(sTop) - r(sTop) = yMax
2945
0
    if ((haveSTop = fabs((y1 - r1) - (y0 - r0)) > 0.000001)) {
2946
0
      sTop = (yMax - (y0 - r0)) / ((y1 - r1) - (y0 - r0));
2947
0
    } else {
2948
0
      sTop = 0; // make gcc happy
2949
0
    }
2950
    // solve r(sZero) = 0
2951
0
    if ((haveSZero = fabs(r1 - r0) > 0.000001)) {
2952
0
      sZero = -r0 / (r1 - r0);
2953
0
    } else {
2954
0
      sZero = 0; // make gcc happy
2955
0
    }
2956
    // solve r(sDiag) = sqrt((xMax-xMin)^2 + (yMax-yMin)^2)
2957
0
    if (haveSZero) {
2958
0
      sDiag = (sqrt((xMax - xMin) * (xMax - xMin) +
2959
0
        (yMax - yMin) * (yMax - yMin)) - r0) / (r1 - r0);
2960
0
    } else {
2961
0
      sDiag = 0; // make gcc happy
2962
0
    }
2963
    // compute sMin
2964
0
    if (shading->getExtend0()) {
2965
0
      sMin = 0;
2966
0
      haveSMin = gFalse;
2967
0
      if (x0 < x1 && haveSLeft && sLeft < 0) {
2968
0
  sMin = sLeft;
2969
0
  haveSMin = gTrue;
2970
0
      } else if (x0 > x1 && haveSRight && sRight < 0) {
2971
0
  sMin = sRight;
2972
0
  haveSMin = gTrue;
2973
0
      }
2974
0
      if (y0 < y1 && haveSBottom && sBottom < 0) {
2975
0
  if (!haveSMin || sBottom > sMin) {
2976
0
    sMin = sBottom;
2977
0
    haveSMin = gTrue;
2978
0
  }
2979
0
      } else if (y0 > y1 && haveSTop && sTop < 0) {
2980
0
  if (!haveSMin || sTop > sMin) {
2981
0
    sMin = sTop;
2982
0
    haveSMin = gTrue;
2983
0
  }
2984
0
      }
2985
0
      if (haveSZero && sZero <= 0) {
2986
0
  if (!haveSMin || sZero > sMin) {
2987
0
    sMin = sZero;
2988
0
  }
2989
0
      }
2990
0
    } else {
2991
0
      sMin = 0;
2992
0
    }
2993
    // compute sMax
2994
0
    if (shading->getExtend1()) {
2995
0
      sMax = 1;
2996
0
      haveSMax = gFalse;
2997
0
      if (x1 < x0 && haveSLeft && sLeft > 1) {
2998
0
  sMax = sLeft;
2999
0
  haveSMax = gTrue;
3000
0
      } else if (x1 > x0 && haveSRight && sRight > 1) {
3001
0
  sMax = sRight;
3002
0
  haveSMax = gTrue;
3003
0
      }
3004
0
      if (y1 < y0 && haveSBottom && sBottom > 1) {
3005
0
  if (!haveSMax || sBottom < sMax) {
3006
0
    sMax = sBottom;
3007
0
    haveSMax = gTrue;
3008
0
  }
3009
0
      } else if (y1 > y0 && haveSTop && sTop > 1) {
3010
0
  if (!haveSMax || sTop < sMax) {
3011
0
    sMax = sTop;
3012
0
    haveSMax = gTrue;
3013
0
  }
3014
0
      }
3015
0
      if (haveSZero && sDiag > 1) {
3016
0
  if (!haveSMax || sDiag < sMax) {
3017
0
    sMax = sDiag;
3018
0
  }
3019
0
      }
3020
0
    } else {
3021
0
      sMax = 1;
3022
0
    }
3023
0
  }
3024
3025
  // compute the number of steps into which circles must be divided to
3026
  // achieve a curve flatness of 0.1 pixel in device space for the
3027
  // largest circle (note that "device space" is 72 dpi when generating
3028
  // PostScript, hence the relatively small 0.1 pixel accuracy)
3029
0
  ctm = state->getCTM();
3030
0
  t = fabs(ctm[0]);
3031
0
  if (fabs(ctm[1]) > t) {
3032
0
    t = fabs(ctm[1]);
3033
0
  }
3034
0
  if (fabs(ctm[2]) > t) {
3035
0
    t = fabs(ctm[2]);
3036
0
  }
3037
0
  if (fabs(ctm[3]) > t) {
3038
0
    t = fabs(ctm[3]);
3039
0
  }
3040
0
  if (r0 > r1) {
3041
0
    t *= r0;
3042
0
  } else {
3043
0
    t *= r1;
3044
0
  }
3045
0
  if (t < 1) {
3046
0
    n = 3;
3047
0
  } else {
3048
0
    n = (int)(M_PI / acos(1 - 0.1 / t));
3049
0
    if (n < 3) {
3050
0
      n = 3;
3051
0
    } else if (n > 200) {
3052
0
      n = 200;
3053
0
    }
3054
0
  }
3055
3056
  // setup for the start circle
3057
0
  ia = 0;
3058
0
  sa = sMin;
3059
0
  ta = t0 + sa * (t1 - t0);
3060
0
  xa = x0 + sa * (x1 - x0);
3061
0
  ya = y0 + sa * (y1 - y0);
3062
0
  ra = r0 + sa * (r1 - r0);
3063
0
  if (ta < t0) {
3064
0
    shading->getColor(t0, &colorA);
3065
0
  } else if (ta > t1) {
3066
0
    shading->getColor(t1, &colorA);
3067
0
  } else {
3068
0
    shading->getColor(ta, &colorA);
3069
0
  }
3070
3071
  // fill the circles
3072
0
  abortCheckCounter = 0;
3073
0
  while (ia < radialMaxSplits) {
3074
3075
0
    if (abortCheckCbk) {
3076
0
      ++abortCheckCounter;
3077
0
      if (abortCheckCounter > 100) {
3078
0
  if ((*abortCheckCbk)(abortCheckCbkData)) {
3079
0
    break;
3080
0
  }
3081
0
  abortCheckCounter = 0;
3082
0
      }
3083
0
    }
3084
3085
    // go as far along the t axis (toward t1) as we can, such that the
3086
    // color difference is within the tolerance (radialColorDelta) --
3087
    // this uses bisection (between the current value, t, and t1),
3088
    // limited to radialMaxSplits points along the t axis; require at
3089
    // least one split to avoid problems when the innermost and
3090
    // outermost colors are the same
3091
0
    ib = radialMaxSplits;
3092
0
    sb = sMax;
3093
0
    tb = t0 + sb * (t1 - t0);
3094
0
    if (tb < t0) {
3095
0
      shading->getColor(t0, &colorB);
3096
0
    } else if (tb > t1) {
3097
0
      shading->getColor(t1, &colorB);
3098
0
    } else {
3099
0
      shading->getColor(tb, &colorB);
3100
0
    }
3101
0
    while (ib - ia > 1) {
3102
0
      for (k = 0; k < nComps; ++k) {
3103
0
  if (abs(colorB.c[k] - colorA.c[k]) > radialColorDelta) {
3104
0
    break;
3105
0
  }
3106
0
      }
3107
0
      if (k == nComps && ib < radialMaxSplits) {
3108
0
  break;
3109
0
      }
3110
0
      ib = (ia + ib) / 2;
3111
0
      sb = sMin + ((double)ib / (double)radialMaxSplits) * (sMax - sMin);
3112
0
      tb = t0 + sb * (t1 - t0);
3113
0
      if (tb < t0) {
3114
0
  shading->getColor(t0, &colorB);
3115
0
      } else if (tb > t1) {
3116
0
  shading->getColor(t1, &colorB);
3117
0
      } else {
3118
0
  shading->getColor(tb, &colorB);
3119
0
      }
3120
0
    }
3121
3122
    // compute center and radius of the circle
3123
0
    xb = x0 + sb * (x1 - x0);
3124
0
    yb = y0 + sb * (y1 - y0);
3125
0
    rb = r0 + sb * (r1 - r0);
3126
3127
    // use the average of the colors at the two circles
3128
0
    for (k = 0; k < nComps; ++k) {
3129
0
      colorA.c[k] = (colorA.c[k] + colorB.c[k]) / 2;
3130
0
    }
3131
0
    state->setFillColor(&colorA);
3132
0
    out->updateFillColor(state);
3133
3134
0
    if (enclosed) {
3135
3136
      // construct path for first circle (counterclockwise)
3137
0
      state->moveTo(xa + ra, ya);
3138
0
      for (k = 1; k < n; ++k) {
3139
0
  angle = ((double)k / (double)n) * 2 * M_PI;
3140
0
  state->lineTo(xa + ra * cos(angle), ya + ra * sin(angle));
3141
0
      }
3142
0
      state->closePath();
3143
3144
      // construct and append path for second circle (clockwise)
3145
0
      state->moveTo(xb + rb, yb);
3146
0
      for (k = 1; k < n; ++k) {
3147
0
  angle = -((double)k / (double)n) * 2 * M_PI;
3148
0
  state->lineTo(xb + rb * cos(angle), yb + rb * sin(angle));
3149
0
      }
3150
0
      state->closePath();
3151
3152
0
    } else {
3153
3154
      // construct the first subpath (clockwise)
3155
0
      state->moveTo(xa + ra * cos(alpha + theta + 0.5 * M_PI),
3156
0
        ya + ra * sin(alpha + theta + 0.5 * M_PI));
3157
0
      for (k = 0; k < n; ++k) {
3158
0
  angle = alpha + theta + 0.5 * M_PI
3159
0
    - ((double)k / (double)n) * (2 * theta + M_PI);
3160
0
  state->lineTo(xb + rb * cos(angle), yb + rb * sin(angle));
3161
0
      }
3162
0
      for (k = 0; k < n; ++k) {
3163
0
  angle = alpha - theta - 0.5 * M_PI
3164
0
    + ((double)k / (double)n) * (2 * theta - M_PI);
3165
0
  state->lineTo(xa + ra * cos(angle), ya + ra * sin(angle));
3166
0
      }
3167
0
      state->closePath();
3168
3169
      // construct the second subpath (counterclockwise)
3170
0
      state->moveTo(xa + ra * cos(alpha + theta + 0.5 * M_PI),
3171
0
        ya + ra * sin(alpha + theta + 0.5 * M_PI));
3172
0
      for (k = 0; k < n; ++k) {
3173
0
  angle = alpha + theta + 0.5 * M_PI
3174
0
          + ((double)k / (double)n) * (-2 * theta + M_PI);
3175
0
  state->lineTo(xb + rb * cos(angle), yb + rb * sin(angle));
3176
0
      }
3177
0
      for (k = 0; k < n; ++k) {
3178
0
  angle = alpha - theta - 0.5 * M_PI
3179
0
          + ((double)k / (double)n) * (2 * theta + M_PI);
3180
0
  state->lineTo(xa + ra * cos(angle), ya + ra * sin(angle));
3181
0
      }
3182
0
      state->closePath();
3183
0
    }
3184
3185
    // fill the path
3186
0
    out->fill(state);
3187
0
    state->clearPath();
3188
3189
    // step to the next value of t
3190
0
    ia = ib;
3191
0
    sa = sb;
3192
0
    ta = tb;
3193
0
    xa = xb;
3194
0
    ya = yb;
3195
0
    ra = rb;
3196
0
    colorA = colorB;
3197
0
  }
3198
3199
0
  if (enclosed) {
3200
    // extend the smaller circle
3201
0
    if ((shading->getExtend0() && r0 <= r1) ||
3202
0
  (shading->getExtend1() && r1 < r0)) {
3203
0
      if (r0 <= r1) {
3204
0
  ta = t0;
3205
0
  ra = r0;
3206
0
  xa = x0;
3207
0
  ya = y0;
3208
0
      } else {
3209
0
  ta = t1;
3210
0
  ra = r1;
3211
0
  xa = x1;
3212
0
  ya = y1;
3213
0
      }
3214
0
      shading->getColor(ta, &colorA);
3215
0
      state->setFillColor(&colorA);
3216
0
      out->updateFillColor(state);
3217
0
      state->moveTo(xa + ra, ya);
3218
0
      for (k = 1; k < n; ++k) {
3219
0
  angle = ((double)k / (double)n) * 2 * M_PI;
3220
0
  state->lineTo(xa + ra * cos(angle), ya + ra * sin(angle));
3221
0
      }
3222
0
      state->closePath();
3223
0
      out->fill(state);
3224
0
      state->clearPath();
3225
0
    }
3226
3227
    // extend the larger circle
3228
0
    if ((shading->getExtend0() && r0 > r1) ||
3229
0
  (shading->getExtend1() && r1 >= r0)) {
3230
0
      if (r0 > r1) {
3231
0
  ta = t0;
3232
0
  ra = r0;
3233
0
  xa = x0;
3234
0
  ya = y0;
3235
0
      } else {
3236
0
  ta = t1;
3237
0
  ra = r1;
3238
0
  xa = x1;
3239
0
  ya = y1;
3240
0
      }
3241
0
      shading->getColor(ta, &colorA);
3242
0
      state->setFillColor(&colorA);
3243
0
      out->updateFillColor(state);
3244
0
      state->moveTo(xMin, yMin);
3245
0
      state->lineTo(xMin, yMax);
3246
0
      state->lineTo(xMax, yMax);
3247
0
      state->lineTo(xMax, yMin);
3248
0
      state->closePath();
3249
0
      state->moveTo(xa + ra, ya);
3250
0
      for (k = 1; k < n; ++k) {
3251
0
  angle = ((double)k / (double)n) * 2 * M_PI;
3252
0
  state->lineTo(xa + ra * cos(angle), ya + ra * sin(angle));
3253
0
      }
3254
0
      state->closePath();
3255
0
      out->fill(state);
3256
0
      state->clearPath();
3257
0
    }
3258
0
  }
3259
0
}
3260
3261
#if defined(__GNUC__) && !defined(__clang__)
3262
#pragma GCC reset_options
3263
#endif
3264
3265
0
void Gfx::doGouraudTriangleShFill(GfxGouraudTriangleShading *shading) {
3266
0
  double x0, y0, x1, y1, x2, y2;
3267
0
  double color0[gfxColorMaxComps];
3268
0
  double color1[gfxColorMaxComps];
3269
0
  double color2[gfxColorMaxComps];
3270
0
  int abortCheckCounter, i;
3271
3272
0
  abortCheckCounter = 0;
3273
0
  for (i = 0; i < shading->getNTriangles(); ++i) {
3274
0
    if (abortCheckCbk) {
3275
0
      ++abortCheckCounter;
3276
0
      if (abortCheckCounter > 25) {
3277
0
  if ((*abortCheckCbk)(abortCheckCbkData)) {
3278
0
    break;
3279
0
  }
3280
0
  abortCheckCounter = 0;
3281
0
      }
3282
0
    }
3283
0
    shading->getTriangle(i, &x0, &y0, color0,
3284
0
       &x1, &y1, color1,
3285
0
       &x2, &y2, color2);
3286
0
    gouraudFillTriangle(x0, y0, color0, x1, y1, color1, x2, y2, color2,
3287
0
      shading, 0);
3288
0
  }
3289
0
}
3290
3291
void Gfx::gouraudFillTriangle(double x0, double y0, double *color0,
3292
            double x1, double y1, double *color1,
3293
            double x2, double y2, double *color2,
3294
0
            GfxGouraudTriangleShading *shading, int depth) {
3295
0
  double dx0, dy0, dx1, dy1, dx2, dy2;
3296
0
  double x01, y01, x12, y12, x20, y20;
3297
0
  double color01[gfxColorMaxComps];
3298
0
  double color12[gfxColorMaxComps];
3299
0
  double color20[gfxColorMaxComps];
3300
0
  GfxColor c0, c1, c2;
3301
0
  int nComps, i;
3302
3303
  // recursion ends when:
3304
  // (1) color difference is smaller than gouraudColorDelta; or
3305
  // (2) triangles are smaller than 0.5 pixel (note that "device
3306
  //     space" is 72dpi when generating PostScript); or
3307
  // (3) max recursion depth (gouraudMaxDepth) is hit.
3308
0
  nComps = shading->getColorSpace()->getNComps();
3309
0
  shading->getColor(color0, &c0);
3310
0
  shading->getColor(color1, &c1);
3311
0
  shading->getColor(color2, &c2);
3312
0
  for (i = 0; i < nComps; ++i) {
3313
0
    if (abs(c0.c[i] - c1.c[i]) > gouraudColorDelta ||
3314
0
  abs(c1.c[i] - c2.c[i]) > gouraudColorDelta) {
3315
0
      break;
3316
0
    }
3317
0
  }
3318
0
  state->transformDelta(x1 - x0, y1 - y0, &dx0, &dy0);
3319
0
  state->transformDelta(x2 - x1, y2 - y1, &dx1, &dy1);
3320
0
  state->transformDelta(x0 - x2, y0 - y2, &dx2, &dy2);
3321
0
  if (i == nComps ||
3322
0
      depth == gouraudMaxDepth ||
3323
0
      (fabs(dx0) < 0.5 && fabs(dy0) < 0.5 &&
3324
0
       fabs(dx1) < 0.5 && fabs(dy1) < 0.5 &&
3325
0
       fabs(dx2) < 0.5 && fabs(dy2) < 0.5)) {
3326
0
    state->setFillColor(&c0);
3327
0
    out->updateFillColor(state);
3328
0
    state->moveTo(x0, y0);
3329
0
    state->lineTo(x1, y1);
3330
0
    state->lineTo(x2, y2);
3331
0
    state->closePath();
3332
0
    out->fill(state);
3333
0
    state->clearPath();
3334
0
  } else {
3335
0
    x01 = 0.5 * (x0 + x1);
3336
0
    y01 = 0.5 * (y0 + y1);
3337
0
    x12 = 0.5 * (x1 + x2);
3338
0
    y12 = 0.5 * (y1 + y2);
3339
0
    x20 = 0.5 * (x2 + x0);
3340
0
    y20 = 0.5 * (y2 + y0);
3341
0
    for (i = 0; i < shading->getNComps(); ++i) {
3342
0
      color01[i] = 0.5 * (color0[i] + color1[i]);
3343
0
      color12[i] = 0.5 * (color1[i] + color2[i]);
3344
0
      color20[i] = 0.5 * (color2[i] + color0[i]);
3345
0
    }
3346
0
    gouraudFillTriangle(x0, y0, color0, x01, y01, color01,
3347
0
      x20, y20, color20, shading, depth + 1);
3348
0
    gouraudFillTriangle(x01, y01, color01, x1, y1, color1,
3349
0
      x12, y12, color12, shading, depth + 1);
3350
0
    gouraudFillTriangle(x01, y01, color01, x12, y12, color12,
3351
0
      x20, y20, color20, shading, depth + 1);
3352
0
    gouraudFillTriangle(x20, y20, color20, x12, y12, color12,
3353
0
      x2, y2, color2, shading, depth + 1);
3354
0
  }
3355
0
}
3356
3357
0
void Gfx::doPatchMeshShFill(GfxPatchMeshShading *shading) {
3358
0
  int start, abortCheckCounter, i;
3359
3360
0
  if (shading->getNPatches() > 128) {
3361
0
    start = 3;
3362
0
  } else if (shading->getNPatches() > 64) {
3363
0
    start = 2;
3364
0
  } else if (shading->getNPatches() > 16) {
3365
0
    start = 1;
3366
0
  } else {
3367
0
    start = 0;
3368
0
  }
3369
0
  abortCheckCounter = 0;
3370
0
  for (i = 0; i < shading->getNPatches(); ++i) {
3371
0
    if (abortCheckCbk) {
3372
0
      ++abortCheckCounter;
3373
0
      if (abortCheckCounter > 25) {
3374
0
  if ((*abortCheckCbk)(abortCheckCbkData)) {
3375
0
    break;
3376
0
  }
3377
0
  abortCheckCounter = 0;
3378
0
      }
3379
0
    }
3380
0
    fillPatch(shading->getPatch(i), shading, start);
3381
0
  }
3382
0
}
3383
3384
0
void Gfx::fillPatch(GfxPatch *patch, GfxPatchMeshShading *shading, int depth) {
3385
0
  GfxPatch patch00, patch01, patch10, patch11;
3386
0
  GfxColor c00, c01, c10, c11;
3387
0
  double xx[4][8], yy[4][8];
3388
0
  double x, y, xMin, yMin, xMax, yMax, xxm, yym;
3389
0
  int nComps, i, j;
3390
0
  GBool stop;
3391
3392
0
  shading->getColor(patch->color[0][0], &c00);
3393
0
  stop = gFalse;
3394
3395
  // stop subdivision at max depth
3396
0
  if (depth == patchMaxDepth) {
3397
0
    stop = gTrue;
3398
0
  }
3399
3400
  // stop subdivision if colors are close enough
3401
0
  if (!stop) {
3402
0
    nComps = shading->getColorSpace()->getNComps();
3403
0
    shading->getColor(patch->color[0][1], &c01);
3404
0
    shading->getColor(patch->color[1][0], &c10);
3405
0
    shading->getColor(patch->color[1][1], &c11);
3406
0
    for (i = 0; i < nComps; ++i) {
3407
0
      if (abs(c00.c[i] - c01.c[i]) > patchColorDelta ||
3408
0
    abs(c01.c[i] - c11.c[i]) > patchColorDelta ||
3409
0
    abs(c11.c[i] - c10.c[i]) > patchColorDelta ||
3410
0
    abs(c10.c[i] - c00.c[i]) > patchColorDelta) {
3411
0
  break;
3412
0
      }
3413
0
    }
3414
0
    if (i == nComps) {
3415
0
      stop = gTrue;
3416
0
    }
3417
0
  }
3418
3419
  // stop subdivision if patch is small enough
3420
0
  if (!stop) {
3421
0
    xMin = yMin = xMax = yMax = 0;
3422
0
    for (j = 0; j < 4; ++j) {
3423
0
      for (i = 0; i < 4; ++i) {
3424
0
  state->transformDelta(patch->x[i][j], patch->y[i][j], &x, &y);
3425
0
  if (i == 0 && j == 0) {
3426
0
    xMin = xMax = x;
3427
0
    yMin = yMax = y;
3428
0
  } else {
3429
0
    if (x < xMin) {
3430
0
      xMin = x;
3431
0
    } else if (x > xMax) {
3432
0
      xMax = x;
3433
0
    }
3434
0
    if (y < yMin) {
3435
0
      yMin = y;
3436
0
    } else if (y > yMax) {
3437
0
      yMax = y;
3438
0
    }
3439
0
  }
3440
0
      }
3441
0
    }
3442
0
    if (xMax - xMin < 1 && yMax - yMin < 1) {
3443
0
      stop = gTrue;
3444
0
    }
3445
0
  }
3446
3447
  // draw the patch
3448
0
  if (stop) {
3449
0
    state->setFillColor(&c00);
3450
0
    out->updateFillColor(state);
3451
0
    state->moveTo(patch->x[0][0], patch->y[0][0]);
3452
0
    state->curveTo(patch->x[0][1], patch->y[0][1],
3453
0
       patch->x[0][2], patch->y[0][2],
3454
0
       patch->x[0][3], patch->y[0][3]);
3455
0
    state->curveTo(patch->x[1][3], patch->y[1][3],
3456
0
       patch->x[2][3], patch->y[2][3],
3457
0
       patch->x[3][3], patch->y[3][3]);
3458
0
    state->curveTo(patch->x[3][2], patch->y[3][2],
3459
0
       patch->x[3][1], patch->y[3][1],
3460
0
       patch->x[3][0], patch->y[3][0]);
3461
0
    state->curveTo(patch->x[2][0], patch->y[2][0],
3462
0
       patch->x[1][0], patch->y[1][0],
3463
0
       patch->x[0][0], patch->y[0][0]);
3464
0
    state->closePath();
3465
0
    out->fill(state);
3466
0
    state->clearPath();
3467
3468
  // subdivide the patch
3469
0
  } else {
3470
0
    for (i = 0; i < 4; ++i) {
3471
0
      xx[i][0] = patch->x[i][0];
3472
0
      yy[i][0] = patch->y[i][0];
3473
0
      xx[i][1] = 0.5 * (patch->x[i][0] + patch->x[i][1]);
3474
0
      yy[i][1] = 0.5 * (patch->y[i][0] + patch->y[i][1]);
3475
0
      xxm = 0.5 * (patch->x[i][1] + patch->x[i][2]);
3476
0
      yym = 0.5 * (patch->y[i][1] + patch->y[i][2]);
3477
0
      xx[i][6] = 0.5 * (patch->x[i][2] + patch->x[i][3]);
3478
0
      yy[i][6] = 0.5 * (patch->y[i][2] + patch->y[i][3]);
3479
0
      xx[i][2] = 0.5 * (xx[i][1] + xxm);
3480
0
      yy[i][2] = 0.5 * (yy[i][1] + yym);
3481
0
      xx[i][5] = 0.5 * (xxm + xx[i][6]);
3482
0
      yy[i][5] = 0.5 * (yym + yy[i][6]);
3483
0
      xx[i][3] = xx[i][4] = 0.5 * (xx[i][2] + xx[i][5]);
3484
0
      yy[i][3] = yy[i][4] = 0.5 * (yy[i][2] + yy[i][5]);
3485
0
      xx[i][7] = patch->x[i][3];
3486
0
      yy[i][7] = patch->y[i][3];
3487
0
    }
3488
0
    for (i = 0; i < 4; ++i) {
3489
0
      patch00.x[0][i] = xx[0][i];
3490
0
      patch00.y[0][i] = yy[0][i];
3491
0
      patch00.x[1][i] = 0.5 * (xx[0][i] + xx[1][i]);
3492
0
      patch00.y[1][i] = 0.5 * (yy[0][i] + yy[1][i]);
3493
0
      xxm = 0.5 * (xx[1][i] + xx[2][i]);
3494
0
      yym = 0.5 * (yy[1][i] + yy[2][i]);
3495
0
      patch10.x[2][i] = 0.5 * (xx[2][i] + xx[3][i]);
3496
0
      patch10.y[2][i] = 0.5 * (yy[2][i] + yy[3][i]);
3497
0
      patch00.x[2][i] = 0.5 * (patch00.x[1][i] + xxm);
3498
0
      patch00.y[2][i] = 0.5 * (patch00.y[1][i] + yym);
3499
0
      patch10.x[1][i] = 0.5 * (xxm + patch10.x[2][i]);
3500
0
      patch10.y[1][i] = 0.5 * (yym + patch10.y[2][i]);
3501
0
      patch00.x[3][i] = 0.5 * (patch00.x[2][i] + patch10.x[1][i]);
3502
0
      patch00.y[3][i] = 0.5 * (patch00.y[2][i] + patch10.y[1][i]);
3503
0
      patch10.x[0][i] = patch00.x[3][i];
3504
0
      patch10.y[0][i] = patch00.y[3][i];
3505
0
      patch10.x[3][i] = xx[3][i];
3506
0
      patch10.y[3][i] = yy[3][i];
3507
0
    }
3508
0
    for (i = 4; i < 8; ++i) {
3509
0
      patch01.x[0][i-4] = xx[0][i];
3510
0
      patch01.y[0][i-4] = yy[0][i];
3511
0
      patch01.x[1][i-4] = 0.5 * (xx[0][i] + xx[1][i]);
3512
0
      patch01.y[1][i-4] = 0.5 * (yy[0][i] + yy[1][i]);
3513
0
      xxm = 0.5 * (xx[1][i] + xx[2][i]);
3514
0
      yym = 0.5 * (yy[1][i] + yy[2][i]);
3515
0
      patch11.x[2][i-4] = 0.5 * (xx[2][i] + xx[3][i]);
3516
0
      patch11.y[2][i-4] = 0.5 * (yy[2][i] + yy[3][i]);
3517
0
      patch01.x[2][i-4] = 0.5 * (patch01.x[1][i-4] + xxm);
3518
0
      patch01.y[2][i-4] = 0.5 * (patch01.y[1][i-4] + yym);
3519
0
      patch11.x[1][i-4] = 0.5 * (xxm + patch11.x[2][i-4]);
3520
0
      patch11.y[1][i-4] = 0.5 * (yym + patch11.y[2][i-4]);
3521
0
      patch01.x[3][i-4] = 0.5 * (patch01.x[2][i-4] + patch11.x[1][i-4]);
3522
0
      patch01.y[3][i-4] = 0.5 * (patch01.y[2][i-4] + patch11.y[1][i-4]);
3523
0
      patch11.x[0][i-4] = patch01.x[3][i-4];
3524
0
      patch11.y[0][i-4] = patch01.y[3][i-4];
3525
0
      patch11.x[3][i-4] = xx[3][i];
3526
0
      patch11.y[3][i-4] = yy[3][i];
3527
0
    }
3528
0
    for (i = 0; i < shading->getNComps(); ++i) {
3529
0
      patch00.color[0][0][i] = patch->color[0][0][i];
3530
0
      patch00.color[0][1][i] = 0.5 * (patch->color[0][0][i] +
3531
0
              patch->color[0][1][i]);
3532
0
      patch01.color[0][0][i] = patch00.color[0][1][i];
3533
0
      patch01.color[0][1][i] = patch->color[0][1][i];
3534
0
      patch01.color[1][1][i] = 0.5 * (patch->color[0][1][i] +
3535
0
              patch->color[1][1][i]);
3536
0
      patch11.color[0][1][i] = patch01.color[1][1][i];
3537
0
      patch11.color[1][1][i] = patch->color[1][1][i];
3538
0
      patch11.color[1][0][i] = 0.5 * (patch->color[1][1][i] +
3539
0
              patch->color[1][0][i]);
3540
0
      patch10.color[1][1][i] = patch11.color[1][0][i];
3541
0
      patch10.color[1][0][i] = patch->color[1][0][i];
3542
0
      patch10.color[0][0][i] = 0.5 * (patch->color[1][0][i] +
3543
0
              patch->color[0][0][i]);
3544
0
      patch00.color[1][0][i] = patch10.color[0][0][i];
3545
0
      patch00.color[1][1][i] = 0.5 * (patch00.color[1][0][i] +
3546
0
              patch01.color[1][1][i]);
3547
0
      patch01.color[1][0][i] = patch00.color[1][1][i];
3548
0
      patch11.color[0][0][i] = patch00.color[1][1][i];
3549
0
      patch10.color[0][1][i] = patch00.color[1][1][i];
3550
0
    }
3551
0
    fillPatch(&patch00, shading, depth + 1);
3552
0
    fillPatch(&patch10, shading, depth + 1);
3553
0
    fillPatch(&patch01, shading, depth + 1);
3554
0
    fillPatch(&patch11, shading, depth + 1);
3555
0
  }
3556
0
}
3557
3558
417k
void Gfx::doEndPath() {
3559
417k
  if (clip != clipNone) {
3560
88.8k
    if (state->isCurPt()) {
3561
37.9k
      state->clip();
3562
37.9k
      if (clip == clipNormal) {
3563
34.0k
  out->clip(state);
3564
34.0k
      } else {
3565
3.92k
  out->eoClip(state);
3566
3.92k
      }
3567
50.8k
    } else {
3568
50.8k
      error(errSyntaxError, getPos(), "Empty path in clip");
3569
50.8k
    }
3570
88.8k
  }
3571
417k
  clip = clipNone;
3572
417k
  state->clearPath();
3573
417k
}
3574
3575
//------------------------------------------------------------------------
3576
// path clipping operators
3577
//------------------------------------------------------------------------
3578
3579
154k
void Gfx::opClip(Object args[], int numArgs) {
3580
154k
  clip = clipNormal;
3581
154k
}
3582
3583
4.30k
void Gfx::opEOClip(Object args[], int numArgs) {
3584
4.30k
  clip = clipEO;
3585
4.30k
}
3586
3587
//------------------------------------------------------------------------
3588
// text object operators
3589
//------------------------------------------------------------------------
3590
3591
338k
void Gfx::opBeginText(Object args[], int numArgs) {
3592
338k
  state->setTextMat(1, 0, 0, 1, 0, 0);
3593
338k
  state->textMoveTo(0, 0);
3594
338k
  out->updateTextMat(state);
3595
338k
  out->updateTextPos(state);
3596
338k
  fontChanged = gTrue;
3597
338k
  haveSavedClipPath = gFalse;
3598
338k
}
3599
3600
341k
void Gfx::opEndText(Object args[], int numArgs) {
3601
341k
  if (haveSavedClipPath) {
3602
2.15k
    out->clipToSavedClipPath(state);
3603
2.15k
    haveSavedClipPath = gFalse;
3604
2.15k
  }
3605
3606
341k
  out->endTextObject(state);
3607
341k
}
3608
3609
//------------------------------------------------------------------------
3610
// text state operators
3611
//------------------------------------------------------------------------
3612
3613
11.3k
void Gfx::opSetCharSpacing(Object args[], int numArgs) {
3614
11.3k
  state->setCharSpace(args[0].getNum());
3615
11.3k
  out->updateCharSpace(state);
3616
11.3k
}
3617
3618
135k
void Gfx::opSetFont(Object args[], int numArgs) {
3619
135k
  doSetFont(res->lookupFont(args[0].getName()), args[1].getNum());
3620
135k
}
3621
3622
135k
void Gfx::doSetFont(GfxFont *font, double size) {
3623
135k
  if (!font) {
3624
54.5k
    if (!defaultFont) {
3625
11.4k
      defaultFont = GfxFont::makeDefaultFont(xref);
3626
11.4k
    }
3627
54.5k
    font = defaultFont;
3628
54.5k
  }
3629
135k
  if (printCommands) {
3630
0
    printf("  font: tag=%s name='%s' %g\n",
3631
0
     font->getTag()->getCString(),
3632
0
     font->getName() ? font->getName()->getCString() : "???",
3633
0
     size);
3634
0
    fflush(stdout);
3635
0
  }
3636
135k
  state->setFont(font, size);
3637
135k
  fontChanged = gTrue;
3638
135k
}
3639
3640
3.07k
void Gfx::opSetTextLeading(Object args[], int numArgs) {
3641
3.07k
  state->setLeading(args[0].getNum());
3642
3.07k
}
3643
3644
11.3k
void Gfx::opSetTextRender(Object args[], int numArgs) {
3645
11.3k
  state->setRender(args[0].getInt());
3646
11.3k
  out->updateRender(state);
3647
11.3k
}
3648
3649
1.70k
void Gfx::opSetTextRise(Object args[], int numArgs) {
3650
1.70k
  state->setRise(args[0].getNum());
3651
1.70k
  out->updateRise(state);
3652
1.70k
}
3653
3654
11.4k
void Gfx::opSetWordSpacing(Object args[], int numArgs) {
3655
11.4k
  state->setWordSpace(args[0].getNum());
3656
11.4k
  out->updateWordSpace(state);
3657
11.4k
}
3658
3659
13.5k
void Gfx::opSetHorizScaling(Object args[], int numArgs) {
3660
13.5k
  state->setHorizScaling(args[0].getNum());
3661
13.5k
  out->updateHorizScaling(state);
3662
13.5k
  fontChanged = gTrue;
3663
13.5k
}
3664
3665
//------------------------------------------------------------------------
3666
// text positioning operators
3667
//------------------------------------------------------------------------
3668
3669
94.0k
void Gfx::opTextMove(Object args[], int numArgs) {
3670
94.0k
  double tx, ty;
3671
3672
94.0k
  tx = state->getLineX() + args[0].getNum();
3673
94.0k
  ty = state->getLineY() + args[1].getNum();
3674
94.0k
  state->textMoveTo(tx, ty);
3675
94.0k
  out->updateTextPos(state);
3676
94.0k
}
3677
3678
36.3k
void Gfx::opTextMoveSet(Object args[], int numArgs) {
3679
36.3k
  double tx, ty;
3680
3681
36.3k
  tx = state->getLineX() + args[0].getNum();
3682
36.3k
  ty = args[1].getNum();
3683
36.3k
  state->setLeading(-ty);
3684
36.3k
  ty += state->getLineY();
3685
36.3k
  state->textMoveTo(tx, ty);
3686
36.3k
  out->updateTextPos(state);
3687
36.3k
}
3688
3689
188k
void Gfx::opSetTextMatrix(Object args[], int numArgs) {
3690
188k
  state->setTextMat(args[0].getNum(), args[1].getNum(),
3691
188k
        args[2].getNum(), args[3].getNum(),
3692
188k
        args[4].getNum(), args[5].getNum());
3693
188k
  state->textMoveTo(0, 0);
3694
188k
  out->updateTextMat(state);
3695
188k
  out->updateTextPos(state);
3696
188k
  fontChanged = gTrue;
3697
188k
}
3698
3699
1.66k
void Gfx::opTextNextLine(Object args[], int numArgs) {
3700
1.66k
  double tx, ty;
3701
3702
1.66k
  tx = state->getLineX();
3703
1.66k
  ty = state->getLineY() - state->getLeading();
3704
1.66k
  state->textMoveTo(tx, ty);
3705
1.66k
  out->updateTextPos(state);
3706
1.66k
}
3707
3708
//------------------------------------------------------------------------
3709
// text string operators
3710
//------------------------------------------------------------------------
3711
3712
355k
void Gfx::opShowText(Object args[], int numArgs) {
3713
355k
  if (!state->getFont()) {
3714
17.8k
    error(errSyntaxError, getPos(), "No font in show");
3715
17.8k
    return;
3716
17.8k
  }
3717
337k
  if (fontChanged) {
3718
259k
    out->updateFont(state);
3719
259k
    fontChanged = gFalse;
3720
259k
  }
3721
337k
  if (ocState) {
3722
337k
    out->beginStringOp(state);
3723
337k
    doShowText(args[0].getString());
3724
337k
    out->endStringOp(state);
3725
337k
  } else {
3726
21
    doIncCharCount(args[0].getString());
3727
21
  }
3728
337k
}
3729
3730
2.17k
void Gfx::opMoveShowText(Object args[], int numArgs) {
3731
2.17k
  double tx, ty;
3732
3733
2.17k
  if (!state->getFont()) {
3734
52
    error(errSyntaxError, getPos(), "No font in move/show");
3735
52
    return;
3736
52
  }
3737
2.12k
  if (fontChanged) {
3738
1.61k
    out->updateFont(state);
3739
1.61k
    fontChanged = gFalse;
3740
1.61k
  }
3741
2.12k
  tx = state->getLineX();
3742
2.12k
  ty = state->getLineY() - state->getLeading();
3743
2.12k
  state->textMoveTo(tx, ty);
3744
2.12k
  out->updateTextPos(state);
3745
2.12k
  if (ocState) {
3746
2.11k
    out->beginStringOp(state);
3747
2.11k
    doShowText(args[0].getString());
3748
2.11k
    out->endStringOp(state);
3749
2.11k
  } else {
3750
11
    doIncCharCount(args[0].getString());
3751
11
  }
3752
2.12k
}
3753
3754
993
void Gfx::opMoveSetShowText(Object args[], int numArgs) {
3755
993
  double tx, ty;
3756
3757
993
  if (!state->getFont()) {
3758
48
    error(errSyntaxError, getPos(), "No font in move/set/show");
3759
48
    return;
3760
48
  }
3761
945
  if (fontChanged) {
3762
532
    out->updateFont(state);
3763
532
    fontChanged = gFalse;
3764
532
  }
3765
945
  state->setWordSpace(args[0].getNum());
3766
945
  state->setCharSpace(args[1].getNum());
3767
945
  tx = state->getLineX();
3768
945
  ty = state->getLineY() - state->getLeading();
3769
945
  state->textMoveTo(tx, ty);
3770
945
  out->updateWordSpace(state);
3771
945
  out->updateCharSpace(state);
3772
945
  out->updateTextPos(state);
3773
945
  if (ocState) {
3774
945
    out->beginStringOp(state);
3775
945
    doShowText(args[2].getString());
3776
945
    out->endStringOp(state);
3777
945
  } else {
3778
0
    doIncCharCount(args[2].getString());
3779
0
  }
3780
945
}
3781
3782
46.3k
void Gfx::opShowSpaceText(Object args[], int numArgs) {
3783
46.3k
  Array *a;
3784
46.3k
  Object obj;
3785
46.3k
  int wMode;
3786
46.3k
  int i;
3787
3788
46.3k
  if (!state->getFont()) {
3789
7.04k
    error(errSyntaxError, getPos(), "No font in show/space");
3790
7.04k
    return;
3791
7.04k
  }
3792
39.2k
  if (fontChanged) {
3793
30.4k
    out->updateFont(state);
3794
30.4k
    fontChanged = gFalse;
3795
30.4k
  }
3796
39.2k
  if (ocState) {
3797
39.2k
    out->beginStringOp(state);
3798
39.2k
    wMode = state->getFont()->getWMode();
3799
39.2k
    a = args[0].getArray();
3800
883k
    for (i = 0; i < a->getLength(); ++i) {
3801
844k
      a->get(i, &obj);
3802
844k
      if (obj.isNum()) {
3803
408k
  if (wMode) {
3804
1.28k
    state->textShift(0, -obj.getNum() * 0.001 *
3805
1.28k
         state->getFontSize());
3806
406k
  } else {
3807
406k
    state->textShift(-obj.getNum() * 0.001 *
3808
406k
         state->getFontSize() *
3809
406k
         state->getHorizScaling(), 0);
3810
406k
  }
3811
408k
  out->updateTextShift(state, obj.getNum());
3812
435k
      } else if (obj.isString()) {
3813
354k
  doShowText(obj.getString());
3814
354k
      } else {
3815
81.8k
  error(errSyntaxError, getPos(),
3816
81.8k
        "Element of show/space array must be number or string");
3817
81.8k
      }
3818
844k
      obj.free();
3819
844k
    }
3820
39.2k
    out->endStringOp(state);
3821
39.2k
  } else {
3822
35
    a = args[0].getArray();
3823
432
    for (i = 0; i < a->getLength(); ++i) {
3824
397
      a->get(i, &obj);
3825
397
      if (obj.isString()) {
3826
43
  doIncCharCount(obj.getString());
3827
43
      }
3828
397
      obj.free();
3829
397
    }
3830
35
  }
3831
39.2k
}
3832
3833
695k
void Gfx::doShowText(GString *s) {
3834
695k
  GfxFont *font = state->getFont();
3835
695k
  int wMode = font->getWMode();
3836
3837
695k
  if (globalParams->isDroppedFont(font->getName()
3838
695k
            ? font->getName()->getCString() : "")) {
3839
0
    doIncCharCount(s);
3840
0
    return;
3841
0
  }
3842
3843
695k
  if (out->useDrawChar()) {
3844
695k
    out->beginString(state, s);
3845
695k
  }
3846
3847
  // figure out the drawing mode
3848
  // note: clipping, pattern fill, and pattern stroke are all handled
3849
  //       by saving the path and then performing the appropriate
3850
  //       actions in opEndText()
3851
695k
  GBool doFill = gFalse;
3852
695k
  GBool doStroke = gFalse;
3853
695k
  GBool doMakePath = gFalse;
3854
695k
  int render = state->getRender() & 7;
3855
695k
  switch (render & 7) {
3856
677k
  case 0:     // fill
3857
677k
    if (state->getFillColorSpace()->getMode() == csPattern) {
3858
20.2k
      doMakePath = gTrue;
3859
657k
    } else {
3860
657k
      doFill = gTrue;
3861
657k
    }
3862
677k
    break;
3863
266
  case 1:     // stroke
3864
266
    if (state->getStrokeColorSpace()->getMode() == csPattern) {
3865
0
      doMakePath = gTrue;
3866
266
    } else {
3867
266
      doStroke = gTrue;
3868
266
    }
3869
266
    break;
3870
11.4k
  case 2:     // fill + stroke
3871
11.4k
    if (state->getFillColorSpace()->getMode() == csPattern ||
3872
7.84k
  state->getStrokeColorSpace()->getMode() == csPattern) {
3873
3.60k
      doMakePath = gTrue;
3874
7.81k
    } else {
3875
7.81k
      doFill = gTrue;
3876
7.81k
      doStroke = gTrue;
3877
7.81k
    }
3878
11.4k
    break;
3879
90
  case 3:     // invisible
3880
    // nothing
3881
90
    break;
3882
104
  case 4:     // fill + clip
3883
1.22k
  case 5:     // stroke + clip
3884
1.50k
  case 6:     // fill + stroke + clip
3885
5.58k
  case 7:     // clip
3886
5.58k
    doMakePath = gTrue;
3887
5.58k
    break;
3888
695k
  }
3889
3890
695k
  double riseX, riseY;
3891
695k
  state->textTransformDelta(0, state->getRise(), &riseX, &riseY);
3892
695k
  double xMin = state->getCurX() + riseX;
3893
695k
  double yMin = state->getCurY() + riseY;
3894
3895
  // handle a Type 3 char
3896
695k
  if (font->getType() == fontType3 && out->interpretType3Chars()) {
3897
33.9k
    double *mat = state->getCTM();
3898
33.9k
    double oldCTM[6];
3899
237k
    for (int i = 0; i < 6; ++i) {
3900
203k
      oldCTM[i] = mat[i];
3901
203k
    }
3902
33.9k
    mat = state->getTextMat();
3903
33.9k
    double newCTM[6];
3904
33.9k
    newCTM[0] = mat[0] * oldCTM[0] + mat[1] * oldCTM[2];
3905
33.9k
    newCTM[1] = mat[0] * oldCTM[1] + mat[1] * oldCTM[3];
3906
33.9k
    newCTM[2] = mat[2] * oldCTM[0] + mat[3] * oldCTM[2];
3907
33.9k
    newCTM[3] = mat[2] * oldCTM[1] + mat[3] * oldCTM[3];
3908
33.9k
    mat = font->getFontMatrix();
3909
33.9k
    newCTM[0] = mat[0] * newCTM[0] + mat[1] * newCTM[2];
3910
33.9k
    newCTM[1] = mat[0] * newCTM[1] + mat[1] * newCTM[3];
3911
33.9k
    newCTM[2] = mat[2] * newCTM[0] + mat[3] * newCTM[2];
3912
33.9k
    newCTM[3] = mat[2] * newCTM[1] + mat[3] * newCTM[3];
3913
33.9k
    newCTM[0] *= state->getFontSize();
3914
33.9k
    newCTM[1] *= state->getFontSize();
3915
33.9k
    newCTM[2] *= state->getFontSize();
3916
33.9k
    newCTM[3] *= state->getFontSize();
3917
33.9k
    newCTM[0] *= state->getHorizScaling();
3918
33.9k
    newCTM[1] *= state->getHorizScaling();
3919
33.9k
    double curX = state->getCurX();
3920
33.9k
    double curY = state->getCurY();
3921
33.9k
    Parser *oldParser = parser;
3922
33.9k
    char *p = s->getCString();
3923
33.9k
    int len = s->getLength();
3924
3.28M
    while (len > 0) {
3925
3.25M
      CharCode code;
3926
3.25M
      Unicode u[8];
3927
3.25M
      int uLen;
3928
3.25M
      double dx, dy, originX, originY;
3929
3.25M
      int n = font->getNextChar(p, len, &code,
3930
3.25M
        u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
3931
3.25M
        &dx, &dy, &originX, &originY);
3932
3.25M
      dx = dx * state->getFontSize() + state->getCharSpace();
3933
3.25M
      if (n == 1 && *p == ' ') {
3934
218k
  dx += state->getWordSpace();
3935
218k
      }
3936
3.25M
      dx *= state->getHorizScaling();
3937
3.25M
      dy *= state->getFontSize();
3938
3.25M
      double tdx, tdy, ddx, ddy, x, y;
3939
3.25M
      state->textTransformDelta(dx, dy, &tdx, &tdy);
3940
3.25M
      state->transform(curX + riseX, curY + riseY, &x, &y);
3941
3.25M
      GfxState *savedState = saveStateStack();
3942
3.25M
      state->setCTM(newCTM[0], newCTM[1], newCTM[2], newCTM[3], x, y);
3943
      //~ the CTM concat values here are wrong (but never used)
3944
3.25M
      out->updateCTM(state, 1, 0, 0, 1, 0, 0);
3945
3.25M
      state->transformDelta(dx, dy, &ddx, &ddy);
3946
#if 0
3947
      // The PDF spec says: "the graphics state shall be inherited
3948
      // from the environment of the text-showing operator that caused
3949
      // the [Type 3] glyph description to be invoked".  However,
3950
      // Acrobat apparently copies the fill color to the stroke color.
3951
      // It looks like Ghostscript does the same.  (This is only
3952
      // relevant for uncached Type 3 glyphs.)  Uncomment this block
3953
      // to make Xpdf mimic Adobe (in violation of the PDF spec).
3954
      state->setStrokeColorSpace(state->getFillColorSpace()->copy());
3955
      out->updateStrokeColorSpace(state);
3956
      state->setStrokeColor(state->getFillColor());
3957
      out->updateStrokeColor(state);
3958
#endif
3959
3.25M
      if (!out->beginType3Char(state, curX + riseX, curY + riseY, ddx, ddy,
3960
3.25M
             code, u, uLen)) {
3961
3.24M
  Object charProcRef, charProc;
3962
3.24M
  ((Gfx8BitFont *)font)->getCharProcNF(code, &charProcRef);
3963
3.24M
  charProcRef.fetch(xref, &charProc);
3964
3.24M
  Dict *resDict = ((Gfx8BitFont *)font)->getResources();
3965
3.24M
  if (resDict) {
3966
2.71M
    pushResources(resDict);
3967
2.71M
  }
3968
3.24M
  if (charProc.isStream()) {
3969
80.8k
    display(&charProcRef, gFalse);
3970
3.16M
  } else {
3971
3.16M
    error(errSyntaxError, getPos(),
3972
3.16M
    "Missing or bad Type3 CharProc entry");
3973
3.16M
  }
3974
3.24M
  out->endType3Char(state);
3975
3.24M
  if (resDict) {
3976
2.71M
    popResources();
3977
2.71M
  }
3978
3.24M
  charProc.free();
3979
3.24M
  charProcRef.free();
3980
3.24M
      }
3981
3.25M
      restoreStateStack(savedState);
3982
3.25M
      curX += tdx;
3983
3.25M
      curY += tdy;
3984
3.25M
      state->moveTo(curX, curY);
3985
3.25M
      p += n;
3986
3.25M
      len -= n;
3987
3.25M
    }
3988
33.9k
    parser = oldParser;
3989
3990
661k
  } else if (out->useDrawChar()) {
3991
661k
    char *p = s->getCString();
3992
661k
    int len = s->getLength();
3993
5.16M
    while (len > 0) {
3994
4.50M
      CharCode code;
3995
4.50M
      Unicode u[8];
3996
4.50M
      int uLen;
3997
4.50M
      double dx, dy, originX, originY;
3998
4.50M
      int n = font->getNextChar(p, len, &code,
3999
4.50M
        u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
4000
4.50M
        &dx, &dy, &originX, &originY);
4001
4.50M
      if (wMode) {
4002
3.43k
  dx *= state->getFontSize();
4003
3.43k
  dy = dy * state->getFontSize() + state->getCharSpace();
4004
3.43k
  if (n == 1 && *p == ' ') {
4005
318
    dy += state->getWordSpace();
4006
318
  }
4007
4.49M
      } else {
4008
4.49M
  dx = dx * state->getFontSize() + state->getCharSpace();
4009
4.49M
  if (n == 1 && *p == ' ') {
4010
341k
    dx += state->getWordSpace();
4011
341k
  }
4012
4.49M
  dx *= state->getHorizScaling();
4013
4.49M
  dy *= state->getFontSize();
4014
4.49M
      }
4015
4.50M
      double tdx, tdy, tOriginX, tOriginY;
4016
4.50M
      state->textTransformDelta(dx, dy, &tdx, &tdy);
4017
4.50M
      originX *= state->getFontSize();
4018
4.50M
      originY *= state->getFontSize();
4019
4.50M
      state->textTransformDelta(originX, originY, &tOriginX, &tOriginY);
4020
4.50M
      out->drawChar(state, state->getCurX() + riseX, state->getCurY() + riseY,
4021
4.50M
        tdx, tdy, tOriginX, tOriginY, code, n, u, uLen,
4022
4.50M
        doFill, doStroke, doMakePath);
4023
4.50M
      state->shift(tdx, tdy);
4024
4.50M
      p += n;
4025
4.50M
      len -= n;
4026
4.50M
    }
4027
4028
661k
  } else {
4029
0
    double dx = 0, dy = 0;
4030
0
    char *p = s->getCString();
4031
0
    int len = s->getLength();
4032
0
    int nChars = 0, nSpaces = 0;
4033
0
    while (len > 0) {
4034
0
      CharCode code;
4035
0
      Unicode u[8];
4036
0
      int uLen;
4037
0
      double dx2, dy2, originX, originY;
4038
0
      int n = font->getNextChar(p, len, &code,
4039
0
        u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
4040
0
        &dx2, &dy2, &originX, &originY);
4041
0
      dx += dx2;
4042
0
      dy += dy2;
4043
0
      if (n == 1 && *p == ' ') {
4044
0
  ++nSpaces;
4045
0
      }
4046
0
      ++nChars;
4047
0
      p += n;
4048
0
      len -= n;
4049
0
    }
4050
0
    if (wMode) {
4051
0
      dx *= state->getFontSize();
4052
0
      dy = dy * state->getFontSize()
4053
0
     + nChars * state->getCharSpace()
4054
0
     + nSpaces * state->getWordSpace();
4055
0
    } else {
4056
0
      dx = dx * state->getFontSize()
4057
0
     + nChars * state->getCharSpace()
4058
0
     + nSpaces * state->getWordSpace();
4059
0
      dx *= state->getHorizScaling();
4060
0
      dy *= state->getFontSize();
4061
0
    }
4062
0
    double tdx, tdy;
4063
0
    state->textTransformDelta(dx, dy, &tdx, &tdy);
4064
0
    out->drawString(state, s, doFill, doStroke, doMakePath);
4065
0
    state->shift(tdx, tdy);
4066
0
  }
4067
4068
695k
  if (out->useDrawChar()) {
4069
695k
    out->endString(state);
4070
695k
  }
4071
4072
695k
  if (doMakePath) {
4073
    // compute the clipping bbox for the saved text path -- assume
4074
    // that the text bounding box does not extend past the baseline in
4075
    // any direction by more than twice the font size
4076
29.4k
    double xMax = state->getCurX() + riseX;
4077
29.4k
    double yMax = state->getCurY() + riseY;
4078
29.4k
    double t;
4079
29.4k
    if (xMin > xMax) {
4080
2.41k
      t = xMin; xMin = xMax; xMax = t;
4081
2.41k
    }
4082
29.4k
    if (yMin > yMax) {
4083
932
      t = yMin; yMin = yMax; yMax = t;
4084
932
    }
4085
29.4k
    double dx1, dy1, dx2, dy2;
4086
29.4k
    state->textTransformDelta(0, state->getFontSize(), &dx1, &dy1);
4087
29.4k
    state->textTransformDelta(state->getFontSize(), 0, &dx2, &dy2);
4088
29.4k
    dx1 = fabs(dx1);
4089
29.4k
    dx2 = fabs(dx2);
4090
29.4k
    if (dx2 > dx1) {
4091
22.8k
      dx1 = dx2;
4092
22.8k
    }
4093
29.4k
    dy1 = fabs(dy1);
4094
29.4k
    dy2 = fabs(dy2);
4095
29.4k
    if (dy2 > dy1) {
4096
644
      dy1 = dy2;
4097
644
    }
4098
29.4k
    xMin -= 2 * dx1;
4099
29.4k
    yMin -= 2 * dy1;
4100
29.4k
    xMax += 2 * dx1;
4101
29.4k
    yMax += 2 * dy1;
4102
4103
    //--- fill
4104
29.4k
    if ((render & 3) == 0 || (render & 3) == 2) {
4105
24.2k
      if (state->getFillColorSpace()->getMode() == csPattern) {
4106
23.8k
  saveState();
4107
23.8k
  state->clipToRect(xMin, yMin, xMax, yMax);
4108
23.8k
  out->clipToTextPath(state);
4109
23.8k
  doPatternText(gFalse);
4110
23.8k
  restoreState();
4111
23.8k
      } else {
4112
419
  out->fillTextPath(state);
4113
419
      }
4114
24.2k
    }
4115
4116
    //--- stroke
4117
29.4k
    if ((render & 3) == 1 || (render & 3) == 2) {
4118
5.00k
      if (state->getStrokeColorSpace()->getMode() == csPattern) {
4119
30
  saveState();
4120
30
  state->clipToRect(xMin, yMin, xMax, yMax);
4121
30
  out->clipToTextStrokePath(state);
4122
30
  doPatternText(gTrue);
4123
30
  restoreState();
4124
4.97k
      } else {
4125
4.97k
  out->strokeTextPath(state);
4126
4.97k
      }
4127
5.00k
    }
4128
4129
    //--- clip
4130
29.4k
    if (render & 4) {
4131
5.58k
      out->addTextPathToSavedClipPath(state);
4132
5.58k
      haveSavedClipPath = gTrue;
4133
23.8k
    } else {
4134
23.8k
      out->clearTextPath(state);
4135
23.8k
    }
4136
29.4k
  }
4137
4138
695k
  opCounter += 10 * s->getLength();
4139
695k
}
4140
4141
// NB: this is only called when ocState is false.
4142
75
void Gfx::doIncCharCount(GString *s) {
4143
75
  if (out->needCharCount()) {
4144
0
    out->incCharCount(s->getLength());
4145
0
  }
4146
75
}
4147
4148
//------------------------------------------------------------------------
4149
// XObject operators
4150
//------------------------------------------------------------------------
4151
4152
96.6k
void Gfx::opXObject(Object args[], int numArgs) {
4153
96.6k
  char *name;
4154
96.6k
  Object xObj, refObj, obj2, obj3;
4155
96.6k
  GBool ocSaved, oc;
4156
96.6k
#if OPI_SUPPORT
4157
96.6k
  Object opiDict;
4158
96.6k
#endif
4159
4160
96.6k
  if (!ocState && !out->needCharCount()) {
4161
257
    return;
4162
257
  }
4163
96.4k
  name = args[0].getName();
4164
  // NB: we get both the reference and the object here, to make sure
4165
  // they refer to the same object.  There's a problematic corner
4166
  // case: if the resource dict contains an entry for [name] with a
4167
  // reference to a nonexistent object ("/name 99999 0 R", where
4168
  // object 99999 doesn't exist), and a parent resource dict contains
4169
  // a valid entry with the same name, then lookupXObjectNF() will
4170
  // return "99999 0 R", but lookupXObject() will return the valid
4171
  // entry.  This causes problems for doImage() and doForm().
4172
96.4k
  if (!res->lookupXObjectNF(name, &refObj)) {
4173
12.4k
    return;
4174
12.4k
  }
4175
83.9k
  if (!refObj.fetch(xref, &xObj)) {
4176
0
    refObj.free();
4177
0
    return;
4178
0
  }
4179
83.9k
  if (!xObj.isStream()) {
4180
13.6k
    error(errSyntaxError, getPos(), "XObject '{0:s}' is wrong type", name);
4181
13.6k
    xObj.free();
4182
13.6k
    refObj.free();
4183
13.6k
    return;
4184
13.6k
  }
4185
4186
  // check for optional content key
4187
70.3k
  ocSaved = ocState;
4188
70.3k
  xObj.streamGetDict()->lookupNF("OC", &obj2);
4189
70.3k
  if (doc->getOptionalContent()->evalOCObject(&obj2, &oc)) {
4190
0
    ocState &= oc;
4191
0
  }
4192
70.3k
  obj2.free();
4193
4194
70.3k
#if USE_EXCEPTIONS
4195
70.3k
  try {
4196
70.3k
#endif
4197
70.3k
#if OPI_SUPPORT
4198
70.3k
    xObj.streamGetDict()->lookup("OPI", &opiDict);
4199
70.3k
    if (opiDict.isDict()) {
4200
0
      out->opiBegin(state, opiDict.getDict());
4201
0
    }
4202
70.3k
#endif
4203
70.3k
    xObj.streamGetDict()->lookup("Subtype", &obj2);
4204
70.3k
    if (obj2.isName("Image")) {
4205
11.4k
      if (out->needNonText()) {
4206
11.4k
  doImage(&refObj, xObj.getStream(), gFalse);
4207
11.4k
      }
4208
58.9k
    } else if (obj2.isName("Form")) {
4209
56.9k
      if (out->useDrawForm() && refObj.isRef()) {
4210
0
  if (ocState) {
4211
0
    out->drawForm(refObj.getRef());
4212
0
  }
4213
56.9k
      } else {
4214
56.9k
  doForm(&refObj, &xObj);
4215
56.9k
      }
4216
56.9k
    } else if (obj2.isName("PS")) {
4217
0
      if (ocState) {
4218
0
  xObj.streamGetDict()->lookup("Level1", &obj3);
4219
0
  out->psXObject(xObj.getStream(),
4220
0
           obj3.isStream() ? obj3.getStream() : (Stream *)NULL);
4221
0
  obj3.free();
4222
0
      }
4223
1.96k
    } else if (obj2.isName()) {
4224
116
      error(errSyntaxError, getPos(),
4225
116
      "Unknown XObject subtype '{0:s}'", obj2.getName());
4226
1.85k
    } else {
4227
1.85k
      error(errSyntaxError, getPos(),
4228
1.85k
      "XObject subtype is missing or wrong type");
4229
1.85k
    }
4230
70.3k
    obj2.free();
4231
70.3k
#if OPI_SUPPORT
4232
70.3k
    if (opiDict.isDict()) {
4233
0
      out->opiEnd(state, opiDict.getDict());
4234
0
    }
4235
70.3k
    opiDict.free();
4236
70.3k
#endif
4237
70.3k
#if USE_EXCEPTIONS
4238
70.3k
  } catch (GMemException e) {
4239
27
    xObj.free();
4240
27
    refObj.free();
4241
27
    throw;
4242
27
  }
4243
0
#endif
4244
4245
70.2k
  ocState = ocSaved;
4246
4247
70.2k
  xObj.free();
4248
70.2k
  refObj.free();
4249
70.2k
}
4250
4251
78.6k
GBool Gfx::doImage(Object *ref, Stream *str, GBool inlineImg) {
4252
78.6k
  Dict *dict, *maskDict;
4253
78.6k
  int width, height;
4254
78.6k
  int bits, maskBits;
4255
78.6k
  StreamColorSpaceMode csMode;
4256
78.6k
  GBool mask, invert;
4257
78.6k
  GfxColorSpace *colorSpace, *maskColorSpace;
4258
78.6k
  GfxImageColorMap *colorMap, *maskColorMap;
4259
78.6k
  Object maskObj, smaskObj, maskRef;
4260
78.6k
  GBool haveColorKeyMask, haveExplicitMask, haveSoftMask, haveMatte;
4261
78.6k
  int maskColors[2*gfxColorMaxComps];
4262
78.6k
  int maskWidth, maskHeight;
4263
78.6k
  GBool maskInvert;
4264
78.6k
  Stream *maskStr;
4265
78.6k
  double matte[gfxColorMaxComps];
4266
78.6k
  GBool interpolate;
4267
78.6k
  GfxRenderingIntent riSaved;
4268
78.6k
  Object obj1, obj2;
4269
78.6k
  int i, n;
4270
4271
  // check for optional content
4272
78.6k
  if (!ocState && !inlineImg) {
4273
0
    return gTrue;
4274
0
  }
4275
4276
  // images can have arbitrarily high compression ratios, but the
4277
  // data size is inherently limited
4278
78.6k
  str->disableDecompressionBombChecking();
4279
4280
  // get info from the stream
4281
78.6k
  bits = 0;
4282
78.6k
  csMode = streamCSNone;
4283
78.6k
  str->getImageParams(&bits, &csMode);
4284
4285
  // get stream dict
4286
78.6k
  dict = str->getDict();
4287
4288
  // save the current rendering intent
4289
78.6k
  riSaved = state->getRenderingIntent();
4290
4291
  // get size
4292
78.6k
  dict->lookup("Width", &obj1);
4293
78.6k
  if (obj1.isNull()) {
4294
67.2k
    obj1.free();
4295
67.2k
    dict->lookup("W", &obj1);
4296
67.2k
  }
4297
78.6k
  if (obj1.isInt()) {
4298
77.6k
    width = obj1.getInt();
4299
77.6k
  } else if (obj1.isReal()) {
4300
179
    error(errSyntaxWarning, getPos(), "Non-integer image width");
4301
179
    width = (int)obj1.getReal();
4302
793
  } else {
4303
793
    goto err2;
4304
793
  }
4305
77.8k
  obj1.free();
4306
77.8k
  if (width <= 0) {
4307
118
    goto err1;
4308
118
  }
4309
77.7k
  dict->lookup("Height", &obj1);
4310
77.7k
  if (obj1.isNull()) {
4311
66.4k
    obj1.free();
4312
66.4k
    dict->lookup("H", &obj1);
4313
66.4k
  }
4314
77.7k
  if (obj1.isInt()) {
4315
74.3k
    height = obj1.getInt();
4316
74.3k
  } else if (obj1.isReal()) {
4317
106
    error(errSyntaxWarning, getPos(), "Non-integer image height");
4318
106
    height = (int)obj1.getReal();
4319
3.25k
  } else {
4320
3.25k
    goto err2;
4321
3.25k
  }
4322
74.4k
  obj1.free();
4323
74.4k
  if (height <= 0) {
4324
248
    goto err1;
4325
248
  }
4326
4327
  // image or mask?
4328
74.2k
  dict->lookup("ImageMask", &obj1);
4329
74.2k
  if (obj1.isNull()) {
4330
74.0k
    obj1.free();
4331
74.0k
    dict->lookup("IM", &obj1);
4332
74.0k
  }
4333
74.2k
  mask = gFalse;
4334
74.2k
  if (obj1.isBool())
4335
32.1k
    mask = obj1.getBool();
4336
42.0k
  else if (!obj1.isNull())
4337
1.35k
    goto err2;
4338
72.8k
  obj1.free();
4339
4340
  // bit depth
4341
72.8k
  if (bits == 0) {
4342
72.5k
    dict->lookup("BitsPerComponent", &obj1);
4343
72.5k
    if (obj1.isNull()) {
4344
61.7k
      obj1.free();
4345
61.7k
      dict->lookup("BPC", &obj1);
4346
61.7k
    }
4347
72.5k
    if (obj1.isNum()) {
4348
66.8k
      if (obj1.isInt()) {
4349
66.7k
  bits = obj1.getInt();
4350
66.7k
      } else if (obj1.isReal()) {
4351
88
  error(errSyntaxWarning, getPos(),
4352
88
        "Non-integer image bits per component");
4353
88
  bits = (int)obj1.getReal();
4354
88
      }
4355
66.8k
      if (bits < 1 || bits > 16) {
4356
468
  goto err2;
4357
468
      }
4358
66.8k
    } else if (mask) {
4359
2.16k
      bits = 1;
4360
3.56k
    } else {
4361
3.56k
      goto err2;
4362
3.56k
    }
4363
68.5k
    obj1.free();
4364
68.5k
  }
4365
4366
  // interpolate flag
4367
68.8k
  dict->lookup("Interpolate", &obj1);
4368
68.8k
  if (obj1.isNull()) {
4369
68.1k
    obj1.free();
4370
68.1k
    dict->lookup("I", &obj1);
4371
68.1k
  }
4372
68.8k
  interpolate = obj1.isBool() && obj1.getBool();
4373
68.8k
  obj1.free();
4374
4375
  // display a mask
4376
68.8k
  if (mask) {
4377
4378
    // check for inverted mask
4379
31.9k
    if (bits != 1)
4380
1.58k
      goto err1;
4381
30.3k
    invert = gFalse;
4382
30.3k
    dict->lookup("Decode", &obj1);
4383
30.3k
    if (obj1.isNull()) {
4384
30.2k
      obj1.free();
4385
30.2k
      dict->lookup("D", &obj1);
4386
30.2k
    }
4387
30.3k
    if (obj1.isArray()) {
4388
5.52k
      obj1.arrayGet(0, &obj2);
4389
5.52k
      invert = obj2.isNum() && obj2.getNum() == 1;
4390
5.52k
      obj2.free();
4391
24.7k
    } else if (!obj1.isNull()) {
4392
3.43k
      goto err2;
4393
3.43k
    }
4394
26.8k
    obj1.free();
4395
4396
    // if drawing is disabled, skip over inline image data
4397
26.8k
    if (!ocState) {
4398
0
      str->disableDecompressionBombChecking();
4399
0
      str->reset();
4400
0
      n = height * ((width + 7) / 8);
4401
0
      for (i = 0; i < n; ++i) {
4402
0
  str->getChar();
4403
0
      }
4404
0
      str->close();
4405
4406
    // draw it
4407
26.8k
    } else {
4408
26.8k
      if (state->getFillColorSpace()->getMode() == csPattern) {
4409
152
  doPatternImageMask(ref, str, width, height, invert, inlineImg,
4410
152
         interpolate);
4411
26.7k
      } else {
4412
26.7k
  out->drawImageMask(state, ref, str, width, height, invert, inlineImg,
4413
26.7k
         interpolate);
4414
26.7k
      }
4415
26.8k
    }
4416
4417
36.9k
  } else {
4418
4419
    // rendering intent
4420
36.9k
    if (dict->lookup("Intent", &obj1)->isName()) {
4421
2.91k
      opSetRenderingIntent(&obj1, 1);
4422
2.91k
    }
4423
36.9k
    obj1.free();
4424
4425
    // get color space and color map
4426
36.9k
    dict->lookup("ColorSpace", &obj1);
4427
36.9k
    if (obj1.isNull()) {
4428
28.2k
      obj1.free();
4429
28.2k
      dict->lookup("CS", &obj1);
4430
28.2k
    }
4431
36.9k
    if (obj1.isName()) {
4432
29.8k
      res->lookupColorSpace(obj1.getName(), &obj2);
4433
29.8k
      if (!obj2.isNull()) {
4434
6
  obj1.free();
4435
6
  obj1 = obj2;
4436
29.8k
      } else {
4437
29.8k
  obj2.free();
4438
29.8k
      }
4439
29.8k
    }
4440
36.9k
    if (!obj1.isNull()) {
4441
31.9k
      colorSpace = GfxColorSpace::parse(&obj1
4442
31.9k
          );
4443
31.9k
    } else if (csMode == streamCSDeviceGray) {
4444
26
      colorSpace = GfxColorSpace::create(csDeviceGray);
4445
4.93k
    } else if (csMode == streamCSDeviceRGB) {
4446
17
      colorSpace = GfxColorSpace::create(csDeviceRGB);
4447
4.91k
    } else if (csMode == streamCSDeviceCMYK) {
4448
26
      colorSpace = GfxColorSpace::create(csDeviceCMYK);
4449
4.88k
    } else {
4450
4.88k
      colorSpace = NULL;
4451
4.88k
    }
4452
36.9k
    obj1.free();
4453
36.9k
    if (!colorSpace) {
4454
7.19k
      goto err1;
4455
7.19k
    }
4456
29.7k
    if (colorSpace->getMode() == csPattern) {
4457
45
      error(errSyntaxError, getPos(), "Image with a Pattern color space");
4458
45
      delete colorSpace;
4459
45
      goto err1;
4460
45
    }
4461
29.6k
    dict->lookup("Decode", &obj1);
4462
29.6k
    if (obj1.isNull()) {
4463
29.0k
      obj1.free();
4464
29.0k
      dict->lookup("D", &obj1);
4465
29.0k
    }
4466
29.6k
    colorMap = new GfxImageColorMap(bits, &obj1, colorSpace);
4467
29.6k
    obj1.free();
4468
29.6k
    if (!colorMap->isOk()) {
4469
1.56k
      delete colorMap;
4470
1.56k
      goto err1;
4471
1.56k
    }
4472
4473
    // get the mask
4474
28.1k
    haveColorKeyMask = haveExplicitMask = haveSoftMask = haveMatte = gFalse;
4475
28.1k
    maskStr = NULL; // make gcc happy
4476
28.1k
    maskWidth = maskHeight = 0; // make gcc happy
4477
28.1k
    maskInvert = gFalse; // make gcc happy
4478
28.1k
    maskColorMap = NULL; // make gcc happy
4479
28.1k
    dict->lookup("Mask", &maskObj);
4480
28.1k
    dict->lookup("SMask", &smaskObj);
4481
28.1k
    if (smaskObj.isStream()) {
4482
      // soft mask
4483
3.40k
      if (inlineImg) {
4484
0
  delete colorMap;
4485
0
  maskObj.free();
4486
0
  smaskObj.free();
4487
0
  goto err1;
4488
0
      }
4489
3.40k
      maskStr = smaskObj.getStream();
4490
3.40k
      maskStr->disableDecompressionBombChecking();
4491
3.40k
      maskDict = smaskObj.streamGetDict();
4492
3.40k
      maskDict->lookup("Width", &obj1);
4493
3.40k
      if (obj1.isNull()) {
4494
90
  obj1.free();
4495
90
  maskDict->lookup("W", &obj1);
4496
90
      }
4497
3.40k
      if (obj1.isInt()) {
4498
3.19k
  maskWidth = obj1.getInt();
4499
3.19k
      } else if (obj1.isReal()) {
4500
74
  error(errSyntaxWarning, getPos(), "Non-integer image mask width");
4501
74
  maskWidth = (int)obj1.getReal();
4502
135
      } else {
4503
135
  delete colorMap;
4504
135
  maskObj.free();
4505
135
  smaskObj.free();
4506
135
  goto err2;
4507
135
      }
4508
3.26k
      obj1.free();
4509
3.26k
      maskDict->lookup("Height", &obj1);
4510
3.26k
      if (obj1.isNull()) {
4511
102
  obj1.free();
4512
102
  maskDict->lookup("H", &obj1);
4513
102
      }
4514
3.26k
      if (obj1.isInt()) {
4515
3.08k
  maskHeight = obj1.getInt();
4516
3.08k
      } else if (obj1.isReal()) {
4517
52
  error(errSyntaxWarning, getPos(), "Non-integer image mask height");
4518
52
  maskHeight = (int)obj1.getReal();
4519
127
      } else {
4520
127
  delete colorMap;
4521
127
  maskObj.free();
4522
127
  smaskObj.free();
4523
127
  goto err2;
4524
127
      }
4525
3.13k
      obj1.free();
4526
3.13k
      if (maskWidth <= 0 || maskHeight <= 0) {
4527
92
  delete colorMap;
4528
92
  maskObj.free();
4529
92
  smaskObj.free();
4530
92
  goto err1;
4531
92
      }
4532
3.04k
      maskDict->lookup("BitsPerComponent", &obj1);
4533
3.04k
      if (obj1.isNull()) {
4534
90
  obj1.free();
4535
90
  maskDict->lookup("BPC", &obj1);
4536
90
      }
4537
3.04k
      if (obj1.isInt()) {
4538
2.89k
  maskBits = obj1.getInt();
4539
2.89k
      } else if (obj1.isReal()) {
4540
63
  error(errSyntaxWarning, getPos(),
4541
63
        "Non-integer image mask bits per component");
4542
63
  maskBits = (int)obj1.getReal();
4543
90
      } else {
4544
90
  delete colorMap;
4545
90
  maskObj.free();
4546
90
  smaskObj.free();
4547
90
  goto err2;
4548
90
      }
4549
2.95k
      obj1.free();
4550
2.95k
      if (maskBits < 1 || maskBits > 16) {
4551
20
  delete colorMap;
4552
20
  maskObj.free();
4553
20
  smaskObj.free();
4554
20
  goto err1;
4555
20
      }
4556
2.93k
      maskDict->lookup("ColorSpace", &obj1);
4557
2.93k
      if (obj1.isNull()) {
4558
60
  obj1.free();
4559
60
  maskDict->lookup("CS", &obj1);
4560
60
      }
4561
2.93k
      if (obj1.isName()) {
4562
2.87k
  res->lookupColorSpace(obj1.getName(), &obj2);
4563
2.87k
  if (!obj2.isNull()) {
4564
0
    obj1.free();
4565
0
    obj1 = obj2;
4566
2.87k
  } else {
4567
2.87k
    obj2.free();
4568
2.87k
  }
4569
2.87k
      }
4570
2.93k
      if (!obj1.isName("DeviceGray")) {
4571
190
  delete colorMap;
4572
190
  maskObj.free();
4573
190
  smaskObj.free();
4574
190
  goto err2;
4575
190
      }
4576
2.74k
      maskColorSpace = new GfxDeviceGrayColorSpace();
4577
2.74k
      obj1.free();
4578
2.74k
      maskDict->lookup("Decode", &obj1);
4579
2.74k
      if (obj1.isNull()) {
4580
2.16k
  obj1.free();
4581
2.16k
  maskDict->lookup("D", &obj1);
4582
2.16k
      }
4583
2.74k
      maskColorMap = new GfxImageColorMap(maskBits, &obj1, maskColorSpace);
4584
2.74k
      obj1.free();
4585
2.74k
      if (!maskColorMap->isOk()) {
4586
83
  delete maskColorMap;
4587
83
  delete colorMap;
4588
83
  maskObj.free();
4589
83
  smaskObj.free();
4590
83
  goto err1;
4591
83
      }
4592
2.66k
      if (maskDict->lookup("Matte", &obj1)->isArray()) {
4593
0
  if (obj1.arrayGetLength() == colorSpace->getNComps()) {
4594
0
    for (i = 0; i < obj1.arrayGetLength(); ++i) {
4595
0
      if (obj1.arrayGet(i, &obj2)->isNum()) {
4596
0
        matte[i] = obj2.getNum();
4597
0
      } else {
4598
0
        error(errSyntaxError, getPos(),
4599
0
        "Invalid Matte entry in soft mask");
4600
0
        matte[i] = 0;
4601
0
      }
4602
0
      obj2.free();
4603
0
    }
4604
0
    haveMatte = gTrue;
4605
0
  } else {
4606
0
    error(errSyntaxError, getPos(), "Invalid Matte entry in soft mask");
4607
0
  }
4608
0
      }
4609
2.66k
      obj1.free();
4610
2.66k
      haveSoftMask = gTrue;
4611
24.7k
    } else if (maskObj.isArray()) {
4612
      // color key mask
4613
471
      haveColorKeyMask = gTrue;
4614
471
      for (i = 0;
4615
493
     i+1 < maskObj.arrayGetLength() && i+1 < 2*gfxColorMaxComps;
4616
471
     i += 2) {
4617
340
  maskObj.arrayGet(i, &obj1);
4618
340
  if (!obj1.isInt()) {
4619
49
    obj1.free();
4620
49
    haveColorKeyMask = gFalse;
4621
49
    break;
4622
49
  }
4623
291
  maskColors[i] = obj1.getInt();
4624
291
  obj1.free();
4625
291
  if (maskColors[i] < 0 || maskColors[i] >= (1 << bits)) {
4626
114
    haveColorKeyMask = gFalse;
4627
114
    break;
4628
114
  }
4629
177
  maskObj.arrayGet(i+1, &obj1);
4630
177
  if (!obj1.isInt()) {
4631
41
    obj1.free();
4632
41
    haveColorKeyMask = gFalse;
4633
41
    break;
4634
41
  }
4635
136
  maskColors[i+1] = obj1.getInt();
4636
136
  obj1.free();
4637
136
  if (maskColors[i+1] < 0 || maskColors[i+1] >= (1 << bits) ||
4638
114
      maskColors[i] > maskColors[i+1]) {
4639
114
    haveColorKeyMask = gFalse;
4640
114
    break;
4641
114
  }
4642
136
      }
4643
24.2k
    } else if (maskObj.isStream()) {
4644
      // explicit mask
4645
101
      haveExplicitMask = gTrue;
4646
101
      if (inlineImg) {
4647
0
  delete colorMap;
4648
0
  maskObj.free();
4649
0
  smaskObj.free();
4650
0
  goto err1;
4651
0
      }
4652
101
      maskStr = maskObj.getStream();
4653
101
      maskStr->disableDecompressionBombChecking();
4654
101
      maskDict = maskObj.streamGetDict();
4655
101
      maskDict->lookup("Width", &obj1);
4656
101
      if (obj1.isNull()) {
4657
19
  obj1.free();
4658
19
  maskDict->lookup("W", &obj1);
4659
19
      }
4660
101
      if (obj1.isInt()) {
4661
82
  maskWidth = obj1.getInt();
4662
82
      } else if (obj1.isReal()) {
4663
0
  error(errSyntaxWarning, getPos(), "Non-integer image mask width");
4664
0
  maskWidth = (int)obj1.getReal();
4665
19
      } else {
4666
19
  haveExplicitMask = gFalse;
4667
19
      }
4668
101
      obj1.free();
4669
101
      maskDict->lookup("Height", &obj1);
4670
101
      if (obj1.isNull()) {
4671
4
  obj1.free();
4672
4
  maskDict->lookup("H", &obj1);
4673
4
      }
4674
101
      if (obj1.isInt()) {
4675
19
  maskHeight = obj1.getInt();
4676
82
      } else if (obj1.isReal()) {
4677
78
  error(errSyntaxWarning, getPos(), "Non-integer image mask height");
4678
78
  maskHeight = (int)obj1.getReal();
4679
78
      } else {
4680
4
  haveExplicitMask = gFalse;
4681
4
      }
4682
101
      obj1.free();
4683
101
      if (maskWidth <= 0 || maskHeight <= 0) {
4684
23
  haveExplicitMask = gFalse;
4685
23
      }
4686
101
      maskDict->lookup("ImageMask", &obj1);
4687
101
      if (obj1.isNull()) {
4688
23
  obj1.free();
4689
23
  maskDict->lookup("IM", &obj1);
4690
23
      }
4691
101
      if (!obj1.isBool() || !obj1.getBool()) {
4692
23
  haveExplicitMask = gFalse;
4693
23
      }
4694
101
      obj1.free();
4695
101
      maskInvert = gFalse;
4696
101
      maskDict->lookup("Decode", &obj1);
4697
101
      if (obj1.isNull()) {
4698
24
  obj1.free();
4699
24
  maskDict->lookup("D", &obj1);
4700
24
      }
4701
101
      if (obj1.isArray()) {
4702
77
  obj1.arrayGet(0, &obj2);
4703
77
  maskInvert = obj2.isNum() && obj2.getNum() == 1;
4704
77
  obj2.free();
4705
77
      } else if (!obj1.isNull()) {
4706
0
  haveExplicitMask = gFalse;
4707
0
      }
4708
101
      obj1.free();
4709
101
      if (!haveExplicitMask) {
4710
42
  error(errSyntaxError, getPos(), "Bad image parameters");
4711
42
      }
4712
101
    }
4713
4714
    // if drawing is disabled, skip over inline image data
4715
27.3k
    if (state->getIgnoreColorOps() || !ocState) {
4716
542
      if (state->getIgnoreColorOps()) {
4717
542
  error(errSyntaxWarning, getPos(), "Ignoring image"
4718
542
        " in uncolored Type 3 char or tiling pattern");
4719
542
      }
4720
542
      if (inlineImg) {
4721
450
  str->disableDecompressionBombChecking();
4722
450
  str->reset();
4723
450
  n = height * ((width * colorMap->getNumPixelComps() *
4724
450
           colorMap->getBits() + 7) / 8);
4725
450
  str->discardChars(n);
4726
450
  str->close();
4727
450
      }
4728
4729
    // draw it
4730
26.8k
    } else {
4731
26.8k
      if (haveSoftMask) {
4732
2.66k
  dict->lookupNF("SMask", &maskRef);
4733
2.66k
  out->drawSoftMaskedImage(state, ref, str, width, height, colorMap,
4734
2.66k
         &maskRef, maskStr, maskWidth, maskHeight,
4735
2.66k
         maskColorMap,
4736
2.66k
         haveMatte ? matte : (double *)NULL,
4737
2.66k
         interpolate);
4738
2.66k
  maskRef.free();
4739
2.66k
  delete maskColorMap;
4740
24.1k
      } else if (haveExplicitMask) {
4741
59
  dict->lookupNF("Mask", &maskRef);
4742
59
  out->drawMaskedImage(state, ref, str, width, height, colorMap,
4743
59
           &maskRef, maskStr, maskWidth, maskHeight,
4744
59
           maskInvert, interpolate);
4745
59
  maskRef.free();
4746
24.1k
      } else {
4747
24.1k
  out->drawImage(state, ref, str, width, height, colorMap,
4748
24.1k
           haveColorKeyMask ? maskColors : (int *)NULL, inlineImg,
4749
24.1k
           interpolate);
4750
24.1k
      }
4751
26.8k
    }
4752
4753
27.3k
    delete colorMap;
4754
27.3k
    maskObj.free();
4755
27.3k
    smaskObj.free();
4756
27.3k
  }
4757
4758
  // restore rendering intent
4759
54.2k
  if (state->getRenderingIntent() != riSaved) {
4760
27
    state->setRenderingIntent(riSaved);
4761
27
    out->updateRenderingIntent(state);
4762
27
  }
4763
4764
54.2k
  if ((i = width * height) > 1000) {
4765
17.6k
    i = 1000;
4766
17.6k
  }
4767
54.2k
  opCounter += i;
4768
4769
54.2k
  return gTrue;
4770
4771
13.4k
 err2:
4772
13.4k
  obj1.free();
4773
24.3k
 err1:
4774
24.3k
  error(errSyntaxError, getPos(), "Bad image parameters");
4775
4776
  // restore rendering intent
4777
24.3k
  if (state->getRenderingIntent() != riSaved) {
4778
304
    state->setRenderingIntent(riSaved);
4779
304
    out->updateRenderingIntent(state);
4780
304
  }
4781
4782
24.3k
  return gFalse;
4783
13.4k
}
4784
4785
56.9k
void Gfx::doForm(Object *strRef, Object *str) {
4786
56.9k
  Dict *dict;
4787
56.9k
  GBool transpGroup, isolated, knockout;
4788
56.9k
  Object matrixObj, bboxObj;
4789
56.9k
  double m[6], bbox[4];
4790
56.9k
  Object resObj;
4791
56.9k
  Dict *resDict;
4792
56.9k
  Object obj1, obj2, obj3;
4793
56.9k
  int i;
4794
4795
  // check for optional content
4796
56.9k
  if (!ocState && !out->needCharCount()) {
4797
0
    return;
4798
0
  }
4799
4800
  // get stream dict
4801
56.9k
  dict = str->streamGetDict();
4802
4803
  // check form type
4804
56.9k
  dict->lookup("FormType", &obj1);
4805
56.9k
  if (!(obj1.isNull() || (obj1.isInt() && obj1.getInt() == 1))) {
4806
1.71k
    error(errSyntaxError, getPos(), "Unknown form type");
4807
1.71k
  }
4808
56.9k
  obj1.free();
4809
4810
  // get bounding box
4811
56.9k
  dict->lookup("BBox", &bboxObj);
4812
56.9k
  if (!(bboxObj.isArray() && bboxObj.arrayGetLength() == 4)) {
4813
405
    bboxObj.free();
4814
405
    error(errSyntaxError, getPos(), "Bad form bounding box");
4815
405
    return;
4816
405
  }
4817
282k
  for (i = 0; i < 4; ++i) {
4818
225k
    bboxObj.arrayGet(i, &obj1);
4819
225k
    if (!obj1.isNum()) {
4820
129
      bboxObj.free();
4821
129
      error(errSyntaxError, getPos(), "Bad form bounding box");
4822
129
      return;
4823
129
    }
4824
225k
    bbox[i] = obj1.getNum();
4825
225k
    obj1.free();
4826
225k
  }
4827
56.4k
  bboxObj.free();
4828
4829
  // get matrix
4830
56.4k
  dict->lookup("Matrix", &matrixObj);
4831
56.4k
  if (matrixObj.isArray() && matrixObj.arrayGetLength() == 6) {
4832
31.6k
    for (i = 0; i < 6; ++i) {
4833
27.0k
      matrixObj.arrayGet(i, &obj1);
4834
27.0k
      if (obj1.isNum()) {
4835
27.0k
  m[i] = obj1.getNum();
4836
27.0k
      } else {
4837
63
  m[i] = 0;
4838
63
      }
4839
27.0k
      obj1.free();
4840
27.0k
    }
4841
51.8k
  } else {
4842
51.8k
    m[0] = 1; m[1] = 0;
4843
51.8k
    m[2] = 0; m[3] = 1;
4844
51.8k
    m[4] = 0; m[5] = 0;
4845
51.8k
  }
4846
56.4k
  matrixObj.free();
4847
4848
  // get resources
4849
56.4k
  dict->lookup("Resources", &resObj);
4850
56.4k
  resDict = resObj.isDict() ? resObj.getDict() : (Dict *)NULL;
4851
4852
  // check for a transparency group
4853
56.4k
  transpGroup = isolated = knockout = gFalse;
4854
56.4k
  if (dict->lookup("Group", &obj1)->isDict()) {
4855
46.7k
    if (obj1.dictLookup("S", &obj2)->isName("Transparency")) {
4856
6.48k
      transpGroup = gTrue;
4857
6.48k
      if (obj1.dictLookup("I", &obj3)->isBool()) {
4858
1.36k
  isolated = obj3.getBool();
4859
1.36k
      }
4860
6.48k
      obj3.free();
4861
6.48k
      if (obj1.dictLookup("K", &obj3)->isBool()) {
4862
1.35k
  knockout = obj3.getBool();
4863
1.35k
      }
4864
6.48k
      obj3.free();
4865
6.48k
    }
4866
46.7k
    obj2.free();
4867
46.7k
  }
4868
56.4k
  obj1.free();
4869
4870
  // draw it
4871
56.4k
  drawForm(strRef, resDict, m, bbox, transpGroup, gFalse, isolated, knockout);
4872
4873
56.4k
  resObj.free();
4874
56.4k
}
4875
4876
void Gfx::drawForm(Object *strRef, Dict *resDict,
4877
       double *matrix, double *bbox,
4878
       GBool transpGroup, GBool softMask,
4879
       GBool isolated, GBool knockout,
4880
       GBool alpha, Function *transferFunc,
4881
98.8k
       Object *backdropColorObj) {
4882
98.8k
  Parser *oldParser;
4883
98.8k
  GfxState *savedState;
4884
98.8k
  GfxColorSpace *blendingColorSpace;
4885
98.8k
  GfxColor backdropColor;
4886
98.8k
  Object strObj, groupAttrsObj, csObj, obj1;
4887
98.8k
  double oldBaseMatrix[6];
4888
98.8k
  int i;
4889
4890
98.8k
  if (formDepth > 100) {
4891
0
    error(errSyntaxError, getPos(), "Excessive recursion in Form XObjects");
4892
0
    return;
4893
0
  }
4894
98.8k
  ++formDepth;
4895
4896
98.8k
  out->startStream(strRef->getRef(), state);
4897
4898
  // push new resources on stack
4899
98.8k
  pushResources(resDict);
4900
4901
  // save current graphics state
4902
98.8k
  saveState();
4903
4904
  // kill any pre-existing path
4905
98.8k
  state->clearPath();
4906
4907
  // save current parser
4908
98.8k
  oldParser = parser;
4909
4910
  // set form transformation matrix
4911
98.8k
  state->concatCTM(matrix[0], matrix[1], matrix[2],
4912
98.8k
       matrix[3], matrix[4], matrix[5]);
4913
98.8k
  out->updateCTM(state, matrix[0], matrix[1], matrix[2],
4914
98.8k
     matrix[3], matrix[4], matrix[5]);
4915
4916
  // set form bounding box
4917
98.8k
  state->moveTo(bbox[0], bbox[1]);
4918
98.8k
  state->lineTo(bbox[2], bbox[1]);
4919
98.8k
  state->lineTo(bbox[2], bbox[3]);
4920
98.8k
  state->lineTo(bbox[0], bbox[3]);
4921
98.8k
  state->closePath();
4922
98.8k
  state->clip();
4923
98.8k
  out->clip(state);
4924
98.8k
  state->clearPath();
4925
4926
98.8k
  blendingColorSpace = NULL;
4927
98.8k
  if (softMask || transpGroup) {
4928
    // get the blending color space
4929
    // NB: this must be done AFTER pushing the resource dictionary,
4930
    //     so that any Default*** color spaces are available
4931
8.55k
    strRef->fetch(xref, &strObj);
4932
8.55k
    if (strObj.streamGetDict()->lookup("Group", &groupAttrsObj)->isDict()) {
4933
8.55k
      if (!groupAttrsObj.dictLookup("CS", &csObj)->isNull()) {
4934
3.26k
  blendingColorSpace = GfxColorSpace::parse(&csObj
4935
3.26k
              );
4936
3.26k
      }
4937
8.55k
      csObj.free();
4938
8.55k
    }
4939
8.55k
    groupAttrsObj.free();
4940
8.55k
    strObj.free();
4941
4942
8.55k
    if (!out->beginTransparencyGroup(state, bbox, blendingColorSpace,
4943
8.55k
             isolated, knockout, softMask)) {
4944
1.83k
      transpGroup = gFalse;
4945
1.83k
    }
4946
4947
8.55k
    if (softMask || transpGroup) {
4948
6.72k
      traceBegin(oldBaseMatrix, softMask ? "begin soft mask" : "begin t-group");
4949
6.72k
      if (state->getBlendMode() != gfxBlendNormal) {
4950
392
  state->setBlendMode(gfxBlendNormal);
4951
392
  out->updateBlendMode(state);
4952
392
      }
4953
6.72k
      if (state->getFillOpacity() != 1) {
4954
1.59k
  state->setFillOpacity(1);
4955
1.59k
  out->updateFillOpacity(state);
4956
1.59k
      }
4957
6.72k
      if (state->getStrokeOpacity() != 1) {
4958
1.64k
  state->setStrokeOpacity(1);
4959
1.64k
  out->updateStrokeOpacity(state);
4960
1.64k
      }
4961
6.72k
      out->clearSoftMask(state);
4962
6.72k
    }
4963
8.55k
  }
4964
4965
  // set new base matrix
4966
691k
  for (i = 0; i < 6; ++i) {
4967
592k
    oldBaseMatrix[i] = baseMatrix[i];
4968
592k
    baseMatrix[i] = state->getCTM()[i];
4969
592k
  }
4970
4971
  // save the state stack -- this handles the case where the form
4972
  // contents have unbalanced q/Q operators
4973
98.8k
  savedState = saveStateStack();
4974
4975
  // draw the form
4976
98.8k
  display(strRef, gFalse);
4977
4978
98.8k
  restoreStateStack(savedState);
4979
4980
98.8k
  if (softMask || transpGroup) {
4981
6.71k
    out->endTransparencyGroup(state);
4982
6.71k
  }
4983
4984
  // restore base matrix
4985
691k
  for (i = 0; i < 6; ++i) {
4986
592k
    baseMatrix[i] = oldBaseMatrix[i];
4987
592k
  }
4988
4989
  // restore parser
4990
98.8k
  parser = oldParser;
4991
4992
  // restore graphics state
4993
98.8k
  restoreState();
4994
4995
  // pop resource stack
4996
98.8k
  popResources();
4997
4998
98.8k
  if (softMask) {
4999
68.2k
    for (i = 0; i < gfxColorMaxComps; ++i) {
5000
66.2k
      backdropColor.c[i] = 0;
5001
66.2k
    }
5002
2.06k
    if (backdropColorObj->isArray()) {
5003
669
      for (i = 0;
5004
2.24k
     i < backdropColorObj->arrayGetLength() && i < gfxColorMaxComps;
5005
1.57k
     ++i) {
5006
1.57k
  backdropColorObj->arrayGet(i, &obj1);
5007
1.57k
  if (obj1.isNum()) {
5008
1.42k
    backdropColor.c[i] = dblToCol(obj1.getNum());
5009
1.42k
  }
5010
1.57k
  obj1.free();
5011
1.57k
      }
5012
1.40k
    } else if (blendingColorSpace) {
5013
874
      blendingColorSpace->getDefaultColor(&backdropColor);
5014
874
    }
5015
    //~ else: need to get the parent or default color space (?)
5016
2.06k
    out->setSoftMask(state, bbox, alpha, transferFunc, &backdropColor);
5017
2.06k
    traceEnd(oldBaseMatrix, "end soft mask");
5018
96.7k
  } else if (transpGroup) {
5019
4.65k
    out->paintTransparencyGroup(state, bbox);
5020
4.65k
    traceEnd(oldBaseMatrix, "end t-group");
5021
4.65k
  }
5022
5023
98.8k
  if (blendingColorSpace) {
5024
2.32k
    delete blendingColorSpace;
5025
2.32k
  }
5026
5027
98.8k
  out->endStream(strRef->getRef());
5028
5029
98.8k
  --formDepth;
5030
98.8k
}
5031
5032
0
void Gfx::takeContentStreamStack(Gfx *oldGfx) {
5033
0
  contentStreamStack->append(oldGfx->contentStreamStack);
5034
0
}
5035
5036
77.4k
void Gfx::endOfPage() {
5037
220k
  while (state->hasSaves()) {
5038
143k
    restoreState();
5039
143k
  }
5040
81.7k
  while (markedContentStack->getLength() > 0) {
5041
4.31k
    opEndMarkedContent(NULL, 0);
5042
4.31k
  }
5043
77.4k
}
5044
5045
//------------------------------------------------------------------------
5046
// in-line image operators
5047
//------------------------------------------------------------------------
5048
5049
68.0k
void Gfx::opBeginImage(Object args[], int numArgs) {
5050
68.0k
  Stream *str;
5051
68.0k
  GBool haveLength;
5052
68.0k
  int c1, c2, c3;
5053
5054
  // NB: this function is run even if ocState is false -- doImage() is
5055
  // responsible for skipping over the inline image data
5056
5057
  // build dict/stream
5058
68.0k
  str = buildImageStream(&haveLength);
5059
5060
  // display the image
5061
68.0k
  if (str) {
5062
67.2k
    if (!doImage(NULL, str, gTrue)) {
5063
20.2k
      delete str;
5064
  
5065
    // if we have the stream length, skip to end-of-stream and then
5066
    // skip 'EI' in the original stream
5067
46.9k
    } else if (haveLength) {
5068
      // NB: getUndecodedStream() returns the EmbedStream created by
5069
      // buildImageStream()
5070
19.0k
      while ((c1 = str->getUndecodedStream()->getChar()) != EOF) ;
5071
494
      delete str;
5072
494
      str = parser->getStream();
5073
494
      c1 = str->getChar();
5074
494
      c2 = str->getChar();
5075
494
      c3 = str->lookChar();
5076
199k
      while (!(c1 == 'E' && c2 == 'I' && Lexer::isSpace(c3)) && c3 != EOF) {
5077
198k
  c1 = c2;
5078
198k
  c2 = str->getChar();
5079
198k
  c3 = str->lookChar();
5080
198k
      }
5081
5082
    // else, look for the 'EI' tag and skip it
5083
46.4k
    } else {
5084
46.4k
      c1 = str->getUndecodedStream()->getChar();
5085
46.4k
      c2 = str->getUndecodedStream()->getChar();
5086
46.4k
      c3 = str->getUndecodedStream()->lookChar();
5087
2.24M
      while (!(c1 == 'E' && c2 == 'I' && Lexer::isSpace(c3)) && c3 != EOF) {
5088
2.20M
  c1 = c2;
5089
2.20M
  c2 = str->getUndecodedStream()->getChar();
5090
2.20M
  c3 = str->getUndecodedStream()->lookChar();
5091
2.20M
      }
5092
46.4k
      delete str;
5093
46.4k
    }
5094
67.2k
  }
5095
68.0k
}
5096
5097
68.0k
Stream *Gfx::buildImageStream(GBool *haveLength) {
5098
68.0k
  Object dict;
5099
68.0k
  Object obj, lengthObj;
5100
68.0k
  char *key;
5101
68.0k
  int length;
5102
68.0k
  Stream *str;
5103
5104
  // build dictionary
5105
68.0k
  dict.initDict(xref);
5106
68.0k
  getContentObj(&obj);
5107
1.52M
  while (!obj.isCmd("ID") && !obj.isEOF()) {
5108
1.46M
    if (!obj.isName()) {
5109
989k
      error(errSyntaxError, getPos(),
5110
989k
      "Inline image dictionary key must be a name object");
5111
989k
      obj.free();
5112
989k
    } else {
5113
471k
      key = copyString(obj.getName());
5114
471k
      obj.free();
5115
471k
      getContentObj(&obj);
5116
471k
      if (obj.isEOF()) {
5117
96
  gfree(key);
5118
96
  break;
5119
96
      }
5120
471k
      if (obj.isError()) {
5121
2.99k
  gfree(key);
5122
2.99k
  obj.free();
5123
468k
      } else {
5124
468k
  dict.dictAdd(key, &obj);
5125
468k
      }
5126
471k
    }
5127
1.46M
    getContentObj(&obj);
5128
1.46M
  }
5129
68.0k
  if (obj.isEOF()) {
5130
739
    error(errSyntaxError, getPos(), "End of file in inline image");
5131
739
    obj.free();
5132
739
    dict.free();
5133
739
    return NULL;
5134
739
  }
5135
67.3k
  obj.free();
5136
5137
  // check for length field
5138
67.3k
  length = 0;
5139
67.3k
  *haveLength = gFalse;
5140
67.3k
  if (!dict.dictLookup("Length", &lengthObj)->isInt()) {
5141
66.8k
    lengthObj.free();
5142
66.8k
    dict.dictLookup("L", &lengthObj);
5143
66.8k
  }
5144
67.3k
  if (lengthObj.isInt()) {
5145
546
    length = lengthObj.getInt();
5146
546
    *haveLength = gTrue;
5147
546
  }
5148
67.3k
  lengthObj.free();
5149
5150
  // make stream
5151
67.3k
  if (!(str = parser->getStream())) {
5152
97
    error(errSyntaxError, getPos(), "Invalid inline image data");
5153
97
    dict.free();
5154
97
    return NULL;
5155
97
  }
5156
67.2k
  str = new EmbedStream(str, &dict, *haveLength, (GFileOffset)length);
5157
67.2k
  str = str->addFilters(&dict);
5158
5159
67.2k
  return str;
5160
67.3k
}
5161
5162
20.2k
void Gfx::opImageData(Object args[], int numArgs) {
5163
20.2k
  error(errInternal, getPos(), "Got 'ID' operator");
5164
20.2k
}
5165
5166
37.3k
void Gfx::opEndImage(Object args[], int numArgs) {
5167
37.3k
  error(errInternal, getPos(), "Got 'EI' operator");
5168
37.3k
}
5169
5170
//------------------------------------------------------------------------
5171
// type 3 font operators
5172
//------------------------------------------------------------------------
5173
5174
10.3k
void Gfx::opSetCharWidth(Object args[], int numArgs) {
5175
10.3k
  out->type3D0(state, args[0].getNum(), args[1].getNum());
5176
10.3k
}
5177
5178
9.94k
void Gfx::opSetCacheDevice(Object args[], int numArgs) {
5179
9.94k
  state->setIgnoreColorOps(gTrue);
5180
9.94k
  out->type3D1(state, args[0].getNum(), args[1].getNum(),
5181
9.94k
         args[2].getNum(), args[3].getNum(),
5182
9.94k
         args[4].getNum(), args[5].getNum());
5183
9.94k
}
5184
5185
//------------------------------------------------------------------------
5186
// compatibility operators
5187
//------------------------------------------------------------------------
5188
5189
2.91k
void Gfx::opBeginIgnoreUndef(Object args[], int numArgs) {
5190
2.91k
  ++ignoreUndef;
5191
2.91k
}
5192
5193
2.40k
void Gfx::opEndIgnoreUndef(Object args[], int numArgs) {
5194
2.40k
  if (ignoreUndef > 0)
5195
1.48k
    --ignoreUndef;
5196
2.40k
}
5197
5198
//------------------------------------------------------------------------
5199
// marked content operators
5200
//------------------------------------------------------------------------
5201
5202
19.6k
void Gfx::opBeginMarkedContent(Object args[], int numArgs) {
5203
19.6k
  GfxMarkedContent *mc;
5204
19.6k
  Object obj;
5205
19.6k
  GBool ocStateNew;
5206
19.6k
  TextString *s;
5207
19.6k
  GfxMarkedContentKind mcKind;
5208
5209
19.6k
  if (printCommands) {
5210
0
    printf("  marked content: %s ", args[0].getName());
5211
0
    if (numArgs == 2) {
5212
0
      args[1].print(stdout);
5213
0
    }
5214
0
    printf("\n");
5215
0
    fflush(stdout);
5216
0
  }
5217
19.6k
  mcKind = gfxMCOther;
5218
19.6k
  if (args[0].isName("OC") && numArgs == 2 && args[1].isName() &&
5219
10.2k
      res->lookupPropertiesNF(args[1].getName(), &obj)) {
5220
5.83k
    if (doc->getOptionalContent()->evalOCObject(&obj, &ocStateNew)) {
5221
1.11k
      ocState &= ocStateNew;
5222
1.11k
    }
5223
5.83k
    obj.free();
5224
5.83k
    mcKind = gfxMCOptionalContent;
5225
13.8k
  } else if (numArgs == 2 && args[1].isDict()) {
5226
4.44k
    if (args[0].isName("Span")) {
5227
177
      if (args[1].dictLookup("ActualText", &obj)->isString()) {
5228
47
  s = new TextString(obj.getString());
5229
47
  out->beginActualText(state, s->getUnicode(), s->getLength());
5230
47
  delete s;
5231
47
  mcKind = gfxMCActualText;
5232
47
      }
5233
177
      obj.free();
5234
177
    }
5235
4.44k
    if (args[1].dictLookup("MCID", &obj)->isInt()) {
5236
3.30k
      out->beginStructureItem(args[0].getName(), obj.getInt(),
5237
3.30k
            args[1].getDict());
5238
3.30k
      if (mcKind == gfxMCActualText) {
5239
0
  mcKind = gfxMCStructureItemAndActualText;
5240
3.30k
      } else {
5241
3.30k
  mcKind = gfxMCStructureItem;
5242
3.30k
      }
5243
3.30k
    }
5244
4.44k
    obj.free();
5245
4.44k
  }
5246
5247
19.6k
  mc = new GfxMarkedContent(mcKind, ocState);
5248
19.6k
  markedContentStack->append(mc);
5249
19.6k
}
5250
5251
31.6k
void Gfx::opEndMarkedContent(Object args[], int numArgs) {
5252
31.6k
  GfxMarkedContent *mc;
5253
31.6k
  GfxMarkedContentKind mcKind;
5254
5255
31.6k
  if (markedContentStack->getLength() > 0) {
5256
19.1k
    mc = (GfxMarkedContent *)
5257
19.1k
             markedContentStack->del(markedContentStack->getLength() - 1);
5258
19.1k
    mcKind = mc->kind;
5259
19.1k
    delete mc;
5260
19.1k
    if (mcKind == gfxMCOptionalContent) {
5261
5.83k
      if (markedContentStack->getLength() > 0) {
5262
2.02k
  mc = (GfxMarkedContent *)
5263
2.02k
           markedContentStack->get(markedContentStack->getLength() - 1);
5264
2.02k
  ocState = mc->ocState;
5265
3.81k
      } else {
5266
3.81k
  ocState = gTrue;
5267
3.81k
      }
5268
13.3k
    } else if (mcKind == gfxMCActualText) {
5269
47
      out->endActualText(state);
5270
13.2k
    } else if (mcKind == gfxMCStructureItem) {
5271
2.84k
      out->endStructureItem();
5272
10.4k
    } else if (mcKind == gfxMCStructureItemAndActualText) {
5273
0
      out->endStructureItem();
5274
0
      out->endActualText(state);
5275
0
    }
5276
19.1k
  } else {
5277
12.4k
    error(errSyntaxWarning, getPos(), "Mismatched EMC operator");
5278
12.4k
  }
5279
31.6k
}
5280
5281
169
void Gfx::opMarkPoint(Object args[], int numArgs) {
5282
169
  if (printCommands) {
5283
0
    printf("  mark point: %s ", args[0].getName());
5284
0
    if (numArgs == 2)
5285
0
      args[1].print(stdout);
5286
0
    printf("\n");
5287
0
    fflush(stdout);
5288
0
  }
5289
169
}
5290
5291
//------------------------------------------------------------------------
5292
// misc
5293
//------------------------------------------------------------------------
5294
5295
void Gfx::drawAnnot(Object *strRef, AnnotBorderStyle *borderStyle,
5296
20.2k
        double xMin, double yMin, double xMax, double yMax) {
5297
20.2k
  Dict *dict, *resDict;
5298
20.2k
  Object str, matrixObj, bboxObj, resObj, obj1;
5299
20.2k
  double formXMin, formYMin, formXMax, formYMax;
5300
20.2k
  double x, y, sx, sy, tx, ty;
5301
20.2k
  double m[6], bbox[4];
5302
20.2k
  double *borderColor;
5303
20.2k
  GfxColor color;
5304
20.2k
  double *dash, *dash2;
5305
20.2k
  int dashLength;
5306
20.2k
  int i;
5307
5308
  // this function assumes that we are in the default user space,
5309
  // i.e., baseMatrix = ctm
5310
5311
  // if the bounding box has zero width or height, don't draw anything
5312
  // at all
5313
20.2k
  if (xMin == xMax || yMin == yMax) {
5314
922
    return;
5315
922
  }
5316
5317
  // draw the appearance stream (if there is one)
5318
19.3k
  strRef->fetch(xref, &str);
5319
19.3k
  if (str.isStream()) {
5320
5321
    // get stream dict
5322
5.74k
    dict = str.streamGetDict();
5323
5324
    // get the form bounding box
5325
5.74k
    dict->lookup("BBox", &bboxObj);
5326
5.74k
    if (!bboxObj.isArray() || bboxObj.arrayGetLength() != 4) {
5327
426
      error(errSyntaxError, getPos(), "Bad form bounding box");
5328
426
      bboxObj.free();
5329
426
      str.free();
5330
426
      return;
5331
426
    }
5332
26.5k
    for (i = 0; i < 4; ++i) {
5333
21.2k
      bboxObj.arrayGet(i, &obj1);
5334
21.2k
      if (obj1.isNum()) {
5335
20.3k
  bbox[i] = obj1.getNum();
5336
20.3k
      } else {
5337
913
  bbox[i] = 0;
5338
913
      }
5339
21.2k
      obj1.free();
5340
21.2k
    }
5341
5.31k
    bboxObj.free();
5342
5343
    // get the form matrix
5344
5.31k
    dict->lookup("Matrix", &matrixObj);
5345
5.31k
    if (matrixObj.isArray() && matrixObj.arrayGetLength() == 6) {
5346
1.71k
      for (i = 0; i < 6; ++i) {
5347
1.47k
  matrixObj.arrayGet(i, &obj1);
5348
1.47k
  if (obj1.isNum()) {
5349
1.43k
    m[i] = obj1.getNum();
5350
1.43k
  } else {
5351
38
    m[i] = 0;
5352
38
  }
5353
1.47k
  obj1.free();
5354
1.47k
      }
5355
5.07k
    } else {
5356
5.07k
      m[0] = 1; m[1] = 0;
5357
5.07k
      m[2] = 0; m[3] = 1;
5358
5.07k
      m[4] = 0; m[5] = 0;
5359
5.07k
    }
5360
5.31k
    matrixObj.free();
5361
5362
    // transform the four corners of the form bbox to default user
5363
    // space, and construct the transformed bbox
5364
5.31k
    x = bbox[0] * m[0] + bbox[1] * m[2] + m[4];
5365
5.31k
    y = bbox[0] * m[1] + bbox[1] * m[3] + m[5];
5366
5.31k
    formXMin = formXMax = x;
5367
5.31k
    formYMin = formYMax = y;
5368
5.31k
    x = bbox[0] * m[0] + bbox[3] * m[2] + m[4];
5369
5.31k
    y = bbox[0] * m[1] + bbox[3] * m[3] + m[5];
5370
5.31k
    if (x < formXMin) {
5371
33
      formXMin = x;
5372
5.28k
    } else if (x > formXMax) {
5373
21
      formXMax = x;
5374
21
    }
5375
5.31k
    if (y < formYMin) {
5376
92
      formYMin = y;
5377
5.22k
    } else if (y > formYMax) {
5378
5.01k
      formYMax = y;
5379
5.01k
    }
5380
5.31k
    x = bbox[2] * m[0] + bbox[1] * m[2] + m[4];
5381
5.31k
    y = bbox[2] * m[1] + bbox[1] * m[3] + m[5];
5382
5.31k
    if (x < formXMin) {
5383
36
      formXMin = x;
5384
5.28k
    } else if (x > formXMax) {
5385
5.09k
      formXMax = x;
5386
5.09k
    }
5387
5.31k
    if (y < formYMin) {
5388
7
      formYMin = y;
5389
5.30k
    } else if (y > formYMax) {
5390
32
      formYMax = y;
5391
32
    }
5392
5.31k
    x = bbox[2] * m[0] + bbox[3] * m[2] + m[4];
5393
5.31k
    y = bbox[2] * m[1] + bbox[3] * m[3] + m[5];
5394
5.31k
    if (x < formXMin) {
5395
9
      formXMin = x;
5396
5.30k
    } else if (x > formXMax) {
5397
16
      formXMax = x;
5398
16
    }
5399
5.31k
    if (y < formYMin) {
5400
6
      formYMin = y;
5401
5.31k
    } else if (y > formYMax) {
5402
8
      formYMax = y;
5403
8
    }
5404
5405
    // construct a mapping matrix, [sx 0  0], which maps the transformed
5406
    //                             [0  sy 0]
5407
    //                             [tx ty 1]
5408
    // bbox to the annotation rectangle
5409
5.31k
    if (formXMax - formXMin < 1e-8) {
5410
      // this shouldn't happen
5411
150
      sx = 1;
5412
5.16k
    } else {
5413
5.16k
      sx = (xMax - xMin) / (formXMax - formXMin);
5414
5.16k
    }
5415
5.31k
    if (formYMax - formYMin < 1e-8) {
5416
      // this shouldn't happen
5417
183
      sy = 1;
5418
5.13k
    } else {
5419
5.13k
      sy = (yMax - yMin) / (formYMax - formYMin);
5420
5.13k
    }
5421
5.31k
    tx = -formXMin * sx + xMin;
5422
5.31k
    ty = -formYMin * sy + yMin;
5423
5424
    // the final transform matrix is (form matrix) * (mapping matrix)
5425
5.31k
    m[0] *= sx;
5426
5.31k
    m[1] *= sy;
5427
5.31k
    m[2] *= sx;
5428
5.31k
    m[3] *= sy;
5429
5.31k
    m[4] = m[4] * sx + tx;
5430
5.31k
    m[5] = m[5] * sy + ty;
5431
5432
    // get the resources
5433
5.31k
    dict->lookup("Resources", &resObj);
5434
5.31k
    resDict = resObj.isDict() ? resObj.getDict() : (Dict *)NULL;
5435
5436
    // draw it
5437
5.31k
    drawForm(strRef, resDict, m, bbox);
5438
5439
5.31k
    resObj.free();
5440
5.31k
  }
5441
18.8k
  str.free();
5442
5443
  // draw the border
5444
18.8k
  if (borderStyle && borderStyle->getWidth() > 0 &&
5445
3.97k
      borderStyle->getNumColorComps() > 0) {
5446
3.97k
    borderColor = borderStyle->getColor();
5447
3.97k
    switch (borderStyle->getNumColorComps()) {
5448
61
    case 1:
5449
61
      if (state->getStrokeColorSpace()->getMode() != csDeviceGray) {
5450
1
  state->setStrokePattern(NULL);
5451
1
  state->setStrokeColorSpace(GfxColorSpace::create(csDeviceGray));
5452
1
  out->updateStrokeColorSpace(state);
5453
1
      }
5454
61
      break;
5455
3.76k
    case 3:
5456
3.76k
      if (state->getStrokeColorSpace()->getMode() != csDeviceRGB) {
5457
1.86k
  state->setStrokePattern(NULL);
5458
1.86k
  state->setStrokeColorSpace(GfxColorSpace::create(csDeviceRGB));
5459
1.86k
  out->updateStrokeColorSpace(state);
5460
1.86k
      }
5461
3.76k
      break;
5462
154
    case 4:
5463
154
      if (state->getStrokeColorSpace()->getMode() != csDeviceCMYK) {
5464
100
  state->setStrokePattern(NULL);
5465
100
  state->setStrokeColorSpace(GfxColorSpace::create(csDeviceCMYK));
5466
100
  out->updateStrokeColorSpace(state);
5467
100
      }
5468
154
      break;
5469
3.97k
    }
5470
3.97k
    color.c[0] = dblToCol(borderColor[0]);
5471
3.97k
    color.c[1] = dblToCol(borderColor[1]);
5472
3.97k
    color.c[2] = dblToCol(borderColor[2]);
5473
3.97k
    color.c[3] = dblToCol(borderColor[3]);
5474
3.97k
    state->setStrokeColor(&color);
5475
3.97k
    out->updateStrokeColor(state);
5476
3.97k
    state->setLineWidth(borderStyle->getWidth());
5477
3.97k
    out->updateLineWidth(state);
5478
3.97k
    borderStyle->getDash(&dash, &dashLength);
5479
3.97k
    if (borderStyle->getType() == annotBorderDashed && dashLength > 0) {
5480
51
      dash2 = (double *)gmallocn(dashLength, sizeof(double));
5481
51
      memcpy(dash2, dash, dashLength * sizeof(double));
5482
51
      state->setLineDash(dash2, dashLength, 0);
5483
51
      out->updateLineDash(state);
5484
51
    }
5485
    //~ this doesn't currently handle the beveled and engraved styles
5486
3.97k
    state->clearPath();
5487
3.97k
    state->moveTo(xMin, yMin);
5488
3.97k
    state->lineTo(xMax, yMin);
5489
3.97k
    if (borderStyle->getType() != annotBorderUnderlined) {
5490
1.57k
      state->lineTo(xMax, yMax);
5491
1.57k
      state->lineTo(xMin, yMax);
5492
1.57k
      state->closePath();
5493
1.57k
    }
5494
3.97k
    out->stroke(state);
5495
3.97k
  }
5496
18.8k
}
5497
5498
557k
void Gfx::saveState() {
5499
557k
  out->saveState(state);
5500
557k
  state = state->save();
5501
557k
}
5502
5503
689k
void Gfx::restoreState() {
5504
689k
  state = state->restore();
5505
689k
  out->restoreState(state);
5506
689k
}
5507
5508
// Create a new state stack, and initialize it with a copy of the
5509
// current state.
5510
3.35M
GfxState *Gfx::saveStateStack() {
5511
3.35M
  GfxState *oldState;
5512
5513
3.35M
  out->saveState(state);
5514
3.35M
  oldState = state;
5515
3.35M
  state = state->copy(gTrue);
5516
3.35M
  return oldState;
5517
3.35M
}
5518
5519
// Switch back to the previous state stack.
5520
3.35M
void Gfx::restoreStateStack(GfxState *oldState) {
5521
3.41M
  while (state->hasSaves()) {
5522
50.9k
    restoreState();
5523
50.9k
  }
5524
3.35M
  delete state;
5525
3.35M
  state = oldState;
5526
3.35M
  out->restoreState(state);
5527
3.35M
}
5528
5529
2.81M
void Gfx::pushResources(Dict *resDict) {
5530
2.81M
  res = new GfxResources(xref, resDict, res);
5531
2.81M
}
5532
5533
2.96M
void Gfx::popResources() {
5534
2.96M
  GfxResources *resPtr;
5535
5536
2.96M
  resPtr = res->getNext();
5537
2.96M
  delete res;
5538
2.96M
  res = resPtr;
5539
2.96M
}