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) {
122 /* now that we have found a space char, make sure it's unique */
124 for(s=0;s<font->num_glyphs;s++) {
125 if(s!=t && font->glyphs[s].unicode==32)
126 font->glyphs[s].unicode=0;
135 static int addSpace(gfxfont_t*font)
137 /* first, make sure the new space char is the only char that'll use unicode 32 */
139 for(t=0;t<font->num_glyphs;t++) {
140 if(font->glyphs[t].unicode==32)
141 font->glyphs[t].unicode=0;
144 font->glyphs = (gfxglyph_t*)realloc(font->glyphs, sizeof(gfxglyph_t)*font->num_glyphs);
145 gfxglyph_t*g = &font->glyphs[font->num_glyphs-1];
146 memset(g, 0, sizeof(*g));
148 g->advance = fabs(font->ascent + font->descent)*2 / 3;
149 if(font->max_unicode > 32)
150 font->unicode2glyph[32] = font->num_glyphs-1;
152 g->line = gfxline_makerectangle(0, -font->ascent, g->advance, font->descent);
154 return font->num_glyphs-1;
157 static gfxfont_t* createGfxFont(FontInfo*src)
159 gfxfont_t*font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
161 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
162 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
166 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
168 //printf("%d glyphs\n", font->num_glyphs);
169 font->num_glyphs = 0;
170 font->ascent = fabs(src->ascender);
171 font->descent = fabs(src->descender);
173 for(t=0;t<src->num_glyphs;t++) {
175 SplashPath*path = src->glyphs[t]->path;
176 int len = path?path->getLength():0;
177 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
178 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
179 src->glyphs[t]->glyphid = font->num_glyphs;
180 glyph->unicode = src->glyphs[t]->unicode;
182 gfxdrawer_target_gfxline(&drawer);
189 path->getPoint(s, &x, &y, &f);
192 if(f&splashPathFirst) {
193 drawer.moveTo(&drawer, x*scale, y*scale);
195 if(f&splashPathCurve) {
197 path->getPoint(++s, &x2, &y2, &f);
198 if(f&splashPathCurve) {
200 path->getPoint(++s, &x3, &y3, &f);
201 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
203 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
206 drawer.lineTo(&drawer, x*scale, y*scale);
208 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
209 // (f&splashPathFirst)?"first":"",
210 // (f&splashPathLast)?"last":"");
213 glyph->line = (gfxline_t*)drawer.result(&drawer);
214 if(src->glyphs[t]->advance>0) {
215 glyph->advance = src->glyphs[t]->advance;
217 glyph->advance = xmax*scale;
220 double max = src->glyphs[t]->advance_max;
221 if(max>0 && max > glyph->advance) {
222 glyph->advance = max;
230 int kerning_size = 0;
231 for(t=0;t<src->num_glyphs;t++) {
232 dict_t* d = src->kerning[t];
234 DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
240 font->kerning_size = kerning_size;
241 font->kerning = (gfxkerning_t*)malloc(sizeof(gfxkerning_t)*kerning_size);
243 for(t=0;t<src->num_glyphs;t++) {
244 dict_t* d = src->kerning[t];
246 DICT_ITERATE_ITEMS(d,void*,key,mtf_t*,m) {
248 font->kerning[pos].c1 = src->glyphs[t]->glyphid;
249 font->kerning[pos].c2 = src->glyphs[(int)(ptroff_t)key]->glyphid;
250 font->kerning[pos].advance = (int)(ptroff_t)m->first->key;
255 //int advance = (int)(ptroff_t)m->first->key;
260 static float find_average_glyph_advance(gfxfont_t*f)
265 float*values = (float*)malloc(sizeof(float)*f->num_glyphs);
267 for(t=0;t<f->num_glyphs;t++) {
268 values[t] = f->glyphs[t].advance;
270 float m = medianf(values, f->num_glyphs);
275 gfxfont_t* FontInfo::getGfxFont()
278 this->gfxfont = createGfxFont(this);
279 this->gfxfont->id = strdup(this->id);
280 this->space_char = findSpace(this->gfxfont);
281 this->average_advance = find_average_glyph_advance(this->gfxfont);
283 if(this->space_char>=0) {
284 msg("<debug> Font %s has space char %d (unicode=%d)",
285 this->id, this->space_char,
286 this->gfxfont->glyphs[this->space_char].unicode);
287 } else if(config_addspace) {
288 this->space_char = addSpace(this->gfxfont);
289 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);
291 gfxfont_fix_unicode(this->gfxfont);
293 return this->gfxfont;
296 GBool InfoOutputDev::upsideDown() {return gTrue;}
297 GBool InfoOutputDev::useDrawChar() {return gTrue;}
298 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
299 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
301 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
304 state->transform(crop_x1,crop_y1,&x1,&y1);
305 state->transform(crop_x2,crop_y2,&x2,&y2);
306 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
307 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
312 msg("<verbose> Generating info structure for page %d", pageNum);
314 void InfoOutputDev::endPage()
317 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
322 /* } else if(!strcmp(key,"fontquality")) {
323 this->config_fontquality = atof(value);
324 if(this->config_fontquality<=1)
325 this->config_fontquality=1;
326 } else if(!strcmp(key,"bigchar")) {
327 this->config_bigchar = atoi(value);
331 double InfoOutputDev::getMaximumFontSize(char*id)
333 FontInfo*info = (FontInfo*)id2font->lookup(id);
335 msg("<error> Unknown font id: %s", id);
338 return info->max_size;
341 char*getFontID(GfxFont*font)
343 Ref*ref = font->getID();
344 GString*gstr = font->getName();
345 char* fname = gstr==0?0:gstr->getCString();
348 if(font->getType() == fontType3) {
349 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
351 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
354 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
359 void InfoOutputDev::updateFont(GfxState *state)
361 GfxFont*font = state->getFont();
366 if(font->getType() == fontType3) {
370 char*id = getFontID(font);
373 currentfont->splash_font = 0;
375 currentfont = (FontInfo*)id2font->lookup(id);
377 currentfont = new FontInfo(id);
378 currentfont->font = font;
379 currentfont->max_size = 0;
380 GString* idStr = new GString(id);
381 id2font->add(idStr, (void*)currentfont);
385 state->setCTM(1.0,0,0,1.0,0,0);
386 splash->updateCTM(state, 0,0,0,0,0,0);
387 state->setTextMat(1.0,0,0,1.0,0,0);
388 state->setFont(font, 1024.0);
389 splash->doUpdateFont(state);
390 currentfont->splash_font = splash->getCurrentFont();
391 if(currentfont->splash_font) {
392 currentfont->ascender = currentfont->splash_font->ascender;
393 currentfont->descender = currentfont->splash_font->descender;
395 currentfont->ascender = currentfont->descender = 0;
401 void InfoOutputDev::fill(GfxState *state)
406 void InfoOutputDev::eoFill(GfxState *state)
411 FontInfo* InfoOutputDev::getFont(char*id)
413 return (FontInfo*)id2font->lookup(id);
416 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
417 double dx, double dy,
418 double originX, double originY,
419 CharCode code, int nBytes, Unicode *u, int uLen)
421 double m11,m21,m12,m22;
422 state->getFontTransMat(&m11, &m12, &m21, &m22);
423 m11 *= state->getHorizScaling();
424 m21 *= state->getHorizScaling();
425 double lenx = sqrt(m11*m11 + m12*m12);
426 double leny = sqrt(m21*m21 + m22*m22);
427 double len = lenx>leny?lenx:leny;
428 if(!currentfont || !currentfont->splash_font) {
431 if(currentfont && currentfont->max_size < len) {
432 currentfont->max_size = len;
437 currentfont->grow(code+1);
438 GlyphInfo*g = currentfont->glyphs[code];
440 g = currentfont->glyphs[code] = new GlyphInfo();
442 currentfont->splash_font->last_advance = -1;
443 g->path = currentfont->splash_font->getGlyphPath(code);
444 g->advance = currentfont->splash_font->last_advance;
447 if(uLen && ((u[0]>=32 && u[0]<g->unicode) || !g->unicode)) {
450 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
451 double xshift = (x - currentfont->lastx);
452 if(xshift>=0 && xshift > g->advance_max) {
453 g->advance_max = xshift;
455 int advance = (int)xshift;
456 if(advance>=0 && advance<g->advance*4 && advance!=currentfont->lastadvance) {
457 int c1 = currentfont->lastchar;
459 dict_t*d = currentfont->kerning[c1];
461 d = currentfont->kerning[c1] = dict_new2(&int_type);
463 mtf_t*k = (mtf_t*)dict_lookup(d, (void*)(ptroff_t)c2);
465 k = mtf_new(&int_type);
466 dict_put(d, (void*)(ptroff_t)c2, k);
468 mtf_increase(k, (void*)(ptroff_t)advance);
472 currentfont->lastx = x;
473 currentfont->lasty = y;
474 currentfont->lastchar = code;
475 currentfont->lastadvance = (int)(g->advance+0.5);
478 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
480 GfxFont*font = state->getFont();
483 if(font->getType() != fontType3)
486 char*id = getFontID(font);
487 currentfont = (FontInfo*)id2font->lookup(id);
489 currentfont = new FontInfo(id);
490 currentfont->font = font;
491 GString* idStr = new GString(id);
492 id2font->add(idStr, (void*)currentfont);
495 currentfont = currentfont;
498 currentfont->grow(code+1);
499 if(!currentfont->glyphs[code]) {
500 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
501 currentglyph->unicode = uLen?u[0]:0;
502 currentglyph->path = new SplashPath();
507 currentglyph->advance=dx;
514 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
522 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
524 if(-lly>currentfont->descender)
525 currentfont->descender = -lly;
526 if(ury>currentfont->ascender)
527 currentfont->ascender = ury;
529 currentglyph->x1=llx;
530 currentglyph->y1=lly;
531 currentglyph->x2=urx;
532 currentglyph->y2=ury;
535 void InfoOutputDev::endType3Char(GfxState *state)
537 double x1 = currentglyph->x1;
538 double y1 = currentglyph->y1;
539 double x2 = currentglyph->x2;
540 double y2 = currentglyph->y2;
541 currentglyph->path->moveTo(x1,y1);
542 currentglyph->path->lineTo(x2,y1);
543 currentglyph->path->lineTo(x2,y2);
544 currentglyph->path->lineTo(x1,y2);
545 currentglyph->path->close();
548 void InfoOutputDev::saveState(GfxState *state)
553 void InfoOutputDev::restoreState(GfxState *state)
558 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
559 int width, int height, GBool invert,
562 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
564 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
566 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
567 int width, int height, GfxImageColorMap *colorMap,
568 int *maskColors, GBool inlineImg)
570 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
572 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
574 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
575 int width, int height,
576 GfxImageColorMap *colorMap,
578 int maskWidth, int maskHeight,
581 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
583 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
586 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
587 int width, int height,
588 GfxImageColorMap *colorMap,
590 int maskWidth, int maskHeight,
591 GfxImageColorMap *maskColorMap)
593 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
595 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
598 void InfoOutputDev::dumpfonts(gfxdevice_t*dev)
603 id2font->startIter(&i);
604 while(id2font->getNext(&i, &key, (void**)&font)) {
605 dev->addfont(dev, font->getGfxFont());