3 #include "InfoOutputDev.h"
4 #include "SplashOutputDev.h"
6 #include <splash/SplashTypes.h>
7 #include <splash/SplashPath.h>
8 #include <splash/SplashFont.h>
9 #include <splash/SplashFontFile.h>
11 #include "SplashTypes.h"
12 #include "SplashPath.h"
13 #include "SplashFont.h"
14 #include "SplashFontFile.h"
20 InfoOutputDev::InfoOutputDev(XRef*xref)
29 id2font = new GHash(1);
30 SplashColor white = {255,255,255};
31 splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
32 splash->startDoc(xref);
34 InfoOutputDev::~InfoOutputDev()
37 id2font->startIter(&i);
40 while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
43 id2font->killIter(&i);
45 delete id2font;id2font=0;
46 delete splash;splash=0;
48 void FontInfo::grow(int size)
50 if(size >= this->num_glyphs) {
51 this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
52 memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
53 this->num_glyphs = size;
58 this->charid2glyph = 0;
62 this->splash_font = 0;
71 if(this->charid2glyph) {
72 free(this->charid2glyph);
73 this->charid2glyph = 0;
76 for(t=0;t<num_glyphs;t++) {
78 delete glyphs[t]->path;glyphs[t]->path = 0;
83 free(glyphs);glyphs=0;
85 GBool InfoOutputDev::upsideDown() {return gTrue;}
86 GBool InfoOutputDev::useDrawChar() {return gTrue;}
87 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
88 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
90 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
93 state->transform(crop_x1,crop_y1,&x1,&y1);
94 state->transform(crop_x2,crop_y2,&x2,&y2);
95 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
96 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
101 msg("<verbose> Generating info structure for page %d", pageNum);
103 void InfoOutputDev::endPage()
106 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
111 /* there's not yet a way to set this */
112 int config_fontquality = 10;
113 int config_bigchar = 0;
115 /* } else if(!strcmp(key,"fontquality")) {
116 this->config_fontquality = atof(value);
117 if(this->config_fontquality<=1)
118 this->config_fontquality=1;
119 } else if(!strcmp(key,"bigchar")) {
120 this->config_bigchar = atoi(value);
124 gfxfont_t* InfoOutputDev::createGfxFont(GfxFont*xpdffont, FontInfo*src)
126 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
127 memset(font, 0, sizeof(gfxfont_t));
129 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
130 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
134 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
136 //printf("%d glyphs\n", font->num_glyphs);
137 font->num_glyphs = 0;
138 font->ascent = fabs(src->descender);
139 font->descent = fabs(src->ascender);
141 for(t=0;t<src->num_glyphs;t++) {
143 SplashPath*path = src->glyphs[t]->path;
144 int len = path?path->getLength():0;
145 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
146 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
147 src->glyphs[t]->glyphid = font->num_glyphs;
148 glyph->unicode = src->glyphs[t]->unicode;
149 if(glyph->unicode >= font->max_unicode)
150 font->max_unicode = glyph->unicode+1;
152 gfxdrawer_target_gfxline(&drawer);
159 path->getPoint(s, &x, &y, &f);
162 if(f&splashPathFirst) {
163 drawer.moveTo(&drawer, x*scale, y*scale);
165 if(f&splashPathCurve) {
167 path->getPoint(++s, &x2, &y2, &f);
168 if(f&splashPathCurve) {
170 path->getPoint(++s, &x3, &y3, &f);
171 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
173 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
176 drawer.lineTo(&drawer, x*scale, y*scale);
178 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
179 // (f&splashPathFirst)?"first":"",
180 // (f&splashPathLast)?"last":"");
183 glyph->line = (gfxline_t*)drawer.result(&drawer);
184 if(src->glyphs[t]->advance>0) {
185 glyph->advance = src->glyphs[t]->advance;
187 msg("<warning> Approximating advance value for glyph %d", t);
188 glyph->advance = xmax*scale;
191 double max = src->glyphs[t]->advance_max;
192 if(max>0 && max > glyph->advance) {
193 glyph->advance = max;
200 font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
201 memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
202 for(t=0;t<font->num_glyphs;t++) {
203 if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
204 font->unicode2glyph[font->glyphs[t].unicode] = t;
208 msg("<trace> %d glyphs.", t, font->num_glyphs);
212 double InfoOutputDev::getMaximumFontSize(char*id)
214 FontInfo*info = (FontInfo*)id2font->lookup(id);
216 msg("<error> Unknown font id: %s", id);
219 return info->max_size;
222 char*getFontID(GfxFont*font)
224 Ref*ref = font->getID();
225 GString*gstr = font->getName();
226 char* fname = gstr==0?0:gstr->getCString();
229 if(font->getType() == fontType3) {
230 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
232 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
235 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
240 void InfoOutputDev::updateFont(GfxState *state)
242 GfxFont*font = state->getFont();
247 if(font->getType() == fontType3) {
251 char*id = getFontID(font);
254 currentfont->splash_font = 0;
256 currentfont = (FontInfo*)id2font->lookup(id);
258 currentfont = new FontInfo;
259 currentfont->font = font;
260 currentfont->max_size = 0;
261 GString* idStr = new GString(id);
262 id2font->add(idStr, (void*)currentfont);
266 state->setCTM(1.0,0,0,1.0,0,0);
267 splash->updateCTM(state, 0,0,0,0,0,0);
268 state->setTextMat(1.0,0,0,1.0,0,0);
269 state->setFont(font, 1024.0);
270 splash->doUpdateFont(state);
271 currentfont->splash_font = splash->getCurrentFont();
272 if(currentfont->splash_font) {
273 currentfont->ascender = currentfont->splash_font->ascender;
274 currentfont->descender = currentfont->splash_font->descender;
276 currentfont->ascender = currentfont->descender = 0;
279 currentfont->gfxfont = this->createGfxFont(font, currentfont);
280 currentfont->gfxfont->id = strdup(id);
281 fonts = gfxfontlist_addfont(fonts, currentfont->gfxfont);
286 void InfoOutputDev::fill(GfxState *state)
291 void InfoOutputDev::eoFill(GfxState *state)
296 FontInfo* InfoOutputDev::getFont(char*id)
298 return (FontInfo*)id2font->lookup(id);
301 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
302 double dx, double dy,
303 double originX, double originY,
304 CharCode code, int nBytes, Unicode *u, int uLen)
306 double m11,m21,m12,m22;
307 state->getFontTransMat(&m11, &m12, &m21, &m22);
308 m11 *= state->getHorizScaling();
309 m21 *= state->getHorizScaling();
310 double lenx = sqrt(m11*m11 + m12*m12);
311 double leny = sqrt(m21*m21 + m22*m22);
312 double len = lenx>leny?lenx:leny;
313 if(!currentfont || !currentfont->splash_font) {
316 if(currentfont && currentfont->max_size < len) {
317 currentfont->max_size = len;
319 currentfont->grow(code+1);
320 GlyphInfo*g = currentfont->glyphs[code];
322 g = currentfont->glyphs[code] = new GlyphInfo();
324 currentfont->splash_font->last_advance = -1;
325 g->path = currentfont->splash_font->getGlyphPath(code);
326 g->advance = currentfont->splash_font->last_advance;
329 if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
332 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
333 double xshift = x - currentfont->lastx;
334 if(xshift>=0 && xshift > g->advance_max) {
335 g->advance_max = xshift;
339 currentfont->lastx = x;
340 currentfont->lasty = y;
341 currentfont->lastchar = code;
344 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
346 GfxFont*font = state->getFont();
349 if(font->getType() != fontType3)
352 char*id = getFontID(font);
353 currentfont = (FontInfo*)id2font->lookup(id);
355 currentfont = new FontInfo;
356 currentfont->font = font;
357 GString* idStr = new GString(id);
358 id2font->add(idStr, (void*)currentfont);
361 currentfont = currentfont;
364 currentfont->grow(code+1);
365 if(!currentfont->glyphs[code]) {
366 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
367 currentglyph->unicode = uLen?u[0]:0;
368 currentglyph->path = new SplashPath();
379 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
387 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
389 currentglyph->x1=llx;
390 currentglyph->y1=lly;
391 currentglyph->x2=urx;
392 currentglyph->y2=ury;
395 void InfoOutputDev::endType3Char(GfxState *state)
397 double x1 = currentglyph->x1;
398 double y1 = currentglyph->y1;
399 double x2 = currentglyph->x2;
400 double y2 = currentglyph->y2;
401 currentglyph->path->moveTo(x1,y1);
402 currentglyph->path->lineTo(x2,y1);
403 currentglyph->path->lineTo(x2,y2);
404 currentglyph->path->lineTo(x1,y2);
405 currentglyph->path->close();
408 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
409 int width, int height, GBool invert,
413 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
415 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
416 int width, int height, GfxImageColorMap *colorMap,
417 int *maskColors, GBool inlineImg)
420 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
422 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
423 int width, int height,
424 GfxImageColorMap *colorMap,
426 int maskWidth, int maskHeight,
429 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
432 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
433 int width, int height,
434 GfxImageColorMap *colorMap,
436 int maskWidth, int maskHeight,
437 GfxImageColorMap *maskColorMap)
439 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);