make sure gfxfonts are only created once
[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 <math.h>
19
20 /* there's not yet a way to set this */
21 int config_fontquality = 10;
22 int config_bigchar = 0;
23
24 InfoOutputDev::InfoOutputDev(XRef*xref) 
25 {
26     num_links = 0;
27     num_jpeg_images = 0;
28     num_ppm_images = 0;
29     num_textfields = 0;
30     num_fonts = 0;
31     num_polygons= 0;
32     currentfont = 0;
33     currentglyph = 0;
34     id2font = new GHash(1);
35     SplashColor white = {255,255,255};
36     splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
37     splash->startDoc(xref);
38 }
39 InfoOutputDev::~InfoOutputDev() 
40 {
41     GHashIter*i;
42     id2font->startIter(&i);
43     GString*key;
44     FontInfo*fontinfo;
45     while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
46         delete fontinfo;
47     }
48     id2font->killIter(&i);
49
50     delete id2font;id2font=0;
51     delete splash;splash=0;
52 }
53 void FontInfo::grow(int size)
54 {
55     if(size >= this->num_glyphs) {
56         this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
57         memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
58         this->num_glyphs = size;
59     }
60 }
61 FontInfo::FontInfo(char*id)
62 {
63     this->id = strdup(id);
64     this->charid2glyph = 0;
65     this->seen = 0;
66     this->num_glyphs = 0;
67     this->glyphs = 0;
68     this->splash_font = 0;
69     this->lastchar = -1;
70     this->lastx = 0;
71     this->lasty = 0;
72     this->gfxfont = 0;
73 }
74 FontInfo::~FontInfo()
75 {
76     if(this->id) {free(this->id);this->id=0;}
77     this->font = 0;
78     if(this->charid2glyph) {
79         free(this->charid2glyph);
80         this->charid2glyph = 0;
81     }
82     int t;
83     for(t=0;t<num_glyphs;t++) {
84         if(glyphs[t]) {
85             delete glyphs[t]->path;glyphs[t]->path = 0;
86             delete glyphs[t];
87             glyphs[t]=0;
88         }
89     }
90     free(glyphs);glyphs=0;
91 }
92
93 gfxfont_t* createGfxFont(FontInfo*src)
94 {
95     gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
96     memset(font, 0, sizeof(gfxfont_t));
97
98     font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
99     memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
100     font->id = 0;
101     int t;
102
103     double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
104     double scale = 1;
105     //printf("%d glyphs\n", font->num_glyphs);
106     font->num_glyphs = 0;
107     font->ascent = fabs(src->descender);
108     font->descent = fabs(src->ascender);
109     
110     for(t=0;t<src->num_glyphs;t++) {
111         if(src->glyphs[t]) {
112             SplashPath*path = src->glyphs[t]->path;
113             int len = path?path->getLength():0;
114             //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
115             gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
116             src->glyphs[t]->glyphid = font->num_glyphs;
117             glyph->unicode = src->glyphs[t]->unicode;
118             if(glyph->unicode >= font->max_unicode)
119                 font->max_unicode = glyph->unicode+1;
120             gfxdrawer_t drawer;
121             gfxdrawer_target_gfxline(&drawer);
122             int s;
123             int count = 0;
124             double xmax = 0;
125             for(s=0;s<len;s++) {
126                 Guchar f;
127                 double x, y;
128                 path->getPoint(s, &x, &y, &f);
129                 if(!s || x > xmax)
130                     xmax = x;
131                 if(f&splashPathFirst) {
132                     drawer.moveTo(&drawer, x*scale, y*scale);
133                 }
134                 if(f&splashPathCurve) {
135                     double x2,y2;
136                     path->getPoint(++s, &x2, &y2, &f);
137                     if(f&splashPathCurve) {
138                         double x3,y3;
139                         path->getPoint(++s, &x3, &y3, &f);
140                         gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
141                     } else {
142                         drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
143                     }
144                 } else {
145                     drawer.lineTo(&drawer, x*scale, y*scale);
146                 }
147              //   printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
148              //                           (f&splashPathFirst)?"first":"",
149              //                           (f&splashPathLast)?"last":"");
150             }
151
152             glyph->line = (gfxline_t*)drawer.result(&drawer);
153             if(src->glyphs[t]->advance>0) {
154                 glyph->advance = src->glyphs[t]->advance;
155             } else {
156                 msg("<warning> Approximating advance value for glyph %d", t);
157                 glyph->advance = xmax*scale;
158             }
159             if(config_bigchar) {
160                 double max = src->glyphs[t]->advance_max;
161                 if(max>0 && max > glyph->advance) {
162                     glyph->advance = max;
163                 }
164             }
165
166             font->num_glyphs++;
167         }
168     }
169     font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
170     memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
171     for(t=0;t<font->num_glyphs;t++) {
172         if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
173             font->unicode2glyph[font->glyphs[t].unicode] = t;
174         }
175
176     }
177     return font;
178 }
179
180
181 gfxfont_t* FontInfo::getGfxFont()
182 {
183     if(!this->gfxfont) {
184         this->gfxfont = createGfxFont(this);
185         this->gfxfont->id = strdup(this->id);
186     }
187     return this->gfxfont;
188 }
189
190 GBool InfoOutputDev::upsideDown() {return gTrue;}
191 GBool InfoOutputDev::useDrawChar() {return gTrue;}
192 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
193 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
194
195 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
196 {
197     double x1,y1,x2,y2;
198     state->transform(crop_x1,crop_y1,&x1,&y1);
199     state->transform(crop_x2,crop_y2,&x2,&y2);
200     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
201     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
202     this->x1 = (int)x1;
203     this->y1 = (int)y1;
204     this->x2 = (int)x2;
205     this->y2 = (int)y2;
206     msg("<verbose> Generating info structure for page %d", pageNum);
207 }
208 void InfoOutputDev::endPage()
209 {
210 }
211 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
212 {
213     num_links++;
214 }
215    
216 /*  } else if(!strcmp(key,"fontquality")) {
217         this->config_fontquality = atof(value);
218         if(this->config_fontquality<=1)
219             this->config_fontquality=1;
220     } else if(!strcmp(key,"bigchar")) {
221         this->config_bigchar = atoi(value);
222     }
223  */
224
225 double InfoOutputDev::getMaximumFontSize(char*id)
226 {
227     FontInfo*info = (FontInfo*)id2font->lookup(id);
228     if(!info) {
229         msg("<error> Unknown font id: %s", id);
230         return 0.0;
231     }
232     return info->max_size;
233 }
234
235 char*getFontID(GfxFont*font)
236 {
237     Ref*ref = font->getID();
238     GString*gstr = font->getName();
239     char* fname = gstr==0?0:gstr->getCString();
240     char buf[128];
241     if(fname==0) {
242         if(font->getType() == fontType3) {
243             sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
244         } else {
245             sprintf(buf, "font-%d-%d", ref->num, ref->gen);
246         }
247     } else {
248         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
249     }
250     return strdup(buf);
251 }
252
253 void InfoOutputDev::updateFont(GfxState *state) 
254 {
255     GfxFont*font = state->getFont();
256     if(!font) {
257         currentfont = 0;
258         return;
259     }
260     if(font->getType() == fontType3) {
261         currentfont = 0;
262         return;
263     }
264     char*id = getFontID(font);
265
266     if(currentfont)
267         currentfont->splash_font = 0;
268
269     currentfont = (FontInfo*)id2font->lookup(id);
270     if(!currentfont) {
271         currentfont = new FontInfo(id);
272         currentfont->font = font;
273         currentfont->max_size = 0;
274         GString* idStr = new GString(id);
275         id2font->add(idStr, (void*)currentfont);
276         num_fonts++;
277     }
278
279     state->setCTM(1.0,0,0,1.0,0,0);
280     splash->updateCTM(state, 0,0,0,0,0,0);
281     state->setTextMat(1.0,0,0,1.0,0,0);
282     state->setFont(font, 1024.0);
283     splash->doUpdateFont(state);
284     currentfont->splash_font = splash->getCurrentFont();
285     if(currentfont->splash_font) {
286         currentfont->ascender = currentfont->splash_font->ascender;
287         currentfont->descender = currentfont->splash_font->descender;
288     } else {
289         currentfont->ascender = currentfont->descender = 0;
290     }
291
292     free(id);
293 }
294
295 void InfoOutputDev::fill(GfxState *state)
296 {
297     num_polygons++;
298 }
299
300 void InfoOutputDev::eoFill(GfxState *state)
301 {
302     num_polygons++;
303 }
304
305 FontInfo* InfoOutputDev::getFont(char*id)
306 {
307     return (FontInfo*)id2font->lookup(id);
308 }
309
310 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
311                       double dx, double dy,
312                       double originX, double originY,
313                       CharCode code, int nBytes, Unicode *u, int uLen)
314 {
315     double m11,m21,m12,m22;
316     state->getFontTransMat(&m11, &m12, &m21, &m22);
317     m11 *= state->getHorizScaling();
318     m21 *= state->getHorizScaling();
319     double lenx = sqrt(m11*m11 + m12*m12);
320     double leny = sqrt(m21*m21 + m22*m22);
321     double len = lenx>leny?lenx:leny;
322     if(!currentfont || !currentfont->splash_font) {
323         return; //error
324     }
325     if(currentfont && currentfont->max_size < len) {
326         currentfont->max_size = len;
327     }
328     
329     num_textfields++;
330
331     currentfont->grow(code+1);
332     GlyphInfo*g = currentfont->glyphs[code];
333     if(!g) {
334         g = currentfont->glyphs[code] = new GlyphInfo();
335         g->advance_max = 0;
336         currentfont->splash_font->last_advance = -1;
337         g->path = currentfont->splash_font->getGlyphPath(code);
338         g->advance = currentfont->splash_font->last_advance;
339         g->unicode = 0;
340     }
341     if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
342         g->unicode = u[0];
343     }
344     if(currentfont->lastchar>=0 && currentfont->lasty == y) {
345         double xshift = x - currentfont->lastx;
346         if(xshift>=0 && xshift > g->advance_max) {
347             g->advance_max = xshift;
348         }
349     }
350
351     currentfont->lastx = x;
352     currentfont->lasty = y;
353     currentfont->lastchar = code;
354 }
355
356 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
357 {
358     GfxFont*font = state->getFont();
359     if(!font)
360         return gTrue;
361     if(font->getType() != fontType3)
362         return gTrue;
363
364     char*id = getFontID(font);
365     currentfont = (FontInfo*)id2font->lookup(id);
366     if(!currentfont) {
367         currentfont = new FontInfo(id);
368         currentfont->font = font;
369         GString* idStr = new GString(id);
370         id2font->add(idStr, (void*)currentfont);
371         num_fonts++;
372     }
373     currentfont = currentfont;
374     free(id);
375
376     currentfont->grow(code+1);
377     if(!currentfont->glyphs[code]) {
378         currentglyph = currentfont->glyphs[code] = new GlyphInfo();
379         currentglyph->unicode = uLen?u[0]:0;
380         currentglyph->path = new SplashPath();
381         currentglyph->x1=0;
382         currentglyph->y1=0;
383         currentglyph->x2=dx;
384         currentglyph->y2=dy;
385         return gFalse;
386     } else {
387         return gTrue;
388     }
389 }
390
391 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
392 {
393     currentglyph->x1=0;
394     currentglyph->y1=0;
395     currentglyph->x2=wx;
396     currentglyph->y2=wy;
397 }
398
399 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
400 {
401     currentglyph->x1=llx;
402     currentglyph->y1=lly;
403     currentglyph->x2=urx;
404     currentglyph->y2=ury;
405 }
406
407 void InfoOutputDev::endType3Char(GfxState *state)
408 {
409     double x1 = currentglyph->x1;
410     double y1 = currentglyph->y1;
411     double x2 = currentglyph->x2;
412     double y2 = currentglyph->y2;
413     currentglyph->path->moveTo(x1,y1);
414     currentglyph->path->lineTo(x2,y1);
415     currentglyph->path->lineTo(x2,y2);
416     currentglyph->path->lineTo(x1,y2);
417     currentglyph->path->close();
418 }
419
420 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
421                            int width, int height, GBool invert,
422                            GBool inlineImg) 
423 {
424     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
425
426     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
427 }
428 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
429                        int width, int height, GfxImageColorMap *colorMap,
430                        int *maskColors, GBool inlineImg)
431 {
432     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
433
434     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
435 }
436 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
437                                 int width, int height,
438                                 GfxImageColorMap *colorMap,
439                                 Stream *maskStr,
440                                 int maskWidth, int maskHeight,
441                                 GBool maskInvert) 
442 {
443     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
444
445     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
446 }
447
448 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
449                                     int width, int height,
450                                     GfxImageColorMap *colorMap,
451                                     Stream *maskStr,
452                                     int maskWidth, int maskHeight,
453                                     GfxImageColorMap *maskColorMap) 
454 {
455     if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
456
457     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
458 }
459     
460 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
461 {
462     GHashIter*i;
463     GString*key;
464     FontInfo*font;
465     id2font->startIter(&i);
466     while(id2font->getNext(&i, &key, (void**)&font)) {
467         dev->addfont(dev, font->getGfxFont());
468     }
469 }