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 #include "../gfxfont.h"
24 int config_addspace = 1;
25 int config_fontquality = 10;
26 int config_bigchar = 0;
28 InfoOutputDev::InfoOutputDev(XRef*xref)
38 id2font = new GHash(1);
39 SplashColor white = {255,255,255};
40 splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
41 splash->startDoc(xref);
43 InfoOutputDev::~InfoOutputDev()
46 id2font->startIter(&i);
49 while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
52 id2font->killIter(&i);
54 delete id2font;id2font=0;
55 delete splash;splash=0;
58 void FontInfo::grow(int size)
60 if(size >= this->num_glyphs) {
61 this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
62 this->kerning = (dict_t**)realloc(this->kerning, sizeof(dict_t*)*(size));
63 memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
64 memset(&this->kerning[this->num_glyphs], 0, sizeof(dict_t*)*((size)-this->num_glyphs));
65 this->num_glyphs = size;
68 FontInfo::FontInfo(char*id)
70 this->id = strdup(id);
75 this->splash_font = 0;
80 this->space_char = -1;
86 if(this->id) {free(this->id);this->id=0;}
89 for(t=0;t<num_glyphs;t++) {
91 delete glyphs[t]->path;glyphs[t]->path = 0;
96 free(glyphs);glyphs=0;
98 gfxfont_free(this->gfxfont);
101 for(t=0;t<num_glyphs;t++) {
102 dict_t* d = kerning[t];
104 DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
114 static int findSpace(gfxfont_t*font)
116 int first_space = -1;
118 for(t=0;t<font->num_glyphs;t++) {
119 gfxglyph_t*g = &font->glyphs[t];
120 if(GLYPH_IS_SPACE(g)) {
121 if(g->unicode == 32) return t;
127 static int addSpace(gfxfont_t*font)
129 /* first, make sure the new space char is the only char that'll use unicode 32 */
131 for(t=0;t<font->num_glyphs;t++) {
132 if(font->glyphs[t].unicode==32)
133 font->glyphs[t].unicode=0;
136 font->glyphs = (gfxglyph_t*)realloc(font->glyphs, sizeof(gfxglyph_t)*font->num_glyphs);
137 gfxglyph_t*g = &font->glyphs[font->num_glyphs-1];
138 memset(g, 0, sizeof(*g));
140 g->advance = fabs(font->ascent + font->descent)*2 / 3;
141 if(font->max_unicode > 32)
142 font->unicode2glyph[32] = font->num_glyphs-1;
144 g->line = gfxline_makerectangle(0, -font->ascent, g->advance, font->descent);
146 return font->num_glyphs-1;
149 static gfxfont_t* createGfxFont(FontInfo*src)
151 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
152 memset(font, 0, sizeof(gfxfont_t));
154 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
155 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
159 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
161 //printf("%d glyphs\n", font->num_glyphs);
162 font->num_glyphs = 0;
163 font->ascent = fabs(src->ascender);
164 font->descent = fabs(src->descender);
166 for(t=0;t<src->num_glyphs;t++) {
168 SplashPath*path = src->glyphs[t]->path;
169 int len = path?path->getLength():0;
170 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
171 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
172 src->glyphs[t]->glyphid = font->num_glyphs;
173 glyph->unicode = src->glyphs[t]->unicode;
175 gfxdrawer_target_gfxline(&drawer);
182 path->getPoint(s, &x, &y, &f);
185 if(f&splashPathFirst) {
186 drawer.moveTo(&drawer, x*scale, y*scale);
188 if(f&splashPathCurve) {
190 path->getPoint(++s, &x2, &y2, &f);
191 if(f&splashPathCurve) {
193 path->getPoint(++s, &x3, &y3, &f);
194 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
196 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
199 drawer.lineTo(&drawer, x*scale, y*scale);
201 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
202 // (f&splashPathFirst)?"first":"",
203 // (f&splashPathLast)?"last":"");
206 glyph->line = (gfxline_t*)drawer.result(&drawer);
207 if(src->glyphs[t]->advance>0) {
208 glyph->advance = src->glyphs[t]->advance;
210 glyph->advance = xmax*scale;
213 double max = src->glyphs[t]->advance_max;
214 if(max>0 && max > glyph->advance) {
215 glyph->advance = max;
223 int kerning_size = 0;
224 for(t=0;t<src->num_glyphs;t++) {
225 dict_t* d = src->kerning[t];
227 DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
233 font->kerning_size = kerning_size;
234 font->kerning = (gfxkerning_t*)malloc(sizeof(gfxkerning_t)*kerning_size);
236 for(t=0;t<src->num_glyphs;t++) {
237 dict_t* d = src->kerning[t];
239 DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
241 font->kerning[pos].c1 = src->glyphs[t]->glyphid;
242 font->kerning[pos].c2 = src->glyphs[(int)(ptroff_t)key]->glyphid;
243 font->kerning[pos].advance = (int)(ptroff_t)m->first->key;
248 //int advance = (int)(ptroff_t)m->first->key;
253 static float find_average_glyph_advance(gfxfont_t*f)
258 float*values = (float*)malloc(sizeof(float)*f->num_glyphs);
260 for(t=0;t<f->num_glyphs;t++) {
261 values[t] = f->glyphs[t].advance;
263 float m = medianf(values, f->num_glyphs);
268 gfxfont_t* FontInfo::getGfxFont()
271 this->gfxfont = createGfxFont(this);
272 this->gfxfont->id = strdup(this->id);
273 this->space_char = findSpace(this->gfxfont);
274 this->average_advance = find_average_glyph_advance(this->gfxfont);
276 if(this->space_char>=0) {
277 msg("<debug> Font %s has space char %d (unicode=%d)",
278 this->id, this->space_char,
279 this->gfxfont->glyphs[this->space_char].unicode);
280 } else if(config_addspace) {
281 this->space_char = addSpace(this->gfxfont);
282 msg("<debug> Appending space char to font %s, position %d, width %f", this->gfxfont->id, this->space_char, this->gfxfont->glyphs[this->space_char].advance);
284 gfxfont_fix_unicode(this->gfxfont);
286 return this->gfxfont;
289 GBool InfoOutputDev::upsideDown() {return gTrue;}
290 GBool InfoOutputDev::useDrawChar() {return gTrue;}
291 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
292 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
294 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
297 state->transform(crop_x1,crop_y1,&x1,&y1);
298 state->transform(crop_x2,crop_y2,&x2,&y2);
299 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
300 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
305 msg("<verbose> Generating info structure for page %d", pageNum);
307 void InfoOutputDev::endPage()
310 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
315 /* } else if(!strcmp(key,"fontquality")) {
316 this->config_fontquality = atof(value);
317 if(this->config_fontquality<=1)
318 this->config_fontquality=1;
319 } else if(!strcmp(key,"bigchar")) {
320 this->config_bigchar = atoi(value);
324 double InfoOutputDev::getMaximumFontSize(char*id)
326 FontInfo*info = (FontInfo*)id2font->lookup(id);
328 msg("<error> Unknown font id: %s", id);
331 return info->max_size;
334 char*getFontID(GfxFont*font)
336 Ref*ref = font->getID();
337 GString*gstr = font->getName();
338 char* fname = gstr==0?0:gstr->getCString();
341 if(font->getType() == fontType3) {
342 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
344 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
347 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
352 void InfoOutputDev::updateFont(GfxState *state)
354 GfxFont*font = state->getFont();
359 if(font->getType() == fontType3) {
363 char*id = getFontID(font);
366 currentfont->splash_font = 0;
368 currentfont = (FontInfo*)id2font->lookup(id);
370 currentfont = new FontInfo(id);
371 currentfont->font = font;
372 currentfont->max_size = 0;
373 GString* idStr = new GString(id);
374 id2font->add(idStr, (void*)currentfont);
378 state->setCTM(1.0,0,0,1.0,0,0);
379 splash->updateCTM(state, 0,0,0,0,0,0);
380 state->setTextMat(1.0,0,0,1.0,0,0);
381 state->setFont(font, 1024.0);
382 splash->doUpdateFont(state);
383 currentfont->splash_font = splash->getCurrentFont();
384 if(currentfont->splash_font) {
385 currentfont->ascender = currentfont->splash_font->ascender;
386 currentfont->descender = currentfont->splash_font->descender;
388 currentfont->ascender = currentfont->descender = 0;
394 void InfoOutputDev::fill(GfxState *state)
399 void InfoOutputDev::eoFill(GfxState *state)
404 FontInfo* InfoOutputDev::getFont(char*id)
406 return (FontInfo*)id2font->lookup(id);
409 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
410 double dx, double dy,
411 double originX, double originY,
412 CharCode code, int nBytes, Unicode *u, int uLen)
414 double m11,m21,m12,m22;
415 state->getFontTransMat(&m11, &m12, &m21, &m22);
416 m11 *= state->getHorizScaling();
417 m21 *= state->getHorizScaling();
418 double lenx = sqrt(m11*m11 + m12*m12);
419 double leny = sqrt(m21*m21 + m22*m22);
420 double len = lenx>leny?lenx:leny;
421 if(!currentfont || !currentfont->splash_font) {
424 if(currentfont && currentfont->max_size < len) {
425 currentfont->max_size = len;
430 currentfont->grow(code+1);
431 GlyphInfo*g = currentfont->glyphs[code];
433 g = currentfont->glyphs[code] = new GlyphInfo();
435 currentfont->splash_font->last_advance = -1;
436 g->path = currentfont->splash_font->getGlyphPath(code);
437 g->advance = currentfont->splash_font->last_advance;
440 if(uLen && ((u[0]>=32 && u[0]<g->unicode) || !g->unicode)) {
443 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
444 double xshift = (x - currentfont->lastx);
445 if(xshift>=0 && xshift > g->advance_max) {
446 g->advance_max = xshift;
448 int advance = (int)xshift;
449 if(advance>=0 && advance<g->advance*4 && advance!=currentfont->lastadvance) {
450 int c1 = currentfont->lastchar;
452 dict_t*d = currentfont->kerning[c1];
454 d = currentfont->kerning[c1] = dict_new2(&int_type);
456 mtf_t*k = (mtf_t*)dict_lookup(d, (void*)(ptroff_t)c2);
458 k = mtf_new(&int_type);
459 dict_put(d, (void*)(ptroff_t)c2, k);
461 mtf_increase(k, (void*)(ptroff_t)advance);
465 currentfont->lastx = x;
466 currentfont->lasty = y;
467 currentfont->lastchar = code;
468 currentfont->lastadvance = (int)(g->advance+0.5);
471 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
473 GfxFont*font = state->getFont();
476 if(font->getType() != fontType3)
479 char*id = getFontID(font);
480 currentfont = (FontInfo*)id2font->lookup(id);
482 currentfont = new FontInfo(id);
483 currentfont->font = font;
484 GString* idStr = new GString(id);
485 id2font->add(idStr, (void*)currentfont);
488 currentfont = currentfont;
491 currentfont->grow(code+1);
492 if(!currentfont->glyphs[code]) {
493 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
494 currentglyph->unicode = uLen?u[0]:0;
495 currentglyph->path = new SplashPath();
500 currentglyph->advance=dx;
507 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
515 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
517 if(-lly>currentfont->descender)
518 currentfont->descender = -lly;
519 if(ury>currentfont->ascender)
520 currentfont->ascender = ury;
522 currentglyph->x1=llx;
523 currentglyph->y1=lly;
524 currentglyph->x2=urx;
525 currentglyph->y2=ury;
528 void InfoOutputDev::endType3Char(GfxState *state)
530 double x1 = currentglyph->x1;
531 double y1 = currentglyph->y1;
532 double x2 = currentglyph->x2;
533 double y2 = currentglyph->y2;
534 currentglyph->path->moveTo(x1,y1);
535 currentglyph->path->lineTo(x2,y1);
536 currentglyph->path->lineTo(x2,y2);
537 currentglyph->path->lineTo(x1,y2);
538 currentglyph->path->close();
541 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
542 int width, int height, GBool invert,
545 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
547 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
549 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
550 int width, int height, GfxImageColorMap *colorMap,
551 int *maskColors, GBool inlineImg)
553 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
555 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
557 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
558 int width, int height,
559 GfxImageColorMap *colorMap,
561 int maskWidth, int maskHeight,
564 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
566 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
569 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
570 int width, int height,
571 GfxImageColorMap *colorMap,
573 int maskWidth, int maskHeight,
574 GfxImageColorMap *maskColorMap)
576 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
578 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
581 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
586 id2font->startIter(&i);
587 while(id2font->getNext(&i, &key, (void**)&font)) {
588 dev->addfont(dev, font->getGfxFont());