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