added experimental splashoutputdev code
[swftools.git] / lib / pdf / InfoOutputDev.cc
1 #include "SplashTypes.h"
2 #include "SplashOutputDev.h"
3 #include "InfoOutputDev.h"
4 #include "GfxState.h"
5 #include "../log.h"
6 #include <math.h>
7
8 InfoOutputDev::InfoOutputDev(XRef*xref) 
9 {
10     num_links = 0;
11     num_images = 0;
12     num_fonts = 0;
13     id2font = new GHash();
14     SplashColor white = {255,255,255};
15     splash = new SplashOutputDev(splashModeRGB8,320,0,white,0,0);
16     splash->startDoc(xref);
17 }
18 InfoOutputDev::~InfoOutputDev() 
19 {
20     delete id2font;
21 }
22 GBool InfoOutputDev::upsideDown() {return gTrue;}
23 GBool InfoOutputDev::useDrawChar() {return gTrue;}
24 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
25 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
26 {
27     double x1,y1,x2,y2;
28     state->transform(crop_x1,crop_y1,&x1,&y1);
29     state->transform(crop_x2,crop_y2,&x2,&y2);
30     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
31     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
32     this->x1 = (int)x1;
33     this->y1 = (int)y1;
34     this->x2 = (int)x2;
35     this->y2 = (int)y2;
36     msg("<verbose> Generating info structure for page %d", pageNum);
37 }
38 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
39 {
40     num_links++;
41 }
42 double InfoOutputDev::getMaximumFontSize(char*id)
43 {
44     FontInfo*info = (FontInfo*)id2font->lookup(id);
45     if(!info) {
46         msg("<error> Unknown font id: %s", id);
47         return 0.0;
48     }
49     return info->max_size;
50 }
51
52 static char*getFontID(GfxFont*font)
53 {
54     Ref*ref = font->getID();
55     GString*gstr = font->getName();
56     char* fname = gstr==0?0:gstr->getCString();
57     char buf[128];
58     if(fname==0) {
59         sprintf(buf, "font-%d-%d", ref->num, ref->gen);
60     } else {
61         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
62     }
63     return strdup(buf);
64 }
65
66 void InfoOutputDev::updateFont(GfxState *state) 
67 {
68     GfxFont*font = state->getFont();
69     if(!font)
70         return;
71     char*id = getFontID(font);
72     
73     FontInfo*info = (FontInfo*)id2font->lookup(id);
74     if(!info) {
75       GString* idStr = new GString(id);
76       info = new FontInfo;
77       info->font = font;
78       info->max_size = 0;
79       id2font->add(idStr, (void*)info);
80       free(id);
81       num_fonts++;
82     }
83     currentfont = info;
84
85     splash->doUpdateFont(state);
86     SplashFont* splash_font = splash->font;
87     //printf("%s: %d chars\n", id, splash_font->getNumChars());
88 }
89
90 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
91                       double dx, double dy,
92                       double originX, double originY,
93                       CharCode code, int nBytes, Unicode *u, int uLen)
94 {
95     int render = state->getRender();
96     if (render == 3)
97         return;
98     double m11,m21,m12,m22;
99     state->getFontTransMat(&m11, &m12, &m21, &m22);
100     m11 *= state->getHorizScaling();
101     m21 *= state->getHorizScaling();
102     double lenx = sqrt(m11*m11 + m12*m12);
103     double leny = sqrt(m21*m21 + m22*m22);
104     double len = lenx>leny?lenx:leny;
105     if(currentfont && currentfont->max_size < len) {
106         currentfont->max_size = len;
107     }
108 }
109 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
110                            int width, int height, GBool invert,
111                            GBool inlineImg) 
112 {
113     num_images++;
114     OutputDev::drawImageMask(state,ref,str,width,height,invert,inlineImg);
115 }
116 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
117                        int width, int height, GfxImageColorMap *colorMap,
118                        int *maskColors, GBool inlineImg)
119 {
120     num_images++;
121     OutputDev::drawImage(state,ref,str,width,height,colorMap,maskColors,inlineImg);
122 }
123 void InfoOutputDev::drawMaskedImage(GfxState *state, Object *ref, Stream *str,
124                                 int width, int height,
125                                 GfxImageColorMap *colorMap,
126                                 Stream *maskStr,
127                                 int maskWidth, int maskHeight,
128                                 GBool maskInvert) 
129 {
130     OutputDev::drawMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskInvert);
131 }
132
133 void InfoOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref, Stream *str,
134                                     int width, int height,
135                                     GfxImageColorMap *colorMap,
136                                     Stream *maskStr,
137                                     int maskWidth, int maskHeight,
138                                     GfxImageColorMap *maskColorMap) 
139 {
140     OutputDev::drawSoftMaskedImage(state,ref,str,width,height,colorMap,maskStr,maskWidth,maskHeight,maskColorMap);
141 }