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 /* there's not yet a way to set this */
21 int config_fontquality = 10;
22 int config_bigchar = 0;
24 InfoOutputDev::InfoOutputDev(XRef*xref)
34 id2font = new GHash(1);
35 SplashColor white = {255,255,255};
36 splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
37 splash->startDoc(xref);
39 InfoOutputDev::~InfoOutputDev()
42 id2font->startIter(&i);
45 while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
48 id2font->killIter(&i);
50 delete id2font;id2font=0;
51 delete splash;splash=0;
53 void FontInfo::grow(int size)
55 if(size >= this->num_glyphs) {
56 this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
57 memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
58 this->num_glyphs = size;
61 FontInfo::FontInfo(char*id)
63 this->id = strdup(id);
64 this->charid2glyph = 0;
68 this->splash_font = 0;
76 if(this->id) {free(this->id);this->id=0;}
78 if(this->charid2glyph) {
79 free(this->charid2glyph);
80 this->charid2glyph = 0;
83 for(t=0;t<num_glyphs;t++) {
85 delete glyphs[t]->path;glyphs[t]->path = 0;
90 free(glyphs);glyphs=0;
93 gfxfont_t* createGfxFont(FontInfo*src)
95 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
96 memset(font, 0, sizeof(gfxfont_t));
98 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
99 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
103 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
105 //printf("%d glyphs\n", font->num_glyphs);
106 font->num_glyphs = 0;
107 font->ascent = fabs(src->descender);
108 font->descent = fabs(src->ascender);
110 for(t=0;t<src->num_glyphs;t++) {
112 SplashPath*path = src->glyphs[t]->path;
113 int len = path?path->getLength():0;
114 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
115 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
116 src->glyphs[t]->glyphid = font->num_glyphs;
117 glyph->unicode = src->glyphs[t]->unicode;
118 if(glyph->unicode >= font->max_unicode)
119 font->max_unicode = glyph->unicode+1;
121 gfxdrawer_target_gfxline(&drawer);
128 path->getPoint(s, &x, &y, &f);
131 if(f&splashPathFirst) {
132 drawer.moveTo(&drawer, x*scale, y*scale);
134 if(f&splashPathCurve) {
136 path->getPoint(++s, &x2, &y2, &f);
137 if(f&splashPathCurve) {
139 path->getPoint(++s, &x3, &y3, &f);
140 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
142 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
145 drawer.lineTo(&drawer, x*scale, y*scale);
147 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
148 // (f&splashPathFirst)?"first":"",
149 // (f&splashPathLast)?"last":"");
152 glyph->line = (gfxline_t*)drawer.result(&drawer);
153 if(src->glyphs[t]->advance>0) {
154 glyph->advance = src->glyphs[t]->advance;
156 msg("<warning> Approximating advance value for glyph %d", t);
157 glyph->advance = xmax*scale;
160 double max = src->glyphs[t]->advance_max;
161 if(max>0 && max > glyph->advance) {
162 glyph->advance = max;
169 font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
170 memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
171 for(t=0;t<font->num_glyphs;t++) {
172 if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
173 font->unicode2glyph[font->glyphs[t].unicode] = t;
181 gfxfont_t* FontInfo::getGfxFont()
184 this->gfxfont = createGfxFont(this);
185 this->gfxfont->id = strdup(this->id);
187 return this->gfxfont;
190 GBool InfoOutputDev::upsideDown() {return gTrue;}
191 GBool InfoOutputDev::useDrawChar() {return gTrue;}
192 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
193 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
195 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
198 state->transform(crop_x1,crop_y1,&x1,&y1);
199 state->transform(crop_x2,crop_y2,&x2,&y2);
200 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
201 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
206 msg("<verbose> Generating info structure for page %d", pageNum);
208 void InfoOutputDev::endPage()
211 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
216 /* } else if(!strcmp(key,"fontquality")) {
217 this->config_fontquality = atof(value);
218 if(this->config_fontquality<=1)
219 this->config_fontquality=1;
220 } else if(!strcmp(key,"bigchar")) {
221 this->config_bigchar = atoi(value);
225 double InfoOutputDev::getMaximumFontSize(char*id)
227 FontInfo*info = (FontInfo*)id2font->lookup(id);
229 msg("<error> Unknown font id: %s", id);
232 return info->max_size;
235 char*getFontID(GfxFont*font)
237 Ref*ref = font->getID();
238 GString*gstr = font->getName();
239 char* fname = gstr==0?0:gstr->getCString();
242 if(font->getType() == fontType3) {
243 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
245 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
248 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
253 void InfoOutputDev::updateFont(GfxState *state)
255 GfxFont*font = state->getFont();
260 if(font->getType() == fontType3) {
264 char*id = getFontID(font);
267 currentfont->splash_font = 0;
269 currentfont = (FontInfo*)id2font->lookup(id);
271 currentfont = new FontInfo(id);
272 currentfont->font = font;
273 currentfont->max_size = 0;
274 GString* idStr = new GString(id);
275 id2font->add(idStr, (void*)currentfont);
279 state->setCTM(1.0,0,0,1.0,0,0);
280 splash->updateCTM(state, 0,0,0,0,0,0);
281 state->setTextMat(1.0,0,0,1.0,0,0);
282 state->setFont(font, 1024.0);
283 splash->doUpdateFont(state);
284 currentfont->splash_font = splash->getCurrentFont();
285 if(currentfont->splash_font) {
286 currentfont->ascender = currentfont->splash_font->ascender;
287 currentfont->descender = currentfont->splash_font->descender;
289 currentfont->ascender = currentfont->descender = 0;
295 void InfoOutputDev::fill(GfxState *state)
300 void InfoOutputDev::eoFill(GfxState *state)
305 FontInfo* InfoOutputDev::getFont(char*id)
307 return (FontInfo*)id2font->lookup(id);
310 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
311 double dx, double dy,
312 double originX, double originY,
313 CharCode code, int nBytes, Unicode *u, int uLen)
315 double m11,m21,m12,m22;
316 state->getFontTransMat(&m11, &m12, &m21, &m22);
317 m11 *= state->getHorizScaling();
318 m21 *= state->getHorizScaling();
319 double lenx = sqrt(m11*m11 + m12*m12);
320 double leny = sqrt(m21*m21 + m22*m22);
321 double len = lenx>leny?lenx:leny;
322 if(!currentfont || !currentfont->splash_font) {
325 if(currentfont && currentfont->max_size < len) {
326 currentfont->max_size = len;
331 currentfont->grow(code+1);
332 GlyphInfo*g = currentfont->glyphs[code];
334 g = currentfont->glyphs[code] = new GlyphInfo();
336 currentfont->splash_font->last_advance = -1;
337 g->path = currentfont->splash_font->getGlyphPath(code);
338 g->advance = currentfont->splash_font->last_advance;
341 if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
344 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
345 double xshift = x - currentfont->lastx;
346 if(xshift>=0 && xshift > g->advance_max) {
347 g->advance_max = xshift;
351 currentfont->lastx = x;
352 currentfont->lasty = y;
353 currentfont->lastchar = code;
356 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
358 GfxFont*font = state->getFont();
361 if(font->getType() != fontType3)
364 char*id = getFontID(font);
365 currentfont = (FontInfo*)id2font->lookup(id);
367 currentfont = new FontInfo(id);
368 currentfont->font = font;
369 GString* idStr = new GString(id);
370 id2font->add(idStr, (void*)currentfont);
373 currentfont = currentfont;
376 currentfont->grow(code+1);
377 if(!currentfont->glyphs[code]) {
378 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
379 currentglyph->unicode = uLen?u[0]:0;
380 currentglyph->path = new SplashPath();
391 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
399 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
401 currentglyph->x1=llx;
402 currentglyph->y1=lly;
403 currentglyph->x2=urx;
404 currentglyph->y2=ury;
407 void InfoOutputDev::endType3Char(GfxState *state)
409 double x1 = currentglyph->x1;
410 double y1 = currentglyph->y1;
411 double x2 = currentglyph->x2;
412 double y2 = currentglyph->y2;
413 currentglyph->path->moveTo(x1,y1);
414 currentglyph->path->lineTo(x2,y1);
415 currentglyph->path->lineTo(x2,y2);
416 currentglyph->path->lineTo(x1,y2);
417 currentglyph->path->close();
420 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
421 int width, int height, GBool invert,
424 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
426 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
428 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
429 int width, int height, GfxImageColorMap *colorMap,
430 int *maskColors, GBool inlineImg)
432 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
434 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
436 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
437 int width, int height,
438 GfxImageColorMap *colorMap,
440 int maskWidth, int maskHeight,
443 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
445 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
448 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
449 int width, int height,
450 GfxImageColorMap *colorMap,
452 int maskWidth, int maskHeight,
453 GfxImageColorMap *maskColorMap)
455 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
457 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
460 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
465 id2font->startIter(&i);
466 while(id2font->getNext(&i, &key, (void**)&font)) {
467 dev->addfont(dev, font->getGfxFont());