1 //========================================================================
2 //
3 // Splash.h
4 //
5 // Copyright 2003-2013 Glyph & Cog, LLC
6 //
7 //========================================================================
8 
9 #ifndef SPLASH_H
10 #define SPLASH_H
11 
12 #include <aconf.h>
13 
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17 
18 #include "SplashTypes.h"
19 #include "SplashClip.h"
20 
21 class Splash;
22 class SplashBitmap;
23 struct SplashGlyphBitmap;
24 class SplashState;
25 class SplashPattern;
26 class SplashScreen;
27 class SplashPath;
28 class SplashXPath;
29 class SplashFont;
30 struct SplashPipe;
31 
32 //------------------------------------------------------------------------
33 
34 // Retrieves the next line of pixels in an image mask.  Normally,
35 // fills in *<line> and returns true.  If the image stream is
36 // exhausted, returns false.
37 typedef GBool (*SplashImageMaskSource)(void *data, Guchar *pixel);
38 
39 // Retrieves the next line of pixels in an image.  Normally, fills in
40 // *<line> and returns true.  If the image stream is exhausted,
41 // returns false.
42 typedef GBool (*SplashImageSource)(void *data, SplashColorPtr colorLine,
43                                    Guchar *alphaLine);
44 
45 
46 //------------------------------------------------------------------------
47 
48 enum SplashPipeResultColorCtrl {
49     splashPipeResultColorNoAlphaBlendMono,
50     splashPipeResultColorNoAlphaBlendRGB,
51 #if SPLASH_CMYK
52     splashPipeResultColorNoAlphaBlendCMYK,
53 #endif
54     splashPipeResultColorAlphaNoBlendMono,
55     splashPipeResultColorAlphaNoBlendRGB,
56 #if SPLASH_CMYK
57     splashPipeResultColorAlphaNoBlendCMYK,
58 #endif
59     splashPipeResultColorAlphaBlendMono,
60     splashPipeResultColorAlphaBlendRGB
61 #if SPLASH_CMYK
62     ,
63     splashPipeResultColorAlphaBlendCMYK
64 #endif
65 };
66 
67 //------------------------------------------------------------------------
68 // Splash
69 //------------------------------------------------------------------------
70 
71 class Splash {
72 public:
73 
74     // Create a new rasterizer object.
75     Splash(SplashBitmap *bitmapA, GBool vectorAntialiasA,
76            SplashScreenParams *screenParams = NULL);
77     Splash(SplashBitmap *bitmapA, GBool vectorAntialiasA,
78            SplashScreen *screenA);
79 
80     ~Splash();
81 
82     //----- state read
83 
84     SplashCoord *getMatrix();
85     SplashPattern *getStrokePattern();
86     SplashPattern *getFillPattern();
87     SplashScreen *getScreen();
88     SplashBlendFunc getBlendFunc();
89     SplashCoord getStrokeAlpha();
90     SplashCoord getFillAlpha();
91     SplashCoord getLineWidth();
92     int getLineCap();
93     int getLineJoin();
94     SplashCoord getMiterLimit();
95     SplashCoord getFlatness();
96     SplashCoord *getLineDash();
97     int getLineDashLength();
98     SplashCoord getLineDashPhase();
99     SplashStrokeAdjustMode getStrokeAdjust();
100     SplashClip *getClip();
101     SplashBitmap *getSoftMask();
102     GBool getInNonIsolatedGroup();
103     GBool getInKnockoutGroup();
104 
105     //----- state write
106 
107     void setMatrix(SplashCoord *matrix);
108     void setStrokePattern(SplashPattern *strokeColor);
109     void setFillPattern(SplashPattern *fillColor);
110     void setScreen(SplashScreen *screen);
111     void setBlendFunc(SplashBlendFunc func);
112     void setStrokeAlpha(SplashCoord alpha);
113     void setFillAlpha(SplashCoord alpha);
114     void setLineWidth(SplashCoord lineWidth);
115     void setLineCap(int lineCap);
116     void setLineJoin(int lineJoin);
117     void setMiterLimit(SplashCoord miterLimit);
118     void setFlatness(SplashCoord flatness);
119     // the <lineDash> array will be copied
120     void setLineDash(SplashCoord *lineDash, int lineDashLength,
121                      SplashCoord lineDashPhase);
122     void setStrokeAdjust(SplashStrokeAdjustMode strokeAdjust);
123     // NB: uses transformed coordinates.
124     void clipResetToRect(SplashCoord x0, SplashCoord y0,
125                          SplashCoord x1, SplashCoord y1);
126     // NB: uses transformed coordinates.
127     SplashError clipToRect(SplashCoord x0, SplashCoord y0,
128                            SplashCoord x1, SplashCoord y1);
129     // NB: uses untransformed coordinates.
130     SplashError clipToPath(SplashPath *path, GBool eo);
131     void setSoftMask(SplashBitmap *softMask);
132     void setInTransparencyGroup(SplashBitmap *groupBackBitmapA,
133                                 int groupBackXA, int groupBackYA,
134                                 GBool nonIsolated, GBool knockout);
135     void setTransfer(Guchar *red, Guchar *green, Guchar *blue, Guchar *gray);
136     void setOverprintMask(Guint overprintMask);
137     void setEnablePathSimplification(GBool en);
138 
139     //----- state save/restore
140 
141     void saveState();
142     SplashError restoreState();
143 
144     //----- drawing operations
145 
146     // Fill the bitmap with <color>.  This is not subject to clipping.
147     void clear(SplashColorPtr color, Guchar alpha = 0x00);
148 
149     // Stroke a path using the current stroke pattern.
150     SplashError stroke(SplashPath *path);
151 
152     // Fill a path using the current fill pattern.
153     SplashError fill(SplashPath *path, GBool eo);
154 
155     // Draw a character, using the current fill pattern.
156     SplashError fillChar(SplashCoord x, SplashCoord y, int c, SplashFont *font);
157 
158     // Draw a glyph, using the current fill pattern.  This function does
159     // not free any data, i.e., it ignores glyph->freeData.
160     SplashError fillGlyph(SplashCoord x, SplashCoord y,
161                           SplashGlyphBitmap *glyph);
162 
163     // Draws an image mask using the fill color.  This will read <h>
164     // lines of <w> pixels from <src>, starting with the top line.  "1"
165     // pixels will be drawn with the current fill color; "0" pixels are
166     // transparent.  The matrix:
167     //    [ mat[0] mat[1] 0 ]
168     //    [ mat[2] mat[3] 0 ]
169     //    [ mat[4] mat[5] 1 ]
170     // maps a unit square to the desired destination for the image, in
171     // PostScript style:
172     //    [x' y' 1] = [x y 1] * mat
173     // Note that the Splash y axis points downward, and the image source
174     // is assumed to produce pixels in raster order, starting from the
175     // top line.
176     SplashError fillImageMask(SplashImageMaskSource src, void *srcData,
177                               int w, int h, SplashCoord *mat,
178                               GBool glyphMode, GBool interpolate);
179 
180     // Draw an image.  This will read <h> lines of <w> pixels from
181     // <src>, starting with the top line.  These pixels are assumed to
182     // be in the source mode, <srcMode>.  If <srcAlpha> is true, the
183     // alpha values returned by <src> are used; otherwise they are
184     // ignored.  The following combinations of source and target modes
185     // are supported:
186     //    source       target
187     //    ------       ------
188     //    Mono8        Mono1   -- with dithering
189     //    Mono8        Mono8
190     //    RGB8         RGB8
191     //    BGR8         RGB8
192     //    CMYK8        CMYK8
193     // The matrix behaves as for fillImageMask.
194     SplashError drawImage(SplashImageSource src, void *srcData,
195                           SplashColorMode srcMode, GBool srcAlpha,
196                           int w, int h, SplashCoord *mat,
197                           GBool interpolate);
198 
199     // Composite a rectangular region from <src> onto this Splash
200     // object.
201     SplashError composite(SplashBitmap *src, int xSrc, int ySrc,
202                           int xDest, int yDest, int w, int h,
203                           GBool noClip, GBool nonIsolated);
204 
205     // Composite this Splash object onto a background color.  The
206     // background alpha is assumed to be 1.
207     void compositeBackground(SplashColorPtr color);
208 
209     // Copy a rectangular region from <src> onto the bitmap belonging to
210     // this Splash object.  The destination alpha values are all set to
211     // zero.
212     SplashError blitTransparent(SplashBitmap *src, int xSrc, int ySrc,
213                                 int xDest, int yDest, int w, int h);
214 
215     // Copy a rectangular region from the bitmap belonging to this
216     // Splash object to <dest>.  The alpha values are corrected for a
217     // non-isolated group.
218     SplashError blitCorrectedAlpha(SplashBitmap *dest, int xSrc, int ySrc,
219                                    int xDest, int yDest, int w, int h);
220 
221     //----- misc
222 
223     // Construct a path for a stroke, given the path to be stroked and
224     // the line width <w>.  All other stroke parameters are taken from
225     // the current state.  If <flatten> is true, this function will
226     // first flatten the path and handle the linedash.
227     SplashPath *makeStrokePath(SplashPath *path, SplashCoord w,
228                                int lineCap, int lineJoin,
229                                GBool flatten = gTrue);
230 
231     // Reduce the size of a rectangle as much as possible by moving any
232     // edges that are completely outside the clip region.  Returns the
233     // clipping status of the resulting rectangle.
234     SplashClipResult limitRectToClipRect(int *xMin, int *yMin,
235                                          int *xMax, int *yMax);
236 
237     // Return the associated bitmap.
getBitmap()238     SplashBitmap *getBitmap() {
239         return bitmap;
240     }
241 
242     // Set the minimum line width.
setMinLineWidth(SplashCoord w)243     void setMinLineWidth(SplashCoord w) {
244         minLineWidth = w;
245     }
246 
247     // Get a bounding box which includes all modifications since the
248     // last call to clearModRegion.
getModRegion(int * xMin,int * yMin,int * xMax,int * yMax)249     void getModRegion(int *xMin, int *yMin, int *xMax, int *yMax)
250     {
251         *xMin = modXMin; *yMin = modYMin; *xMax = modXMax; *yMax = modYMax;
252     }
253 
254     // Clear the modified region bounding box.
255     void clearModRegion();
256 
257     // Get clipping status for the last drawing operation subject to
258     // clipping.
getClipRes()259     SplashClipResult getClipRes() {
260         return opClipRes;
261     }
262 
263     // Toggle debug mode on or off.
setDebugMode(GBool debugModeA)264     void setDebugMode(GBool debugModeA) {
265         debugMode = debugModeA;
266     }
267 
268 #if 1 //~tmp: turn off anti-aliasing temporarily
setInShading(GBool sh)269     void setInShading(GBool sh) {
270         inShading = sh;
271     }
272 #endif
273 
274 
275 private:
276 
277     void pipeInit(SplashPipe *pipe, SplashPattern *pattern,
278                   Guchar aInput, GBool usesShape,
279                   GBool nonIsolatedGroup);
280     void pipeRun(SplashPipe *pipe, int x0, int x1, int y,
281                  Guchar *shapePtr, SplashColorPtr cSrcPtr);
282     void pipeRunSimpleMono1(SplashPipe *pipe, int x0, int x1, int y,
283                             Guchar *shapePtr, SplashColorPtr cSrcPtr);
284     void pipeRunSimpleMono8(SplashPipe *pipe, int x0, int x1, int y,
285                             Guchar *shapePtr, SplashColorPtr cSrcPtr);
286     void pipeRunSimpleRGB8(SplashPipe *pipe, int x0, int x1, int y,
287                            Guchar *shapePtr, SplashColorPtr cSrcPtr);
288     void pipeRunSimpleBGR8(SplashPipe *pipe, int x0, int x1, int y,
289                            Guchar *shapePtr, SplashColorPtr cSrcPtr);
290 #if SPLASH_CMYK
291     void pipeRunSimpleCMYK8(SplashPipe *pipe, int x0, int x1, int y,
292                             Guchar *shapePtr, SplashColorPtr cSrcPtr);
293 #endif
294     void pipeRunShapeMono1(SplashPipe *pipe, int x0, int x1, int y,
295                            Guchar *shapePtr, SplashColorPtr cSrcPtr);
296     void pipeRunShapeMono8(SplashPipe *pipe, int x0, int x1, int y,
297                            Guchar *shapePtr, SplashColorPtr cSrcPtr);
298     void pipeRunShapeRGB8(SplashPipe *pipe, int x0, int x1, int y,
299                           Guchar *shapePtr, SplashColorPtr cSrcPtr);
300     void pipeRunShapeBGR8(SplashPipe *pipe, int x0, int x1, int y,
301                           Guchar *shapePtr, SplashColorPtr cSrcPtr);
302 #if SPLASH_CMYK
303     void pipeRunShapeCMYK8(SplashPipe *pipe, int x0, int x1, int y,
304                            Guchar *shapePtr, SplashColorPtr cSrcPtr);
305 #endif
306     void pipeRunAAMono1(SplashPipe *pipe, int x0, int x1, int y,
307                         Guchar *shapePtr, SplashColorPtr cSrcPtr);
308     void pipeRunAAMono8(SplashPipe *pipe, int x0, int x1, int y,
309                         Guchar *shapePtr, SplashColorPtr cSrcPtr);
310     void pipeRunAARGB8(SplashPipe *pipe, int x0, int x1, int y,
311                        Guchar *shapePtr, SplashColorPtr cSrcPtr);
312     void pipeRunAABGR8(SplashPipe *pipe, int x0, int x1, int y,
313                        Guchar *shapePtr, SplashColorPtr cSrcPtr);
314 #if SPLASH_CMYK
315     void pipeRunAACMYK8(SplashPipe *pipe, int x0, int x1, int y,
316                         Guchar *shapePtr, SplashColorPtr cSrcPtr);
317 #endif
318     void transform(SplashCoord *matrix, SplashCoord xi, SplashCoord yi,
319                    SplashCoord *xo, SplashCoord *yo);
320     void updateModX(int x);
321     void updateModY(int y);
322     void strokeNarrow(SplashPath *path);
323     void drawStrokeSpan(SplashPipe *pipe, int x0, int x1, int y, GBool noClip);
324     void strokeWide(SplashPath *path, SplashCoord w,
325                     int lineCap, int lineJoin);
326     SplashPath *flattenPath(SplashPath *path, SplashCoord *matrix,
327                             SplashCoord flatness);
328     void flattenCurve(SplashCoord x0, SplashCoord y0,
329                       SplashCoord x1, SplashCoord y1,
330                       SplashCoord x2, SplashCoord y2,
331                       SplashCoord x3, SplashCoord y3,
332                       SplashCoord *matrix, SplashCoord flatness2,
333                       SplashPath *fPath);
334     SplashPath *makeDashedPath(SplashPath *xPath);
335     SplashError fillWithPattern(SplashPath *path, GBool eo,
336                                 SplashPattern *pattern, SplashCoord alpha);
337     SplashPath *tweakFillPath(SplashPath *path);
338     GBool pathAllOutside(SplashPath *path);
339     SplashError fillGlyph2(int x0, int y0, SplashGlyphBitmap *glyph);
340     void getImageBounds(SplashCoord xyMin, SplashCoord xyMax,
341                         int *xyMinI, int *xyMaxI);
342     void upscaleMask(SplashImageMaskSource src, void *srcData,
343                      int srcWidth, int srcHeight,
344                      SplashCoord *mat, GBool glyphMode,
345                      GBool interpolate);
346     void arbitraryTransformMask(SplashImageMaskSource src, void *srcData,
347                                 int srcWidth, int srcHeight,
348                                 SplashCoord *mat, GBool glyphMode,
349                                 GBool interpolate);
350     SplashBitmap *scaleMask(SplashImageMaskSource src, void *srcData,
351                             int srcWidth, int srcHeight,
352                             int scaledWidth, int scaledHeight,
353                             GBool interpolate);
354     void scaleMaskYdXd(SplashImageMaskSource src, void *srcData,
355                        int srcWidth, int srcHeight,
356                        int scaledWidth, int scaledHeight,
357                        SplashBitmap *dest);
358     void scaleMaskYdXu(SplashImageMaskSource src, void *srcData,
359                        int srcWidth, int srcHeight,
360                        int scaledWidth, int scaledHeight,
361                        SplashBitmap *dest);
362     void scaleMaskYuXd(SplashImageMaskSource src, void *srcData,
363                        int srcWidth, int srcHeight,
364                        int scaledWidth, int scaledHeight,
365                        SplashBitmap *dest);
366     void scaleMaskYuXu(SplashImageMaskSource src, void *srcData,
367                        int srcWidth, int srcHeight,
368                        int scaledWidth, int scaledHeight,
369                        SplashBitmap *dest);
370     void scaleMaskYuXuI(SplashImageMaskSource src, void *srcData,
371                         int srcWidth, int srcHeight,
372                         int scaledWidth, int scaledHeight,
373                         SplashBitmap *dest);
374     void blitMask(SplashBitmap *src, int xDest, int yDest,
375                   SplashClipResult clipRes);
376     void upscaleImage(SplashImageSource src, void *srcData,
377                       SplashColorMode srcMode, int nComps,
378                       GBool srcAlpha, int srcWidth, int srcHeight,
379                       SplashCoord *mat, GBool interpolate);
380     void arbitraryTransformImage(SplashImageSource src, void *srcData,
381                                  SplashColorMode srcMode, int nComps,
382                                  GBool srcAlpha,
383                                  int srcWidth, int srcHeight,
384                                  SplashCoord *mat, GBool interpolate);
385     SplashBitmap *scaleImage(SplashImageSource src, void *srcData,
386                              SplashColorMode srcMode, int nComps,
387                              GBool srcAlpha, int srcWidth, int srcHeight,
388                              int scaledWidth, int scaledHeight,
389                              GBool interpolate);
390     void scaleImageYdXd(SplashImageSource src, void *srcData,
391                         SplashColorMode srcMode, int nComps,
392                         GBool srcAlpha, int srcWidth, int srcHeight,
393                         int scaledWidth, int scaledHeight,
394                         SplashBitmap *dest);
395     void scaleImageYdXu(SplashImageSource src, void *srcData,
396                         SplashColorMode srcMode, int nComps,
397                         GBool srcAlpha, int srcWidth, int srcHeight,
398                         int scaledWidth, int scaledHeight,
399                         SplashBitmap *dest);
400     void scaleImageYuXd(SplashImageSource src, void *srcData,
401                         SplashColorMode srcMode, int nComps,
402                         GBool srcAlpha, int srcWidth, int srcHeight,
403                         int scaledWidth, int scaledHeight,
404                         SplashBitmap *dest);
405     void scaleImageYuXu(SplashImageSource src, void *srcData,
406                         SplashColorMode srcMode, int nComps,
407                         GBool srcAlpha, int srcWidth, int srcHeight,
408                         int scaledWidth, int scaledHeight,
409                         SplashBitmap *dest);
410     void scaleImageYuXuI(SplashImageSource src, void *srcData,
411                          SplashColorMode srcMode, int nComps,
412                          GBool srcAlpha, int srcWidth, int srcHeight,
413                          int scaledWidth, int scaledHeight,
414                          SplashBitmap *dest);
415     void vertFlipImage(SplashBitmap *img, int width, int height,
416                        int nComps);
417     void horizFlipImage(SplashBitmap *img, int width, int height,
418                         int nComps);
419     void blitImage(SplashBitmap *src, GBool srcAlpha, int xDest, int yDest,
420                    SplashClipResult clipRes);
421     void blitImageClipped(SplashBitmap *src, GBool srcAlpha,
422                           int xSrc, int ySrc, int xDest, int yDest,
423                           int w, int h);
424     void dumpPath(SplashPath *path);
425     void dumpXPath(SplashXPath *path);
426 
427 
428     static SplashPipeResultColorCtrl pipeResultColorNoAlphaBlend[];
429     static SplashPipeResultColorCtrl pipeResultColorAlphaNoBlend[];
430     static SplashPipeResultColorCtrl pipeResultColorAlphaBlend[];
431     static int pipeNonIsoGroupCorrection[];
432 
433     SplashBitmap *bitmap;
434     int bitmapComps;
435     SplashState *state;
436     Guchar *scanBuf;
437     Guchar *scanBuf2;
438     SplashBitmap        // for transparency groups, this is the bitmap
439     *groupBackBitmap;       //   containing the alpha0/color0 values
440     int groupBackX, groupBackY; // offset within groupBackBitmap
441     SplashCoord minLineWidth;
442     int modXMin, modYMin, modXMax, modYMax;
443     SplashClipResult opClipRes;
444     GBool vectorAntialias;
445     GBool inShading;
446     GBool debugMode;
447 };
448 
449 #endif
450