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" |
| 5 | #include "SkPdfPodofoMapper_autogen.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
| 10 | #include "SkUtils.h" |
| 11 | |
| 12 | class SkPdfType0Font; |
| 13 | class SkPdfType1Font; |
| 14 | class SkPdfType3Font; |
| 15 | class SkPdfTrueTypeFont; |
| 16 | class SkPdfCIDFont; |
| 17 | class SkPdfMultiMasterFont; |
| 18 | class SkPdfFont; |
| 19 | |
| 20 | |
| 21 | struct SkPdfStandardFontEntry { |
| 22 | const char* fName; |
| 23 | bool fIsBold; |
| 24 | bool fIsItalic; |
| 25 | }; |
| 26 | |
| 27 | std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts(); |
| 28 | SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic); |
| 29 | SkPdfFont* SkPdfFontFromName(SkPdfObject* obj, const char* fontName); |
| 30 | |
| 31 | struct SkUnencodedText { |
| 32 | void* text; |
| 33 | int len; |
| 34 | |
| 35 | public: |
| 36 | SkUnencodedText(const SkPdfObject* obj) { |
| 37 | text = (void*)obj->podofo()->GetString().GetString(); |
| 38 | len = obj->podofo()->GetString().GetLength(); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | struct SkDecodedText { |
| 43 | uint16_t* text; |
| 44 | int len; |
| 45 | }; |
| 46 | |
| 47 | struct SkUnicodeText { |
| 48 | uint16_t* text; |
| 49 | int len; |
| 50 | |
| 51 | public: |
| 52 | unsigned int operator[](int i) const { return text[i]; } |
| 53 | int size() const { return len; } |
| 54 | }; |
| 55 | |
| 56 | class SkPdfEncoding { |
| 57 | public: |
| 58 | virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const = 0; |
| 59 | }; |
| 60 | |
| 61 | class SkPdfIdentityHEncoding : public SkPdfEncoding { |
| 62 | public: |
| 63 | virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const { |
| 64 | // TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error? |
| 65 | |
| 66 | uint16_t* text = (uint16_t*)textIn.text; |
| 67 | textOut->text = new uint16_t[textIn.len / 2]; |
| 68 | textOut->len = textIn.len / 2; |
| 69 | |
| 70 | for (int i = 0; i < textOut->len; i++) { |
| 71 | textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff); |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | static SkPdfIdentityHEncoding* instance() { |
| 78 | static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding(); |
| 79 | return inst; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | class SkPdfFont { |
| 84 | public: |
| 85 | SkPdfFont* fBaseFont; |
| 86 | SkPdfEncoding* fEncoding; |
| 87 | |
| 88 | public: |
| 89 | SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfIdentityHEncoding::instance()) {} |
| 90 | |
| 91 | const SkPdfEncoding* encoding() const {return fEncoding;} |
| 92 | |
| 93 | void drawText(const SkUnicodeText& text, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 94 | for (int i = 0 ; i < text.size(); i++) { |
| 95 | drawOneChar(text[i], paint, canvas, matrix); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | virtual void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { |
| 100 | textOut->text = textIn.text; |
| 101 | textOut->len = textIn.len; |
| 102 | }; |
| 103 | |
| 104 | static SkPdfFont* fontFromPdfDictionary(SkPdfFontDictionary* dict); |
| 105 | static SkPdfFont* Default() {return SkPdfFontFromName(NULL, "TimesNewRoman");} |
| 106 | |
| 107 | static SkPdfType0Font* fontFromType0FontDictionary(SkPdfType0FontDictionary* dict); |
| 108 | static SkPdfType1Font* fontFromType1FontDictionary(SkPdfType1FontDictionary* dict); |
| 109 | static SkPdfType3Font* fontFromType3FontDictionary(SkPdfType3FontDictionary* dict); |
| 110 | static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfTrueTypeFontDictionary* dict); |
| 111 | static SkPdfCIDFont* fontFromCIDFontDictionary(SkPdfCIDFontDictionary* dict); |
| 112 | static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(SkPdfMultiMasterFontDictionary* dict); |
| 113 | |
| 114 | public: |
| 115 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) = 0; |
| 116 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) = 0; |
| 117 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0; |
| 118 | }; |
| 119 | |
| 120 | class SkPdfStandardFont : public SkPdfFont { |
| 121 | SkTypeface* fTypeface; |
| 122 | |
| 123 | public: |
| 124 | SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {} |
| 125 | |
| 126 | public: |
| 127 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 128 | paint->setTypeface(fTypeface); |
| 129 | paint->setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 130 | |
| 131 | unsigned long ch4 = ch; |
| 132 | char utf8[10]; |
| 133 | int len = SkUTF8_FromUnichar(ch4, utf8); |
| 134 | |
| 135 | canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *paint); |
| 136 | |
| 137 | SkScalar textWidth = paint->measureText(utf8, len); |
| 138 | matrix->preTranslate(textWidth, SkDoubleToScalar(0.0)); |
| 139 | } |
| 140 | |
| 141 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) {} |
| 142 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | class SkPdfType0Font : public SkPdfFont { |
| 147 | unsigned short* fCMapEncoding; |
| 148 | unsigned char* fCMapEncodingFlag; |
| 149 | public: |
| 150 | SkPdfType0Font(SkPdfType0FontDictionary* dict); |
| 151 | |
| 152 | public: |
| 153 | virtual void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { |
| 154 | textOut->text = new uint16_t[textIn.len]; |
| 155 | textOut->len = textIn.len; |
| 156 | for (int i = 0; i < textIn.len; i++) { |
| 157 | textOut->text[i] = fCMapEncoding[textIn.text[i]]; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 162 | fBaseFont->drawOneChar(ch, paint, canvas, matrix); |
| 163 | } |
| 164 | |
| 165 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { |
| 166 | |
| 167 | } |
| 168 | |
| 169 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 170 | |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | class SkPdfTrueTypeFont : public SkPdfFont { |
| 175 | public: |
| 176 | SkPdfTrueTypeFont(SkPdfTrueTypeFontDictionary* dict) { |
| 177 | fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); |
| 178 | } |
| 179 | |
| 180 | public: |
| 181 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 182 | |
| 183 | } |
| 184 | |
| 185 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { |
| 186 | |
| 187 | } |
| 188 | |
| 189 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 190 | |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | class SkPdfType1Font : public SkPdfFont { |
| 196 | public: |
| 197 | SkPdfType1Font(SkPdfType1FontDictionary* dict) { |
| 198 | fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | public: |
| 203 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 204 | |
| 205 | } |
| 206 | |
| 207 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { |
| 208 | |
| 209 | } |
| 210 | |
| 211 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 212 | |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | |
| 217 | class SkPdfCIDFont : public SkPdfFont { |
| 218 | public: |
| 219 | SkPdfCIDFont(SkPdfCIDFontDictionary* dict) { |
| 220 | fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); |
| 221 | } |
| 222 | |
| 223 | public: |
| 224 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 225 | |
| 226 | } |
| 227 | |
| 228 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { |
| 229 | |
| 230 | } |
| 231 | |
| 232 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 233 | |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | class SkPdfMultiMasterFont : public SkPdfFont { |
| 238 | public: |
| 239 | SkPdfMultiMasterFont(SkPdfMultiMasterFontDictionary* dict) { |
| 240 | fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); |
| 241 | } |
| 242 | |
| 243 | public: |
| 244 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 245 | |
| 246 | } |
| 247 | |
| 248 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { |
| 249 | |
| 250 | } |
| 251 | |
| 252 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 253 | |
| 254 | } |
| 255 | }; |
| 256 | |
| 257 | class SkPdfType3Font : public SkPdfFont { |
| 258 | public: |
| 259 | SkPdfType3Font(SkPdfType3FontDictionary* dict) { |
| 260 | fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str()); |
| 261 | } |
| 262 | |
| 263 | public: |
| 264 | virtual void drawOneChar(unsigned int ch, SkPaint* paint, SkCanvas* canvas, SkMatrix* matrix) { |
| 265 | |
| 266 | } |
| 267 | |
| 268 | virtual void afterChar(SkPaint* paint, SkMatrix* matrix) { |
| 269 | |
| 270 | } |
| 271 | |
| 272 | virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
| 273 | |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | #endif // __DEFINED__SkPdfFont |