c2accf29c8dadd7dac06ffb1a2773ad9f0b4f1fa
[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     currentfont = 0;
27     currentglyph = 0;
28     id2font = new GHash(1);
29     SplashColor white = {255,255,255};
30     splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
31     splash->startDoc(xref);
32 }
33 InfoOutputDev::~InfoOutputDev() 
34 {
35     GHashIter*i;
36     id2font->startIter(&i);
37     GString*key;
38     FontInfo*fontinfo;
39     while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
40         delete fontinfo;
41     }
42     id2font->killIter(&i);
43
44     delete id2font;id2font=0;
45     delete splash;splash=0;
46 }
47 void FontInfo::grow(int size)
48 {
49     if(size >= this->num_glyphs) {
50         this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
51         memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
52         this->num_glyphs = size;
53     }
54 }
55 FontInfo::FontInfo()
56 {
57     this->charid2glyph = 0;
58     this->seen = 0;
59     this->num_glyphs = 0;
60     this->glyphs = 0;
61     this->splash_font = 0;
62     this->lastchar = -1;
63     this->lastx = 0;
64     this->lasty = 0;
65 }
66 FontInfo::~FontInfo()
67 {
68     this->font = 0;
69     if(this->charid2glyph) {
70         free(this->charid2glyph);
71         this->charid2glyph = 0;
72     }
73     int t;
74     for(t=0;t<num_glyphs;t++) {
75         if(glyphs[t]) {
76             delete glyphs[t]->path;glyphs[t]->path = 0;
77             delete glyphs[t];
78             glyphs[t]=0;
79         }
80     }
81     free(glyphs);glyphs=0;
82 }
83 GBool InfoOutputDev::upsideDown() {return gTrue;}
84 GBool InfoOutputDev::useDrawChar() {return gTrue;}
85 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
86 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
87
88 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
89 {
90     double x1,y1,x2,y2;
91     state->transform(crop_x1,crop_y1,&x1,&y1);
92     state->transform(crop_x2,crop_y2,&x2,&y2);
93     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
94     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
95     this->x1 = (int)x1;
96     this->y1 = (int)y1;
97     this->x2 = (int)x2;
98     this->y2 = (int)y2;
99     msg("<verbose> Generating info structure for page %d", pageNum);
100 }
101 void InfoOutputDev::endPage()
102 {
103 }
104 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
105 {
106     num_links++;
107 }
108 double InfoOutputDev::getMaximumFontSize(char*id)
109 {
110     FontInfo*info = (FontInfo*)id2font->lookup(id);
111     if(!info) {
112         msg("<error> Unknown font id: %s", id);
113         return 0.0;
114     }
115     return info->max_size;
116 }
117
118 char*getFontID(GfxFont*font)
119 {
120     Ref*ref = font->getID();
121     GString*gstr = font->getName();
122     char* fname = gstr==0?0:gstr->getCString();
123     char buf[128];
124     if(fname==0) {
125         if(font->getType() == fontType3) {
126             sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
127         } else {
128             sprintf(buf, "font-%d-%d", ref->num, ref->gen);
129         }
130     } else {
131         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
132     }
133     return strdup(buf);
134 }
135
136 void InfoOutputDev::updateFont(GfxState *state) 
137 {
138     GfxFont*font = state->getFont();
139     if(!font) {
140         currentfont = 0;
141         return;
142     }
143     if(font->getType() == fontType3) {
144         currentfont = 0;
145         return;
146     }
147     char*id = getFontID(font);
148
149     if(currentfont)
150         currentfont->splash_font = 0;
151
152     currentfont = (FontInfo*)id2font->lookup(id);
153     if(!currentfont) {
154         currentfont = new FontInfo;
155         currentfont->font = font;
156         currentfont->max_size = 0;
157         GString* idStr = new GString(id);
158         id2font->add(idStr, (void*)currentfont);
159         num_fonts++;
160     }
161
162     state->setCTM(1.0,0,0,1.0,0,0);
163     splash->updateCTM(state, 0,0,0,0,0,0);
164     state->setTextMat(1.0,0,0,1.0,0,0);
165     state->setFont(font, 1024.0);
166     splash->doUpdateFont(state);
167     currentfont->splash_font = splash->getCurrentFont();
168     currentfont->ascender = currentfont->splash_font->ascender;
169     currentfont->descender = currentfont->splash_font->descender;
170     free(id);
171 }
172
173 void InfoOutputDev::fill(GfxState *state)
174 {
175     num_polygons++;
176 }
177
178 void InfoOutputDev::eoFill(GfxState *state)
179 {
180     num_polygons++;
181 }
182
183 FontInfo* InfoOutputDev::getFont(char*id)
184 {
185     return (FontInfo*)id2font->lookup(id);
186 }
187
188 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
189                       double dx, double dy,
190                       double originX, double originY,
191                       CharCode code, int nBytes, Unicode *u, int uLen)
192 {
193     double m11,m21,m12,m22;
194     state->getFontTransMat(&m11, &m12, &m21, &m22);
195     m11 *= state->getHorizScaling();
196     m21 *= state->getHorizScaling();
197     double lenx = sqrt(m11*m11 + m12*m12);
198     double leny = sqrt(m21*m21 + m22*m22);
199     double len = lenx>leny?lenx:leny;
200     if(!currentfont || !currentfont->splash_font) {
201         return; //error
202     }
203     if(currentfont && currentfont->max_size < len) {
204         currentfont->max_size = len;
205     }
206     currentfont->grow(code+1);
207     GlyphInfo*g = currentfont->glyphs[code];
208     if(!g) {
209         g = currentfont->glyphs[code] = new GlyphInfo();
210         g->advance_samples = 0;
211         currentfont->splash_font->last_advance = -1;
212         g->path = currentfont->splash_font->getGlyphPath(code);
213         g->advance = currentfont->splash_font->last_advance;
214         g->unicode = 0;
215     }
216     if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
217         g->unicode = u[0];
218     }
219     if(currentfont->lastchar>=0 && currentfont->lasty == y) {
220         double xshift = x - currentfont->lastx;
221         if(xshift>=0) {
222             AdvanceSample* old = g->advance_samples;
223             g->advance_samples = new AdvanceSample();
224             g->advance_samples->next = old;
225             g->advance_samples->advance = xshift;
226         }
227     }
228
229     currentfont->lastx = x;
230     currentfont->lasty = y;
231     currentfont->lastchar = code;
232 }
233
234 static int compare_double(const void *_a, const void *_b)
235 {
236     const double*a = (const double*)_a;
237     const double*b = (const double*)_b;
238     if(*a < *b) 
239         return -1;
240     if(*a > *b) 
241         return 1;
242     return 0;
243 }
244
245 double GlyphInfo::estimateAdvance()
246 {
247     AdvanceSample*a = advance_samples;
248     int n=0;
249     while(a) {
250         n++;
251         a = a->next;
252     }
253     if(!n)
254         return -1;
255     double*list = (double*)malloc(sizeof(double)*n);
256     n = 0;
257     a = advance_samples;
258     while(a) {
259         list[n++] = a->advance;
260         a = a->next;
261     }
262     // FIXME: a true median algorithm would be faster
263     qsort(list, n, sizeof(double), compare_double);
264     double median = list[n/2];
265     free(list);
266     return median;
267 }
268
269 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
270 {
271     GfxFont*font = state->getFont();
272     if(!font)
273         return gTrue;
274     if(font->getType() != fontType3)
275         return gTrue;
276
277     char*id = getFontID(font);
278     currentfont = (FontInfo*)id2font->lookup(id);
279     if(!currentfont) {
280         currentfont = new FontInfo;
281         currentfont->font = font;
282         GString* idStr = new GString(id);
283         id2font->add(idStr, (void*)currentfont);
284         num_fonts++;
285     }
286     currentfont = currentfont;
287     free(id);
288
289     currentfont->grow(code+1);
290     if(!currentfont->glyphs[code]) {
291         currentglyph = currentfont->glyphs[code] = new GlyphInfo();
292         currentglyph->unicode = uLen?u[0]:0;
293         currentglyph->path = new SplashPath();
294         currentglyph->x1=0;
295         currentglyph->y1=0;
296         currentglyph->x2=dx;
297         currentglyph->y2=dy;
298         return gFalse;
299     } else {
300         return gTrue;
301     }
302 }
303
304 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
305 {
306     currentglyph->x1=0;
307     currentglyph->y1=0;
308     currentglyph->x2=wx;
309     currentglyph->y2=wy;
310 }
311
312 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
313 {
314     currentglyph->x1=llx;
315     currentglyph->y1=lly;
316     currentglyph->x2=urx;
317     currentglyph->y2=ury;
318 }
319
320 void InfoOutputDev::endType3Char(GfxState *state)
321 {
322     double x1 = currentglyph->x1;
323     double y1 = currentglyph->y1;
324     double x2 = currentglyph->x2;
325     double y2 = currentglyph->y2;
326     currentglyph->path->moveTo(x1,y1);
327     currentglyph->path->lineTo(x2,y1);
328     currentglyph->path->lineTo(x2,y2);
329     currentglyph->path->lineTo(x1,y2);
330     currentglyph->path->close();
331 }
332
333 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
334                            int width, int height, GBool invert,
335                            GBool inlineImg) 
336 {
337     num_images++;
338     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
339 }
340 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
341                        int width, int height, GfxImageColorMap *colorMap,
342                        int *maskColors, GBool inlineImg)
343 {
344     num_images++;
345     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
346 }
347 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
348                                 int width, int height,
349                                 GfxImageColorMap *colorMap,
350                                 Stream *maskStr,
351                                 int maskWidth, int maskHeight,
352                                 GBool maskInvert) 
353 {
354     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
355 }
356
357 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
358                                     int width, int height,
359                                     GfxImageColorMap *colorMap,
360                                     Stream *maskStr,
361                                     int maskWidth, int maskHeight,
362                                     GfxImageColorMap *maskColorMap) 
363 {
364     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
365 }