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