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