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;
92 gfxfont_free(this->gfxfont);
95 gfxfont_t* createGfxFont(FontInfo*src)
97 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
98 memset(font, 0, sizeof(gfxfont_t));
100 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
101 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
105 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
107 //printf("%d glyphs\n", font->num_glyphs);
108 font->num_glyphs = 0;
109 font->ascent = fabs(src->descender);
110 font->descent = fabs(src->ascender);
112 for(t=0;t<src->num_glyphs;t++) {
114 SplashPath*path = src->glyphs[t]->path;
115 int len = path?path->getLength():0;
116 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
117 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
118 src->glyphs[t]->glyphid = font->num_glyphs;
119 glyph->unicode = src->glyphs[t]->unicode;
120 if(glyph->unicode >= font->max_unicode)
121 font->max_unicode = glyph->unicode+1;
123 gfxdrawer_target_gfxline(&drawer);
130 path->getPoint(s, &x, &y, &f);
133 if(f&splashPathFirst) {
134 drawer.moveTo(&drawer, x*scale, y*scale);
136 if(f&splashPathCurve) {
138 path->getPoint(++s, &x2, &y2, &f);
139 if(f&splashPathCurve) {
141 path->getPoint(++s, &x3, &y3, &f);
142 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
144 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
147 drawer.lineTo(&drawer, x*scale, y*scale);
149 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
150 // (f&splashPathFirst)?"first":"",
151 // (f&splashPathLast)?"last":"");
154 glyph->line = (gfxline_t*)drawer.result(&drawer);
155 if(src->glyphs[t]->advance>0) {
156 glyph->advance = src->glyphs[t]->advance;
158 msg("<warning> Approximating advance value for glyph %d", t);
159 glyph->advance = xmax*scale;
162 double max = src->glyphs[t]->advance_max;
163 if(max>0 && max > glyph->advance) {
164 glyph->advance = max;
171 font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
172 memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
173 for(t=0;t<font->num_glyphs;t++) {
174 if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
175 font->unicode2glyph[font->glyphs[t].unicode] = t;
183 gfxfont_t* FontInfo::getGfxFont()
186 this->gfxfont = createGfxFont(this);
187 this->gfxfont->id = strdup(this->id);
189 return this->gfxfont;
192 GBool InfoOutputDev::upsideDown() {return gTrue;}
193 GBool InfoOutputDev::useDrawChar() {return gTrue;}
194 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
195 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
197 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
200 state->transform(crop_x1,crop_y1,&x1,&y1);
201 state->transform(crop_x2,crop_y2,&x2,&y2);
202 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
203 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
208 msg("<verbose> Generating info structure for page %d", pageNum);
210 void InfoOutputDev::endPage()
213 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
218 /* } else if(!strcmp(key,"fontquality")) {
219 this->config_fontquality = atof(value);
220 if(this->config_fontquality<=1)
221 this->config_fontquality=1;
222 } else if(!strcmp(key,"bigchar")) {
223 this->config_bigchar = atoi(value);
227 double InfoOutputDev::getMaximumFontSize(char*id)
229 FontInfo*info = (FontInfo*)id2font->lookup(id);
231 msg("<error> Unknown font id: %s", id);
234 return info->max_size;
237 char*getFontID(GfxFont*font)
239 Ref*ref = font->getID();
240 GString*gstr = font->getName();
241 char* fname = gstr==0?0:gstr->getCString();
244 if(font->getType() == fontType3) {
245 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
247 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
250 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
255 void InfoOutputDev::updateFont(GfxState *state)
257 GfxFont*font = state->getFont();
262 if(font->getType() == fontType3) {
266 char*id = getFontID(font);
269 currentfont->splash_font = 0;
271 currentfont = (FontInfo*)id2font->lookup(id);
273 currentfont = new FontInfo(id);
274 currentfont->font = font;
275 currentfont->max_size = 0;
276 GString* idStr = new GString(id);
277 id2font->add(idStr, (void*)currentfont);
281 state->setCTM(1.0,0,0,1.0,0,0);
282 splash->updateCTM(state, 0,0,0,0,0,0);
283 state->setTextMat(1.0,0,0,1.0,0,0);
284 state->setFont(font, 1024.0);
285 splash->doUpdateFont(state);
286 currentfont->splash_font = splash->getCurrentFont();
287 if(currentfont->splash_font) {
288 currentfont->ascender = currentfont->splash_font->ascender;
289 currentfont->descender = currentfont->splash_font->descender;
291 currentfont->ascender = currentfont->descender = 0;
297 void InfoOutputDev::fill(GfxState *state)
302 void InfoOutputDev::eoFill(GfxState *state)
307 FontInfo* InfoOutputDev::getFont(char*id)
309 return (FontInfo*)id2font->lookup(id);
312 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
313 double dx, double dy,
314 double originX, double originY,
315 CharCode code, int nBytes, Unicode *u, int uLen)
317 double m11,m21,m12,m22;
318 state->getFontTransMat(&m11, &m12, &m21, &m22);
319 m11 *= state->getHorizScaling();
320 m21 *= state->getHorizScaling();
321 double lenx = sqrt(m11*m11 + m12*m12);
322 double leny = sqrt(m21*m21 + m22*m22);
323 double len = lenx>leny?lenx:leny;
324 if(!currentfont || !currentfont->splash_font) {
327 if(currentfont && currentfont->max_size < len) {
328 currentfont->max_size = len;
333 currentfont->grow(code+1);
334 GlyphInfo*g = currentfont->glyphs[code];
336 g = currentfont->glyphs[code] = new GlyphInfo();
338 currentfont->splash_font->last_advance = -1;
339 g->path = currentfont->splash_font->getGlyphPath(code);
340 g->advance = currentfont->splash_font->last_advance;
343 if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
346 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
347 double xshift = x - currentfont->lastx;
348 if(xshift>=0 && xshift > g->advance_max) {
349 g->advance_max = xshift;
353 currentfont->lastx = x;
354 currentfont->lasty = y;
355 currentfont->lastchar = code;
358 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
360 GfxFont*font = state->getFont();
363 if(font->getType() != fontType3)
366 char*id = getFontID(font);
367 currentfont = (FontInfo*)id2font->lookup(id);
369 currentfont = new FontInfo(id);
370 currentfont->font = font;
371 GString* idStr = new GString(id);
372 id2font->add(idStr, (void*)currentfont);
375 currentfont = currentfont;
378 currentfont->grow(code+1);
379 if(!currentfont->glyphs[code]) {
380 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
381 currentglyph->unicode = uLen?u[0]:0;
382 currentglyph->path = new SplashPath();
393 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
401 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
403 currentglyph->x1=llx;
404 currentglyph->y1=lly;
405 currentglyph->x2=urx;
406 currentglyph->y2=ury;
409 void InfoOutputDev::endType3Char(GfxState *state)
411 double x1 = currentglyph->x1;
412 double y1 = currentglyph->y1;
413 double x2 = currentglyph->x2;
414 double y2 = currentglyph->y2;
415 currentglyph->path->moveTo(x1,y1);
416 currentglyph->path->lineTo(x2,y1);
417 currentglyph->path->lineTo(x2,y2);
418 currentglyph->path->lineTo(x1,y2);
419 currentglyph->path->close();
422 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
423 int width, int height, GBool invert,
426 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
428 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
430 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
431 int width, int height, GfxImageColorMap *colorMap,
432 int *maskColors, GBool inlineImg)
434 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
436 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
438 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
439 int width, int height,
440 GfxImageColorMap *colorMap,
442 int maskWidth, int maskHeight,
445 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
447 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
450 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
451 int width, int height,
452 GfxImageColorMap *colorMap,
454 int maskWidth, int maskHeight,
455 GfxImageColorMap *maskColorMap)
457 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
459 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
462 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
467 id2font->startIter(&i);
468 while(id2font->getNext(&i, &key, (void**)&font)) {
469 dev->addfont(dev, font->getGfxFont());