| OLD | NEW |
| 1 #include "SkPdfFont.h" | 1 #include "SkPdfFont.h" |
| 2 #include "SkPdfParser.h" | 2 #include "SkPdfParser.h" |
| 3 | 3 |
| 4 #include "SkStream.h" | 4 #include "SkStream.h" |
| 5 #include "SkTypeface.h" | 5 #include "SkTypeface.h" |
| 6 #include "SkPdfPodofoTokenizer.h" |
| 6 | 7 |
| 7 std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts() { | 8 std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts() { |
| 8 static std::map<std::string, SkPdfStandardFontEntry> gPdfStandardFonts; | 9 static std::map<std::string, SkPdfStandardFontEntry> gPdfStandardFonts; |
| 9 | 10 |
| 10 // TODO (edisonn): , vs - ? what does it mean? | 11 // TODO (edisonn): , vs - ? what does it mean? |
| 11 // TODO (edisonn): MT, PS, Oblique=italic?, ... what does it mean? | 12 // TODO (edisonn): MT, PS, Oblique=italic?, ... what does it mean? |
| 12 if (gPdfStandardFonts.empty()) { | 13 if (gPdfStandardFonts.empty()) { |
| 13 gPdfStandardFonts["Arial"] = {"Arial", false, false}; | 14 gPdfStandardFonts["Arial"] = {"Arial", false, false}; |
| 14 gPdfStandardFonts["Arial,Bold"] = {"Arial", true, false}; | 15 gPdfStandardFonts["Arial,Bold"] = {"Arial", true, false}; |
| 15 gPdfStandardFonts["Arial,BoldItalic"] = {"Arial", true, true}; | 16 gPdfStandardFonts["Arial,BoldItalic"] = {"Arial", true, true}; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } else if (fd->has_FontFile2()) { | 158 } else if (fd->has_FontFile2()) { |
| 158 pdfStream = fd->FontFile2(); | 159 pdfStream = fd->FontFile2(); |
| 159 } if (fd->has_FontFile3()) { | 160 } if (fd->has_FontFile3()) { |
| 160 pdfStream = fd->FontFile3(); | 161 pdfStream = fd->FontFile3(); |
| 161 } else { | 162 } else { |
| 162 if (loadFromName) { | 163 if (loadFromName) { |
| 163 return fontFromName(fd, fd->FontName().c_str()); | 164 return fontFromName(fd, fd->FontName().c_str()); |
| 164 } | 165 } |
| 165 } | 166 } |
| 166 | 167 |
| 167 if (!pdfStream || !pdfStream->podofo()->GetStream()) { | 168 char* uncompressedStream = NULL; |
| 168 // TODO(edisonn): report warning to be used in testing. | 169 long uncompressedStreamLength = 0; |
| 170 |
| 171 // TODO(edisonn): report warning to be used in testing. |
| 172 if (!pdfStream || |
| 173 !pdfStream->GetFilteredCopy(&uncompressedStream, &uncompressedStream
Length) || |
| 174 !uncompressedStream || |
| 175 !uncompressedStreamLength) { |
| 169 return NULL; | 176 return NULL; |
| 170 } | 177 } |
| 171 | 178 |
| 172 char* uncompressedStream = NULL; | |
| 173 pdf_long uncompressedStreamLength = 0; | |
| 174 | |
| 175 // TODO(edisonn): get rid of try/catch exceptions! We should not throw on us
er data! | |
| 176 try { | |
| 177 pdfStream->podofo()->GetStream()->GetFilteredCopy(&uncompressedStream, &
uncompressedStreamLength); | |
| 178 } catch (PdfError& e) { | |
| 179 // TODO(edisonn): report warning to be used in testing. | |
| 180 return NULL; | |
| 181 } | |
| 182 SkMemoryStream* skStream = new SkMemoryStream(uncompressedStream, uncompress
edStreamLength); | 179 SkMemoryStream* skStream = new SkMemoryStream(uncompressedStream, uncompress
edStreamLength); |
| 183 SkTypeface* face = SkTypeface::CreateFromStream(skStream); | 180 SkTypeface* face = SkTypeface::CreateFromStream(skStream); |
| 184 | 181 |
| 185 if (face == NULL) { | 182 if (face == NULL) { |
| 186 // TODO(edisonn): report warning to be used in testing. | 183 // TODO(edisonn): report warning to be used in testing. |
| 187 return NULL; | 184 return NULL; |
| 188 } | 185 } |
| 189 | 186 |
| 190 face->ref(); | 187 face->ref(); |
| 191 | 188 |
| 192 return new SkPdfStandardFont(face); | 189 return new SkPdfStandardFont(face); |
| 193 } | 190 } |
| 194 | 191 |
| 195 SkPdfFont* fontFromName(SkPdfObject* obj, const char* fontName) { | 192 SkPdfFont* fontFromName(SkPdfObject* obj, const char* fontName) { |
| 196 SkTypeface* typeface = SkTypefaceFromPdfStandardFont(fontName, false, false)
; | 193 SkTypeface* typeface = SkTypefaceFromPdfStandardFont(fontName, false, false)
; |
| 197 if (typeface != NULL) { | 194 if (typeface != NULL) { |
| 198 return new SkPdfStandardFont(typeface); | 195 return new SkPdfStandardFont(typeface); |
| 199 } | 196 } |
| 200 | 197 |
| 201 // TODO(edisonn): perf - make a map | 198 // TODO(edisonn): perf - make a map |
| 202 for (int i = 0 ; i < obj->doc()->GetObjects().GetSize(); i++) { | 199 for (int i = 0 ; i < obj->doc()->objects(); i++) { |
| 203 PdfVecObjects& objects = (PdfVecObjects&)obj->doc()->GetObjects(); | 200 const SkPdfObject* podofoFont = obj->doc()->object(i); |
| 204 const PdfObject* podofoFont = objects[i]; | |
| 205 SkPdfFontDescriptorDictionary* fd = NULL; | 201 SkPdfFontDescriptorDictionary* fd = NULL; |
| 206 | 202 |
| 207 if (mapFontDescriptorDictionary(*obj->doc(), *podofoFont, &fd)) { | 203 if (obj->doc()->mapper()->mapFontDescriptorDictionary(podofoFont, &fd))
{ |
| 208 if (fd->has_FontName() && fd->FontName() == fontName) { | 204 if (fd->has_FontName() && fd->FontName() == fontName) { |
| 209 SkPdfFont* font = SkPdfFont::fontFromFontDescriptor(fd, false); | 205 SkPdfFont* font = SkPdfFont::fontFromFontDescriptor(fd, false); |
| 210 if (font) { | 206 if (font) { |
| 211 return font; | 207 return font; |
| 212 } else { | 208 } else { |
| 213 // failed to load font descriptor | 209 // failed to load font descriptor |
| 214 break; | 210 break; |
| 215 } | 211 } |
| 216 } | 212 } |
| 217 } | 213 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 SkPdfMultiMasterFont* SkPdfFont::fontFromMultiMasterFontDictionary(SkPdfMultiMas
terFontDictionary* dict) { | 278 SkPdfMultiMasterFont* SkPdfFont::fontFromMultiMasterFontDictionary(SkPdfMultiMas
terFontDictionary* dict) { |
| 283 if (dict == NULL) { | 279 if (dict == NULL) { |
| 284 return NULL; // default one? | 280 return NULL; // default one? |
| 285 } | 281 } |
| 286 | 282 |
| 287 return new SkPdfMultiMasterFont(dict); | 283 return new SkPdfMultiMasterFont(dict); |
| 288 } | 284 } |
| 289 | 285 |
| 290 static int skstoi(const SkPdfString* str) { | 286 static int skstoi(const SkPdfString* str) { |
| 291 int ret = 0; | 287 int ret = 0; |
| 292 for (int i = 0 ; i < str->podofo()->GetString().GetLength(); i++) { | 288 for (int i = 0 ; i < str->len(); i++) { |
| 293 ret = (ret << 8) + ((unsigned char*)str->podofo()->GetString().GetString
())[i]; | 289 ret = (ret << 8) + ((unsigned char*)str->c_str())[i]; |
| 294 } | 290 } |
| 295 return ret; | 291 return ret; |
| 296 } | 292 } |
| 297 | 293 |
| 298 SkPdfToUnicode::SkPdfToUnicode(const SkPdfStream* stream) { | 294 SkPdfToUnicode::SkPdfToUnicode(const SkPdfStream* stream) { |
| 299 fCMapEncoding = NULL; | 295 fCMapEncoding = NULL; |
| 300 fCMapEncodingFlag = NULL; | 296 fCMapEncodingFlag = NULL; |
| 301 | 297 |
| 302 if (stream) { | 298 if (stream) { |
| 303 SkPdfTokenizer tokenizer(stream); | 299 SkPdfPodofoTokenizer* tokenizer = stream->doc()->tokenizerOfStream(strea
m); |
| 304 PdfToken token; | 300 PdfToken token; |
| 305 | 301 |
| 306 fCMapEncoding = new unsigned short[256 * 256]; | 302 fCMapEncoding = new unsigned short[256 * 256]; |
| 307 fCMapEncodingFlag = new unsigned char[256 * 256]; | 303 fCMapEncodingFlag = new unsigned char[256 * 256]; |
| 308 for (int i = 0 ; i < 256 * 256; i++) { | 304 for (int i = 0 ; i < 256 * 256; i++) { |
| 309 fCMapEncoding[i] = i; | 305 fCMapEncoding[i] = i; |
| 310 fCMapEncodingFlag[i] = 0; | 306 fCMapEncodingFlag[i] = 0; |
| 311 } | 307 } |
| 312 | 308 |
| 313 // TODO(edisonn): deal with multibyte character, or longer strings. | 309 // TODO(edisonn): deal with multibyte character, or longer strings. |
| 314 // Ritght now we deal with up 2 characters, e.g. <0020> but not longer l
ike <00660066006C> | 310 // Ritght now we deal with up 2 characters, e.g. <0020> but not longer l
ike <00660066006C> |
| 315 //2 beginbfrange | 311 //2 beginbfrange |
| 316 //<0000> <005E> <0020> | 312 //<0000> <005E> <0020> |
| 317 //<005F> <0061> [<00660066> <00660069> <00660066006C>] | 313 //<005F> <0061> [<00660066> <00660069> <00660066006C>] |
| 318 | 314 |
| 319 while (tokenizer.readToken(&token)) { | 315 while (tokenizer->readToken(&token)) { |
| 320 | 316 |
| 321 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beg
incodespacerange") == 0) { | 317 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beg
incodespacerange") == 0) { |
| 322 while (tokenizer.readToken(&token) && !(token.fType == kKeyword_
TokenType && strcmp(token.fKeyword, "endcodespacerange") == 0)) { | 318 while (tokenizer->readToken(&token) && !(token.fType == kKeyword
_TokenType && strcmp(token.fKeyword, "endcodespacerange") == 0)) { |
| 323 // tokenizer.PutBack(token); | 319 // tokenizer->PutBack(token); |
| 324 // tokenizer.readToken(&token); | 320 // tokenizer->readToken(&token); |
| 325 // TODO(edisonn): check token type! ignore/report errors. | 321 // TODO(edisonn): check token type! ignore/report errors. |
| 326 int start = skstoi(token.fObject->asString()); | 322 int start = skstoi(token.fObject->asString()); |
| 327 tokenizer.readToken(&token); | 323 tokenizer->readToken(&token); |
| 328 int end = skstoi(token.fObject->asString()); | 324 int end = skstoi(token.fObject->asString()); |
| 329 for (int i = start; i <= end; i++) { | 325 for (int i = start; i <= end; i++) { |
| 330 fCMapEncodingFlag[i] |= 1; | 326 fCMapEncodingFlag[i] |= 1; |
| 331 } | 327 } |
| 332 } | 328 } |
| 333 } | 329 } |
| 334 | 330 |
| 335 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beg
inbfchar") == 0) { | 331 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beg
inbfchar") == 0) { |
| 336 while (tokenizer.readToken(&token) && !(token.fType == kKeyword_
TokenType && strcmp(token.fKeyword, "endbfchar") == 0)) { | 332 while (tokenizer->readToken(&token) && !(token.fType == kKeyword
_TokenType && strcmp(token.fKeyword, "endbfchar") == 0)) { |
| 337 // tokenizer.PutBack(token); | 333 // tokenizer->PutBack(token); |
| 338 // tokenizer.readToken(&token); | 334 // tokenizer->readToken(&token); |
| 339 int from = skstoi(token.fObject->asString()); | 335 int from = skstoi(token.fObject->asString()); |
| 340 tokenizer.readToken(&token); | 336 tokenizer->readToken(&token); |
| 341 int to = skstoi(token.fObject->asString()); | 337 int to = skstoi(token.fObject->asString()); |
| 342 | 338 |
| 343 fCMapEncodingFlag[from] |= 2; | 339 fCMapEncodingFlag[from] |= 2; |
| 344 fCMapEncoding[from] = to; | 340 fCMapEncoding[from] = to; |
| 345 } | 341 } |
| 346 } | 342 } |
| 347 | 343 |
| 348 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beg
inbfrange") == 0) { | 344 if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beg
inbfrange") == 0) { |
| 349 while (tokenizer.readToken(&token) && !(token.fType == kKeyword_
TokenType && strcmp(token.fKeyword, "endbfrange") == 0)) { | 345 while (tokenizer->readToken(&token) && !(token.fType == kKeyword
_TokenType && strcmp(token.fKeyword, "endbfrange") == 0)) { |
| 350 // tokenizer.PutBack(token); | 346 // tokenizer->PutBack(token); |
| 351 // tokenizer.readToken(&token); | 347 // tokenizer->readToken(&token); |
| 352 int start = skstoi(token.fObject->asString()); | 348 int start = skstoi(token.fObject->asString()); |
| 353 tokenizer.readToken(&token); | 349 tokenizer->readToken(&token); |
| 354 int end = skstoi(token.fObject->asString()); | 350 int end = skstoi(token.fObject->asString()); |
| 355 | 351 |
| 356 | 352 |
| 357 tokenizer.readToken(&token); // [ or just an array directly? | 353 tokenizer->readToken(&token); // [ or just an array directly
? |
| 358 // tokenizer.PutBack(token); | 354 // tokenizer->PutBack(token); |
| 359 | 355 |
| 360 if (token.fType == kObject_TokenType && token.fObject->asStr
ing()) { | 356 if (token.fType == kObject_TokenType && token.fObject->asStr
ing()) { |
| 361 // tokenizer.readToken(&token); | 357 // tokenizer->readToken(&token); |
| 362 int value = skstoi(token.fObject->asString()); | 358 int value = skstoi(token.fObject->asString()); |
| 363 | 359 |
| 364 for (int i = start; i <= end; i++) { | 360 for (int i = start; i <= end; i++) { |
| 365 fCMapEncodingFlag[i] |= 2; | 361 fCMapEncodingFlag[i] |= 2; |
| 366 fCMapEncoding[i] = value; | 362 fCMapEncoding[i] = value; |
| 367 value++; | 363 value++; |
| 368 // if i != end, verify last byte id not if, ignore/r
eport error | 364 // if i != end, verify last byte id not if, ignore/r
eport error |
| 369 } | 365 } |
| 370 | 366 |
| 371 // read one string | 367 // read one string |
| 372 } else if (token.fType == kObject_TokenType && token.fObject
->asArray()) { | 368 } else if (token.fType == kObject_TokenType && token.fObject
->asArray()) { |
| 373 // tokenizer.readToken(&token); | 369 // tokenizer->readToken(&token); |
| 374 for (int i = 0; i < token.fObject->asArray()->size(); i+
+) { | 370 for (int i = 0; i < token.fObject->asArray()->size(); i+
+) { |
| 375 fCMapEncodingFlag[start + i] |= 2; | 371 fCMapEncodingFlag[start + i] |= 2; |
| 376 fCMapEncoding[start + i] = skstoi((*token.fObject->a
sArray())[i]->asString()); | 372 fCMapEncoding[start + i] = skstoi((*token.fObject->a
sArray())[i]->asString()); |
| 377 } | 373 } |
| 378 // read array | 374 // read array |
| 379 } | 375 } |
| 380 | 376 |
| 381 //fCMapEncodingFlag[from] = 1; | 377 //fCMapEncodingFlag[from] = 1; |
| 382 //fCMapEncoding[from] = to; | 378 //fCMapEncoding[from] = to; |
| 383 } | 379 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 SkPdfEncoding* SkPdfEncoding::fromName(const char* name) { | 415 SkPdfEncoding* SkPdfEncoding::fromName(const char* name) { |
| 420 SkPdfEncoding* encoding = getStandardEncodings()[name]; | 416 SkPdfEncoding* encoding = getStandardEncodings()[name]; |
| 421 | 417 |
| 422 #ifdef PDF_TRACE | 418 #ifdef PDF_TRACE |
| 423 if (encoding == NULL) { | 419 if (encoding == NULL) { |
| 424 printf("Encoding not found: %s\n", name); | 420 printf("Encoding not found: %s\n", name); |
| 425 } | 421 } |
| 426 #endif | 422 #endif |
| 427 return encoding; | 423 return encoding; |
| 428 } | 424 } |
| OLD | NEW |