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