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