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/SplashOutputDev.cc
Line
Count
Source
1
//========================================================================
2
//
3
// SplashOutputDev.cc
4
//
5
// Copyright 2003-2013 Glyph & Cog, LLC
6
//
7
//========================================================================
8
9
#include <aconf.h>
10
11
#include <string.h>
12
#include <math.h>
13
#include <limits.h>
14
#include "gmempp.h"
15
#include "gfile.h"
16
#include "Trace.h"
17
#include "GlobalParams.h"
18
#include "Error.h"
19
#include "Object.h"
20
#include "Gfx.h"
21
#include "GfxFont.h"
22
#include "ShadingImage.h"
23
#include "Link.h"
24
#include "CharCodeToUnicode.h"
25
#include "FontEncodingTables.h"
26
#include "BuiltinFont.h"
27
#include "BuiltinFontTables.h"
28
#include "FoFiTrueType.h"
29
#include "FoFiType1C.h"
30
#include "JPXStream.h"
31
#include "SplashBitmap.h"
32
#include "SplashGlyphBitmap.h"
33
#include "SplashPattern.h"
34
#include "SplashScreen.h"
35
#include "SplashPath.h"
36
#include "SplashState.h"
37
#include "SplashErrorCodes.h"
38
#include "SplashFontEngine.h"
39
#include "SplashFont.h"
40
#include "SplashFontFile.h"
41
#include "SplashFontFileID.h"
42
#include "Splash.h"
43
#include "SplashOutputDev.h"
44
45
#ifdef VMS
46
#if (__VMS_VER < 70000000)
47
extern "C" int unlink(char *filename);
48
#endif
49
#endif
50
51
//------------------------------------------------------------------------
52
53
// max tile size (used in tilingPatternFill())
54
// - Adobe uses a resolution-independent threshold here, of 6M sq pts
55
// - xpdf uses a resolution-dependent max, but with different values
56
//   on 32-bit and 64-bit systems
57
#if SplashBitmapRowSizeMax == INT_MAX
58
#  define maxTileSize 200000000
59
#else
60
1.04k
#  define maxTileSize 2000000000
61
#endif
62
63
//------------------------------------------------------------------------
64
65
// Type 3 font cache size parameters
66
10.8k
#define type3FontCacheAssoc   8
67
10.8k
#define type3FontCacheMaxSets 8
68
17.3k
#define type3FontCacheSize    (128*1024)
69
70
// Map StrokeAdjustMode (from GlobalParams) to SplashStrokeAdjustMode
71
// (for Splash).
72
static SplashStrokeAdjustMode mapStrokeAdjustMode[3] = {
73
  splashStrokeAdjustOff,
74
  splashStrokeAdjustNormal,
75
  splashStrokeAdjustCAD
76
};
77
78
//------------------------------------------------------------------------
79
80
// Divide a 16-bit value (in [0, 255*255]) by 255, returning an 8-bit result.
81
1.06k
static inline Guchar div255(int x) {
82
1.06k
  return (Guchar)((x + (x >> 8) + 0x80) >> 8);
83
1.06k
}
84
85
// Clip x to lie in [0, 255].
86
349
static inline Guchar clip255(int x) {
87
349
  return x < 0 ? 0 : x > 255 ? 255 : (Guchar)x;
88
349
}
89
90
91
//------------------------------------------------------------------------
92
// Blend functions
93
//------------------------------------------------------------------------
94
95
static void splashOutBlendMultiply(SplashColorPtr src, SplashColorPtr dest,
96
161
           SplashColorPtr blend, SplashColorMode cm) {
97
161
  int i;
98
99
644
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
100
483
    blend[i] = (Guchar)((dest[i] * src[i]) / 255);
101
483
  }
102
161
}
103
104
static void splashOutBlendScreen(SplashColorPtr src, SplashColorPtr dest,
105
91
         SplashColorPtr blend, SplashColorMode cm) {
106
91
  int i;
107
108
364
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
109
273
    blend[i] = (Guchar)(dest[i] + src[i] - (dest[i] * src[i]) / 255);
110
273
  }
111
91
}
112
113
// note: this is the same as HardLight, with src/dest reversed
114
static void splashOutBlendOverlay(SplashColorPtr src, SplashColorPtr dest,
115
95
          SplashColorPtr blend, SplashColorMode cm) {
116
95
  int i;
117
118
380
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
119
    // the spec says "if Cs <= 0.5" -- note that 0x80 is 128/255=0.5020
120
285
    blend[i] = dest[i] < 0x80
121
285
                 ? (Guchar)((src[i] * 2 * dest[i]) / 255)
122
285
                 : (Guchar)(255 - 2 * ((255 - src[i]) * (255 - dest[i])) / 255);
123
285
  }
124
95
}
125
126
static void splashOutBlendDarken(SplashColorPtr src, SplashColorPtr dest,
127
107
         SplashColorPtr blend, SplashColorMode cm) {
128
107
  int i;
129
130
428
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
131
321
    blend[i] = dest[i] < src[i] ? dest[i] : src[i];
132
321
  }
133
107
}
134
135
static void splashOutBlendLighten(SplashColorPtr src, SplashColorPtr dest,
136
0
          SplashColorPtr blend, SplashColorMode cm) {
137
0
  int i;
138
139
0
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
140
0
    blend[i] = dest[i] > src[i] ? dest[i] : src[i];
141
0
  }
142
0
}
143
144
static void splashOutBlendColorDodge(SplashColorPtr src, SplashColorPtr dest,
145
             SplashColorPtr blend,
146
129
             SplashColorMode cm) {
147
129
  int i;
148
149
516
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
150
387
    if (dest[i] == 0) {
151
102
      blend[i] = 0;
152
285
    } else if (dest[i] >= 255 - src[i]) {
153
197
      blend[i] = 255;
154
197
    } else {
155
88
      blend[i] = (Guchar)((dest[i] * 255) / (255 - src[i]));
156
88
    }
157
387
  }
158
129
}
159
160
static void splashOutBlendColorBurn(SplashColorPtr src, SplashColorPtr dest,
161
0
            SplashColorPtr blend, SplashColorMode cm) {
162
0
  int i;
163
164
0
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
165
0
    if (dest[i] == 255) {
166
0
      blend[i] = 255;
167
0
    } else if (255 - dest[i] >= src[i]) {
168
0
      blend[i] = 0;
169
0
    } else {
170
0
      blend[i] = (Guchar)(255 - (((255 - dest[i]) * 255) / src[i]));
171
0
    }
172
0
  }
173
0
}
174
175
static void splashOutBlendHardLight(SplashColorPtr src, SplashColorPtr dest,
176
38
            SplashColorPtr blend, SplashColorMode cm) {
177
38
  int i;
178
179
152
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
180
    // the spec says "if Cs <= 0.5" -- note that 0x80 is 128/255=0.5020
181
114
    blend[i] = src[i] < 0x80
182
114
                 ? (Guchar)((dest[i] * 2 * src[i]) / 255)
183
114
                 : (Guchar)(255 - 2 * ((255 - dest[i]) * (255 - src[i])) / 255);
184
114
  }
185
38
}
186
187
static void splashOutBlendSoftLight(SplashColorPtr src, SplashColorPtr dest,
188
26
            SplashColorPtr blend, SplashColorMode cm) {
189
26
  int i, x;
190
191
104
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
192
    // the spec says "if Cs <= 0.5" -- note that 0x80 is 128/255=0.5020
193
78
    if (src[i] < 0x80) {
194
53
      blend[i] = (Guchar)(dest[i] -
195
53
        (255 - 2 * src[i]) * dest[i] * (255 - dest[i]) /
196
53
          (255 * 255));
197
53
    } else {
198
      // the spec says "if Cb <= 0.25" -- note that 0x40 is 64/255=0.2510
199
25
      if (dest[i] < 0x40) {
200
6
  x = (((((16 * dest[i] - 12 * 255) * dest[i]) / 255)
201
6
        + 4 * 255) * dest[i]) / 255;
202
19
      } else {
203
19
  x = (int)sqrt(255.0 * dest[i]);
204
19
      }
205
25
      blend[i] = (Guchar)(dest[i] + (2 * src[i] - 255) * (x - dest[i]) / 255);
206
25
    }
207
78
  }
208
26
}
209
210
static void splashOutBlendDifference(SplashColorPtr src, SplashColorPtr dest,
211
             SplashColorPtr blend,
212
121
             SplashColorMode cm) {
213
121
  int i;
214
215
484
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
216
363
    blend[i] = dest[i] < src[i] ? (Guchar)(src[i] - dest[i])
217
363
              : (Guchar)(dest[i] - src[i]);
218
363
  }
219
121
}
220
221
static void splashOutBlendExclusion(SplashColorPtr src, SplashColorPtr dest,
222
42
            SplashColorPtr blend, SplashColorMode cm) {
223
42
  int i;
224
225
168
  for (i = 0; i < splashColorModeNComps[cm]; ++i) {
226
126
    blend[i] = (Guchar)(dest[i] + src[i] - (2 * dest[i] * src[i]) / 255);
227
126
  }
228
42
}
229
230
2.90k
static int getLum(int r, int g, int b) {
231
2.90k
  return (int)(0.3 * r + 0.59 * g + 0.11 * b);
232
2.90k
}
233
234
789
static int getSat(int r, int g, int b) {
235
789
  int rgbMin, rgbMax;
236
237
789
  rgbMin = rgbMax = r;
238
789
  if (g < rgbMin) {
239
117
    rgbMin = g;
240
672
  } else if (g > rgbMax) {
241
250
    rgbMax = g;
242
250
  }
243
789
  if (b < rgbMin) {
244
161
    rgbMin = b;
245
628
  } else if (b > rgbMax) {
246
142
    rgbMax = b;
247
142
  }
248
789
  return rgbMax - rgbMin;
249
789
}
250
251
static void clipColor(int rIn, int gIn, int bIn,
252
967
          Guchar *rOut, Guchar *gOut, Guchar *bOut) {
253
967
  int lum, rgbMin, rgbMax, r, g, b;
254
255
967
  lum = getLum(rIn, gIn, bIn);
256
967
  rgbMin = rgbMax = rIn;
257
967
  if (gIn < rgbMin) {
258
116
    rgbMin = gIn;
259
851
  } else if (gIn > rgbMax) {
260
297
    rgbMax = gIn;
261
297
  }
262
967
  if (bIn < rgbMin) {
263
278
    rgbMin = bIn;
264
689
  } else if (bIn > rgbMax) {
265
135
    rgbMax = bIn;
266
135
  }
267
967
  r = rIn;
268
967
  g = gIn;
269
967
  b = bIn;
270
967
  if (rgbMin < 0) {
271
22
    r = lum + ((r - lum) * lum) / (lum - rgbMin);
272
22
    g = lum + ((g - lum) * lum) / (lum - rgbMin);
273
22
    b = lum + ((b - lum) * lum) / (lum - rgbMin);
274
22
  }
275
967
  if (rgbMax > 255) {
276
8
    r = lum + ((r - lum) * (255 - lum)) / (rgbMax - lum);
277
8
    g = lum + ((g - lum) * (255 - lum)) / (rgbMax - lum);
278
8
    b = lum + ((b - lum) * (255 - lum)) / (rgbMax - lum);
279
8
  }
280
967
  *rOut = (Guchar)r;
281
967
  *gOut = (Guchar)g;
282
967
  *bOut = (Guchar)b;
283
967
}
284
285
static void setLum(Guchar rIn, Guchar gIn, Guchar bIn, int lum,
286
967
       Guchar *rOut, Guchar *gOut, Guchar *bOut) {
287
967
  int d;
288
289
967
  d = lum - getLum(rIn, gIn, bIn);
290
967
  clipColor(rIn + d, gIn + d, bIn + d, rOut, gOut, bOut);
291
967
}
292
293
static void setSat(Guchar rIn, Guchar gIn, Guchar bIn, int sat,
294
789
       Guchar *rOut, Guchar *gOut, Guchar *bOut) {
295
789
  int rgbMin, rgbMid, rgbMax;
296
789
  Guchar *minOut, *midOut, *maxOut;
297
298
789
  if (rIn < gIn) {
299
248
    rgbMin = rIn;  minOut = rOut;
300
248
    rgbMid = gIn;  midOut = gOut;
301
541
  } else {
302
541
    rgbMin = gIn;  minOut = gOut;
303
541
    rgbMid = rIn;  midOut = rOut;
304
541
  }
305
789
  if (bIn > rgbMid) {
306
141
    rgbMax = bIn;  maxOut = bOut;
307
648
  } else if (bIn > rgbMin) {
308
176
    rgbMax = rgbMid;  maxOut = midOut;
309
176
    rgbMid = bIn;     midOut = bOut;
310
472
  } else {
311
472
    rgbMax = rgbMid;  maxOut = midOut;
312
472
    rgbMid = rgbMin;  midOut = minOut;
313
472
    rgbMin = bIn;     minOut = bOut;
314
472
  }
315
789
  if (rgbMax > rgbMin) {
316
599
    *midOut = (Guchar)(((rgbMid - rgbMin) * sat) / (rgbMax - rgbMin));
317
599
    *maxOut = (Guchar)sat;
318
599
  } else {
319
190
    *midOut = *maxOut = 0;
320
190
  }
321
789
  *minOut = 0;
322
789
}
323
324
static void splashOutBlendHue(SplashColorPtr src, SplashColorPtr dest,
325
335
            SplashColorPtr blend, SplashColorMode cm) {
326
335
  Guchar r0, g0, b0;
327
328
335
  switch (cm) {
329
0
  case splashModeMono1:
330
0
  case splashModeMono8:
331
0
    blend[0] = dest[0];
332
0
    break;
333
335
  case splashModeRGB8:
334
335
  case splashModeBGR8:
335
335
    setSat(src[0], src[1], src[2], getSat(dest[0], dest[1], dest[2]),
336
335
     &r0, &g0, &b0);
337
335
    setLum(r0, g0, b0, getLum(dest[0], dest[1], dest[2]),
338
335
     &blend[0], &blend[1], &blend[2]);
339
335
    break;
340
0
#if SPLASH_CMYK
341
0
  case splashModeCMYK8:
342
    // NB: inputs have already been converted to additive mode
343
0
    setSat(src[0], src[1], src[2], getSat(dest[0], dest[1], dest[2]),
344
0
     &r0, &g0, &b0);
345
0
    setLum(r0, g0, b0, getLum(dest[0], dest[1], dest[2]),
346
0
     &blend[0], &blend[1], &blend[2]);
347
0
    blend[3] = dest[3];
348
0
    break;
349
335
#endif
350
335
  }
351
335
}
352
353
static void splashOutBlendSaturation(SplashColorPtr src, SplashColorPtr dest,
354
             SplashColorPtr blend,
355
454
             SplashColorMode cm) {
356
454
  Guchar r0, g0, b0;
357
358
454
  switch (cm) {
359
0
  case splashModeMono1:
360
0
  case splashModeMono8:
361
0
    blend[0] = dest[0];
362
0
    break;
363
454
  case splashModeRGB8:
364
454
  case splashModeBGR8:
365
454
    setSat(dest[0], dest[1], dest[2], getSat(src[0], src[1], src[2]),
366
454
     &r0, &g0, &b0);
367
454
    setLum(r0, g0, b0, getLum(dest[0], dest[1], dest[2]),
368
454
     &blend[0], &blend[1], &blend[2]);
369
454
    break;
370
0
#if SPLASH_CMYK
371
0
  case splashModeCMYK8:
372
    // NB: inputs have already been converted to additive mode
373
0
    setSat(dest[0], dest[1], dest[2], getSat(src[0], src[1], src[2]),
374
0
     &r0, &g0, &b0);
375
0
    setLum(r0, g0, b0, getLum(dest[0], dest[1], dest[2]),
376
0
     &blend[0], &blend[1], &blend[2]);
377
0
    blend[3] = dest[3];
378
0
    break;
379
454
#endif
380
454
  }
381
454
}
382
383
static void splashOutBlendColor(SplashColorPtr src, SplashColorPtr dest,
384
92
        SplashColorPtr blend, SplashColorMode cm) {
385
386
92
  switch (cm) {
387
0
  case splashModeMono1:
388
0
  case splashModeMono8:
389
0
    blend[0] = dest[0];
390
0
    break;
391
92
  case splashModeRGB8:
392
92
  case splashModeBGR8:
393
92
    setLum(src[0], src[1], src[2], getLum(dest[0], dest[1], dest[2]),
394
92
     &blend[0], &blend[1], &blend[2]);
395
92
    break;
396
0
#if SPLASH_CMYK
397
0
  case splashModeCMYK8:
398
    // NB: inputs have already been converted to additive mode
399
0
    setLum(src[0], src[1], src[2], getLum(dest[0], dest[1], dest[2]),
400
0
     &blend[0], &blend[1], &blend[2]);
401
0
    blend[3] = dest[3];
402
0
    break;
403
92
#endif
404
92
  }
405
92
}
406
407
static void splashOutBlendLuminosity(SplashColorPtr src, SplashColorPtr dest,
408
             SplashColorPtr blend,
409
86
             SplashColorMode cm) {
410
411
86
  switch (cm) {
412
0
  case splashModeMono1:
413
0
  case splashModeMono8:
414
0
    blend[0] = dest[0];
415
0
    break;
416
86
  case splashModeRGB8:
417
86
  case splashModeBGR8:
418
86
    setLum(dest[0], dest[1], dest[2], getLum(src[0], src[1], src[2]),
419
86
     &blend[0], &blend[1], &blend[2]);
420
86
    break;
421
0
#if SPLASH_CMYK
422
0
  case splashModeCMYK8:
423
    // NB: inputs have already been converted to additive mode
424
0
    setLum(dest[0], dest[1], dest[2], getLum(src[0], src[1], src[2]),
425
0
     &blend[0], &blend[1], &blend[2]);
426
0
    blend[3] = src[3];
427
0
    break;
428
86
#endif
429
86
  }
430
86
}
431
432
// NB: This must match the GfxBlendMode enum defined in GfxState.h.
433
SplashBlendFunc splashOutBlendFuncs[] = {
434
  NULL,
435
  &splashOutBlendMultiply,
436
  &splashOutBlendScreen,
437
  &splashOutBlendOverlay,
438
  &splashOutBlendDarken,
439
  &splashOutBlendLighten,
440
  &splashOutBlendColorDodge,
441
  &splashOutBlendColorBurn,
442
  &splashOutBlendHardLight,
443
  &splashOutBlendSoftLight,
444
  &splashOutBlendDifference,
445
  &splashOutBlendExclusion,
446
  &splashOutBlendHue,
447
  &splashOutBlendSaturation,
448
  &splashOutBlendColor,
449
  &splashOutBlendLuminosity
450
};
451
452
453
//------------------------------------------------------------------------
454
// SplashOutFontFileID
455
//------------------------------------------------------------------------
456
457
class SplashOutFontFileID: public SplashFontFileID {
458
public:
459
460
88.8k
  SplashOutFontFileID(Ref *rA) {
461
88.8k
    r = *rA;
462
88.8k
    substIdx = -1;
463
88.8k
    oblique = 0;
464
88.8k
  }
465
466
0
  ~SplashOutFontFileID() {}
467
468
43.2k
  GBool matches(SplashFontFileID *id) {
469
43.2k
    return ((SplashOutFontFileID *)id)->r.num == r.num &&
470
38.1k
           ((SplashOutFontFileID *)id)->r.gen == r.gen;
471
43.2k
  }
472
473
0
  void setOblique(double obliqueA) { oblique = obliqueA; }
474
27.0k
  double getOblique() { return oblique; }
475
0
  void setSubstIdx(int substIdxA) { substIdx = substIdxA; }
476
27.0k
  int getSubstIdx() { return substIdx; }
477
478
private:
479
480
  Ref r;
481
  double oblique;
482
  int substIdx;
483
};
484
485
//------------------------------------------------------------------------
486
// T3FontCache
487
//------------------------------------------------------------------------
488
489
struct T3FontCacheTag {
490
  Gushort code;
491
  Gushort mru;      // valid bit (0x8000) and MRU index
492
};
493
494
class T3FontCache {
495
public:
496
497
  T3FontCache(Ref *fontID, double m11A, double m12A,
498
        double m21A, double m22A,
499
        int glyphXA, int glyphYA, int glyphWA, int glyphHA,
500
        GBool validBBoxA, GBool aa);
501
  ~T3FontCache();
502
  GBool matches(Ref *idA, double m11A, double m12A,
503
    double m21A, double m22A)
504
3.32M
    { return fontID.num == idA->num && fontID.gen == idA->gen &&
505
3.32M
       m11 == m11A && m12 == m12A && m21 == m21A && m22 == m22A; }
506
507
  Ref fontID;     // PDF font ID
508
  double m11, m12, m21, m22;  // transform matrix
509
  int glyphX, glyphY;   // pixel offset of glyph bitmaps
510
  int glyphW, glyphH;   // size of glyph bitmaps, in pixels
511
  GBool validBBox;    // false if the bbox was [0 0 0 0]
512
  int glyphSize;    // size of glyph bitmaps, in bytes
513
  int cacheSets;    // number of sets in cache
514
  int cacheAssoc;   // cache associativity (glyphs per set)
515
  Guchar *cacheData;    // glyph pixmap cache
516
  T3FontCacheTag *cacheTags;  // cache tags, i.e., char codes
517
  int refCount;     // active reference count for this T3 font
518
};
519
520
T3FontCache::T3FontCache(Ref *fontIDA, double m11A, double m12A,
521
       double m21A, double m22A,
522
       int glyphXA, int glyphYA, int glyphWA, int glyphHA,
523
10.8k
       GBool validBBoxA, GBool aa) {
524
10.8k
  int i;
525
526
10.8k
  fontID = *fontIDA;
527
10.8k
  m11 = m11A;
528
10.8k
  m12 = m12A;
529
10.8k
  m21 = m21A;
530
10.8k
  m22 = m22A;
531
10.8k
  glyphX = glyphXA;
532
10.8k
  glyphY = glyphYA;
533
10.8k
  glyphW = glyphWA;
534
10.8k
  glyphH = glyphHA;
535
10.8k
  validBBox = validBBoxA;
536
  // sanity check for excessively large glyphs (which most likely
537
  // indicate an incorrect BBox)
538
10.8k
  i = glyphW * glyphH;
539
10.8k
  if (i > 100000 || glyphW > INT_MAX / glyphH || glyphW <= 0 || glyphH <= 0) {
540
3.28k
    glyphW = glyphH = 100;
541
3.28k
    validBBox = gFalse;
542
3.28k
  }
543
10.8k
  if (aa) {
544
10.8k
    glyphSize = glyphW * glyphH;
545
10.8k
  } else {
546
0
    glyphSize = ((glyphW + 7) >> 3) * glyphH;
547
0
  }
548
10.8k
  cacheAssoc = type3FontCacheAssoc;
549
10.8k
  for (cacheSets = type3FontCacheMaxSets;
550
20.6k
       cacheSets > 1 &&
551
17.3k
   cacheSets * cacheAssoc * glyphSize > type3FontCacheSize;
552
10.8k
       cacheSets >>= 1) ;
553
10.8k
  cacheData = (Guchar *)gmallocn(cacheSets * cacheAssoc, glyphSize);
554
10.8k
  cacheTags = (T3FontCacheTag *)gmallocn(cacheSets * cacheAssoc,
555
10.8k
           sizeof(T3FontCacheTag));
556
518k
  for (i = 0; i < cacheSets * cacheAssoc; ++i) {
557
507k
    cacheTags[i].mru = (Gushort)(i & (cacheAssoc - 1));
558
507k
  }
559
10.8k
  refCount = 0;
560
10.8k
}
561
562
10.8k
T3FontCache::~T3FontCache() {
563
10.8k
  gfree(cacheData);
564
10.8k
  gfree(cacheTags);
565
10.8k
}
566
567
struct T3GlyphStack {
568
  Gushort code;     // character code
569
570
  GBool haveDx;     // set after seeing a d0/d1 operator
571
  GBool doNotCache;   // set if we see a gsave/grestore before
572
        //   the d0/d1
573
574
  //----- cache info
575
  T3FontCache *cache;   // font cache for the current font
576
  T3FontCacheTag *cacheTag; // pointer to cache tag for the glyph
577
  Guchar *cacheData;    // pointer to cache data for the glyph
578
579
  //----- saved state
580
  SplashBitmap *origBitmap;
581
  Splash *origSplash;
582
  double origCTM4, origCTM5;
583
  SplashStrokeAdjustMode savedStrokeAdjust;
584
585
  T3GlyphStack *next;   // next object on stack
586
};
587
588
//------------------------------------------------------------------------
589
// SplashTransparencyGroup
590
//------------------------------------------------------------------------
591
592
struct SplashTransparencyGroup {
593
  int tx, ty;     // translation coordinates
594
  SplashBitmap *tBitmap;  // bitmap for transparency group
595
  SplashAlphaBitmap *alpha0Bitmap;
596
  SplashAlphaBitmap *shapeBitmap;
597
  GfxColorSpace *blendingColorSpace;
598
  GBool isolated;
599
  GBool inKnockoutGroup;  // true if this, or any containing
600
        //   group, is a knockout group
601
  GBool inSoftMask;   // true if this, or any containing
602
        //   group, is a soft mask
603
604
  //----- modified region in tBitmap
605
  int modXMin, modYMin, modXMax, modYMax;
606
607
  //----- saved state
608
  SplashBitmap *origBitmap;
609
  Splash *origSplash;
610
611
  SplashTransparencyGroup *next;
612
};
613
614
//------------------------------------------------------------------------
615
// SplashOutputDev
616
//------------------------------------------------------------------------
617
618
SplashOutputDev::SplashOutputDev(SplashColorMode colorModeA,
619
         int bitmapRowPadA,
620
         GBool reverseVideoA,
621
         SplashColorPtr paperColorA,
622
         GBool bitmapTopDownA,
623
18.4k
         GBool allowAntialiasA) {
624
18.4k
  colorMode = colorModeA;
625
18.4k
  bitmapRowPad = bitmapRowPadA;
626
18.4k
  bitmapTopDown = bitmapTopDownA;
627
18.4k
  bitmapUpsideDown = gFalse;
628
18.4k
  noComposite = gFalse;
629
18.4k
  allowAntialias = allowAntialiasA;
630
18.4k
  vectorAntialias = allowAntialias &&
631
18.4k
          globalParams->getVectorAntialias() &&
632
18.4k
          colorMode != splashModeMono1;
633
18.4k
  setupScreenParams(72.0, 72.0);
634
18.4k
  reverseVideo = reverseVideoA;
635
18.4k
  splashColorCopy(paperColor, paperColorA);
636
18.4k
  skipHorizText = gFalse;
637
18.4k
  skipRotatedText = gFalse;
638
639
18.4k
  xref = NULL;
640
641
18.4k
  bitmapMemCache = new SplashBitmapMemCache();
642
18.4k
  bitmap = new SplashBitmap(1, 1, bitmapRowPad, colorMode,
643
18.4k
          colorMode != splashModeMono1, bitmapTopDown,
644
18.4k
          bitmapMemCache);
645
18.4k
  splash = new Splash(bitmap, vectorAntialias, NULL, &screenParams);
646
18.4k
  splash->setMinLineWidth(globalParams->getMinLineWidth());
647
18.4k
  splash->setStrokeAdjust(
648
18.4k
     mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
649
18.4k
  splash->setEnablePathSimplification(
650
18.4k
     globalParams->getEnablePathSimplification());
651
18.4k
  splash->clear(paperColor, 0);
652
653
18.4k
  fontEngine = NULL;
654
655
18.4k
  nT3Fonts = 0;
656
18.4k
  t3GlyphStack = NULL;
657
658
18.4k
  tileCacheRef.num = -1;
659
18.4k
  tileCacheRef.gen = -1;
660
18.4k
  tileCacheBitmap = NULL;
661
18.4k
  tileCacheOverprintMaskBitmap = NULL;
662
663
18.4k
  font = NULL;
664
18.4k
  needFontUpdate = gFalse;
665
18.4k
  savedTextPath = NULL;
666
18.4k
  savedClipPath = NULL;
667
668
18.4k
  transpGroupStack = NULL;
669
670
18.4k
  nestCount = 0;
671
672
18.4k
  startPageCbk = NULL;
673
18.4k
  startPageCbkData = NULL;
674
18.4k
}
675
676
677
167k
void SplashOutputDev::setupScreenParams(double hDPI, double vDPI) {
678
167k
  screenParams.size = globalParams->getScreenSize();
679
167k
  screenParams.dotRadius = globalParams->getScreenDotRadius();
680
167k
  screenParams.gamma = (SplashCoord)globalParams->getScreenGamma();
681
167k
  screenParams.blackThreshold =
682
167k
      (SplashCoord)globalParams->getScreenBlackThreshold();
683
167k
  screenParams.whiteThreshold =
684
167k
      (SplashCoord)globalParams->getScreenWhiteThreshold();
685
167k
  switch (globalParams->getScreenType()) {
686
0
  case screenDispersed:
687
0
    screenParams.type = splashScreenDispersed;
688
0
    if (screenParams.size < 0) {
689
0
      screenParams.size = 4;
690
0
    }
691
0
    break;
692
0
  case screenClustered:
693
0
    screenParams.type = splashScreenClustered;
694
0
    if (screenParams.size < 0) {
695
0
      screenParams.size = 10;
696
0
    }
697
0
    break;
698
0
  case screenStochasticClustered:
699
0
    screenParams.type = splashScreenStochasticClustered;
700
0
    if (screenParams.size < 0) {
701
0
      screenParams.size = 64;
702
0
    }
703
0
    if (screenParams.dotRadius < 0) {
704
0
      screenParams.dotRadius = 2;
705
0
    }
706
0
    break;
707
167k
  case screenUnset:
708
167k
  default:
709
    // use clustered dithering for resolution >= 300 dpi
710
    // (compare to 299.9 to avoid floating point issues)
711
167k
    if (hDPI > 299.9 && vDPI > 299.9) {
712
3.05k
      screenParams.type = splashScreenStochasticClustered;
713
3.05k
      if (screenParams.size < 0) {
714
3.05k
  screenParams.size = 64;
715
3.05k
      }
716
3.05k
      if (screenParams.dotRadius < 0) {
717
3.05k
  screenParams.dotRadius = 2;
718
3.05k
      }
719
164k
    } else {
720
164k
      screenParams.type = splashScreenDispersed;
721
164k
      if (screenParams.size < 0) {
722
164k
  screenParams.size = 4;
723
164k
      }
724
164k
    }
725
167k
  }
726
167k
}
727
728
18.4k
SplashOutputDev::~SplashOutputDev() {
729
18.4k
  int i;
730
731
18.4k
  if (tileCacheBitmap) {
732
17
    delete tileCacheBitmap;
733
17
  }
734
18.4k
  gfree(tileCacheOverprintMaskBitmap);
735
20.3k
  for (i = 0; i < nT3Fonts; ++i) {
736
1.87k
    delete t3FontCache[i];
737
1.87k
  }
738
18.4k
  if (fontEngine) {
739
18.4k
    delete fontEngine;
740
18.4k
  }
741
18.4k
  if (splash) {
742
18.4k
    delete splash;
743
18.4k
  }
744
18.4k
  if (bitmap) {
745
18.4k
    delete bitmap;
746
18.4k
  }
747
18.4k
  delete bitmapMemCache;
748
18.4k
  if (savedTextPath) {
749
0
    delete savedTextPath;
750
0
  }
751
18.4k
  if (savedClipPath) {
752
113
    delete savedClipPath;
753
113
  }
754
18.4k
}
755
756
18.4k
void SplashOutputDev::startDoc(XRef *xrefA) {
757
18.4k
  int i;
758
759
18.4k
  xref = xrefA;
760
18.4k
  if (fontEngine) {
761
0
    delete fontEngine;
762
0
  }
763
18.4k
  fontEngine = new SplashFontEngine(
764
18.4k
#if HAVE_FREETYPE_H
765
18.4k
            globalParams->getEnableFreeType(),
766
18.4k
            globalParams->getDisableFreeTypeHinting()
767
18.4k
              ? splashFTNoHinting : 0,
768
18.4k
#endif
769
18.4k
            allowAntialias &&
770
18.4k
              globalParams->getAntialias() &&
771
18.4k
              colorMode != splashModeMono1);
772
18.4k
  for (i = 0; i < nT3Fonts; ++i) {
773
0
    delete t3FontCache[i];
774
0
  }
775
18.4k
  nT3Fonts = 0;
776
18.4k
  if (tileCacheBitmap) {
777
0
    delete tileCacheBitmap;
778
0
    tileCacheBitmap = NULL;
779
0
  }
780
18.4k
  gfree(tileCacheOverprintMaskBitmap);
781
18.4k
  tileCacheRef.num = -1;
782
18.4k
  tileCacheRef.gen = -1;
783
18.4k
}
784
785
149k
void SplashOutputDev::startPage(int pageNum, GfxState *state) {
786
149k
  int w, h;
787
149k
  double *ctm;
788
149k
  SplashCoord mat[6];
789
149k
  SplashColor color;
790
791
149k
  if (state) {
792
149k
    setupScreenParams(state->getHDPI(), state->getVDPI());
793
149k
    w = (int)(state->getPageWidth() + 0.5);
794
149k
    if (w <= 0) {
795
149k
      w = 1;
796
149k
    }
797
149k
    h = (int)(state->getPageHeight() + 0.5);
798
149k
    if (h <= 0) {
799
149k
      h = 1;
800
149k
    }
801
149k
  } else {
802
0
    w = h = 1;
803
0
  }
804
149k
  if (splash) {
805
149k
    delete splash;
806
149k
    splash = NULL;
807
149k
  }
808
149k
  if (!bitmap || w != bitmap->getWidth() || h != bitmap->getHeight()) {
809
0
    if (bitmap) {
810
0
      delete bitmap;
811
0
      bitmap = NULL;
812
0
    }
813
0
    traceMessage("page bitmap");
814
0
    bitmap = new SplashBitmap(w, h, bitmapRowPad, colorMode,
815
0
            colorMode != splashModeMono1, bitmapTopDown,
816
0
            bitmapMemCache);
817
0
  }
818
149k
  splash = new Splash(bitmap, vectorAntialias, NULL, &screenParams);
819
149k
  splash->setMinLineWidth(globalParams->getMinLineWidth());
820
149k
  splash->setEnablePathSimplification(
821
149k
     globalParams->getEnablePathSimplification());
822
149k
  if (state) {
823
149k
    ctm = state->getCTM();
824
149k
    mat[0] = (SplashCoord)ctm[0];
825
149k
    mat[1] = (SplashCoord)ctm[1];
826
149k
    mat[2] = (SplashCoord)ctm[2];
827
149k
    mat[3] = (SplashCoord)ctm[3];
828
149k
    mat[4] = (SplashCoord)ctm[4];
829
149k
    mat[5] = (SplashCoord)ctm[5];
830
149k
    splash->setMatrix(mat);
831
149k
  }
832
149k
  switch (colorMode) {
833
0
  case splashModeMono1:
834
0
  case splashModeMono8:
835
0
    color[0] = 0;
836
0
    break;
837
149k
  case splashModeRGB8:
838
149k
  case splashModeBGR8:
839
149k
    color[0] = color[1] = color[2] = 0;
840
149k
    break;
841
0
#if SPLASH_CMYK
842
0
  case splashModeCMYK8:
843
0
    color[0] = color[1] = color[2] = color[3] = 0;
844
0
    break;
845
149k
#endif
846
149k
  }
847
149k
  splash->setStrokePattern(new SplashSolidColor(color));
848
149k
  splash->setFillPattern(new SplashSolidColor(color));
849
149k
  splash->setLineCap(splashLineCapButt);
850
149k
  splash->setLineJoin(splashLineJoinMiter);
851
149k
  splash->setLineDash(NULL, 0, 0);
852
149k
  splash->setMiterLimit(10);
853
149k
  splash->setFlatness(1);
854
  // the SA parameter supposedly defaults to false, but Acrobat
855
  // apparently hardwires it to true
856
149k
  splash->setStrokeAdjust(
857
149k
        mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
858
149k
  splash->clear(paperColor, 0);
859
149k
  reverseVideoInvertImages = globalParams->getReverseVideoInvertImages();
860
149k
  if (startPageCbk) {
861
0
    (*startPageCbk)(startPageCbkData);
862
0
  }
863
149k
}
864
865
148k
void SplashOutputDev::endPage() {
866
148k
  if (colorMode != splashModeMono1 && !noComposite) {
867
0
    splash->compositeBackground(paperColor);
868
0
  }
869
148k
}
870
871
3.91M
void SplashOutputDev::saveState(GfxState *state) {
872
3.91M
  splash->saveState();
873
3.91M
  if (t3GlyphStack && !t3GlyphStack->haveDx) {
874
2.94M
    t3GlyphStack->doNotCache = gTrue;
875
2.94M
    error(errSyntaxWarning, -1,
876
2.94M
    "Save (q) operator before d0/d1 in Type 3 glyph");
877
2.94M
  }
878
3.91M
}
879
880
4.04M
void SplashOutputDev::restoreState(GfxState *state) {
881
4.04M
  splash->restoreState();
882
4.04M
  needFontUpdate = gTrue;
883
4.04M
  if (t3GlyphStack && !t3GlyphStack->haveDx) {
884
2.95M
    t3GlyphStack->doNotCache = gTrue;
885
2.95M
    error(errSyntaxWarning, -1,
886
2.95M
    "Restore (Q) operator before d0/d1 in Type 3 glyph");
887
2.95M
  }
888
4.04M
}
889
890
149k
void SplashOutputDev::updateAll(GfxState *state) {
891
149k
  updateLineDash(state);
892
149k
  updateLineJoin(state);
893
149k
  updateLineCap(state);
894
149k
  updateLineWidth(state);
895
149k
  updateFlatness(state);
896
149k
  updateMiterLimit(state);
897
149k
  updateStrokeAdjust(state);
898
149k
  updateFillColor(state);
899
149k
  updateStrokeColor(state);
900
149k
  needFontUpdate = gTrue;
901
149k
}
902
903
void SplashOutputDev::updateCTM(GfxState *state, double m11, double m12,
904
        double m21, double m22,
905
3.52M
        double m31, double m32) {
906
3.52M
  double *ctm;
907
3.52M
  SplashCoord mat[6];
908
909
3.52M
  ctm = state->getCTM();
910
3.52M
  mat[0] = (SplashCoord)ctm[0];
911
3.52M
  mat[1] = (SplashCoord)ctm[1];
912
3.52M
  mat[2] = (SplashCoord)ctm[2];
913
3.52M
  mat[3] = (SplashCoord)ctm[3];
914
3.52M
  mat[4] = (SplashCoord)ctm[4];
915
3.52M
  mat[5] = (SplashCoord)ctm[5];
916
3.52M
  splash->setMatrix(mat);
917
3.52M
}
918
919
159k
void SplashOutputDev::updateLineDash(GfxState *state) {
920
159k
  double *dashPattern;
921
159k
  int dashLength;
922
159k
  double dashStart;
923
159k
  SplashCoord dash[20];
924
159k
  int i;
925
926
159k
  state->getLineDash(&dashPattern, &dashLength, &dashStart);
927
159k
  if (dashLength > 20) {
928
173
    dashLength = 20;
929
173
  }
930
166k
  for (i = 0; i < dashLength; ++i) {
931
6.60k
    dash[i] = (SplashCoord)dashPattern[i];
932
6.60k
    if (dash[i] < 0) {
933
454
      dash[i] = 0;
934
454
    }
935
6.60k
  }
936
159k
  splash->setLineDash(dash, dashLength, (SplashCoord)dashStart);
937
159k
}
938
939
252k
void SplashOutputDev::updateFlatness(GfxState *state) {
940
#if 0 // Acrobat ignores the flatness setting, and always renders curves
941
      // with a fairly small flatness value
942
  splash->setFlatness(state->getFlatness());
943
#endif
944
252k
}
945
946
161k
void SplashOutputDev::updateLineJoin(GfxState *state) {
947
161k
  splash->setLineJoin(state->getLineJoin());
948
161k
}
949
950
186k
void SplashOutputDev::updateLineCap(GfxState *state) {
951
186k
  splash->setLineCap(state->getLineCap());
952
186k
}
953
954
171k
void SplashOutputDev::updateMiterLimit(GfxState *state) {
955
171k
  splash->setMiterLimit(state->getMiterLimit());
956
171k
}
957
958
233k
void SplashOutputDev::updateLineWidth(GfxState *state) {
959
233k
  splash->setLineWidth(state->getLineWidth());
960
233k
}
961
962
152k
void SplashOutputDev::updateStrokeAdjust(GfxState *state) {
963
#if 0 // the SA parameter supposedly defaults to false, but Acrobat
964
      // apparently hardwires it to true
965
  if (state->getStrokeAdjust()) {
966
    if (globalParams->getStrokeAdjustMode() == strokeAdjustCAD) {
967
      splash->setStrokeAdjust(splashStrokeAdjustCAD);
968
    } else {
969
      splash->setStrokeAdjust(splashStrokeAdjustNormal);
970
    }
971
  } else {
972
    splash->setStrokeAdjust(splashStrokeAdjustOff);
973
  }
974
#endif
975
152k
}
976
977
978
316k
void SplashOutputDev::updateFillColor(GfxState *state) {
979
316k
  GfxGray gray;
980
316k
  GfxRGB rgb;
981
316k
#if SPLASH_CMYK
982
316k
  GfxCMYK cmyk;
983
316k
#endif
984
985
316k
  switch (colorMode) {
986
0
  case splashModeMono1:
987
285
  case splashModeMono8:
988
285
    state->getFillGray(&gray);
989
285
    splash->setFillPattern(getColor(gray));
990
285
    break;
991
315k
  case splashModeRGB8:
992
315k
  case splashModeBGR8:
993
315k
    state->getFillRGB(&rgb);
994
315k
    splash->setFillPattern(getColor(&rgb));
995
315k
    break;
996
0
#if SPLASH_CMYK
997
305
  case splashModeCMYK8:
998
305
    state->getFillCMYK(&cmyk);
999
305
    splash->setFillPattern(getColor(&cmyk));
1000
305
    break;
1001
316k
#endif
1002
316k
  }
1003
316k
}
1004
1005
253k
void SplashOutputDev::updateStrokeColor(GfxState *state) {
1006
253k
  GfxGray gray;
1007
253k
  GfxRGB rgb;
1008
253k
#if SPLASH_CMYK
1009
253k
  GfxCMYK cmyk;
1010
253k
#endif
1011
1012
253k
  switch (colorMode) {
1013
0
  case splashModeMono1:
1014
266
  case splashModeMono8:
1015
266
    state->getStrokeGray(&gray);
1016
266
    splash->setStrokePattern(getColor(gray));
1017
266
    break;
1018
253k
  case splashModeRGB8:
1019
253k
  case splashModeBGR8:
1020
253k
    state->getStrokeRGB(&rgb);
1021
253k
    splash->setStrokePattern(getColor(&rgb));
1022
253k
    break;
1023
0
#if SPLASH_CMYK
1024
343
  case splashModeCMYK8:
1025
343
    state->getStrokeCMYK(&cmyk);
1026
343
    splash->setStrokePattern(getColor(&cmyk));
1027
343
    break;
1028
253k
#endif
1029
253k
  }
1030
253k
}
1031
1032
1033
551
SplashPattern *SplashOutputDev::getColor(GfxGray gray) {
1034
551
  SplashColor color;
1035
1036
551
  getColor(gray, color);
1037
551
  return new SplashSolidColor(color);
1038
551
}
1039
1040
568k
SplashPattern *SplashOutputDev::getColor(GfxRGB *rgb) {
1041
568k
  SplashColor color;
1042
1043
568k
  getColor(rgb, color);
1044
568k
  return new SplashSolidColor(color);
1045
568k
}
1046
1047
#if SPLASH_CMYK
1048
648
SplashPattern *SplashOutputDev::getColor(GfxCMYK *cmyk) {
1049
648
  SplashColor color;
1050
1051
648
  getColor(cmyk, color);
1052
648
  return new SplashSolidColor(color);
1053
648
}
1054
#endif
1055
1056
1057
551
void SplashOutputDev::getColor(GfxGray gray, SplashColorPtr color) {
1058
551
  if (reverseVideo) {
1059
0
    gray = gfxColorComp1 - gray;
1060
0
  }
1061
551
  color[0] = colToByte(gray);
1062
551
}
1063
1064
568k
void SplashOutputDev::getColor(GfxRGB *rgb, SplashColorPtr color) {
1065
568k
  GfxColorComp r, g, b;
1066
1067
568k
  if (reverseVideo) {
1068
0
    r = gfxColorComp1 - rgb->r;
1069
0
    g = gfxColorComp1 - rgb->g;
1070
0
    b = gfxColorComp1 - rgb->b;
1071
568k
  } else {
1072
568k
    r = rgb->r;
1073
568k
    g = rgb->g;
1074
568k
    b = rgb->b;
1075
568k
  }
1076
568k
  color[0] = colToByte(r);
1077
568k
  color[1] = colToByte(g);
1078
568k
  color[2] = colToByte(b);
1079
568k
}
1080
1081
#if SPLASH_CMYK
1082
648
void SplashOutputDev::getColor(GfxCMYK *cmyk, SplashColorPtr color) {
1083
648
  color[0] = colToByte(cmyk->c);
1084
648
  color[1] = colToByte(cmyk->m);
1085
648
  color[2] = colToByte(cmyk->y);
1086
648
  color[3] = colToByte(cmyk->k);
1087
648
}
1088
#endif
1089
1090
1091
1092
void SplashOutputDev::setOverprintMask(GfxState *state,
1093
               GfxColorSpace *colorSpace,
1094
               GBool overprintFlag,
1095
               int overprintMode,
1096
883k
               GfxColor *singleColor) {
1097
883k
#if SPLASH_CMYK
1098
883k
  Guint mask;
1099
883k
  GfxCMYK cmyk;
1100
1101
  // Adobe ignores overprint in soft masks.
1102
883k
  if (overprintFlag &&
1103
54.3k
      !(transpGroupStack && transpGroupStack->inSoftMask) &&
1104
54.3k
      globalParams->getOverprintPreview()) {
1105
0
    mask = colorSpace->getOverprintMask();
1106
    // The OPM (overprintMode) setting is only relevant when the color
1107
    // space is DeviceCMYK or is "implicitly converted to DeviceCMYK".
1108
    // Per the PDF spec, this happens with ICCBased color spaces only
1109
    // if the profile matches the output device.
1110
0
    if (singleColor && overprintMode &&
1111
0
  colorSpace->getMode() == csDeviceCMYK) {
1112
0
      colorSpace->getCMYK(singleColor, &cmyk, state->getRenderingIntent());
1113
0
      if (cmyk.c == 0) {
1114
0
  mask &= ~1;
1115
0
      }
1116
0
      if (cmyk.m == 0) {
1117
0
  mask &= ~2;
1118
0
      }
1119
0
      if (cmyk.y == 0) {
1120
0
  mask &= ~4;
1121
0
      }
1122
0
      if (cmyk.k == 0) {
1123
0
  mask &= ~8;
1124
0
      }
1125
0
    }
1126
883k
  } else {
1127
883k
    mask = 0xffffffff;
1128
883k
  }
1129
883k
  splash->setOverprintMask(mask);
1130
883k
#endif
1131
1132
883k
}
1133
1134
6.57k
void SplashOutputDev::updateBlendMode(GfxState *state) {
1135
6.57k
  splash->setBlendFunc(splashOutBlendFuncs[state->getBlendMode()]);
1136
6.57k
}
1137
1138
8.46k
void SplashOutputDev::updateFillOpacity(GfxState *state) {
1139
8.46k
  splash->setFillAlpha((SplashCoord)state->getFillOpacity());
1140
8.46k
}
1141
1142
8.59k
void SplashOutputDev::updateStrokeOpacity(GfxState *state) {
1143
8.59k
  splash->setStrokeAlpha((SplashCoord)state->getStrokeOpacity());
1144
8.59k
}
1145
1146
4.85k
void SplashOutputDev::updateRenderingIntent(GfxState *state) {
1147
4.85k
  updateFillColor(state);
1148
4.85k
  updateStrokeColor(state);
1149
4.85k
}
1150
1151
928
void SplashOutputDev::updateTransfer(GfxState *state) {
1152
928
  Function **transfer;
1153
928
  Guchar red[256], green[256], blue[256], gray[256];
1154
928
  double x, y;
1155
928
  int i;
1156
1157
928
  transfer = state->getTransfer();
1158
928
  if (transfer[0] &&
1159
239
      transfer[0]->getInputSize() == 1 &&
1160
239
      transfer[0]->getOutputSize() == 1) {
1161
239
    if (transfer[1] &&
1162
11
  transfer[1]->getInputSize() == 1 &&
1163
11
  transfer[1]->getOutputSize() == 1 &&
1164
11
  transfer[2] &&
1165
11
  transfer[2]->getInputSize() == 1 &&
1166
11
  transfer[2]->getOutputSize() == 1 &&
1167
11
  transfer[3] &&
1168
11
  transfer[3]->getInputSize() == 1 &&
1169
11
  transfer[3]->getOutputSize() == 1) {
1170
2.82k
      for (i = 0; i < 256; ++i) {
1171
2.81k
  x = i / 255.0;
1172
2.81k
  transfer[0]->transform(&x, &y);
1173
2.81k
  red[i] = (Guchar)(y * 255.0 + 0.5);
1174
2.81k
  transfer[1]->transform(&x, &y);
1175
2.81k
  green[i] = (Guchar)(y * 255.0 + 0.5);
1176
2.81k
  transfer[2]->transform(&x, &y);
1177
2.81k
  blue[i] = (Guchar)(y * 255.0 + 0.5);
1178
2.81k
  transfer[3]->transform(&x, &y);
1179
2.81k
  gray[i] = (Guchar)(y * 255.0 + 0.5);
1180
2.81k
      }
1181
228
    } else {
1182
58.5k
      for (i = 0; i < 256; ++i) {
1183
58.3k
  x = i / 255.0;
1184
58.3k
  transfer[0]->transform(&x, &y);
1185
58.3k
  red[i] = green[i] = blue[i] = gray[i] = (Guchar)(y * 255.0 + 0.5);
1186
58.3k
      }
1187
228
    }
1188
689
  } else {
1189
177k
    for (i = 0; i < 256; ++i) {
1190
176k
      red[i] = green[i] = blue[i] = gray[i] = (Guchar)i;
1191
176k
    }
1192
689
  }
1193
928
  splash->setTransfer(red, green, blue, gray);
1194
928
}
1195
1196
5.00k
void SplashOutputDev::updateAlphaIsShape(GfxState *state) {
1197
5.00k
  splash->setAlphaIsShape(state->getAlphaIsShape());
1198
5.00k
}
1199
1200
291k
void SplashOutputDev::updateFont(GfxState *state) {
1201
291k
  needFontUpdate = gTrue;
1202
291k
}
1203
1204
281k
void SplashOutputDev::doUpdateFont(GfxState *state) {
1205
281k
  GfxFont *gfxFont;
1206
281k
  GfxFontLoc *fontLoc;
1207
281k
  GfxFontType fontType;
1208
281k
  SplashOutFontFileID *id;
1209
281k
  SplashFontFile *fontFile;
1210
281k
  int fontNum;
1211
281k
  FoFiTrueType *ff;
1212
281k
  FoFiType1C *ffT1C;
1213
281k
  Ref embRef;
1214
281k
  Object refObj, strObj;
1215
#if LOAD_FONTS_FROM_MEM
1216
  GString *fontBuf;
1217
  FILE *extFontFile;
1218
#else
1219
281k
  GString *tmpFileName, *fileName;
1220
281k
  FILE *tmpFile;
1221
281k
#endif
1222
281k
  char blk[4096];
1223
281k
  int *codeToGID;
1224
281k
  CharCodeToUnicode *ctu;
1225
281k
  double *textMat;
1226
281k
  double m11, m12, m21, m22, fontSize, oblique;
1227
281k
  double fsx, fsy, w, fontScaleMin, fontScaleAvg, fontScale;
1228
281k
  Gushort ww;
1229
281k
  SplashCoord mat[4];
1230
281k
  char *name, *start;
1231
281k
  Unicode uBuf[8];
1232
281k
  int substIdx, n, code, cmap, cmapPlatform, cmapEncoding, length, i;
1233
1234
281k
  needFontUpdate = gFalse;
1235
281k
  font = NULL;
1236
#if LOAD_FONTS_FROM_MEM
1237
  fontBuf = NULL;
1238
#else
1239
281k
  tmpFileName = NULL;
1240
281k
  fileName = NULL;
1241
281k
#endif
1242
281k
  substIdx = -1;
1243
1244
281k
  if (!(gfxFont = state->getFont())) {
1245
0
    goto err1;
1246
0
  }
1247
281k
  fontType = gfxFont->getType();
1248
281k
  if (fontType == fontType3) {
1249
0
    goto err1;
1250
0
  }
1251
1252
  // sanity-check the font size: skip anything larger than 10k x 10k,
1253
  // to avoid problems allocating a bitmap (note that code in
1254
  // SplashFont disables caching at a smaller size than this)
1255
281k
  state->textTransformDelta(state->getFontSize(), state->getFontSize(),
1256
281k
          &fsx, &fsy);
1257
281k
  state->transformDelta(fsx, fsy, &fsx, &fsy);
1258
281k
  if (fabs(fsx) > 20000 || fabs(fsy) > 20000) {
1259
192k
    goto err1;
1260
192k
  }
1261
1262
  // check the font file cache
1263
88.8k
  id = new SplashOutFontFileID(gfxFont->getID());
1264
88.8k
  if (fontEngine->checkForBadFontFile(id)) {
1265
17.8k
    goto err2;
1266
17.8k
  }
1267
70.9k
  if ((fontFile = fontEngine->getFontFile(id))) {
1268
20.2k
    delete id;
1269
1270
50.7k
  } else {
1271
1272
50.7k
    fontNum = 0;
1273
1274
50.7k
    if (!(fontLoc = gfxFont->locateFont(xref, gFalse))) {
1275
40.6k
      error(errSyntaxError, -1, "Couldn't find a font for '{0:s}'",
1276
40.6k
      gfxFont->getName() ? gfxFont->getName()->getCString()
1277
40.6k
                         : "(unnamed)");
1278
40.6k
      goto err2;
1279
40.6k
    }
1280
1281
    // embedded font
1282
10.0k
    if (fontLoc->locType == gfxFontLocEmbedded) {
1283
10.0k
      gfxFont->getEmbeddedFontID(&embRef);
1284
#if LOAD_FONTS_FROM_MEM
1285
      fontBuf = new GString();
1286
      refObj.initRef(embRef.num, embRef.gen);
1287
      refObj.fetch(xref, &strObj);
1288
      refObj.free();
1289
      if (!strObj.isStream()) {
1290
  error(errSyntaxError, -1, "Embedded font object is wrong type");
1291
  strObj.free();
1292
  delete fontLoc;
1293
  goto err2;
1294
      }
1295
      strObj.streamReset();
1296
      while ((n = strObj.streamGetBlock(blk, sizeof(blk))) > 0) {
1297
  fontBuf->append(blk, n);
1298
      }
1299
      strObj.streamClose();
1300
      strObj.free();
1301
#else
1302
10.0k
      if (!openTempFile(&tmpFileName, &tmpFile, "wb", NULL)) {
1303
0
  error(errIO, -1, "Couldn't create temporary font file");
1304
0
  delete fontLoc;
1305
0
  goto err2;
1306
0
      }
1307
10.0k
      refObj.initRef(embRef.num, embRef.gen);
1308
10.0k
      refObj.fetch(xref, &strObj);
1309
10.0k
      refObj.free();
1310
10.0k
      if (!strObj.isStream()) {
1311
0
  error(errSyntaxError, -1, "Embedded font object is wrong type");
1312
0
  strObj.free();
1313
0
  fclose(tmpFile);
1314
0
  delete fontLoc;
1315
0
  goto err2;
1316
0
      }
1317
10.0k
      strObj.streamReset();
1318
99.0k
      while ((n = strObj.streamGetBlock(blk, sizeof(blk))) > 0) {
1319
89.0k
  fwrite(blk, 1, n, tmpFile);
1320
89.0k
      }
1321
10.0k
      strObj.streamClose();
1322
10.0k
      strObj.free();
1323
10.0k
      fclose(tmpFile);
1324
10.0k
      fileName = tmpFileName;
1325
10.0k
#endif
1326
1327
    // external font
1328
10.0k
    } else { // gfxFontLocExternal
1329
#if LOAD_FONTS_FROM_MEM
1330
      if (!(extFontFile = fopen(fontLoc->path->getCString(), "rb"))) {
1331
  error(errSyntaxError, -1, "Couldn't open external font file '{0:t}'",
1332
        fontLoc->path);
1333
  delete fontLoc;
1334
  goto err2;
1335
      }
1336
      fontBuf = new GString();
1337
      while ((n = fread(blk, 1, sizeof(blk), extFontFile)) > 0) {
1338
  fontBuf->append(blk, n);
1339
      }
1340
      fclose(extFontFile);
1341
#else
1342
0
      fileName = fontLoc->path;
1343
0
#endif
1344
0
      fontNum = fontLoc->fontNum;
1345
0
      if (fontLoc->substIdx >= 0) {
1346
0
  id->setSubstIdx(fontLoc->substIdx);
1347
0
      }
1348
0
      if (fontLoc->oblique != 0) {
1349
0
  id->setOblique(fontLoc->oblique);
1350
0
      }
1351
0
    }
1352
1353
    // load the font file
1354
10.0k
    switch (fontLoc->fontType) {
1355
1.96k
    case fontType1:
1356
1.96k
      if (!(fontFile = fontEngine->loadType1Font(
1357
1.96k
       id,
1358
#if LOAD_FONTS_FROM_MEM
1359
       fontBuf,
1360
#else
1361
1.96k
       fileName->getCString(),
1362
1.96k
       fileName == tmpFileName,
1363
1.96k
#endif
1364
1.96k
       (const char **)((Gfx8BitFont *)gfxFont)->getEncoding()))) {
1365
1.78k
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1366
1.78k
        gfxFont->getName() ? gfxFont->getName()->getCString()
1367
1.78k
                           : "(unnamed)");
1368
1.78k
  delete fontLoc;
1369
1.78k
  goto err1;
1370
1.78k
      }
1371
175
      break;
1372
2.16k
    case fontType1C:
1373
#if LOAD_FONTS_FROM_MEM
1374
      if ((ffT1C = FoFiType1C::make(fontBuf->getCString(),
1375
            fontBuf->getLength()))) {
1376
#else
1377
2.16k
      if ((ffT1C = FoFiType1C::load(fileName->getCString()))) {
1378
1.22k
#endif
1379
1.22k
  codeToGID = ((Gfx8BitFont *)gfxFont)->getCodeToGIDMap(ffT1C);
1380
1.22k
  delete ffT1C;
1381
1.22k
      } else {
1382
941
  codeToGID = NULL;
1383
941
      }
1384
2.16k
      if (!(fontFile = fontEngine->loadType1CFont(
1385
2.16k
       id,
1386
#if LOAD_FONTS_FROM_MEM
1387
       fontBuf,
1388
#else
1389
2.16k
       fileName->getCString(),
1390
2.16k
       fileName == tmpFileName,
1391
2.16k
#endif
1392
2.16k
       codeToGID,
1393
2.16k
       (const char **)((Gfx8BitFont *)gfxFont)->getEncoding()))) {
1394
576
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1395
576
        gfxFont->getName() ? gfxFont->getName()->getCString()
1396
576
                           : "(unnamed)");
1397
576
  delete fontLoc;
1398
576
  goto err1;
1399
576
      }
1400
1.58k
      break;
1401
1.58k
    case fontType1COT:
1402
815
      codeToGID = NULL;
1403
#if LOAD_FONTS_FROM_MEM
1404
      if ((ff = FoFiTrueType::make(fontBuf->getCString(), fontBuf->getLength(),
1405
           fontNum, gTrue))) {
1406
#else
1407
815
  if ((ff = FoFiTrueType::load(fileName->getCString(),
1408
815
             fontNum, gTrue))) {
1409
805
#endif
1410
805
  if (ff->getCFFBlock(&start, &length) &&
1411
804
      (ffT1C = FoFiType1C::make(start, length))) {
1412
736
    codeToGID = ((Gfx8BitFont *)gfxFont)->getCodeToGIDMap(ffT1C);
1413
736
    delete ffT1C;
1414
736
  }
1415
805
  delete ff;
1416
805
      }
1417
815
      if (!(fontFile = fontEngine->loadOpenTypeT1CFont(
1418
815
       id,
1419
#if LOAD_FONTS_FROM_MEM
1420
       fontBuf,
1421
#else
1422
815
       fileName->getCString(),
1423
815
       fileName == tmpFileName,
1424
815
#endif
1425
815
       codeToGID,
1426
815
       (const char **)((Gfx8BitFont *)gfxFont)->getEncoding()))) {
1427
467
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1428
467
        gfxFont->getName() ? gfxFont->getName()->getCString()
1429
467
                           : "(unnamed)");
1430
467
  delete fontLoc;
1431
467
  goto err1;
1432
467
      }
1433
348
      break;
1434
2.13k
    case fontTrueType:
1435
2.13k
    case fontTrueTypeOT:
1436
#if LOAD_FONTS_FROM_MEM
1437
      if ((ff = FoFiTrueType::make(fontBuf->getCString(), fontBuf->getLength(),
1438
           fontNum))) {
1439
#else
1440
2.13k
      if ((ff = FoFiTrueType::load(fileName->getCString(), fontNum))) {
1441
2.10k
#endif
1442
2.10k
  codeToGID = ((Gfx8BitFont *)gfxFont)->getCodeToGIDMap(ff);
1443
2.10k
  n = 256;
1444
2.10k
  delete ff;
1445
  // if we're substituting for a non-TrueType font, we need to mark
1446
  // all notdef codes as "do not draw" (rather than drawing TrueType
1447
  // notdef glyphs)
1448
2.10k
  if (gfxFont->getType() != fontTrueType &&
1449
0
      gfxFont->getType() != fontTrueTypeOT) {
1450
0
    for (i = 0; i < 256; ++i) {
1451
0
      if (codeToGID[i] == 0) {
1452
0
        codeToGID[i] = -1;
1453
0
      }
1454
0
    }
1455
0
  }
1456
2.10k
      } else {
1457
23
  codeToGID = NULL;
1458
23
  n = 0;
1459
23
      }
1460
2.13k
      if (!(fontFile = fontEngine->loadTrueTypeFont(
1461
2.13k
         id,
1462
#if LOAD_FONTS_FROM_MEM
1463
         fontBuf,
1464
#else
1465
2.13k
         fileName->getCString(),
1466
2.13k
         fileName == tmpFileName,
1467
2.13k
#endif
1468
2.13k
         fontNum, codeToGID, n,
1469
2.13k
         gfxFont->getEmbeddedFontName()
1470
2.13k
           ? gfxFont->getEmbeddedFontName()->getCString()
1471
2.13k
           : (char *)NULL))) {
1472
60
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1473
60
        gfxFont->getName() ? gfxFont->getName()->getCString()
1474
60
                           : "(unnamed)");
1475
60
  delete fontLoc;
1476
60
  goto err1;
1477
60
      }
1478
2.07k
      break;
1479
2.07k
    case fontCIDType0:
1480
52
    case fontCIDType0C:
1481
52
      if (((GfxCIDFont *)gfxFont)->getCIDToGID()) {
1482
0
  n = ((GfxCIDFont *)gfxFont)->getCIDToGIDLen();
1483
0
  codeToGID = (int *)gmallocn(n, sizeof(int));
1484
0
  memcpy(codeToGID, ((GfxCIDFont *)gfxFont)->getCIDToGID(),
1485
0
         n * sizeof(int));
1486
52
      } else {
1487
52
  codeToGID = NULL;
1488
52
  n = 0;
1489
52
      }
1490
52
      if (!(fontFile = fontEngine->loadCIDFont(
1491
52
         id,
1492
#if LOAD_FONTS_FROM_MEM
1493
         fontBuf,
1494
#else
1495
52
         fileName->getCString(),
1496
52
         fileName == tmpFileName,
1497
52
#endif
1498
52
         codeToGID, n))) {
1499
1500
8
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1501
8
        gfxFont->getName() ? gfxFont->getName()->getCString()
1502
8
                           : "(unnamed)");
1503
8
  delete fontLoc;
1504
8
  goto err1;
1505
8
      }
1506
44
      break;
1507
44
    case fontCIDType0COT:
1508
33
      codeToGID = NULL;
1509
33
      n = 0;
1510
33
      if (fontLoc->locType == gfxFontLocEmbedded) {
1511
33
  if (((GfxCIDFont *)gfxFont)->getCIDToGID()) {
1512
0
    n = ((GfxCIDFont *)gfxFont)->getCIDToGIDLen();
1513
0
    codeToGID = (int *)gmallocn(n, sizeof(int));
1514
0
    memcpy(codeToGID, ((GfxCIDFont *)gfxFont)->getCIDToGID(),
1515
0
     n * sizeof(int));
1516
0
  }
1517
33
      } else if (globalParams->getMapExtTrueTypeFontsViaUnicode()) {
1518
  // create a CID-to-GID mapping, via Unicode
1519
0
  if ((ctu = ((GfxCIDFont *)gfxFont)->getToUnicode())) {
1520
#if LOAD_FONTS_FROM_MEM
1521
    if ((ff = FoFiTrueType::make(fontBuf->getCString(),
1522
               fontBuf->getLength(), fontNum))) {
1523
#else
1524
0
    if ((ff = FoFiTrueType::load(fileName->getCString(), fontNum))) {
1525
0
#endif
1526
      // look for a Unicode cmap
1527
0
      for (cmap = 0; cmap < ff->getNumCmaps(); ++cmap) {
1528
0
        cmapPlatform = ff->getCmapPlatform(cmap);
1529
0
        cmapEncoding = ff->getCmapEncoding(cmap);
1530
0
        if ((cmapPlatform == 3 && cmapEncoding == 1) ||
1531
0
      (cmapPlatform == 0 && cmapEncoding <= 4)) {
1532
0
    break;
1533
0
        }
1534
0
      }
1535
0
      if (cmap < ff->getNumCmaps()) {
1536
        // map CID -> Unicode -> GID
1537
0
        if (ctu->isIdentity()) {
1538
0
    n = 65536;
1539
0
        } else {
1540
0
    n = ctu->getLength();
1541
0
        }
1542
0
        codeToGID = (int *)gmallocn(n, sizeof(int));
1543
0
        for (code = 0; code < n; ++code) {
1544
0
    if (ctu->mapToUnicode(code, uBuf, 8) > 0) {
1545
0
      codeToGID[code] = ff->mapCodeToGID(cmap, uBuf[0]);
1546
0
    } else {
1547
0
      codeToGID[code] = -1;
1548
0
    }
1549
0
        }
1550
0
      }
1551
0
      delete ff;
1552
0
    }
1553
0
    ctu->decRefCnt();
1554
0
  } else {
1555
0
    error(errSyntaxError, -1,
1556
0
    "Couldn't find a mapping to Unicode for font '{0:s}'",
1557
0
    gfxFont->getName() ? gfxFont->getName()->getCString()
1558
0
                       : "(unnamed)");
1559
0
  }
1560
0
      }
1561
33
      if (!(fontFile = fontEngine->loadOpenTypeCFFFont(
1562
33
         id,
1563
#if LOAD_FONTS_FROM_MEM
1564
         fontBuf,
1565
#else
1566
33
         fileName->getCString(),
1567
33
         fileName == tmpFileName,
1568
33
#endif
1569
33
         codeToGID, n))) {
1570
13
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1571
13
        gfxFont->getName() ? gfxFont->getName()->getCString()
1572
13
                           : "(unnamed)");
1573
13
  delete fontLoc;
1574
13
  goto err1;
1575
13
      }
1576
20
      break;
1577
2.92k
    case fontCIDType2:
1578
2.92k
    case fontCIDType2OT:
1579
2.92k
      codeToGID = NULL;
1580
2.92k
      n = 0;
1581
2.92k
      if (fontLoc->locType == gfxFontLocEmbedded) {
1582
2.92k
  if (((GfxCIDFont *)gfxFont)->getCIDToGID()) {
1583
9
    n = ((GfxCIDFont *)gfxFont)->getCIDToGIDLen();
1584
9
    codeToGID = (int *)gmallocn(n, sizeof(int));
1585
9
    memcpy(codeToGID, ((GfxCIDFont *)gfxFont)->getCIDToGID(),
1586
9
     n * sizeof(int));
1587
9
  }
1588
2.92k
      } else if (globalParams->getMapExtTrueTypeFontsViaUnicode() &&
1589
0
     !((GfxCIDFont *)gfxFont)->usesIdentityEncoding()) {
1590
  // create a CID-to-GID mapping, via Unicode
1591
0
  if ((ctu = ((GfxCIDFont *)gfxFont)->getToUnicode())) {
1592
#if LOAD_FONTS_FROM_MEM
1593
    if ((ff = FoFiTrueType::make(fontBuf->getCString(),
1594
               fontBuf->getLength(), fontNum))) {
1595
#else
1596
0
    if ((ff = FoFiTrueType::load(fileName->getCString(), fontNum))) {
1597
0
#endif
1598
      // look for a Unicode cmap
1599
0
      for (cmap = 0; cmap < ff->getNumCmaps(); ++cmap) {
1600
0
        cmapPlatform = ff->getCmapPlatform(cmap);
1601
0
        cmapEncoding = ff->getCmapEncoding(cmap);
1602
0
        if ((cmapPlatform == 3 && cmapEncoding == 1) ||
1603
0
      (cmapPlatform == 0 && cmapEncoding <= 4)) {
1604
0
    break;
1605
0
        }
1606
0
      }
1607
0
      if (cmap < ff->getNumCmaps()) {
1608
        // map CID -> Unicode -> GID
1609
0
        if (ctu->isIdentity()) {
1610
0
    n = 65536;
1611
0
        } else {
1612
0
    n = ctu->getLength();
1613
0
        }
1614
0
        codeToGID = (int *)gmallocn(n, sizeof(int));
1615
0
        for (code = 0; code < n; ++code) {
1616
0
    if (ctu->mapToUnicode(code, uBuf, 8) > 0) {
1617
0
      codeToGID[code] = ff->mapCodeToGID(cmap, uBuf[0]);
1618
0
    } else {
1619
0
      codeToGID[code] = -1;
1620
0
    }
1621
0
        }
1622
0
      }
1623
0
      delete ff;
1624
0
    }
1625
0
    ctu->decRefCnt();
1626
0
  } else {
1627
0
    error(errSyntaxError, -1,
1628
0
    "Couldn't find a mapping to Unicode for font '{0:s}'",
1629
0
    gfxFont->getName() ? gfxFont->getName()->getCString()
1630
0
                       : "(unnamed)");
1631
0
  }
1632
0
      }
1633
2.92k
      if (!(fontFile = fontEngine->loadTrueTypeFont(
1634
2.92k
         id,
1635
#if LOAD_FONTS_FROM_MEM
1636
         fontBuf,
1637
#else
1638
2.92k
         fileName->getCString(),
1639
2.92k
         fileName == tmpFileName,
1640
2.92k
#endif
1641
2.92k
         fontNum, codeToGID, n,
1642
2.92k
         gfxFont->getEmbeddedFontName()
1643
2.92k
           ? gfxFont->getEmbeddedFontName()->getCString()
1644
2.92k
           : (char *)NULL))) {
1645
405
  error(errSyntaxError, -1, "Couldn't create a font for '{0:s}'",
1646
405
        gfxFont->getName() ? gfxFont->getName()->getCString()
1647
405
                           : "(unnamed)");
1648
405
  delete fontLoc;
1649
405
  goto err1;
1650
405
      }
1651
2.51k
      break;
1652
2.51k
    default:
1653
      // this shouldn't happen
1654
0
      delete fontLoc;
1655
0
      goto err2;
1656
10.0k
    }
1657
1658
6.76k
    delete fontLoc;
1659
6.76k
  }
1660
1661
  // get the font matrix
1662
27.0k
  textMat = state->getTextMat();
1663
27.0k
  fontSize = state->getFontSize();
1664
27.0k
  oblique = ((SplashOutFontFileID *)fontFile->getID())->getOblique();
1665
27.0k
  m11 = state->getHorizScaling() * textMat[0];
1666
27.0k
  m12 = state->getHorizScaling() * textMat[1];
1667
27.0k
  m21 = oblique * m11 + textMat[2];
1668
27.0k
  m22 = oblique * m12 + textMat[3];
1669
27.0k
  m11 *= fontSize;
1670
27.0k
  m12 *= fontSize;
1671
27.0k
  m21 *= fontSize;
1672
27.0k
  m22 *= fontSize;
1673
1674
  // for substituted fonts: adjust the font matrix -- compare the
1675
  // widths of letters and digits (A-Z, a-z, 0-9) in the original font
1676
  // and the substituted font
1677
27.0k
  substIdx = ((SplashOutFontFileID *)fontFile->getID())->getSubstIdx();
1678
27.0k
  if (substIdx >= 0 && substIdx < 12) {
1679
0
    fontScaleMin = 1;
1680
0
    fontScaleAvg = 0;
1681
0
    n = 0;
1682
0
    for (code = 0; code < 256; ++code) {
1683
0
      if ((name = ((Gfx8BitFont *)gfxFont)->getCharName(code)) &&
1684
0
    name[0] && !name[1] &&
1685
0
    ((name[0] >= 'A' && name[0] <= 'Z') ||
1686
0
     (name[0] >= 'a' && name[0] <= 'z') ||
1687
0
     (name[0] >= '0' && name[0] <= '9'))) {
1688
0
  w = ((Gfx8BitFont *)gfxFont)->getWidth((Guchar)code);
1689
0
  if (builtinFontSubst[substIdx]->widths->getWidth(name, &ww) &&
1690
0
      w > 0.01 && ww > 10) {
1691
0
    w /= ww * 0.001;
1692
0
    if (w < fontScaleMin) {
1693
0
      fontScaleMin = w;
1694
0
    }
1695
0
    fontScaleAvg += w;
1696
0
    ++n;
1697
0
  }
1698
0
      }
1699
0
    }
1700
    // if real font is narrower than substituted font, reduce the font
1701
    // size accordingly -- this currently uses a scale factor halfway
1702
    // between the minimum and average computed scale factors, which
1703
    // is a bit of a kludge, but seems to produce mostly decent
1704
    // results
1705
0
    if (n) {
1706
0
      fontScaleAvg /= n;
1707
0
      if (fontScaleAvg < 1) {
1708
0
  fontScale = 0.5 * (fontScaleMin + fontScaleAvg);
1709
0
  m11 *= fontScale;
1710
0
  m12 *= fontScale;
1711
0
      }
1712
0
    }
1713
0
  }
1714
1715
  // create the scaled font
1716
27.0k
  mat[0] = m11;  mat[1] = m12;
1717
27.0k
  mat[2] = m21;  mat[3] = m22;
1718
27.0k
  font = fontEngine->getFont(fontFile, mat, splash->getMatrix());
1719
1720
27.0k
#if !LOAD_FONTS_FROM_MEM
1721
27.0k
  if (tmpFileName) {
1722
6.76k
    delete tmpFileName;
1723
6.76k
  }
1724
27.0k
#endif
1725
27.0k
  return;
1726
1727
58.5k
 err2:
1728
58.5k
  delete id;
1729
254k
 err1:
1730
#if LOAD_FONTS_FROM_MEM
1731
  if (fontBuf) {
1732
    delete fontBuf;
1733
  }
1734
#else
1735
254k
  if (tmpFileName) {
1736
3.31k
    unlink(tmpFileName->getCString());
1737
3.31k
    delete tmpFileName;
1738
3.31k
  }
1739
254k
#endif
1740
254k
  return;
1741
58.5k
}
1742
1743
47.4k
void SplashOutputDev::stroke(GfxState *state) {
1744
47.4k
  SplashPath *path;
1745
1746
47.4k
  if (state->getStrokeColorSpace()->isNonMarking()) {
1747
0
    return;
1748
0
  }
1749
47.4k
  setOverprintMask(state, state->getStrokeColorSpace(),
1750
47.4k
       state->getStrokeOverprint(), state->getOverprintMode(),
1751
47.4k
       state->getStrokeColor());
1752
47.4k
  path = convertPath(state, state->getPath(), gFalse);
1753
47.4k
  splash->stroke(path);
1754
47.4k
  delete path;
1755
47.4k
}
1756
1757
67.9k
void SplashOutputDev::fill(GfxState *state) {
1758
67.9k
  SplashPath *path;
1759
1760
67.9k
  if (state->getFillColorSpace()->isNonMarking()) {
1761
0
    return;
1762
0
  }
1763
67.9k
  setOverprintMask(state, state->getFillColorSpace(),
1764
67.9k
       state->getFillOverprint(), state->getOverprintMode(),
1765
67.9k
       state->getFillColor());
1766
67.9k
  path = convertPath(state, state->getPath(), gTrue);
1767
67.9k
  splash->fill(path, gFalse);
1768
67.9k
  delete path;
1769
67.9k
}
1770
1771
4.71k
void SplashOutputDev::eoFill(GfxState *state) {
1772
4.71k
  SplashPath *path;
1773
1774
4.71k
  if (state->getFillColorSpace()->isNonMarking()) {
1775
0
    return;
1776
0
  }
1777
4.71k
  setOverprintMask(state, state->getFillColorSpace(),
1778
4.71k
       state->getFillOverprint(), state->getOverprintMode(),
1779
4.71k
       state->getFillColor());
1780
4.71k
  path = convertPath(state, state->getPath(), gTrue);
1781
4.71k
  splash->fill(path, gTrue);
1782
4.71k
  delete path;
1783
4.71k
}
1784
1785
void SplashOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx,
1786
          Object *strRef,
1787
          int paintType, int tilingType,
1788
          Dict *resDict,
1789
          double *mat, double *bbox,
1790
          int x0, int y0, int x1, int y1,
1791
2.02k
          double xStep, double yStep) {
1792
2.02k
  SplashBitmap *origBitmap, *tileBitmap;
1793
2.02k
  Splash *origSplash;
1794
2.02k
  SplashColor color;
1795
2.02k
  Guint *overprintMaskBitmap;
1796
2.02k
  double *ctm, *baseMatrix;
1797
2.02k
  double ictm[6], tileMat[6], mat1[6], mat2[6];
1798
2.02k
  double tileXMin, tileYMin, tileXMax, tileYMax;
1799
2.02k
  double xStepX, xStepY, yStepX, yStepY;
1800
2.02k
  double adjXMin, adjYMin;
1801
2.02k
  double sx, sy;
1802
2.02k
  double clipXMin, clipYMin, clipXMax, clipYMax, clipXC, clipYC;
1803
2.02k
  double tx, ty, idet, txMin, tyMin, txMax, tyMax;
1804
2.02k
  GBool clipped;
1805
2.02k
  int tileW, tileH, tileSize;
1806
2.02k
  int ixMin, ixMax, iyMin, iyMax, ix, iy, x, y;
1807
2.02k
  int i;
1808
1809
  // Notes:
1810
  // - PTM = pattern matrix = transform from pattern space to default
1811
  //         user space (default for most recent page or form)
1812
  // - BTM = transform from default user space to device space
1813
  //
1814
  // This function is called with:
1815
  // - mat = PTM * BTM * iCTM = transform from pattern space to
1816
  //         current user space
1817
1818
  // transform the four corners of the pattern bbox from pattern space
1819
  // to device space and compute the device space bbox
1820
2.02k
  state->transform(bbox[0] * mat[0] + bbox[1] * mat[2] + mat[4],
1821
2.02k
       bbox[0] * mat[1] + bbox[1] * mat[3] + mat[5],
1822
2.02k
       &tx, &ty);
1823
2.02k
  tileXMin = tileXMax = tx;
1824
2.02k
  tileYMin = tileYMax = ty;
1825
2.02k
  state->transform(bbox[2] * mat[0] + bbox[1] * mat[2] + mat[4],
1826
2.02k
       bbox[2] * mat[1] + bbox[1] * mat[3] + mat[5],
1827
2.02k
       &tx, &ty);
1828
2.02k
  if (tx < tileXMin) {
1829
295
    tileXMin = tx;
1830
1.72k
  } else if (tx > tileXMax) {
1831
555
    tileXMax = tx;
1832
555
  }
1833
2.02k
  if (ty < tileYMin) {
1834
26
    tileYMin = ty;
1835
1.99k
  } else if (ty > tileYMax) {
1836
25
    tileYMax = ty;
1837
25
  }
1838
2.02k
  state->transform(bbox[2] * mat[0] + bbox[3] * mat[2] + mat[4],
1839
2.02k
       bbox[2] * mat[1] + bbox[3] * mat[3] + mat[5],
1840
2.02k
       &tx, &ty);
1841
2.02k
  if (tx < tileXMin) {
1842
8
    tileXMin = tx;
1843
2.01k
  } else if (tx > tileXMax) {
1844
124
    tileXMax = tx;
1845
124
  }
1846
2.02k
  if (ty < tileYMin) {
1847
734
    tileYMin = ty;
1848
1.28k
  } else if (ty > tileYMax) {
1849
108
    tileYMax = ty;
1850
108
  }
1851
2.02k
  state->transform(bbox[0] * mat[0] + bbox[3] * mat[2] + mat[4],
1852
2.02k
       bbox[0] * mat[1] + bbox[3] * mat[3] + mat[5],
1853
2.02k
       &tx, &ty);
1854
2.02k
  if (tx < tileXMin) {
1855
187
    tileXMin = tx;
1856
1.83k
  } else if (tx > tileXMax) {
1857
7
    tileXMax = tx;
1858
7
  }
1859
2.02k
  if (ty < tileYMin) {
1860
36
    tileYMin = ty;
1861
1.98k
  } else if (ty > tileYMax) {
1862
4
    tileYMax = ty;
1863
4
  }
1864
2.02k
  if (tileXMin == tileXMax || tileYMin == tileYMax) {
1865
131
    return;
1866
131
  }
1867
1.89k
  tileW = (int)(tileXMax - tileXMin + 0.5);
1868
1.89k
  tileH = (int)(tileYMax - tileYMin + 0.5);
1869
1.89k
  if (tileW < 1) {
1870
1.89k
    tileW = 1;
1871
1.89k
  }
1872
1.89k
  if (tileH < 1) {
1873
1.89k
    tileH = 1;
1874
1.89k
  }
1875
1876
  // check for an excessively large tile size
1877
1.89k
  tileSize = tileW * tileH;
1878
1.89k
  if (tileXMax - tileXMin + 0.5 > (double)INT_MAX ||
1879
1.04k
      tileYMax - tileYMin + 0.5 > (double)INT_MAX ||
1880
1.04k
      tileW > INT_MAX / tileH ||
1881
1.04k
      tileSize > maxTileSize) {
1882
842
    mat1[0] = mat[0];
1883
842
    mat1[1] = mat[1];
1884
842
    mat1[2] = mat[2];
1885
842
    mat1[3] = mat[3];
1886
3.45k
    for (iy = y0; iy < y1; ++iy) {
1887
37.2k
      for (ix = x0; ix < x1; ++ix) {
1888
34.6k
  tx = ix * xStep;
1889
34.6k
  ty = iy * yStep;
1890
34.6k
  mat1[4] = tx * mat[0] + ty * mat[2] + mat[4];
1891
34.6k
  mat1[5] = tx * mat[1] + ty * mat[3] + mat[5];
1892
34.6k
  gfx->drawForm(strRef, resDict, mat1, bbox);
1893
34.6k
      }
1894
2.61k
    }
1895
842
    return;
1896
842
  }
1897
1898
  // transform XStep and YStep to device space
1899
1.04k
  state->transformDelta(xStep * mat[0], xStep * mat[1], &xStepX, &xStepY);
1900
1.04k
  state->transformDelta(yStep * mat[2], yStep * mat[3], &yStepX, &yStepY);
1901
1902
  // get the clipping bbox (in device space)
1903
1.04k
  state->getClipBBox(&clipXMin, &clipYMin, &clipXMax, &clipYMax);
1904
1905
  // compute tiling parameters
1906
1.04k
  idet = xStepX * yStepY - yStepX * xStepY;
1907
1.04k
  if (tilingType == 2 || idet == 0) {
1908
457
    adjXMin = tileXMin;
1909
457
    adjYMin = tileYMin;
1910
457
    sx = 1;
1911
457
    sy = 1;
1912
592
  } else {
1913
    // reposition the pattern origin to the center of the clipping bbox
1914
592
    idet = 1 / idet;
1915
592
    clipXC = 0.5 * (clipXMin + clipXMax);
1916
592
    clipYC = 0.5 * (clipYMin + clipYMax);
1917
592
    ix = (int)floor((yStepX * (tileYMin - clipYC)
1918
592
         - (tileXMin - clipXC) * yStepY) * idet + 0.5);
1919
592
    iy = (int)floor((xStepX * (clipYC - tileYMin)
1920
592
         - (clipXC - tileXMin) * xStepY) * idet + 0.5);
1921
592
    adjXMin = (int)floor(tileXMin + ix * xStepX + iy * yStepX + 0.5);
1922
592
    adjYMin = (int)floor(tileYMin + ix * xStepY + iy * yStepY + 0.5);
1923
592
    sx = tileW / (tileXMax - tileXMin);
1924
592
    sy = tileH / (tileYMax - tileYMin);
1925
592
    xStepX = (int)floor(sx * xStepX + 0.5);
1926
592
    xStepY = (int)floor(sy * xStepY + 0.5);
1927
592
    yStepX = (int)floor(sx * yStepX + 0.5);
1928
592
    yStepY = (int)floor(sy * yStepY + 0.5);
1929
592
  }
1930
1931
  // compute tiling range:
1932
  // - look at the four corners of the clipping bbox
1933
  // - solve for the (ix,iy) tile position at each corner
1934
  // - take the min and max values for ix, iy
1935
1.04k
  idet = xStepX * yStepY - xStepY * yStepX;
1936
1.04k
  if (idet == 0) {
1937
640
    return;
1938
640
  }
1939
409
  idet = 1 / idet;
1940
  // LL corner
1941
409
  tx = idet * (yStepY * (clipXMin - tileW - 1 - adjXMin)
1942
409
         - yStepX * (clipYMax + 1 - adjYMin));
1943
409
  ty = idet * (xStepX * (clipYMax + 1 - adjYMin)
1944
409
         - xStepY * (clipXMin - tileW - 1 - adjXMin));
1945
409
  txMin = txMax = tx;
1946
409
  tyMin = tyMax = ty;
1947
  // LR corner
1948
409
  tx = idet * (yStepY * (clipXMax + 1 - adjXMin)
1949
409
         - yStepX * (clipYMax + 1 - adjYMin));
1950
409
  ty = idet * (xStepX * (clipYMax + 1 - adjYMin)
1951
409
         - xStepY * (clipXMax + 1 - adjXMin));
1952
409
  if (tx < txMin) {
1953
0
    txMin = tx;
1954
409
  } else if (tx > txMax) {
1955
0
    txMax = tx;
1956
0
  }
1957
409
  if (ty < tyMin) {
1958
0
    tyMin = ty;
1959
409
  } else if (ty > tyMax) {
1960
0
    tyMax = ty;
1961
0
  }
1962
  // UL corner
1963
409
  tx = idet * (yStepY * (clipXMin - tileW - 1 - adjXMin)
1964
409
         - yStepX * (clipYMin - tileH - 1 - adjYMin));
1965
409
  ty = idet * (xStepX * (clipYMin - tileH - 1 - adjYMin)
1966
409
         - xStepY * (clipXMin - tileW - 1 - adjXMin));
1967
409
  if (tx < txMin) {
1968
0
    txMin = tx;
1969
409
  } else if (tx > txMax) {
1970
0
    txMax = tx;
1971
0
  }
1972
409
  if (ty < tyMin) {
1973
0
    tyMin = ty;
1974
409
  } else if (ty > tyMax) {
1975
0
    tyMax = ty;
1976
0
  }
1977
  // UR corner
1978
409
  tx = idet * (yStepY * (clipXMax + 1 - adjXMin)
1979
409
         - yStepX * (clipYMin - tileH - 1 - adjYMin));
1980
409
  ty = idet * (xStepX * (clipYMin - tileH - 1 - adjYMin)
1981
409
         - xStepY * (clipXMax + 1 - adjXMin));
1982
409
  if (tx < txMin) {
1983
0
    txMin = tx;
1984
409
  } else if (tx > txMax) {
1985
0
    txMax = tx;
1986
0
  }
1987
409
  if (ty < tyMin) {
1988
0
    tyMin = ty;
1989
409
  } else if (ty > tyMax) {
1990
0
    tyMax = ty;
1991
0
  }
1992
409
  ixMin = (int)ceil(txMin);
1993
409
  ixMax = (int)floor(txMax) + 1;
1994
409
  iyMin = (int)ceil(tyMin);
1995
409
  iyMax = (int)floor(tyMax) + 1;
1996
1997
  // check for an excessively large tiling range, which
1998
  // can happen if the pattern matrix is "non-rectangular"
1999
  // (casting to double to avoid integer overflow)
2000
409
  if ((double)(ixMax - ixMin) * (double)(iyMax - iyMin) >
2001
409
      10 * (double)(x1 - x0) * (double)(y1 - y0)) {
2002
0
    mat1[0] = mat[0];
2003
0
    mat1[1] = mat[1];
2004
0
    mat1[2] = mat[2];
2005
0
    mat1[3] = mat[3];
2006
0
    for (iy = y0; iy < y1; ++iy) {
2007
0
      for (ix = x0; ix < x1; ++ix) {
2008
0
  tx = ix * xStep;
2009
0
  ty = iy * yStep;
2010
0
  mat1[4] = tx * mat[0] + ty * mat[2] + mat[4];
2011
0
  mat1[5] = tx * mat[1] + ty * mat[3] + mat[5];
2012
0
  gfx->drawForm(strRef, resDict, mat1, bbox);
2013
0
      }
2014
0
    }
2015
0
    return;
2016
0
  }
2017
2018
  // special case: pattern tile is larger than clipping bbox
2019
409
  if (ixMax - ixMin == 1 && iyMax - iyMin == 1 &&
2020
409
      (clipXMax - clipXMin < 0.5 * tileW ||
2021
216
       clipYMax - clipYMin < 0.5 * tileH)) {
2022
216
    clipped = gTrue;
2023
    // reduce the tile size to just the clipping bbox -- this improves
2024
    // performance in cases where just a small portion of one tile is
2025
    // needed
2026
216
    tileW = (int)(clipXMax - clipXMin + 0.5);
2027
216
    tileH = (int)(clipYMax - clipYMin + 0.5);
2028
216
    if (tileW < 1) {
2029
216
      tileW = 1;
2030
216
    }
2031
216
    if (tileH < 1) {
2032
216
      tileH = 1;
2033
216
    }
2034
216
    tileXMin += clipXMin - (adjXMin + ixMin * xStepX + iyMin * yStepX);
2035
216
    tileYMin += clipYMin - (adjYMin + ixMin * xStepY + iyMin * yStepY);
2036
216
    ixMin = 0;
2037
216
    iyMin = 0;
2038
216
    ixMax = 1;
2039
216
    iyMax = 1;
2040
216
    adjXMin = clipXMin;
2041
216
    adjYMin = clipYMin;
2042
216
  } else {
2043
193
    clipped = gFalse;
2044
193
  }
2045
2046
  // compute tile matrix = PTM * BTM * Mtranslate * Mscale * iCTM
2047
  //                     = mat * CTM * Mtranslate * Mscale * iCTM
2048
409
  ctm = state->getCTM();
2049
409
  idet = 1 / (ctm[0] * ctm[3] - ctm[1] * ctm[2]);
2050
409
  ictm[0] = ctm[3] * idet;
2051
409
  ictm[1] = -ctm[1] * idet;
2052
409
  ictm[2] = -ctm[2] * idet;
2053
409
  ictm[3] = ctm[0] * idet;
2054
409
  ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * idet;
2055
409
  ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * idet;
2056
  // mat * CTM
2057
409
  mat1[0] = mat[0] * ctm[0] + mat[1] * ctm[2];
2058
409
  mat1[1] = mat[0] * ctm[1] + mat[1] * ctm[3];
2059
409
  mat1[2] = mat[2] * ctm[0] + mat[3] * ctm[2];
2060
409
  mat1[3] = mat[2] * ctm[1] + mat[3] * ctm[3];
2061
409
  mat1[4] = mat[4] * ctm[0] + mat[5] * ctm[2] + ctm[4];
2062
409
  mat1[5] = mat[4] * ctm[1] + mat[5] * ctm[3] + ctm[5];
2063
  // mat * CTM * (Mtranslate * Mscale)
2064
409
  mat2[0] = mat1[0] * sx;
2065
409
  mat2[1] = mat1[1] * sy;
2066
409
  mat2[2] = mat1[2] * sx;
2067
409
  mat2[3] = mat1[3] * sy;
2068
409
  mat2[4] = mat1[4] * sx - sx * tileXMin;
2069
409
  mat2[5] = mat1[5] * sy - sy * tileYMin;
2070
  // mat * CTM * (Mtranslate * Mscale) * iCTM
2071
409
  tileMat[0] = mat2[0] * ictm[0] + mat2[1] * ictm[2];
2072
409
  tileMat[1] = mat2[0] * ictm[1] + mat2[1] * ictm[3];
2073
409
  tileMat[2] = mat2[2] * ictm[0] + mat2[3] * ictm[2];
2074
409
  tileMat[3] = mat2[2] * ictm[1] + mat2[3] * ictm[3];
2075
409
  tileMat[4] = mat2[4] * ictm[0] + mat2[5] * ictm[2] + ictm[4];
2076
409
  tileMat[5] = mat2[4] * ictm[1] + mat2[5] * ictm[3] + ictm[5];
2077
2078
409
  baseMatrix = gfx->getBaseMatrix();
2079
409
  if (paintType == 1 &&   // colored tiling pattern
2080
383
      !clipped &&
2081
191
      strRef->isRef() &&
2082
191
      strRef->getRefNum() == tileCacheRef.num &&
2083
164
      strRef->getRefGen() == tileCacheRef.gen &&
2084
164
      tileCacheBitmap &&
2085
159
      tileCacheBitmap->getWidth() == tileW &&
2086
159
      tileCacheBitmap->getHeight() == tileH &&
2087
159
      tileCacheBaseMatrix[0] == baseMatrix[0] &&
2088
159
      tileCacheBaseMatrix[1] == baseMatrix[1] &&
2089
159
      tileCacheBaseMatrix[2] == baseMatrix[2] &&
2090
159
      tileCacheBaseMatrix[3] == baseMatrix[3] &&
2091
159
      tileCacheBaseMatrix[4] == baseMatrix[4] &&
2092
21
      tileCacheBaseMatrix[5] == baseMatrix[5]) {
2093
21
    tileBitmap = tileCacheBitmap;
2094
21
    overprintMaskBitmap = tileCacheOverprintMaskBitmap;
2095
2096
388
  } else {
2097
388
    if (tileCacheBitmap) {
2098
146
      delete tileCacheBitmap;
2099
146
      tileCacheBitmap = NULL;
2100
146
    }
2101
388
    gfree(tileCacheOverprintMaskBitmap);
2102
388
    tileCacheOverprintMaskBitmap = NULL;
2103
2104
    // create a temporary bitmap
2105
388
    origBitmap = bitmap;
2106
388
    origSplash = splash;
2107
388
    traceMessage("tiling pattern bitmap");
2108
388
    bitmap = tileBitmap = new SplashBitmap(tileW, tileH, bitmapRowPad,
2109
388
             colorMode, gTrue, bitmapTopDown,
2110
388
             bitmapMemCache);
2111
388
    splash = new Splash(bitmap, vectorAntialias,
2112
388
      origSplash->getImageCache(), origSplash->getScreen());
2113
1.94k
    for (i = 0; i < splashMaxColorComps; ++i) {
2114
1.55k
      color[i] = 0;
2115
1.55k
    }
2116
388
    splash->clear(color);
2117
388
#if SPLASH_CMYK
2118
    // if we're doing overprint preview, we need to track the overprint
2119
    // mask at each pixel in the tile bitmap
2120
388
    if (globalParams->getOverprintPreview() &&
2121
0
        colorMode == splashModeCMYK8) {
2122
0
      overprintMaskBitmap =
2123
0
    (Guint *)gmallocn(tileH, tileW * (int)sizeof(Guint));
2124
0
      memset(overprintMaskBitmap, 0, tileH * tileW * sizeof(Guint));
2125
0
      splash->setOverprintMaskBitmap(overprintMaskBitmap);
2126
388
    } else {
2127
388
      overprintMaskBitmap = NULL;
2128
388
    }
2129
#else // SPLASH_CMYK
2130
    overprintMaskBitmap = NULL;
2131
#endif // SPLASH_CMYK
2132
388
    splash->setMinLineWidth(globalParams->getMinLineWidth());
2133
388
    splash->setStrokeAdjust(
2134
388
       mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
2135
388
    splash->setEnablePathSimplification(
2136
388
       globalParams->getEnablePathSimplification());
2137
388
    ++nestCount;
2138
2139
    // copy the fill color (for uncolored tiling patterns)
2140
    // (and stroke color, to handle buggy PDF files)
2141
    // -- Acrobat apparently doesn't copy the full state here
2142
388
    splash->setFillPattern(origSplash->getFillPattern()->copy());
2143
388
    splash->setStrokePattern(origSplash->getStrokePattern()->copy());
2144
2145
    // reset the clip rectangle
2146
388
    state->resetDevClipRect(0, 0, tileW, tileH);
2147
2148
    // render the tile
2149
388
    gfx->drawForm(strRef, resDict, tileMat, bbox);
2150
2151
    // restore the original bitmap
2152
388
    --nestCount;
2153
388
    delete splash;
2154
388
    bitmap = origBitmap;
2155
388
    splash = origSplash;
2156
388
    splash->setOverprintMask(0xffffffff);
2157
2158
388
  }
2159
2160
  // draw the tiles
2161
409
  if (tileW == 1 && tileH == 1 &&
2162
409
      fabs(xStepX * yStepY - xStepY * yStepX) < 0.9) {
2163
    // if the tile is 1x1 pixel, and the stepping completely fills the
2164
    // area, just composite the 1x1 image across the clip region
2165
    // (this avoids performance problems in cases where the step size
2166
    // is very small) (we compare to 0.9 instead of 1.0 to avoid fp
2167
    // jitter issues)
2168
0
    ixMin = (int)floor(clipXMin);
2169
0
    ixMax = (int)floor(clipXMax) + 1;
2170
0
    iyMin = (int)floor(clipYMin);
2171
0
    iyMax = (int)floor(clipYMax) + 1;
2172
0
    for (iy = iyMin; iy < iyMax; ++iy) {
2173
0
      for (ix = ixMin; ix < ixMax; ++ix) {
2174
0
  splash->composite(tileBitmap, NULL, 0, 0, ix, iy, tileW, tileH,
2175
0
        gFalse, gFalse);
2176
0
      }
2177
0
    }
2178
409
  } else {
2179
818
    for (iy = iyMin; iy < iyMax; ++iy) {
2180
818
      for (ix = ixMin; ix < ixMax; ++ix) {
2181
409
  x = (int)floor(adjXMin + ix * xStepX + iy * yStepX + 0.5);
2182
409
  y = (int)floor(adjYMin + ix * xStepY + iy * yStepY + 0.5);
2183
409
  if (overprintMaskBitmap) {
2184
0
    splash->compositeWithOverprint(tileBitmap, NULL, overprintMaskBitmap,
2185
0
           0, 0, x, y, tileW, tileH,
2186
0
           gFalse, gFalse);
2187
409
  } else {
2188
409
    splash->composite(tileBitmap, NULL, 0, 0, x, y, tileW, tileH,
2189
409
          gFalse, gFalse);
2190
409
  }
2191
409
      }
2192
409
    }
2193
409
  }
2194
2195
  // NB: a nested pattern might have been cached
2196
409
  if (tileBitmap != tileCacheBitmap) {
2197
388
    if (paintType == 1 &&   // colored tiling pattern
2198
362
  !clipped) {
2199
170
      tileCacheRef = strRef->getRef();
2200
170
      tileCacheBaseMatrix[0] = baseMatrix[0];
2201
170
      tileCacheBaseMatrix[1] = baseMatrix[1];
2202
170
      tileCacheBaseMatrix[2] = baseMatrix[2];
2203
170
      tileCacheBaseMatrix[3] = baseMatrix[3];
2204
170
      tileCacheBaseMatrix[4] = baseMatrix[4];
2205
170
      tileCacheBaseMatrix[5] = baseMatrix[5];
2206
170
      if (tileCacheBitmap) {
2207
7
  delete tileCacheBitmap;
2208
7
      }
2209
170
      tileCacheBitmap = tileBitmap;
2210
170
      gfree(tileCacheOverprintMaskBitmap);
2211
170
      tileCacheOverprintMaskBitmap = overprintMaskBitmap;
2212
218
    } else {
2213
218
      delete tileBitmap;
2214
218
      gfree(overprintMaskBitmap);
2215
218
    }
2216
388
  }
2217
409
}
2218
2219
4.37k
GBool SplashOutputDev::shadedFill(GfxState *state, GfxShading *shading) {
2220
2221
  // generate the bitmap
2222
4.37k
  SplashColorMode srcMode;
2223
4.37k
  if (colorMode == splashModeMono1) {
2224
0
    srcMode = splashModeMono8;
2225
4.37k
  } else if (colorMode == splashModeBGR8) {
2226
0
    srcMode = splashModeRGB8;
2227
4.37k
  } else {
2228
4.37k
    srcMode = colorMode;
2229
4.37k
  }
2230
4.37k
  int x, y;
2231
4.37k
  SplashBitmap *tBitmap = ShadingImage::generateBitmap(state, shading, srcMode,
2232
4.37k
                   reverseVideo,
2233
4.37k
                   splash, bitmapMemCache,
2234
4.37k
                   &x, &y);
2235
4.37k
  if (!tBitmap) {
2236
    // clip region is empty - nothing to draw
2237
2.64k
    return gTrue;
2238
2.64k
  }
2239
2240
  // check clipping and composite the bitmap
2241
1.72k
  int xMin = x;
2242
1.72k
  int yMin = y;
2243
1.72k
  int xMax = x + tBitmap->getWidth();
2244
1.72k
  int yMax = y + tBitmap->getHeight();
2245
1.72k
  SplashClipResult clipRes = splash->limitRectToClipRect(&xMin, &yMin,
2246
1.72k
               &xMax, &yMax);
2247
1.72k
  if (clipRes != splashClipAllOutside) {
2248
56
    setOverprintMask(state, state->getFillColorSpace(),
2249
56
         state->getFillOverprint(), state->getOverprintMode(),
2250
56
         NULL);
2251
56
    splash->composite(tBitmap, NULL, xMin - x, yMin - y, xMin, yMin,
2252
56
          xMax - xMin, yMax - yMin,
2253
56
          clipRes == splashClipAllInside, gFalse);
2254
56
  }
2255
2256
1.72k
  delete tBitmap;
2257
2258
1.72k
  return gTrue;
2259
4.37k
}
2260
2261
147k
void SplashOutputDev::clip(GfxState *state) {
2262
147k
  SplashPath *path;
2263
2264
147k
  path = convertPath(state, state->getPath(), gTrue);
2265
147k
  splash->clipToPath(path, gFalse);
2266
147k
  delete path;
2267
147k
}
2268
2269
4.45k
void SplashOutputDev::eoClip(GfxState *state) {
2270
4.45k
  SplashPath *path;
2271
2272
4.45k
  path = convertPath(state, state->getPath(), gTrue);
2273
4.45k
  splash->clipToPath(path, gTrue);
2274
4.45k
  delete path;
2275
4.45k
}
2276
2277
538
void SplashOutputDev::clipToStrokePath(GfxState *state) {
2278
538
  SplashPath *path, *path2;
2279
2280
538
  path = convertPath(state, state->getPath(), gFalse);
2281
538
  path2 = splash->makeStrokePath(path, state->getLineWidth(),
2282
538
         state->getLineCap(), state->getLineJoin());
2283
538
  delete path;
2284
538
  splash->clipToPath(path2, gFalse);
2285
538
  delete path2;
2286
538
}
2287
2288
SplashPath *SplashOutputDev::convertPath(GfxState *state, GfxPath *path,
2289
272k
           GBool dropEmptySubpaths) {
2290
272k
  SplashPath *sPath;
2291
272k
  GfxSubpath *subpath;
2292
272k
  int n, i, j;
2293
2294
272k
  n = dropEmptySubpaths ? 1 : 0;
2295
272k
  sPath = new SplashPath();
2296
578k
  for (i = 0; i < path->getNumSubpaths(); ++i) {
2297
305k
    subpath = path->getSubpath(i);
2298
305k
    if (subpath->getNumPoints() > n) {
2299
302k
      sPath->moveTo((SplashCoord)subpath->getX(0),
2300
302k
        (SplashCoord)subpath->getY(0));
2301
302k
      j = 1;
2302
1.39M
      while (j < subpath->getNumPoints()) {
2303
1.09M
  if (subpath->getCurve(j)) {
2304
55.7k
    sPath->curveTo((SplashCoord)subpath->getX(j),
2305
55.7k
       (SplashCoord)subpath->getY(j),
2306
55.7k
       (SplashCoord)subpath->getX(j+1),
2307
55.7k
       (SplashCoord)subpath->getY(j+1),
2308
55.7k
       (SplashCoord)subpath->getX(j+2),
2309
55.7k
       (SplashCoord)subpath->getY(j+2));
2310
55.7k
    j += 3;
2311
1.03M
  } else {
2312
1.03M
    sPath->lineTo((SplashCoord)subpath->getX(j),
2313
1.03M
      (SplashCoord)subpath->getY(j));
2314
1.03M
    ++j;
2315
1.03M
  }
2316
1.09M
      }
2317
302k
      if (subpath->isClosed()) {
2318
252k
  sPath->close();
2319
252k
      }
2320
302k
    }
2321
305k
  }
2322
272k
  return sPath;
2323
272k
}
2324
2325
void SplashOutputDev::drawChar(GfxState *state, double x, double y,
2326
             double dx, double dy,
2327
             double originX, double originY,
2328
             CharCode code, int nBytes,
2329
             Unicode *u, int uLen,
2330
4.50M
             GBool fill, GBool stroke, GBool makePath) {
2331
4.50M
  if (skipHorizText || skipRotatedText) {
2332
0
    double m[4];
2333
0
    state->getFontTransMat(&m[0], &m[1], &m[2], &m[3]);
2334
    // this matches the 'diagonal' test in TextPage::updateFont()
2335
0
    GBool horiz = m[0] > 0 && fabs(m[1]) < 0.001 &&
2336
0
                  fabs(m[2]) < 0.001 && m[3] < 0;
2337
0
    if ((skipHorizText && horiz) || (skipRotatedText && !horiz)) {
2338
0
      return;
2339
0
    }
2340
0
  }
2341
2342
4.50M
  fill = fill && !state->getFillColorSpace()->isNonMarking();
2343
4.50M
  stroke = stroke && !state->getStrokeColorSpace()->isNonMarking();
2344
2345
  // check for invisible text -- this is used by Acrobat Capture
2346
4.50M
  if (!fill && !stroke && !makePath) {
2347
154
    return;
2348
154
  }
2349
2350
4.50M
  if (needFontUpdate) {
2351
281k
    doUpdateFont(state);
2352
281k
  }
2353
4.50M
  if (!font) {
2354
2.33M
    return;
2355
2.33M
  }
2356
2357
2.16M
  x -= originX;
2358
2.16M
  y -= originY;
2359
2360
2.16M
  SplashPath *path = NULL;
2361
2.16M
  if (stroke || makePath) {
2362
1.46M
    if ((path = font->getGlyphPath(code))) {
2363
1.21M
      path->offset((SplashCoord)x, (SplashCoord)y);
2364
1.21M
    }
2365
1.46M
  }
2366
2367
  // don't use stroke adjustment when stroking text -- the results
2368
  // tend to be ugly (because characters with horizontal upper or
2369
  // lower edges get misaligned relative to the other characters)
2370
2.16M
  SplashStrokeAdjustMode savedStrokeAdjust = splashStrokeAdjustOff;
2371
2.16M
  if (stroke) {
2372
5.74k
    savedStrokeAdjust = splash->getStrokeAdjust();
2373
5.74k
    splash->setStrokeAdjust(splashStrokeAdjustOff);
2374
5.74k
  }
2375
2376
  // the possible operations are:
2377
  //   - fill
2378
  //   - stroke
2379
  //   - fill + stroke
2380
  //   - makePath
2381
2382
2.16M
  if (fill && stroke) {
2383
1.86k
    if (path) {
2384
1.23k
      setOverprintMask(state, state->getFillColorSpace(),
2385
1.23k
           state->getFillOverprint(), state->getOverprintMode(),
2386
1.23k
           state->getFillColor());
2387
1.23k
      splash->fill(path, gFalse);
2388
1.23k
      setOverprintMask(state, state->getStrokeColorSpace(),
2389
1.23k
           state->getStrokeOverprint(), state->getOverprintMode(),
2390
1.23k
           state->getStrokeColor());
2391
1.23k
      splash->stroke(path);
2392
1.23k
    }
2393
2394
2.15M
  } else if (fill) {
2395
700k
    setOverprintMask(state, state->getFillColorSpace(),
2396
700k
         state->getFillOverprint(), state->getOverprintMode(),
2397
700k
         state->getFillColor());
2398
700k
    splash->fillChar((SplashCoord)x, (SplashCoord)y, code, font);
2399
2400
1.45M
  } else if (stroke) {
2401
3.87k
    if (path) {
2402
2.30k
      setOverprintMask(state, state->getStrokeColorSpace(),
2403
2.30k
           state->getStrokeOverprint(), state->getOverprintMode(),
2404
2.30k
           state->getStrokeColor());
2405
2.30k
      splash->stroke(path);
2406
2.30k
    }
2407
2408
1.45M
  } else if (makePath) {
2409
1.45M
    if (path) {
2410
1.20M
      if (savedTextPath) {
2411
1.19M
  savedTextPath->append(path);
2412
1.19M
      } else {
2413
9.66k
  savedTextPath = path;
2414
9.66k
  path = NULL;
2415
9.66k
      }
2416
1.20M
    }
2417
1.45M
  }
2418
2419
2.16M
  if (stroke) {
2420
5.74k
    splash->setStrokeAdjust(savedStrokeAdjust);
2421
5.74k
  }
2422
2423
2.16M
  if (path) {
2424
1.20M
    delete path;
2425
1.20M
  }
2426
2.16M
}
2427
2428
419
void SplashOutputDev::fillTextPath(GfxState *state) {
2429
419
  if (!savedTextPath) {
2430
350
    return;
2431
350
  }
2432
69
  setOverprintMask(state, state->getFillColorSpace(),
2433
69
       state->getFillOverprint(), state->getOverprintMode(),
2434
69
       state->getFillColor());
2435
69
  splash->fill(savedTextPath, gFalse);
2436
69
}
2437
2438
4.97k
void SplashOutputDev::strokeTextPath(GfxState *state) {
2439
4.97k
  if (!savedTextPath) {
2440
4.80k
    return;
2441
4.80k
  }
2442
169
  setOverprintMask(state, state->getStrokeColorSpace(),
2443
169
       state->getStrokeOverprint(), state->getOverprintMode(),
2444
169
       state->getStrokeColor());
2445
169
  splash->stroke(savedTextPath);
2446
169
}
2447
2448
23.8k
void SplashOutputDev::clipToTextPath(GfxState *state) {
2449
23.8k
  if (!savedTextPath) {
2450
17.3k
    return;
2451
17.3k
  }
2452
6.49k
  splash->clipToPath(savedTextPath, gFalse);
2453
6.49k
}
2454
2455
30
void SplashOutputDev::clipToTextStrokePath(GfxState *state) {
2456
30
  if (!savedTextPath) {
2457
30
    return;
2458
30
  }
2459
0
  SplashPath *path = splash->makeStrokePath(savedTextPath,
2460
0
              state->getLineWidth(),
2461
0
              state->getLineCap(),
2462
0
              state->getLineJoin());
2463
0
  splash->clipToPath(path, gFalse);
2464
0
  delete path;
2465
0
}
2466
2467
23.8k
void SplashOutputDev::clearTextPath(GfxState *state) {
2468
23.8k
  if (savedTextPath) {
2469
6.49k
    delete savedTextPath;
2470
6.49k
    savedTextPath = NULL;
2471
6.49k
  }
2472
23.8k
}
2473
2474
5.58k
void SplashOutputDev::addTextPathToSavedClipPath(GfxState *state) {
2475
5.58k
  if (savedTextPath) {
2476
3.16k
    if (savedClipPath) {
2477
1.93k
      savedClipPath->append(savedTextPath);
2478
1.93k
      delete savedTextPath;
2479
1.93k
    } else {
2480
1.22k
      savedClipPath = savedTextPath;
2481
1.22k
    }
2482
3.16k
    savedTextPath = NULL;
2483
3.16k
  }
2484
5.58k
}
2485
2486
2.15k
void SplashOutputDev::clipToSavedClipPath(GfxState *state) {
2487
2.15k
  if (!savedClipPath) {
2488
1.03k
    return;
2489
1.03k
  }
2490
1.11k
  splash->clipToPath(savedClipPath, gFalse);
2491
1.11k
  delete savedClipPath;
2492
1.11k
  savedClipPath = NULL;
2493
1.11k
}
2494
2495
GBool SplashOutputDev::beginType3Char(GfxState *state, double x, double y,
2496
              double dx, double dy,
2497
3.25M
              CharCode code, Unicode *u, int uLen) {
2498
3.25M
  GfxFont *gfxFont;
2499
3.25M
  Ref *fontID;
2500
3.25M
  double *ctm, *bbox;
2501
3.25M
  T3FontCache *t3Font;
2502
3.25M
  T3GlyphStack *t3gs;
2503
3.25M
  GBool validBBox;
2504
3.25M
  double m[4];
2505
3.25M
  GBool horiz;
2506
3.25M
  double x1, y1, xMin, yMin, xMax, yMax, xt, yt;
2507
3.25M
  int render, i, j;
2508
2509
3.25M
  if (skipHorizText || skipRotatedText) {
2510
0
    state->getFontTransMat(&m[0], &m[1], &m[2], &m[3]);
2511
0
    horiz = m[0] > 0 && fabs(m[1]) < 0.001 &&
2512
0
            fabs(m[2]) < 0.001 && m[3] < 0;
2513
0
    if ((skipHorizText && horiz) || (skipRotatedText && !horiz)) {
2514
0
      return gTrue;
2515
0
    }
2516
0
  }
2517
2518
  // check for invisible text
2519
3.25M
  render = state->getRender();
2520
3.25M
  if (render == 3 || render == 7) {
2521
7.96k
    return gTrue;
2522
7.96k
  }
2523
2524
3.24M
  if (!(gfxFont = state->getFont())) {
2525
0
    return gTrue;
2526
0
  }
2527
3.24M
  fontID = gfxFont->getID();
2528
3.24M
  ctm = state->getCTM();
2529
3.24M
  state->transform(0, 0, &xt, &yt);
2530
2531
  // is it the first (MRU) font in the cache?
2532
3.24M
  if (!(nT3Fonts > 0 &&
2533
3.24M
  t3FontCache[0]->matches(fontID, ctm[0], ctm[1], ctm[2], ctm[3]))) {
2534
2535
    // is the font elsewhere in the cache?
2536
96.1k
    for (i = 1; i < nT3Fonts; ++i) {
2537
85.3k
      if (t3FontCache[i]->matches(fontID, ctm[0], ctm[1], ctm[2], ctm[3])) {
2538
11.2k
  t3Font = t3FontCache[i];
2539
31.5k
  for (j = i; j > 0; --j) {
2540
20.2k
    t3FontCache[j] = t3FontCache[j - 1];
2541
20.2k
  }
2542
11.2k
  t3FontCache[0] = t3Font;
2543
11.2k
  break;
2544
11.2k
      }
2545
85.3k
    }
2546
22.0k
    if (i >= nT3Fonts) {
2547
2548
      // create new entry in the font cache
2549
10.8k
      if (nT3Fonts < splashOutT3FontCacheSize) {
2550
5.79k
  for (j = nT3Fonts; j > 0; --j) {
2551
3.91k
    t3FontCache[j] = t3FontCache[j - 1];
2552
3.91k
  }
2553
8.92k
      } else {
2554
18.3k
  for (j = nT3Fonts - 1; j >= 0; --j) {
2555
18.3k
    if (t3FontCache[j]->refCount == 0) {
2556
8.92k
      break;
2557
8.92k
    }
2558
18.3k
  }
2559
8.92k
  if (j < 0) {
2560
0
    error(errSyntaxError, -1, "Type 3 fonts nested too deeply");
2561
0
    return gTrue;
2562
0
  }
2563
8.92k
  delete t3FontCache[j];
2564
8.92k
  --nT3Fonts;
2565
61.9k
  for (; j > 0; --j) {
2566
53.0k
    t3FontCache[j] = t3FontCache[j - 1];
2567
53.0k
  }
2568
8.92k
      }
2569
10.8k
      ++nT3Fonts;
2570
10.8k
      bbox = gfxFont->getFontBBox();
2571
10.8k
      if (bbox[0] == 0 && bbox[1] == 0 && bbox[2] == 0 && bbox[3] == 0) {
2572
  // unspecified bounding box -- just take a guess
2573
2.08k
  xMin = xt - 5;
2574
2.08k
  xMax = xMin + 30;
2575
2.08k
  yMax = yt + 15;
2576
2.08k
  yMin = yMax - 45;
2577
2.08k
  validBBox = gFalse;
2578
8.72k
      } else {
2579
8.72k
  state->transform(bbox[0], bbox[1], &x1, &y1);
2580
8.72k
  xMin = xMax = x1;
2581
8.72k
  yMin = yMax = y1;
2582
8.72k
  state->transform(bbox[0], bbox[3], &x1, &y1);
2583
8.72k
  if (x1 < xMin) {
2584
90
    xMin = x1;
2585
8.63k
  } else if (x1 > xMax) {
2586
398
    xMax = x1;
2587
398
  }
2588
8.72k
  if (y1 < yMin) {
2589
844
    yMin = y1;
2590
7.87k
  } else if (y1 > yMax) {
2591
4.00k
    yMax = y1;
2592
4.00k
  }
2593
8.72k
  state->transform(bbox[2], bbox[1], &x1, &y1);
2594
8.72k
  if (x1 < xMin) {
2595
1.96k
    xMin = x1;
2596
6.75k
  } else if (x1 > xMax) {
2597
265
    xMax = x1;
2598
265
  }
2599
8.72k
  if (y1 < yMin) {
2600
687
    yMin = y1;
2601
8.03k
  } else if (y1 > yMax) {
2602
4.03k
    yMax = y1;
2603
4.03k
  }
2604
8.72k
  state->transform(bbox[2], bbox[3], &x1, &y1);
2605
8.72k
  if (x1 < xMin) {
2606
34
    xMin = x1;
2607
8.68k
  } else if (x1 > xMax) {
2608
89
    xMax = x1;
2609
89
  }
2610
8.72k
  if (y1 < yMin) {
2611
614
    yMin = y1;
2612
8.10k
  } else if (y1 > yMax) {
2613
3.44k
    yMax = y1;
2614
3.44k
  }
2615
8.72k
  validBBox = gTrue;
2616
8.72k
      }
2617
10.8k
      t3FontCache[0] = new T3FontCache(fontID, ctm[0], ctm[1], ctm[2], ctm[3],
2618
10.8k
                                 (int)floor(xMin - xt) - 2,
2619
10.8k
               (int)floor(yMin - yt) - 2,
2620
10.8k
               (int)ceil(xMax) - (int)floor(xMin) + 4,
2621
10.8k
               (int)ceil(yMax) - (int)floor(yMin) + 4,
2622
10.8k
               validBBox,
2623
10.8k
               colorMode != splashModeMono1);
2624
10.8k
    }
2625
22.0k
  }
2626
3.24M
  t3Font = t3FontCache[0];
2627
2628
  // is the glyph in the cache?
2629
3.24M
  i = (code & (t3Font->cacheSets - 1)) * t3Font->cacheAssoc;
2630
29.2M
  for (j = 0; j < t3Font->cacheAssoc; ++j) {
2631
25.9M
    if ((t3Font->cacheTags[i+j].mru & 0x8000) &&
2632
15.2k
  t3Font->cacheTags[i+j].code == code) {
2633
3.89k
      drawType3Glyph(state, t3Font, &t3Font->cacheTags[i+j],
2634
3.89k
         t3Font->cacheData + (i+j) * t3Font->glyphSize);
2635
3.89k
      return gTrue;
2636
3.89k
    }
2637
25.9M
  }
2638
2639
3.24M
  if (t3Font->refCount > 1000) {
2640
0
    error(errSyntaxError, -1, "Type 3 CharProcs nested too deeply");
2641
0
    return gTrue;
2642
0
  }
2643
3.24M
  ++t3Font->refCount;
2644
2645
  // push a new Type 3 glyph record
2646
3.24M
  t3gs = new T3GlyphStack();
2647
3.24M
  t3gs->next = t3GlyphStack;
2648
3.24M
  t3GlyphStack = t3gs;
2649
3.24M
  t3GlyphStack->code = (Gushort)code;
2650
3.24M
  t3GlyphStack->cache = t3Font;
2651
3.24M
  t3GlyphStack->cacheTag = NULL;
2652
3.24M
  t3GlyphStack->cacheData = NULL;
2653
3.24M
  t3GlyphStack->haveDx = gFalse;
2654
3.24M
  t3GlyphStack->doNotCache = gFalse;
2655
3.24M
#if 1 //~t3-sa
2656
3.24M
  t3GlyphStack->savedStrokeAdjust = splash->getStrokeAdjust();
2657
3.24M
  splash->setStrokeAdjust(splashStrokeAdjustOff);
2658
3.24M
#endif
2659
2660
3.24M
  return gFalse;
2661
3.24M
}
2662
2663
3.24M
void SplashOutputDev::endType3Char(GfxState *state) {
2664
3.24M
  T3GlyphStack *t3gs;
2665
3.24M
  double *ctm;
2666
2667
3.24M
  if (t3GlyphStack->cacheTag) {
2668
275
    --nestCount;
2669
275
    memcpy(t3GlyphStack->cacheData, bitmap->getDataPtr(),
2670
275
     t3GlyphStack->cache->glyphSize);
2671
275
    delete bitmap;
2672
275
    delete splash;
2673
275
    bitmap = t3GlyphStack->origBitmap;
2674
275
    colorMode = bitmap->getMode();
2675
275
    splash = t3GlyphStack->origSplash;
2676
275
    ctm = state->getCTM();
2677
275
    state->setCTM(ctm[0], ctm[1], ctm[2], ctm[3],
2678
275
      t3GlyphStack->origCTM4, t3GlyphStack->origCTM5);
2679
275
    updateCTM(state, 0, 0, 0, 0, 0, 0);
2680
275
    drawType3Glyph(state, t3GlyphStack->cache,
2681
275
       t3GlyphStack->cacheTag, t3GlyphStack->cacheData);
2682
275
  }
2683
3.24M
#if 1 //~t3-sa
2684
3.24M
  splash->setStrokeAdjust(t3GlyphStack->savedStrokeAdjust);
2685
3.24M
#endif
2686
3.24M
  t3gs = t3GlyphStack;
2687
3.24M
  t3GlyphStack = t3gs->next;
2688
3.24M
  --t3gs->cache->refCount;
2689
3.24M
  delete t3gs;
2690
3.24M
}
2691
2692
10.3k
void SplashOutputDev::type3D0(GfxState *state, double wx, double wy) {
2693
10.3k
  if (!t3GlyphStack) {
2694
8.32k
    error(errSyntaxError, -1,
2695
8.32k
    "Encountered d0 operator outside of Type 3 CharProc");
2696
8.32k
    return;
2697
8.32k
  }
2698
2.03k
  t3GlyphStack->haveDx = gTrue;
2699
2.03k
}
2700
2701
void SplashOutputDev::type3D1(GfxState *state, double wx, double wy,
2702
9.94k
            double llx, double lly, double urx, double ury) {
2703
9.94k
  double *ctm;
2704
9.94k
  T3FontCache *t3Font;
2705
9.94k
  SplashColor color;
2706
9.94k
  double xt, yt, xMin, xMax, yMin, yMax, x1, y1;
2707
9.94k
  int i, j;
2708
2709
9.94k
  if (!t3GlyphStack) {
2710
636
    error(errSyntaxError, -1,
2711
636
    "Encountered d1 operator outside of Type 3 CharProc");
2712
636
    return;
2713
636
  }
2714
2715
  // ignore multiple d0/d1 operators
2716
9.30k
  if (t3GlyphStack->haveDx) {
2717
1.67k
    return;
2718
1.67k
  }
2719
7.63k
  t3GlyphStack->haveDx = gTrue;
2720
  // don't cache if we got a gsave/grestore before the d1
2721
7.63k
  if (t3GlyphStack->doNotCache) {
2722
415
    return;
2723
415
  }
2724
2725
7.21k
  t3Font = t3GlyphStack->cache;
2726
2727
  // check for a valid bbox
2728
7.21k
  state->transform(0, 0, &xt, &yt);
2729
7.21k
  state->transform(llx, lly, &x1, &y1);
2730
7.21k
  xMin = xMax = x1;
2731
7.21k
  yMin = yMax = y1;
2732
7.21k
  state->transform(llx, ury, &x1, &y1);
2733
7.21k
  if (x1 < xMin) {
2734
897
    xMin = x1;
2735
6.32k
  } else if (x1 > xMax) {
2736
122
    xMax = x1;
2737
122
  }
2738
7.21k
  if (y1 < yMin) {
2739
1.73k
    yMin = y1;
2740
5.48k
  } else if (y1 > yMax) {
2741
2.66k
    yMax = y1;
2742
2.66k
  }
2743
7.21k
  state->transform(urx, lly, &x1, &y1);
2744
7.21k
  if (x1 < xMin) {
2745
3.73k
    xMin = x1;
2746
3.73k
  } else if (x1 > xMax) {
2747
716
    xMax = x1;
2748
716
  }
2749
7.21k
  if (y1 < yMin) {
2750
608
    yMin = y1;
2751
6.61k
  } else if (y1 > yMax) {
2752
563
    yMax = y1;
2753
563
  }
2754
7.21k
  state->transform(urx, ury, &x1, &y1);
2755
7.21k
  if (x1 < xMin) {
2756
658
    xMin = x1;
2757
6.56k
  } else if (x1 > xMax) {
2758
78
    xMax = x1;
2759
78
  }
2760
7.21k
  if (y1 < yMin) {
2761
665
    yMin = y1;
2762
6.55k
  } else if (y1 > yMax) {
2763
234
    yMax = y1;
2764
234
  }
2765
7.21k
  if (xMin - xt < t3Font->glyphX ||
2766
2.48k
      yMin - yt < t3Font->glyphY ||
2767
1.83k
      xMax - xt > t3Font->glyphX + t3Font->glyphW ||
2768
6.94k
      yMax - yt > t3Font->glyphY + t3Font->glyphH) {
2769
6.94k
    if (t3Font->validBBox) {
2770
1.74k
      error(errSyntaxWarning, -1, "Bad bounding box in Type 3 glyph");
2771
1.74k
    }
2772
6.94k
    return;
2773
6.94k
  }
2774
2775
  // allocate a cache entry
2776
275
  i = (t3GlyphStack->code & (t3Font->cacheSets - 1)) * t3Font->cacheAssoc;
2777
2.47k
  for (j = 0; j < t3Font->cacheAssoc; ++j) {
2778
2.20k
    if ((t3Font->cacheTags[i+j].mru & 0x7fff) == t3Font->cacheAssoc - 1) {
2779
275
      t3Font->cacheTags[i+j].mru = 0x8000;
2780
275
      t3Font->cacheTags[i+j].code = t3GlyphStack->code;
2781
275
      t3GlyphStack->cacheTag = &t3Font->cacheTags[i+j];
2782
275
      t3GlyphStack->cacheData = t3Font->cacheData + (i+j) * t3Font->glyphSize;
2783
1.92k
    } else {
2784
1.92k
      ++t3Font->cacheTags[i+j].mru;
2785
1.92k
    }
2786
2.20k
  }
2787
2788
  // save state
2789
275
  t3GlyphStack->origBitmap = bitmap;
2790
275
  t3GlyphStack->origSplash = splash;
2791
275
  ctm = state->getCTM();
2792
275
  t3GlyphStack->origCTM4 = ctm[4];
2793
275
  t3GlyphStack->origCTM5 = ctm[5];
2794
2795
  // create the temporary bitmap
2796
275
  if (colorMode == splashModeMono1) {
2797
0
    colorMode = splashModeMono1;
2798
0
    traceMessage("T3 glyph bitmap");
2799
0
    bitmap = new SplashBitmap(t3Font->glyphW, t3Font->glyphH, 1,
2800
0
            splashModeMono1, gFalse, gTrue, bitmapMemCache);
2801
0
    splash = new Splash(bitmap, gFalse,
2802
0
      t3GlyphStack->origSplash->getImageCache(), 
2803
0
      t3GlyphStack->origSplash->getScreen());
2804
0
    color[0] = 0;
2805
0
    splash->clear(color);
2806
0
    color[0] = 0xff;
2807
275
  } else {
2808
275
    colorMode = splashModeMono8;
2809
275
    traceMessage("T3 glyph bitmap");
2810
275
    bitmap = new SplashBitmap(t3Font->glyphW, t3Font->glyphH, 1,
2811
275
            splashModeMono8, gFalse, gTrue, bitmapMemCache);
2812
275
    splash = new Splash(bitmap, vectorAntialias,
2813
275
      t3GlyphStack->origSplash->getImageCache(), 
2814
275
      t3GlyphStack->origSplash->getScreen());
2815
275
    color[0] = 0x00;
2816
275
    splash->clear(color);
2817
275
    color[0] = 0xff;
2818
275
  }
2819
275
  splash->setMinLineWidth(globalParams->getMinLineWidth());
2820
275
  splash->setStrokeAdjust(t3GlyphStack->origSplash->getStrokeAdjust());
2821
275
  splash->setEnablePathSimplification(
2822
275
     globalParams->getEnablePathSimplification());
2823
275
  copyState(t3GlyphStack->origSplash, gFalse);
2824
275
  splash->setFillPattern(new SplashSolidColor(color));
2825
275
  splash->setStrokePattern(new SplashSolidColor(color));
2826
275
  state->setCTM(ctm[0], ctm[1], ctm[2], ctm[3],
2827
275
    -t3Font->glyphX, -t3Font->glyphY);
2828
275
  updateCTM(state, 0, 0, 0, 0, 0, 0);
2829
275
  ++nestCount;
2830
275
}
2831
2832
void SplashOutputDev::drawType3Glyph(GfxState *state, T3FontCache *t3Font,
2833
4.16k
             T3FontCacheTag *tag, Guchar *data) {
2834
4.16k
  SplashGlyphBitmap glyph;
2835
2836
4.16k
  setOverprintMask(state, state->getFillColorSpace(),
2837
4.16k
       state->getFillOverprint(), state->getOverprintMode(),
2838
4.16k
       state->getFillColor());
2839
4.16k
  glyph.x = -t3Font->glyphX;
2840
4.16k
  glyph.y = -t3Font->glyphY;
2841
4.16k
  glyph.w = t3Font->glyphW;
2842
4.16k
  glyph.h = t3Font->glyphH;
2843
4.16k
  glyph.aa = colorMode != splashModeMono1;
2844
4.16k
  glyph.data = data;
2845
4.16k
  glyph.freeData = gFalse;
2846
4.16k
  splash->fillGlyph(0, 0, &glyph);
2847
4.16k
}
2848
2849
341k
void SplashOutputDev::endTextObject(GfxState *state) {
2850
341k
}
2851
2852
struct SplashOutImageMaskData {
2853
  ImageStream *imgStr;
2854
  Guchar invert;
2855
  int width, height, y;
2856
};
2857
2858
3.87M
GBool SplashOutputDev::imageMaskSrc(void *data, Guchar *line) {
2859
3.87M
  SplashOutImageMaskData *imgMaskData = (SplashOutImageMaskData *)data;
2860
3.87M
  Guchar *p;
2861
3.87M
  SplashColorPtr q;
2862
3.87M
  int x;
2863
2864
3.87M
  if (imgMaskData->y == imgMaskData->height ||
2865
3.87M
      !(p = imgMaskData->imgStr->getLine())) {
2866
3.81M
    memset(line, 0, imgMaskData->width);
2867
3.81M
    return gFalse;
2868
3.81M
  }
2869
779k
  for (x = 0, q = line; x < imgMaskData->width; ++x) {
2870
723k
    *q++ = *p++ ^ imgMaskData->invert;
2871
723k
  }
2872
55.6k
  ++imgMaskData->y;
2873
55.6k
  return gTrue;
2874
3.87M
}
2875
2876
void SplashOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
2877
            int width, int height, GBool invert,
2878
26.7k
            GBool inlineImg, GBool interpolate) {
2879
26.7k
  double *ctm;
2880
26.7k
  SplashCoord mat[6];
2881
26.7k
  SplashOutImageMaskData imgMaskData;
2882
26.7k
  GString *imgTag;
2883
2884
26.7k
  if (state->getFillColorSpace()->isNonMarking()) {
2885
0
    return;
2886
0
  }
2887
26.7k
  setOverprintMask(state, state->getFillColorSpace(),
2888
26.7k
       state->getFillOverprint(), state->getOverprintMode(),
2889
26.7k
       state->getFillColor());
2890
2891
26.7k
  ctm = state->getCTM();
2892
26.7k
  mat[0] = ctm[0];
2893
26.7k
  mat[1] = ctm[1];
2894
26.7k
  mat[2] = -ctm[2];
2895
26.7k
  mat[3] = -ctm[3];
2896
26.7k
  mat[4] = ctm[2] + ctm[4];
2897
26.7k
  mat[5] = ctm[3] + ctm[5];
2898
2899
26.7k
  reduceImageResolution(str, ctm, &width, &height);
2900
2901
26.7k
  imgMaskData.imgStr = new ImageStream(str, width, 1, 1);
2902
26.7k
  imgMaskData.imgStr->reset();
2903
26.7k
  imgMaskData.invert = invert ? 0 : 1;
2904
26.7k
  imgMaskData.width = width;
2905
26.7k
  imgMaskData.height = height;
2906
26.7k
  imgMaskData.y = 0;
2907
2908
26.7k
  imgTag = makeImageTag(ref, gfxRenderingIntentRelativeColorimetric, NULL);
2909
26.7k
  splash->fillImageMask(imgTag,
2910
26.7k
      &imageMaskSrc, &imgMaskData, width, height, mat,
2911
26.7k
      t3GlyphStack != NULL, interpolate,
2912
26.7k
      globalParams->getImageMaskAntialias());
2913
2914
26.7k
  if (inlineImg) {
2915
4.55M
    while (imgMaskData.y < height) {
2916
4.53M
      imgMaskData.imgStr->getLine();
2917
4.53M
      ++imgMaskData.y;
2918
4.53M
    }
2919
26.6k
  }
2920
2921
26.7k
  delete imgTag;
2922
26.7k
  delete imgMaskData.imgStr;
2923
26.7k
  str->close();
2924
26.7k
}
2925
2926
void SplashOutputDev::setSoftMaskFromImageMask(GfxState *state,
2927
                 Object *ref, Stream *str,
2928
                 int width, int height,
2929
                 GBool invert,
2930
                 GBool inlineImg,
2931
152
                 GBool interpolate) {
2932
152
  double *ctm;
2933
152
  SplashCoord mat[6];
2934
152
  SplashOutImageMaskData imgMaskData;
2935
152
  SplashBitmap *maskBitmap;
2936
152
  Splash *maskSplash;
2937
152
  SplashColor maskColor;
2938
152
  GString *imgTag;
2939
2940
152
  ctm = state->getCTM();
2941
152
  mat[0] = ctm[0];
2942
152
  mat[1] = ctm[1];
2943
152
  mat[2] = -ctm[2];
2944
152
  mat[3] = -ctm[3];
2945
152
  mat[4] = ctm[2] + ctm[4];
2946
152
  mat[5] = ctm[3] + ctm[5];
2947
152
  reduceImageResolution(str, ctm, &width, &height);
2948
152
  imgMaskData.imgStr = new ImageStream(str, width, 1, 1);
2949
152
  imgMaskData.imgStr->reset();
2950
152
  imgMaskData.invert = invert ? 0 : 1;
2951
152
  imgMaskData.width = width;
2952
152
  imgMaskData.height = height;
2953
152
  imgMaskData.y = 0;
2954
152
  traceMessage("image mask soft mask bitmap");
2955
152
  maskBitmap = new SplashBitmap(bitmap->getWidth(), bitmap->getHeight(),
2956
152
        1, splashModeMono8, gFalse, gTrue,
2957
152
        bitmapMemCache);
2958
152
  maskSplash = new Splash(maskBitmap, gTrue, splash->getImageCache());
2959
152
  maskSplash->setStrokeAdjust(
2960
152
         mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
2961
152
  maskSplash->setEnablePathSimplification(
2962
152
         globalParams->getEnablePathSimplification());
2963
152
  if (splash->getSoftMask()) {
2964
0
    maskSplash->setSoftMask(splash->getSoftMask(), gFalse);
2965
0
  }
2966
152
  clearMaskRegion(state, maskSplash, 0, 0, 1, 1);
2967
152
  maskColor[0] = 0xff;
2968
152
  maskSplash->setFillPattern(new SplashSolidColor(maskColor));
2969
152
  imgTag = makeImageTag(ref, gfxRenderingIntentRelativeColorimetric, NULL);
2970
152
  maskSplash->fillImageMask(imgTag, &imageMaskSrc, &imgMaskData,
2971
152
          width, height, mat, gFalse, interpolate,
2972
152
          globalParams->getImageMaskAntialias());
2973
152
  delete imgTag;
2974
152
  delete imgMaskData.imgStr;
2975
152
  str->close();
2976
152
  delete maskSplash;
2977
152
  splash->setSoftMask(maskBitmap);
2978
152
}
2979
2980
struct SplashOutImageData {
2981
  ImageStream *imgStr;
2982
  GfxImageColorMap *colorMap;
2983
  GfxRenderingIntent ri;
2984
  SplashColorPtr lookup;
2985
  int *maskColors;
2986
  SplashColorMode colorMode;
2987
  GBool invert;
2988
  int width, height, y;
2989
};
2990
2991
GBool SplashOutputDev::imageSrc(void *data, SplashColorPtr colorLine,
2992
47.9k
        Guchar *alphaLine) {
2993
47.9k
  SplashOutImageData *imgData = (SplashOutImageData *)data;
2994
2995
47.9k
  Guchar *p;
2996
47.9k
  if (imgData->y == imgData->height ||
2997
47.9k
      !(p = imgData->imgStr->getLine())) {
2998
41.2k
    memset(colorLine, 0,
2999
41.2k
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3000
41.2k
    return gFalse;
3001
41.2k
  }
3002
3003
6.67k
  int x;
3004
6.67k
  switch (imgData->colorMode) {
3005
0
  case splashModeMono1:
3006
0
  case splashModeMono8:
3007
0
    imgData->colorMap->getGrayByteLine(p, colorLine, imgData->width,
3008
0
               imgData->ri);
3009
0
    break;
3010
6.67k
  case splashModeRGB8:
3011
6.67k
  case splashModeBGR8:
3012
6.67k
    imgData->colorMap->getRGBByteLine(p, colorLine, imgData->width,
3013
6.67k
              imgData->ri);
3014
6.67k
    break;
3015
0
#if SPLASH_CMYK
3016
0
  case splashModeCMYK8:
3017
0
    imgData->colorMap->getCMYKByteLine(p, colorLine, imgData->width,
3018
0
               imgData->ri);
3019
0
    break;
3020
6.67k
#endif
3021
6.67k
  }
3022
3023
6.67k
  if (imgData->invert) {
3024
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3025
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3026
0
      *p ^= 0xff;
3027
0
    }
3028
0
  }
3029
3030
6.67k
  ++imgData->y;
3031
6.67k
  return gTrue;
3032
6.67k
}
3033
3034
GBool SplashOutputDev::imageLookup1Src(void *data, SplashColorPtr colorLine,
3035
179k
               Guchar *alphaLine) {
3036
179k
  SplashOutImageData *imgData = (SplashOutImageData *)data;
3037
3038
179k
  Guchar *p;
3039
179k
  if (imgData->y == imgData->height ||
3040
179k
      !(p = imgData->imgStr->getLine())) {
3041
70.2k
    memset(colorLine, 0,
3042
70.2k
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3043
70.2k
    return gFalse;
3044
70.2k
  }
3045
3046
109k
  int x;
3047
109k
  SplashColorPtr q, col;
3048
109k
  switch (imgData->colorMode) {
3049
0
  case splashModeMono1:
3050
62.8k
  case splashModeMono8:
3051
10.8M
    for (x = 0, q = colorLine; x < imgData->width; ++x, ++p) {
3052
10.8M
      *q++ = imgData->lookup[*p];
3053
10.8M
    }
3054
62.8k
    break;
3055
45.9k
  case splashModeRGB8:
3056
45.9k
  case splashModeBGR8:
3057
2.90M
    for (x = 0, q = colorLine; x < imgData->width; ++x, ++p) {
3058
2.85M
      col = &imgData->lookup[3 * *p];
3059
2.85M
      *q++ = col[0];
3060
2.85M
      *q++ = col[1];
3061
2.85M
      *q++ = col[2];
3062
2.85M
    }
3063
45.9k
    break;
3064
0
#if SPLASH_CMYK
3065
541
  case splashModeCMYK8:
3066
39.1k
    for (x = 0, q = colorLine; x < imgData->width; ++x, ++p) {
3067
38.6k
      col = &imgData->lookup[4 * *p];
3068
38.6k
      *q++ = col[0];
3069
38.6k
      *q++ = col[1];
3070
38.6k
      *q++ = col[2];
3071
38.6k
      *q++ = col[3];
3072
38.6k
    }
3073
541
    break;
3074
109k
#endif
3075
109k
  }
3076
3077
109k
  if (imgData->invert) {
3078
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3079
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3080
0
      *p ^= 0xff;
3081
0
    }
3082
0
  }
3083
3084
109k
  ++imgData->y;
3085
109k
  return gTrue;
3086
109k
}
3087
3088
GBool SplashOutputDev::imageLookup2Src(void *data, SplashColorPtr colorLine,
3089
0
               Guchar *alphaLine) {
3090
0
  SplashOutImageData *imgData = (SplashOutImageData *)data;
3091
3092
0
  Guchar *p;
3093
0
  if (imgData->y == imgData->height ||
3094
0
      !(p = imgData->imgStr->getLine())) {
3095
0
    memset(colorLine, 0,
3096
0
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3097
0
    return gFalse;
3098
0
  }
3099
3100
0
  int bpc = imgData->colorMap->getBits();
3101
0
  if (bpc > 8) {
3102
0
    bpc = 8;
3103
0
  }
3104
0
  int x;
3105
0
  SplashColorPtr q, col;
3106
0
  switch (imgData->colorMode) {
3107
0
  case splashModeMono1:
3108
0
  case splashModeMono8:
3109
0
    for (x = 0, q = colorLine; x < imgData->width; ++x, p += 2) {
3110
0
      int k = (p[0] << bpc) + p[1];
3111
0
      *q++ = imgData->lookup[k];
3112
0
    }
3113
0
    break;
3114
0
  case splashModeRGB8:
3115
0
  case splashModeBGR8:
3116
0
    for (x = 0, q = colorLine; x < imgData->width; ++x, p += 2) {
3117
0
      int k = (p[0] << bpc) + p[1];
3118
0
      col = &imgData->lookup[3 * k];
3119
0
      *q++ = col[0];
3120
0
      *q++ = col[1];
3121
0
      *q++ = col[2];
3122
0
    }
3123
0
    break;
3124
0
#if SPLASH_CMYK
3125
0
  case splashModeCMYK8:
3126
0
    for (x = 0, q = colorLine; x < imgData->width; ++x, p += 2) {
3127
0
      int k = (p[0] << bpc) + p[1];
3128
0
      col = &imgData->lookup[4 * k];
3129
0
      *q++ = col[0];
3130
0
      *q++ = col[1];
3131
0
      *q++ = col[2];
3132
0
      *q++ = col[3];
3133
0
    }
3134
0
    break;
3135
0
#endif
3136
0
  }
3137
3138
0
  if (imgData->invert) {
3139
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3140
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3141
0
      *p ^= 0xff;
3142
0
    }
3143
0
  }
3144
3145
0
  ++imgData->y;
3146
0
  return gTrue;
3147
0
}
3148
3149
GBool SplashOutputDev::alphaImageSrc(void *data, SplashColorPtr colorLine,
3150
1.22k
             Guchar *alphaLine) {
3151
1.22k
  SplashOutImageData *imgData = (SplashOutImageData *)data;
3152
3153
1.22k
  Guchar *p0;
3154
1.22k
  if (imgData->y == imgData->height ||
3155
1.22k
      !(p0 = imgData->imgStr->getLine())) {
3156
709
    memset(colorLine, 0,
3157
709
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3158
709
    memset(alphaLine, 0, imgData->width);
3159
709
    return gFalse;
3160
709
  }
3161
3162
513
  int nComps = imgData->colorMap->getNumPixelComps();
3163
3164
513
  int x;
3165
513
  Guchar *p;
3166
513
  switch (imgData->colorMode) {
3167
0
  case splashModeMono1:
3168
0
  case splashModeMono8:
3169
0
    imgData->colorMap->getGrayByteLine(p0, colorLine, imgData->width,
3170
0
               imgData->ri);
3171
0
    break;
3172
513
  case splashModeRGB8:
3173
513
  case splashModeBGR8:
3174
513
    imgData->colorMap->getRGBByteLine(p0, colorLine, imgData->width,
3175
513
              imgData->ri);
3176
513
    break;
3177
0
#if SPLASH_CMYK
3178
0
  case splashModeCMYK8:
3179
0
    imgData->colorMap->getCMYKByteLine(p0, colorLine, imgData->width,
3180
0
               imgData->ri);
3181
0
    break;
3182
513
#endif
3183
513
  }
3184
3185
513
  Guchar *aq;
3186
3.66k
  for (x = 0, p = p0, aq = alphaLine; x < imgData->width; ++x, p += nComps) {
3187
3.15k
    Guchar alpha = 0;
3188
3.15k
    for (int i = 0; i < nComps; ++i) {
3189
3.15k
      if (p[i] < imgData->maskColors[2*i] ||
3190
3.15k
    p[i] > imgData->maskColors[2*i+1]) {
3191
3.15k
  alpha = 0xff;
3192
3.15k
  break;
3193
3.15k
      }
3194
3.15k
    }
3195
3.15k
    *aq++ = alpha;
3196
3.15k
  }
3197
3198
513
  if (imgData->invert) {
3199
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3200
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3201
0
      *p ^= 0xff;
3202
0
    }
3203
0
  }
3204
3205
513
  ++imgData->y;
3206
513
  return gTrue;
3207
513
}
3208
3209
GBool SplashOutputDev::alphaImageLookup1Src(void *data,
3210
              SplashColorPtr colorLine,
3211
12.8k
              Guchar *alphaLine) {
3212
12.8k
  SplashOutImageData *imgData = (SplashOutImageData *)data;
3213
3214
12.8k
  Guchar *p0;
3215
12.8k
  if (imgData->y == imgData->height ||
3216
12.8k
      !(p0 = imgData->imgStr->getLine())) {
3217
2.87k
    memset(colorLine, 0,
3218
2.87k
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3219
2.87k
    memset(alphaLine, 0, imgData->width);
3220
2.87k
    return gFalse;
3221
2.87k
  }
3222
3223
9.92k
  int nComps = imgData->colorMap->getNumPixelComps();
3224
3225
9.92k
  int x;
3226
9.92k
  Guchar *p;
3227
9.92k
  SplashColorPtr q, col;
3228
9.92k
  switch (imgData->colorMode) {
3229
0
  case splashModeMono1:
3230
0
  case splashModeMono8:
3231
0
    for (x = 0, p = p0, q = colorLine; x < imgData->width; ++x, ++p) {
3232
0
      *q++ = imgData->lookup[*p];
3233
0
    }
3234
0
    break;
3235
9.92k
  case splashModeRGB8:
3236
9.92k
  case splashModeBGR8:
3237
48.1k
    for (x = 0, p = p0, q = colorLine; x < imgData->width; ++x, ++p) {
3238
38.1k
      col = &imgData->lookup[3 * *p];
3239
38.1k
      *q++ = col[0];
3240
38.1k
      *q++ = col[1];
3241
38.1k
      *q++ = col[2];
3242
38.1k
    }
3243
9.92k
    break;
3244
0
#if SPLASH_CMYK
3245
0
  case splashModeCMYK8:
3246
0
    for (x = 0, p = p0, q = colorLine; x < imgData->width; ++x, ++p) {
3247
0
      col = &imgData->lookup[4 * *p];
3248
0
      *q++ = col[0];
3249
0
      *q++ = col[1];
3250
0
      *q++ = col[2];
3251
0
      *q++ = col[3];
3252
0
    }
3253
0
    break;
3254
9.92k
#endif
3255
9.92k
  }
3256
3257
9.92k
  Guchar *aq;
3258
48.1k
  for (x = 0, p = p0, aq = alphaLine; x < imgData->width; ++x, p += nComps) {
3259
38.1k
    Guchar alpha = 0;
3260
38.8k
    for (int i = 0; i < nComps; ++i) {
3261
38.1k
      if (p[i] < imgData->maskColors[2*i] ||
3262
37.5k
    p[i] > imgData->maskColors[2*i+1]) {
3263
37.5k
  alpha = 0xff;
3264
37.5k
  break;
3265
37.5k
      }
3266
38.1k
    }
3267
38.1k
    *aq++ = alpha;
3268
38.1k
  }
3269
3270
9.92k
  if (imgData->invert) {
3271
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3272
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3273
0
      *p ^= 0xff;
3274
0
    }
3275
0
  }
3276
3277
9.92k
  ++imgData->y;
3278
9.92k
  return gTrue;
3279
9.92k
}
3280
3281
GBool SplashOutputDev::alphaImageLookup2Src(void *data,
3282
              SplashColorPtr colorLine,
3283
0
              Guchar *alphaLine) {
3284
0
  SplashOutImageData *imgData = (SplashOutImageData *)data;
3285
3286
0
  Guchar *p0;
3287
0
  if (imgData->y == imgData->height ||
3288
0
      !(p0 = imgData->imgStr->getLine())) {
3289
0
    memset(colorLine, 0,
3290
0
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3291
0
    memset(alphaLine, 0, imgData->width);
3292
0
    return gFalse;
3293
0
  }
3294
3295
0
  int nComps = imgData->colorMap->getNumPixelComps();
3296
3297
0
  int bpc = imgData->colorMap->getBits();
3298
0
  if (bpc > 8) {
3299
0
    bpc = 8;
3300
0
  }
3301
0
  int x;
3302
0
  Guchar *p;
3303
0
  SplashColorPtr q, col;
3304
0
  switch (imgData->colorMode) {
3305
0
  case splashModeMono1:
3306
0
  case splashModeMono8:
3307
0
    for (x = 0, p = p0, q = colorLine; x < imgData->width; ++x, p += 2) {
3308
0
      int k = (p[0] << bpc) + p[1];
3309
0
      *q++ = imgData->lookup[k];
3310
0
    }
3311
0
    break;
3312
0
  case splashModeRGB8:
3313
0
  case splashModeBGR8:
3314
0
    for (x = 0, p = p0, q = colorLine; x < imgData->width; ++x, p += 2) {
3315
0
      int k = (p[0] << bpc) + p[1];
3316
0
      col = &imgData->lookup[3 * k];
3317
0
      *q++ = col[0];
3318
0
      *q++ = col[1];
3319
0
      *q++ = col[2];
3320
0
    }
3321
0
    break;
3322
0
#if SPLASH_CMYK
3323
0
  case splashModeCMYK8:
3324
0
    for (x = 0, p = p0, q = colorLine; x < imgData->width; ++x, p += 2) {
3325
0
      int k = (p[0] << bpc) + p[1];
3326
0
      col = &imgData->lookup[4 * k];
3327
0
      *q++ = col[0];
3328
0
      *q++ = col[1];
3329
0
      *q++ = col[2];
3330
0
      *q++ = col[3];
3331
0
    }
3332
0
    break;
3333
0
#endif
3334
0
  }
3335
3336
0
  Guchar *aq;
3337
0
  for (x = 0, p = p0, aq = alphaLine; x < imgData->width; ++x, p += nComps) {
3338
0
    Guchar alpha = 0;
3339
0
    for (int i = 0; i < nComps; ++i) {
3340
0
      if (p[i] < imgData->maskColors[2*i] ||
3341
0
    p[i] > imgData->maskColors[2*i+1]) {
3342
0
  alpha = 0xff;
3343
0
  break;
3344
0
      }
3345
0
    }
3346
0
    *aq++ = alpha;
3347
0
  }
3348
3349
0
  if (imgData->invert) {
3350
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3351
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3352
0
      *p ^= 0xff;
3353
0
    }
3354
0
  }
3355
3356
0
  ++imgData->y;
3357
0
  return gTrue;
3358
0
}
3359
3360
3361
void SplashOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
3362
        int width, int height,
3363
        GfxImageColorMap *colorMap,
3364
        int *maskColors, GBool inlineImg,
3365
24.1k
        GBool interpolate) {
3366
3367
24.1k
  setOverprintMask(state, colorMap->getColorSpace(),
3368
24.1k
       state->getFillOverprint(), state->getOverprintMode(),
3369
24.1k
       NULL);
3370
3371
24.1k
  double *ctm = state->getCTM();
3372
24.1k
  SplashCoord mat[6];
3373
24.1k
  mat[0] = ctm[0];
3374
24.1k
  mat[1] = ctm[1];
3375
24.1k
  mat[2] = -ctm[2];
3376
24.1k
  mat[3] = -ctm[3];
3377
24.1k
  mat[4] = ctm[2] + ctm[4];
3378
24.1k
  mat[5] = ctm[3] + ctm[5];
3379
3380
24.1k
  reduceImageResolution(str, ctm, &width, &height);
3381
3382
24.1k
  SplashOutImageData imgData;
3383
24.1k
  imgData.imgStr = new ImageStream(str, width,
3384
24.1k
           colorMap->getNumPixelComps(),
3385
24.1k
           colorMap->getBits());
3386
24.1k
  imgData.imgStr->reset();
3387
24.1k
  imgData.colorMap = colorMap;
3388
24.1k
  imgData.ri = state->getRenderingIntent();
3389
24.1k
  imgData.maskColors = maskColors;
3390
24.1k
  imgData.colorMode = colorMode;
3391
24.1k
  imgData.invert = reverseVideo && reverseVideoInvertImages;
3392
24.1k
  imgData.width = width;
3393
24.1k
  imgData.height = height;
3394
24.1k
  imgData.y = 0;
3395
3396
  // special case for one-channel (monochrome/gray/separation) images:
3397
  // build a lookup table here
3398
24.1k
  SplashImageSource src = maskColors ? &alphaImageSrc : &imageSrc;
3399
24.1k
  imgData.lookup = NULL;
3400
24.1k
  if (colorMap->getNumPixelComps() == 1) {
3401
20.8k
    imgData.lookup = buildColorMapLookupTable1Idx(colorMap, state);
3402
20.8k
    if (imgData.lookup) {
3403
20.8k
      src = maskColors ? &alphaImageLookup1Src : &imageLookup1Src;
3404
20.8k
    }
3405
20.8k
  } else if (colorMap->getNumPixelComps() == 2 &&
3406
0
       height > 65536 / width) {
3407
0
    imgData.lookup = buildColorMapLookupTable2Idx(colorMap, state);
3408
0
    if (imgData.lookup) {
3409
0
      src = maskColors ? &alphaImageLookup2Src : &imageLookup2Src;
3410
0
    }
3411
0
  }
3412
3413
24.1k
  SplashColorMode srcMode;
3414
24.1k
  if (colorMode == splashModeMono1) {
3415
0
    srcMode = splashModeMono8;
3416
24.1k
  } else if (colorMode == splashModeBGR8) {
3417
0
    srcMode = splashModeRGB8;
3418
24.1k
  } else {
3419
24.1k
    srcMode = colorMode;
3420
24.1k
  }
3421
3422
24.1k
  GString *imgTag = makeImageTag(ref, state->getRenderingIntent(),
3423
24.1k
         colorMap->getColorSpace());
3424
24.1k
  splash->drawImage(imgTag,
3425
24.1k
        src, &imgData, srcMode, maskColors ? gTrue : gFalse,
3426
24.1k
        width, height, mat, interpolate);
3427
24.1k
  if (inlineImg) {
3428
705k
    while (imgData.y < height) {
3429
686k
      imgData.imgStr->getLine();
3430
686k
      ++imgData.y;
3431
686k
    }
3432
19.7k
  }
3433
24.1k
  delete imgTag;
3434
3435
24.1k
  gfree(imgData.lookup);
3436
24.1k
  delete imgData.imgStr;
3437
24.1k
  str->close();
3438
24.1k
}
3439
3440
3441
struct SplashOutMaskedImageData {
3442
  ImageStream *imgStr;
3443
  GfxImageColorMap *colorMap;
3444
  GfxRenderingIntent ri;
3445
  SplashBitmap *mask;
3446
  SplashColorPtr lookup;
3447
  SplashColorMode colorMode;
3448
  GBool invert;
3449
  int width, height, y;
3450
};
3451
3452
GBool SplashOutputDev::maskedImageSrc(void *data, SplashColorPtr colorLine,
3453
0
              Guchar *alphaLine) {
3454
0
  SplashOutMaskedImageData *imgData = (SplashOutMaskedImageData *)data;
3455
3456
0
  Guchar *p;
3457
0
  if (imgData->y == imgData->height ||
3458
0
      !(p = imgData->imgStr->getLine())) {
3459
0
    memset(colorLine, 0,
3460
0
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3461
0
    memset(alphaLine, 0, imgData->width);
3462
0
    return gFalse;
3463
0
  }
3464
3465
0
  Guchar *maskPtr = imgData->mask->getDataPtr() +
3466
0
                      imgData->y * imgData->mask->getRowSize();
3467
0
  Guchar *aq = alphaLine;
3468
0
  static Guchar bitToByte[2] = {0x00, 0xff};
3469
0
  int x;
3470
0
  for (x = 0; x <= imgData->width - 8; x += 8) {
3471
0
    aq[0] = bitToByte[(*maskPtr >> 7) & 1];
3472
0
    aq[1] = bitToByte[(*maskPtr >> 6) & 1];
3473
0
    aq[2] = bitToByte[(*maskPtr >> 5) & 1];
3474
0
    aq[3] = bitToByte[(*maskPtr >> 4) & 1];
3475
0
    aq[4] = bitToByte[(*maskPtr >> 3) & 1];
3476
0
    aq[5] = bitToByte[(*maskPtr >> 2) & 1];
3477
0
    aq[6] = bitToByte[(*maskPtr >> 1) & 1];
3478
0
    aq[7] = bitToByte[*maskPtr & 1];
3479
0
    aq += 8;
3480
0
    ++maskPtr;
3481
0
  }
3482
0
  int maskShift = 7;
3483
0
  for (; x < imgData->width; ++x) {
3484
0
    *aq++ = bitToByte[(*maskPtr >> maskShift) & 1];
3485
0
    --maskShift;
3486
0
  }
3487
3488
0
  switch (imgData->colorMode) {
3489
0
  case splashModeMono1:
3490
0
  case splashModeMono8:
3491
0
    imgData->colorMap->getGrayByteLine(p, colorLine, imgData->width,
3492
0
               imgData->ri);
3493
0
    break;
3494
0
  case splashModeRGB8:
3495
0
  case splashModeBGR8:
3496
0
    imgData->colorMap->getRGBByteLine(p, colorLine, imgData->width,
3497
0
              imgData->ri);
3498
0
    break;
3499
0
#if SPLASH_CMYK
3500
0
  case splashModeCMYK8:
3501
0
    imgData->colorMap->getCMYKByteLine(p, colorLine, imgData->width,
3502
0
               imgData->ri);
3503
0
    break;
3504
0
#endif
3505
0
  }
3506
3507
0
  if (imgData->invert) {
3508
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3509
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3510
0
      *p ^= 0xff;
3511
0
    }
3512
0
  }
3513
3514
0
  ++imgData->y;
3515
0
  return gTrue;
3516
0
}
3517
3518
GBool SplashOutputDev::maskedImageLookup1Src(void *data,
3519
               SplashColorPtr colorLine,
3520
0
               Guchar *alphaLine) {
3521
0
  SplashOutMaskedImageData *imgData = (SplashOutMaskedImageData *)data;
3522
3523
0
  Guchar *p;
3524
0
  if (imgData->y == imgData->height ||
3525
0
      !(p = imgData->imgStr->getLine())) {
3526
0
    memset(colorLine, 0,
3527
0
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3528
0
    memset(alphaLine, 0, imgData->width);
3529
0
    return gFalse;
3530
0
  }
3531
3532
0
  Guchar *maskPtr = imgData->mask->getDataPtr() +
3533
0
                      imgData->y * imgData->mask->getRowSize();
3534
0
  Guchar *aq = alphaLine;
3535
0
  static Guchar bitToByte[2] = {0x00, 0xff};
3536
0
  int x;
3537
0
  for (x = 0; x <= imgData->width - 8; x += 8) {
3538
0
    aq[0] = bitToByte[(*maskPtr >> 7) & 1];
3539
0
    aq[1] = bitToByte[(*maskPtr >> 6) & 1];
3540
0
    aq[2] = bitToByte[(*maskPtr >> 5) & 1];
3541
0
    aq[3] = bitToByte[(*maskPtr >> 4) & 1];
3542
0
    aq[4] = bitToByte[(*maskPtr >> 3) & 1];
3543
0
    aq[5] = bitToByte[(*maskPtr >> 2) & 1];
3544
0
    aq[6] = bitToByte[(*maskPtr >> 1) & 1];
3545
0
    aq[7] = bitToByte[*maskPtr & 1];
3546
0
    aq += 8;
3547
0
    ++maskPtr;
3548
0
  }
3549
0
  int maskShift = 7;
3550
0
  for (; x < imgData->width; ++x) {
3551
0
    *aq++ = bitToByte[(*maskPtr >> maskShift) & 1];
3552
0
    --maskShift;
3553
0
  }
3554
3555
0
  SplashColorPtr q, col;
3556
0
  switch (imgData->colorMode) {
3557
0
  case splashModeMono1:
3558
0
  case splashModeMono8:
3559
0
    for (x = 0, q = colorLine; x < imgData->width; ++x, ++p) {
3560
0
      *q++ = imgData->lookup[*p];
3561
0
    }
3562
0
    break;
3563
0
  case splashModeRGB8:
3564
0
  case splashModeBGR8:
3565
0
    for (x = 0, q = colorLine; x < imgData->width; ++x, ++p) {
3566
0
      col = &imgData->lookup[3 * *p];
3567
0
      *q++ = col[0];
3568
0
      *q++ = col[1];
3569
0
      *q++ = col[2];
3570
0
    }
3571
0
    break;
3572
0
#if SPLASH_CMYK
3573
0
  case splashModeCMYK8:
3574
0
    for (x = 0, q = colorLine; x < imgData->width; ++x, ++p) {
3575
0
      col = &imgData->lookup[4 * *p];
3576
0
      *q++ = col[0];
3577
0
      *q++ = col[1];
3578
0
      *q++ = col[2];
3579
0
      *q++ = col[3];
3580
0
    }
3581
0
    break;
3582
0
#endif
3583
0
  }
3584
3585
0
  if (imgData->invert) {
3586
0
    int n = imgData->width * splashColorModeNComps[imgData->colorMode];
3587
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3588
0
      *p ^= 0xff;
3589
0
    }
3590
0
  }
3591
3592
0
  ++imgData->y;
3593
0
  return gTrue;
3594
0
}
3595
3596
3597
void SplashOutputDev::drawMaskedImage(GfxState *state, Object *ref,
3598
              Stream *str, int width, int height,
3599
              GfxImageColorMap *colorMap,
3600
              Object *maskRef, Stream *maskStr,
3601
              int maskWidth, int maskHeight,
3602
59
              GBool maskInvert, GBool interpolate) {
3603
  // If the mask is higher resolution than the image, use
3604
  // drawSoftMaskedImage() instead.
3605
59
  if (maskWidth > width || maskHeight > height) {
3606
58
    Object decodeLow, decodeHigh, maskDecode;
3607
58
    decodeLow.initInt(maskInvert ? 0 : 1);
3608
58
    decodeHigh.initInt(maskInvert ? 1 : 0);
3609
58
    maskDecode.initArray(xref);
3610
58
    maskDecode.arrayAdd(&decodeLow);
3611
58
    maskDecode.arrayAdd(&decodeHigh);
3612
58
    GfxImageColorMap *maskColorMap =
3613
58
        new GfxImageColorMap(1, &maskDecode, new GfxDeviceGrayColorSpace());
3614
58
    maskDecode.free();
3615
58
    drawSoftMaskedImage(state, ref, str, width, height, colorMap,
3616
58
      maskRef, maskStr, maskWidth, maskHeight, maskColorMap,
3617
58
      NULL, interpolate);
3618
58
    delete maskColorMap;
3619
58
    return;
3620
58
  }
3621
3622
3623
1
  drawMaskedImage8(state, ref, str, width, height, colorMap,
3624
1
       maskRef, maskStr, maskWidth, maskHeight,
3625
1
       maskInvert, interpolate);
3626
1
}
3627
3628
void SplashOutputDev::drawMaskedImage8(GfxState *state, Object *ref,
3629
               Stream *str, int width, int height,
3630
               GfxImageColorMap *colorMap,
3631
               Object *maskRef, Stream *maskStr,
3632
               int maskWidth, int maskHeight,
3633
1
               GBool maskInvert, GBool interpolate) {
3634
1
  setOverprintMask(state, colorMap->getColorSpace(),
3635
1
       state->getFillOverprint(), state->getOverprintMode(),
3636
1
       NULL);
3637
3638
1
  double *ctm = state->getCTM();
3639
1
  reduceImageResolution(str, ctm, &width, &height);
3640
1
  reduceImageResolution(maskStr, ctm, &maskWidth, &maskHeight);
3641
3642
  //----- scale the mask image to the same size as the source image
3643
3644
1
  SplashCoord mat[6];
3645
1
  mat[0] = (SplashCoord)width;
3646
1
  mat[1] = 0;
3647
1
  mat[2] = 0;
3648
1
  mat[3] = (SplashCoord)height;
3649
1
  mat[4] = 0;
3650
1
  mat[5] = 0;
3651
1
  SplashOutImageMaskData imgMaskData;
3652
1
  imgMaskData.imgStr = new ImageStream(maskStr, maskWidth, 1, 1);
3653
1
  imgMaskData.imgStr->reset();
3654
1
  imgMaskData.invert = maskInvert ? 0 : 1;
3655
1
  imgMaskData.width = maskWidth;
3656
1
  imgMaskData.height = maskHeight;
3657
1
  imgMaskData.y = 0;
3658
1
  traceMessage("masked image bitmap");
3659
1
  SplashBitmap *maskBitmap = new SplashBitmap(width, height, 1, splashModeMono1,
3660
1
                gFalse, gTrue, bitmapMemCache);
3661
1
  Splash *maskSplash = new Splash(maskBitmap, gFalse, splash->getImageCache());
3662
1
  maskSplash->setStrokeAdjust(
3663
1
      mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
3664
1
  maskSplash->setEnablePathSimplification(
3665
1
      globalParams->getEnablePathSimplification());
3666
1
  SplashColor maskColor;
3667
1
  maskColor[0] = 0;
3668
1
  maskSplash->clear(maskColor);
3669
1
  maskColor[0] = 0xff;
3670
1
  maskSplash->setFillPattern(new SplashSolidColor(maskColor));
3671
  // use "glyph mode" here to get the correct scaled size
3672
1
  maskSplash->fillImageMask(NULL, &imageMaskSrc, &imgMaskData,
3673
1
          maskWidth, maskHeight, mat, gTrue, interpolate,
3674
1
          globalParams->getImageMaskAntialias());
3675
1
  delete imgMaskData.imgStr;
3676
1
  maskStr->close();
3677
1
  delete maskSplash;
3678
3679
  //----- draw the source image
3680
3681
1
  mat[0] = ctm[0];
3682
1
  mat[1] = ctm[1];
3683
1
  mat[2] = -ctm[2];
3684
1
  mat[3] = -ctm[3];
3685
1
  mat[4] = ctm[2] + ctm[4];
3686
1
  mat[5] = ctm[3] + ctm[5];
3687
1
  SplashOutMaskedImageData imgData;
3688
1
  imgData.imgStr = new ImageStream(str, width,
3689
1
           colorMap->getNumPixelComps(),
3690
1
           colorMap->getBits());
3691
1
  imgData.imgStr->reset();
3692
1
  imgData.colorMap = colorMap;
3693
1
  imgData.ri = state->getRenderingIntent();
3694
1
  imgData.mask = maskBitmap;
3695
1
  imgData.colorMode = colorMode;
3696
1
  imgData.invert = reverseVideo && reverseVideoInvertImages;
3697
1
  imgData.width = width;
3698
1
  imgData.height = height;
3699
1
  imgData.y = 0;
3700
3701
  // special case for one-channel (monochrome/gray/separation) images:
3702
  // build a lookup table here
3703
1
  imgData.lookup = NULL;
3704
1
  if (colorMap->getNumPixelComps() == 1) {
3705
1
    imgData.lookup = buildColorMapLookupTable1Idx(colorMap, state);
3706
1
  }
3707
3708
1
  SplashColorMode srcMode;
3709
1
  if (colorMode == splashModeMono1) {
3710
0
    srcMode = splashModeMono8;
3711
1
  } else if (colorMode == splashModeBGR8) {
3712
0
    srcMode = splashModeRGB8;
3713
1
  } else {
3714
1
    srcMode = colorMode;
3715
1
  }
3716
1
  GString *imgTag = makeImageTag(ref, state->getRenderingIntent(),
3717
1
         colorMap->getColorSpace());
3718
1
  splash->drawImage(imgTag,
3719
1
        imgData.lookup ? &maskedImageLookup1Src : &maskedImageSrc,
3720
1
        &imgData, srcMode, gTrue,
3721
1
        width, height, mat, interpolate);
3722
1
  delete imgTag;
3723
3724
1
  delete maskBitmap;
3725
1
  gfree(imgData.lookup);
3726
1
  delete imgData.imgStr;
3727
1
  str->close();
3728
1
}
3729
3730
3731
struct SplashOutSoftMaskMatteImageData {
3732
  ImageStream *imgStr;
3733
  ImageStream *maskStr;
3734
  GfxImageColorMap *colorMap;
3735
  GfxRenderingIntent ri;
3736
  Guchar matte[gfxColorMaxComps];
3737
  SplashColorMode colorMode;
3738
  GBool invert;
3739
  int width, height, y;
3740
};
3741
3742
GBool SplashOutputDev::softMaskMatteImageSrc(void *data,
3743
               SplashColorPtr colorLine,
3744
0
               Guchar *alphaLine) {
3745
0
  SplashOutSoftMaskMatteImageData *imgData =
3746
0
      (SplashOutSoftMaskMatteImageData *)data;
3747
0
  Guchar *p, *ap, *aq;
3748
0
  SplashColorPtr q;
3749
0
  GfxRGB rgb;
3750
0
  GfxGray gray;
3751
0
#if SPLASH_CMYK
3752
0
  GfxCMYK cmyk;
3753
0
#endif
3754
0
  Guchar alpha;
3755
0
  int nComps, n, x;
3756
3757
0
  if (imgData->y == imgData->height ||
3758
0
      !(p = imgData->imgStr->getLine()) ||
3759
0
      !(ap = imgData->maskStr->getLine())) {
3760
0
    memset(colorLine, 0,
3761
0
     imgData->width * splashColorModeNComps[imgData->colorMode]);
3762
0
    memset(alphaLine, 0, imgData->width);
3763
0
    return gFalse;
3764
0
  }
3765
3766
0
  nComps = imgData->colorMap->getNumPixelComps();
3767
3768
0
  for (x = 0, q = colorLine, aq = alphaLine;
3769
0
       x < imgData->width;
3770
0
       ++x, p += nComps, ++ap) {
3771
0
    alpha = *ap;
3772
0
    switch (imgData->colorMode) {
3773
0
    case splashModeMono1:
3774
0
    case splashModeMono8:
3775
0
      if (alpha) {
3776
0
  imgData->colorMap->getGray(p, &gray, imgData->ri);
3777
0
  *q++ = clip255(imgData->matte[0] +
3778
0
           (255 * (colToByte(gray) - imgData->matte[0])) / alpha);
3779
0
      } else {
3780
0
  *q++ = 0;
3781
0
      }
3782
0
      break;
3783
0
    case splashModeRGB8:
3784
0
    case splashModeBGR8:
3785
0
      if (alpha) {
3786
0
  imgData->colorMap->getRGB(p, &rgb, imgData->ri);
3787
0
  *q++ = clip255(imgData->matte[0] +
3788
0
           (255 * (colToByte(rgb.r) - imgData->matte[0])) / alpha);
3789
0
  *q++ = clip255(imgData->matte[1] +
3790
0
           (255 * (colToByte(rgb.g) - imgData->matte[1])) / alpha);
3791
0
  *q++ = clip255(imgData->matte[2] +
3792
0
           (255 * (colToByte(rgb.b) - imgData->matte[2])) / alpha);
3793
0
      } else {
3794
0
  *q++ = 0;
3795
0
  *q++ = 0;
3796
0
  *q++ = 0;
3797
0
      }
3798
0
      break;
3799
0
#if SPLASH_CMYK
3800
0
    case splashModeCMYK8:
3801
0
      if (alpha) {
3802
0
  imgData->colorMap->getCMYK(p, &cmyk, imgData->ri);
3803
0
  *q++ = clip255(imgData->matte[0] +
3804
0
           (255 * (colToByte(cmyk.c) - imgData->matte[0]))
3805
0
       / alpha);
3806
0
  *q++ = clip255(imgData->matte[1] +
3807
0
           (255 * (colToByte(cmyk.m) - imgData->matte[1]))
3808
0
       / alpha);
3809
0
  *q++ = clip255(imgData->matte[2] +
3810
0
           (255 * (colToByte(cmyk.y) - imgData->matte[2]))
3811
0
       / alpha);
3812
0
  *q++ = clip255(imgData->matte[3] +
3813
0
           (255 * (colToByte(cmyk.k) - imgData->matte[3]))
3814
0
             / alpha);
3815
0
      } else {
3816
0
  *q++ = 0;
3817
0
  *q++ = 0;
3818
0
  *q++ = 0;
3819
0
  *q++ = 0;
3820
0
      }
3821
0
      break;
3822
0
#endif
3823
0
    }
3824
0
    *aq++ = alpha;
3825
0
  }
3826
3827
0
  if (imgData->invert) {
3828
0
    n = imgData->width * splashColorModeNComps[imgData->colorMode];
3829
0
    for (x = 0, p = colorLine; x < n; ++x, ++p) {
3830
0
      *p ^= 0xff;
3831
0
    }
3832
0
  }
3833
3834
0
  ++imgData->y;
3835
0
  return gTrue;
3836
0
}
3837
3838
3839
void SplashOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref,
3840
            Stream *str, int width, int height,
3841
            GfxImageColorMap *colorMap,
3842
            Object *maskRef, Stream *maskStr,
3843
            int maskWidth, int maskHeight,
3844
            GfxImageColorMap *maskColorMap,
3845
2.72k
            double *matte, GBool interpolate) {
3846
  // handle a preblended image
3847
2.72k
  if (matte && width == maskWidth && height == maskHeight) {
3848
0
    drawSoftMaskedMatteImage8(state, ref, str, width, height,
3849
0
            colorMap, maskRef, maskStr,
3850
0
            maskWidth, maskHeight, maskColorMap,
3851
0
            matte, interpolate);
3852
0
    return;
3853
0
  }
3854
3855
3856
2.72k
  drawSoftMaskedImage8(state, ref, str, width, height,
3857
2.72k
           colorMap, maskRef, maskStr,
3858
2.72k
           maskWidth, maskHeight, maskColorMap,
3859
2.72k
           matte, interpolate);
3860
2.72k
}
3861
3862
void SplashOutputDev::drawSoftMaskedImage8(GfxState *state, Object *ref,
3863
             Stream *str, int width, int height,
3864
             GfxImageColorMap *colorMap,
3865
             Object *maskRef, Stream *maskStr,
3866
             int maskWidth, int maskHeight,
3867
             GfxImageColorMap *maskColorMap,
3868
2.72k
             double *matte, GBool interpolate) {
3869
2.72k
  setOverprintMask(state, colorMap->getColorSpace(),
3870
2.72k
       state->getFillOverprint(), state->getOverprintMode(),
3871
2.72k
       NULL);
3872
3873
2.72k
  double *ctm = state->getCTM();
3874
2.72k
  SplashCoord mat[6];
3875
2.72k
  mat[0] = ctm[0];
3876
2.72k
  mat[1] = ctm[1];
3877
2.72k
  mat[2] = -ctm[2];
3878
2.72k
  mat[3] = -ctm[3];
3879
2.72k
  mat[4] = ctm[2] + ctm[4];
3880
2.72k
  mat[5] = ctm[3] + ctm[5];
3881
3882
2.72k
  SplashColorMode srcMode;
3883
2.72k
  if (colorMode == splashModeMono1) {
3884
0
    srcMode = splashModeMono8;
3885
2.72k
  } else if (colorMode == splashModeBGR8) {
3886
0
    srcMode = splashModeRGB8;
3887
2.72k
  } else {
3888
2.72k
    srcMode = colorMode;
3889
2.72k
  }
3890
3891
2.72k
  reduceImageResolution(str, ctm, &width, &height);
3892
2.72k
  reduceImageResolution(maskStr, ctm, &maskWidth, &maskHeight);
3893
3894
  //----- set up the soft mask
3895
3896
2.72k
  SplashOutImageData imgMaskData;
3897
2.72k
  imgMaskData.imgStr = new ImageStream(maskStr, maskWidth,
3898
2.72k
               maskColorMap->getNumPixelComps(),
3899
2.72k
               maskColorMap->getBits());
3900
2.72k
  imgMaskData.imgStr->reset();
3901
2.72k
  imgMaskData.colorMap = maskColorMap;
3902
2.72k
  imgMaskData.ri = state->getRenderingIntent();
3903
2.72k
  imgMaskData.maskColors = NULL;
3904
2.72k
  imgMaskData.colorMode = splashModeMono8;
3905
2.72k
  imgMaskData.invert = gFalse;
3906
2.72k
  imgMaskData.width = maskWidth;
3907
2.72k
  imgMaskData.height = maskHeight;
3908
2.72k
  imgMaskData.y = 0;
3909
2.72k
  int n;
3910
2.72k
  if (maskColorMap->getBits() <= 8) {
3911
2.67k
    n = 1 << maskColorMap->getBits();
3912
2.67k
  } else {
3913
    // GfxImageColorMap and ImageStream compress 16-bit samples to 8-bit
3914
51
    n = 1 << 8;
3915
51
  }
3916
2.72k
  imgMaskData.lookup = (SplashColorPtr)gmalloc(n);
3917
2.72k
  GfxGray gray;
3918
684k
  for (int i = 0; i < n; ++i) {
3919
681k
    Guchar pix = (Guchar)i;
3920
681k
    maskColorMap->getGray(&pix, &gray, state->getRenderingIntent());
3921
681k
    imgMaskData.lookup[i] = colToByte(gray);
3922
681k
  }
3923
2.72k
  traceMessage("soft masked image bitmap");
3924
2.72k
  SplashBitmap *maskBitmap = new SplashBitmap(bitmap->getWidth(),
3925
2.72k
                bitmap->getHeight(),
3926
2.72k
                1, splashModeMono8,
3927
2.72k
                gFalse, gTrue,
3928
2.72k
                bitmapMemCache);
3929
2.72k
  Splash *maskSplash = new Splash(maskBitmap, vectorAntialias,
3930
2.72k
        splash->getImageCache());
3931
2.72k
  maskSplash->setStrokeAdjust(
3932
2.72k
      mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
3933
2.72k
  maskSplash->setEnablePathSimplification(
3934
2.72k
      globalParams->getEnablePathSimplification());
3935
2.72k
  clearMaskRegion(state, maskSplash, 0, 0, 1, 1);
3936
2.72k
  maskSplash->drawImage(NULL, &imageLookup1Src, &imgMaskData,
3937
2.72k
      splashModeMono8, gFalse,
3938
2.72k
      maskWidth, maskHeight, mat, interpolate);
3939
2.72k
  delete imgMaskData.imgStr;
3940
2.72k
  maskStr->close();
3941
2.72k
  gfree(imgMaskData.lookup);
3942
2.72k
  delete maskSplash;
3943
2.72k
  splash->setSoftMask(maskBitmap);
3944
3945
  //----- draw the source image
3946
3947
2.72k
  SplashOutImageData imgData;
3948
2.72k
  imgData.imgStr = new ImageStream(str, width,
3949
2.72k
           colorMap->getNumPixelComps(),
3950
2.72k
           colorMap->getBits());
3951
2.72k
  imgData.imgStr->reset();
3952
2.72k
  imgData.colorMap = colorMap;
3953
2.72k
  imgData.ri = state->getRenderingIntent();
3954
2.72k
  imgData.maskColors = NULL;
3955
2.72k
  imgData.colorMode = colorMode;
3956
2.72k
  imgData.invert = reverseVideo && reverseVideoInvertImages;
3957
2.72k
  imgData.width = width;
3958
2.72k
  imgData.height = height;
3959
2.72k
  imgData.y = 0;
3960
3961
  // special case for one-channel (monochrome/gray/separation) images:
3962
  // build a lookup table here
3963
2.72k
  imgData.lookup = NULL;
3964
2.72k
  if (colorMap->getNumPixelComps() == 1) {
3965
565
    imgData.lookup = buildColorMapLookupTable1Idx(colorMap, state);
3966
565
  }
3967
3968
2.72k
  GString *imgTag = makeImageTag(ref, state->getRenderingIntent(),
3969
2.72k
         colorMap->getColorSpace());
3970
2.72k
  splash->drawImage(imgTag, imgData.lookup ? imageLookup1Src : imageSrc,
3971
2.72k
        &imgData, srcMode, gFalse, width, height, mat,
3972
2.72k
        interpolate);
3973
2.72k
  delete imgTag;
3974
3975
2.72k
  splash->setSoftMask(NULL);
3976
2.72k
  gfree(imgData.lookup);
3977
2.72k
  delete imgData.imgStr;
3978
2.72k
  str->close();
3979
2.72k
}
3980
3981
3982
void SplashOutputDev::drawSoftMaskedMatteImage8(
3983
        GfxState *state, Object *ref,
3984
        Stream *str, int width, int height,
3985
        GfxImageColorMap *colorMap,
3986
        Object *maskRef, Stream *maskStr,
3987
        int maskWidth, int maskHeight,
3988
        GfxImageColorMap *maskColorMap,
3989
0
        double *matte, GBool interpolate) {
3990
0
  setOverprintMask(state, colorMap->getColorSpace(),
3991
0
       state->getFillOverprint(), state->getOverprintMode(),
3992
0
       NULL);
3993
3994
0
  double *ctm = state->getCTM();
3995
0
  SplashCoord mat[6];
3996
0
  mat[0] = ctm[0];
3997
0
  mat[1] = ctm[1];
3998
0
  mat[2] = -ctm[2];
3999
0
  mat[3] = -ctm[3];
4000
0
  mat[4] = ctm[2] + ctm[4];
4001
0
  mat[5] = ctm[3] + ctm[5];
4002
4003
0
  SplashColorMode srcMode;
4004
0
  if (colorMode == splashModeMono1) {
4005
0
    srcMode = splashModeMono8;
4006
0
  } else if (colorMode == splashModeBGR8) {
4007
0
    srcMode = splashModeRGB8;
4008
0
  } else {
4009
0
    srcMode = colorMode;
4010
0
  }
4011
4012
  // the image and mask must be the same size, so don't call
4013
  // reduceImageResolution(), which might result in different
4014
  // reductions (e.g., if the image filter supports resolution
4015
  // reduction but the mask filter doesn't)
4016
4017
0
  SplashOutSoftMaskMatteImageData matteImgData;
4018
0
  matteImgData.imgStr = new ImageStream(str, width,
4019
0
          colorMap->getNumPixelComps(),
4020
0
          colorMap->getBits());
4021
0
  matteImgData.imgStr->reset();
4022
0
  matteImgData.maskStr = new ImageStream(maskStr, maskWidth,
4023
0
           maskColorMap->getNumPixelComps(),
4024
0
           maskColorMap->getBits());
4025
0
  matteImgData.maskStr->reset();
4026
0
  matteImgData.colorMap = colorMap;
4027
0
  matteImgData.ri = state->getRenderingIntent();
4028
0
  int n = colorMap->getNumPixelComps();
4029
0
  GfxColor matteColor;
4030
0
  for (int i = 0; i < n; ++i) {
4031
0
    matteColor.c[i] = dblToCol(matte[i]);
4032
0
  }
4033
0
  switch (colorMode) {
4034
0
  case splashModeMono1:
4035
0
  case splashModeMono8: {
4036
0
    GfxGray gray;
4037
0
    colorMap->getColorSpace()->getGray(&matteColor, &gray,
4038
0
               state->getRenderingIntent());
4039
0
    matteImgData.matte[0] = colToByte(gray);
4040
0
    break;
4041
0
  }
4042
0
  case splashModeRGB8:
4043
0
  case splashModeBGR8: {
4044
0
    GfxRGB rgb;
4045
0
    colorMap->getColorSpace()->getRGB(&matteColor, &rgb,
4046
0
              state->getRenderingIntent());
4047
0
    matteImgData.matte[0] = colToByte(rgb.r);
4048
0
    matteImgData.matte[1] = colToByte(rgb.g);
4049
0
    matteImgData.matte[2] = colToByte(rgb.b);
4050
0
    break;
4051
0
  }
4052
0
#if SPLASH_CMYK
4053
0
  case splashModeCMYK8: {
4054
0
    GfxCMYK cmyk;
4055
0
    colorMap->getColorSpace()->getCMYK(&matteColor, &cmyk,
4056
0
               state->getRenderingIntent());
4057
0
    matteImgData.matte[0] = colToByte(cmyk.c);
4058
0
    matteImgData.matte[1] = colToByte(cmyk.m);
4059
0
    matteImgData.matte[2] = colToByte(cmyk.y);
4060
0
    matteImgData.matte[3] = colToByte(cmyk.k);
4061
0
    break;
4062
0
  }
4063
0
#endif
4064
0
  }
4065
  //~ could add the matteImgData.lookup special case
4066
0
  matteImgData.colorMode = colorMode;
4067
0
  matteImgData.invert = reverseVideo && reverseVideoInvertImages;
4068
0
  matteImgData.width = width;
4069
0
  matteImgData.height = height;
4070
0
  matteImgData.y = 0;
4071
0
  GString *imgTag = makeImageTag(ref, state->getRenderingIntent(),
4072
0
         colorMap->getColorSpace());
4073
0
  splash->drawImage(imgTag, &softMaskMatteImageSrc, &matteImgData,
4074
0
        srcMode, gTrue, width, height, mat, interpolate);
4075
0
  delete imgTag;
4076
4077
0
  delete matteImgData.maskStr;
4078
0
  delete matteImgData.imgStr;
4079
0
  maskStr->close();
4080
0
  str->close();
4081
0
}
4082
4083
4084
SplashColorPtr SplashOutputDev::buildColorMapLookupTable1Idx(
4085
            GfxImageColorMap *colorMap,
4086
21.3k
            GfxState *state) {
4087
21.3k
  SplashColorPtr lookup = NULL;
4088
21.3k
  int n;
4089
21.3k
  if (colorMap->getBits() <= 8) {
4090
21.3k
    n = 1 << colorMap->getBits();
4091
21.3k
  } else {
4092
    // GfxImageColorMap and ImageStream compress 16-bit samples to 8-bit
4093
5
    n = 1 << 8;
4094
5
  }
4095
21.3k
  switch (colorMode) {
4096
0
  case splashModeMono1:
4097
566
  case splashModeMono8:
4098
566
    lookup = (SplashColorPtr)gmalloc(n);
4099
566
    GfxGray gray;
4100
145k
    for (int i = 0; i < n; ++i) {
4101
144k
      Guchar pix = (Guchar)i;
4102
144k
      colorMap->getGray(&pix, &gray, state->getRenderingIntent());
4103
144k
      lookup[i] = colToByte(gray);
4104
144k
    }
4105
566
    break;
4106
20.7k
  case splashModeRGB8:
4107
20.7k
  case splashModeBGR8:
4108
20.7k
    lookup = (SplashColorPtr)gmallocn(n, 3);
4109
20.7k
    GfxRGB rgb;
4110
2.77M
    for (int i = 0; i < n; ++i) {
4111
2.75M
      Guchar pix = (Guchar)i;
4112
2.75M
      colorMap->getRGB(&pix, &rgb, state->getRenderingIntent());
4113
2.75M
      lookup[3*i] = colToByte(rgb.r);
4114
2.75M
      lookup[3*i+1] = colToByte(rgb.g);
4115
2.75M
      lookup[3*i+2] = colToByte(rgb.b);
4116
2.75M
    }
4117
20.7k
    break;
4118
0
#if SPLASH_CMYK
4119
94
  case splashModeCMYK8:
4120
94
    lookup = (SplashColorPtr)gmallocn(n, 4);
4121
94
    GfxCMYK cmyk;
4122
19.8k
    for (int i = 0; i < n; ++i) {
4123
19.7k
      Guchar pix = (Guchar)i;
4124
19.7k
      colorMap->getCMYK(&pix, &cmyk, state->getRenderingIntent());
4125
19.7k
      lookup[4*i] = colToByte(cmyk.c);
4126
19.7k
      lookup[4*i+1] = colToByte(cmyk.m);
4127
19.7k
      lookup[4*i+2] = colToByte(cmyk.y);
4128
19.7k
      lookup[4*i+3] = colToByte(cmyk.k);
4129
19.7k
    }
4130
94
    break;
4131
21.3k
#endif
4132
21.3k
  }
4133
21.3k
  return lookup;
4134
21.3k
}
4135
4136
SplashColorPtr SplashOutputDev::buildColorMapLookupTable2Idx(
4137
            GfxImageColorMap *colorMap,
4138
0
            GfxState *state) {
4139
0
  SplashColorPtr lookup = NULL;
4140
0
  int n;
4141
0
  if (colorMap->getBits() <= 8) {
4142
0
    n = 1 << colorMap->getBits();
4143
0
  } else {
4144
    // GfxImageColorMap and ImageStream compress 16-bit samples to 8-bit
4145
0
    n = 1 << 8;
4146
0
  }
4147
0
  switch (colorMode) {
4148
0
  case splashModeMono1:
4149
0
  case splashModeMono8: {
4150
0
    lookup = (SplashColorPtr)gmalloc(n * n);
4151
0
    GfxGray gray;
4152
0
    Guchar pix[2];
4153
0
    int k = 0;
4154
0
    for (int i0 = 0; i0 < n; ++i0) {
4155
0
      pix[0] = (Guchar)i0;
4156
0
      for (int i1 = 0; i1 < n; ++i1) {
4157
0
  pix[1] = (Guchar)i1;
4158
0
  colorMap->getGray(pix, &gray, state->getRenderingIntent());
4159
0
  lookup[k] = colToByte(gray);
4160
0
  ++k;
4161
0
      }
4162
0
    }
4163
0
    break;
4164
0
  }
4165
0
  case splashModeRGB8:
4166
0
  case splashModeBGR8: {
4167
0
    lookup = (SplashColorPtr)gmallocn(n * n, 3);
4168
0
    GfxRGB rgb;
4169
0
    Guchar pix[2];
4170
0
    int k = 0;
4171
0
    for (int i0 = 0; i0 < n; ++i0) {
4172
0
      pix[0] = (Guchar)i0;
4173
0
      for (int i1 = 0; i1 < n; ++i1) {
4174
0
  pix[1] = (Guchar)i1;
4175
0
  colorMap->getRGB(pix, &rgb, state->getRenderingIntent());
4176
0
  lookup[k  ] = colToByte(rgb.r);
4177
0
  lookup[k+1] = colToByte(rgb.g);
4178
0
  lookup[k+2] = colToByte(rgb.b);
4179
0
  k += 3;
4180
0
      }
4181
0
    }
4182
0
    break;
4183
0
  }
4184
0
#if SPLASH_CMYK
4185
0
  case splashModeCMYK8: {
4186
0
    lookup = (SplashColorPtr)gmallocn(n * n, 4);
4187
0
    GfxCMYK cmyk;
4188
0
    Guchar pix[2];
4189
0
    int k = 0;
4190
0
    for (int i0 = 0; i0 < n; ++i0) {
4191
0
      pix[0] = (Guchar)i0;
4192
0
      for (int i1 = 0; i1 < n; ++i1) {
4193
0
  pix[1] = (Guchar)i1;
4194
0
  colorMap->getCMYK(pix, &cmyk, state->getRenderingIntent());
4195
0
  lookup[k  ] = colToByte(cmyk.c);
4196
0
  lookup[k+1] = colToByte(cmyk.m);
4197
0
  lookup[k+2] = colToByte(cmyk.y);
4198
0
  lookup[k+3] = colToByte(cmyk.k);
4199
0
  k += 4;
4200
0
      }
4201
0
    }
4202
0
    break;
4203
0
  }
4204
0
#endif
4205
0
  }
4206
0
  return lookup;
4207
0
}
4208
4209
4210
GString *SplashOutputDev::makeImageTag(Object *ref, GfxRenderingIntent ri,
4211
53.7k
               GfxColorSpace *colorSpace) {
4212
53.7k
  if (!ref || !ref->isRef() ||
4213
46.5k
      (colorSpace && colorSpace->isDefaultColorSpace())) {
4214
46.5k
    return NULL;
4215
46.5k
  }
4216
7.21k
  return GString::format("{0:d}_{1:d}_{2:d}",
4217
7.21k
       ref->getRefNum(), ref->getRefGen(), (int)ri);
4218
53.7k
}
4219
4220
void SplashOutputDev::reduceImageResolution(Stream *str, double *ctm,
4221
56.4k
              int *width, int *height) {
4222
56.4k
  double sw, sh;
4223
56.4k
  int reduction;
4224
4225
56.4k
  if (str->getKind() == strJPX &&
4226
1.17k
      *width >= 256 &&
4227
456
      *height >= 256 &&
4228
410
      *width * *height > 10000000) {
4229
6
    sw = (double)*width / (fabs(ctm[0]) + fabs(ctm[1]));
4230
6
    sh = (double)*height / (fabs(ctm[2]) + fabs(ctm[3]));
4231
6
    if (sw > 8 && sh > 8) {
4232
0
      reduction = 3;
4233
6
    } else if (sw > 4 && sh > 4) {
4234
0
      reduction = 2;
4235
6
    } else if (sw > 2 && sh > 2) {
4236
0
      reduction = 1;
4237
6
    } else {
4238
6
      reduction = 0;
4239
6
    }
4240
6
    if (reduction > 0) {
4241
0
      ((JPXStream *)str)->reduceResolution(reduction);
4242
0
      *width >>= reduction;
4243
0
      *height >>= reduction;
4244
0
    }
4245
6
  }
4246
56.4k
}
4247
4248
void SplashOutputDev::clearMaskRegion(GfxState *state,
4249
              Splash *maskSplash,
4250
              double xMin, double yMin,
4251
2.87k
              double xMax, double yMax) {
4252
2.87k
  SplashBitmap *maskBitmap;
4253
2.87k
  double xxMin, yyMin, xxMax, yyMax, xx, yy;
4254
2.87k
  int xxMinI, yyMinI, xxMaxI, yyMaxI, y, n;
4255
2.87k
  Guchar *p;
4256
4257
2.87k
  maskBitmap = maskSplash->getBitmap();
4258
2.87k
  xxMin = maskBitmap->getWidth();
4259
2.87k
  xxMax = 0;
4260
2.87k
  yyMin = maskBitmap->getHeight();
4261
2.87k
  yyMax = 0;
4262
2.87k
  state->transform(xMin, yMin, &xx, &yy);
4263
2.87k
  if (xx < xxMin) { xxMin = xx; }
4264
2.87k
  if (xx > xxMax) { xxMax = xx; }
4265
2.87k
  if (yy < yyMin) { yyMin = yy; }
4266
2.87k
  if (yy > yyMax) { yyMax = yy; }
4267
2.87k
  state->transform(xMin, yMax, &xx, &yy);
4268
2.87k
  if (xx < xxMin) { xxMin = xx; }
4269
2.87k
  if (xx > xxMax) { xxMax = xx; }
4270
2.87k
  if (yy < yyMin) { yyMin = yy; }
4271
2.87k
  if (yy > yyMax) { yyMax = yy; }
4272
2.87k
  state->transform(xMax, yMin, &xx, &yy);
4273
2.87k
  if (xx < xxMin) { xxMin = xx; }
4274
2.87k
  if (xx > xxMax) { xxMax = xx; }
4275
2.87k
  if (yy < yyMin) { yyMin = yy; }
4276
2.87k
  if (yy > yyMax) { yyMax = yy; }
4277
2.87k
  state->transform(xMax, yMax, &xx, &yy);
4278
2.87k
  if (xx < xxMin) { xxMin = xx; }
4279
2.87k
  if (xx > xxMax) { xxMax = xx; }
4280
2.87k
  if (yy < yyMin) { yyMin = yy; }
4281
2.87k
  if (yy > yyMax) { yyMax = yy; }
4282
2.87k
  xxMinI = (int)floor(xxMin);
4283
2.87k
  if (xxMinI < 0) {
4284
1.30k
    xxMinI = 0;
4285
1.30k
  }
4286
2.87k
  xxMaxI = (int)ceil(xxMax);
4287
2.87k
  if (xxMaxI > maskBitmap->getWidth()) {
4288
280
    xxMaxI = maskBitmap->getWidth();
4289
280
  }
4290
2.87k
  yyMinI = (int)floor(yyMin);
4291
2.87k
  if (yyMinI < 0) {
4292
1.42k
    yyMinI = 0;
4293
1.42k
  }
4294
2.87k
  yyMaxI = (int)ceil(yyMax);
4295
2.87k
  if (yyMaxI > maskBitmap->getHeight()) {
4296
216
    yyMaxI = maskBitmap->getHeight();
4297
216
  }
4298
2.87k
  p = maskBitmap->getDataPtr() + yyMinI * maskBitmap->getRowSize();
4299
2.87k
  if (maskBitmap->getMode() == splashModeMono1) {
4300
0
    n = (xxMaxI + 7) / 8 - xxMinI / 8;
4301
0
    p += xxMinI / 8;
4302
2.87k
  } else {
4303
2.87k
    n = xxMaxI - xxMinI;
4304
2.87k
    p += xxMinI;
4305
2.87k
  }
4306
2.87k
  if (xxMaxI > xxMinI) {
4307
296
    for (y = yyMinI; y < yyMaxI; ++y) {
4308
15
      memset(p, 0, n);
4309
15
      p += maskBitmap->getRowSize();
4310
15
    }
4311
281
  }
4312
2.87k
}
4313
4314
GBool SplashOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
4315
                GfxColorSpace *blendingColorSpace,
4316
                GBool isolated, GBool knockout,
4317
8.55k
                GBool forSoftMask) {
4318
8.55k
  SplashTransparencyGroup *transpGroup;
4319
8.55k
  SplashAlphaBitmap *alpha0Bitmap;
4320
8.55k
  double xMin, yMin, xMax, yMax, x, y;
4321
8.55k
  int bw, bh, tx, ty, w, h, xx, yy;
4322
4323
  // transform the bbox
4324
8.55k
  state->transform(bbox[0], bbox[1], &x, &y);
4325
8.55k
  xMin = xMax = x;
4326
8.55k
  yMin = yMax = y;
4327
8.55k
  state->transform(bbox[0], bbox[3], &x, &y);
4328
8.55k
  if (x < xMin) {
4329
1.01k
    xMin = x;
4330
7.54k
  } else if (x > xMax) {
4331
1.05k
    xMax = x;
4332
1.05k
  }
4333
8.55k
  if (y < yMin) {
4334
3.00k
    yMin = y;
4335
5.54k
  } else if (y > yMax) {
4336
3.57k
    yMax = y;
4337
3.57k
  }
4338
8.55k
  state->transform(bbox[2], bbox[1], &x, &y);
4339
8.55k
  if (x < xMin) {
4340
2.70k
    xMin = x;
4341
5.85k
  } else if (x > xMax) {
4342
2.34k
    xMax = x;
4343
2.34k
  }
4344
8.55k
  if (y < yMin) {
4345
165
    yMin = y;
4346
8.38k
  } else if (y > yMax) {
4347
614
    yMax = y;
4348
614
  }
4349
8.55k
  state->transform(bbox[2], bbox[3], &x, &y);
4350
8.55k
  if (x < xMin) {
4351
763
    xMin = x;
4352
7.79k
  } else if (x > xMax) {
4353
632
    xMax = x;
4354
632
  }
4355
8.55k
  if (y < yMin) {
4356
218
    yMin = y;
4357
8.33k
  } else if (y > yMax) {
4358
243
    yMax = y;
4359
243
  }
4360
4361
  // clip the box
4362
8.55k
  x = splash->getClip()->getXMin();
4363
8.55k
  if (x > xMin) {
4364
6.12k
    xMin = x;
4365
6.12k
  }
4366
8.55k
  x = splash->getClip()->getXMax();
4367
8.55k
  if (x < xMax) {
4368
4.67k
    xMax = x;
4369
4.67k
  }
4370
8.55k
  y = splash->getClip()->getYMin();
4371
8.55k
  if (y > yMin) {
4372
6.84k
    yMin = y;
4373
6.84k
  }
4374
8.55k
  y = splash->getClip()->getYMax();
4375
8.55k
  if (y < yMax) {
4376
5.80k
    yMax = y;
4377
5.80k
  }
4378
4379
  // convert box coords to integers
4380
8.55k
  bw = bitmap->getWidth();
4381
8.55k
  bh = bitmap->getHeight();
4382
8.55k
  tx = (int)floor(xMin);
4383
8.55k
  if (tx < 0) {
4384
586
    tx = 0;
4385
7.96k
  } else if (tx >= bw) {
4386
1.72k
    tx = bw - 1;
4387
1.72k
  }
4388
8.55k
  ty = (int)floor(yMin);
4389
8.55k
  if (ty < 0) {
4390
1.13k
    ty = 0;
4391
7.42k
  } else if (ty >= bh) {
4392
1.99k
    ty = bh - 1;
4393
1.99k
  }
4394
8.55k
  xx = (int)ceil(xMax);
4395
8.55k
  if (xx < tx) {
4396
1.75k
    w = 1;
4397
6.79k
  } else if (xx > bw - 1) {
4398
    // NB bw and tx are both non-negative, so 'bw - tx' can't overflow
4399
4.09k
    w = bw - tx;
4400
4.09k
  } else {
4401
2.70k
    w = xx - tx + 1;
4402
2.70k
  }
4403
8.55k
  yy = (int)ceil(yMax);
4404
8.55k
  if (yy < ty) {
4405
3.08k
    h = 1;
4406
5.47k
  } else if (yy > bh - 1) {
4407
    // NB bh and ty are both non-negative, so 'bh - ty' can't overflow
4408
4.48k
    h = bh - ty;
4409
4.48k
  } else {
4410
982
    h = yy - ty + 1;
4411
982
  }
4412
4413
  // optimization: a non-isolated group drawn with alpha=1 and
4414
  // Blend=Normal and backdrop alpha=0 is equivalent to drawing
4415
  // directly onto the backdrop (i.e., a regular non-t-group Form)
4416
  // notes:
4417
  // - if we are already in a non-isolated group, it means the
4418
  //   backdrop alpha is non-zero (otherwise the parent non-isolated
4419
  //   group would have been optimized away)
4420
  // - if there is a soft mask in place, then source alpha is not 1
4421
  //   (i.e., source alpha = fillOpacity * softMask)
4422
  // - both the parent and child groups must be non-knockout
4423
8.55k
  if (!isolated &&
4424
6.25k
      !splash->getInNonIsolatedGroup() &&
4425
4.90k
      !knockout &&
4426
4.90k
      !splash->getInKnockoutGroup() &&
4427
4.90k
      !forSoftMask &&
4428
4.90k
      !splash->getSoftMask() &&
4429
4.79k
      state->getFillOpacity() == 1 &&
4430
4.77k
      state->getBlendMode() == gfxBlendNormal &&
4431
4.69k
      splash->checkTransparentRect(tx, ty, w, h)) {
4432
1.83k
    return gFalse;
4433
1.83k
  }
4434
4435
  // push a new stack entry
4436
6.72k
  transpGroup = new SplashTransparencyGroup();
4437
6.72k
  transpGroup->tx = tx;
4438
6.72k
  transpGroup->ty = ty;
4439
6.72k
  transpGroup->blendingColorSpace = blendingColorSpace;
4440
6.72k
  transpGroup->isolated = isolated;
4441
6.72k
  transpGroup->inKnockoutGroup = (transpGroupStack &&
4442
2.11k
          transpGroupStack->inKnockoutGroup)
4443
6.72k
                                 || knockout;    
4444
6.72k
  transpGroup->inSoftMask = (transpGroupStack && transpGroupStack->inSoftMask)
4445
5.31k
                            || forSoftMask;
4446
6.72k
  transpGroup->next = transpGroupStack;
4447
6.72k
  transpGroupStack = transpGroup;
4448
4449
  // save state
4450
6.72k
  transpGroup->origBitmap = bitmap;
4451
6.72k
  transpGroup->origSplash = splash;
4452
4453
  //~ this handles the blendingColorSpace arg for soft masks, but
4454
  //~   not yet for transparency groups
4455
4456
  // switch to the blending color space
4457
6.72k
  if (forSoftMask && isolated && !knockout && blendingColorSpace) {
4458
1.34k
    if (blendingColorSpace->getMode() == csDeviceGray ||
4459
349
  blendingColorSpace->getMode() == csCalGray ||
4460
349
  (blendingColorSpace->getMode() == csICCBased &&
4461
997
   blendingColorSpace->getNComps() == 1)) {
4462
997
      colorMode = splashModeMono8;
4463
997
    } else if (blendingColorSpace->getMode() == csDeviceRGB ||
4464
349
         blendingColorSpace->getMode() == csCalRGB ||
4465
349
         (blendingColorSpace->getMode() == csICCBased &&
4466
0
    blendingColorSpace->getNComps() == 3)) {
4467
      //~ does this need to use BGR8?
4468
0
      colorMode = splashModeRGB8;
4469
0
#if SPLASH_CMYK
4470
349
    } else if (blendingColorSpace->getMode() == csDeviceCMYK ||
4471
0
         (blendingColorSpace->getMode() == csICCBased &&
4472
349
    blendingColorSpace->getNComps() == 4)) {
4473
349
      colorMode = splashModeCMYK8;
4474
349
#endif
4475
349
    }
4476
1.34k
  }
4477
4478
  // create the temporary bitmap
4479
6.72k
  traceMessage("t-group bitmap");
4480
6.72k
  bitmap = new SplashBitmap(w, h, bitmapRowPad, colorMode, gTrue,
4481
6.72k
          bitmapTopDown, bitmapMemCache); 
4482
6.72k
  splash = new Splash(bitmap, vectorAntialias,
4483
6.72k
          transpGroup->origSplash->getImageCache(),
4484
6.72k
          transpGroup->origSplash->getScreen());
4485
6.72k
  splash->setMinLineWidth(globalParams->getMinLineWidth());
4486
6.72k
  splash->setStrokeAdjust(
4487
6.72k
     mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
4488
6.72k
  splash->setEnablePathSimplification(
4489
6.72k
     globalParams->getEnablePathSimplification());
4490
6.72k
  copyState(transpGroup->origSplash, gTrue);
4491
6.72k
  if (transpGroupStack->inKnockoutGroup) {
4492
0
    transpGroup->shapeBitmap = new SplashAlphaBitmap(w, h, bitmapMemCache);
4493
0
    memset(transpGroup->shapeBitmap->getAlphaPtr(), 0,
4494
0
     h * transpGroup->shapeBitmap->getAlphaRowSize());
4495
6.72k
  } else {
4496
6.72k
    transpGroup->shapeBitmap = NULL;
4497
6.72k
  }
4498
6.72k
  if (!isolated &&
4499
4.41k
      transpGroup->origBitmap->getAlphaPtr() &&
4500
4.41k
      transpGroup->origSplash->getInNonIsolatedGroup() &&
4501
1.34k
      colorMode != splashModeMono1) {
4502
    // non-isolated group drawn in another non-isolated group:
4503
    // allocate an alpha0 bitmap (which will be initialized as needed
4504
    // by Splash)
4505
1.34k
    traceMessage("t-group alpha0 bitmap");
4506
1.34k
    alpha0Bitmap = new SplashAlphaBitmap(w, h, bitmapMemCache);
4507
5.37k
  } else {
4508
5.37k
    alpha0Bitmap = NULL;
4509
5.37k
  }
4510
6.72k
  splash->setInTransparencyGroup(transpGroup->origSplash, tx, ty,
4511
6.72k
         alpha0Bitmap, transpGroup->shapeBitmap,
4512
6.72k
         !isolated, knockout);
4513
6.72k
  splash->clearModRegion();
4514
6.72k
  transpGroup->tBitmap = bitmap;
4515
6.72k
  transpGroup->alpha0Bitmap = alpha0Bitmap;
4516
6.72k
  state->shiftCTM(-tx, -ty);
4517
6.72k
  updateCTM(state, 0, 0, 0, 0, 0, 0);
4518
6.72k
  ++nestCount;
4519
4520
6.72k
  return gTrue;
4521
8.55k
}
4522
4523
6.71k
void SplashOutputDev::endTransparencyGroup(GfxState *state) {
4524
6.71k
  splash->getModRegion(&transpGroupStack->modXMin, &transpGroupStack->modYMin,
4525
6.71k
           &transpGroupStack->modXMax, &transpGroupStack->modYMax);
4526
4527
  // setSoftMask uses the whole bitmap, not just the mod region,
4528
  // so we need to force any deferred initialization
4529
6.71k
  if (transpGroupStack->inSoftMask) {
4530
2.92k
      splash->forceDeferredInit(0, transpGroupStack->tBitmap->getHeight());
4531
2.92k
  }
4532
4533
  // restore state
4534
6.71k
  --nestCount;
4535
6.71k
  delete splash;
4536
6.71k
  bitmap = transpGroupStack->origBitmap;
4537
6.71k
  colorMode = bitmap->getMode();
4538
6.71k
  splash = transpGroupStack->origSplash;
4539
6.71k
  state->shiftCTM(transpGroupStack->tx, transpGroupStack->ty);
4540
6.71k
  updateCTM(state, 0, 0, 0, 0, 0, 0);
4541
6.71k
}
4542
4543
4.65k
void SplashOutputDev::paintTransparencyGroup(GfxState *state, double *bbox) {
4544
4.65k
  SplashBitmap *tBitmap;
4545
4.65k
  SplashAlphaBitmap *shapeBitmap;
4546
4.65k
  SplashTransparencyGroup *transpGroup;
4547
4.65k
  GBool isolated;
4548
4.65k
  int xSrc, ySrc, xDest, yDest, w, h;
4549
4550
4.65k
  xSrc = transpGroupStack->modXMin;
4551
4.65k
  ySrc = transpGroupStack->modYMin;
4552
4.65k
  xDest = transpGroupStack->tx + transpGroupStack->modXMin;
4553
4.65k
  yDest = transpGroupStack->ty + transpGroupStack->modYMin;
4554
4.65k
  w = transpGroupStack->modXMax - transpGroupStack->modXMin + 1;
4555
4.65k
  h = transpGroupStack->modYMax - transpGroupStack->modYMin + 1;
4556
4.65k
  tBitmap = transpGroupStack->tBitmap;
4557
4.65k
  shapeBitmap = transpGroupStack->shapeBitmap;
4558
4.65k
  isolated = transpGroupStack->isolated;
4559
4560
  // paint the transparency group onto the parent bitmap
4561
  // - the clip path was set in the parent's state)
4562
4.65k
  if (xDest < bitmap->getWidth() && yDest < bitmap->getHeight() &&
4563
2.82k
      w > 0 && h > 0) {
4564
2.82k
    splash->setOverprintMask(0xffffffff);
4565
2.82k
    splash->composite(tBitmap, shapeBitmap, xSrc, ySrc, xDest, yDest, w, h,
4566
2.82k
          gFalse, !isolated);
4567
2.82k
  }
4568
4569
  // free the shape bitmap
4570
4.65k
  if (shapeBitmap) {
4571
0
    delete shapeBitmap;
4572
0
  }
4573
4574
  // free the alpha0 bitmap
4575
4.65k
  if (transpGroupStack->alpha0Bitmap) {
4576
1.34k
    delete transpGroupStack->alpha0Bitmap;
4577
1.34k
  }
4578
4579
  // pop the stack
4580
4.65k
  transpGroup = transpGroupStack;
4581
4.65k
  transpGroupStack = transpGroup->next;
4582
4.65k
  delete transpGroup;
4583
4584
4.65k
  delete tBitmap;
4585
4.65k
}
4586
4587
void SplashOutputDev::setSoftMask(GfxState *state, double *bbox,
4588
          GBool alpha, Function *transferFunc,
4589
2.06k
          GfxColor *backdropColor) {
4590
2.06k
  SplashBitmap *softMask, *tBitmap;
4591
2.06k
  Splash *tSplash;
4592
2.06k
  SplashTransparencyGroup *transpGroup;
4593
2.06k
  SplashColor color;
4594
2.06k
  SplashColorPtr p, colorPtr, colorPtr2;
4595
2.06k
  Guchar *alphaPtr;
4596
2.06k
  GfxGray gray;
4597
2.06k
  GfxRGB rgb;
4598
2.06k
#if SPLASH_CMYK
4599
2.06k
  GfxCMYK cmyk;
4600
2.06k
#endif
4601
2.06k
  double backdrop, backdrop2, lum, lum2;
4602
2.06k
  Guchar lum8;
4603
2.06k
  SplashBitmapRowSize rowSize;
4604
2.06k
  int tw, th, tNComps, tx, ty, x, y;
4605
4606
2.06k
  tx = transpGroupStack->tx;
4607
2.06k
  ty = transpGroupStack->ty;
4608
2.06k
  tBitmap = transpGroupStack->tBitmap;
4609
4610
  // composite with backdrop color
4611
2.06k
  backdrop = 0;
4612
2.06k
  if (!alpha && tBitmap->getMode() != splashModeMono1) {
4613
    //~ need to correctly handle the case where no blending color
4614
    //~ space is given
4615
1.95k
    if (transpGroupStack->blendingColorSpace) {
4616
1.24k
      tSplash = new Splash(tBitmap, vectorAntialias,
4617
1.24k
         transpGroupStack->origSplash->getImageCache(),
4618
1.24k
         transpGroupStack->origSplash->getScreen());
4619
1.24k
      tSplash->setStrokeAdjust(
4620
1.24k
          mapStrokeAdjustMode[globalParams->getStrokeAdjust()]);
4621
1.24k
      tSplash->setEnablePathSimplification(
4622
1.24k
          globalParams->getEnablePathSimplification());
4623
1.24k
      switch (tBitmap->getMode()) {
4624
0
      case splashModeMono1:
4625
  // transparency is not supported in mono1 mode
4626
0
  break;
4627
895
      case splashModeMono8:
4628
895
  transpGroupStack->blendingColorSpace->getGray(
4629
895
        backdropColor, &gray, state->getRenderingIntent());
4630
895
  backdrop = colToDbl(gray);
4631
895
  color[0] = colToByte(gray);
4632
895
  tSplash->compositeBackground(color);
4633
895
  break;
4634
0
      case splashModeRGB8:
4635
0
      case splashModeBGR8:
4636
0
  transpGroupStack->blendingColorSpace->getRGB(
4637
0
        backdropColor, &rgb, state->getRenderingIntent());
4638
0
  backdrop = 0.3 * colToDbl(rgb.r) +
4639
0
             0.59 * colToDbl(rgb.g) +
4640
0
             0.11 * colToDbl(rgb.b);
4641
0
  color[0] = colToByte(rgb.r);
4642
0
  color[1] = colToByte(rgb.g);
4643
0
  color[2] = colToByte(rgb.b);
4644
0
  tSplash->compositeBackground(color);
4645
0
  break;
4646
0
#if SPLASH_CMYK
4647
349
      case splashModeCMYK8:
4648
349
  transpGroupStack->blendingColorSpace->getCMYK(
4649
349
        backdropColor, &cmyk, state->getRenderingIntent());
4650
349
  backdrop = (1 - colToDbl(cmyk.k))
4651
349
             - 0.3 * colToDbl(cmyk.c)
4652
349
             - 0.59 * colToDbl(cmyk.m)
4653
349
             - 0.11 * colToDbl(cmyk.y);
4654
349
  if (backdrop < 0) {
4655
243
    backdrop = 0;
4656
243
  }
4657
349
  color[0] = colToByte(cmyk.c);
4658
349
  color[1] = colToByte(cmyk.m);
4659
349
  color[2] = colToByte(cmyk.y);
4660
349
  color[3] = colToByte(cmyk.k);
4661
349
  tSplash->compositeBackground(color);
4662
349
  break;
4663
1.24k
#endif
4664
1.24k
      }
4665
1.24k
      delete tSplash;
4666
1.24k
    }
4667
1.95k
  }
4668
2.06k
  if (transferFunc) {
4669
86
    transferFunc->transform(&backdrop, &backdrop2);
4670
1.98k
  } else {
4671
1.98k
    backdrop2 = backdrop;
4672
1.98k
  }
4673
4674
2.06k
  traceMessage("soft mask bitmap");
4675
2.06k
  softMask = new SplashBitmap(bitmap->getWidth(), bitmap->getHeight(),
4676
2.06k
            1, splashModeMono8, gFalse, gTrue,
4677
2.06k
            bitmapMemCache);
4678
2.06k
  memset(softMask->getDataPtr(), (int)(backdrop2 * 255.0 + 0.5),
4679
2.06k
   softMask->getRowSize() * softMask->getHeight());
4680
2.06k
  if (tx < softMask->getWidth() && ty < softMask->getHeight()) {
4681
2.06k
    p = softMask->getDataPtr() + ty * softMask->getRowSize() + tx;
4682
2.06k
    tw = tBitmap->getWidth();
4683
2.06k
    th = tBitmap->getHeight();
4684
2.06k
    rowSize = softMask->getRowSize();
4685
2.06k
    if (alpha) {
4686
113
      alphaPtr = tBitmap->getAlphaPtr();
4687
226
      for (y = 0; y < th; ++y) {
4688
226
  for (x = 0; x < tw; ++x) {
4689
113
    lum = *alphaPtr++ / 255.0;
4690
113
    if (transferFunc) {
4691
48
      transferFunc->transform(&lum, &lum2);
4692
65
    } else {
4693
65
      lum2 = lum;
4694
65
    }
4695
113
    p[x] = (Guchar)(lum2 * 255.0 + 0.5);
4696
113
  }
4697
113
  p += rowSize;
4698
113
      }
4699
1.95k
    } else {
4700
1.95k
      colorPtr = tBitmap->getDataPtr();
4701
1.95k
      tNComps = splashColorModeNComps[tBitmap->getMode()];
4702
1.95k
      lum8 = 0;  // make gcc happy
4703
3.91k
      for (y = 0; y < th; ++y) {
4704
1.95k
  colorPtr2 = colorPtr;
4705
3.91k
  for (x = 0; x < tw; ++x) {
4706
    // convert to luminosity
4707
1.95k
    switch (tBitmap->getMode()) {
4708
0
    case splashModeMono1:
4709
0
      lum8 = 0;
4710
0
      break;
4711
895
    case splashModeMono8:
4712
895
      lum8 = colorPtr2[0];
4713
895
      break;
4714
712
    case splashModeRGB8:
4715
712
    case splashModeBGR8:
4716
      // [0.3, 0.59, 0.11] * 255 = [77, 150, 28]
4717
712
      lum8 = div255(77 * colorPtr2[0] +
4718
712
        150 * colorPtr2[1] +
4719
712
        28 * colorPtr2[2]);
4720
712
      break;
4721
0
#if SPLASH_CMYK
4722
349
    case splashModeCMYK8:
4723
349
      lum8 = clip255(255 - colorPtr2[3]
4724
349
         - div255(77 * colorPtr2[0] +
4725
349
            150 * colorPtr2[1] +
4726
349
            28 * colorPtr2[2]));
4727
349
      break;
4728
1.95k
#endif
4729
1.95k
    }
4730
1.95k
    if (transferFunc) {
4731
38
      lum = lum8 / 255.0;
4732
38
      transferFunc->transform(&lum, &lum2);
4733
38
      lum8 = (Guchar)(lum2 * 255.0 + 0.5);
4734
38
    }
4735
1.95k
    p[x] = lum8;
4736
1.95k
    colorPtr2 += tNComps;
4737
1.95k
  }
4738
1.95k
  p += rowSize;
4739
1.95k
  colorPtr += tBitmap->getRowSize();
4740
1.95k
      }
4741
1.95k
    }
4742
2.06k
  }
4743
2.06k
  splash->setSoftMask(softMask);
4744
4745
  // free the alpha0 bitmap
4746
2.06k
  if (transpGroupStack->alpha0Bitmap) {
4747
0
    delete transpGroupStack->alpha0Bitmap;
4748
0
  }
4749
4750
  // pop the stack
4751
2.06k
  transpGroup = transpGroupStack;
4752
2.06k
  transpGroupStack = transpGroup->next;
4753
2.06k
  delete transpGroup;
4754
4755
2.06k
  delete tBitmap;
4756
2.06k
}
4757
4758
8.03k
void SplashOutputDev::clearSoftMask(GfxState *state) {
4759
8.03k
  splash->setSoftMask(NULL);
4760
8.03k
}
4761
4762
0
void SplashOutputDev::setPaperColor(SplashColorPtr paperColorA) {
4763
0
  splashColorCopy(paperColor, paperColorA);
4764
0
}
4765
4766
0
int SplashOutputDev::getBitmapWidth() {
4767
0
  return bitmap->getWidth();
4768
0
}
4769
4770
0
int SplashOutputDev::getBitmapHeight() {
4771
0
  return bitmap->getHeight();
4772
0
}
4773
4774
0
SplashBitmap *SplashOutputDev::takeBitmap() {
4775
0
  SplashBitmap *ret;
4776
4777
0
  ret = bitmap;
4778
0
  ret->doNotCache();
4779
0
  bitmap = new SplashBitmap(1, 1, bitmapRowPad, colorMode,
4780
0
          colorMode != splashModeMono1, bitmapTopDown,
4781
0
          bitmapMemCache);
4782
0
  return ret;
4783
0
}
4784
4785
void SplashOutputDev::getModRegion(int *xMin, int *yMin,
4786
0
           int *xMax, int *yMax) {
4787
0
  splash->getModRegion(xMin, yMin, xMax, yMax);
4788
0
}
4789
4790
0
void SplashOutputDev::clearModRegion() {
4791
0
  splash->clearModRegion();
4792
0
}
4793
4794
0
void SplashOutputDev::setFillColor(int r, int g, int b) {
4795
0
  GfxRGB rgb;
4796
0
  GfxGray gray;
4797
0
#if SPLASH_CMYK
4798
0
  GfxCMYK cmyk;
4799
0
#endif
4800
4801
0
  rgb.r = byteToCol((Guchar)r);
4802
0
  rgb.g = byteToCol((Guchar)g);
4803
0
  rgb.b = byteToCol((Guchar)b);
4804
0
  switch (colorMode) {
4805
0
  case splashModeMono1:
4806
0
  case splashModeMono8:
4807
0
    gray = (GfxColorComp)(0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.g + 0.5);
4808
0
    if (gray > gfxColorComp1) {
4809
0
      gray = gfxColorComp1;
4810
0
    }
4811
0
    splash->setFillPattern(getColor(gray));
4812
0
    break;
4813
0
  case splashModeRGB8:
4814
0
  case splashModeBGR8:
4815
0
    splash->setFillPattern(getColor(&rgb));
4816
0
    break;
4817
0
#if SPLASH_CMYK
4818
0
  case splashModeCMYK8:
4819
0
    cmyk.c = gfxColorComp1 - rgb.r;
4820
0
    cmyk.m = gfxColorComp1 - rgb.g;
4821
0
    cmyk.y = gfxColorComp1 - rgb.b;
4822
0
    cmyk.k = 0;
4823
0
    splash->setFillPattern(getColor(&cmyk));
4824
0
    break;
4825
0
#endif
4826
0
  }
4827
0
}
4828
4829
0
SplashFont *SplashOutputDev::getFont(GString *name, SplashCoord *textMatA) {
4830
0
  Ref ref;
4831
0
  SplashOutFontFileID *id;
4832
0
  GfxFontLoc *fontLoc;
4833
#if LOAD_FONTS_FROM_MEM
4834
  GString *fontBuf;
4835
  FILE *extFontFile;
4836
  char blk[4096];
4837
  int n;
4838
#endif
4839
0
  SplashFontFile *fontFile;
4840
0
  SplashFont *fontObj;
4841
0
  FoFiTrueType *ff;
4842
0
  int *codeToGID;
4843
0
  Unicode u;
4844
0
  SplashCoord textMat[4];
4845
0
  SplashCoord oblique;
4846
0
  int cmap, cmapPlatform, cmapEncoding, i;
4847
4848
0
  for (i = 0; i < nBuiltinFonts; ++i) {
4849
0
    if (!name->cmp(builtinFonts[i].name)) {
4850
0
      break;
4851
0
    }
4852
0
  }
4853
0
  if (i == nBuiltinFonts) {
4854
0
    return NULL;
4855
0
  }
4856
0
  ref.num = i;
4857
0
  ref.gen = -1;
4858
0
  id = new SplashOutFontFileID(&ref);
4859
4860
  // check the font file cache
4861
0
  if ((fontFile = fontEngine->getFontFile(id))) {
4862
0
    delete id;
4863
4864
  // load the font file
4865
0
  } else {
4866
0
    if (!(fontLoc = GfxFont::locateBase14Font(name))) {
4867
0
      return NULL;
4868
0
    }
4869
#if LOAD_FONTS_FROM_MEM
4870
    fontBuf = NULL;
4871
    if (fontLoc->fontType == fontType1 ||
4872
  fontLoc->fontType == fontTrueType) {
4873
      if (!(extFontFile = fopen(fontLoc->path->getCString(), "rb"))) {
4874
  delete fontLoc;
4875
  delete id;
4876
  return NULL;
4877
      }
4878
      fontBuf = new GString();
4879
      while ((n = fread(blk, 1, sizeof(blk), extFontFile)) > 0) {
4880
  fontBuf->append(blk, n);
4881
      }
4882
      fclose(extFontFile);
4883
    }
4884
#endif
4885
0
    if (fontLoc->fontType == fontType1) {
4886
0
      fontFile = fontEngine->loadType1Font(id,
4887
#if LOAD_FONTS_FROM_MEM
4888
             fontBuf,
4889
#else
4890
0
             fontLoc->path->getCString(),
4891
0
             gFalse,
4892
0
#endif
4893
0
             winAnsiEncoding);
4894
0
    } else if (fontLoc->fontType == fontTrueType) {
4895
#if LOAD_FONTS_FROM_MEM
4896
      if (!(ff = FoFiTrueType::make(fontBuf->getCString(),
4897
            fontBuf->getLength(),
4898
            fontLoc->fontNum))) {
4899
#else
4900
0
      if (!(ff = FoFiTrueType::load(fontLoc->path->getCString(),
4901
0
            fontLoc->fontNum))) {
4902
0
#endif
4903
0
  delete fontLoc;
4904
0
  delete id;
4905
0
  return NULL;
4906
0
      }
4907
0
      for (cmap = 0; cmap < ff->getNumCmaps(); ++cmap) {
4908
0
  cmapPlatform = ff->getCmapPlatform(cmap);
4909
0
  cmapEncoding = ff->getCmapEncoding(cmap);
4910
0
  if ((cmapPlatform == 3 && cmapEncoding == 1) ||
4911
0
      (cmapPlatform == 0 && cmapEncoding <= 4)) {
4912
0
    break;
4913
0
  }
4914
0
      }
4915
0
      if (cmap == ff->getNumCmaps()) {
4916
0
  delete ff;
4917
0
  delete fontLoc;
4918
0
  delete id;
4919
0
  return NULL;
4920
0
      }
4921
0
      codeToGID = (int *)gmallocn(256, sizeof(int));
4922
0
      for (i = 0; i < 256; ++i) {
4923
0
  codeToGID[i] = 0;
4924
0
  if (winAnsiEncoding[i] &&
4925
0
      (u = globalParams->mapNameToUnicode(winAnsiEncoding[i]))) {
4926
0
    codeToGID[i] = ff->mapCodeToGID(cmap, u);
4927
0
  }
4928
0
      }
4929
0
      delete ff;
4930
0
      fontFile = fontEngine->loadTrueTypeFont(id,
4931
#if LOAD_FONTS_FROM_MEM
4932
                fontBuf,
4933
#else
4934
0
                fontLoc->path->getCString(),
4935
0
                gFalse,
4936
0
#endif
4937
0
                fontLoc->fontNum,
4938
0
                codeToGID, 256, NULL);
4939
0
    } else {
4940
0
      delete fontLoc;
4941
0
      delete id;
4942
0
      return NULL;
4943
0
    }
4944
0
    delete fontLoc;
4945
0
  }
4946
0
  if (!fontFile) {
4947
0
    return NULL;
4948
0
  }
4949
4950
  // create the scaled font
4951
0
  oblique = (SplashCoord)
4952
0
              ((SplashOutFontFileID *)fontFile->getID())->getOblique();
4953
0
  textMat[0] = (SplashCoord)textMatA[0];
4954
0
  textMat[1] = (SplashCoord)textMatA[1];
4955
0
  textMat[2] = oblique * textMatA[0] + textMatA[2];
4956
0
  textMat[3] = oblique * textMatA[1] + textMatA[3];
4957
0
  fontObj = fontEngine->getFont(fontFile, textMat, splash->getMatrix());
4958
4959
0
  return fontObj;
4960
0
}
4961
4962
// This is called when initializing a temporary Splash object for Type
4963
// 3 characters and transparency groups.  Acrobat apparently copies at
4964
// least the fill and stroke colors, and the line parameters.
4965
//~ not sure what else should be copied -- the PDF spec is unclear
4966
//~   - fill and stroke alpha?
4967
6.99k
void SplashOutputDev::copyState(Splash *oldSplash, GBool copyColors) {
4968
  // cached Type 3 chars set a color, so no need to copy the color here
4969
6.99k
  if (copyColors) {
4970
6.72k
    splash->setFillPattern(oldSplash->getFillPattern()->copy());
4971
6.72k
    splash->setStrokePattern(oldSplash->getStrokePattern()->copy());
4972
6.72k
  }
4973
6.99k
  splash->setLineDash(oldSplash->getLineDash(),
4974
6.99k
          oldSplash->getLineDashLength(),
4975
6.99k
          oldSplash->getLineDashPhase());
4976
6.99k
  splash->setLineCap(oldSplash->getLineCap());
4977
6.99k
  splash->setLineJoin(oldSplash->getLineJoin());
4978
6.99k
  splash->setLineWidth(oldSplash->getLineWidth());
4979
6.99k
}