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