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