poppler: fix drawImage*() parameters
[swftools.git] / lib / pdf / InfoOutputDev.cc
1 #include "../../config.h"
2 #include "Object.h"
3 #include "InfoOutputDev.h"
4 #include "SplashOutputDev.h"
5 #include "GfxState.h"
6 #include "../log.h"
7 #include "../types.h"
8 #include "../q.h"
9 #include "../gfxfont.h"
10 #include <math.h>
11 #include <assert.h>
12
13 int config_addspace = 1;
14 int config_fontquality = 10;
15 int config_bigchar = 0;
16
17 InfoOutputDev::InfoOutputDev(XRef*xref) 
18 {
19     num_links = 0;
20     num_jpeg_images = 0;
21     num_ppm_images = 0;
22     num_textfields = 0;
23     num_fonts = 0;
24     num_polygons= 0;
25     currentfont = 0;
26     currentglyph = 0;
27     id2font = new GHash(1);
28     SplashColor white = {255,255,255};
29     splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
30     splash->startDoc(xref);
31 }
32 InfoOutputDev::~InfoOutputDev() 
33 {
34     GHashIter*i;
35     id2font->startIter(&i);
36     GString*key;
37     FontInfo*fontinfo;
38     while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
39         delete fontinfo;
40     }
41     id2font->killIter(&i);
42
43     delete id2font;id2font=0;
44     delete splash;splash=0;
45 }
46
47 void FontInfo::grow(int size)
48 {
49     if(size >= this->num_glyphs) {
50         this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
51         this->kerning = (dict_t**)realloc(this->kerning, sizeof(dict_t*)*(size));
52         memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
53         memset(&this->kerning[this->num_glyphs], 0, sizeof(dict_t*)*((size)-this->num_glyphs));
54         this->num_glyphs = size;
55     }
56 }
57 FontInfo::FontInfo(char*id)
58 {
59     this->id = strdup(id);
60     this->seen = 0;
61     this->num_glyphs = 0;
62     this->glyphs = 0;
63     this->kerning = 0;
64     this->splash_font = 0;
65     this->lastchar = -1;
66     this->lastx = 0;
67     this->lasty = 0;
68     this->gfxfont = 0;
69     this->space_char = -1;
70     this->ascender = 0;
71     this->descender = 0;
72 }
73 FontInfo::~FontInfo()
74 {
75     if(this->id) {free(this->id);this->id=0;}
76     this->font = 0;
77     int t;
78     for(t=0;t<num_glyphs;t++) {
79         if(glyphs[t]) {
80             delete glyphs[t]->path;glyphs[t]->path = 0;
81             delete glyphs[t];
82             glyphs[t]=0;
83         }
84     }
85     free(glyphs);glyphs=0;
86     if(this->gfxfont)
87         gfxfont_free(this->gfxfont);
88    
89     if(kerning) {
90         for(t=0;t<num_glyphs;t++) {
91             dict_t* d = kerning[t];
92             if(!d) continue;
93             DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
94                 mtf_destroy(m);
95             }
96             dict_destroy(d);
97         }
98         free(kerning);
99         kerning=0;
100     }
101 }
102
103 static int findSpace(gfxfont_t*font)
104 {
105     int first_space = -1;
106     int t;
107     for(t=0;t<font->num_glyphs;t++) {
108         gfxglyph_t*g = &font->glyphs[t];
109         if(GLYPH_IS_SPACE(g)) {
110             if(g->unicode == 32) {
111                 /* now that we have found a space char, make sure it's unique */
112                 int s;
113                 for(s=0;s<font->num_glyphs;s++) {
114                     if(s!=t && font->glyphs[s].unicode==32)
115                         font->glyphs[s].unicode=0;
116                 }
117                 return t;
118             }
119         }
120     }
121     return -1;
122 }
123
124 static int addSpace(gfxfont_t*font)
125 {
126     /* first, make sure the new space char is the only char that'll use unicode 32 */
127     int t;
128     for(t=0;t<font->num_glyphs;t++) {
129         if(font->glyphs[t].unicode==32)
130             font->glyphs[t].unicode=0;
131     }
132     font->num_glyphs++;
133     font->glyphs = (gfxglyph_t*)realloc(font->glyphs, sizeof(gfxglyph_t)*font->num_glyphs);
134     gfxglyph_t*g = &font->glyphs[font->num_glyphs-1];
135     memset(g, 0, sizeof(*g));
136     g->unicode = 32;
137     g->advance = fabs(font->ascent + font->descent)*2 / 3;
138     if(font->max_unicode > 32)
139         font->unicode2glyph[32] = font->num_glyphs-1;
140 #if 0
141     g->line = gfxline_makerectangle(0, -font->ascent, g->advance, font->descent);
142 #endif
143     return font->num_glyphs-1;
144 }
145
146 static gfxfont_t* createGfxFont(FontInfo*src)
147 {
148     gfxfont_t*font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
149
150     font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
151     memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
152     font->id = 0;
153     int t;
154
155     double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
156     double scale = 1;
157     //printf("%d glyphs\n", font->num_glyphs);
158     font->num_glyphs = 0;
159     font->ascent = fabs(src->ascender);
160     font->descent = fabs(src->descender);
161     
162     for(t=0;t<src->num_glyphs;t++) {
163         if(src->glyphs[t]) {
164             SplashPath*path = src->glyphs[t]->path;
165             int len = path?path->getLength():0;
166             //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
167             gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
168             src->glyphs[t]->glyphid = font->num_glyphs;
169             glyph->unicode = src->glyphs[t]->unicode;
170             gfxdrawer_t drawer;
171             gfxdrawer_target_gfxline(&drawer);
172             int s;
173             int count = 0;
174             double xmax = 0;
175             for(s=0;s<len;s++) {
176                 Guchar f;
177                 double x, y;
178                 path->getPoint(s, &x, &y, &f);
179                 if(!s || x > xmax)
180                     xmax = x;
181                 if(f&splashPathFirst) {
182                     drawer.moveTo(&drawer, x*scale, y*scale);
183                 }
184                 if(f&splashPathCurve) {
185                     double x2,y2;
186                     path->getPoint(++s, &x2, &y2, &f);
187                     if(f&splashPathCurve) {
188                         double x3,y3;
189                         path->getPoint(++s, &x3, &y3, &f);
190                         gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
191                     } else {
192                         drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
193                     }
194                 } else {
195                     drawer.lineTo(&drawer, x*scale, y*scale);
196                 }
197              //   printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
198              //                           (f&splashPathFirst)?"first":"",
199              //                           (f&splashPathLast)?"last":"");
200             }
201
202             glyph->line = (gfxline_t*)drawer.result(&drawer);
203             if(src->glyphs[t]->advance>0) {
204                 glyph->advance = src->glyphs[t]->advance;
205             } else {
206                 glyph->advance = xmax*scale;
207             }
208             if(config_bigchar) {
209                 double max = src->glyphs[t]->advance_max;
210                 if(max>0 && max > glyph->advance) {
211                     glyph->advance = max;
212                 }
213             }
214
215             font->num_glyphs++;
216         }
217     }
218
219     int kerning_size = 0;
220     for(t=0;t<src->num_glyphs;t++) {
221         dict_t* d = src->kerning[t];
222         if(!d) continue;
223         DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
224             if(m) {
225                 kerning_size++;
226             }
227         }
228     }
229     font->kerning_size = kerning_size;
230     font->kerning = (gfxkerning_t*)malloc(sizeof(gfxkerning_t)*kerning_size);
231     int pos = 0;
232     for(t=0;t<src->num_glyphs;t++) {
233         dict_t* d = src->kerning[t];
234         if(!d) continue;
235         DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
236             if(m) {
237                 font->kerning[pos].c1 = src->glyphs[t]->glyphid;
238                 font->kerning[pos].c2 = src->glyphs[(int)(ptroff_t)key]->glyphid;
239                 font->kerning[pos].advance = (int)(ptroff_t)m->first->key;
240                 pos++;
241             }
242         }
243     }
244     //int advance = (int)(ptroff_t)m->first->key;
245
246     return font;
247 }
248
249 static float find_average_glyph_advance(gfxfont_t*f)
250 {
251     if(!f->num_glyphs)
252         return 0.0;
253
254     float*values = (float*)malloc(sizeof(float)*f->num_glyphs);
255     int t;
256     for(t=0;t<f->num_glyphs;t++) {
257         values[t] = f->glyphs[t].advance;
258     }
259     float m = medianf(values, f->num_glyphs);
260     free(values);
261     return m;
262 }
263
264 gfxfont_t* FontInfo::getGfxFont()
265 {
266     if(!this->gfxfont) {
267         this->gfxfont = createGfxFont(this);
268         this->gfxfont->id = strdup(this->id);
269         this->space_char = findSpace(this->gfxfont);
270         this->average_advance = find_average_glyph_advance(this->gfxfont);
271
272         if(this->space_char>=0) {
273             msg("<debug> Font %s has space char %d (unicode=%d)", 
274                     this->id, this->space_char, 
275                     this->gfxfont->glyphs[this->space_char].unicode);
276         } else if(config_addspace) {
277             this->space_char = addSpace(this->gfxfont);
278             msg("<debug> Appending space char to font %s, position %d, width %f", this->gfxfont->id, this->space_char, this->gfxfont->glyphs[this->space_char].advance);
279         }
280         gfxfont_fix_unicode(this->gfxfont);
281     }
282     return this->gfxfont;
283 }
284
285 GBool InfoOutputDev::upsideDown() {return gTrue;}
286 GBool InfoOutputDev::useDrawChar() {return gTrue;}
287 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
288 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
289
290 GBool InfoOutputDev::checkPageSlice(Page *page, double hDPI, double vDPI,
291              int rotate, GBool useMediaBox, GBool crop,
292              int sliceX, int sliceY, int sliceW, int sliceH,
293              GBool printing, Catalog *catalog,
294              GBool (*abortCheckCbk)(void *data),
295              void *abortCheckCbkData)
296 {
297     this->page = page;
298     return gTrue;
299 }
300
301 void InfoOutputDev::startPage(int pageNum, GfxState *state)
302 {
303     PDFRectangle *r = this->page->getCropBox();
304     double x1,y1,x2,y2;
305     state->transform(r->x1,r->y1,&x1,&y1);
306     state->transform(r->x2,r->y2,&x2,&y2);
307     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
308     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
309     this->x1 = (int)x1;
310     this->y1 = (int)y1;
311     this->x2 = (int)x2;
312     this->y2 = (int)y2;
313     msg("<verbose> Generating info structure for page %d", pageNum);
314 }
315 void InfoOutputDev::endPage()
316 {
317 }
318 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
319 {
320     num_links++;
321 }
322    
323 /*  } else if(!strcmp(key,"fontquality")) {
324         this->config_fontquality = atof(value);
325         if(this->config_fontquality<=1)
326             this->config_fontquality=1;
327     } else if(!strcmp(key,"bigchar")) {
328         this->config_bigchar = atoi(value);
329     }
330  */
331
332 double InfoOutputDev::getMaximumFontSize(char*id)
333 {
334     FontInfo*info = (FontInfo*)id2font->lookup(id);
335     if(!info) {
336         msg("<error> Unknown font id: %s", id);
337         return 0.0;
338     }
339     return info->max_size;
340 }
341
342 char*getFontID(GfxFont*font)
343 {
344     Ref*ref = font->getID();
345     GString*gstr = font->getName();
346     char* fname = gstr==0?0:gstr->getCString();
347     char buf[128];
348     if(fname==0) {
349         if(font->getType() == fontType3) {
350             sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
351         } else {
352             sprintf(buf, "font-%d-%d", ref->num, ref->gen);
353         }
354     } else {
355         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
356     }
357     return strdup(buf);
358 }
359
360 void InfoOutputDev::updateFont(GfxState *state) 
361 {
362     GfxFont*font = state->getFont();
363     if(!font) {
364         currentfont = 0;
365         return;
366     }
367     if(font->getType() == fontType3) {
368         currentfont = 0;
369         return;
370     }
371     char*id = getFontID(font);
372
373     if(currentfont)
374         currentfont->splash_font = 0;
375
376     currentfont = (FontInfo*)id2font->lookup(id);
377     if(!currentfont) {
378         currentfont = new FontInfo(id);
379         currentfont->font = font;
380         currentfont->max_size = 0;
381         GString* idStr = new GString(id);
382         id2font->add(idStr, (void*)currentfont);
383         num_fonts++;
384     }
385
386     state->setCTM(1.0,0,0,1.0,0,0);
387     splash->updateCTM(state, 0,0,0,0,0,0);
388     state->setTextMat(1.0,0,0,1.0,0,0);
389     state->setFont(font, 1024.0);
390     splash->doUpdateFont(state);
391     currentfont->splash_font = splash->getCurrentFont();
392     if(currentfont->splash_font) {
393         currentfont->ascender = currentfont->splash_font->ascender;
394         currentfont->descender = currentfont->splash_font->descender;
395     } else {
396         currentfont->ascender = currentfont->descender = 0;
397     }
398
399     free(id);
400 }
401
402 void InfoOutputDev::fill(GfxState *state)
403 {
404     num_polygons++;
405 }
406
407 void InfoOutputDev::eoFill(GfxState *state)
408 {
409     num_polygons++;
410 }
411
412 FontInfo* InfoOutputDev::getFont(char*id)
413 {
414     return (FontInfo*)id2font->lookup(id);
415 }
416
417 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
418                       double dx, double dy,
419                       double originX, double originY,
420                       CharCode code, int nBytes, Unicode *u, int uLen)
421 {
422     double m11,m21,m12,m22;
423     state->getFontTransMat(&m11, &m12, &m21, &m22);
424     m11 *= state->getHorizScaling();
425     m21 *= state->getHorizScaling();
426     double lenx = sqrt(m11*m11 + m12*m12);
427     double leny = sqrt(m21*m21 + m22*m22);
428     double len = lenx>leny?lenx:leny;
429     if(!currentfont || !currentfont->splash_font) {
430         return; //error
431     }
432     if(currentfont && currentfont->max_size < len) {
433         currentfont->max_size = len;
434     }
435     
436     num_textfields++;
437
438     currentfont->grow(code+1);
439     GlyphInfo*g = currentfont->glyphs[code];
440     if(!g) {
441         g = currentfont->glyphs[code] = new GlyphInfo();
442         g->advance_max = 0;
443         currentfont->splash_font->last_advance = -1;
444         g->path = currentfont->splash_font->getGlyphPath(code);
445         g->advance = currentfont->splash_font->last_advance;
446         g->unicode = 0;
447     }
448     if(uLen && ((u[0]>=32 && u[0]<g->unicode) || !g->unicode)) {
449         g->unicode = u[0];
450     }
451     if(currentfont->lastchar>=0 && currentfont->lasty == y) {
452         double xshift = (x - currentfont->lastx);
453         if(xshift>=0 && xshift > g->advance_max) {
454             g->advance_max = xshift;
455         }
456         int advance = (int)xshift;
457         if(advance>=0 && advance<g->advance*4 && advance!=currentfont->lastadvance) {
458             int c1 = currentfont->lastchar;
459             int c2 = code;
460             dict_t*d = currentfont->kerning[c1];
461             if(!d) {
462                 d = currentfont->kerning[c1] = dict_new2(&int_type);
463             }
464             mtf_t*k = (mtf_t*)dict_lookup(d, (void*)(ptroff_t)c2);
465             if(!k) {
466                 k = mtf_new(&int_type);
467                 dict_put(d, (void*)(ptroff_t)c2, k);
468             }
469             mtf_increase(k, (void*)(ptroff_t)advance);
470         }
471     }
472
473     currentfont->lastx = x;
474     currentfont->lasty = y;
475     currentfont->lastchar = code;
476     currentfont->lastadvance = (int)(g->advance+0.5);
477 }
478
479 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
480 {
481     GfxFont*font = state->getFont();
482     if(!font)
483         return gTrue;
484     if(font->getType() != fontType3)
485         return gTrue;
486
487     char*id = getFontID(font);
488     currentfont = (FontInfo*)id2font->lookup(id);
489     if(!currentfont) {
490         currentfont = new FontInfo(id);
491         currentfont->font = font;
492         GString* idStr = new GString(id);
493         id2font->add(idStr, (void*)currentfont);
494         num_fonts++;
495     }
496     currentfont = currentfont;
497     free(id);
498
499     currentfont->grow(code+1);
500     if(!currentfont->glyphs[code]) {
501         currentglyph = currentfont->glyphs[code] = new GlyphInfo();
502         currentglyph->unicode = uLen?u[0]:0;
503         currentglyph->path = new SplashPath();
504         currentglyph->x1=0;
505         currentglyph->y1=0;
506         currentglyph->x2=dx;
507         currentglyph->y2=dy;
508         currentglyph->advance=dx;
509         return gFalse;
510     } else {
511         return gTrue;
512     }
513 }
514
515 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
516 {
517     currentglyph->x1=0;
518     currentglyph->y1=0;
519     currentglyph->x2=wx;
520     currentglyph->y2=wy;
521 }
522
523 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
524 {
525     if(-lly>currentfont->descender)
526         currentfont->descender = -lly;
527     if(ury>currentfont->ascender)
528         currentfont->ascender = ury;
529
530     currentglyph->x1=llx;
531     currentglyph->y1=lly;
532     currentglyph->x2=urx;
533     currentglyph->y2=ury;
534 }
535
536 void InfoOutputDev::endType3Char(GfxState *state)
537 {
538     double x1 = currentglyph->x1;
539     double y1 = currentglyph->y1;
540     double x2 = currentglyph->x2;
541     double y2 = currentglyph->y2;
542     currentglyph->path->moveTo(x1,y1);
543     currentglyph->path->lineTo(x2,y1);
544     currentglyph->path->lineTo(x2,y2);
545     currentglyph->path->lineTo(x1,y2);
546     currentglyph->path->close();
547 }
548     
549 void InfoOutputDev::saveState(GfxState *state)
550 {
551     updateAll(state);
552 }
553
554 void InfoOutputDev::restoreState(GfxState *state)
555 {
556     updateAll(state);
557 }
558
559 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
560                            int width, int height, GBool invert,
561                            POPPLER_INTERPOLATE
562                            GBool inlineImg) 
563 {
564     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
565
566     OutputDev::drawImageMask(state,ref,str,width,height,invert, POPPLER_INTERPOLATE_ARG inlineImg);
567 }
568 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
569                        int width, int height, GfxImageColorMap *colorMap,
570                        POPPLER_INTERPOLATE
571                        int *maskColors, GBool inlineImg)
572 {
573     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
574
575     OutputDev::drawImage(state,ref,str,width,height,colorMap, POPPLER_INTERPOLATE_ARG maskColors,inlineImg);
576 }
577 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
578                                 int width, int height,
579                                 GfxImageColorMap *colorMap,
580                                 POPPLER_INTERPOLATE
581                                 Stream *maskStr,
582                                 int maskWidth, int maskHeight,
583                                 GBool maskInvert
584                                 POPPLER_MASK_INTERPOLATE)
585 {
586     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
587
588     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap, POPPLER_INTERPOLATE_ARG maskStr,maskWidth,maskHeight,maskInvert POPPLER_MASK_INTERPOLATE_ARG);
589 }
590
591 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
592                                     int width, int height,
593                                     GfxImageColorMap *colorMap,
594                                     POPPLER_INTERPOLATE
595                                     Stream *maskStr,
596                                     int maskWidth, int maskHeight,
597                                     GfxImageColorMap *maskColorMap
598                                     POPPLER_MASK_INTERPOLATE)
599 {
600     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
601
602     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap, POPPLER_INTERPOLATE_ARG maskStr,maskWidth,maskHeight,maskColorMap POPPLER_MASK_INTERPOLATE_ARG);
603 }
604     
605 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
606 {
607     GHashIter*i;
608     GString*key;
609     FontInfo*font;
610     id2font->startIter(&i);
611     while(id2font->getNext(&i, &key, (void**)&font)) {
612         dev->addfont(dev, font->getGfxFont());
613     }
614 }