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