Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: experimental/PdfViewer/SkPdfFont.h

Issue 18117005: more work on pdf fonts, more to come (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « experimental/PdfViewer/PdfReference-okular-1.txt ('k') | experimental/PdfViewer/SkPdfFont.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #ifndef __DEFINED__SkPdfFont 1 #ifndef __DEFINED__SkPdfFont
2 #define __DEFINED__SkPdfFont 2 #define __DEFINED__SkPdfFont
3 3
4 #include "SkPdfHeaders_autogen.h" 4 #include "SkPdfHeaders_autogen.h"
5 #include "SkPdfPodofoMapper_autogen.h" 5 #include "SkPdfPodofoMapper_autogen.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "SkTypeface.h" 10 #include "SkTypeface.h"
11 #include "SkUtils.h" 11 #include "SkUtils.h"
12 #include "SkPdfBasics.h" 12 #include "SkPdfBasics.h"
13 #include "SkPdfUtils.h" 13 #include "SkPdfUtils.h"
14 14
15 15
16 class SkPdfType0Font; 16 class SkPdfType0Font;
17 class SkPdfType1Font; 17 class SkPdfType1Font;
18 class SkPdfType3Font; 18 class SkPdfType3Font;
19 class SkPdfTrueTypeFont; 19 class SkPdfTrueTypeFont;
20 class SkPdfCIDFont;
21 class SkPdfMultiMasterFont; 20 class SkPdfMultiMasterFont;
22 class SkPdfFont; 21 class SkPdfFont;
23 22
24 23
25 struct SkPdfStandardFontEntry { 24 struct SkPdfStandardFontEntry {
26 const char* fName; 25 const char* fName;
27 bool fIsBold; 26 bool fIsBold;
28 bool fIsItalic; 27 bool fIsItalic;
29 }; 28 };
30 29
31 std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts(); 30 std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts();
32 SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic); 31 SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic);
33 SkPdfFont* SkPdfFontFromName(SkPdfObject* obj, const char* fontName); 32 SkPdfFont* fontFromName(SkPdfObject* obj, const char* fontName);
34 33
35 struct SkUnencodedText { 34 struct SkUnencodedText {
36 void* text; 35 void* text;
37 int len; 36 int len;
38 37
39 public: 38 public:
40 SkUnencodedText(const SkPdfObject* obj) { 39 SkUnencodedText(const SkPdfObject* obj) {
41 text = (void*)obj->podofo()->GetString().GetString(); 40 text = (void*)obj->podofo()->GetString().GetString();
42 len = obj->podofo()->GetString().GetLength(); 41 len = obj->podofo()->GetString().GetLength();
43 } 42 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 92
94 return true; 93 return true;
95 } 94 }
96 95
97 static SkPdfIdentityHEncoding* instance() { 96 static SkPdfIdentityHEncoding* instance() {
98 static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding(); 97 static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding();
99 return inst; 98 return inst;
100 } 99 }
101 }; 100 };
102 101
102 // TODO(edisonn): using this one when no encoding is specified
103 class SkPdfDefaultEncoding : public SkPdfEncoding {
104 public:
105 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOu t) const {
106 // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
107
108 unsigned char* text = (unsigned char*)textIn.text;
109 textOut->text = new uint16_t[textIn.len];
110 textOut->len = textIn.len;
111
112 for (int i = 0; i < textOut->len; i++) {
113 textOut->text[i] = text[i];
114 }
115
116 return true;
117 }
118
119 static SkPdfDefaultEncoding* instance() {
120 static SkPdfDefaultEncoding* inst = new SkPdfDefaultEncoding();
121 return inst;
122 }
123 };
103 124
104 class SkPdfCIDToGIDMapIdentityEncoding : public SkPdfEncoding { 125 class SkPdfCIDToGIDMapIdentityEncoding : public SkPdfEncoding {
105 public: 126 public:
106 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOu t) const { 127 virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOu t) const {
107 // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error? 128 // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
108 129
109 unsigned char* text = (unsigned char*)textIn.text; 130 unsigned char* text = (unsigned char*)textIn.text;
110 textOut->text = new uint16_t[textIn.len]; 131 textOut->text = new uint16_t[textIn.len];
111 textOut->len = textIn.len; 132 textOut->len = textIn.len;
112 133
(...skipping 11 matching lines...) Expand all
124 }; 145 };
125 146
126 class SkPdfFont { 147 class SkPdfFont {
127 public: 148 public:
128 SkPdfFont* fBaseFont; 149 SkPdfFont* fBaseFont;
129 SkPdfEncoding* fEncoding; 150 SkPdfEncoding* fEncoding;
130 SkPdfToUnicode* fToUnicode; 151 SkPdfToUnicode* fToUnicode;
131 152
132 153
133 public: 154 public:
134 SkPdfFont() : fBaseFont(NULL), fEncoding(NULL), fToUnicode(NULL) {} 155 SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()), fToUnicode(NULL) {}
135 156
136 const SkPdfEncoding* encoding() const {return fEncoding;} 157 const SkPdfEncoding* encoding() const {return fEncoding;}
137 158
138 void drawText(const SkDecodedText& text, SkPaint* paint, PdfContext* pdfCont ext, SkCanvas* canvas, SkMatrix* matrix) { 159 void drawText(const SkDecodedText& text, SkPaint* paint, PdfContext* pdfCont ext, SkCanvas* canvas) {
139 for (int i = 0 ; i < text.size(); i++) { 160 for (int i = 0 ; i < text.size(); i++) {
140 drawOneChar(text[i], paint, pdfContext, canvas, matrix); 161 double width = drawOneChar(text[i], paint, pdfContext, canvas);
162 pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(w idth), SkDoubleToScalar(0.0));
163 canvas->translate(SkDoubleToScalar(width), SkDoubleToScalar(0.0));
141 } 164 }
142 } 165 }
143 166
144 void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { 167 void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const {
145 if (fToUnicode) { 168 if (fToUnicode) {
146 textOut->text = new uint16_t[textIn.len]; 169 textOut->text = new uint16_t[textIn.len];
147 textOut->len = textIn.len; 170 textOut->len = textIn.len;
148 for (int i = 0; i < textIn.len; i++) { 171 for (int i = 0; i < textIn.len; i++) {
149 textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]]; 172 textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]];
150 } 173 }
151 } else { 174 } else {
152 textOut->text = textIn.text; 175 textOut->text = textIn.text;
153 textOut->len = textIn.len; 176 textOut->len = textIn.len;
154 } 177 }
155 }; 178 };
156 179
157 inline unsigned int ToUnicode(unsigned int ch) const { 180 inline unsigned int ToUnicode(unsigned int ch) const {
158 if (fToUnicode) { 181 if (fToUnicode) {
159 return fToUnicode->fCMapEncoding[ch]; 182 return fToUnicode->fCMapEncoding[ch];
160 } else { 183 } else {
161 return ch; 184 return ch;
162 } 185 }
163 }; 186 };
164 187
165 static SkPdfFont* fontFromPdfDictionary(SkPdfFontDictionary* dict); 188 static SkPdfFont* fontFromPdfDictionary(SkPdfFontDictionary* dict);
166 static SkPdfFont* Default() {return SkPdfFontFromName(NULL, "TimesNewRoman") ;} 189 static SkPdfFont* Default() {return fontFromName(NULL, "TimesNewRoman");}
167 190
168 static SkPdfType0Font* fontFromType0FontDictionary(SkPdfType0FontDictionary* dict); 191 static SkPdfType0Font* fontFromType0FontDictionary(SkPdfType0FontDictionary* dict);
169 static SkPdfType1Font* fontFromType1FontDictionary(SkPdfType1FontDictionary* dict); 192 static SkPdfType1Font* fontFromType1FontDictionary(SkPdfType1FontDictionary* dict);
170 static SkPdfType3Font* fontFromType3FontDictionary(SkPdfType3FontDictionary* dict); 193 static SkPdfType3Font* fontFromType3FontDictionary(SkPdfType3FontDictionary* dict);
171 static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfTrueTypeFontDi ctionary* dict); 194 static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfTrueTypeFontDi ctionary* dict);
172 static SkPdfCIDFont* fontFromCIDFontDictionary(SkPdfCIDFontDictionary* dict) ;
173 static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(SkPdfMultiMas terFontDictionary* dict); 195 static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(SkPdfMultiMas terFontDictionary* dict);
174 196
197 static SkPdfFont* fontFromFontDescriptor(SkPdfFontDescriptorDictionary* fd, bool loadFromName = true);
198
175 public: 199 public:
176 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) = 0; 200 virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfC ontext, SkCanvas* canvas) = 0;
177 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) = 0;
178 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0; 201 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0;
179 }; 202 };
180 203
181 class SkPdfStandardFont : public SkPdfFont { 204 class SkPdfStandardFont : public SkPdfFont {
182 SkTypeface* fTypeface; 205 SkTypeface* fTypeface;
183 206
184 public: 207 public:
185 SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {} 208 SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {}
186 209
187 public: 210 public:
188 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) { 211 virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfC ontext, SkCanvas* canvas) {
189 paint->setTypeface(fTypeface); 212 paint->setTypeface(fTypeface);
190 paint->setTextEncoding(SkPaint::kUTF8_TextEncoding); 213 paint->setTextEncoding(SkPaint::kUTF8_TextEncoding);
191 214
192 unsigned long ch4 = ch; 215 unsigned long ch4 = ch;
193 char utf8[10]; 216 char utf8[10];
194 int len = SkUTF8_FromUnichar(ch4, utf8); 217 int len = SkUTF8_FromUnichar(ch4, utf8);
195 218
196 canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *p aint); 219 canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *p aint);
197 220
198 SkScalar textWidth = paint->measureText(utf8, len); 221 SkScalar textWidth = paint->measureText(utf8, len);
199 matrix->preTranslate(textWidth, SkDoubleToScalar(0.0)); 222 return SkScalarToDouble(textWidth);
200 } 223 }
201 224
202 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {}
203 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} 225 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
204 }; 226 };
205 227
206 class SkPdfType0Font : public SkPdfFont { 228 class SkPdfType0Font : public SkPdfFont {
207 public: 229 public:
208 SkPdfType0Font(SkPdfType0FontDictionary* dict); 230 SkPdfType0Font(SkPdfType0FontDictionary* dict);
209 231
210 public: 232 public:
211 233
212 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) { 234 virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfC ontext, SkCanvas* canvas) {
213 fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas, matrix) ; 235 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
214 }
215
216 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {
217
218 } 236 }
219 237
220 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { 238 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
221 } 239 }
222 }; 240 };
223 241
224 class SkPdfTrueTypeFont : public SkPdfFont { 242 class SkPdfType1Font : public SkPdfFont {
225 public: 243 public:
226 SkPdfTrueTypeFont(SkPdfTrueTypeFontDictionary* dict) { 244 SkPdfType1Font(SkPdfType1FontDictionary* dict) {
227 fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); 245 if (dict->has_FontDescriptor()) {
246 fBaseFont = SkPdfFont::fontFromFontDescriptor(dict->FontDescriptor() );
247 } else {
248 fBaseFont = fontFromName(dict, dict->BaseFont().c_str());
249 }
228 } 250 }
229 251
230 public: 252 public:
231 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) { 253 virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pd fContext, SkCanvas* canvas) {
232 254 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas );
233 }
234
235 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {
236
237 }
238
239 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
240
241 }
242 };
243
244
245 class SkPdfType1Font : public SkPdfFont {
246 public:
247 SkPdfType1Font(SkPdfType1FontDictionary* dict) {
248 fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str());
249 }
250
251
252 public:
253 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfC ontext, SkCanvas* canvas, SkMatrix* matrix) {
254
255 }
256
257 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {
258
259 } 255 }
260 256
261 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { 257 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
262 258
263 } 259 }
264 }; 260 };
265 261
266 262 class SkPdfTrueTypeFont : public SkPdfType1Font {
267 class SkPdfCIDFont : public SkPdfFont {
268 public: 263 public:
269 SkPdfCIDFont(SkPdfCIDFontDictionary* dict) { 264 SkPdfTrueTypeFont(SkPdfTrueTypeFontDictionary* dict) : SkPdfType1Font(dict) {
270 fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str());
271 }
272
273 public:
274 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) {
275
276 }
277
278 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {
279
280 }
281
282 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
283
284 } 265 }
285 }; 266 };
286 267
287 class SkPdfMultiMasterFont : public SkPdfFont { 268 class SkPdfMultiMasterFont : public SkPdfType1Font {
288 public: 269 public:
289 SkPdfMultiMasterFont(SkPdfMultiMasterFontDictionary* dict) { 270 SkPdfMultiMasterFont(SkPdfMultiMasterFontDictionary* dict) : SkPdfType1Font( dict) {
290 fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str());
291 }
292
293 public:
294 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) {
295
296 }
297
298 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {
299
300 }
301
302 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
303
304 } 271 }
305 }; 272 };
306 /* 273 /*
307 class CIDToGIDMap { 274 class CIDToGIDMap {
308 virtual unsigned int map(unsigned int cid) = 0; 275 virtual unsigned int map(unsigned int cid) = 0;
309 static CIDToGIDMap* fromName(const char* name); 276 static CIDToGIDMap* fromName(const char* name);
310 }; 277 };
311 278
312 class CIDToGIDMap_Identity { 279 class CIDToGIDMap_Identity {
313 virtual unsigned int map(unsigned int cid) { return cid; } 280 virtual unsigned int map(unsigned int cid) { return cid; }
(...skipping 30 matching lines...) Expand all
344 unsigned int fFirstChar; 311 unsigned int fFirstChar;
345 unsigned int fLastChar; 312 unsigned int fLastChar;
346 313
347 SkRect fFontBBox; 314 SkRect fFontBBox;
348 SkMatrix fFonMatrix; 315 SkMatrix fFonMatrix;
349 316
350 Type3FontChar* fChars; 317 Type3FontChar* fChars;
351 318
352 public: 319 public:
353 SkPdfType3Font(SkPdfType3FontDictionary* dict) { 320 SkPdfType3Font(SkPdfType3FontDictionary* dict) {
354 fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); 321 fBaseFont = fontFromName(dict, dict->BaseFont().c_str());
355 322
356 if (dict->has_Encoding()) { 323 if (dict->has_Encoding()) {
357 if (dict->isEncodingAName()) { 324 if (dict->isEncodingAName()) {
358 fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName().c _str()); 325 fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName().c _str());
359 } else if (dict->isEncodingAEncodingdictionary()) { 326 } else if (dict->isEncodingAEncodingdictionary()) {
360 // technically, there is no encoding. 327 // technically, there is no encoding.
361 fEncoding = SkPdfCIDToGIDMapIdentityEncoding::instance(); 328 fEncoding = SkPdfCIDToGIDMapIdentityEncoding::instance();
362 fEncodingDict = dict->getEncodingAsEncodingdictionary(); 329 fEncodingDict = dict->getEncodingAsEncodingdictionary();
363 } 330 }
364 } 331 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 } 370 }
404 fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]->asNam e()->value().c_str()); 371 fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]->asNam e()->value().c_str());
405 j++; 372 j++;
406 } else { 373 } else {
407 // err 374 // err
408 } 375 }
409 } 376 }
410 } 377 }
411 378
412 public: 379 public:
413 virtual void drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfCon text, SkCanvas* canvas, SkMatrix* matrix) { 380 virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfC ontext, SkCanvas* canvas) {
414 if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) { 381 if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) {
415 fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas, mat rix); 382 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canv as);
416 return;
417 } 383 }
418 384
419 #ifdef PDF_TRACE 385 #ifdef PDF_TRACE
420 printf("Type 3 char to unicode: %c\n", ToUnicode(ch)); 386 printf("Type 3 char to unicode: %c\n", ToUnicode(ch));
421 if (ToUnicode(ch) == 'A') { 387 if (ToUnicode(ch) == 'A') {
422 printf("break;\n"); 388 printf("break;\n");
423 } 389 }
424 #endif 390 #endif
425 391
426 doType3Char(pdfContext, canvas, fChars[ch - fFirstChar].fObj, fFontBBox, fFonMatrix, pdfContext->fGraphicsState.fCurFontSize); 392 doType3Char(pdfContext, canvas, fChars[ch - fFirstChar].fObj, fFontBBox, fFonMatrix, pdfContext->fGraphicsState.fCurFontSize);
427 }
428 393
429 virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { 394 // TODO(edisonn): verify/test translate code, not tested yet
395 pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(pdfCo ntext->fGraphicsState.fCurFontSize * fChars[ch - fFirstChar].fWidth),
396 SkDoubleToScalar(0.0));
430 } 397 }
431 398
432 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { 399 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
433 400
434 } 401 }
435 }; 402 };
436 403
437 #endif // __DEFINED__SkPdfFont 404 #endif // __DEFINED__SkPdfFont
OLDNEW
« no previous file with comments | « experimental/PdfViewer/PdfReference-okular-1.txt ('k') | experimental/PdfViewer/SkPdfFont.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698