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