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