edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 1 | #include "SkPdfFont.h" |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 2 | #include "SkPdfParser.h" |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 3 | |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 4 | #include "SkStream.h" |
| 5 | #include "SkTypeface.h" |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 6 | #include "SkPdfNativeTokenizer.h" |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 7 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 8 | std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts() { |
| 9 | static std::map<std::string, SkPdfStandardFontEntry> gPdfStandardFonts; |
| 10 | |
| 11 | // TODO (edisonn): , vs - ? what does it mean? |
| 12 | // TODO (edisonn): MT, PS, Oblique=italic?, ... what does it mean? |
| 13 | if (gPdfStandardFonts.empty()) { |
| 14 | gPdfStandardFonts["Arial"] = {"Arial", false, false}; |
| 15 | gPdfStandardFonts["Arial,Bold"] = {"Arial", true, false}; |
| 16 | gPdfStandardFonts["Arial,BoldItalic"] = {"Arial", true, true}; |
| 17 | gPdfStandardFonts["Arial,Italic"] = {"Arial", false, true}; |
| 18 | gPdfStandardFonts["Arial-Bold"] = {"Arial", true, false}; |
| 19 | gPdfStandardFonts["Arial-BoldItalic"] = {"Arial", true, true}; |
| 20 | gPdfStandardFonts["Arial-BoldItalicMT"] = {"Arial", true, true}; |
| 21 | gPdfStandardFonts["Arial-BoldMT"] = {"Arial", true, false}; |
| 22 | gPdfStandardFonts["Arial-Italic"] = {"Arial", false, true}; |
| 23 | gPdfStandardFonts["Arial-ItalicMT"] = {"Arial", false, true}; |
| 24 | gPdfStandardFonts["ArialMT"] = {"Arial", false, false}; |
| 25 | gPdfStandardFonts["Courier"] = {"Courier New", false, false}; |
| 26 | gPdfStandardFonts["Courier,Bold"] = {"Courier New", true, false}; |
| 27 | gPdfStandardFonts["Courier,BoldItalic"] = {"Courier New", true, true}; |
| 28 | gPdfStandardFonts["Courier,Italic"] = {"Courier New", false, true}; |
| 29 | gPdfStandardFonts["Courier-Bold"] = {"Courier New", true, false}; |
| 30 | gPdfStandardFonts["Courier-BoldOblique"] = {"Courier New", true, true}; |
| 31 | gPdfStandardFonts["Courier-Oblique"] = {"Courier New", false, true}; |
| 32 | gPdfStandardFonts["CourierNew"] = {"Courier New", false, false}; |
| 33 | gPdfStandardFonts["CourierNew,Bold"] = {"Courier New", true, false}; |
| 34 | gPdfStandardFonts["CourierNew,BoldItalic"] = {"Courier New", true, true}; |
| 35 | gPdfStandardFonts["CourierNew,Italic"] = {"Courier New", false, true}; |
| 36 | gPdfStandardFonts["CourierNew-Bold"] = {"Courier New", true, false}; |
| 37 | gPdfStandardFonts["CourierNew-BoldItalic"] = {"Courier New", true, true}; |
| 38 | gPdfStandardFonts["CourierNew-Italic"] = {"Courier New", false, true}; |
| 39 | gPdfStandardFonts["CourierNewPS-BoldItalicMT"] = {"Courier New", true, true}; |
| 40 | gPdfStandardFonts["CourierNewPS-BoldMT"] = {"Courier New", true, false}; |
| 41 | gPdfStandardFonts["CourierNewPS-ItalicMT"] = {"Courier New", false, true}; |
| 42 | gPdfStandardFonts["CourierNewPSMT"] = {"Courier New", false, false}; |
| 43 | gPdfStandardFonts["Helvetica"] = {"Helvetica", false, false}; |
| 44 | gPdfStandardFonts["Helvetica,Bold"] = {"Helvetica", true, false}; |
| 45 | gPdfStandardFonts["Helvetica,BoldItalic"] = {"Helvetica", true, true}; |
| 46 | gPdfStandardFonts["Helvetica,Italic"] = {"Helvetica", false, true}; |
| 47 | gPdfStandardFonts["Helvetica-Bold"] = {"Helvetica", true, false}; |
| 48 | gPdfStandardFonts["Helvetica-BoldItalic"] = {"Helvetica", true, true}; |
| 49 | gPdfStandardFonts["Helvetica-BoldOblique"] = {"Helvetica", true, true}; |
| 50 | gPdfStandardFonts["Helvetica-Italic"] = {"Helvetica", false, true}; |
| 51 | gPdfStandardFonts["Helvetica-Oblique"] = {"Helvetica", false, true}; |
| 52 | gPdfStandardFonts["Times-Bold"] = {"Times New Roman", true, false}; |
| 53 | gPdfStandardFonts["Times-BoldItalic"] = {"Times New Roman", true, true}; |
| 54 | gPdfStandardFonts["Times-Italic"] = {"Times New Roman", false, true}; |
| 55 | gPdfStandardFonts["Times-Roman"] = {"Times New Roman", false, false}; |
| 56 | gPdfStandardFonts["TimesNewRoman"] = {"Times New Roman", false, false}; |
| 57 | gPdfStandardFonts["TimesNewRoman,Bold"] = {"Times New Roman", true, false}; |
| 58 | gPdfStandardFonts["TimesNewRoman,BoldItalic"] = {"Times New Roman", true, true}; |
| 59 | gPdfStandardFonts["TimesNewRoman,Italic"] = {"Times New Roman", false, true}; |
| 60 | gPdfStandardFonts["TimesNewRoman-Bold"] = {"Times New Roman", true, false}; |
| 61 | gPdfStandardFonts["TimesNewRoman-BoldItalic"] = {"Times New Roman", true, true}; |
| 62 | gPdfStandardFonts["TimesNewRoman-Italic"] = {"Times New Roman", false, true}; |
| 63 | gPdfStandardFonts["TimesNewRomanPS"] = {"Times New Roman", false, false}; |
| 64 | gPdfStandardFonts["TimesNewRomanPS-Bold"] = {"Times New Roman", true, false}; |
| 65 | gPdfStandardFonts["TimesNewRomanPS-BoldItalic"] = {"Times New Roman", true, true}; |
| 66 | gPdfStandardFonts["TimesNewRomanPS-BoldItalicMT"] = {"Times New Roman", true, true}; |
| 67 | gPdfStandardFonts["TimesNewRomanPS-BoldMT"] = {"Times New Roman", true, false}; |
| 68 | gPdfStandardFonts["TimesNewRomanPS-Italic"] = {"Times New Roman", false, true}; |
| 69 | gPdfStandardFonts["TimesNewRomanPS-ItalicMT"] = {"Times New Roman", false, true}; |
| 70 | gPdfStandardFonts["TimesNewRomanPSMT"] = {"Times New Roman", false, false}; |
| 71 | gPdfStandardFonts["Symbol"] = {"Symbol", false, false}; |
| 72 | gPdfStandardFonts["ZapfDingbats"] = {"ZapfDingbats", false, false}; |
| 73 | |
| 74 | // TODO(edisonn): these are hacks. Load Post Script font name. |
| 75 | // see FT_Get_Postscript_Name |
| 76 | // Font config is not using it, yet. |
| 77 | //https://bugs.freedesktop.org/show_bug.cgi?id=18095 |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 78 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 79 | gPdfStandardFonts["Arial-Black"] = {"Arial", true, false}; |
| 80 | gPdfStandardFonts["DejaVuSans"] = {"DejaVu Sans", false, false}; |
| 81 | gPdfStandardFonts["DejaVuSansMono"] = {"DejaVuSans Mono", false, false}; |
| 82 | gPdfStandardFonts["DejaVuSansMono-Bold"] = {"DejaVuSans Mono", true, false}; |
| 83 | gPdfStandardFonts["DejaVuSansMono-Oblique"] = {"DejaVuSans Mono", false, true}; |
| 84 | gPdfStandardFonts["Georgia-Bold"] = {"Georgia", true, false}; |
| 85 | gPdfStandardFonts["Georgia-BoldItalic"] = {"Georgia", true, true}; |
| 86 | gPdfStandardFonts["Georgia-Italic"] = {"Georgia", false, true}; |
| 87 | gPdfStandardFonts["TrebuchetMS"] = {"Trebuchet MS", false, false}; |
| 88 | gPdfStandardFonts["TrebuchetMS-Bold"] = {"Trebuchet MS", true, false}; |
| 89 | gPdfStandardFonts["Verdana-Bold"] = {"Verdana", true, false}; |
| 90 | gPdfStandardFonts["WenQuanYiMicroHei"] = {"WenQuanYi Micro Hei", false, false}; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 91 | |
| 92 | // TODO(edisonn): list all phonts available, builf post script name as in pdf spec |
| 93 | /* |
| 94 | * The PostScript name for the value of BaseFontis determined in one of two ways: |
| 95 | • Use the PostScript name that is an optional entry in the “name” table of the |
| 96 | TrueType font itself. |
| 97 | • In the absence of such an entry in the “name” table, derive a PostScript name |
| 98 | from the name by which the font is known in the host operating system: on a |
| 99 | Windows system, it is based on the lfFaceName field in a LOGFONT structure; in |
| 100 | the Mac OS, it is based on the name of the FONDresource. If the name contains |
| 101 | any spaces, the spaces are removed. |
| 102 | If the font in a source document uses a bold or italic style, but there is no font |
| 103 | data for that style, the host operating system will synthesize the style. In this case, |
| 104 | a comma and the style name (one of Bold, Italic, or BoldItalic) are appended to the |
| 105 | font name. For example, for a TrueType font that is a bold variant of the New |
| 106 | */ |
| 107 | |
| 108 | /* |
| 109 | * If the value of Subtype is MMType1. |
| 110 | • If the PostScript name of the instance contains spaces, the spaces are replaced |
| 111 | by underscores in the value of BaseFont. For instance, as illustrated in Example |
| 112 | 5.7, the name “MinionMM 366 465 11 ” (which ends with a space character) |
| 113 | becomes /MinionMM_366_465_11_. |
| 114 | */ |
| 115 | |
| 116 | // might not work on all oses ? |
| 117 | // |
| 118 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return gPdfStandardFonts; |
| 122 | } |
| 123 | |
| 124 | SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic) { |
| 125 | std::map<std::string, SkPdfStandardFontEntry>& standardFontMap = getStandardFonts(); |
| 126 | |
| 127 | SkTypeface* typeface = NULL; |
| 128 | if (standardFontMap.find(fontName) != standardFontMap.end()) { |
| 129 | SkPdfStandardFontEntry fontData = standardFontMap[fontName]; |
| 130 | |
| 131 | // TODO(edisonn): How does the bold/italic specified in standard definition combines with |
| 132 | // the one in /font key? use OR for now. |
| 133 | bold = bold || fontData.fIsBold; |
| 134 | italic = italic || fontData.fIsItalic; |
| 135 | |
| 136 | typeface = SkTypeface::CreateFromName( |
| 137 | fontData.fName, |
| 138 | SkTypeface::Style((bold ? SkTypeface::kBold : 0) | |
| 139 | (italic ? SkTypeface::kItalic : 0))); |
| 140 | } else { |
| 141 | typeface = SkTypeface::CreateFromName( |
| 142 | fontName, |
| 143 | SkTypeface::kNormal); |
| 144 | } |
| 145 | |
| 146 | if (typeface) { |
| 147 | typeface->ref(); |
| 148 | } |
| 149 | return typeface; |
| 150 | } |
| 151 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 152 | SkPdfFont* SkPdfFont::fontFromFontDescriptor(SkNativeParsedPDF* doc, SkPdfFontDescriptorDictionary* fd, bool loadFromName) { |
| 153 | // TODO(edisonn): partial implementation ... also const handling ... |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 154 | // Only one, at most be available |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 155 | SkPdfStream* pdfStream = NULL; |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 156 | if (fd->has_FontFile()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 157 | pdfStream = fd->FontFile(doc); |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 158 | } else if (fd->has_FontFile2()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 159 | pdfStream = fd->FontFile2(doc); |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 160 | } if (fd->has_FontFile3()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 161 | pdfStream = fd->FontFile3(doc); |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 162 | } else { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 163 | if (loadFromName) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 164 | return fontFromName(doc, fd, fd->FontName(doc).c_str()); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 165 | } |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 166 | } |
| 167 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 168 | unsigned char* uncompressedStream = NULL; |
| 169 | size_t uncompressedStreamLength = 0; |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 170 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 171 | // TODO(edisonn): report warning to be used in testing. |
| 172 | if (!pdfStream || |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 173 | !pdfStream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, doc->allocator()) || |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 174 | !uncompressedStream || |
| 175 | !uncompressedStreamLength) { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 178 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 179 | SkMemoryStream* skStream = new SkMemoryStream(uncompressedStream, uncompressedStreamLength); |
| 180 | SkTypeface* face = SkTypeface::CreateFromStream(skStream); |
| 181 | |
| 182 | if (face == NULL) { |
| 183 | // TODO(edisonn): report warning to be used in testing. |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | face->ref(); |
| 188 | |
| 189 | return new SkPdfStandardFont(face); |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 190 | } |
| 191 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 192 | SkPdfFont* fontFromName(SkNativeParsedPDF* doc, SkPdfObject* obj, const char* fontName) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 193 | SkTypeface* typeface = SkTypefaceFromPdfStandardFont(fontName, false, false); |
| 194 | if (typeface != NULL) { |
| 195 | return new SkPdfStandardFont(typeface); |
| 196 | } |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 197 | |
| 198 | // TODO(edisonn): perf - make a map |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 199 | for (unsigned int i = 0 ; i < doc->objects(); i++) { |
| 200 | SkPdfObject* obj = doc->object(i); |
| 201 | if (!obj->isDictionary()) { |
| 202 | continue; |
| 203 | } |
edisonn@google.com | ab03e68 | 2013-06-28 18:51:20 +0000 | [diff] [blame] | 204 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 205 | SkPdfFontDescriptorDictionary* fd = obj->asDictionary()->asFontDescriptorDictionary(); |
| 206 | |
| 207 | if (!fd->valid()) { |
| 208 | continue; |
| 209 | } |
| 210 | |
| 211 | if (fd->has_FontName() && fd->FontName(doc) == fontName) { |
| 212 | SkPdfFont* font = SkPdfFont::fontFromFontDescriptor(doc, fd, false); |
| 213 | if (font) { |
| 214 | return font; |
| 215 | } else { |
| 216 | // failed to load font descriptor |
| 217 | break; |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // TODO(edisonn): warning/report issue |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 223 | return SkPdfFont::Default(); |
| 224 | } |
| 225 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 226 | SkPdfFont* SkPdfFont::fontFromPdfDictionaryOnce(SkNativeParsedPDF* doc, SkPdfFontDictionary* dict) { |
| 227 | // TODO(edisonn): keep the type in a smart way in the SkPdfObject |
| 228 | // 1) flag, isResolved (1bit): reset at reset, add/remove/update (array) and set(dict) |
| 229 | // in a tree like structure, 3-4 bits for all the datatypes inheriting from obj (int, real, ...) |
| 230 | // if is a dict, reserveve a few bytes to encode type of dict, and so on like in a tree |
| 231 | // issue: type can be determined from context! atribute night be missing/wrong |
| 232 | switch (doc->mapper()->mapFontDictionary(dict)) { |
| 233 | case kType0FontDictionary_SkPdfObjectType: |
| 234 | return fontFromType0FontDictionary(doc, dict->asType0FontDictionary()); |
| 235 | |
| 236 | case kTrueTypeFontDictionary_SkPdfObjectType: |
| 237 | return fontFromTrueTypeFontDictionary(doc, dict->asTrueTypeFontDictionary()); |
| 238 | |
| 239 | case kType1FontDictionary_SkPdfObjectType: |
| 240 | return fontFromType1FontDictionary(doc, dict->asType1FontDictionary()); |
| 241 | |
| 242 | case kMultiMasterFontDictionary_SkPdfObjectType: |
| 243 | return fontFromMultiMasterFontDictionary(doc, dict->asMultiMasterFontDictionary()); |
| 244 | |
| 245 | case kType3FontDictionary_SkPdfObjectType: |
| 246 | return fontFromType3FontDictionary(doc, dict->asType3FontDictionary()); |
| 247 | |
| 248 | default: |
| 249 | // TODO(edisonn): report error? |
| 250 | return NULL; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | SkPdfFont* SkPdfFont::fontFromPdfDictionary(SkNativeParsedPDF* doc, SkPdfFontDictionary* dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 255 | if (dict == NULL) { |
| 256 | return NULL; // TODO(edisonn): report default one? |
| 257 | } |
| 258 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 259 | if (dict->data() == NULL) { |
| 260 | dict->setData(fontFromPdfDictionaryOnce(doc, dict)); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 261 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 262 | return (SkPdfFont*)dict->data(); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 263 | } |
| 264 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 265 | |
| 266 | |
| 267 | SkPdfType0Font* SkPdfFont::fontFromType0FontDictionary(SkNativeParsedPDF* doc, SkPdfType0FontDictionary* dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 268 | if (dict == NULL) { |
| 269 | return NULL; // default one? |
| 270 | } |
| 271 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 272 | return new SkPdfType0Font(doc, dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 273 | } |
| 274 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 275 | SkPdfType1Font* SkPdfFont:: fontFromType1FontDictionary(SkNativeParsedPDF* doc, SkPdfType1FontDictionary* dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 276 | if (dict == NULL) { |
| 277 | return NULL; // default one? |
| 278 | } |
| 279 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 280 | return new SkPdfType1Font(doc, dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 281 | } |
| 282 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 283 | SkPdfType3Font* SkPdfFont::fontFromType3FontDictionary(SkNativeParsedPDF* doc, SkPdfType3FontDictionary* dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 284 | if (dict == NULL) { |
| 285 | return NULL; // default one? |
| 286 | } |
| 287 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 288 | |
| 289 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 290 | return new SkPdfType3Font(doc, dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 291 | } |
| 292 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 293 | SkPdfTrueTypeFont* SkPdfFont::fontFromTrueTypeFontDictionary(SkNativeParsedPDF* doc, SkPdfTrueTypeFontDictionary* dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 294 | if (dict == NULL) { |
| 295 | return NULL; // default one? |
| 296 | } |
| 297 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 298 | return new SkPdfTrueTypeFont(doc, dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 299 | } |
| 300 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 301 | SkPdfMultiMasterFont* SkPdfFont::fontFromMultiMasterFontDictionary(SkNativeParsedPDF* doc, SkPdfMultiMasterFontDictionary* dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 302 | if (dict == NULL) { |
| 303 | return NULL; // default one? |
| 304 | } |
| 305 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 306 | return new SkPdfMultiMasterFont(doc, dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 307 | } |
| 308 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 309 | static int skstoi(const SkPdfObject* str) { |
| 310 | // TODO(edisonn): report err of it is not a (hex) string |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 311 | int ret = 0; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 312 | for (unsigned int i = 0 ; i < str->len(); i++) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 313 | ret = (ret << 8) + ((unsigned char*)str->c_str())[i]; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 314 | } |
| 315 | return ret; |
| 316 | } |
| 317 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 318 | #define tokenIsKeyword(token,keyword) (token.fType == kKeyword_TokenType && token.fKeywordLength==sizeof(keyword)-1 && strncmp(token.fKeyword, keyword, sizeof(keyword)-1) == 0) |
| 319 | |
| 320 | SkPdfToUnicode::SkPdfToUnicode(SkNativeParsedPDF* parsed, SkPdfStream* stream) : fParsed(parsed) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 321 | fCMapEncoding = NULL; |
| 322 | fCMapEncodingFlag = NULL; |
| 323 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 324 | if (stream) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 325 | SkPdfNativeTokenizer* tokenizer = fParsed->tokenizerOfStream(stream); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 326 | PdfToken token; |
| 327 | |
| 328 | fCMapEncoding = new unsigned short[256 * 256]; |
| 329 | fCMapEncodingFlag = new unsigned char[256 * 256]; |
| 330 | for (int i = 0 ; i < 256 * 256; i++) { |
| 331 | fCMapEncoding[i] = i; |
| 332 | fCMapEncodingFlag[i] = 0; |
| 333 | } |
| 334 | |
| 335 | // TODO(edisonn): deal with multibyte character, or longer strings. |
| 336 | // Ritght now we deal with up 2 characters, e.g. <0020> but not longer like <00660066006C> |
| 337 | //2 beginbfrange |
| 338 | //<0000> <005E> <0020> |
| 339 | //<005F> <0061> [<00660066> <00660069> <00660066006C>] |
| 340 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 341 | while (tokenizer->readToken(&token)) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 342 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 343 | // TODO(edisonn): perf, macro that would make equal first for token.fKeywordLength with sizeof(keyword), instead od strlen, make sure it is keyword, not a char* |
| 344 | if (tokenIsKeyword(token, "begincodespacerange")) { |
| 345 | while (tokenizer->readToken(&token) && !tokenIsKeyword(token, "endcodespacerange")) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 346 | // tokenizer->PutBack(token); |
| 347 | // tokenizer->readToken(&token); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 348 | // TODO(edisonn): check token type! ignore/report errors. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 349 | int start = skstoi(token.fObject); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 350 | tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 351 | int end = skstoi(token.fObject); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 352 | for (int i = start; i <= end; i++) { |
| 353 | fCMapEncodingFlag[i] |= 1; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 358 | if (tokenIsKeyword(token, "beginbfchar")) { |
| 359 | while (tokenizer->readToken(&token) && !tokenIsKeyword(token, "endbfchar")) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 360 | // tokenizer->PutBack(token); |
| 361 | // tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 362 | int from = skstoi(token.fObject); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 363 | tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 364 | int to = skstoi(token.fObject); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 365 | |
| 366 | fCMapEncodingFlag[from] |= 2; |
| 367 | fCMapEncoding[from] = to; |
| 368 | } |
| 369 | } |
| 370 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 371 | if (tokenIsKeyword(token, "beginbfrange")) { |
| 372 | while (tokenizer->readToken(&token) && !tokenIsKeyword(token, "endbfrange")) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 373 | // tokenizer->PutBack(token); |
| 374 | // tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 375 | int start = skstoi(token.fObject); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 376 | tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 377 | int end = skstoi(token.fObject); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 378 | |
| 379 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 380 | tokenizer->readToken(&token); // [ or just an array directly? |
| 381 | // tokenizer->PutBack(token); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 382 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 383 | // TODO(edisonn): read spec: any string or only hex string? |
| 384 | if (token.fType == kObject_TokenType && token.fObject->isAnyString()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 385 | // tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 386 | int value = skstoi(token.fObject); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 387 | |
| 388 | for (int i = start; i <= end; i++) { |
| 389 | fCMapEncodingFlag[i] |= 2; |
| 390 | fCMapEncoding[i] = value; |
| 391 | value++; |
| 392 | // if i != end, verify last byte id not if, ignore/report error |
| 393 | } |
| 394 | |
| 395 | // read one string |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 396 | } else if (token.fType == kObject_TokenType && token.fObject->isArray()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 397 | // tokenizer->readToken(&token); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 398 | for (unsigned int i = 0; i < token.fObject->size(); i++) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 399 | fCMapEncodingFlag[start + i] |= 2; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 400 | fCMapEncoding[start + i] = skstoi((*token.fObject)[i]); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 401 | } |
| 402 | // read array |
| 403 | } |
| 404 | |
| 405 | //fCMapEncodingFlag[from] = 1; |
| 406 | //fCMapEncoding[from] = to; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 412 | |
| 413 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 414 | SkPdfType0Font::SkPdfType0Font(SkNativeParsedPDF* doc, SkPdfType0FontDictionary* dict) { |
| 415 | fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str()); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 416 | fEncoding = NULL; |
| 417 | |
| 418 | if (dict->has_Encoding()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 419 | if (dict->isEncodingAName(doc)) { |
| 420 | fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(doc).c_str()); |
| 421 | } else if (dict->isEncodingAStream(doc)) { |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 422 | //fEncoding = loadEncodingFromStream(dict->getEncodingAsStream()); |
| 423 | } else { |
| 424 | // TODO(edisonn): error ... warning .. assert? |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (dict->has_ToUnicode()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 429 | fToUnicode = new SkPdfToUnicode(doc, dict->ToUnicode(doc)); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | std::map<std::string, SkPdfEncoding*>& getStandardEncodings() { |
| 434 | static std::map<std::string, SkPdfEncoding*> encodings; |
| 435 | if (encodings.empty()) { |
| 436 | encodings["Identity-H"] = SkPdfIdentityHEncoding::instance(); |
| 437 | } |
| 438 | |
| 439 | return encodings; |
| 440 | } |
| 441 | |
| 442 | |
| 443 | SkPdfEncoding* SkPdfEncoding::fromName(const char* name) { |
| 444 | SkPdfEncoding* encoding = getStandardEncodings()[name]; |
| 445 | |
| 446 | #ifdef PDF_TRACE |
| 447 | if (encoding == NULL) { |
| 448 | printf("Encoding not found: %s\n", name); |
| 449 | } |
| 450 | #endif |
| 451 | return encoding; |
| 452 | } |