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