moved from ../xpdf/
[swftools.git] / lib / pdf / InfoOutputDev.cc
1 #include "InfoOutputDev.h"
2 #include "GfxState.h"
3 #include "../log.h"
4 #include <math.h>
5
6 InfoOutputDev::InfoOutputDev() 
7 {
8     num_links = 0;
9     num_images = 0;
10     num_fonts = 0;
11     id2font = new GHash();
12 }
13 InfoOutputDev::~InfoOutputDev() 
14 {
15     delete id2font;
16 }
17 GBool InfoOutputDev::upsideDown() {return gTrue;}
18 GBool InfoOutputDev::useDrawChar() {return gTrue;}
19 GBool InfoOutputDev::interpretType3Chars() {return gTrue;}
20 void InfoOutputDev::startPage(int pageNum, GfxState *state, double crop_x1, double crop_y1, double crop_x2, double crop_y2)
21 {
22     double x1,y1,x2,y2;
23     state->transform(crop_x1,crop_y1,&x1,&y1);
24     state->transform(crop_x2,crop_y2,&x2,&y2);
25     if(x2<x1) {double x3=x1;x1=x2;x2=x3;}
26     if(y2<y1) {double y3=y1;y1=y2;y2=y3;}
27     this->x1 = (int)x1;
28     this->y1 = (int)y1;
29     this->x2 = (int)x2;
30     this->y2 = (int)y2;
31 }
32 void InfoOutputDev::drawLink(Link *link, Catalog *catalog) 
33 {
34     num_links++;
35 }
36 double InfoOutputDev::getMaximumFontSize(char*id)
37 {
38     FontInfo*info = (FontInfo*)id2font->lookup(id);
39     if(!info) {
40         msg("<error> Unknown font id: %s", id);
41         return 0.0;
42     }
43     return info->max_size;
44 }
45
46 static char*getFontID(GfxFont*font)
47 {
48     Ref*ref = font->getID();
49     GString*gstr = font->getName();
50     char* fname = gstr==0?0:gstr->getCString();
51     char buf[128];
52     if(fname==0) {
53         sprintf(buf, "font-%d-%d", ref->num, ref->gen);
54     } else {
55         sprintf(buf, "%s-%d-%d", fname, ref->num, ref->gen);
56     }
57     return strdup(buf);
58 }
59
60
61 void InfoOutputDev::updateFont(GfxState *state) 
62 {
63     GfxFont*font = state->getFont();
64     if(!font)
65         return;
66     char*id = getFontID(font);
67     
68     FontInfo*info = (FontInfo*)id2font->lookup(id);
69     if(!info) {
70       GString* idStr = new GString(id);
71       info = new FontInfo;
72       info->font = font;
73       info->max_size = 0;
74       id2font->add(idStr, (void*)info);
75       free(id);
76       num_fonts++;
77     }
78     currentfont = info;
79 }
80
81 void InfoOutputDev::drawChar(GfxState *state, double x, double y,
82                       double dx, double dy,
83                       double originX, double originY,
84                       CharCode code, int nBytes, Unicode *u, int uLen)
85 {
86     int render = state->getRender();
87     if (render == 3)
88         return;
89     double m11,m21,m12,m22;
90     state->getFontTransMat(&m11, &m12, &m21, &m22);
91     m11 *= state->getHorizScaling();
92     m21 *= state->getHorizScaling();
93     double lenx = sqrt(m11*m11 + m12*m12);
94     double leny = sqrt(m21*m21 + m22*m22);
95     double len = lenx>leny?lenx:leny;
96     if(currentfont && currentfont->max_size < len) {
97         currentfont->max_size = len;
98     }
99 }
100 void InfoOutputDev::drawImageMask(GfxState *state, Object *ref, Stream *str,
101                            int width, int height, GBool invert,
102                            GBool inlineImg) 
103 {
104     num_images++;
105 }
106 void InfoOutputDev::drawImage(GfxState *state, Object *ref, Stream *str,
107                        int width, int height, GfxImageColorMap *colorMap,
108                        int *maskColors, GBool inlineImg)
109 {
110     num_images++;
111 }
112