added bounding box stubs
[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 }
63 FontInfo::~FontInfo()
64 {
65     this->font = 0;
66     if(this->charid2glyph) {
67         free(this->charid2glyph);
68         this->charid2glyph = 0;
69     }
70     int t;
71     for(t=0;t<num_glyphs;t++) {
72         if(glyphs[t]) {
73             delete glyphs[t]->path;glyphs[t]->path = 0;
74             delete glyphs[t];
75             glyphs[t]=0;
76         }
77     }
78     free(glyphs);glyphs=0;
79 }
80 GBool InfoOutputDev::upsideDown() {return gTrue;}
81 GBool InfoOutputDev::useDrawChar() {return gTrue;}
82 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
83 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
84
85 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
86 {
87     double x1,y1,x2,y2;
88     state->transform(crop_x1,crop_y1,&x1,&y1);
89     state->transform(crop_x2,crop_y2,&x2,&y2);
90     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
91     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
92     this->x1 = (int)x1;
93     this->y1 = (int)y1;
94     this->x2 = (int)x2;
95     this->y2 = (int)y2;
96     msg("<verbose> Generating info structure for page %d", pageNum);
97 }
98 void InfoOutputDev::endPage()
99 {
100 }
101 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
102 {
103     num_links++;
104 }
105 double InfoOutputDev::getMaximumFontSize(char*id)
106 {
107     FontInfo*info = (FontInfo*)id2font->lookup(id);
108     if(!info) {
109         msg("<error> Unknown font id: %s", id);
110         return 0.0;
111     }
112     return info->max_size;
113 }
114
115 char*getFontID(GfxFont*font)
116 {
117     Ref*ref = font->getID();
118     GString*gstr = font->getName();
119     char* fname = gstr==0?0:gstr->getCString();
120     char buf[128];
121     if(fname==0) {
122         if(font->getType() == fontType3) {
123             sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
124         } else {
125             sprintf(buf, "font-%d-%d", ref->num, ref->gen);
126         }
127     } else {
128         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
129     }
130     return strdup(buf);
131 }
132
133 void InfoOutputDev::updateFont(GfxState *state) 
134 {
135     GfxFont*font = state->getFont();
136     if(!font) {
137         currentfont = 0;
138         return;
139     }
140     if(font->getType() == fontType3) {
141         currentfont = 0;
142         return;
143     }
144     char*id = getFontID(font);
145
146     if(currentfont)
147         currentfont->splash_font = 0;
148
149     currentfont = (FontInfo*)id2font->lookup(id);
150     if(!currentfont) {
151         currentfont = new FontInfo;
152         currentfont->font = font;
153         currentfont->max_size = 0;
154         GString* idStr = new GString(id);
155         id2font->add(idStr, (void*)currentfont);
156         num_fonts++;
157     }
158
159     state->setCTM(1.0,0,0,1.0,0,0);
160     splash->updateCTM(state, 0,0,0,0,0,0);
161     state->setTextMat(1.0,0,0,1.0,0,0);
162     state->setFont(font, 1024.0);
163     splash->doUpdateFont(state);
164     currentfont->splash_font = splash->getCurrentFont();
165     free(id);
166 }
167
168 void InfoOutputDev::fill(GfxState *state)
169 {
170     num_polygons++;
171 }
172
173 void InfoOutputDev::eoFill(GfxState *state)
174 {
175     num_polygons++;
176 }
177
178 FontInfo* InfoOutputDev::getFont(char*id)
179 {
180     return (FontInfo*)id2font->lookup(id);
181 }
182
183 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
184                       double dx, double dy,
185                       double originX, double originY,
186                       CharCode code, int nBytes, Unicode *u, int uLen)
187 {
188     double m11,m21,m12,m22;
189     state->getFontTransMat(&m11, &m12, &m21, &m22);
190     m11 *= state->getHorizScaling();
191     m21 *= state->getHorizScaling();
192     double lenx = sqrt(m11*m11 + m12*m12);
193     double leny = sqrt(m21*m21 + m22*m22);
194     double len = lenx>leny?lenx:leny;
195     if(!currentfont || !currentfont->splash_font) {
196         return; //error
197     }
198     if(currentfont && currentfont->max_size < len) {
199         currentfont->max_size = len;
200     }
201     currentfont->grow(code+1);
202     GlyphInfo*g = currentfont->glyphs[code];
203     if(!g) {
204         g = currentfont->glyphs[code] = new GlyphInfo();
205         g->path = currentfont->splash_font->getGlyphPath(code);
206         g->unicode = 0;
207     }
208     if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
209         g->unicode = u[0];
210     }
211
212 }
213
214 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
215 {
216     GfxFont*font = state->getFont();
217     if(!font)
218         return gTrue;
219     if(font->getType() != fontType3)
220         return gTrue;
221
222     char*id = getFontID(font);
223     currentfont = (FontInfo*)id2font->lookup(id);
224     if(!currentfont) {
225         currentfont = new FontInfo;
226         currentfont->font = font;
227         GString* idStr = new GString(id);
228         id2font->add(idStr, (void*)currentfont);
229         num_fonts++;
230     }
231     currentfont = currentfont;
232     free(id);
233
234     currentfont->grow(code+1);
235     if(!currentfont->glyphs[code]) {
236         currentglyph = currentfont->glyphs[code] = new GlyphInfo();
237         currentglyph->unicode = uLen?u[0]:0;
238         currentglyph->path = new SplashPath();
239         currentglyph->x1=0;
240         currentglyph->y1=0;
241         currentglyph->x2=dx;
242         currentglyph->y2=dy;
243         return gFalse;
244     } else {
245         return gTrue;
246     }
247 }
248
249 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
250 {
251     currentglyph->x1=0;
252     currentglyph->y1=0;
253     currentglyph->x2=wx;
254     currentglyph->y2=wy;
255 }
256
257 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
258 {
259     currentglyph->x1=llx;
260     currentglyph->y1=lly;
261     currentglyph->x2=urx;
262     currentglyph->y2=ury;
263 }
264
265 void InfoOutputDev::endType3Char(GfxState *state)
266 {
267     double x1 = currentglyph->x1;
268     double y1 = currentglyph->y1;
269     double x2 = currentglyph->x2;
270     double y2 = currentglyph->y2;
271     currentglyph->path->moveTo(x1,y1);
272     currentglyph->path->lineTo(x2,y1);
273     currentglyph->path->lineTo(x2,y2);
274     currentglyph->path->lineTo(x1,y2);
275     currentglyph->path->close();
276 }
277
278 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
279                            int width, int height, GBool invert,
280                            GBool inlineImg) 
281 {
282     num_images++;
283     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
284 }
285 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
286                        int width, int height, GfxImageColorMap *colorMap,
287                        int *maskColors, GBool inlineImg)
288 {
289     num_images++;
290     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
291 }
292 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
293                                 int width, int height,
294                                 GfxImageColorMap *colorMap,
295                                 Stream *maskStr,
296                                 int maskWidth, int maskHeight,
297                                 GBool maskInvert) 
298 {
299     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
300 }
301
302 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
303                                     int width, int height,
304                                     GfxImageColorMap *colorMap,
305                                     Stream *maskStr,
306                                     int maskWidth, int maskHeight,
307                                     GfxImageColorMap *maskColorMap) 
308 {
309     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
310 }