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