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"
22 int config_addspace = 1;
23 int config_fontquality = 10;
24 int config_bigchar = 0;
26 InfoOutputDev::InfoOutputDev(XRef*xref)
36 id2font = new GHash(1);
37 SplashColor white = {255,255,255};
38 splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
39 splash->startDoc(xref);
41 InfoOutputDev::~InfoOutputDev()
44 id2font->startIter(&i);
47 while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
50 id2font->killIter(&i);
52 delete id2font;id2font=0;
53 delete splash;splash=0;
55 void FontInfo::grow(int size)
57 if(size >= this->num_glyphs) {
58 this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
59 memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
60 this->num_glyphs = size;
63 FontInfo::FontInfo(char*id)
65 this->id = strdup(id);
66 this->charid2glyph = 0;
70 this->splash_font = 0;
75 this->space_char = -1;
79 if(this->id) {free(this->id);this->id=0;}
81 if(this->charid2glyph) {
82 free(this->charid2glyph);
83 this->charid2glyph = 0;
86 for(t=0;t<num_glyphs;t++) {
88 delete glyphs[t]->path;glyphs[t]->path = 0;
93 free(glyphs);glyphs=0;
95 gfxfont_free(this->gfxfont);
98 static int findSpace(gfxfont_t*font)
100 int first_space = -1;
102 for(t=0;t<font->num_glyphs;t++) {
103 gfxglyph_t*g = &font->glyphs[t];
104 if(GLYPH_IS_SPACE(g)) {
105 if(g->unicode == 32) return t;
110 if(font->num_glyphs>32 && GLYPH_IS_SPACE(&font->glyphs[32])) {
116 static int addSpace(gfxfont_t*font)
119 font->glyphs = (gfxglyph_t*)realloc(font->glyphs, sizeof(gfxglyph_t)*font->num_glyphs);
120 gfxglyph_t*g = &font->glyphs[font->num_glyphs-1];
121 memset(g, 0, sizeof(*g));
123 //g->advance = font->ascent;
124 g->advance = fabs(font->ascent - font->descent)*2 / 3;
125 if(font->max_unicode > 32)
126 font->unicode2glyph[32] = font->num_glyphs-1;
128 g->line = gfxline_makerectangle(0, -font->ascent, g->advance, font->descent);
130 return font->num_glyphs-1;
133 static gfxfont_t* createGfxFont(FontInfo*src)
135 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
136 memset(font, 0, sizeof(gfxfont_t));
138 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
139 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
143 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
145 //printf("%d glyphs\n", font->num_glyphs);
146 font->num_glyphs = 0;
147 font->ascent = fabs(src->descender);
148 font->descent = fabs(src->ascender);
150 for(t=0;t<src->num_glyphs;t++) {
152 SplashPath*path = src->glyphs[t]->path;
153 int len = path?path->getLength():0;
154 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
155 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
156 src->glyphs[t]->glyphid = font->num_glyphs;
157 glyph->unicode = src->glyphs[t]->unicode;
158 if(glyph->unicode >= font->max_unicode)
159 font->max_unicode = glyph->unicode+1;
161 gfxdrawer_target_gfxline(&drawer);
168 path->getPoint(s, &x, &y, &f);
171 if(f&splashPathFirst) {
172 drawer.moveTo(&drawer, x*scale, y*scale);
174 if(f&splashPathCurve) {
176 path->getPoint(++s, &x2, &y2, &f);
177 if(f&splashPathCurve) {
179 path->getPoint(++s, &x3, &y3, &f);
180 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
182 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
185 drawer.lineTo(&drawer, x*scale, y*scale);
187 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
188 // (f&splashPathFirst)?"first":"",
189 // (f&splashPathLast)?"last":"");
192 glyph->line = (gfxline_t*)drawer.result(&drawer);
193 if(src->glyphs[t]->advance>0) {
194 glyph->advance = src->glyphs[t]->advance;
196 msg("<warning> Approximating advance value for glyph %d", t);
197 glyph->advance = xmax*scale;
200 double max = src->glyphs[t]->advance_max;
201 if(max>0 && max > glyph->advance) {
202 glyph->advance = max;
209 font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
210 memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
211 for(t=0;t<font->num_glyphs;t++) {
212 if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
213 font->unicode2glyph[font->glyphs[t].unicode] = t;
220 static float find_average_glyph_advance(gfxfont_t*f)
225 float*values = (float*)malloc(sizeof(float)*f->num_glyphs);
227 for(t=0;t<f->num_glyphs;t++) {
228 values[t] = f->glyphs[t].advance;
230 float m = medianf(values, f->num_glyphs);
235 gfxfont_t* FontInfo::getGfxFont()
238 this->gfxfont = createGfxFont(this);
239 this->gfxfont->id = strdup(this->id);
240 this->space_char = findSpace(this->gfxfont);
241 this->average_advance = find_average_glyph_advance(this->gfxfont);
243 if(this->space_char>=0) {
244 msg("<debug> Font %s has space char %d (unicode=%d)",
245 this->id, this->space_char,
246 this->gfxfont->glyphs[this->space_char].unicode);
247 } else if(config_addspace) {
248 this->space_char = addSpace(this->gfxfont);
249 msg("<debug> Appending space char to font %s, position %d", this->gfxfont->id, this->space_char);
252 return this->gfxfont;
255 GBool InfoOutputDev::upsideDown() {return gTrue;}
256 GBool InfoOutputDev::useDrawChar() {return gTrue;}
257 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
258 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
260 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
263 state->transform(crop_x1,crop_y1,&x1,&y1);
264 state->transform(crop_x2,crop_y2,&x2,&y2);
265 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
266 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
271 msg("<verbose> Generating info structure for page %d", pageNum);
273 void InfoOutputDev::endPage()
276 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
281 /* } else if(!strcmp(key,"fontquality")) {
282 this->config_fontquality = atof(value);
283 if(this->config_fontquality<=1)
284 this->config_fontquality=1;
285 } else if(!strcmp(key,"bigchar")) {
286 this->config_bigchar = atoi(value);
290 double InfoOutputDev::getMaximumFontSize(char*id)
292 FontInfo*info = (FontInfo*)id2font->lookup(id);
294 msg("<error> Unknown font id: %s", id);
297 return info->max_size;
300 char*getFontID(GfxFont*font)
302 Ref*ref = font->getID();
303 GString*gstr = font->getName();
304 char* fname = gstr==0?0:gstr->getCString();
307 if(font->getType() == fontType3) {
308 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
310 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
313 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
318 void InfoOutputDev::updateFont(GfxState *state)
320 GfxFont*font = state->getFont();
325 if(font->getType() == fontType3) {
329 char*id = getFontID(font);
332 currentfont->splash_font = 0;
334 currentfont = (FontInfo*)id2font->lookup(id);
336 currentfont = new FontInfo(id);
337 currentfont->font = font;
338 currentfont->max_size = 0;
339 GString* idStr = new GString(id);
340 id2font->add(idStr, (void*)currentfont);
344 state->setCTM(1.0,0,0,1.0,0,0);
345 splash->updateCTM(state, 0,0,0,0,0,0);
346 state->setTextMat(1.0,0,0,1.0,0,0);
347 state->setFont(font, 1024.0);
348 splash->doUpdateFont(state);
349 currentfont->splash_font = splash->getCurrentFont();
350 if(currentfont->splash_font) {
351 currentfont->ascender = currentfont->splash_font->ascender;
352 currentfont->descender = currentfont->splash_font->descender;
354 currentfont->ascender = currentfont->descender = 0;
360 void InfoOutputDev::fill(GfxState *state)
365 void InfoOutputDev::eoFill(GfxState *state)
370 FontInfo* InfoOutputDev::getFont(char*id)
372 return (FontInfo*)id2font->lookup(id);
375 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
376 double dx, double dy,
377 double originX, double originY,
378 CharCode code, int nBytes, Unicode *u, int uLen)
380 double m11,m21,m12,m22;
381 state->getFontTransMat(&m11, &m12, &m21, &m22);
382 m11 *= state->getHorizScaling();
383 m21 *= state->getHorizScaling();
384 double lenx = sqrt(m11*m11 + m12*m12);
385 double leny = sqrt(m21*m21 + m22*m22);
386 double len = lenx>leny?lenx:leny;
387 if(!currentfont || !currentfont->splash_font) {
390 if(currentfont && currentfont->max_size < len) {
391 currentfont->max_size = len;
396 currentfont->grow(code+1);
397 GlyphInfo*g = currentfont->glyphs[code];
399 g = currentfont->glyphs[code] = new GlyphInfo();
401 currentfont->splash_font->last_advance = -1;
402 g->path = currentfont->splash_font->getGlyphPath(code);
403 g->advance = currentfont->splash_font->last_advance;
406 if(uLen && ((u[0]>=32 && u[0]<g->unicode) || !g->unicode)) {
409 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
410 double xshift = x - currentfont->lastx;
411 if(xshift>=0 && xshift > g->advance_max) {
412 g->advance_max = xshift;
416 currentfont->lastx = x;
417 currentfont->lasty = y;
418 currentfont->lastchar = code;
421 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
423 GfxFont*font = state->getFont();
426 if(font->getType() != fontType3)
429 char*id = getFontID(font);
430 currentfont = (FontInfo*)id2font->lookup(id);
432 currentfont = new FontInfo(id);
433 currentfont->font = font;
434 GString* idStr = new GString(id);
435 id2font->add(idStr, (void*)currentfont);
438 currentfont = currentfont;
441 currentfont->grow(code+1);
442 if(!currentfont->glyphs[code]) {
443 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
444 currentglyph->unicode = uLen?u[0]:0;
445 currentglyph->path = new SplashPath();
456 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
464 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
466 currentglyph->x1=llx;
467 currentglyph->y1=lly;
468 currentglyph->x2=urx;
469 currentglyph->y2=ury;
472 void InfoOutputDev::endType3Char(GfxState *state)
474 double x1 = currentglyph->x1;
475 double y1 = currentglyph->y1;
476 double x2 = currentglyph->x2;
477 double y2 = currentglyph->y2;
478 currentglyph->path->moveTo(x1,y1);
479 currentglyph->path->lineTo(x2,y1);
480 currentglyph->path->lineTo(x2,y2);
481 currentglyph->path->lineTo(x1,y2);
482 currentglyph->path->close();
485 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
486 int width, int height, GBool invert,
489 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
491 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
493 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
494 int width, int height, GfxImageColorMap *colorMap,
495 int *maskColors, GBool inlineImg)
497 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
499 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
501 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
502 int width, int height,
503 GfxImageColorMap *colorMap,
505 int maskWidth, int maskHeight,
508 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
510 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
513 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
514 int width, int height,
515 GfxImageColorMap *colorMap,
517 int maskWidth, int maskHeight,
518 GfxImageColorMap *maskColorMap)
520 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
522 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
525 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
530 id2font->startIter(&i);
531 while(id2font->getNext(&i, &key, (void**)&font)) {
532 dev->addfont(dev, font->getGfxFont());