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(); |
edisonn@google.com | e878e72 | 2013-07-29 19:10:58 +0000 | [diff] [blame] | 50 | len = obj->lenstr(); |
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 | |
edisonn@google.com | 7ee12ca | 2013-08-05 18:18:06 +0000 | [diff] [blame] | 167 | virtual ~SkPdfFont() { |
| 168 | // TODO(edisonn): NYI (will leak for now) |
| 169 | } |
| 170 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 171 | const SkPdfEncoding* encoding() const {return fEncoding;} |
| 172 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 173 | void drawText(const SkDecodedText& text, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 174 | for (int i = 0 ; i < text.size(); i++) { |
edisonn@google.com | e57c62d | 2013-08-07 18:04:15 +0000 | [diff] [blame] | 175 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm); |
| 176 | #ifdef PDF_TRACE |
| 177 | SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(0)); |
| 178 | pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1); |
| 179 | printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()), SkScalarToDouble(point.y())); |
| 180 | #endif // PDF_TRACE |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 181 | double width = drawOneChar(text[i], paint, pdfContext, canvas); |
| 182 | pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(width), SkDoubleToScalar(0.0)); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 186 | void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { |
| 187 | if (fToUnicode) { |
| 188 | textOut->text = new uint16_t[textIn.len]; |
| 189 | textOut->len = textIn.len; |
| 190 | for (int i = 0; i < textIn.len; i++) { |
| 191 | textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]]; |
| 192 | } |
| 193 | } else { |
| 194 | textOut->text = textIn.text; |
| 195 | textOut->len = textIn.len; |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | inline unsigned int ToUnicode(unsigned int ch) const { |
| 200 | if (fToUnicode) { |
| 201 | return fToUnicode->fCMapEncoding[ch]; |
| 202 | } else { |
| 203 | return ch; |
| 204 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 207 | static SkPdfFont* fontFromPdfDictionary(SkNativeParsedPDF* doc, SkPdfFontDictionary* dict); |
| 208 | static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman");} |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 209 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 210 | static SkPdfType0Font* fontFromType0FontDictionary(SkNativeParsedPDF* doc, SkPdfType0FontDictionary* dict); |
| 211 | static SkPdfType1Font* fontFromType1FontDictionary(SkNativeParsedPDF* doc, SkPdfType1FontDictionary* dict); |
| 212 | static SkPdfType3Font* fontFromType3FontDictionary(SkNativeParsedPDF* doc, SkPdfType3FontDictionary* dict); |
| 213 | static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkNativeParsedPDF* doc, SkPdfTrueTypeFontDictionary* dict); |
| 214 | static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(SkNativeParsedPDF* doc, SkPdfMultiMasterFontDictionary* dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 215 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 216 | static SkPdfFont* fontFromFontDescriptor(SkNativeParsedPDF* doc, SkPdfFontDescriptorDictionary* fd, bool loadFromName = true); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 217 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 218 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 219 | 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] | 220 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 221 | |
| 222 | private: |
| 223 | static SkPdfFont* fontFromPdfDictionaryOnce(SkNativeParsedPDF* doc, SkPdfFontDictionary* dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | class SkPdfStandardFont : public SkPdfFont { |
| 227 | SkTypeface* fTypeface; |
| 228 | |
| 229 | public: |
| 230 | SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {} |
| 231 | |
| 232 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 233 | 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] | 234 | paint->setTypeface(fTypeface); |
| 235 | paint->setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 236 | |
| 237 | unsigned long ch4 = ch; |
| 238 | char utf8[10]; |
| 239 | int len = SkUTF8_FromUnichar(ch4, utf8); |
| 240 | |
| 241 | canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *paint); |
| 242 | |
| 243 | SkScalar textWidth = paint->measureText(utf8, len); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 244 | return SkScalarToDouble(textWidth); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 245 | } |
| 246 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 247 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} |
| 248 | }; |
| 249 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 250 | class SkPdfType0Font : public SkPdfFont { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 251 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 252 | SkPdfType0Font(SkNativeParsedPDF* doc, SkPdfType0FontDictionary* dict); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 253 | |
| 254 | public: |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 255 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 256 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
| 257 | return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 261 | } |
| 262 | }; |
| 263 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 264 | class SkPdfType1Font : public SkPdfFont { |
| 265 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 266 | SkPdfType1Font(SkNativeParsedPDF* doc, SkPdfType1FontDictionary* dict) { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 267 | if (dict->has_FontDescriptor()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 268 | fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescriptor(doc)); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 269 | } else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 270 | fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str()); |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 271 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 272 | } |
| 273 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 274 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 275 | virtual double drawOneChar(unsigned int ch, SkPaint* paint, PdfContext* pdfContext, SkCanvas* canvas) { |
| 276 | return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 280 | |
| 281 | } |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 282 | }; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 283 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 284 | class SkPdfTrueTypeFont : public SkPdfType1Font { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 285 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 286 | SkPdfTrueTypeFont(SkNativeParsedPDF* doc, SkPdfTrueTypeFontDictionary* dict) : SkPdfType1Font(doc, dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 287 | } |
| 288 | }; |
| 289 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 290 | class SkPdfMultiMasterFont : public SkPdfType1Font { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 291 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 292 | SkPdfMultiMasterFont(SkNativeParsedPDF* doc, SkPdfMultiMasterFontDictionary* dict) : SkPdfType1Font(doc, dict) { |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 293 | } |
| 294 | }; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 295 | /* |
| 296 | class CIDToGIDMap { |
| 297 | virtual unsigned int map(unsigned int cid) = 0; |
| 298 | static CIDToGIDMap* fromName(const char* name); |
| 299 | }; |
| 300 | |
| 301 | class CIDToGIDMap_Identity { |
| 302 | virtual unsigned int map(unsigned int cid) { return cid; } |
| 303 | |
| 304 | static CIDToGIDMap_Identity* instance() { |
| 305 | static CIDToGIDMap_Identity* inst = new CIDToGIDMap_Identity(); |
| 306 | return inst; |
| 307 | } |
| 308 | }; |
| 309 | |
| 310 | CIDToGIDMap* CIDToGIDMap::fromName(const char* name) { |
| 311 | // The only one supported right now is Identity |
| 312 | if (strcmp(name, "Identity") == 0) { |
| 313 | return CIDToGIDMap_Identity::instance(); |
| 314 | } |
| 315 | |
| 316 | #ifdef PDF_TRACE |
| 317 | // TODO(edisonn): warning/report |
| 318 | printf("Unknown CIDToGIDMap: %s\n", name); |
| 319 | #endif |
| 320 | return NULL; |
| 321 | } |
| 322 | CIDToGIDMap* fCidToGid; |
| 323 | */ |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 324 | |
| 325 | class SkPdfType3Font : public SkPdfFont { |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 326 | struct Type3FontChar { |
edisonn@google.com | b0145ce | 2013-08-05 16:23:23 +0000 | [diff] [blame] | 327 | SkPdfObject* fObj; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 328 | double fWidth; |
| 329 | }; |
| 330 | |
| 331 | SkPdfDictionary* fCharProcs; |
| 332 | SkPdfEncodingDictionary* fEncodingDict; |
| 333 | unsigned int fFirstChar; |
| 334 | unsigned int fLastChar; |
| 335 | |
| 336 | SkRect fFontBBox; |
| 337 | SkMatrix fFonMatrix; |
| 338 | |
| 339 | Type3FontChar* fChars; |
| 340 | |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 341 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 342 | SkPdfType3Font(SkNativeParsedPDF* parsed, SkPdfType3FontDictionary* dict) { |
| 343 | fBaseFont = fontFromName(parsed, dict, dict->BaseFont(parsed).c_str()); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 344 | |
| 345 | if (dict->has_Encoding()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 346 | if (dict->isEncodingAName(parsed)) { |
| 347 | fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(parsed).c_str()); |
| 348 | } else if (dict->isEncodingAEncodingdictionary(parsed)) { |
edisonn@google.com | 2fd5d36 | 2013-07-23 19:43:48 +0000 | [diff] [blame] | 349 | // No encoding. |
| 350 | fEncoding = SkPdfDefaultEncoding::instance(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 351 | fEncodingDict = dict->getEncodingAsEncodingdictionary(parsed); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | // null? |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 356 | fCharProcs = dict->CharProcs(parsed); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 357 | |
| 358 | fToUnicode = NULL; |
| 359 | if (dict->has_ToUnicode()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 360 | fToUnicode = new SkPdfToUnicode(parsed, dict->ToUnicode(parsed)); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 361 | } |
| 362 | |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 363 | fFirstChar = (unsigned int)dict->FirstChar(parsed); |
| 364 | fLastChar = (unsigned int)dict->LastChar(parsed); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 365 | fFonMatrix = dict->has_FontMatrix() ? dict->FontMatrix(parsed) : SkMatrix::I(); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 366 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 367 | if (dict->has_FontBBox()) { |
| 368 | fFontBBox = dict->FontBBox(parsed); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | fChars = new Type3FontChar[fLastChar - fFirstChar + 1]; |
| 372 | |
| 373 | memset(fChars, 0, sizeof(fChars[0]) * (fLastChar - fFirstChar + 1)); |
| 374 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 375 | const SkPdfArray* widths = dict->Widths(parsed); |
| 376 | for (unsigned int i = 0 ; i < widths->size(); i++) { |
edisonn@google.com | ac4bedc | 2013-07-25 21:40:23 +0000 | [diff] [blame] | 377 | if ((fFirstChar + i) >= fFirstChar && (fFirstChar + i) <= fLastChar) { |
| 378 | fChars[i].fWidth = (*widths)[i]->numberValue(); |
| 379 | } else { |
| 380 | // TODO(edisonn): report pdf corruption |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 381 | } |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 382 | } |
| 383 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 384 | const SkPdfArray* diffs = fEncodingDict->Differences(parsed); |
| 385 | unsigned int j = fFirstChar; |
| 386 | for (unsigned int i = 0 ; i < diffs->size(); i++) { |
| 387 | if ((*diffs)[i]->isInteger()) { |
edisonn@google.com | a3356fc | 2013-07-10 18:20:06 +0000 | [diff] [blame] | 388 | j = (unsigned int)(*diffs)[i]->intValue(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 389 | } else if ((*diffs)[i]->isName()) { |
edisonn@google.com | ac4bedc | 2013-07-25 21:40:23 +0000 | [diff] [blame] | 390 | if (j >= fFirstChar && j <= fLastChar) { |
| 391 | fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]); |
| 392 | } else { |
| 393 | // TODO(edisonn): report pdf corruption |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 394 | } |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 395 | j++; |
| 396 | } else { |
edisonn@google.com | ac4bedc | 2013-07-25 21:40:23 +0000 | [diff] [blame] | 397 | // TODO(edisonn): report bad pdf |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | public: |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 403 | 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] | 404 | if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) { |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 405 | return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 406 | } |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 407 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 408 | #ifdef PDF_TRACE |
| 409 | printf("Type 3 char to unicode: %c\n", ToUnicode(ch)); |
| 410 | if (ToUnicode(ch) == 'A') { |
| 411 | printf("break;\n"); |
| 412 | } |
| 413 | #endif |
| 414 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 415 | // TODO(edisonn): is it better to resolve the reference at load time, or now? |
| 416 | 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] | 417 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 418 | // TODO(edisonn): verify/test translate code, not tested yet |
| 419 | pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize * fChars[ch - fFirstChar].fWidth), |
| 420 | SkDoubleToScalar(0.0)); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 421 | return fChars[ch - fFirstChar].fWidth; |
edisonn@google.com | 1be794f | 2013-06-21 21:43:09 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 425 | |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | #endif // __DEFINED__SkPdfFont |