lib/pdf: make startPage() upstream compatible
[swftools.git] / lib / pdf / FullBitmapOutputDev.cc
1 /* FullBitmapOutputDev.cc
2
3    Output Device which creates a bitmap.
4
5    Swftools is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    Swftools is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with swftools; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <memory.h>
22 #include "config.h"
23 #include "FullBitmapOutputDev.h"
24 #include "GFXOutputDev.h"
25 #include "SplashBitmap.h"
26 #include "SplashPattern.h"
27 #include "Splash.h"
28 #include "../log.h"
29 #include "../png.h"
30 #include "../devices/record.h"
31
32 static SplashColor splash_white = {255,255,255};
33 static SplashColor splash_black = {0,0,0};
34     
35 FullBitmapOutputDev::FullBitmapOutputDev(InfoOutputDev*info, PDFDoc*doc)
36 {
37     this->doc = doc;
38     this->xref = doc->getXRef();
39     
40     msg("<verbose> Rendering everything to a bitmap");
41     
42     /* color graphic output device, for creating bitmaps */
43     this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
44   
45     /* device for handling links */
46     this->gfxdev = new GFXOutputDev(info, this->doc);
47
48     this->rgbdev->startDoc(this->xref);
49 }
50 FullBitmapOutputDev::~FullBitmapOutputDev()
51 {
52     if(this->rgbdev) {
53         delete this->rgbdev;this->rgbdev = 0;
54     }
55     if(this->gfxdev) {
56         delete this->gfxdev;this->gfxdev= 0;
57     }
58 }
59
60 GBool FullBitmapOutputDev::getVectorAntialias()
61 {
62     return this->rgbdev->getVectorAntialias();
63 }
64 void FullBitmapOutputDev::setVectorAntialias(GBool vaa)
65 {
66     this->rgbdev->setVectorAntialias(vaa);
67 }
68 void FullBitmapOutputDev::setDevice(gfxdevice_t*dev)
69 {
70     this->dev = dev;
71     gfxdev->setDevice(dev);
72 }
73 void FullBitmapOutputDev::setMove(int x,int y)
74 {
75     this->user_movex = x;
76     this->user_movey = y;
77     gfxdev->setMove(x,y);
78 }
79 void FullBitmapOutputDev::setClip(int x1,int y1,int x2,int y2)
80 {
81     this->user_clipx1 = x1;
82     this->user_clipy1 = y1;
83     this->user_clipx2 = x2;
84     this->user_clipy2 = y2;
85     gfxdev->setClip(x1,y1,x2,y2);
86 }
87 void FullBitmapOutputDev::setParameter(const char*key, const char*value)
88 {
89 }
90 void FullBitmapOutputDev::setPageMap(int*pagemap, int pagemap_len)
91 {
92     gfxdev->setPageMap(pagemap, pagemap_len);
93 }
94
95 static void getBitmapBBox(Guchar*alpha, int width, int height, int*xmin, int*ymin, int*xmax, int*ymax)
96 {
97     *ymin = -1;
98     *xmin = width;
99     *xmax = 0;
100     int x,y;
101     for(y=0;y<height;y++) {
102         Guchar*a = &alpha[y*width];
103         for(x=0;x<width;x++) {
104             if(a[x]) break;
105         }
106         int left = x; //first occupied pixel from left
107         int right = x+1; //last non-occupied pixel from right
108         for(;x<width;x++) {
109             if(a[x]) right=x+1;
110         }
111
112         if(left!=width) {
113             if(*ymin<0) 
114                 *ymin=y;
115             *ymax=y+1;
116             if(left<*xmin) *xmin = left;
117             if(right>*xmax) *xmax = right;
118         }
119     }
120     if(*xmin>=*xmax || *ymin>=*ymax) {
121         *xmin = 0;
122         *ymin = 0;
123         *xmax = 0;
124         *ymax = 0;
125     }
126 }
127
128 void FullBitmapOutputDev::flushBitmap()
129 {
130     int width = rgbdev->getBitmapWidth();
131     int height = rgbdev->getBitmapHeight();
132     
133     SplashColorPtr rgb = rgbdev->getBitmap()->getDataPtr();
134     Guchar*alpha = rgbdev->getBitmap()->getAlphaPtr();
135
136     int xmin,ymin,xmax,ymax;
137     getBitmapBBox(alpha, width, height, &xmin,&ymin,&xmax,&ymax);
138
139     /* clip against (-movex, -movey, -movex+width, -movey+height) */
140     if(xmin < -this->movex) xmin = -this->movex;
141     if(ymin < -this->movey) ymin = -this->movey;
142     if(xmax > -this->movex + width) xmax = -this->movex+this->width;
143     if(ymax > -this->movey + height) ymax = -this->movey+this->height;
144
145     msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d)", xmin,ymin,xmax,ymax);
146     
147     if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
148         return;
149
150     if(sizeof(SplashColor)!=3) {
151         msg("<error> sizeof(SplashColor)!=3");
152         return;
153     }
154     //xmin = ymin = 0;
155     //xmax = width;
156     //ymax = height;
157
158     int rangex = xmax-xmin;
159     int rangey = ymax-ymin;
160     gfximage_t*img = (gfximage_t*)malloc(sizeof(gfximage_t)); 
161     img->data = (gfxcolor_t*)malloc(rangex * rangey * 4);
162     img->width = rangex;
163     img->height = rangey;
164     int x,y;
165     for(y=0;y<rangey;y++) {
166         SplashColorPtr in=&rgb[((y+ymin)*width+xmin)*sizeof(SplashColor)];
167         gfxcolor_t*out = &img->data[y*rangex];
168         Guchar*ain = &alpha[(y+ymin)*width+xmin];
169         for(x=0;x<rangex;x++) {
170             // blend against a white background
171             out[x].r = (in[x*3+0]*ain[x])/255 + 255-ain[x];
172             out[x].g = (in[x*3+1]*ain[x])/255 + 255-ain[x];
173             out[x].b = (in[x*3+2]*ain[x])/255 + 255-ain[x];
174             out[x].a = 255;//ain[x];
175         }
176     }
177     /* transform bitmap rectangle to "device space" */
178     xmin += movex;
179     ymin += movey;
180     xmax += movex;
181     ymax += movey;
182
183     gfxmatrix_t m;
184     m.tx = xmin;
185     m.ty = ymin;
186     m.m00 = m.m11 = 1;
187     m.m10 = m.m01 = 0;
188
189     gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
190     dev->fillbitmap(dev, line, img, &m, 0);
191     gfxline_free(line);
192
193     free(img->data);img->data=0;free(img);img=0;
194 }
195
196 GBool FullBitmapOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
197              int rotate, GBool useMediaBox, GBool crop,
198              int sliceX, int sliceY, int sliceW, int sliceH,
199              GBool printing, Catalog *catalog,
200              GBool (*abortCheckCbk)(void *data),
201              void *abortCheckCbkData)
202 {
203     this->setPage(page);
204     gfxdev->setPage(page);
205     return gTrue;
206 }
207
208 void FullBitmapOutputDev::startPage(int pageNum, GfxState *state)
209 {
210     double x1,y1,x2,y2;
211     PDFRectangle *r = page->getCropBox();
212     state->transform(r->x1,r->y1,&x1,&y1);
213     state->transform(r->x2,r->y2,&x2,&y2);
214     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
215     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
216     
217     this->movex = -(int)x1 - user_movex;
218     this->movey = -(int)y1 - user_movey;
219     
220     if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
221         x1 = user_clipx1;
222         x2 = user_clipx2;
223         y1 = user_clipy1;
224         y2 = user_clipy2;
225     }
226     this->width = (int)(x2-x1);
227     this->height = (int)(y2-y1);
228
229     msg("<debug> startPage");
230     rgbdev->startPage(pageNum, state);
231     gfxdev->startPage(pageNum, state);
232 }
233
234 void FullBitmapOutputDev::endPage()
235 {
236     msg("<verbose> endPage (FullBitmapOutputDev)");
237     flushBitmap();
238     rgbdev->endPage();
239     gfxdev->endPage();
240 }
241 GBool FullBitmapOutputDev::upsideDown()
242 {
243     return rgbdev->upsideDown();
244 }
245 GBool FullBitmapOutputDev::useDrawChar()
246 {
247     return rgbdev->useDrawChar();
248 }
249 GBool FullBitmapOutputDev::useTilingPatternFill()
250 {
251     return rgbdev->useTilingPatternFill();
252 }
253 GBool FullBitmapOutputDev::useShadedFills()
254 {
255     return rgbdev->useShadedFills();
256 }
257 GBool FullBitmapOutputDev::useDrawForm()
258 {
259     return rgbdev->useDrawForm();
260 }
261 GBool FullBitmapOutputDev::interpretType3Chars()
262 {
263     return rgbdev->interpretType3Chars();
264 }
265 GBool FullBitmapOutputDev::needNonText() 
266 {
267     return rgbdev->needNonText();
268 }
269 void FullBitmapOutputDev::setDefaultCTM(double *ctm) 
270 {
271     rgbdev->setDefaultCTM(ctm);
272     gfxdev->setDefaultCTM(ctm);
273 }
274 void FullBitmapOutputDev::saveState(GfxState *state) 
275 {
276     rgbdev->saveState(state);
277 }
278 void FullBitmapOutputDev::restoreState(GfxState *state) 
279 {
280     rgbdev->restoreState(state);
281 }
282 void FullBitmapOutputDev::updateAll(GfxState *state)
283 {
284     rgbdev->updateAll(state);
285 }
286 void FullBitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
287 {
288     rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
289     gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
290 }
291 void FullBitmapOutputDev::updateLineDash(GfxState *state)
292 {
293     rgbdev->updateLineDash(state);
294 }
295 void FullBitmapOutputDev::updateFlatness(GfxState *state)
296 {
297     rgbdev->updateFlatness(state);
298 }
299 void FullBitmapOutputDev::updateLineJoin(GfxState *state)
300 {
301     rgbdev->updateLineJoin(state);
302 }
303 void FullBitmapOutputDev::updateLineCap(GfxState *state)
304 {
305     rgbdev->updateLineCap(state);
306 }
307 void FullBitmapOutputDev::updateMiterLimit(GfxState *state)
308 {
309     rgbdev->updateMiterLimit(state);
310 }
311 void FullBitmapOutputDev::updateLineWidth(GfxState *state)
312 {
313     rgbdev->updateLineWidth(state);
314 }
315 void FullBitmapOutputDev::updateStrokeAdjust(GfxState *state)
316 {
317     rgbdev->updateStrokeAdjust(state);
318 }
319 void FullBitmapOutputDev::updateFillColorSpace(GfxState *state)
320 {
321     rgbdev->updateFillColorSpace(state);
322 }
323 void FullBitmapOutputDev::updateStrokeColorSpace(GfxState *state)
324 {
325     rgbdev->updateStrokeColorSpace(state);
326 }
327 void FullBitmapOutputDev::updateFillColor(GfxState *state)
328 {
329     rgbdev->updateFillColor(state);
330 }
331 void FullBitmapOutputDev::updateStrokeColor(GfxState *state)
332 {
333     rgbdev->updateStrokeColor(state);
334 }
335 void FullBitmapOutputDev::updateBlendMode(GfxState *state)
336 {
337     rgbdev->updateBlendMode(state);
338 }
339 void FullBitmapOutputDev::updateFillOpacity(GfxState *state)
340 {
341     rgbdev->updateFillOpacity(state);
342 }
343 void FullBitmapOutputDev::updateStrokeOpacity(GfxState *state)
344 {
345     rgbdev->updateStrokeOpacity(state);
346 }
347 void FullBitmapOutputDev::updateFillOverprint(GfxState *state)
348 {
349     rgbdev->updateFillOverprint(state);
350 }
351 void FullBitmapOutputDev::updateStrokeOverprint(GfxState *state)
352 {
353     rgbdev->updateStrokeOverprint(state);
354 }
355 void FullBitmapOutputDev::updateTransfer(GfxState *state)
356 {
357     rgbdev->updateTransfer(state);
358 }
359 void FullBitmapOutputDev::updateFont(GfxState *state)
360 {
361     rgbdev->updateFont(state);
362 }
363 void FullBitmapOutputDev::updateTextMat(GfxState *state)
364 {
365     rgbdev->updateTextMat(state);
366 }
367 void FullBitmapOutputDev::updateCharSpace(GfxState *state)
368 {
369     rgbdev->updateCharSpace(state);
370 }
371 void FullBitmapOutputDev::updateRender(GfxState *state)
372 {
373     rgbdev->updateRender(state);
374 }
375 void FullBitmapOutputDev::updateRise(GfxState *state)
376 {
377     rgbdev->updateRise(state);
378 }
379 void FullBitmapOutputDev::updateWordSpace(GfxState *state)
380 {
381     rgbdev->updateWordSpace(state);
382 }
383 void FullBitmapOutputDev::updateHorizScaling(GfxState *state)
384 {
385     rgbdev->updateHorizScaling(state);
386 }
387 void FullBitmapOutputDev::updateTextPos(GfxState *state)
388 {
389     rgbdev->updateTextPos(state);
390 }
391 void FullBitmapOutputDev::updateTextShift(GfxState *state, double shift)
392 {
393     rgbdev->updateTextShift(state, shift);
394 }
395
396 void FullBitmapOutputDev::stroke(GfxState *state)
397 {
398     msg("<debug> stroke");
399     rgbdev->stroke(state);
400 }
401 void FullBitmapOutputDev::fill(GfxState *state)
402 {
403     msg("<debug> fill");
404     rgbdev->fill(state);
405 }
406 void FullBitmapOutputDev::eoFill(GfxState *state)
407 {
408     msg("<debug> eoFill");
409     rgbdev->eoFill(state);
410 }
411 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
412 void FullBitmapOutputDev::tilingPatternFill(GfxState *state, Object *str,
413                                int paintType, Dict *resDict,
414                                double *mat, double *bbox,
415                                int x0, int y0, int x1, int y1,
416                                double xStep, double yStep)
417 {
418     msg("<debug> tilingPatternFill");
419     rgbdev->tilingPatternFill(state, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
420 }
421 #else
422 void FullBitmapOutputDev::tilingPatternFill(GfxState *state, Gfx *gfx, Object *str,
423                                int paintType, Dict *resDict,
424                                double *mat, double *bbox,
425                                int x0, int y0, int x1, int y1,
426                                double xStep, double yStep) 
427 {
428     msg("<debug> tilingPatternFill");
429     rgbdev->tilingPatternFill(state, gfx, str, paintType, resDict, mat, bbox, x0, y0, x1, y1, xStep, yStep);
430 }
431 #endif
432
433 GBool FullBitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading) 
434 {
435     msg("<debug> functionShadedFill");
436     return rgbdev->functionShadedFill(state, shading);
437 }
438 GBool FullBitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
439 {
440     msg("<debug> axialShadedFill");
441     return rgbdev->axialShadedFill(state, shading);
442 }
443 GBool FullBitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
444 {
445     msg("<debug> radialShadedFill");
446     return rgbdev->radialShadedFill(state, shading);
447 }
448
449 void FullBitmapOutputDev::clip(GfxState *state)
450 {
451     msg("<debug> clip");
452     rgbdev->clip(state);
453 }
454 void FullBitmapOutputDev::eoClip(GfxState *state)
455 {
456     msg("<debug> eoClip");
457     rgbdev->eoClip(state);
458 }
459 void FullBitmapOutputDev::clipToStrokePath(GfxState *state)
460 {
461     msg("<debug> clipToStrokePath");
462     rgbdev->clipToStrokePath(state);
463 }
464
465 void FullBitmapOutputDev::beginStringOp(GfxState *state)
466 {
467     msg("<debug> beginStringOp");
468     rgbdev->beginStringOp(state);
469 }
470 void FullBitmapOutputDev::endStringOp(GfxState *state)
471 {
472     msg("<debug> endStringOp");
473     rgbdev->endStringOp(state);
474 }
475 void FullBitmapOutputDev::beginString(GfxState *state, GString *s)
476 {
477     msg("<debug> beginString");
478     rgbdev->beginString(state, s);
479 }
480 void FullBitmapOutputDev::endString(GfxState *state)
481 {
482     msg("<debug> endString");
483     rgbdev->endString(state);
484 }
485 void FullBitmapOutputDev::drawChar(GfxState *state, double x, double y,
486                       double dx, double dy,
487                       double originX, double originY,
488                       CharCode code, int nBytes, Unicode *u, int uLen)
489 {
490     msg("<debug> drawChar");
491     rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
492 }
493 void FullBitmapOutputDev::drawString(GfxState *state, GString *s)
494 {
495     msg("<error> internal error: drawString not implemented");
496     rgbdev->drawString(state, s);
497 }
498 void FullBitmapOutputDev::endTextObject(GfxState *state)
499 {
500     /* FIXME: the below might render things (stroke outlines etc.) to gfxdev which
501               might end up unflushed- should be handled similarily as
502               drawChar() above
503      */
504     msg("<debug> endTextObject");
505     rgbdev->endTextObject(state);
506 }
507
508 /* TODO: these four operations below *should* do nothing, as type3
509          chars are drawn using operations like fill() */
510 GBool FullBitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
511                              double dx, double dy,
512                              CharCode code, Unicode *u, int uLen)
513 {
514     msg("<debug> beginType3Char");
515     return rgbdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
516 }
517 void FullBitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
518 {
519     msg("<debug> type3D0");
520     rgbdev->type3D0(state, wx, wy);
521 }
522 void FullBitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
523 {
524     msg("<debug> type3D1");
525     rgbdev->type3D1(state, wx, wy, llx, lly, urx, ury);
526 }
527 void FullBitmapOutputDev::endType3Char(GfxState *state)
528 {
529     msg("<debug> endType3Char");
530     rgbdev->endType3Char(state);
531 }
532 void FullBitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
533                            int width, int height, GBool invert,
534                            GBool inlineImg)
535 {
536     msg("<debug> drawImageMask");
537     rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
538 }
539 void FullBitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
540                        int width, int height, GfxImageColorMap *colorMap,
541                        int *maskColors, GBool inlineImg)
542 {
543     msg("<debug> drawImage");
544     rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
545 }
546 void FullBitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
547                              int width, int height,
548                              GfxImageColorMap *colorMap,
549                              Stream *maskStr, int maskWidth, int maskHeight,
550                              GBool maskInvert)
551 {
552     msg("<debug> drawMaskedImage");
553     rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
554 }
555 void FullBitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
556                                  int width, int height,
557                                  GfxImageColorMap *colorMap,
558                                  Stream *maskStr,
559                                  int maskWidth, int maskHeight,
560                                  GfxImageColorMap *maskColorMap)
561 {
562     msg("<debug> drawSoftMaskedImage");
563     rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
564 }
565 void FullBitmapOutputDev::drawForm(Ref id)
566 {
567     msg("<debug> drawForm");
568     rgbdev->drawForm(id);
569 }
570
571 void FullBitmapOutputDev::processLink(Link *link, Catalog *catalog)
572 {
573     msg("<debug> processLink");
574     gfxdev->processLink(link, catalog);
575 }
576
577 void FullBitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
578                                     GfxColorSpace *blendingColorSpace,
579                                     GBool isolated, GBool knockout,
580                                     GBool forSoftMask)
581 {
582     msg("<debug> beginTransparencyGroup");
583     rgbdev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
584 }
585 void FullBitmapOutputDev::endTransparencyGroup(GfxState *state)
586 {
587     msg("<debug> endTransparencyGroup");
588     rgbdev->endTransparencyGroup(state);
589 }
590 void FullBitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
591 {
592     msg("<debug> paintTransparencyGroup");
593     rgbdev->paintTransparencyGroup(state,bbox);
594 }
595 void FullBitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
596 {
597     msg("<debug> setSoftMask");
598     rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
599 }
600 void FullBitmapOutputDev::clearSoftMask(GfxState *state)
601 {
602     msg("<debug> clearSoftMask");
603     rgbdev->clearSoftMask(state);
604 }
605
606
607