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 InfoOutputDev::InfoOutputDev(XRef*xref)
30 id2font = new GHash(1);
31 SplashColor white = {255,255,255};
32 splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
33 splash->startDoc(xref);
35 InfoOutputDev::~InfoOutputDev()
38 id2font->startIter(&i);
41 while(id2font->getNext(&i, &key, (void**)&fontinfo)) {
44 id2font->killIter(&i);
46 delete id2font;id2font=0;
47 delete splash;splash=0;
49 void FontInfo::grow(int size)
51 if(size >= this->num_glyphs) {
52 this->glyphs = (GlyphInfo**)realloc(this->glyphs, sizeof(GlyphInfo*)*(size));
53 memset(&this->glyphs[this->num_glyphs], 0, sizeof(SplashPath*)*((size)-this->num_glyphs));
54 this->num_glyphs = size;
59 this->charid2glyph = 0;
63 this->splash_font = 0;
72 if(this->charid2glyph) {
73 free(this->charid2glyph);
74 this->charid2glyph = 0;
77 for(t=0;t<num_glyphs;t++) {
79 delete glyphs[t]->path;glyphs[t]->path = 0;
84 free(glyphs);glyphs=0;
86 GBool InfoOutputDev::upsideDown() {return gTrue;}
87 GBool InfoOutputDev::useDrawChar() {return gTrue;}
88 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
89 GBool InfoOutputDev::useTilingPatternFill() {return gTrue;}
91 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
94 state->transform(crop_x1,crop_y1,&x1,&y1);
95 state->transform(crop_x2,crop_y2,&x2,&y2);
96 if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
97 if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
102 msg("<verbose> Generating info structure for page %d", pageNum);
104 void InfoOutputDev::endPage()
107 void InfoOutputDev::drawLink(Link *link, Catalog *catalog)
112 /* there's not yet a way to set this */
113 int config_fontquality = 10;
114 int config_bigchar = 0;
116 /* } else if(!strcmp(key,"fontquality")) {
117 this->config_fontquality = atof(value);
118 if(this->config_fontquality<=1)
119 this->config_fontquality=1;
120 } else if(!strcmp(key,"bigchar")) {
121 this->config_bigchar = atoi(value);
125 gfxfont_t* InfoOutputDev::createGfxFont(GfxFont*xpdffont, FontInfo*src)
127 gfxfont_t*font = (gfxfont_t*)malloc(sizeof(gfxfont_t));
128 memset(font, 0, sizeof(gfxfont_t));
130 font->glyphs = (gfxglyph_t*)malloc(sizeof(gfxglyph_t)*src->num_glyphs);
131 memset(font->glyphs, 0, sizeof(gfxglyph_t)*src->num_glyphs);
135 double quality = (INTERNAL_FONT_SIZE * 200 / config_fontquality) / src->max_size;
137 //printf("%d glyphs\n", font->num_glyphs);
138 font->num_glyphs = 0;
139 font->ascent = fabs(src->descender);
140 font->descent = fabs(src->ascender);
142 for(t=0;t<src->num_glyphs;t++) {
144 SplashPath*path = src->glyphs[t]->path;
145 int len = path?path->getLength():0;
146 //printf("glyph %d) %08x (%d line segments)\n", t, path, len);
147 gfxglyph_t*glyph = &font->glyphs[font->num_glyphs];
148 src->glyphs[t]->glyphid = font->num_glyphs;
149 glyph->unicode = src->glyphs[t]->unicode;
150 if(glyph->unicode >= font->max_unicode)
151 font->max_unicode = glyph->unicode+1;
153 gfxdrawer_target_gfxline(&drawer);
160 path->getPoint(s, &x, &y, &f);
163 if(f&splashPathFirst) {
164 drawer.moveTo(&drawer, x*scale, y*scale);
166 if(f&splashPathCurve) {
168 path->getPoint(++s, &x2, &y2, &f);
169 if(f&splashPathCurve) {
171 path->getPoint(++s, &x3, &y3, &f);
172 gfxdraw_cubicTo(&drawer, x*scale, y*scale, x2*scale, y2*scale, x3*scale, y3*scale, quality);
174 drawer.splineTo(&drawer, x*scale, y*scale, x2*scale, y2*scale);
177 drawer.lineTo(&drawer, x*scale, y*scale);
179 // printf("%f %f %s %s\n", x, y, (f&splashPathCurve)?"curve":"",
180 // (f&splashPathFirst)?"first":"",
181 // (f&splashPathLast)?"last":"");
184 glyph->line = (gfxline_t*)drawer.result(&drawer);
185 if(src->glyphs[t]->advance>0) {
186 glyph->advance = src->glyphs[t]->advance;
188 msg("<warning> Approximating advance value for glyph %d", t);
189 glyph->advance = xmax*scale;
192 double max = src->glyphs[t]->advance_max;
193 if(max>0 && max > glyph->advance) {
194 glyph->advance = max;
201 font->unicode2glyph = (int*)malloc(sizeof(int)*font->max_unicode);
202 memset(font->unicode2glyph, -1, sizeof(int)*font->max_unicode);
203 for(t=0;t<font->num_glyphs;t++) {
204 if(font->glyphs[t].unicode>0 && font->glyphs[t].unicode<font->max_unicode) {
205 font->unicode2glyph[font->glyphs[t].unicode] = t;
209 msg("<trace> %d glyphs.", t, font->num_glyphs);
213 double InfoOutputDev::getMaximumFontSize(char*id)
215 FontInfo*info = (FontInfo*)id2font->lookup(id);
217 msg("<error> Unknown font id: %s", id);
220 return info->max_size;
223 char*getFontID(GfxFont*font)
225 Ref*ref = font->getID();
226 GString*gstr = font->getName();
227 char* fname = gstr==0?0:gstr->getCString();
230 if(font->getType() == fontType3) {
231 sprintf(buf, "t3font-%d-%d", ref->num, ref->gen);
233 sprintf(buf, "font-%d-%d", ref->num, ref->gen);
236 sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
241 void InfoOutputDev::updateFont(GfxState *state)
243 GfxFont*font = state->getFont();
248 if(font->getType() == fontType3) {
252 char*id = getFontID(font);
255 currentfont->splash_font = 0;
257 currentfont = (FontInfo*)id2font->lookup(id);
259 currentfont = new FontInfo;
260 currentfont->font = font;
261 currentfont->max_size = 0;
262 GString* idStr = new GString(id);
263 id2font->add(idStr, (void*)currentfont);
267 state->setCTM(1.0,0,0,1.0,0,0);
268 splash->updateCTM(state, 0,0,0,0,0,0);
269 state->setTextMat(1.0,0,0,1.0,0,0);
270 state->setFont(font, 1024.0);
271 splash->doUpdateFont(state);
272 currentfont->splash_font = splash->getCurrentFont();
273 if(currentfont->splash_font) {
274 currentfont->ascender = currentfont->splash_font->ascender;
275 currentfont->descender = currentfont->splash_font->descender;
277 currentfont->ascender = currentfont->descender = 0;
280 currentfont->gfxfont = this->createGfxFont(font, currentfont);
281 currentfont->gfxfont->id = strdup(id);
282 fonts = gfxfontlist_addfont(fonts, currentfont->gfxfont);
287 void InfoOutputDev::fill(GfxState *state)
292 void InfoOutputDev::eoFill(GfxState *state)
297 FontInfo* InfoOutputDev::getFont(char*id)
299 return (FontInfo*)id2font->lookup(id);
302 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
303 double dx, double dy,
304 double originX, double originY,
305 CharCode code, int nBytes, Unicode *u, int uLen)
307 double m11,m21,m12,m22;
308 state->getFontTransMat(&m11, &m12, &m21, &m22);
309 m11 *= state->getHorizScaling();
310 m21 *= state->getHorizScaling();
311 double lenx = sqrt(m11*m11 + m12*m12);
312 double leny = sqrt(m21*m21 + m22*m22);
313 double len = lenx>leny?lenx:leny;
314 if(!currentfont || !currentfont->splash_font) {
317 if(currentfont && currentfont->max_size < len) {
318 currentfont->max_size = len;
320 currentfont->grow(code+1);
321 GlyphInfo*g = currentfont->glyphs[code];
323 g = currentfont->glyphs[code] = new GlyphInfo();
325 currentfont->splash_font->last_advance = -1;
326 g->path = currentfont->splash_font->getGlyphPath(code);
327 g->advance = currentfont->splash_font->last_advance;
330 if(uLen && (u[0]>=32 && u[0]<g->unicode || !g->unicode)) {
333 if(currentfont->lastchar>=0 && currentfont->lasty == y) {
334 double xshift = x - currentfont->lastx;
335 if(xshift>=0 && xshift > g->advance_max) {
336 g->advance_max = xshift;
340 currentfont->lastx = x;
341 currentfont->lasty = y;
342 currentfont->lastchar = code;
345 GBool InfoOutputDev::beginType3Char(GfxState *state, double x, double y, double dx, double dy, CharCode code, Unicode *u, int uLen)
347 GfxFont*font = state->getFont();
350 if(font->getType() != fontType3)
353 char*id = getFontID(font);
354 currentfont = (FontInfo*)id2font->lookup(id);
356 currentfont = new FontInfo;
357 currentfont->font = font;
358 GString* idStr = new GString(id);
359 id2font->add(idStr, (void*)currentfont);
362 currentfont = currentfont;
365 currentfont->grow(code+1);
366 if(!currentfont->glyphs[code]) {
367 currentglyph = currentfont->glyphs[code] = new GlyphInfo();
368 currentglyph->unicode = uLen?u[0]:0;
369 currentglyph->path = new SplashPath();
380 void InfoOutputDev::type3D0(GfxState *state, double wx, double wy)
388 void InfoOutputDev::type3D1(GfxState *state, double wx, double wy, double llx, double lly, double urx, double ury)
390 currentglyph->x1=llx;
391 currentglyph->y1=lly;
392 currentglyph->x2=urx;
393 currentglyph->y2=ury;
396 void InfoOutputDev::endType3Char(GfxState *state)
398 double x1 = currentglyph->x1;
399 double y1 = currentglyph->y1;
400 double x2 = currentglyph->x2;
401 double y2 = currentglyph->y2;
402 currentglyph->path->moveTo(x1,y1);
403 currentglyph->path->lineTo(x2,y1);
404 currentglyph->path->lineTo(x2,y2);
405 currentglyph->path->lineTo(x1,y2);
406 currentglyph->path->close();
409 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
410 int width, int height, GBool invert,
413 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
415 OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
417 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
418 int width, int height, GfxImageColorMap *colorMap,
419 int *maskColors, GBool inlineImg)
421 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
423 OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
425 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
426 int width, int height,
427 GfxImageColorMap *colorMap,
429 int maskWidth, int maskHeight,
432 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
434 OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
437 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
438 int width, int height,
439 GfxImageColorMap *colorMap,
441 int maskWidth, int maskHeight,
442 GfxImageColorMap *maskColorMap)
444 if(str->getKind()==strDCT) num_jpeg_images++; else num_ppm_images++;
446 OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);