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