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"
21 int config_addspace = 1;
22 int config_fontquality = 10;
23 int config_bigchar = 0;
25 InfoOutputDev::InfoOutputDev(XRef*xref)
35 id2font = new GHash(1);
36 SplashColor white = {255,255,255};
37 splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
38 splash->startDoc(xref);
40 InfoOutputDev::~InfoOutputDev()
43 id2font->startIter(&i);
46 while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
49 id2font->killIter(&i);
51 delete id2font;id2font=0;
52 delete splash;splash=0;
54 void FontInfo::grow(int size)
56 if(size >= this->num_glyphs) {
57 this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
58 memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
59 this->num_glyphs = size;
62 FontInfo::FontInfo(char*id)
64 this->id = strdup(id);
65 this->charid2glyph = 0;
69 this->splash_font = 0;
74 this->space_char = -1;
78 if(this->id) {free(this->id);this->id=0;}
80 if(this->charid2glyph) {
81 free(this->charid2glyph);
82 this->charid2glyph = 0;
85 for(t=0;t<num_glyphs;t++) {
87 delete glyphs[t]->path;glyphs[t]->path = 0;
92 free(glyphs);glyphs=0;
94 gfxfont_free(this->gfxfont);
97 static int findSpace(gfxfont_t*font)
101 for(t=0;t<font->num_glyphs;t++) {
102 gfxglyph_t*g = &font->glyphs[t];
103 if(GLYPH_IS_SPACE(g)) {
104 if(g->unicode == 32) return t;
109 if(font->num_glyphs>32 && GLYPH_IS_SPACE(&font->glyphs[32])) {
115 static int addSpace(gfxfont_t*font)
118 font->glyphs = (gfxglyph_t*)realloc(font->glyphs, sizeof(gfxglyph_t)*font->num_glyphs);
119 gfxglyph_t*g = &font->glyphs[font->num_glyphs-1];
120 memset(g, 0, sizeof(*g));
122 //g->advance = font->ascent;
123 g->advance = fabs(font->ascent - font->descent)*2 / 3;
124 if(font->max_unicode > 32)
125 font->unicode2glyph[32] = font->num_glyphs-1;
127 g->line = gfxline_makerectangle(0, -font->ascent, g->advance, font->descent);
129 return font->num_glyphs-1;
132 static gfxfont_t* createGfxFont(FontInfo*src)
134 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
135 memset(font, 0, sizeof(gfxfont_t));
137 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
138 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
142 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
144 //printf("%d glyphs\n", font->num_glyphs);
145 font->num_glyphs = 0;
146 font->ascent = fabs(src->descender);
147 font->descent = fabs(src->ascender);
149 for(t=0;t<src->num_glyphs;t++) {
151 SplashPath*path = src->glyphs[t]->path;
152 int len = path?path->getLength():0;
153 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
154 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
155 src->glyphs[t]->glyphid = font->num_glyphs;
156 glyph->unicode = src->glyphs[t]->unicode;
157 if(glyph->unicode >= font->max_unicode)
158 font->max_unicode = glyph->unicode+1;
160 gfxdrawer_target_gfxline(&drawer);
167 path->getPoint(s, &x, &y, &f);
170 if(f&splashPathFirst) {
171 drawer.moveTo(&drawer, x*scale, y*scale);
173 if(f&splashPathCurve) {
175 path->getPoint(++s, &x2, &y2, &f);
176 if(f&splashPathCurve) {
178 path->getPoint(++s, &x3, &y3, &f);
179 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
181 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
184 drawer.lineTo(&drawer, x*scale, y*scale);
186 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
187 // (f&splashPathFirst)?"first":"",
188 // (f&splashPathLast)?"last":"");
191 glyph->line = (gfxline_t*)drawer.result(&drawer);
192 if(src->glyphs[t]->advance>0) {
193 glyph->advance = src->glyphs[t]->advance;
195 msg("<warning> Approximating advance value for glyph %d", t);
196 glyph->advance = xmax*scale;
199 double max = src->glyphs[t]->advance_max;
200 if(max>0 && max > glyph->advance) {
201 glyph->advance = max;
208 font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
209 memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
210 for(t=0;t<font->num_glyphs;t++) {
211 if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
212 font->unicode2glyph[font->glyphs[t].unicode] = t;
220 gfxfont_t* FontInfo::getGfxFont()
223 this->gfxfont = createGfxFont(this);
224 this->gfxfont->id = strdup(this->id);
225 this->space_char = findSpace(this->gfxfont);
226 if(this->space_char>=0) {
227 msg("<debug> Font %s has space char %d (unicode=%d)",
228 this->id, this->space_char,
229 this->gfxfont->glyphs[this->space_char].unicode);
230 } else if(config_addspace) {
231 this->space_char = addSpace(this->gfxfont);
232 msg("<debug> Appending space char to font %s, position %d", this->gfxfont->id, this->space_char);
235 return this->gfxfont;
238 GBool InfoOutputDev::upsideDown() {return gTrue;}
239 GBool InfoOutputDev::useDrawChar() {return gTrue;}
240 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
241 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
243 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
246 state->transform(crop_x1,crop_y1,&x1,&y1);
247 state->transform(crop_x2,crop_y2,&x2,&y2);
248 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
249 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
254 msg("<verbose> Generating info structure for page %d", pageNum);
256 void InfoOutputDev::endPage()
259 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
264 /* } else if(!strcmp(key,"fontquality")) {
265 this->config_fontquality = atof(value);
266 if(this->config_fontquality<=1)
267 this->config_fontquality=1;
268 } else if(!strcmp(key,"bigchar")) {
269 this->config_bigchar = atoi(value);
273 double InfoOutputDev::getMaximumFontSize(char*id)
275 FontInfo*info = (FontInfo*)id2font->lookup(id);
277 msg("<error> Unknown font id: %s", id);
280 return info->max_size;
283 char*getFontID(GfxFont*font)
285 Ref*ref = font->getID();
286 GString*gstr = font->getName();
287 char* fname = gstr==0?0:gstr->getCString();
290 if(font->getType() == fontType3) {
291 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
293 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
296 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
301 void InfoOutputDev::updateFont(GfxState *state)
303 GfxFont*font = state->getFont();
308 if(font->getType() == fontType3) {
312 char*id = getFontID(font);
315 currentfont->splash_font = 0;
317 currentfont = (FontInfo*)id2font->lookup(id);
319 currentfont = new FontInfo(id);
320 currentfont->font = font;
321 currentfont->max_size = 0;
322 GString* idStr = new GString(id);
323 id2font->add(idStr, (void*)currentfont);
327 state->setCTM(1.0,0,0,1.0,0,0);
328 splash->updateCTM(state, 0,0,0,0,0,0);
329 state->setTextMat(1.0,0,0,1.0,0,0);
330 state->setFont(font, 1024.0);
331 splash->doUpdateFont(state);
332 currentfont->splash_font = splash->getCurrentFont();
333 if(currentfont->splash_font) {
334 currentfont->ascender = currentfont->splash_font->ascender;
335 currentfont->descender = currentfont->splash_font->descender;
337 currentfont->ascender = currentfont->descender = 0;
343 void InfoOutputDev::fill(GfxState *state)
348 void InfoOutputDev::eoFill(GfxState *state)
353 FontInfo* InfoOutputDev::getFont(char*id)
355 return (FontInfo*)id2font->lookup(id);
358 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
359 double dx, double dy,
360 double originX, double originY,
361 CharCode code, int nBytes, Unicode *u, int uLen)
363 double m11,m21,m12,m22;
364 state->getFontTransMat(&m11, &m12, &m21, &m22);
365 m11 *= state->getHorizScaling();
366 m21 *= state->getHorizScaling();
367 double lenx = sqrt(m11*m11 + m12*m12);
368 double leny = sqrt(m21*m21 + m22*m22);
369 double len = lenx>leny?lenx:leny;
370 if(!currentfont || !currentfont->splash_font) {
373 if(currentfont && currentfont->max_size < len) {
374 currentfont->max_size = len;
379 currentfont->grow(code+1);
380 GlyphInfo*g = currentfont->glyphs[code];
382 g = currentfont->glyphs[code] = new GlyphInfo();
384 currentfont->splash_font->last_advance = -1;
385 g->path = currentfont->splash_font->getGlyphPath(code);
386 g->advance = currentfont->splash_font->last_advance;
389 if(uLen && ((u[0]>=32 && u[0]<g->unicode) || !g->unicode)) {
392 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
393 double xshift = x - currentfont->lastx;
394 if(xshift>=0 && xshift > g->advance_max) {
395 g->advance_max = xshift;
399 currentfont->lastx = x;
400 currentfont->lasty = y;
401 currentfont->lastchar = code;
404 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
406 GfxFont*font = state->getFont();
409 if(font->getType() != fontType3)
412 char*id = getFontID(font);
413 currentfont = (FontInfo*)id2font->lookup(id);
415 currentfont = new FontInfo(id);
416 currentfont->font = font;
417 GString* idStr = new GString(id);
418 id2font->add(idStr, (void*)currentfont);
421 currentfont = currentfont;
424 currentfont->grow(code+1);
425 if(!currentfont->glyphs[code]) {
426 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
427 currentglyph->unicode = uLen?u[0]:0;
428 currentglyph->path = new SplashPath();
439 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
447 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
449 currentglyph->x1=llx;
450 currentglyph->y1=lly;
451 currentglyph->x2=urx;
452 currentglyph->y2=ury;
455 void InfoOutputDev::endType3Char(GfxState *state)
457 double x1 = currentglyph->x1;
458 double y1 = currentglyph->y1;
459 double x2 = currentglyph->x2;
460 double y2 = currentglyph->y2;
461 currentglyph->path->moveTo(x1,y1);
462 currentglyph->path->lineTo(x2,y1);
463 currentglyph->path->lineTo(x2,y2);
464 currentglyph->path->lineTo(x1,y2);
465 currentglyph->path->close();
468 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
469 int width, int height, GBool invert,
472 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
474 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
476 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
477 int width, int height, GfxImageColorMap *colorMap,
478 int *maskColors, GBool inlineImg)
480 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
482 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
484 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
485 int width, int height,
486 GfxImageColorMap *colorMap,
488 int maskWidth, int maskHeight,
491 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
493 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
496 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
497 int width, int height,
498 GfxImageColorMap *colorMap,
500 int maskWidth, int maskHeight,
501 GfxImageColorMap *maskColorMap)
503 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
505 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
508 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
513 id2font->startIter(&i);
514 while(id2font->getNext(&i, &key, (void**)&font)) {
515 dev->addfont(dev, font->getGfxFont());