~ubuntu-branches/ubuntu/dapper/poppler/dapper-security

« back to all changes in this revision

Viewing changes to utils/HtmlFonts.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2005-12-30 11:34:07 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051230113407-8grizsk0ar874uoi
Tags: 0.4.3-1
* New upstream release.
* New maintainer (Closes: #344738)
* CVE-2005-3191 and CAN-2005-2097 fixes merged upstream.
* Fixed some rendering bugs and disabled Cairo output
  (Closes: #314556, #322964, #328211)
* Acknowledge NMU (Closes: #342288)
* Add 001-selection-crash-bug.patch (Closes: #330544)
* Add poppler-utils (merge patch from Ubuntu)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "HtmlFonts.h"
2
 
#include "GlobalParams.h"
3
 
#include "UnicodeMap.h"
4
 
#include <stdio.h>
5
 
 
6
 
 struct Fonts{
7
 
    char *Fontname;
8
 
    char *name;
9
 
  };
10
 
 
11
 
const int font_num=13;
12
 
 
13
 
static Fonts fonts[font_num+1]={  
14
 
     {"Courier",               "Courier" },
15
 
     {"Courier-Bold",           "Courier"},
16
 
     {"Courier-BoldOblique",    "Courier"},
17
 
     {"Courier-Oblique",        "Courier"},
18
 
     {"Helvetica",              "Helvetica"},
19
 
     {"Helvetica-Bold",         "Helvetica"},
20
 
     {"Helvetica-BoldOblique",  "Helvetica"},
21
 
     {"Helvetica-Oblique",      "Helvetica"},
22
 
     {"Symbol",                 "Symbol"   },
23
 
     {"Times-Bold",             "Times"    },
24
 
     {"Times-BoldItalic",       "Times"    },
25
 
     {"Times-Italic",           "Times"    },
26
 
     {"Times-Roman",            "Times"    },
27
 
     {" "          ,            "Times"    },
28
 
};
29
 
 
30
 
#define xoutRound(x) ((int)(x + 0.5))
31
 
extern GBool xml;
32
 
 
33
 
GooString* HtmlFont::DefaultFont=new GooString("Times"); // Arial,Helvetica,sans-serif
34
 
 
35
 
HtmlFontColor::HtmlFontColor(GfxRGB rgb){
36
 
  r=static_cast<int>(255*rgb.r);
37
 
  g=static_cast<int>(255*rgb.g);
38
 
  b=static_cast<int>(255*rgb.b);
39
 
  if (!(Ok(r)&&Ok(b)&&Ok(g))) {printf("Error : Bad color \n");r=0;g=0;b=0;}
40
 
}
41
 
 
42
 
GooString *HtmlFontColor::convtoX(unsigned int xcol) const{
43
 
  GooString *xret=new GooString();
44
 
  char tmp;
45
 
  unsigned  int k;
46
 
  k = (xcol/16);
47
 
  if ((k>=0)&&(k<10)) tmp=(char) ('0'+k); else tmp=(char)('a'+k-10);
48
 
  xret->append(tmp);
49
 
  k = (xcol%16);
50
 
  if ((k>=0)&&(k<10)) tmp=(char) ('0'+k); else tmp=(char)('a'+k-10);
51
 
  xret->append(tmp);
52
 
 return xret;
53
 
}
54
 
 
55
 
GooString *HtmlFontColor::toString() const{
56
 
  GooString *tmp=new GooString("#");
57
 
  GooString *tmpr=convtoX(r); 
58
 
  GooString *tmpg=convtoX(g);
59
 
  GooString *tmpb=convtoX(b);
60
 
  tmp->append(tmpr);
61
 
  tmp->append(tmpg);
62
 
  tmp->append(tmpb);
63
 
  delete tmpr;
64
 
  delete tmpg;
65
 
  delete tmpb;
66
 
  return tmp;
67
 
68
 
 
69
 
HtmlFont::HtmlFont(GooString* ftname,int _size, GfxRGB rgb){
70
 
  //if (col) color=HtmlFontColor(col); 
71
 
  //else color=HtmlFontColor();
72
 
  color=HtmlFontColor(rgb);
73
 
 
74
 
  GooString *fontname = NULL;
75
 
 
76
 
  if( ftname ){
77
 
    fontname = new GooString(ftname);
78
 
    FontName=new GooString(ftname);
79
 
  }
80
 
  else {
81
 
    fontname = NULL;
82
 
    FontName = NULL;
83
 
  }
84
 
  
85
 
  lineSize = -1;
86
 
 
87
 
  size=(_size-1);
88
 
  italic = gFalse;
89
 
  bold = gFalse;
90
 
 
91
 
  if (fontname){
92
 
    if (strstr(fontname->lowerCase()->getCString(),"bold"))  bold=gTrue;
93
 
    
94
 
    if (strstr(fontname->lowerCase()->getCString(),"italic")||
95
 
        strstr(fontname->lowerCase()->getCString(),"oblique"))  italic=gTrue;
96
 
    
97
 
    int i=0;
98
 
    while (strcmp(ftname->getCString(),fonts[i].Fontname)&&(i<font_num)) 
99
 
        {
100
 
                i++;
101
 
        }
102
 
    pos=i;
103
 
    delete fontname;
104
 
  }  
105
 
  if (!DefaultFont) DefaultFont=new GooString(fonts[font_num].name);
106
 
 
107
 
}
108
 
 
109
 
HtmlFont::HtmlFont(const HtmlFont& x){
110
 
   size=x.size;
111
 
   lineSize=x.lineSize;
112
 
   italic=x.italic;
113
 
   bold=x.bold;
114
 
   pos=x.pos;
115
 
   color=x.color;
116
 
   if (x.FontName) FontName=new GooString(x.FontName);
117
 
 }
118
 
 
119
 
 
120
 
HtmlFont::~HtmlFont(){
121
 
  if (FontName) delete FontName;
122
 
}
123
 
 
124
 
HtmlFont& HtmlFont::operator=(const HtmlFont& x){
125
 
   if (this==&x) return *this; 
126
 
   size=x.size;
127
 
   lineSize=x.lineSize;
128
 
   italic=x.italic;
129
 
   bold=x.bold;
130
 
   pos=x.pos;
131
 
   color=x.color;
132
 
   if (FontName) delete FontName;
133
 
   if (x.FontName) FontName=new GooString(x.FontName);
134
 
   return *this;
135
 
}
136
 
 
137
 
void HtmlFont::clear(){
138
 
  if(DefaultFont) delete DefaultFont;
139
 
  DefaultFont = NULL;
140
 
}
141
 
 
142
 
 
143
 
 
144
 
/*
145
 
  This function is used to compare font uniquily for insertion into
146
 
  the list of all encountered fonts
147
 
*/
148
 
GBool HtmlFont::isEqual(const HtmlFont& x) const{
149
 
  return ((size==x.size) &&
150
 
          (lineSize==x.lineSize) &&
151
 
          (pos==x.pos) && (bold==x.bold) && (italic==x.italic) &&
152
 
          (color.isEqual(x.getColor())));
153
 
}
154
 
 
155
 
/*
156
 
  This one is used to decide whether two pieces of text can be joined together
157
 
  and therefore we don't care about bold/italics properties
158
 
*/
159
 
GBool HtmlFont::isEqualIgnoreBold(const HtmlFont& x) const{
160
 
  return ((size==x.size) &&
161
 
          (!strcmp(fonts[pos].name, fonts[x.pos].name)) &&
162
 
          (color.isEqual(x.getColor())));
163
 
}
164
 
 
165
 
GooString* HtmlFont::getFontName(){
166
 
   if (pos!=font_num) return new GooString(fonts[pos].name);
167
 
    else return new GooString(DefaultFont);
168
 
}
169
 
 
170
 
GooString* HtmlFont::getFullName(){
171
 
  if (FontName)
172
 
    return new GooString(FontName);
173
 
  else return new GooString(DefaultFont);
174
 
175
 
 
176
 
void HtmlFont::setDefaultFont(GooString* defaultFont){
177
 
  if (DefaultFont) delete DefaultFont;
178
 
  DefaultFont=new GooString(defaultFont);
179
 
}
180
 
 
181
 
 
182
 
GooString* HtmlFont::getDefaultFont(){
183
 
  return DefaultFont;
184
 
}
185
 
 
186
 
// this method if plain wrong todo
187
 
GooString* HtmlFont::HtmlFilter(Unicode* u, int uLen) {
188
 
  GooString *tmp = new GooString();
189
 
  UnicodeMap *uMap;
190
 
  char buf[8];
191
 
  int n;
192
 
 
193
 
  // get the output encoding
194
 
  if (!(uMap = globalParams->getTextEncoding())) {
195
 
    return tmp;
196
 
  }
197
 
 
198
 
  for (int i = 0; i < uLen; ++i) {
199
 
    switch (u[i])
200
 
      { 
201
 
        case '"': tmp->append("&quot;");  break;
202
 
        case '&': tmp->append("&amp;");  break;
203
 
        case '<': tmp->append("&lt;");  break;
204
 
        case '>': tmp->append("&gt;");  break;
205
 
        default:  
206
 
          {
207
 
            // convert unicode to string
208
 
            if ((n = uMap->mapUnicode(u[i], buf, sizeof(buf))) > 0) {
209
 
              tmp->append(buf, n); 
210
 
          }
211
 
      }
212
 
    }
213
 
  }
214
 
 
215
 
  uMap->decRefCnt();
216
 
  return tmp;
217
 
}
218
 
 
219
 
GooString* HtmlFont::simple(HtmlFont* font, Unicode* content, int uLen){
220
 
  GooString *cont=HtmlFilter (content, uLen); 
221
 
 
222
 
  /*if (font.isBold()) {
223
 
    cont->insert(0,"<b>",3);
224
 
    cont->append("</b>",4);
225
 
  }
226
 
  if (font.isItalic()) {
227
 
    cont->insert(0,"<i>",3);
228
 
    cont->append("</i>",4);
229
 
    } */
230
 
 
231
 
  return cont;
232
 
}
233
 
 
234
 
HtmlFontAccu::HtmlFontAccu(){
235
 
  accu=new GVector<HtmlFont>();
236
 
}
237
 
 
238
 
HtmlFontAccu::~HtmlFontAccu(){
239
 
  if (accu) delete accu;
240
 
}
241
 
 
242
 
int HtmlFontAccu::AddFont(const HtmlFont& font){
243
 
 GVector<HtmlFont>::iterator i; 
244
 
 for (i=accu->begin();i!=accu->end();i++)
245
 
 {
246
 
        if (font.isEqual(*i)) 
247
 
        {
248
 
                return (int)(i-(accu->begin()));
249
 
        }
250
 
 }
251
 
 
252
 
 accu->push_back(font);
253
 
 return (accu->size()-1);
254
 
}
255
 
 
256
 
// get CSS font name for font #i 
257
 
GooString* HtmlFontAccu::getCSStyle(int i, GooString* content){
258
 
  GooString *tmp;
259
 
  GooString *iStr=GooString::fromInt(i);
260
 
  
261
 
  if (!xml) {
262
 
    tmp = new GooString("<span class=\"ft");
263
 
    tmp->append(iStr);
264
 
    tmp->append("\">");
265
 
    tmp->append(content);
266
 
    tmp->append("</span>");
267
 
  } else {
268
 
    tmp = new GooString("");
269
 
    tmp->append(content);
270
 
  }
271
 
 
272
 
  delete iStr;
273
 
  return tmp;
274
 
}
275
 
 
276
 
// get CSS font definition for font #i 
277
 
GooString* HtmlFontAccu::CSStyle(int i){
278
 
   GooString *tmp=new GooString();
279
 
   GooString *iStr=GooString::fromInt(i);
280
 
 
281
 
   GVector<HtmlFont>::iterator g=accu->begin();
282
 
   g+=i;
283
 
   HtmlFont font=*g;
284
 
   GooString *Size=GooString::fromInt(font.getSize());
285
 
   GooString *colorStr=font.getColor().toString();
286
 
   GooString *fontName=font.getFontName();
287
 
   GooString *lSize;
288
 
   
289
 
   if(!xml){
290
 
     tmp->append(".ft");
291
 
     tmp->append(iStr);
292
 
     tmp->append("{font-size:");
293
 
     tmp->append(Size);
294
 
     if( font.getLineSize() != -1 )
295
 
     {
296
 
         lSize = GooString::fromInt(font.getLineSize());
297
 
         tmp->append("px;line-height:");
298
 
         tmp->append(lSize);
299
 
         delete lSize;
300
 
     }
301
 
     tmp->append("px;font-family:");
302
 
     tmp->append(fontName); //font.getFontName());
303
 
     tmp->append(";color:");
304
 
     tmp->append(colorStr);
305
 
     tmp->append(";}");
306
 
   }
307
 
   if (xml) {
308
 
     tmp->append("<fontspec id=\"");
309
 
     tmp->append(iStr);
310
 
     tmp->append("\" size=\"");
311
 
     tmp->append(Size);
312
 
     tmp->append("\" family=\"");
313
 
     tmp->append(fontName); //font.getFontName());
314
 
     tmp->append("\" color=\"");
315
 
     tmp->append(colorStr);
316
 
     tmp->append("\"/>");
317
 
   }
318
 
 
319
 
   delete fontName;
320
 
   delete colorStr;
321
 
   delete iStr;
322
 
   delete Size;
323
 
   return tmp;
324
 
}
325
 
 
326