3 Output Device which creates a bitmap.
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.
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.
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 */
23 #include "BitmapOutputDev.h"
24 #include "GFXOutputDev.h"
27 #include "splash/SplashBitmap.h"
28 #include "splash/SplashPattern.h"
29 #include "splash/Splash.h"
31 #include "xpdf/config.h"
32 #include "SplashBitmap.h"
33 #include "SplashPattern.h"
39 #include "../devices/record.h"
40 #include "../gfxtools.h"
44 #define UNKNOWN_BOUNDING_BOX 0,0,0,0
46 static SplashColor splash_white = {255,255,255};
47 static SplashColor splash_black = {0,0,0};
49 ClipState::ClipState()
56 BitmapOutputDev::BitmapOutputDev(InfoOutputDev*info, PDFDoc*doc)
60 this->xref = doc->getXRef();
62 /* color graphic output device, for creating bitmaps */
63 this->rgbdev = new SplashOutputDev(splashModeRGB8, 1, gFalse, splash_white, gTrue, gTrue);
65 /* color mode for binary bitmaps */
66 SplashColorMode colorMode = splashModeMono1;
68 /* two devices for testing things against clipping: one clips, the other doesn't */
69 this->clip0dev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
70 this->clip1dev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
72 /* device indicating where polygonal pixels were drawn */
73 this->boolpolydev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
74 /* device indicating where text pixels were drawn */
75 this->booltextdev = new SplashOutputDev(colorMode, 1, gFalse, splash_black, gTrue, gFalse);
77 /* device for handling texts and links */
78 this->gfxdev = new GFXOutputDev(info, this->doc);
80 this->rgbdev->startDoc(this->xref);
81 this->boolpolydev->startDoc(this->xref);
82 this->booltextdev->startDoc(this->xref);
83 this->clip0dev->startDoc(this->xref);
84 this->clip1dev->startDoc(this->xref);
86 this->gfxoutput = (gfxdevice_t*)malloc(sizeof(gfxdevice_t));
87 gfxdevice_record_init(this->gfxoutput, 0);
89 this->gfxdev->setDevice(this->gfxoutput);
91 this->config_extrafontdata = 0;
92 this->config_optimizeplaincolorfills = 0;
95 //this->clipstates = 0;
97 BitmapOutputDev::~BitmapOutputDev()
100 gfxresult_t*r = this->gfxoutput->finish(this->gfxoutput);
102 free(this->gfxoutput);this->gfxoutput = 0;
105 delete this->bboxpath;this->bboxpath = 0;
108 delete this->rgbdev;this->rgbdev = 0;
111 delete this->gfxdev;this->gfxdev= 0;
113 if(this->boolpolydev) {
114 delete this->boolpolydev;this->boolpolydev = 0;
116 if(this->stalepolybitmap) {
117 delete this->stalepolybitmap;this->stalepolybitmap = 0;
119 if(this->staletextbitmap) {
120 delete this->staletextbitmap;this->staletextbitmap = 0;
122 if(this->booltextdev) {
123 delete this->booltextdev;this->booltextdev = 0;
126 delete this->clip0dev;this->clip0dev = 0;
129 delete this->clip1dev;this->clip1dev = 0;
131 //if(this->clipbitmap) {
132 // delete this->clipbitmap;this->clipbitmap = 0;
134 //if(this->clipdev) {
135 // delete this->clipdev;this->clipdev = 0;
140 GBool BitmapOutputDev::getVectorAntialias()
142 return this->rgbdev->getVectorAntialias();
144 void BitmapOutputDev::setVectorAntialias(GBool vaa)
146 this->rgbdev->setVectorAntialias(vaa);
148 void BitmapOutputDev::setDevice(gfxdevice_t*dev)
152 void BitmapOutputDev::setMove(int x,int y)
154 this->gfxdev->setMove(x,y);
155 this->user_movex = x;
156 this->user_movey = y;
158 void BitmapOutputDev::setClip(int x1,int y1,int x2,int y2)
160 this->gfxdev->setClip(x1,y1,x2,y2);
161 this->user_clipx1 = x1;
162 this->user_clipy1 = y1;
163 this->user_clipx2 = x2;
164 this->user_clipy2 = y2;
166 void BitmapOutputDev::setParameter(const char*key, const char*value)
168 if(!strcmp(key, "extrafontdata")) {
169 this->config_extrafontdata = atoi(value);
171 this->gfxdev->setParameter(key, value);
173 void BitmapOutputDev::setPageMap(int*page2page, int num_pages)
175 this->gfxdev->setPageMap(page2page, num_pages);
178 void writeBitmap(SplashBitmap*bitmap, char*filename);
179 void writeAlpha(SplashBitmap*bitmap, char*filename);
181 static int dbg_btm_counter=1;
183 void BitmapOutputDev::flushBitmap()
185 int bitmap_width = rgbdev->getBitmapWidth();
186 int bitmap_height = rgbdev->getBitmapHeight();
188 if(sizeof(SplashColor)!=3) {
189 msg("<error> sizeof(SplashColor)!=3");
193 /*static int counter=0;
195 writeBitmap(rgbdev->getBitmap(), "test.png");
198 /*static int counter=0;
200 sprintf(filename, "test%d.png", counter++);
201 writeBitmap(rgbbitmap, filename);*/
203 SplashColorPtr rgb = rgbbitmap->getDataPtr();
204 Guchar*alpha = rgbbitmap->getAlphaPtr();
206 Guchar*alpha2 = stalepolybitmap->getDataPtr();
207 int bitmap_width8 = (stalepolybitmap->getWidth()+7)/8;
210 sprintf(filename, "flush%d_mask.png", dbg_btm_counter);
211 writeAlpha(stalepolybitmap, filename);
212 sprintf(filename, "flush%d_alpha.png", dbg_btm_counter);
213 writeAlpha(rgbbitmap, filename);
214 sprintf(filename, "flush%d_bitmap.png", dbg_btm_counter);
215 writeBitmap(rgbbitmap, filename);*/
217 ibbox_t pagebox = {-movex, -movey, -movex + this->width, -movey + this->height, 0};
218 ibbox_t bitmapbox = {0, 0, bitmap_width, bitmap_height, 0};
219 ibbox_t c = ibbox_clip(&bitmapbox, &pagebox);
220 ibbox_t* boxes = get_bitmap_bboxes((unsigned char*)(alpha+c.ymin*bitmap_width+c.xmin), c.xmax - c.xmin, c.ymax - c.ymin, bitmap_width);
223 for(b=boxes;b;b=b->next) {
224 int xmin = b->xmin - this->movex;
225 int ymin = b->ymin - this->movey;
226 int xmax = b->xmax - this->movex;
227 int ymax = b->ymax - this->movey;
229 /* clip against (-movex, -movey, -movex+width, -movey+height) */
231 msg("<verbose> Flushing bitmap (bbox: %d,%d,%d,%d %dx%d) (clipped against %d,%d,%d,%d)", xmin,ymin,xmax,ymax, xmax-xmin, ymax-ymin,
232 -this->movex, -this->movey, -this->movex+this->width, -this->movey+this->height);
234 if(xmin < -this->movex) {
236 if(xmax < -this->movex) continue;
238 if(ymin < -this->movey) {
240 if(ymax < -this->movey) continue;
242 if(xmax >= -this->movex + this->width) {
243 xmax = -this->movex+this->width;
244 if(xmin >= -this->movex + this->width) continue;
246 if(ymax >= -this->movey + this->height) {
247 ymax = -this->movey+this->height;
248 if(ymin >= -this->movey + this->height) continue;
251 if((xmax-xmin)<=0 || (ymax-ymin)<=0) // no bitmap, nothing to do
254 int rangex = xmax-xmin;
255 int rangey = ymax-ymin;
256 gfximage_t*img = (gfximage_t*)malloc(sizeof(gfximage_t));
257 img->data = (gfxcolor_t*)malloc(rangex * rangey * 4);
259 img->height = rangey;
261 for(y=0;y<rangey;y++) {
262 SplashColorPtr in=&rgb[((y+ymin)*bitmap_width+xmin)*sizeof(SplashColor)];
263 gfxcolor_t*out = &img->data[y*rangex];
264 Guchar*ain = &alpha[(y+ymin)*bitmap_width+xmin];
265 Guchar*ain2 = &alpha2[(y+ymin)*bitmap_width8];
266 if(this->emptypage) {
267 for(x=0;x<rangex;x++) {
268 /* the first bitmap on the page doesn't need to have an alpha channel-
269 blend against a white background*/
270 out[x].r = (in[x*3+0]*ain[x])/255 + 255-ain[x];
271 out[x].g = (in[x*3+1]*ain[x])/255 + 255-ain[x];
272 out[x].b = (in[x*3+2]*ain[x])/255 + 255-ain[x];
276 for(x=0;x<rangex;x++) {
277 if(!(ain2[(x+xmin)/8]&(0x80>>((x+xmin)&7)))) {
278 /* cut away pixels that we don't remember drawing (i.e., that are
279 not in the monochrome bitmap). Prevents some "hairlines" showing
280 up to the left and right of bitmaps. */
281 out[x].r = 0;out[x].g = 0;out[x].b = 0;out[x].a = 0;
283 /* according to endPage()/compositeBackground() in xpdf/SplashOutputDev.cc, this
284 data has non-premultiplied alpha, which is exactly what the output device
285 expects, so don't premultiply it here, either.
287 out[x].r = in[x*3+0];
288 out[x].g = in[x*3+1];
289 out[x].b = in[x*3+2];
296 /* transform bitmap rectangle to "device space" */
310 gfxline_t* line = gfxline_makerectangle(xmin, ymin, xmax, ymax);
311 dev->fillbitmap(dev, line, img, &m, 0);
314 free(img->data);img->data=0;free(img);img=0;
316 ibbox_destroy(boxes);
318 memset(rgbbitmap->getAlphaPtr(), 0, rgbbitmap->getWidth()*rgbbitmap->getHeight());
319 memset(rgbbitmap->getDataPtr(), 0, rgbbitmap->getRowSize()*rgbbitmap->getHeight());
324 void BitmapOutputDev::flushText()
326 msg("<verbose> Flushing text");
328 static gfxfontlist_t*output_font_list = 0;
329 static gfxdevice_t*last = 0;
330 if(last != this->dev) {
332 gfxfontlist_free(output_font_list, 0);
333 output_font_list = gfxfontlist_create();
335 gfxdevice_record_flush(this->gfxoutput, this->dev, &output_font_list);
341 void writeMonoBitmap(SplashBitmap*btm, char*filename)
343 int width8 = (btm->getWidth()+7)/8;
344 int width = btm->getWidth();
345 int height = btm->getHeight();
346 gfxcolor_t*b = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
347 unsigned char*data = btm->getDataPtr();
349 for(y=0;y<height;y++) {
350 unsigned char*l = &data[width8*y];
351 gfxcolor_t*d = &b[width*y];
352 for(x=0;x<width;x++) {
353 if(l[x>>3]&(128>>(x&7))) {
354 d[x].r = d[x].b = d[x].a = 255;
357 d[x].r = d[x].g = d[x].b = d[x].a = 0;
361 png_write(filename, (unsigned char*)b, width, height);
365 void writeBitmap(SplashBitmap*bitmap, char*filename)
369 int width = bitmap->getWidth();
370 int height = bitmap->getHeight();
372 gfxcolor_t*data = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
374 if(bitmap->getMode()==splashModeMono1) {
375 writeMonoBitmap(bitmap, filename);
379 for(y=0;y<height;y++) {
380 gfxcolor_t*line = &data[y*width];
381 for(x=0;x<width;x++) {
382 Guchar c[4] = {0,0,0,0};
383 bitmap->getPixel(x,y,c);
387 line[x].a = bitmap->getAlpha(x,y);
390 png_write(filename, (unsigned char*)data, width, height);
394 void writeAlpha(SplashBitmap*bitmap, char*filename)
398 int width = bitmap->getWidth();
399 int height = bitmap->getHeight();
401 if(bitmap->getMode()==splashModeMono1) {
402 writeMonoBitmap(bitmap, filename);
406 gfxcolor_t*data = (gfxcolor_t*)malloc(sizeof(gfxcolor_t)*width*height);
408 for(y=0;y<height;y++) {
409 gfxcolor_t*line = &data[y*width];
410 for(x=0;x<width;x++) {
411 int a = bitmap->getAlpha(x,y);
418 png_write(filename, (unsigned char*)data, width, height);
422 static const char*STATE_NAME[] = {"parallel", "textabovebitmap", "bitmapabovetext"};
424 int checkAlphaSanity(SplashBitmap*boolbtm, SplashBitmap*alphabtm)
426 assert(boolbtm->getWidth() == alphabtm->getWidth());
427 assert(boolbtm->getHeight() == alphabtm->getHeight());
428 if(boolbtm->getMode()==splashModeMono1) {
432 int width = boolbtm->getWidth();
433 int height = boolbtm->getHeight();
437 for(y=0;y<height;y++) {
438 for(x=0;x<width;x++) {
439 int a1 = alphabtm->getAlpha(x,y);
440 int a2 = boolbtm->getAlpha(x,y);
446 double badness = bad/(double)(width*height);
448 msg("<error> Bitmaps don't correspond: %d out of %d pixels wrong (%.2f%%)", bad, width*height,
452 msg("<notice> %f", badness);
456 static inline GBool fixBBox(int*x1, int*y1, int*x2, int*y2, int width, int height)
458 if(!(*x1|*y1|*x2|*y2)) {
465 if(*x2<=*x1) return gFalse;
466 if(*x2<0) return gFalse;
468 if(*x1>=width) return gFalse;
469 if(*x2>width) *x2=width;
471 if(*y2<=*y1) return gFalse;
472 if(*y2<0) return gFalse;
474 if(*y1>=height) return gFalse;
475 if(*y2>height) *y2=height;
479 static void update_bitmap(SplashBitmap*bitmap, SplashBitmap*update, int x1, int y1, int x2, int y2, char overwrite)
481 assert(bitmap->getMode()==splashModeMono1);
482 assert(update->getMode()==splashModeMono1);
484 int width8 = (bitmap->getWidth()+7)/8;
485 assert(width8 == bitmap->getRowSize());
486 assert(width8 == update->getRowSize());
487 int height = bitmap->getHeight();
488 assert(height == update->getHeight());
490 if(!fixBBox(&x1, &y1, &x2, &y2, bitmap->getWidth(), bitmap->getHeight()))
493 Guchar*b = bitmap->getDataPtr() + y1*width8 + x1/8;
494 Guchar*u = update->getDataPtr() + y1*width8 + x1/8;
496 int xspan = (x2+7)/8 - x1/8;
497 int size = (y2-y1)*width8;
501 for(y=0;y<yspan;y++) {
507 if(((ptroff_t)b&7)==((ptroff_t)u&7)) {
509 for(y=0;y<yspan;y++) {
510 Guchar*e1 = b+xspan-8;
512 while(((ptroff_t)b&7) && b<e1) {
517 *(long long*)b |= *(long long*)u;
529 for(y=0;y<yspan;y++) {
530 for(x=0;x<xspan;x++) {
540 static void clearBooleanBitmap(SplashBitmap*btm, int x1, int y1, int x2, int y2)
542 if(!fixBBox(&x1, &y1, &x2, &y2, btm->getWidth(), btm->getHeight()))
545 if(btm->getMode()==splashModeMono1) {
546 int width8 = (btm->getWidth()+7)/8;
547 assert(width8 == btm->getRowSize());
548 int width = btm->getWidth();
549 int height = btm->getHeight();
550 Guchar*data = btm->getDataPtr();
551 memset(data+y1*width8, 0, width8*(y2-y1));
553 int width = btm->getAlphaRowSize();
554 int height = btm->getHeight();
555 memset(btm->getAlphaPtr(), 0, width*height);
559 void BitmapOutputDev::dbg_newdata(char*newdata)
565 sprintf(filename1, "state%03dboolbitmap_after%s.png", dbg_btm_counter, newdata);
566 sprintf(filename2, "state%03dbooltext_after%s.png", dbg_btm_counter, newdata);
567 sprintf(filename3, "state%03dbitmap_after%s.png", dbg_btm_counter, newdata);
568 msg("<verbose> %s %s %s", filename1, filename2, filename3);
569 writeAlpha(stalepolybitmap, filename1);
570 writeAlpha(booltextbitmap, filename2);
571 writeBitmap(rgbdev->getBitmap(), filename3);
576 GBool BitmapOutputDev::checkNewText(int x1, int y1, int x2, int y2)
578 /* called once some new text was drawn on booltextdev, and
579 before the same thing is drawn on gfxdev */
581 msg("<trace> Testing new text data against current bitmap data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
584 if(intersection(booltextbitmap, stalepolybitmap, x1,y1,x2,y2)) {
585 if(layerstate==STATE_PARALLEL) {
586 /* the new text is above the bitmap. So record that fact. */
587 msg("<verbose> Text is above current bitmap/polygon data");
588 layerstate=STATE_TEXT_IS_ABOVE;
589 update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 0);
590 } else if(layerstate==STATE_BITMAP_IS_ABOVE) {
591 /* there's a bitmap above the (old) text. So we need
592 to flush out that text, and record that the *new*
593 text is now *above* the bitmap
595 msg("<verbose> Text is above current bitmap/polygon data (which is above some other text)");
597 layerstate=STATE_TEXT_IS_ABOVE;
600 /* re-apply the update (which we would otherwise lose) */
601 update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 1);
604 /* we already know that the current text section is
605 above the current bitmap section- now just new
606 bitmap data *and* new text data was drawn, and
607 *again* it's above the current bitmap. */
608 msg("<verbose> Text is still above current bitmap/polygon data");
609 update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 0);
612 update_bitmap(staletextbitmap, booltextbitmap, x1, y1, x2, y2, 0);
615 /* clear the thing we just drew from our temporary drawing bitmap */
616 clearBooleanBitmap(booltextbitmap, x1, y1, x2, y2);
620 GBool BitmapOutputDev::checkNewBitmap(int x1, int y1, int x2, int y2)
622 /* similar to checkNewText() above, only in reverse */
623 msg("<trace> Testing new graphics data against current text data, state=%s, counter=%d\n", STATE_NAME[layerstate], dbg_btm_counter);
626 if(intersection(boolpolybitmap, staletextbitmap, x1,y1,x2,y2)) {
627 if(layerstate==STATE_PARALLEL) {
628 msg("<verbose> Bitmap is above current text data");
629 layerstate=STATE_BITMAP_IS_ABOVE;
630 update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 0);
631 } else if(layerstate==STATE_TEXT_IS_ABOVE) {
632 msg("<verbose> Bitmap is above current text data (which is above some bitmap)");
634 layerstate=STATE_BITMAP_IS_ABOVE;
636 update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 1);
639 msg("<verbose> Bitmap is still above current text data");
640 update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 0);
643 update_bitmap(stalepolybitmap, boolpolybitmap, x1, y1, x2, y2, 0);
646 /* clear the thing we just drew from our temporary drawing bitmap */
647 clearBooleanBitmap(boolpolybitmap, x1, y1, x2, y2);
652 //void checkNewText() {
653 // Guchar*alpha = rgbbitmap->getAlphaPtr();
654 // Guchar*charpixels = clip1bitmap->getDataPtr();
656 // for(yy=0;yy<height;yy++) {
657 // Guchar*aline = &alpha[yy*width];
658 // Guchar*cline = &charpixels[yy*width8];
659 // for(xx=0;xx<width;xx++) {
661 // int bytepos = xx>>3;
662 // /* TODO: is the bit order correct? */
663 // if(aline[xx] && (cline[bytepos]&(1<<bit)))
670 GBool BitmapOutputDev::clip0and1differ(int x1,int y1,int x2,int y2)
672 if(clip0bitmap->getMode()==splashModeMono1) {
673 int width = clip0bitmap->getWidth();
674 int width8 = (width+7)/8;
675 int height = clip0bitmap->getHeight();
677 if(!fixBBox(&x1,&y1,&x2,&y2,width,height)) {
678 /* area is outside or null */
682 SplashBitmap*clip0 = clip0bitmap;
683 SplashBitmap*clip1 = clip1bitmap;
689 unsigned char*row1 = &clip0bitmap->getDataPtr()[width8*y+x18];
690 unsigned char*row2 = &clip1bitmap->getDataPtr()[width8*y+x18];
691 if(memcmp(row1, row2, x28-x18)) {
697 SplashBitmap*clip0 = clip0bitmap;
698 SplashBitmap*clip1 = clip1bitmap;
699 int width = clip0->getAlphaRowSize();
700 int height = clip0->getHeight();
702 if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
706 Guchar*a0 = clip0->getAlphaPtr();
707 Guchar*a1 = clip1->getAlphaPtr();
712 if(a0[y*width+x]!=a1[y*width+x]) {
720 char differs2 = memcmp(a0, a1, width*height);
721 if(differs && !differs2)
722 msg("<warning> Strange internal error (2)");
723 else if(!differs && differs2) {
724 msg("<warning> Bad Bounding Box: Difference in clip0 and clip1 outside bbox");
725 msg("<warning> %d %d %d %d", x1, y1, x2, y2);
731 GBool compare8(unsigned char*data1, unsigned char*data2, int len)
735 if(((ptroff_t)data1&7)==((ptroff_t)data2&7)) {
736 // oh good, we can align both to 8 byte
737 while((ptroff_t)data1&7) {
746 /* use 64 bit for the (hopefully aligned) middle section */
748 long long unsigned int*d1 = (long long unsigned int*)data1;
749 long long unsigned int*d2 = (long long unsigned int*)data2;
750 long long unsigned int x = 0;
762 if(data1[t]&data2[t]) {
769 GBool BitmapOutputDev::intersection(SplashBitmap*boolpoly, SplashBitmap*booltext, int x1, int y1, int x2, int y2)
771 if(boolpoly->getMode()==splashModeMono1) {
772 /* alternative implementation, using one bit per pixel-
773 needs the no-dither patch in xpdf */
775 int width = boolpoly->getWidth();
776 int height = boolpoly->getHeight();
778 if(!fixBBox(&x1,&y1,&x2,&y2, width, height)) {
782 Guchar*polypixels = boolpoly->getDataPtr();
783 Guchar*textpixels = booltext->getDataPtr();
785 int width8 = (width+7)/8;
790 polypixels+=y1*width8+x1/8;
791 textpixels+=y1*width8+x1/8;
792 runx=(x2+7)/8 - x1/8;
799 /*assert(sizeof(unsigned long long int)==8);
800 if(((ptroff_t)polypixels&7) || ((ptroff_t)textpixels&7)) {
801 //msg("<warning> Non-optimal alignment");
805 unsigned char*data1 = (unsigned char*)polypixels;
806 unsigned char*data2 = (unsigned char*)textpixels;
807 msg("<verbose> Testing area (%d,%d,%d,%d), runx=%d,runy=%d", x1,y1,x2,y2, runx, runy);
808 for(y=0;y<runy;y++) {
809 if(compare8(data1,data2,runx))
816 int width = boolpoly->getAlphaRowSize();
817 int height = boolpoly->getHeight();
819 if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
822 Guchar*polypixels = boolpoly->getAlphaPtr();
823 Guchar*textpixels = booltext->getAlphaPtr();
830 if(polypixels[width*y+x]&&textpixels[width*y+x])
834 int ax1=0,ay1=0,ax2=0,ay2=0;
835 for(y=0;y<height;y++) {
836 for(x=0;x<width;x++) {
837 if(polypixels[width*y+x]&&textpixels[width*y+x]) {
839 if(!(ax1|ay1|ax2|ay2)) {
851 if(overlap1 && !overlap2)
852 msg("<warning> strange internal error");
853 if(!overlap1 && overlap2) {
854 msg("<warning> Bad bounding box: intersection outside bbox");
855 msg("<warning> given bbox: %d %d %d %d", x1, y1, x2, y2);
856 msg("<warning> changed area: %d %d %d %d", ax1, ay1, ax2, ay2);
862 GBool BitmapOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
863 int rotate, GBool useMediaBox, GBool crop,
864 int sliceX, int sliceY, int sliceW, int sliceH,
865 GBool printing, Catalog *catalog,
866 GBool (*abortCheckCbk)(void *data),
867 void *abortCheckCbkData)
870 gfxdev->setPage(page);
874 void BitmapOutputDev::startPage(int pageNum, GfxState *state)
876 PDFRectangle *r = this->page->getCropBox();
878 state->transform(r->x1,r->y1,&x1,&y1);
879 state->transform(r->x2,r->y2,&x2,&y2);
880 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
881 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
883 this->movex = -(int)x1 - user_movex;
884 this->movey = -(int)y1 - user_movey;
886 if(user_clipx1|user_clipy1|user_clipx2|user_clipy2) {
892 this->width = (int)(x2-x1);
893 this->height = (int)(y2-y1);
895 rgbdev->startPage(pageNum, state);
896 boolpolydev->startPage(pageNum, state);
897 booltextdev->startPage(pageNum, state);
898 clip0dev->startPage(pageNum, state);
899 clip1dev->startPage(pageNum, state);
900 gfxdev->startPage(pageNum, state);
902 boolpolybitmap = boolpolydev->getBitmap();
903 stalepolybitmap = new SplashBitmap(boolpolybitmap->getWidth(), boolpolybitmap->getHeight(), 1, boolpolybitmap->getMode(), 0);
904 assert(stalepolybitmap->getRowSize() == boolpolybitmap->getRowSize());
906 booltextbitmap = booltextdev->getBitmap();
907 staletextbitmap = new SplashBitmap(booltextbitmap->getWidth(), booltextbitmap->getHeight(), 1, booltextbitmap->getMode(), 0);
908 assert(staletextbitmap->getRowSize() == booltextbitmap->getRowSize());
910 msg("<debug> startPage %dx%d (%dx%d)", this->width, this->height, booltextbitmap->getWidth(), booltextbitmap->getHeight());
912 clip0bitmap = clip0dev->getBitmap();
913 clip1bitmap = clip1dev->getBitmap();
914 rgbbitmap = rgbdev->getBitmap();
916 flushText(); // write out the initial clipping rectangle
918 /* just in case any device did draw a white background rectangle
923 this->layerstate = STATE_PARALLEL;
925 msg("<debug> startPage done");
928 void BitmapOutputDev::endPage()
930 msg("<verbose> endPage (BitmapOutputDev)");
932 /* notice: we're not fully done yet with this page- there might still be
933 a few calls to drawLink() yet to come */
935 void BitmapOutputDev::finishPage()
937 msg("<verbose> finishPage (BitmapOutputDev)");
942 /* splash will now destroy alpha, and paint the
943 background color into the "holes" in the bitmap */
944 boolpolydev->endPage();
945 booltextdev->endPage();
951 GBool BitmapOutputDev::upsideDown()
953 boolpolydev->upsideDown();
954 booltextdev->upsideDown();
955 clip0dev->upsideDown();
956 clip1dev->upsideDown();
957 return rgbdev->upsideDown();
960 GBool BitmapOutputDev::useDrawChar()
962 boolpolydev->useDrawChar();
963 booltextdev->useDrawChar();
964 clip0dev->useDrawChar();
965 clip1dev->useDrawChar();
966 return rgbdev->useDrawChar();
969 GBool BitmapOutputDev::useTilingPatternFill()
971 boolpolydev->useTilingPatternFill();
972 booltextdev->useTilingPatternFill();
973 clip0dev->useTilingPatternFill();
974 clip1dev->useTilingPatternFill();
975 return rgbdev->useTilingPatternFill();
978 GBool BitmapOutputDev::useShadedFills()
980 boolpolydev->useShadedFills();
981 booltextdev->useShadedFills();
982 clip0dev->useShadedFills();
983 clip1dev->useShadedFills();
984 return rgbdev->useShadedFills();
987 GBool BitmapOutputDev::useDrawForm()
989 boolpolydev->useDrawForm();
990 booltextdev->useDrawForm();
991 clip0dev->useDrawForm();
992 clip1dev->useDrawForm();
993 return rgbdev->useDrawForm();
996 GBool BitmapOutputDev::interpretType3Chars()
998 boolpolydev->interpretType3Chars();
999 booltextdev->interpretType3Chars();
1000 clip0dev->interpretType3Chars();
1001 clip1dev->interpretType3Chars();
1002 return rgbdev->interpretType3Chars();
1005 GBool BitmapOutputDev::needNonText()
1007 boolpolydev->needNonText();
1008 booltextdev->needNonText();
1009 clip0dev->needNonText();
1010 clip1dev->needNonText();
1011 return rgbdev->needNonText();
1013 void BitmapOutputDev::setDefaultCTM(double *ctm)
1015 boolpolydev->setDefaultCTM(ctm);
1016 booltextdev->setDefaultCTM(ctm);
1017 rgbdev->setDefaultCTM(ctm);
1018 clip0dev->setDefaultCTM(ctm);
1019 clip1dev->setDefaultCTM(ctm);
1020 gfxdev->setDefaultCTM(ctm);
1022 void BitmapOutputDev::saveState(GfxState *state)
1024 boolpolydev->saveState(state);
1025 booltextdev->saveState(state);
1026 rgbdev->saveState(state);
1027 clip0dev->saveState(state);
1028 clip1dev->saveState(state);
1030 /*ClipState*cstate = new ClipState();
1031 cstate->next = this->clipstates;
1032 this->clipstates = cstate;*/
1034 void BitmapOutputDev::restoreState(GfxState *state)
1036 boolpolydev->restoreState(state);
1037 booltextdev->restoreState(state);
1038 rgbdev->restoreState(state);
1039 clip0dev->restoreState(state);
1040 clip1dev->restoreState(state);
1042 /*if(this->clipstates) {
1043 ClipState*old = this->clipstates;
1045 gfxdev->restoreState(state);
1047 this->clipstates = this->clipstates->next;
1050 msg("<error> invalid restoreState()");
1053 void BitmapOutputDev::updateAll(GfxState *state)
1055 boolpolydev->updateAll(state);
1056 booltextdev->updateAll(state);
1057 rgbdev->updateAll(state);
1058 clip0dev->updateAll(state);
1059 clip1dev->updateAll(state);
1060 gfxdev->updateAll(state);
1062 void BitmapOutputDev::updateCTM(GfxState *state, double m11, double m12, double m21, double m22, double m31, double m32)
1064 boolpolydev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1065 booltextdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1066 rgbdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1067 clip0dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1068 clip1dev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1069 gfxdev->updateCTM(state,m11,m12,m21,m22,m31,m32);
1071 void BitmapOutputDev::updateLineDash(GfxState *state)
1073 boolpolydev->updateLineDash(state);
1074 booltextdev->updateLineDash(state);
1075 rgbdev->updateLineDash(state);
1076 clip0dev->updateLineDash(state);
1077 clip1dev->updateLineDash(state);
1078 gfxdev->updateLineDash(state);
1080 void BitmapOutputDev::updateFlatness(GfxState *state)
1082 boolpolydev->updateFlatness(state);
1083 booltextdev->updateFlatness(state);
1084 rgbdev->updateFlatness(state);
1085 clip0dev->updateFlatness(state);
1086 clip1dev->updateFlatness(state);
1087 gfxdev->updateFlatness(state);
1089 void BitmapOutputDev::updateLineJoin(GfxState *state)
1091 boolpolydev->updateLineJoin(state);
1092 booltextdev->updateLineJoin(state);
1093 rgbdev->updateLineJoin(state);
1094 clip0dev->updateLineJoin(state);
1095 clip1dev->updateLineJoin(state);
1096 gfxdev->updateLineJoin(state);
1098 void BitmapOutputDev::updateLineCap(GfxState *state)
1100 boolpolydev->updateLineCap(state);
1101 booltextdev->updateLineCap(state);
1102 rgbdev->updateLineCap(state);
1103 clip0dev->updateLineCap(state);
1104 clip1dev->updateLineCap(state);
1105 gfxdev->updateLineCap(state);
1107 void BitmapOutputDev::updateMiterLimit(GfxState *state)
1109 boolpolydev->updateMiterLimit(state);
1110 booltextdev->updateMiterLimit(state);
1111 rgbdev->updateMiterLimit(state);
1112 clip0dev->updateMiterLimit(state);
1113 clip1dev->updateMiterLimit(state);
1114 gfxdev->updateMiterLimit(state);
1116 void BitmapOutputDev::updateLineWidth(GfxState *state)
1118 boolpolydev->updateLineWidth(state);
1119 booltextdev->updateLineWidth(state);
1120 rgbdev->updateLineWidth(state);
1121 clip0dev->updateLineWidth(state);
1122 clip1dev->updateLineWidth(state);
1123 gfxdev->updateLineWidth(state);
1125 void BitmapOutputDev::updateStrokeAdjust(GfxState *state)
1127 boolpolydev->updateStrokeAdjust(state);
1128 booltextdev->updateStrokeAdjust(state);
1129 rgbdev->updateStrokeAdjust(state);
1130 clip0dev->updateStrokeAdjust(state);
1131 clip1dev->updateStrokeAdjust(state);
1132 gfxdev->updateStrokeAdjust(state);
1134 void BitmapOutputDev::updateFillColorSpace(GfxState *state)
1136 boolpolydev->updateFillColorSpace(state);
1137 booltextdev->updateFillColorSpace(state);
1138 rgbdev->updateFillColorSpace(state);
1139 clip0dev->updateFillColorSpace(state);
1140 clip1dev->updateFillColorSpace(state);
1141 gfxdev->updateFillColorSpace(state);
1143 void BitmapOutputDev::updateStrokeColorSpace(GfxState *state)
1145 boolpolydev->updateStrokeColorSpace(state);
1146 booltextdev->updateStrokeColorSpace(state);
1147 rgbdev->updateStrokeColorSpace(state);
1148 clip0dev->updateStrokeColorSpace(state);
1149 clip1dev->updateStrokeColorSpace(state);
1150 gfxdev->updateStrokeColorSpace(state);
1152 void BitmapOutputDev::updateFillColor(GfxState *state)
1154 boolpolydev->updateFillColor(state);
1155 booltextdev->updateFillColor(state);
1156 rgbdev->updateFillColor(state);
1157 clip0dev->updateFillColor(state);
1158 clip1dev->updateFillColor(state);
1159 gfxdev->updateFillColor(state);
1161 void BitmapOutputDev::updateStrokeColor(GfxState *state)
1163 boolpolydev->updateStrokeColor(state);
1164 booltextdev->updateStrokeColor(state);
1165 rgbdev->updateStrokeColor(state);
1166 clip0dev->updateStrokeColor(state);
1167 clip1dev->updateStrokeColor(state);
1168 gfxdev->updateStrokeColor(state);
1170 void BitmapOutputDev::updateBlendMode(GfxState *state)
1172 boolpolydev->updateBlendMode(state);
1173 booltextdev->updateBlendMode(state);
1174 rgbdev->updateBlendMode(state);
1175 clip0dev->updateBlendMode(state);
1176 clip1dev->updateBlendMode(state);
1177 gfxdev->updateBlendMode(state);
1179 void BitmapOutputDev::updateFillOpacity(GfxState *state)
1181 boolpolydev->updateFillOpacity(state);
1182 booltextdev->updateFillOpacity(state);
1183 rgbdev->updateFillOpacity(state);
1184 clip0dev->updateFillOpacity(state);
1185 clip1dev->updateFillOpacity(state);
1186 gfxdev->updateFillOpacity(state);
1188 void BitmapOutputDev::updateStrokeOpacity(GfxState *state)
1190 boolpolydev->updateStrokeOpacity(state);
1191 booltextdev->updateStrokeOpacity(state);
1192 rgbdev->updateStrokeOpacity(state);
1193 clip0dev->updateStrokeOpacity(state);
1194 clip1dev->updateStrokeOpacity(state);
1195 gfxdev->updateStrokeOpacity(state);
1197 void BitmapOutputDev::updateFillOverprint(GfxState *state)
1199 boolpolydev->updateFillOverprint(state);
1200 booltextdev->updateFillOverprint(state);
1201 rgbdev->updateFillOverprint(state);
1202 clip0dev->updateFillOverprint(state);
1203 clip1dev->updateFillOverprint(state);
1204 gfxdev->updateFillOverprint(state);
1206 void BitmapOutputDev::updateStrokeOverprint(GfxState *state)
1208 boolpolydev->updateStrokeOverprint(state);
1209 booltextdev->updateStrokeOverprint(state);
1210 rgbdev->updateStrokeOverprint(state);
1211 clip0dev->updateStrokeOverprint(state);
1212 clip1dev->updateStrokeOverprint(state);
1213 gfxdev->updateStrokeOverprint(state);
1215 void BitmapOutputDev::updateTransfer(GfxState *state)
1217 boolpolydev->updateTransfer(state);
1218 booltextdev->updateTransfer(state);
1219 rgbdev->updateTransfer(state);
1220 clip0dev->updateTransfer(state);
1221 clip1dev->updateTransfer(state);
1222 gfxdev->updateTransfer(state);
1225 void BitmapOutputDev::updateFont(GfxState *state)
1227 boolpolydev->updateFont(state);
1228 booltextdev->updateFont(state);
1229 rgbdev->updateFont(state);
1230 clip0dev->updateFont(state);
1231 clip1dev->updateFont(state);
1232 gfxdev->updateFont(state);
1234 void BitmapOutputDev::updateTextMat(GfxState *state)
1236 boolpolydev->updateTextMat(state);
1237 booltextdev->updateTextMat(state);
1238 rgbdev->updateTextMat(state);
1239 clip0dev->updateTextMat(state);
1240 clip1dev->updateTextMat(state);
1241 gfxdev->updateTextMat(state);
1243 void BitmapOutputDev::updateCharSpace(GfxState *state)
1245 boolpolydev->updateCharSpace(state);
1246 booltextdev->updateCharSpace(state);
1247 rgbdev->updateCharSpace(state);
1248 clip0dev->updateCharSpace(state);
1249 clip1dev->updateCharSpace(state);
1250 gfxdev->updateCharSpace(state);
1252 void BitmapOutputDev::updateRender(GfxState *state)
1254 boolpolydev->updateRender(state);
1255 booltextdev->updateRender(state);
1256 rgbdev->updateRender(state);
1257 clip0dev->updateRender(state);
1258 clip1dev->updateRender(state);
1259 gfxdev->updateRender(state);
1261 void BitmapOutputDev::updateRise(GfxState *state)
1263 boolpolydev->updateRise(state);
1264 booltextdev->updateRise(state);
1265 rgbdev->updateRise(state);
1266 clip0dev->updateRise(state);
1267 clip1dev->updateRise(state);
1268 gfxdev->updateRise(state);
1270 void BitmapOutputDev::updateWordSpace(GfxState *state)
1272 boolpolydev->updateWordSpace(state);
1273 booltextdev->updateWordSpace(state);
1274 rgbdev->updateWordSpace(state);
1275 clip0dev->updateWordSpace(state);
1276 clip1dev->updateWordSpace(state);
1277 gfxdev->updateWordSpace(state);
1279 void BitmapOutputDev::updateHorizScaling(GfxState *state)
1281 boolpolydev->updateHorizScaling(state);
1282 booltextdev->updateHorizScaling(state);
1283 rgbdev->updateHorizScaling(state);
1284 clip0dev->updateHorizScaling(state);
1285 clip1dev->updateHorizScaling(state);
1286 gfxdev->updateHorizScaling(state);
1288 void BitmapOutputDev::updateTextPos(GfxState *state)
1290 boolpolydev->updateTextPos(state);
1291 booltextdev->updateTextPos(state);
1292 rgbdev->updateTextPos(state);
1293 clip0dev->updateTextPos(state);
1294 clip1dev->updateTextPos(state);
1295 gfxdev->updateTextPos(state);
1297 void BitmapOutputDev::updateTextShift(GfxState *state, double shift)
1299 boolpolydev->updateTextShift(state, shift);
1300 booltextdev->updateTextShift(state, shift);
1301 rgbdev->updateTextShift(state, shift);
1302 clip0dev->updateTextShift(state, shift);
1303 clip1dev->updateTextShift(state, shift);
1304 gfxdev->updateTextShift(state, shift);
1307 double max(double x, double y)
1311 double min(double x, double y)
1316 gfxbbox_t BitmapOutputDev::getBBox(GfxState*state)
1318 GfxPath * path = state->getPath();
1319 int num = path->getNumSubpaths();
1320 gfxbbox_t bbox = {0,0,1,1};
1323 for(t = 0; t < num; t++) {
1324 GfxSubpath *subpath = path->getSubpath(t);
1325 int subnum = subpath->getNumPoints();
1327 for(s=0;s<subnum;s++) {
1329 state->transform(subpath->getX(s),subpath->getY(s),&x,&y);
1331 bbox.xmin = x; bbox.ymin = y;
1332 bbox.xmax = x; bbox.ymax = y;
1335 bbox.xmin = min(bbox.xmin, x);
1336 bbox.ymin = min(bbox.ymin, y);
1337 bbox.xmax = max(bbox.xmax, x);
1338 bbox.ymax = max(bbox.ymax, y);
1345 void BitmapOutputDev::stroke(GfxState *state)
1347 msg("<debug> stroke");
1348 boolpolydev->stroke(state);
1349 gfxbbox_t bbox = getBBox(state);
1350 double width = state->getTransformedLineWidth();
1351 bbox.xmin -= width; bbox.ymin -= width;
1352 bbox.xmax += width; bbox.ymax += width;
1353 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1354 rgbdev->stroke(state);
1355 dbg_newdata("stroke");
1358 extern gfxcolor_t getFillColor(GfxState * state);
1360 char area_is_plain_colored(GfxState*state, SplashBitmap*boolpoly, SplashBitmap*rgbbitmap, int x1, int y1, int x2, int y2)
1362 int width = boolpoly->getWidth();
1363 int height = boolpoly->getHeight();
1364 if(!fixBBox(&x1, &y1, &x2, &y2, width, height)) {
1367 gfxcolor_t color = getFillColor(state);
1368 SplashColorPtr rgb = rgbbitmap->getDataPtr()
1369 + (y1*width+x1)*sizeof(SplashColor);
1370 int width8 = (width+7)/8;
1371 unsigned char*bits = (unsigned char*)boolpoly->getDataPtr()
1378 if(rgb[x*3+0] != color.r ||
1379 rgb[x*3+1] != color.g ||
1380 rgb[x*3+2] != color.b)
1383 rgb += width*sizeof(SplashColor);
1388 void BitmapOutputDev::fill(GfxState *state)
1390 msg("<debug> fill");
1391 boolpolydev->fill(state);
1392 gfxbbox_t bbox = getBBox(state);
1393 if(config_optimizeplaincolorfills) {
1394 if(area_is_plain_colored(state, boolpolybitmap, rgbbitmap, bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax)) {
1398 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1399 rgbdev->fill(state);
1400 dbg_newdata("fill");
1402 void BitmapOutputDev::eoFill(GfxState *state)
1404 msg("<debug> eoFill");
1405 boolpolydev->eoFill(state);
1406 gfxbbox_t bbox = getBBox(state);
1407 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1408 rgbdev->eoFill(state);
1409 dbg_newdata("eofill");
1412 POPPLER_TILING_PATERN_RETURN BitmapOutputDev::tilingPatternFill(GfxState *state, POPPLER_TILING_PATERN_GFX Object *str,
1413 int paintType, Dict *resDict,
1414 double *mat, double *bbox,
1415 int x0, int y0, int x1, int y1,
1416 double xStep, double yStep)
1418 msg("<debug> tilingPatternFill");
1419 boolpolydev->tilingPatternFill(state, POPPLER_TILING_PATERN_GFX_ARG str, paintType, resDict, mat,
1420 bbox, x0, y0, x1, y1, xStep, yStep);
1421 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1422 rgbdev->tilingPatternFill(state, POPPLER_TILING_PATERN_GFX_ARG str, paintType, resDict, mat,
1423 bbox, x0, y0, x1, y1, xStep, yStep);
1424 dbg_newdata("tilingpatternfill");
1430 GBool BitmapOutputDev::functionShadedFill(GfxState *state, GfxFunctionShading *shading)
1432 msg("<debug> functionShadedFill");
1433 boolpolydev->functionShadedFill(state, shading);
1434 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1435 return rgbdev->functionShadedFill(state, shading);
1437 GBool BitmapOutputDev::axialShadedFill(GfxState *state, GfxAxialShading *shading)
1439 msg("<debug> axialShadedFill");
1440 boolpolydev->axialShadedFill(state, shading);
1441 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1442 return rgbdev->axialShadedFill(state, shading);
1444 GBool BitmapOutputDev::radialShadedFill(GfxState *state, GfxRadialShading *shading)
1446 msg("<debug> radialShadedFill");
1447 boolpolydev->radialShadedFill(state, shading);
1448 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1449 return rgbdev->radialShadedFill(state, shading);
1452 SplashColor black = {0,0,0};
1453 SplashColor white = {255,255,255};
1455 void BitmapOutputDev::clip(GfxState *state)
1457 msg("<debug> clip");
1458 boolpolydev->clip(state);
1459 booltextdev->clip(state);
1460 rgbdev->clip(state);
1461 clip1dev->clip(state);
1463 void BitmapOutputDev::eoClip(GfxState *state)
1465 msg("<debug> eoClip");
1466 boolpolydev->eoClip(state);
1467 booltextdev->eoClip(state);
1468 rgbdev->eoClip(state);
1469 clip1dev->eoClip(state);
1471 void BitmapOutputDev::clipToStrokePath(GfxState *state)
1473 msg("<debug> clipToStrokePath");
1474 boolpolydev->clipToStrokePath(state);
1475 booltextdev->clipToStrokePath(state);
1476 rgbdev->clipToStrokePath(state);
1477 clip1dev->clipToStrokePath(state);
1480 void BitmapOutputDev::beginStringOp(GfxState *state)
1482 msg("<debug> beginStringOp");
1483 clip0dev->beginStringOp(state);
1484 clip1dev->beginStringOp(state);
1485 booltextdev->beginStringOp(state);
1486 gfxdev->beginStringOp(state);
1488 void BitmapOutputDev::beginString(GfxState *state, GString *s)
1490 msg("<debug> beginString");
1491 clip0dev->beginString(state, s);
1492 clip1dev->beginString(state, s);
1493 booltextdev->beginString(state, s);
1494 gfxdev->beginString(state, s);
1497 void BitmapOutputDev::clearClips()
1499 clearBooleanBitmap(clip0bitmap, 0, 0, 0, 0);
1500 clearBooleanBitmap(clip1bitmap, 0, 0, 0, 0);
1502 void BitmapOutputDev::clearBoolPolyDev()
1504 clearBooleanBitmap(stalepolybitmap, 0, 0, stalepolybitmap->getWidth(), stalepolybitmap->getHeight());
1506 void BitmapOutputDev::clearBoolTextDev()
1508 clearBooleanBitmap(staletextbitmap, 0, 0, staletextbitmap->getWidth(), staletextbitmap->getHeight());
1510 void BitmapOutputDev::drawChar(GfxState *state, double x, double y,
1511 double dx, double dy,
1512 double originX, double originY,
1513 CharCode code, int nBytes, Unicode *u, int uLen)
1515 msg("<debug> drawChar render=%d", state->getRender());
1517 if(state->getRender()&RENDER_CLIP) {
1518 //char is just a clipping boundary
1519 rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1520 boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1521 booltextdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1522 clip1dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1523 } else if(state->getRender()&RENDER_STROKE) {
1524 // we're drawing as stroke
1525 boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1526 rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1527 } else if(rgbbitmap != rgbdev->getBitmap()) {
1528 // we're doing softmasking or transparency grouping
1529 boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1530 rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1532 // we're drawing a regular char
1534 clip0dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1535 clip1dev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1537 /* calculate the bbox of this character */
1538 int x1 = (int)x, x2 = (int)x+1, y1 = (int)y, y2 = (int)y+1;
1539 SplashFont*font = clip0dev->getCurrentFont();
1540 SplashPath*path = font?font->getGlyphPath(code):NULL;
1544 path->offset((SplashCoord)x, (SplashCoord)y);
1546 for(t=0;t<path->getLength();t++) {
1549 path->getPoint(t,&xx,&yy,&f);
1550 state->transform(xx,yy,&xx,&yy);
1551 if(xx<x1) x1=(int)xx;
1552 if(yy<y1) y1=(int)yy;
1553 if(xx>=x2) x2=(int)xx+1;
1554 if(yy>=y2) y2=(int)yy+1;
1556 delete(path);path=0;
1559 /* if this character is affected somehow by the various clippings (i.e., it looks
1560 different on a device without clipping), then draw it on the bitmap, not as
1562 if(clip0and1differ(x1,y1,x2,y2)) {
1563 msg("<verbose> Char %d is affected by clipping", code);
1564 boolpolydev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1565 checkNewBitmap(x1,y1,x2,y2);
1566 rgbdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1567 if(config_extrafontdata) {
1568 int oldrender = state->getRender();
1569 state->setRender(3); //invisible
1570 gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1571 state->setRender(oldrender);
1575 /* this char is not at all affected by clipping.
1576 Now just dump out the bitmap we're currently working on, if necessary. */
1577 booltextdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1578 checkNewText(x1,y1,x2,y2);
1579 /* use polygonal output device to do the actual text handling */
1580 gfxdev->drawChar(state, x, y, dx, dy, originX, originY, code, nBytes, u, uLen);
1583 dbg_newdata("text");
1585 void BitmapOutputDev::drawString(GfxState *state, GString *s)
1587 msg("<error> internal error: drawString not implemented");
1590 void BitmapOutputDev::endTextObject(GfxState *state)
1592 msg("<debug> endTextObject");
1593 rgbdev->endTextObject(state);
1594 clip0dev->endTextObject(state);
1595 clip1dev->endTextObject(state);
1596 booltextdev->endTextObject(state);
1597 /* the only thing "drawn" here is clipping */
1598 //checkNewText(UNKNOWN_BOUNDING_BOX);
1599 gfxdev->endTextObject(state);
1600 dbg_newdata("endtextobject");
1602 void BitmapOutputDev::endString(GfxState *state)
1604 msg("<debug> endString");
1605 clip0dev->endString(state);
1606 clip1dev->endString(state);
1607 booltextdev->endString(state);
1608 int render = state->getRender();
1609 if(render != RENDER_INVISIBLE && render != RENDER_FILL) {
1610 /* xpdf draws things like stroke text or fill+stroke text in the
1611 endString() method */
1612 checkNewText(UNKNOWN_BOUNDING_BOX);
1614 gfxdev->endString(state);
1615 dbg_newdata("endstring");
1617 void BitmapOutputDev::endStringOp(GfxState *state)
1619 msg("<debug> endStringOp");
1620 clip0dev->endStringOp(state);
1621 clip1dev->endStringOp(state);
1622 booltextdev->endStringOp(state);
1623 gfxdev->endStringOp(state);
1624 dbg_newdata("endstringop");
1627 /* TODO: these four operations below *should* do nothing, as type3
1628 chars are drawn using operations like fill() */
1629 GBool BitmapOutputDev::beginType3Char(GfxState *state, double x, double y,
1630 double dx, double dy,
1631 CharCode code, Unicode *u, int uLen)
1633 msg("<debug> beginType3Char");
1634 /* call gfxdev so that it can generate "invisible" characters
1635 on top of the actual graphic content, for text extraction */
1636 return gfxdev->beginType3Char(state, x, y, dx, dy, code, u, uLen);
1638 void BitmapOutputDev::type3D0(GfxState *state, double wx, double wy)
1640 msg("<debug> type3D0");
1641 return gfxdev->type3D0(state, wx, wy);
1643 void BitmapOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
1645 msg("<debug> type3D1");
1646 return gfxdev->type3D1(state, wx, wy, llx, lly, urx, ury);
1648 void BitmapOutputDev::endType3Char(GfxState *state)
1650 msg("<debug> endType3Char");
1651 gfxdev->endType3Char(state);
1654 class CopyStream: public Object
1658 MemStream*memstream;
1660 CopyStream(Stream*str, int len)
1665 buf = (char*)malloc(len);
1667 for (t=0; t<len; t++)
1668 buf[t] = str->getChar();
1671 this->dict = str->getDict();
1672 this->memstream = new MemStream(buf, 0, len, this);
1676 ::free(this->buf);this->buf = 0;
1677 delete this->memstream;
1679 Dict* getDict() {return dict;}
1680 Stream* getStream() {return this->memstream;};
1683 gfxbbox_t BitmapOutputDev::getImageBBox(GfxState*state)
1687 state->transform(0, 1, &x, &y);
1688 bbox.xmin=bbox.xmax = x;
1689 bbox.ymin=bbox.ymax = y;
1690 state->transform(0, 0, &x, &y);
1691 bbox.xmin=min(bbox.xmin,x);
1692 bbox.ymin=min(bbox.ymin,y);
1693 bbox.xmax=max(bbox.xmax,x);
1694 bbox.ymax=max(bbox.ymax,y);
1695 state->transform(1, 0, &x, &y);
1696 bbox.xmin=min(bbox.xmin,x);
1697 bbox.ymin=min(bbox.ymin,y);
1698 bbox.xmax=max(bbox.xmax,x);
1699 bbox.ymax=max(bbox.ymax,y);
1700 state->transform(1, 1, &x, &y);
1701 bbox.xmin=min(bbox.xmin,x);
1702 bbox.ymin=min(bbox.ymin,y);
1703 bbox.xmax=max(bbox.xmax,x);
1704 bbox.ymax=max(bbox.ymax,y);
1708 GBool invalid_size(int width, int height)
1710 if((U64)width*(U64)height > 0x7fffffffll)
1715 void BitmapOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
1716 int width, int height, GBool invert,
1719 msg("<debug> drawImageMask streamkind=%d", str->getKind());
1720 if(invalid_size(width,height)) return;
1722 CopyStream*cpystr = new CopyStream(str, height * ((width + 7) / 8));
1723 str = cpystr->getStream();
1725 boolpolydev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1726 gfxbbox_t bbox = getImageBBox(state);
1727 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1728 rgbdev->drawImageMask(state, ref, str, width, height, invert, inlineImg);
1730 dbg_newdata("imagemask");
1732 void BitmapOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
1733 int width, int height, GfxImageColorMap *colorMap,
1734 int *maskColors, GBool inlineImg)
1736 msg("<debug> drawImage streamkind=%d", str->getKind());
1737 if(invalid_size(width,height)) return;
1739 CopyStream*cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1740 str = cpystr->getStream();
1742 boolpolydev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1743 gfxbbox_t bbox=getImageBBox(state);
1744 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1745 rgbdev->drawImage(state, ref, str, width, height, colorMap, maskColors, inlineImg);
1747 dbg_newdata("image");
1749 void BitmapOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
1750 int width, int height,
1751 GfxImageColorMap *colorMap,
1752 Stream *maskStr, int maskWidth, int maskHeight,
1755 msg("<debug> drawMaskedImage streamkind=%d", str->getKind());
1756 if(invalid_size(width,height)) return;
1758 CopyStream*cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1759 str = cpystr->getStream();
1761 boolpolydev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1762 gfxbbox_t bbox=getImageBBox(state);
1763 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1764 rgbdev->drawMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskInvert);
1766 dbg_newdata("maskedimage");
1768 void BitmapOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
1769 int width, int height,
1770 GfxImageColorMap *colorMap,
1772 int maskWidth, int maskHeight,
1773 GfxImageColorMap *maskColorMap)
1775 msg("<debug> drawSoftMaskedImage %dx%d (%dx%d) streamkind=%d", width, height, maskWidth, maskHeight, str->getKind());
1776 if(invalid_size(width,height)) return;
1778 CopyStream*cpystr = new CopyStream(str, height * ((width * colorMap->getNumPixelComps() * colorMap->getBits() + 7) / 8));
1779 str = cpystr->getStream();
1781 boolpolydev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1782 gfxbbox_t bbox=getImageBBox(state);
1783 checkNewBitmap(bbox.xmin, bbox.ymin, ceil(bbox.xmax), ceil(bbox.ymax));
1784 rgbdev->drawSoftMaskedImage(state, ref, str, width, height, colorMap, maskStr, maskWidth, maskHeight, maskColorMap);
1786 dbg_newdata("softmaskimage");
1788 void BitmapOutputDev::drawForm(Ref id)
1790 msg("<debug> drawForm");
1791 boolpolydev->drawForm(id);
1792 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1793 rgbdev->drawForm(id);
1796 void BitmapOutputDev::processLink(Link *link, Catalog *catalog)
1798 msg("<debug> processLink");
1800 gfxdev->processLink(link, catalog);
1802 void BitmapOutputDev::flushEverything()
1804 if(layerstate == STATE_BITMAP_IS_ABOVE) {
1806 this->flushBitmap();
1808 this->flushBitmap();
1813 void BitmapOutputDev::beginTransparencyGroup(GfxState *state, double *bbox,
1814 GfxColorSpace *blendingColorSpace,
1815 GBool isolated, GBool knockout,
1818 msg("<debug> beginTransparencyGroup");
1819 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1820 GfxState*state1 = state->copy();
1821 GfxState*state2 = state->copy();
1823 state1->setPath(state->getPath()->copy());
1825 state2->setPath(state->getPath()->copy());
1827 GfxState*state1 = state->copy(gTrue);
1828 GfxState*state2 = state->copy(gTrue);
1830 boolpolydev->beginTransparencyGroup(state1, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1831 rgbdev->beginTransparencyGroup(state2, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1832 clip1dev->beginTransparencyGroup(state, bbox, blendingColorSpace, isolated, knockout, forSoftMask);
1835 dbg_newdata("endtransparencygroup");
1837 void BitmapOutputDev::endTransparencyGroup(GfxState *state)
1839 msg("<debug> endTransparencyGroup");
1840 #if (xpdfMajorVersion*10000 + xpdfMinorVersion*100 + xpdfUpdateVersion) < 30207
1841 GfxState*state1 = state->copy();
1842 GfxState*state2 = state->copy();
1844 state1->setPath(state->getPath()->copy());
1846 state2->setPath(state->getPath()->copy());
1848 GfxState*state1 = state->copy(gTrue);
1849 GfxState*state2 = state->copy(gTrue);
1851 boolpolydev->endTransparencyGroup(state1);
1852 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1853 rgbdev->endTransparencyGroup(state2);
1856 clip1dev->endTransparencyGroup(state);
1857 dbg_newdata("endtransparencygroup");
1859 void BitmapOutputDev::paintTransparencyGroup(GfxState *state, double *bbox)
1861 msg("<debug> paintTransparencyGroup");
1862 boolpolydev->paintTransparencyGroup(state,bbox);
1863 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1864 rgbdev->paintTransparencyGroup(state,bbox);
1865 clip1dev->paintTransparencyGroup(state,bbox);
1866 dbg_newdata("painttransparencygroup");
1868 void BitmapOutputDev::setSoftMask(GfxState *state, double *bbox, GBool alpha, Function *transferFunc, GfxColor *backdropColor)
1870 msg("<debug> setSoftMask");
1871 boolpolydev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1872 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1873 rgbdev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1874 clip1dev->setSoftMask(state, bbox, alpha, transferFunc, backdropColor);
1875 dbg_newdata("setsoftmask");
1877 void BitmapOutputDev::clearSoftMask(GfxState *state)
1879 msg("<debug> clearSoftMask");
1880 boolpolydev->clearSoftMask(state);
1881 checkNewBitmap(UNKNOWN_BOUNDING_BOX);
1882 rgbdev->clearSoftMask(state);
1883 clip1dev->clearSoftMask(state);
1884 dbg_newdata("clearsoftmask");