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