reworked font backend
[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 FontInfo::FontInfo()
34 {
35     this->charid2glyph = 0;
36 }
37 FontInfo::~FontInfo()
38 {
39     this->font = 0;
40     if(this->charid2glyph) {
41         free(this->charid2glyph);
42         this->charid2glyph = 0;
43     }
44     int t;
45     for(t=0;t<num_glyphs;t++) {
46         if(glyphs[t]) {
47             delete glyphs[t]->path;glyphs[t]->path = 0;
48             delete glyphs[t];
49             glyphs[t]=0;
50         }
51     }
52 }
53 GBool InfoOutputDev::upsideDown() {return gTrue;}
54 GBool InfoOutputDev::useDrawChar() {return gTrue;}
55 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
56 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
57 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
58 {
59     double x1,y1,x2,y2;
60     state->transform(crop_x1,crop_y1,&x1,&y1);
61     state->transform(crop_x2,crop_y2,&x2,&y2);
62     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
63     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
64     this->x1 = (int)x1;
65     this->y1 = (int)y1;
66     this->x2 = (int)x2;
67     this->y2 = (int)y2;
68     msg("<verbose> Generating info structure for page %d", pageNum);
69 }
70 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
71 {
72     num_links++;
73 }
74 double InfoOutputDev::getMaximumFontSize(char*id)
75 {
76     FontInfo*info = (FontInfo*)id2font->lookup(id);
77     if(!info) {
78         msg("<error> Unknown font id: %s", id);
79         return 0.0;
80     }
81     return info->max_size;
82 }
83
84 char*getFontID(GfxFont*font)
85 {
86     Ref*ref = font->getID();
87     GString*gstr = font->getName();
88     char* fname = gstr==0?0:gstr->getCString();
89     char buf[128];
90     if(fname==0) {
91         sprintf(buf, "font-%d-%d", ref->num, ref->gen);
92     } else {
93         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
94     }
95     return strdup(buf);
96 }
97
98 void InfoOutputDev::updateFont(GfxState *state) 
99 {
100     GfxFont*font = state->getFont();
101     if(!font)
102         return;
103     if(font->getType() == fontType3) {
104         return;
105     }
106     char*id = getFontID(font);
107
108     FontInfo*info = (FontInfo*)id2font->lookup(id);
109     if(info) {
110         /* font already known */
111         free(id);
112         currentfont = info;
113         return;
114     }
115
116     info = new FontInfo;
117     info->font = font;
118     info->max_size = 0;
119
120     state->setCTM(1.0,0,0,1.0,0,0);
121     splash->updateCTM(state, 0,0,0,0,0,0);
122     state->setTextMat(1.0,0,0,-1.0,0,0);
123     state->setFont(font, 1024.0);
124     splash->doUpdateFont(state);
125     info->splash_font = splash->getCurrentFont();
126     info->num_glyphs = 0;
127     info->glyphs = 0;
128
129     if(!info->splash_font) {
130         delete info;
131         return;
132     }
133  
134     GString* idStr = new GString(id);
135     id2font->add(idStr, (void*)info);
136     num_fonts++;
137     currentfont = info;
138     free(id);
139 }
140 FontInfo* InfoOutputDev::getFont(char*id)
141 {
142     return (FontInfo*)id2font->lookup(id);
143 }
144
145 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
146                       double dx, double dy,
147                       double originX, double originY,
148                       CharCode code, int nBytes, Unicode *u, int uLen)
149 {
150     double m11,m21,m12,m22;
151     state->getFontTransMat(&m11, &m12, &m21, &m22);
152     m11 *= state->getHorizScaling();
153     m21 *= state->getHorizScaling();
154     double lenx = sqrt(m11*m11 + m12*m12);
155     double leny = sqrt(m21*m21 + m22*m22);
156     double len = lenx>leny?lenx:leny;
157     if(currentfont && currentfont->max_size < len) {
158         currentfont->max_size = len;
159     }
160     if(code >= currentfont->num_glyphs) {
161         currentfont->glyphs = (GlyphInfo**)realloc(currentfont->glyphs, sizeof(GlyphInfo*)*(code+1));
162         memset(&currentfont->glyphs[currentfont->num_glyphs], 0, sizeof(SplashPath*)*((code+1)-currentfont->num_glyphs));
163         currentfont->num_glyphs = code+1;
164     }
165     GlyphInfo*g = currentfont->glyphs[code];
166     if(!g) {
167         g = currentfont->glyphs[code] = new GlyphInfo();
168         g->path = currentfont->splash_font->getGlyphPath(code);
169         g->unicode = 0;
170     }
171     if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
172         g->unicode = u[0];
173     }
174
175 }
176 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
177                            int width, int height, GBool invert,
178                            GBool inlineImg) 
179 {
180     num_images++;
181     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
182 }
183 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
184                        int width, int height, GfxImageColorMap *colorMap,
185                        int *maskColors, GBool inlineImg)
186 {
187     num_images++;
188     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
189 }
190 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
191                                 int width, int height,
192                                 GfxImageColorMap *colorMap,
193                                 Stream *maskStr,
194                                 int maskWidth, int maskHeight,
195                                 GBool maskInvert) 
196 {
197     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
198 }
199
200 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
201                                     int width, int height,
202                                     GfxImageColorMap *colorMap,
203                                     Stream *maskStr,
204                                     int maskWidth, int maskHeight,
205                                     GfxImageColorMap *maskColorMap) 
206 {
207     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
208 }