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