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 #include "SkRefCnt.h" |
8 #include "SkTypeface.h" | 9 #include "SkTypeface.h" |
| 10 #include "SkTypefaceCache.h" |
9 #include "Test.h" | 11 #include "Test.h" |
10 | 12 |
11 DEF_TEST(Typeface, reporter) { | 13 DEF_TEST(Typeface, reporter) { |
12 | 14 |
13 SkAutoTUnref<SkTypeface> t1(SkTypeface::CreateFromName(nullptr, SkTypeface::
kNormal)); | 15 SkAutoTUnref<SkTypeface> t1(SkTypeface::CreateFromName(nullptr, SkTypeface::
kNormal)); |
14 SkAutoTUnref<SkTypeface> t2(SkTypeface::RefDefault(SkTypeface::kNormal)); | 16 SkAutoTUnref<SkTypeface> t2(SkTypeface::RefDefault(SkTypeface::kNormal)); |
15 | 17 |
16 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get())); | 18 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get())); |
17 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get())); | 19 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get())); |
18 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get())); | 20 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get())); |
19 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0)); | 21 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0)); |
20 REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0)); | 22 REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0)); |
21 | 23 |
22 #ifdef SK_BUILD_FOR_ANDROID | 24 #ifdef SK_BUILD_FOR_ANDROID |
23 SkAutoTUnref<SkTypeface> t3(SkTypeface::CreateFromName("non-existent-font",
SkTypeface::kNormal)); | 25 SkAutoTUnref<SkTypeface> t3(SkTypeface::CreateFromName("non-existent-font",
SkTypeface::kNormal)); |
24 REPORTER_ASSERT(reporter, nullptr == t3.get()); | 26 REPORTER_ASSERT(reporter, nullptr == t3.get()); |
25 #endif | 27 #endif |
26 } | 28 } |
| 29 |
| 30 class SkEmptyTypeface : public SkTypeface { |
| 31 public: |
| 32 static sk_sp<SkTypeface> Create(SkFontID id) { return sk_sp<SkTypeface>(new
SkEmptyTypeface(id)); } |
| 33 protected: |
| 34 SkEmptyTypeface(SkFontID id) : SkTypeface(SkFontStyle(), id, true) { } |
| 35 |
| 36 SkStreamAsset* onOpenStream(int* ttcIndex) const override { return nullptr;
} |
| 37 SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, |
| 38 const SkDescriptor*) const override { |
| 39 return nullptr; |
| 40 } |
| 41 void onFilterRec(SkScalerContextRec*) const override { } |
| 42 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( |
| 43 PerGlyphInfo, |
| 44 const uint32_t*, uint32_t) const override { retu
rn nullptr; } |
| 45 void onGetFontDescriptor(SkFontDescriptor*, bool*) const override { } |
| 46 virtual int onCharsToGlyphs(const void* chars, Encoding encoding, |
| 47 uint16_t glyphs[], int glyphCount) const overrid
e { |
| 48 SK_ABORT("unimplemented"); |
| 49 return 0; |
| 50 } |
| 51 int onCountGlyphs() const override { return 0; }; |
| 52 int onGetUPEM() const override { return 0; }; |
| 53 void onGetFamilyName(SkString* familyName) const override { familyName->rese
t(); } |
| 54 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { |
| 55 SK_ABORT("unimplemented"); |
| 56 return nullptr; |
| 57 }; |
| 58 int onGetTableTags(SkFontTableTag tags[]) const override { return 0; } |
| 59 size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const override
{ return 0; } |
| 60 }; |
| 61 |
| 62 static bool count_proc(SkTypeface* face, void* ctx) { |
| 63 int* count = static_cast<int*>(ctx); |
| 64 *count = *count + 1; |
| 65 return false; |
| 66 } |
| 67 static int count(skiatest::Reporter* reporter, const SkTypefaceCache& cache) { |
| 68 int count = 0; |
| 69 SkTypeface* none = cache.findByProcAndRef(count_proc, &count); |
| 70 REPORTER_ASSERT(reporter, none == nullptr); |
| 71 return count; |
| 72 } |
| 73 |
| 74 DEF_TEST(TypefaceCache, reporter) { |
| 75 sk_sp<SkTypeface> t1(SkEmptyTypeface::Create(1)); |
| 76 { |
| 77 SkTypefaceCache cache; |
| 78 REPORTER_ASSERT(reporter, count(reporter, cache) == 0); |
| 79 { |
| 80 sk_sp<SkTypeface> t0(SkEmptyTypeface::Create(0)); |
| 81 cache.add(t0.get()); |
| 82 REPORTER_ASSERT(reporter, count(reporter, cache) == 1); |
| 83 cache.add(t1.get()); |
| 84 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); |
| 85 cache.purgeAll(); |
| 86 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); |
| 87 } |
| 88 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); |
| 89 cache.purgeAll(); |
| 90 REPORTER_ASSERT(reporter, count(reporter, cache) == 1); |
| 91 } |
| 92 REPORTER_ASSERT(reporter, t1->unique()); |
| 93 } |
OLD | NEW |