OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkPdfFont_DEFINED | 8 #ifndef SkPdfFont_DEFINED |
9 #define SkPdfFont_DEFINED | 9 #define SkPdfFont_DEFINED |
10 | 10 |
| 11 #include "SkPdfGraphicsState.h" |
11 #include "SkPdfHeaders_autogen.h" | 12 #include "SkPdfHeaders_autogen.h" |
12 #include "SkPdfMapper_autogen.h" | 13 #include "SkPdfMapper_autogen.h" |
13 | 14 #include "SkPdfUtils.h" |
14 #include "SkTypeface.h" | 15 #include "SkTypeface.h" |
| 16 #include "SkTDict.h" |
15 #include "SkUtils.h" | 17 #include "SkUtils.h" |
16 #include "SkPdfGraphicsState.h" | |
17 #include "SkPdfUtils.h" | |
18 #include "SkTDict.h" | |
19 | 18 |
20 class SkPdfType0Font; | 19 class SkPdfType0Font; |
21 class SkPdfType1Font; | 20 class SkPdfType1Font; |
22 class SkPdfType3Font; | 21 class SkPdfType3Font; |
23 class SkPdfTrueTypeFont; | 22 class SkPdfTrueTypeFont; |
24 class SkPdfMultiMasterFont; | 23 class SkPdfMultiMasterFont; |
25 class SkPdfFont; | 24 class SkPdfFont; |
26 | 25 |
27 struct SkPdfStandardFontEntry { | 26 struct SkPdfStandardFontEntry { |
28 // We don't own this pointer! | 27 // We don't own this pointer! |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 166 |
168 public: | 167 public: |
169 SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()),
fToUnicode(NULL) {} | 168 SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()),
fToUnicode(NULL) {} |
170 | 169 |
171 virtual ~SkPdfFont() { | 170 virtual ~SkPdfFont() { |
172 // TODO(edisonn): NYI (will leak for now) | 171 // TODO(edisonn): NYI (will leak for now) |
173 } | 172 } |
174 | 173 |
175 const SkPdfEncoding* encoding() const {return fEncoding;} | 174 const SkPdfEncoding* encoding() const {return fEncoding;} |
176 | 175 |
177 void drawText(const SkDecodedText& text, SkPaint* paint, SkPdfContext* pdfCo
ntext, SkCanvas* canvas) { | 176 void drawText(const SkDecodedText& text, SkPaint* paint, SkPdfContext* pdfCo
ntext, |
| 177 SkCanvas* canvas) { |
178 for (int i = 0 ; i < text.size(); i++) { | 178 for (int i = 0 ; i < text.size(); i++) { |
179 canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm); | 179 canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm); |
180 #ifdef PDF_TRACE | 180 #ifdef PDF_TRACE |
181 SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(
0)); | 181 SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(
0)); |
182 pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1); | 182 pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1); |
183 printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()), SkScal
arToDouble(point.y())); | 183 printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()), |
| 184 SkScalarToDouble(point.y())); |
184 #endif // PDF_TRACE | 185 #endif // PDF_TRACE |
185 | 186 |
186 #ifdef PDF_TRACE_DRAWTEXT | 187 #ifdef PDF_TRACE_DRAWTEXT |
187 SkPaint col; | 188 SkPaint col; |
188 col.setColor(SK_ColorMAGENTA); | 189 col.setColor(SK_ColorMAGENTA); |
189 SkRect rect = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScal
ar(0.0), SkDoubleToScalar(10.0), SkDoubleToScalar(10.0)); | 190 SkRect rect = SkRect::MakeXYWH(SkDoubleToScalar(0.0), |
| 191 SkDoubleToScalar(0.0), |
| 192 SkDoubleToScalar(10.0), |
| 193 SkDoubleToScalar(10.0)); |
190 canvas->save(); | 194 canvas->save(); |
191 canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm); | 195 canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm); |
192 canvas->drawRect(rect, col); | 196 canvas->drawRect(rect, col); |
193 canvas->restore(); | 197 canvas->restore(); |
194 #endif | 198 #endif |
195 double width = drawOneChar(text[i], paint, pdfContext, canvas); | 199 double width = drawOneChar(text[i], paint, pdfContext, canvas); |
196 pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(w
idth), SkDoubleToScalar(0.0)); | 200 pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(w
idth), |
| 201 SkDoubleToScalar(0
.0)); |
197 } | 202 } |
198 } | 203 } |
199 | 204 |
200 void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { | 205 void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const { |
201 if (fToUnicode) { | 206 if (fToUnicode) { |
202 textOut->text = new uint16_t[textIn.len]; | 207 textOut->text = new uint16_t[textIn.len]; |
203 textOut->len = textIn.len; | 208 textOut->len = textIn.len; |
204 for (int i = 0; i < textIn.len; i++) { | 209 for (int i = 0; i < textIn.len; i++) { |
205 textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]]; | 210 textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]]; |
206 } | 211 } |
207 } else { | 212 } else { |
208 textOut->text = textIn.text; | 213 textOut->text = textIn.text; |
209 textOut->len = textIn.len; | 214 textOut->len = textIn.len; |
210 } | 215 } |
211 }; | 216 }; |
212 | 217 |
213 inline unsigned int ToUnicode(unsigned int ch) const { | 218 inline unsigned int ToUnicode(unsigned int ch) const { |
214 if (fToUnicode && fToUnicode->fCMapEncoding) { | 219 if (fToUnicode && fToUnicode->fCMapEncoding) { |
215 return fToUnicode->fCMapEncoding[ch]; | 220 return fToUnicode->fCMapEncoding[ch]; |
216 } else { | 221 } else { |
217 return ch; | 222 return ch; |
218 } | 223 } |
219 }; | 224 }; |
220 | 225 |
221 static SkPdfFont* fontFromPdfDictionary(SkPdfNativeDoc* doc, SkPdfFontDictio
nary* dict); | 226 static SkPdfFont* fontFromPdfDictionary(SkPdfNativeDoc* doc, SkPdfFontDictio
nary* dict); |
222 static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman"
);} | 227 static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman"
);} |
223 | 228 |
224 static SkPdfType0Font* fontFromType0FontDictionary(SkPdfNativeDoc* doc, SkPd
fType0FontDictionary* dict); | 229 static SkPdfType0Font* fontFromType0FontDictionary(SkPdfNativeDoc* doc, |
225 static SkPdfType1Font* fontFromType1FontDictionary(SkPdfNativeDoc* doc, SkPd
fType1FontDictionary* dict); | 230 SkPdfType0FontDictionary*
dict); |
226 static SkPdfType3Font* fontFromType3FontDictionary(SkPdfNativeDoc* doc, SkPd
fType3FontDictionary* dict); | 231 static SkPdfType1Font* fontFromType1FontDictionary(SkPdfNativeDoc* doc, |
227 static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfNativeDoc* doc
, SkPdfTrueTypeFontDictionary* dict); | 232 SkPdfType1FontDictionary*
dict); |
228 static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(SkPdfNativeDo
c* doc, SkPdfMultiMasterFontDictionary* dict); | 233 static SkPdfType3Font* fontFromType3FontDictionary(SkPdfNativeDoc* doc, |
| 234 SkPdfType3FontDictionary*
dict); |
| 235 static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfNativeDoc* doc
, |
| 236 SkPdfTrueTypeFontDi
ctionary* dict); |
| 237 static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary( |
| 238 SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict); |
229 | 239 |
230 static SkPdfFont* fontFromFontDescriptor(SkPdfNativeDoc* doc, SkPdfFontDescr
iptorDictionary* fd, bool loadFromName = true); | 240 static SkPdfFont* fontFromFontDescriptor(SkPdfNativeDoc* doc, |
| 241 SkPdfFontDescriptorDictionary* fd, |
| 242 bool loadFromName = true); |
231 | 243 |
232 public: | 244 public: |
233 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, SkCanvas* canvas) = 0; | 245 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, |
| 246 SkCanvas* canvas) = 0; |
234 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0; | 247 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0; |
235 | 248 |
236 private: | 249 private: |
237 static SkPdfFont* fontFromPdfDictionaryOnce(SkPdfNativeDoc* doc, SkPdfFontDi
ctionary* dict); | 250 static SkPdfFont* fontFromPdfDictionaryOnce(SkPdfNativeDoc* doc, SkPdfFontDi
ctionary* dict); |
238 }; | 251 }; |
239 | 252 |
240 class SkPdfStandardFont : public SkPdfFont { | 253 class SkPdfStandardFont : public SkPdfFont { |
241 SkTypeface* fTypeface; | 254 SkTypeface* fTypeface; |
242 | 255 |
243 public: | 256 public: |
244 SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {} | 257 SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {} |
245 | 258 |
246 public: | 259 public: |
247 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, SkCanvas* canvas) { | 260 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, |
| 261 SkCanvas* canvas) { |
248 paint->setTypeface(fTypeface); | 262 paint->setTypeface(fTypeface); |
249 paint->setTextEncoding(SkPaint::kUTF8_TextEncoding); | 263 paint->setTextEncoding(SkPaint::kUTF8_TextEncoding); |
250 | 264 |
251 unsigned long ch4 = ch; | 265 unsigned long ch4 = ch; |
252 char utf8[10]; | 266 char utf8[10]; |
253 int len = SkUTF8_FromUnichar(ch4, utf8); | 267 int len = SkUTF8_FromUnichar(ch4, utf8); |
254 | 268 |
255 canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *p
aint); | 269 canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *p
aint); |
256 | 270 |
257 SkScalar textWidth = paint->measureText(utf8, len); | 271 SkScalar textWidth = paint->measureText(utf8, len); |
258 return SkScalarToDouble(textWidth); | 272 return SkScalarToDouble(textWidth); |
259 } | 273 } |
260 | 274 |
261 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} | 275 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} |
262 }; | 276 }; |
263 | 277 |
264 class SkPdfType0Font : public SkPdfFont { | 278 class SkPdfType0Font : public SkPdfFont { |
265 public: | 279 public: |
266 SkPdfType0Font(SkPdfNativeDoc* doc, SkPdfType0FontDictionary* dict); | 280 SkPdfType0Font(SkPdfNativeDoc* doc, SkPdfType0FontDictionary* dict); |
267 | 281 |
268 public: | 282 public: |
269 | 283 |
270 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, SkCanvas* canvas) { | 284 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, |
| 285 SkCanvas* canvas) { |
271 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); | 286 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas); |
272 } | 287 } |
273 | 288 |
274 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { | 289 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
275 } | 290 } |
276 }; | 291 }; |
277 | 292 |
278 class SkPdfType1Font : public SkPdfFont { | 293 class SkPdfType1Font : public SkPdfFont { |
279 public: | 294 public: |
280 SkPdfType1Font(SkPdfNativeDoc* doc, SkPdfType1FontDictionary* dict) { | 295 SkPdfType1Font(SkPdfNativeDoc* doc, SkPdfType1FontDictionary* dict) { |
281 if (dict->has_FontDescriptor()) { | 296 if (dict->has_FontDescriptor()) { |
282 fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescrip
tor(doc)); | 297 fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescrip
tor(doc)); |
283 } else { | 298 } else { |
284 fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str()); | 299 fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str()); |
285 } | 300 } |
286 | 301 |
287 if (dict->isEncodingAName(doc)) { | 302 if (dict->isEncodingAName(doc)) { |
288 fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(doc).c_s
tr()); | 303 fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(doc).c_s
tr()); |
289 } else if (dict->isEncodingADictionary(doc)) { | 304 } else if (dict->isEncodingADictionary(doc)) { |
290 //SkPdfDictionary* dictEnc = dict->getEncodingAsDictionary(doc); | 305 //SkPdfDictionary* dictEnc = dict->getEncodingAsDictionary(doc); |
291 } | 306 } |
292 dict->FontDescriptor(doc); | 307 dict->FontDescriptor(doc); |
293 } | 308 } |
294 | 309 |
295 public: | 310 public: |
296 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext*
pdfContext, SkCanvas* canvas) { | 311 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext*
pdfContext, |
| 312 SkCanvas* canvas) { |
297 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas
); | 313 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas
); |
298 } | 314 } |
299 | 315 |
300 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { | 316 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { |
301 | 317 |
302 } | 318 } |
303 }; | 319 }; |
304 | 320 |
305 class SkPdfTrueTypeFont : public SkPdfType1Font { | 321 class SkPdfTrueTypeFont : public SkPdfType1Font { |
306 public: | 322 public: |
307 SkPdfTrueTypeFont(SkPdfNativeDoc* doc, SkPdfTrueTypeFontDictionary* dict) :
SkPdfType1Font(doc, dict) { | 323 SkPdfTrueTypeFont(SkPdfNativeDoc* doc, SkPdfTrueTypeFontDictionary* dict) |
308 } | 324 : SkPdfType1Font(doc, dict) {} |
309 }; | 325 }; |
310 | 326 |
311 class SkPdfMultiMasterFont : public SkPdfType1Font { | 327 class SkPdfMultiMasterFont : public SkPdfType1Font { |
312 public: | 328 public: |
313 SkPdfMultiMasterFont(SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* di
ct) : SkPdfType1Font(doc, dict) { | 329 SkPdfMultiMasterFont(SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* di
ct) |
314 } | 330 : SkPdfType1Font(doc, dict) {} |
315 }; | 331 }; |
316 /* | 332 /* |
317 class CIDToGIDMap { | 333 class CIDToGIDMap { |
318 virtual unsigned int map(unsigned int cid) = 0; | 334 virtual unsigned int map(unsigned int cid) = 0; |
319 static CIDToGIDMap* fromName(const char* name); | 335 static CIDToGIDMap* fromName(const char* name); |
320 }; | 336 }; |
321 | 337 |
322 class CIDToGIDMap_Identity { | 338 class CIDToGIDMap_Identity { |
323 virtual unsigned int map(unsigned int cid) { return cid; } | 339 virtual unsigned int map(unsigned int cid) { return cid; } |
324 | 340 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 // TODO(edisonn): report pdf corruption | 430 // TODO(edisonn): report pdf corruption |
415 } | 431 } |
416 j++; | 432 j++; |
417 } else { | 433 } else { |
418 // TODO(edisonn): report bad pdf | 434 // TODO(edisonn): report bad pdf |
419 } | 435 } |
420 } | 436 } |
421 } | 437 } |
422 | 438 |
423 public: | 439 public: |
424 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, SkCanvas* canvas) { | 440 virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pd
fContext, |
| 441 SkCanvas* canvas) { |
425 if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj)
{ | 442 if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj)
{ |
426 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canv
as); | 443 return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canv
as); |
427 } | 444 } |
428 | 445 |
429 #ifdef PDF_TRACE | 446 #ifdef PDF_TRACE |
430 printf("Type 3 char to unicode: %c\n", ToUnicode(ch)); | 447 printf("Type 3 char to unicode: %c\n", ToUnicode(ch)); |
431 if (ToUnicode(ch) == 'A') { | 448 if (ToUnicode(ch) == 'A') { |
432 printf("break;\n"); | 449 printf("break;\n"); |
433 } | 450 } |
434 #endif | 451 #endif |
435 | 452 |
436 // TODO(edisonn): is it better to resolve the reference at load time, or
now? | 453 // TODO(edisonn): is it better to resolve the reference at load time, or
now? |
437 doType3Char(pdfContext, canvas, pdfContext->fPdfDoc->resolveReference(fC
hars[ch - fFirstChar].fObj), fFontBBox, fFonMatrix, pdfContext->fGraphicsState.f
CurFontSize); | 454 doType3Char(pdfContext, |
| 455 canvas, |
| 456 pdfContext->fPdfDoc->resolveReference(fChars[ch - fFirstChar
].fObj), |
| 457 fFontBBox, |
| 458 fFonMatrix, |
| 459 pdfContext->fGraphicsState.fCurFontSize); |
438 | 460 |
439 // TODO(edisonn): verify/test translate code, not tested yet | 461 // TODO(edisonn): verify/test translate code, not tested yet |
440 pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(pdfCo
ntext->fGraphicsState.fCurFontSize * fChars[ch - fFirstChar].fWidth), | 462 pdfContext->fGraphicsState.fMatrixTm.preTranslate( |
441 SkDoubleToScalar(0.0)); | 463 SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize * |
| 464 fChars[ch - fFirstChar].fWidth), |
| 465 SkDoubleToScalar(0.0)); |
442 return fChars[ch - fFirstChar].fWidth; | 466 return fChars[ch - fFirstChar].fWidth; |
443 } | 467 } |
444 | 468 |
445 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) { | 469 virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {} |
446 | |
447 } | |
448 }; | 470 }; |
449 | 471 |
450 #endif // SkPdfFont_DEFINED | 472 #endif // SkPdfFont_DEFINED |
OLD | NEW |