edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 1 | #ifndef __DEFINED__SkPdfFont |
| 2 | #define __DEFINED__SkPdfFont |
| 3 | |
| 4 | #include "SkPdfHeaders_autogen.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 5 | #include "SkPdfMapper_autogen.h" |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 10 | #include "SkTypeface.h" |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 11 | #include "SkUtils.h" |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 12 | #include "SkPdfBasics.h" |
| 13 | #include "SkPdfUtils.h" |
| 14 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 15 | |
| 16 | class SkPdfType0Font; |
| 17 | class SkPdfType1Font; |
| 18 | class SkPdfType3Font; |
| 19 | class SkPdfTrueTypeFont; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 20 | class SkPdfMultiMasterFont; |
| 21 | class SkPdfFont; |
| 22 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 23 | struct SkPdfStandardFontEntry { |
edisonn@google.com | 596d2e2 | 2013-07-10 17:44:55 +0000 | [diff] [blame] | 24 | // We don't own this pointer! |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 25 | const char* fName; |
| 26 | bool fIsBold; |
| 27 | bool fIsItalic; |
edisonn@google.com | 596d2e2 | 2013-07-10 17:44:55 +0000 | [diff] [blame] | 28 | SkPdfStandardFontEntry() |
| 29 | : fName(NULL), |
| 30 | fIsBold(false), |
| 31 | fIsItalic(false) {} |
| 32 | |
| 33 | SkPdfStandardFontEntry(const char* name, bool bold, bool italic) |
| 34 | : fName(name), |
| 35 | fIsBold(bold), |
| 36 | fIsItalic(italic) {} |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts(); |
| 40 | SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 41 | SkPdfFont* fontFromName(SkNativeParsedPDF* doc, SkPdfObject* obj, const char* fontName); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 42 | |
| 43 | struct SkUnencodedText { |
| 44 | void* text; |
| 45 | int len; |
| 46 | |
| 47 | public: |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 48 | SkUnencodedText(const SkPdfString* obj) { |
| 49 | text = (void*)obj->c_str(); |
| 50 | len = obj->len(); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 51 | } |
| 52 | }; |
| 53 | |
| 54 | struct SkDecodedText { |
| 55 | uint16_t* text; |
| 56 | int len; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 57 | public: |
| 58 | unsigned int operator[](int i) const { return text[i]; } |
| 59 | int size() const { return len; } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct SkUnicodeText { |
| 63 | uint16_t* text; |
| 64 | int len; |
| 65 | |
| 66 | public: |
| 67 | unsigned int operator[](int i) const { return text[i]; } |
| 68 | int size() const { return len; } |
| 69 | }; |
| 70 | |
| 71 | class SkPdfEncoding { |
| 72 | public: |
| 73 | virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const = 0; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 74 | static SkPdfEncoding* fromName(const char* name); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 77 | std::map<std::string, SkPdfEncoding*>& getStandardEncodings(); |
| 78 | |
| 79 | class SkPdfToUnicode { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 80 | SkNativeParsedPDF* fParsed; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 81 | // TODO(edisonn): hide public members |
| 82 | public: |
| 83 | unsigned short* fCMapEncoding; |
| 84 | unsigned char* fCMapEncodingFlag; |
| 85 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 86 | SkPdfToUnicode(SkNativeParsedPDF* parsed, SkPdfStream* stream); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 90 | class SkPdfIdentityHEncoding : public SkPdfEncoding { |
| 91 | public: |
| 92 | virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const { |
| 93 | // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error? |
| 94 | |
| 95 | uint16_t* text = (uint16_t*)textIn.text; |
| 96 | textOut->text = new uint16_t[textIn.len / 2]; |
| 97 | textOut->len = textIn.len / 2; |
| 98 | |
| 99 | for (int i = 0; i < textOut->len; i++) { |
| 100 | textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff); |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static SkPdfIdentityHEncoding* instance() { |
| 107 | static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding(); |
| 108 | return inst; |
| 109 | } |
| 110 | }; |
| 111 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 112 | // TODO(edisonn): using this one when no encoding is specified |
| 113 | class SkPdfDefaultEncoding : public SkPdfEncoding { |
| 114 | public: |
| 115 | virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const { |
| 116 | // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error? |
| 117 | |
| 118 | unsigned char* text = (unsigned char*)textIn.text; |
| 119 | textOut->text = new uint16_t[textIn.len]; |
| 120 | textOut->len = textIn.len; |
| 121 | |
| 122 | for (int i = 0; i < textOut->len; i++) { |
| 123 | textOut->text[i] = text[i]; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | static SkPdfDefaultEncoding* instance() { |
| 130 | static SkPdfDefaultEncoding* inst = new SkPdfDefaultEncoding(); |
| 131 | return inst; |
| 132 | } |
| 133 | }; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 134 | |
| 135 | class SkPdfCIDToGIDMapIdentityEncoding : public SkPdfEncoding { |
| 136 | public: |
| 137 | virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const { |
| 138 | // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error? |
| 139 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 140 | uint16_t* text = (uint16_t*)textIn.text; |
| 141 | textOut->text = new uint16_t[textIn.len / 2]; |
| 142 | textOut->len = textIn.len / 2; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 143 | |
| 144 | for (int i = 0; i < textOut->len; i++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 145 | textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | static SkPdfCIDToGIDMapIdentityEncoding* instance() { |
| 152 | static SkPdfCIDToGIDMapIdentityEncoding* inst = new SkPdfCIDToGIDMapIdentityEncoding(); |
| 153 | return inst; |
| 154 | } |
| 155 | }; |
| 156 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 157 | class SkPdfFont { |
| 158 | public: |
| 159 | SkPdfFont* fBaseFont; |
| 160 | SkPdfEncoding* fEncoding; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 161 | SkPdfToUnicode* fToUnicode; |
| 162 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 163 | |
| 164 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 165 | SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()), fToUnicode(NULL) {} |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 166 | |
| 167 | const SkPdfEncoding* encoding() const {return fEncoding;} |
| 168 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 169 | void drawText(const SkDecodedText& text, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 170 | for (int i = 0 ; i < text.size(); i++) { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 171 | double width = drawOneChar(text[i], paint, pdfContext, canvas); |
| 172 | pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(width), SkDoubleToScalar(0.0)); |
| 173 | canvas->translate(SkDoubleToScalar(width), SkDoubleToScalar(0.0)); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 177 | void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { |
| 178 | if (fToUnicode) { |
| 179 | textOut->text = new uint16_t[textIn.len]; |
| 180 | textOut->len = textIn.len; |
| 181 | for (int i = 0; i < textIn.len; i++) { |
| 182 | textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]]; |
| 183 | } |
| 184 | } else { |
| 185 | textOut->text = textIn.text; |
| 186 | textOut->len = textIn.len; |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | inline unsigned int ToUnicode(unsigned int ch) const { |
| 191 | if (fToUnicode) { |
| 192 | return fToUnicode->fCMapEncoding[ch]; |
| 193 | } else { |
| 194 | return ch; |
| 195 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 198 | static SkPdfFont* fontFromPdfDictionary(SkNativeParsedPDF* doc, SkPdfFontDictionary* dict); |
| 199 | static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman");} |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 200 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 201 | static SkPdfType0Font* fontFromType0FontDictionary(SkNativeParsedPDF* doc, SkPdfType0FontDictionary* dict); |
| 202 | static SkPdfType1Font* fontFromType1FontDictionary(SkNativeParsedPDF* doc, SkPdfType1FontDictionary* dict); |
| 203 | static SkPdfType3Font* fontFromType3FontDictionary(SkNativeParsedPDF* doc, SkPdfType3FontDictionary* dict); |
| 204 | static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkNativeParsedPDF* doc, SkPdfTrueTypeFontDictionary* dict); |
| 205 | static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(SkNativeParsedPDF* doc, SkPdfMultiMasterFontDictionary* dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 206 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 207 | static SkPdfFont* fontFromFontDescriptor(SkNativeParsedPDF* doc, SkPdfFontDescriptorDictionary* fd, bool loadFromName = true); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 208 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 209 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 210 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) = 0; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 211 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 212 | |
| 213 | private: |
| 214 | static SkPdfFont* fontFromPdfDictionaryOnce(SkNativeParsedPDF* doc, SkPdfFontDictionary* dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | class SkPdfStandardFont : public SkPdfFont { |
| 218 | SkTypeface* fTypeface; |
| 219 | |
| 220 | public: |
| 221 | SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {} |
| 222 | |
| 223 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 224 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 225 | paint->setTypeface(fTypeface); |
| 226 | paint->setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 227 | |
| 228 | unsigned long ch4 = ch; |
| 229 | char utf8[10]; |
| 230 | int len = SkUTF8_FromUnichar(ch4, utf8); |
| 231 | |
| 232 | canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *paint); |
| 233 | |
| 234 | SkScalar textWidth = paint->measureText(utf8, len); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 235 | return SkScalarToDouble(textWidth); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 236 | } |
| 237 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 238 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} |
| 239 | }; |
| 240 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 241 | class SkPdfType0Font : public SkPdfFont { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 242 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 243 | SkPdfType0Font(SkNativeParsedPDF* doc, SkPdfType0FontDictionary* dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 244 | |
| 245 | public: |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 246 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 247 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
| 248 | return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 252 | } |
| 253 | }; |
| 254 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 255 | class SkPdfType1Font : public SkPdfFont { |
| 256 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 257 | SkPdfType1Font(SkNativeParsedPDF* doc, SkPdfType1FontDictionary* dict) { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 258 | if (dict->has_FontDescriptor()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 259 | fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescriptor(doc)); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 260 | } else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 261 | fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str()); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 262 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 263 | } |
| 264 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 265 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 266 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
| 267 | return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 271 | |
| 272 | } |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 273 | }; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 274 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 275 | class SkPdfTrueTypeFont : public SkPdfType1Font { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 276 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 277 | SkPdfTrueTypeFont(SkNativeParsedPDF* doc, SkPdfTrueTypeFontDictionary* dict) : SkPdfType1Font(doc, dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 278 | } |
| 279 | }; |
| 280 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 281 | class SkPdfMultiMasterFont : public SkPdfType1Font { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 282 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 283 | SkPdfMultiMasterFont(SkNativeParsedPDF* doc, SkPdfMultiMasterFontDictionary* dict) : SkPdfType1Font(doc, dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 284 | } |
| 285 | }; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 286 | /* |
| 287 | class CIDToGIDMap { |
| 288 | virtual unsigned int map(unsigned int cid) = 0; |
| 289 | static CIDToGIDMap* fromName(const char* name); |
| 290 | }; |
| 291 | |
| 292 | class CIDToGIDMap_Identity { |
| 293 | virtual unsigned int map(unsigned int cid) { return cid; } |
| 294 | |
| 295 | static CIDToGIDMap_Identity* instance() { |
| 296 | static CIDToGIDMap_Identity* inst = new CIDToGIDMap_Identity(); |
| 297 | return inst; |
| 298 | } |
| 299 | }; |
| 300 | |
| 301 | CIDToGIDMap* CIDToGIDMap::fromName(const char* name) { |
| 302 | // The only one supported right now is Identity |
| 303 | if (strcmp(name, "Identity") == 0) { |
| 304 | return CIDToGIDMap_Identity::instance(); |
| 305 | } |
| 306 | |
| 307 | #ifdef PDF_TRACE |
| 308 | // TODO(edisonn): warning/report |
| 309 | printf("Unknown CIDToGIDMap: %s\n", name); |
| 310 | #endif |
| 311 | return NULL; |
| 312 | } |
| 313 | CIDToGIDMap* fCidToGid; |
| 314 | */ |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 315 | |
| 316 | class SkPdfType3Font : public SkPdfFont { |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 317 | struct Type3FontChar { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 318 | const SkPdfObject* fObj; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 319 | double fWidth; |
| 320 | }; |
| 321 | |
| 322 | SkPdfDictionary* fCharProcs; |
| 323 | SkPdfEncodingDictionary* fEncodingDict; |
| 324 | unsigned int fFirstChar; |
| 325 | unsigned int fLastChar; |
| 326 | |
| 327 | SkRect fFontBBox; |
| 328 | SkMatrix fFonMatrix; |
| 329 | |
| 330 | Type3FontChar* fChars; |
| 331 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 332 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 333 | SkPdfType3Font(SkNativeParsedPDF* parsed, SkPdfType3FontDictionary* dict) { |
| 334 | fBaseFont = fontFromName(parsed, dict, dict->BaseFont(parsed).c_str()); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 335 | |
| 336 | if (dict->has_Encoding()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 337 | if (dict->isEncodingAName(parsed)) { |
| 338 | fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(parsed).c_str()); |
| 339 | } else if (dict->isEncodingAEncodingdictionary(parsed)) { |
edisonn@google.com | 2fd5d36 | 2013-07-23 19:43:48 +0000 | [diff] [blame^] | 340 | // No encoding. |
| 341 | fEncoding = SkPdfDefaultEncoding::instance(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 342 | fEncodingDict = dict->getEncodingAsEncodingdictionary(parsed); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | // null? |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 347 | fCharProcs = dict->CharProcs(parsed); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 348 | |
| 349 | fToUnicode = NULL; |
| 350 | if (dict->has_ToUnicode()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 351 | fToUnicode = new SkPdfToUnicode(parsed, dict->ToUnicode(parsed)); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 352 | } |
| 353 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 354 | fFirstChar = (unsigned int)dict->FirstChar(parsed); |
| 355 | fLastChar = (unsigned int)dict->LastChar(parsed); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 356 | fFonMatrix = dict->has_FontMatrix() ? dict->FontMatrix(parsed) : SkMatrix::I(); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 357 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 358 | if (dict->has_FontBBox()) { |
| 359 | fFontBBox = dict->FontBBox(parsed); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | fChars = new Type3FontChar[fLastChar - fFirstChar + 1]; |
| 363 | |
| 364 | memset(fChars, 0, sizeof(fChars[0]) * (fLastChar - fFirstChar + 1)); |
| 365 | |
| 366 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 367 | const SkPdfArray* widths = dict->Widths(parsed); |
| 368 | for (unsigned int i = 0 ; i < widths->size(); i++) { |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 369 | if ((fFirstChar + i) < fFirstChar || (fFirstChar + i) > fLastChar) { |
| 370 | printf("break; error 1\n"); |
| 371 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 372 | fChars[i].fWidth = (*widths)[i]->numberValue(); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 373 | } |
| 374 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 375 | const SkPdfArray* diffs = fEncodingDict->Differences(parsed); |
| 376 | unsigned int j = fFirstChar; |
| 377 | for (unsigned int i = 0 ; i < diffs->size(); i++) { |
| 378 | if ((*diffs)[i]->isInteger()) { |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 379 | j = (unsigned int)(*diffs)[i]->intValue(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 380 | } else if ((*diffs)[i]->isName()) { |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 381 | if (j < fFirstChar || j > fLastChar) { |
| 382 | printf("break; error 2\n"); |
| 383 | } |
edisonn@google.com | 2fd5d36 | 2013-07-23 19:43:48 +0000 | [diff] [blame^] | 384 | fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 385 | j++; |
| 386 | } else { |
| 387 | // err |
| 388 | } |
| 389 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 393 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 394 | if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 395 | return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 396 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 397 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 398 | #ifdef PDF_TRACE |
| 399 | printf("Type 3 char to unicode: %c\n", ToUnicode(ch)); |
| 400 | if (ToUnicode(ch) == 'A') { |
| 401 | printf("break;\n"); |
| 402 | } |
| 403 | #endif |
| 404 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 405 | // TODO(edisonn): is it better to resolve the reference at load time, or now? |
| 406 | doType3Char(pdfContext, canvas, pdfContext->fPdfDoc->resolveReference(fChars[ch - fFirstChar].fObj), fFontBBox, fFonMatrix, pdfContext->fGraphicsState.fCurFontSize); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 407 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 408 | // TODO(edisonn): verify/test translate code, not tested yet |
| 409 | pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize * fChars[ch - fFirstChar].fWidth), |
| 410 | SkDoubleToScalar(0.0)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 411 | return fChars[ch - fFirstChar].fWidth; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 415 | |
| 416 | } |
| 417 | }; |
| 418 | |
| 419 | #endif // __DEFINED__SkPdfFont |