count number of polygons
[swftools.git] / lib / pdf / InfoOutputDev.cc
1 #include "Object.h"
2 #include "SplashTypes.h"
3 #include "SplashOutputDev.h"
4 #include "SplashPath.h"
5 #include "SplashFontFile.h"
6 #include "InfoOutputDev.h"
7 #include "GfxState.h"
8 #include "../log.h"
9 #include <math.h>
10
11 InfoOutputDev::InfoOutputDev(XRef*xref) 
12 {
13     num_links = 0;
14     num_images = 0;
15     num_fonts = 0;
16     num_polygons= 0;
17     currentfont = 0;
18     currentglyph = 0;
19     id2font = new GHash(1);
20     SplashColor white = {255,255,255};
21     splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
22     splash->startDoc(xref);
23 }
24 InfoOutputDev::~InfoOutputDev() 
25 {
26     GHashIter*i;
27     id2font->startIter(&i);
28     GString*key;
29     FontInfo*fontinfo;
30     while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
31         delete fontinfo;
32     }
33     id2font->killIter(&i);
34
35     delete id2font;id2font=0;
36     delete splash;splash=0;
37 }
38 void FontInfo::grow(int size)
39 {
40     if(size >= this->num_glyphs) {
41         this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
42         memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
43         this->num_glyphs = size;
44     }
45 }
46 FontInfo::FontInfo()
47 {
48     this->charid2glyph = 0;
49     this->seen = 0;
50     this->num_glyphs = 0;
51     this->glyphs = 0;
52     this->splash_font = 0;
53 }
54 FontInfo::~FontInfo()
55 {
56     this->font = 0;
57     if(this->charid2glyph) {
58         free(this->charid2glyph);
59         this->charid2glyph = 0;
60     }
61     int t;
62     for(t=0;t<num_glyphs;t++) {
63         if(glyphs[t]) {
64             delete glyphs[t]->path;glyphs[t]->path = 0;
65             delete glyphs[t];
66             glyphs[t]=0;
67         }
68     }
69     free(glyphs);glyphs=0;
70 }
71 GBool InfoOutputDev::upsideDown() {return gTrue;}
72 GBool InfoOutputDev::useDrawChar() {return gTrue;}
73 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
74 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
75
76 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
77 {
78     double x1,y1,x2,y2;
79     state->transform(crop_x1,crop_y1,&x1,&y1);
80     state->transform(crop_x2,crop_y2,&x2,&y2);
81     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
82     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
83     this->x1 = (int)x1;
84     this->y1 = (int)y1;
85     this->x2 = (int)x2;
86     this->y2 = (int)y2;
87     msg("<verbose> Generating info structure for page %d", pageNum);
88 }
89 void InfoOutputDev::endPage()
90 {
91 }
92 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
93 {
94     num_links++;
95 }
96 double InfoOutputDev::getMaximumFontSize(char*id)
97 {
98     FontInfo*info = (FontInfo*)id2font->lookup(id);
99     if(!info) {
100         msg("<error> Unknown font id: %s", id);
101         return 0.0;
102     }
103     return info->max_size;
104 }
105
106 char*getFontID(GfxFont*font)
107 {
108     Ref*ref = font->getID();
109     GString*gstr = font->getName();
110     char* fname = gstr==0?0:gstr->getCString();
111     char buf[128];
112     if(fname==0) {
113         if(font->getType() == fontType3) {
114             sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
115         } else {
116             sprintf(buf, "font-%d-%d", ref->num, ref->gen);
117         }
118     } else {
119         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
120     }
121     return strdup(buf);
122 }
123
124 void InfoOutputDev::updateFont(GfxState *state) 
125 {
126     GfxFont*font = state->getFont();
127     if(!font) {
128         currentfont = 0;
129         return;
130     }
131     if(font->getType() == fontType3) {
132         currentfont = 0;
133         return;
134     }
135     char*id = getFontID(font);
136
137     if(currentfont)
138         currentfont->splash_font = 0;
139
140     currentfont = (FontInfo*)id2font->lookup(id);
141     if(!currentfont) {
142         currentfont = new FontInfo;
143         currentfont->font = font;
144         currentfont->max_size = 0;
145         GString* idStr = new GString(id);
146         id2font->add(idStr, (void*)currentfont);
147         num_fonts++;
148     }
149
150     state->setCTM(1.0,0,0,1.0,0,0);
151     splash->updateCTM(state, 0,0,0,0,0,0);
152     state->setTextMat(1.0,0,0,1.0,0,0);
153     state->setFont(font, 1024.0);
154     splash->doUpdateFont(state);
155     currentfont->splash_font = splash->getCurrentFont();
156     free(id);
157 }
158
159 void InfoOutputDev::fill(GfxState *state)
160 {
161     num_polygons++;
162 }
163
164 void InfoOutputDev::eoFill(GfxState *state)
165 {
166     num_polygons++;
167 }
168
169 FontInfo* InfoOutputDev::getFont(char*id)
170 {
171     return (FontInfo*)id2font->lookup(id);
172 }
173
174 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
175                       double dx, double dy,
176                       double originX, double originY,
177                       CharCode code, int nBytes, Unicode *u, int uLen)
178 {
179     double m11,m21,m12,m22;
180     state->getFontTransMat(&m11, &m12, &m21, &m22);
181     m11 *= state->getHorizScaling();
182     m21 *= state->getHorizScaling();
183     double lenx = sqrt(m11*m11 + m12*m12);
184     double leny = sqrt(m21*m21 + m22*m22);
185     double len = lenx>leny?lenx:leny;
186     if(!currentfont || !currentfont->splash_font) {
187         return; //error
188     }
189     if(currentfont && currentfont->max_size < len) {
190         currentfont->max_size = len;
191     }
192     currentfont->grow(code+1);
193     GlyphInfo*g = currentfont->glyphs[code];
194     if(!g) {
195         g = currentfont->glyphs[code] = new GlyphInfo();
196         g->path = currentfont->splash_font->getGlyphPath(code);
197         g->unicode = 0;
198     }
199     if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
200         g->unicode = u[0];
201     }
202
203 }
204
205 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
206 {
207     GfxFont*font = state->getFont();
208     if(!font)
209         return gTrue;
210     if(font->getType() != fontType3)
211         return gTrue;
212
213     char*id = getFontID(font);
214     currentfont = (FontInfo*)id2font->lookup(id);
215     if(!currentfont) {
216         currentfont = new FontInfo;
217         currentfont->font = font;
218         GString* idStr = new GString(id);
219         id2font->add(idStr, (void*)currentfont);
220         num_fonts++;
221     }
222     currentfont = currentfont;
223     free(id);
224
225     currentfont->grow(code+1);
226     if(!currentfont->glyphs[code]) {
227         currentglyph = currentfont->glyphs[code] = new GlyphInfo();
228         currentglyph->unicode = uLen?u[0]:0;
229         currentglyph->path = new SplashPath();
230         currentglyph->x1=0;
231         currentglyph->y1=0;
232         currentglyph->x2=dx;
233         currentglyph->y2=dy;
234         return gFalse;
235     } else {
236         return gTrue;
237     }
238 }
239
240 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
241 {
242     currentglyph->x1=0;
243     currentglyph->y1=0;
244     currentglyph->x2=wx;
245     currentglyph->y2=wy;
246 }
247
248 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
249 {
250     currentglyph->x1=llx;
251     currentglyph->y1=lly;
252     currentglyph->x2=urx;
253     currentglyph->y2=ury;
254 }
255
256 void InfoOutputDev::endType3Char(GfxState *state)
257 {
258     double x1 = currentglyph->x1;
259     double y1 = currentglyph->y1;
260     double x2 = currentglyph->x2;
261     double y2 = currentglyph->y2;
262     currentglyph->path->moveTo(x1,y1);
263     currentglyph->path->lineTo(x2,y1);
264     currentglyph->path->lineTo(x2,y2);
265     currentglyph->path->lineTo(x1,y2);
266     currentglyph->path->close();
267 }
268
269 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
270                            int width, int height, GBool invert,
271                            GBool inlineImg) 
272 {
273     num_images++;
274     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
275 }
276 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
277                        int width, int height, GfxImageColorMap *colorMap,
278                        int *maskColors, GBool inlineImg)
279 {
280     num_images++;
281     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
282 }
283 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
284                                 int width, int height,
285                                 GfxImageColorMap *colorMap,
286                                 Stream *maskStr,
287                                 int maskWidth, int maskHeight,
288                                 GBool maskInvert) 
289 {
290     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
291 }
292
293 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
294                                     int width, int height,
295                                     GfxImageColorMap *colorMap,
296                                     Stream *maskStr,
297                                     int maskWidth, int maskHeight,
298                                     GfxImageColorMap *maskColorMap) 
299 {
300     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
301 }