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