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