OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkFontHost.h" | 10 #include "SkFontHost.h" |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 // that and then remove that family from the name list | 251 // that and then remove that family from the name list |
252 FamilyRec* family = remove_from_family(this); | 252 FamilyRec* family = remove_from_family(this); |
253 if (NULL != family) { | 253 if (NULL != family) { |
254 remove_from_names(family); | 254 remove_from_names(family); |
255 detach_and_delete_family(family); | 255 detach_and_delete_family(family); |
256 } | 256 } |
257 } | 257 } |
258 | 258 |
259 bool isSysFont() const { return fIsSysFont; } | 259 bool isSysFont() const { return fIsSysFont; } |
260 FamilyRec* getFamily() const { return fFamilyRec; } | 260 FamilyRec* getFamily() const { return fFamilyRec; } |
261 // openStream returns a SkStream that has been ref-ed | 261 |
262 virtual SkStream* openStream() = 0; | |
263 virtual const char* getUniqueString() const = 0; | 262 virtual const char* getUniqueString() const = 0; |
264 | 263 |
265 private: | 264 private: |
266 FamilyRec* fFamilyRec; // we don't own this, just point to it | 265 FamilyRec* fFamilyRec; // we don't own this, just point to it |
267 bool fIsSysFont; | 266 bool fIsSysFont; |
268 | 267 |
269 typedef SkTypeface_FreeType INHERITED; | 268 typedef SkTypeface_FreeType INHERITED; |
270 }; | 269 }; |
271 | 270 |
272 /////////////////////////////////////////////////////////////////////////////// | 271 /////////////////////////////////////////////////////////////////////////////// |
273 | 272 |
274 /* This subclass is just a place holder for when we have no fonts available. | 273 /* This subclass is just a place holder for when we have no fonts available. |
275 It exists so that our globals (e.g. gFamilyHead) that expect *something* | 274 It exists so that our globals (e.g. gFamilyHead) that expect *something* |
276 will not be null. | 275 will not be null. |
277 */ | 276 */ |
278 class EmptyTypeface : public FamilyTypeface { | 277 class EmptyTypeface : public FamilyTypeface { |
279 public: | 278 public: |
280 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} | 279 EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {} |
281 | 280 |
282 // overrides | |
283 virtual SkStream* openStream() SK_OVERRIDE { return NULL; } | |
284 virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; } | 281 virtual const char* getUniqueString() SK_OVERRIDE const { return NULL; } |
285 | 282 |
| 283 protected: |
| 284 virtual SkStream* onOpenStream(int*) const SK_OVERRIDE { return NULL; } |
| 285 |
286 private: | 286 private: |
287 typedef FamilyTypeface INHERITED; | 287 typedef FamilyTypeface INHERITED; |
288 }; | 288 }; |
289 | 289 |
290 class StreamTypeface : public FamilyTypeface { | 290 class StreamTypeface : public FamilyTypeface { |
291 public: | 291 public: |
292 StreamTypeface(Style style, bool sysFont, FamilyRec* family, | 292 StreamTypeface(Style style, bool sysFont, FamilyRec* family, |
293 SkStream* stream, bool isFixedWidth) | 293 SkStream* stream, bool isFixedWidth) |
294 : INHERITED(style, sysFont, family, isFixedWidth) { | 294 : INHERITED(style, sysFont, family, isFixedWidth) { |
295 stream->ref(); | 295 stream->ref(); |
296 fStream = stream; | 296 fStream = stream; |
297 } | 297 } |
298 virtual ~StreamTypeface() { | 298 virtual ~StreamTypeface() { |
299 fStream->unref(); | 299 fStream->unref(); |
300 } | 300 } |
301 | 301 |
302 virtual SkStream* openStream() SK_OVERRIDE { | 302 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } |
303 // openStream returns a refed stream. | 303 |
304 fStream->ref(); | 304 protected: |
305 return fStream; | 305 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
| 306 *ttcIndex = 0; |
| 307 fStream->ref(); |
| 308 return fStream; |
306 } | 309 } |
307 virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; } | |
308 | 310 |
309 private: | 311 private: |
310 SkStream* fStream; | 312 SkStream* fStream; |
311 | 313 |
312 typedef FamilyTypeface INHERITED; | 314 typedef FamilyTypeface INHERITED; |
313 }; | 315 }; |
314 | 316 |
315 class FileTypeface : public FamilyTypeface { | 317 class FileTypeface : public FamilyTypeface { |
316 public: | 318 public: |
317 FileTypeface(Style style, bool sysFont, FamilyRec* family, | 319 FileTypeface(Style style, bool sysFont, FamilyRec* family, |
318 const char path[], bool isFixedWidth) | 320 const char path[], bool isFixedWidth) |
319 : INHERITED(style, sysFont, family, isFixedWidth) { | 321 : INHERITED(style, sysFont, family, isFixedWidth) { |
320 fPath.set(path); | 322 fPath.set(path); |
321 } | 323 } |
322 | 324 |
323 virtual SkStream* openStream() SK_OVERRIDE { | |
324 return SkStream::NewFromFile(fPath.c_str()); | |
325 } | |
326 | |
327 virtual const char* getUniqueString() const SK_OVERRIDE { | 325 virtual const char* getUniqueString() const SK_OVERRIDE { |
328 const char* str = strrchr(fPath.c_str(), '/'); | 326 const char* str = strrchr(fPath.c_str(), '/'); |
329 if (str) { | 327 if (str) { |
330 str += 1; // skip the '/' | 328 str += 1; // skip the '/' |
331 } | 329 } |
332 return str; | 330 return str; |
333 } | 331 } |
334 | 332 |
| 333 protected: |
| 334 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { |
| 335 *ttcIndex = 0; |
| 336 return SkStream::NewFromFile(fPath.c_str()); |
| 337 } |
| 338 |
335 private: | 339 private: |
336 SkString fPath; | 340 SkString fPath; |
337 | 341 |
338 typedef FamilyTypeface INHERITED; | 342 typedef FamilyTypeface INHERITED; |
339 }; | 343 }; |
340 | 344 |
341 /////////////////////////////////////////////////////////////////////////////// | 345 /////////////////////////////////////////////////////////////////////////////// |
342 /////////////////////////////////////////////////////////////////////////////// | 346 /////////////////////////////////////////////////////////////////////////////// |
343 | 347 |
344 static bool get_name_and_style(const char path[], SkString* name, | 348 static bool get_name_and_style(const char path[], SkString* name, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 SkFontDescriptor descriptor; | 462 SkFontDescriptor descriptor; |
459 descriptor.setFamilyName(find_family_name(face)); | 463 descriptor.setFamilyName(find_family_name(face)); |
460 descriptor.setStyle(face->style()); | 464 descriptor.setStyle(face->style()); |
461 descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString()); | 465 descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString()); |
462 | 466 |
463 descriptor.serialize(stream); | 467 descriptor.serialize(stream); |
464 | 468 |
465 const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); | 469 const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont(); |
466 if (isCustomFont) { | 470 if (isCustomFont) { |
467 // store the entire font in the fontData | 471 // store the entire font in the fontData |
468 SkStream* fontStream = ((FamilyTypeface*)face)->openStream(); | 472 SkStream* fontStream = face->openStream(NULL); |
469 const uint32_t length = fontStream->getLength(); | 473 const uint32_t length = fontStream->getLength(); |
470 | 474 |
471 stream->writePackedUInt(length); | 475 stream->writePackedUInt(length); |
472 stream->writeStream(fontStream, length); | 476 stream->writeStream(fontStream, length); |
473 | 477 |
474 fontStream->unref(); | 478 fontStream->unref(); |
475 } else { | 479 } else { |
476 stream->writePackedUInt(0); | 480 stream->writePackedUInt(0); |
477 } | 481 } |
478 } | 482 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 } | 526 } |
523 | 527 |
524 if (NULL == tf) { | 528 if (NULL == tf) { |
525 tf = find_best_face(gDefaultFamily, style); | 529 tf = find_best_face(gDefaultFamily, style); |
526 } | 530 } |
527 | 531 |
528 SkSafeRef(tf); | 532 SkSafeRef(tf); |
529 return tf; | 533 return tf; |
530 } | 534 } |
531 | 535 |
532 SkStream* SkFontHost::OpenStream(uint32_t fontID) { | |
533 FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID); | |
534 SkStream* stream = tf ? tf->openStream() : NULL; | |
535 | |
536 if (stream && stream->getLength() == 0) { | |
537 stream->unref(); | |
538 stream = NULL; | |
539 } | |
540 return stream; | |
541 } | |
542 | |
543 SkTypeface* SkFontHost::NextLogicalTypeface(SkFontID currFontID, SkFontID origFo
ntID) { | 536 SkTypeface* SkFontHost::NextLogicalTypeface(SkFontID currFontID, SkFontID origFo
ntID) { |
544 return NULL; | 537 return NULL; |
545 } | 538 } |
546 | 539 |
547 /////////////////////////////////////////////////////////////////////////////// | 540 /////////////////////////////////////////////////////////////////////////////// |
548 | 541 |
549 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { | 542 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { |
550 if (NULL == stream || stream->getLength() <= 0) { | 543 if (NULL == stream || stream->getLength() <= 0) { |
551 SkDELETE(stream); | 544 SkDELETE(stream); |
552 return NULL; | 545 return NULL; |
553 } | 546 } |
554 | 547 |
555 bool isFixedWidth; | 548 bool isFixedWidth; |
556 SkTypeface::Style style; | 549 SkTypeface::Style style; |
557 if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) { | 550 if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) { |
558 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWi
dth)); | 551 return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWi
dth)); |
559 } else { | 552 } else { |
560 return NULL; | 553 return NULL; |
561 } | 554 } |
562 } | 555 } |
563 | 556 |
564 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { | 557 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { |
565 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | 558 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
566 return stream.get() ? CreateTypefaceFromStream(stream) : NULL; | 559 return stream.get() ? CreateTypefaceFromStream(stream) : NULL; |
567 } | 560 } |
OLD | NEW |