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