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