added text field and jpeg counting
[swftools.git] / lib / pdf / InfoOutputDev.cc
1 #include "config.h"
2 #include "Object.h"
3 #include "InfoOutputDev.h"
4 #include "SplashOutputDev.h"
5 #ifdef HAVE_POPPLER
6 #include <splash/SplashTypes.h>
7 #include <splash/SplashPath.h>
8 #include <splash/SplashFont.h>
9 #include <splash/SplashFontFile.h>
10 #else
11 #include "SplashTypes.h"
12 #include "SplashPath.h"
13 #include "SplashFont.h"
14 #include "SplashFontFile.h"
15 #endif
16 #include "GfxState.h"
17 #include "../log.h"
18 #include <math.h>
19
20 InfoOutputDev::InfoOutputDev(XRef*xref) 
21 {
22     num_links = 0;
23     num_jpeg_images = 0;
24     num_ppm_images = 0;
25     num_fonts = 0;
26     num_polygons= 0;
27     fonts = 0;
28     currentfont = 0;
29     currentglyph = 0;
30     id2font = new GHash(1);
31     SplashColor white = {255,255,255};
32     splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
33     splash->startDoc(xref);
34 }
35 InfoOutputDev::~InfoOutputDev() 
36 {
37     GHashIter*i;
38     id2font->startIter(&i);
39     GString*key;
40     FontInfo*fontinfo;
41     while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
42         delete fontinfo;
43     }
44     id2font->killIter(&i);
45
46     delete id2font;id2font=0;
47     delete splash;splash=0;
48 }
49 void FontInfo::grow(int size)
50 {
51     if(size >= this->num_glyphs) {
52         this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
53         memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
54         this->num_glyphs = size;
55     }
56 }
57 FontInfo::FontInfo()
58 {
59     this->charid2glyph = 0;
60     this->seen = 0;
61     this->num_glyphs = 0;
62     this->glyphs = 0;
63     this->splash_font = 0;
64     this->lastchar = -1;
65     this->lastx = 0;
66     this->lasty = 0;
67     this->gfxfont = 0;
68 }
69 FontInfo::~FontInfo()
70 {
71     this->font = 0;
72     if(this->charid2glyph) {
73         free(this->charid2glyph);
74         this->charid2glyph = 0;
75     }
76     int t;
77     for(t=0;t<num_glyphs;t++) {
78         if(glyphs[t]) {
79             delete glyphs[t]->path;glyphs[t]->path = 0;
80             delete glyphs[t];
81             glyphs[t]=0;
82         }
83     }
84     free(glyphs);glyphs=0;
85 }
86 GBool InfoOutputDev::upsideDown() {return gTrue;}
87 GBool InfoOutputDev::useDrawChar() {return gTrue;}
88 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
89 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
90
91 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
92 {
93     double x1,y1,x2,y2;
94     state->transform(crop_x1,crop_y1,&x1,&y1);
95     state->transform(crop_x2,crop_y2,&x2,&y2);
96     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
97     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
98     this->x1 = (int)x1;
99     this->y1 = (int)y1;
100     this->x2 = (int)x2;
101     this->y2 = (int)y2;
102     msg("<verbose> Generating info structure for page %d", pageNum);
103 }
104 void InfoOutputDev::endPage()
105 {
106 }
107 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
108 {
109     num_links++;
110 }
111    
112 /* there's not yet a way to set this */
113 int config_fontquality = 10;
114 int config_bigchar = 0;
115
116 /*  } else if(!strcmp(key,"fontquality")) {
117         this->config_fontquality = atof(value);
118         if(this->config_fontquality<=1)
119             this->config_fontquality=1;
120     } else if(!strcmp(key,"bigchar")) {
121         this->config_bigchar = atoi(value);
122     }
123  */
124
125 gfxfont_t* InfoOutputDev::createGfxFont(GfxFont*xpdffont, FontInfo*src)
126 {
127     gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
128     memset(font, 0, sizeof(gfxfont_t));
129
130     font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
131     memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
132     font->id = 0;
133     int t;
134
135     double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
136     double scale = 1;
137     //printf("%d glyphs\n", font->num_glyphs);
138     font->num_glyphs = 0;
139     font->ascent = fabs(src->descender);
140     font->descent = fabs(src->ascender);
141     
142     for(t=0;t<src->num_glyphs;t++) {
143         if(src->glyphs[t]) {
144             SplashPath*path = src->glyphs[t]->path;
145             int len = path?path->getLength():0;
146             //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
147             gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
148             src->glyphs[t]->glyphid = font->num_glyphs;
149             glyph->unicode = src->glyphs[t]->unicode;
150             if(glyph->unicode >= font->max_unicode)
151                 font->max_unicode = glyph->unicode+1;
152             gfxdrawer_t drawer;
153             gfxdrawer_target_gfxline(&drawer);
154             int s;
155             int count = 0;
156             double xmax = 0;
157             for(s=0;s<len;s++) {
158                 Guchar f;
159                 double x, y;
160                 path->getPoint(s, &x, &y, &f);
161                 if(!s || x > xmax)
162                     xmax = x;
163                 if(f&splashPathFirst) {
164                     drawer.moveTo(&drawer, x*scale, y*scale);
165                 }
166                 if(f&splashPathCurve) {
167                     double x2,y2;
168                     path->getPoint(++s, &x2, &y2, &f);
169                     if(f&splashPathCurve) {
170                         double x3,y3;
171                         path->getPoint(++s, &x3, &y3, &f);
172                         gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
173                     } else {
174                         drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
175                     }
176                 } else {
177                     drawer.lineTo(&drawer, x*scale, y*scale);
178                 }
179              //   printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
180              //                           (f&splashPathFirst)?"first":"",
181              //                           (f&splashPathLast)?"last":"");
182             }
183
184             glyph->line = (gfxline_t*)drawer.result(&drawer);
185             if(src->glyphs[t]->advance>0) {
186                 glyph->advance = src->glyphs[t]->advance;
187             } else {
188                 msg("<warning> Approximating advance value for glyph %d", t);
189                 glyph->advance = xmax*scale;
190             }
191             if(config_bigchar) {
192                 double max = src->glyphs[t]->advance_max;
193                 if(max>0 && max > glyph->advance) {
194                     glyph->advance = max;
195                 }
196             }
197
198             font->num_glyphs++;
199         }
200     }
201     font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
202     memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
203     for(t=0;t<font->num_glyphs;t++) {
204         if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
205             font->unicode2glyph[font->glyphs[t].unicode] = t;
206         }
207
208     }
209     msg("<trace> %d glyphs.", t, font->num_glyphs);
210     return font;
211 }
212
213 double InfoOutputDev::getMaximumFontSize(char*id)
214 {
215     FontInfo*info = (FontInfo*)id2font->lookup(id);
216     if(!info) {
217         msg("<error> Unknown font id: %s", id);
218         return 0.0;
219     }
220     return info->max_size;
221 }
222
223 char*getFontID(GfxFont*font)
224 {
225     Ref*ref = font->getID();
226     GString*gstr = font->getName();
227     char* fname = gstr==0?0:gstr->getCString();
228     char buf[128];
229     if(fname==0) {
230         if(font->getType() == fontType3) {
231             sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
232         } else {
233             sprintf(buf, "font-%d-%d", ref->num, ref->gen);
234         }
235     } else {
236         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
237     }
238     return strdup(buf);
239 }
240
241 void InfoOutputDev::updateFont(GfxState *state) 
242 {
243     GfxFont*font = state->getFont();
244     if(!font) {
245         currentfont = 0;
246         return;
247     }
248     if(font->getType() == fontType3) {
249         currentfont = 0;
250         return;
251     }
252     char*id = getFontID(font);
253
254     if(currentfont)
255         currentfont->splash_font = 0;
256
257     currentfont = (FontInfo*)id2font->lookup(id);
258     if(!currentfont) {
259         currentfont = new FontInfo;
260         currentfont->font = font;
261         currentfont->max_size = 0;
262         GString* idStr = new GString(id);
263         id2font->add(idStr, (void*)currentfont);
264         num_fonts++;
265     }
266
267     state->setCTM(1.0,0,0,1.0,0,0);
268     splash->updateCTM(state, 0,0,0,0,0,0);
269     state->setTextMat(1.0,0,0,1.0,0,0);
270     state->setFont(font, 1024.0);
271     splash->doUpdateFont(state);
272     currentfont->splash_font = splash->getCurrentFont();
273     if(currentfont->splash_font) {
274         currentfont->ascender = currentfont->splash_font->ascender;
275         currentfont->descender = currentfont->splash_font->descender;
276     } else {
277         currentfont->ascender = currentfont->descender = 0;
278     }
279
280     currentfont->gfxfont = this->createGfxFont(font, currentfont);
281     currentfont->gfxfont->id = strdup(id);
282     fonts = gfxfontlist_addfont(fonts, currentfont->gfxfont);
283
284     free(id);
285 }
286
287 void InfoOutputDev::fill(GfxState *state)
288 {
289     num_polygons++;
290 }
291
292 void InfoOutputDev::eoFill(GfxState *state)
293 {
294     num_polygons++;
295 }
296
297 FontInfo* InfoOutputDev::getFont(char*id)
298 {
299     return (FontInfo*)id2font->lookup(id);
300 }
301
302 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
303                       double dx, double dy,
304                       double originX, double originY,
305                       CharCode code, int nBytes, Unicode *u, int uLen)
306 {
307     double m11,m21,m12,m22;
308     state->getFontTransMat(&m11, &m12, &m21, &m22);
309     m11 *= state->getHorizScaling();
310     m21 *= state->getHorizScaling();
311     double lenx = sqrt(m11*m11 + m12*m12);
312     double leny = sqrt(m21*m21 + m22*m22);
313     double len = lenx>leny?lenx:leny;
314     if(!currentfont || !currentfont->splash_font) {
315         return; //error
316     }
317     if(currentfont && currentfont->max_size < len) {
318         currentfont->max_size = len;
319     }
320     currentfont->grow(code+1);
321     GlyphInfo*g = currentfont->glyphs[code];
322     if(!g) {
323         g = currentfont->glyphs[code] = new GlyphInfo();
324         g->advance_max = 0;
325         currentfont->splash_font->last_advance = -1;
326         g->path = currentfont->splash_font->getGlyphPath(code);
327         g->advance = currentfont->splash_font->last_advance;
328         g->unicode = 0;
329     }
330     if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
331         g->unicode = u[0];
332     }
333     if(currentfont->lastchar>=0 && currentfont->lasty == y) {
334         double xshift = x - currentfont->lastx;
335         if(xshift>=0 && xshift > g->advance_max) {
336             g->advance_max = xshift;
337         }
338     }
339
340     currentfont->lastx = x;
341     currentfont->lasty = y;
342     currentfont->lastchar = code;
343 }
344
345 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
346 {
347     GfxFont*font = state->getFont();
348     if(!font)
349         return gTrue;
350     if(font->getType() != fontType3)
351         return gTrue;
352
353     char*id = getFontID(font);
354     currentfont = (FontInfo*)id2font->lookup(id);
355     if(!currentfont) {
356         currentfont = new FontInfo;
357         currentfont->font = font;
358         GString* idStr = new GString(id);
359         id2font->add(idStr, (void*)currentfont);
360         num_fonts++;
361     }
362     currentfont = currentfont;
363     free(id);
364
365     currentfont->grow(code+1);
366     if(!currentfont->glyphs[code]) {
367         currentglyph = currentfont->glyphs[code] = new GlyphInfo();
368         currentglyph->unicode = uLen?u[0]:0;
369         currentglyph->path = new SplashPath();
370         currentglyph->x1=0;
371         currentglyph->y1=0;
372         currentglyph->x2=dx;
373         currentglyph->y2=dy;
374         return gFalse;
375     } else {
376         return gTrue;
377     }
378 }
379
380 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
381 {
382     currentglyph->x1=0;
383     currentglyph->y1=0;
384     currentglyph->x2=wx;
385     currentglyph->y2=wy;
386 }
387
388 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
389 {
390     currentglyph->x1=llx;
391     currentglyph->y1=lly;
392     currentglyph->x2=urx;
393     currentglyph->y2=ury;
394 }
395
396 void InfoOutputDev::endType3Char(GfxState *state)
397 {
398     double x1 = currentglyph->x1;
399     double y1 = currentglyph->y1;
400     double x2 = currentglyph->x2;
401     double y2 = currentglyph->y2;
402     currentglyph->path->moveTo(x1,y1);
403     currentglyph->path->lineTo(x2,y1);
404     currentglyph->path->lineTo(x2,y2);
405     currentglyph->path->lineTo(x1,y2);
406     currentglyph->path->close();
407 }
408
409 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
410                            int width, int height, GBool invert,
411                            GBool inlineImg) 
412 {
413     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
414
415     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
416 }
417 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
418                        int width, int height, GfxImageColorMap *colorMap,
419                        int *maskColors, GBool inlineImg)
420 {
421     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
422
423     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
424 }
425 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
426                                 int width, int height,
427                                 GfxImageColorMap *colorMap,
428                                 Stream *maskStr,
429                                 int maskWidth, int maskHeight,
430                                 GBool maskInvert) 
431 {
432     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
433
434     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
435 }
436
437 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
438                                     int width, int height,
439                                     GfxImageColorMap *colorMap,
440                                     Stream *maskStr,
441                                     int maskWidth, int maskHeight,
442                                     GfxImageColorMap *maskColorMap) 
443 {
444     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
445
446     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
447 }