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 "SkData.h" | |
9 #include "SkOTTable_OS_2.h" | |
10 #include "SkSFNTHeader.h" | |
11 #include "SkStream.h" | |
8 #include "SkRefCnt.h" | 12 #include "SkRefCnt.h" |
9 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
10 #include "SkTypefaceCache.h" | 14 #include "SkTypefaceCache.h" |
15 #include "Resources.h" | |
11 #include "Test.h" | 16 #include "Test.h" |
12 | 17 |
18 #include <memory> | |
19 | |
20 static void TypefaceStyle_test(skiatest::Reporter* reporter, | |
21 uint16_t weight, uint16_t width, SkData* data) | |
22 { | |
23 sk_sp<SkData> dataCopy; | |
24 SkData* dataToUse = data; | |
25 if (!dataToUse->unique()) { | |
bungeman-skia
2016/07/25 13:23:08
data should always be unique here, but it isn't on
| |
26 dataCopy = SkData::MakeWithCopy(data->data(), data->size()); | |
27 dataToUse = dataCopy.get(); | |
28 } | |
29 SkSFNTHeader* sfntHeader = static_cast<SkSFNTHeader*>(dataToUse->writable_da ta()); | |
30 | |
31 SkSFNTHeader::TableDirectoryEntry* tableEntry = | |
32 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader); | |
33 SkSFNTHeader::TableDirectoryEntry* os2TableEntry = nullptr; | |
34 int numTables = SkEndian_SwapBE16(sfntHeader->numTables); | |
35 for (int tableEntryIndex = 0; tableEntryIndex < numTables; ++tableEntryIndex ) { | |
36 if (SkOTTableOS2::TAG == tableEntry[tableEntryIndex].tag) { | |
37 os2TableEntry = tableEntry + tableEntryIndex; | |
38 break; | |
39 } | |
40 } | |
41 SkASSERT_RELEASE(os2TableEntry); | |
42 | |
43 size_t os2TableOffset = SkEndian_SwapBE32(os2TableEntry->offset); | |
44 SkOTTableOS2_V0* os2Table = SkTAddOffset<SkOTTableOS2_V0>(sfntHeader, os2Tab leOffset); | |
45 os2Table->usWeightClass.value = SkEndian_SwapBE16(weight); | |
46 using WidthType = SkOTTableOS2_V0::WidthClass::Value; | |
47 os2Table->usWidthClass.value = static_cast<WidthType>(SkEndian_SwapBE16(widt h)); | |
48 | |
49 sk_sp<SkTypeface> newTypeface(SkTypeface::MakeFromStream(new SkMemoryStream( dataToUse))); | |
50 SkASSERT_RELEASE(newTypeface); | |
51 | |
52 SkFontStyle newStyle = newTypeface->fontStyle(); | |
53 | |
54 //printf("%d, %f\n", weight, (newStyle.weight() - (float)0x7FFF) / (float)0x 7FFF); | |
55 //printf("%d, %f\n", width , (newStyle.width() - (float)0x7F) / (float)0x 7F); | |
56 //printf("%d, %d\n", weight, newStyle.weight()); | |
57 //printf("%d, %d\n", width , newStyle.width()); | |
58 | |
59 // Some back-ends (CG, GDI, DW) support OS/2 version A which uses 0 - 10 (bu t all differently). | |
60 REPORTER_ASSERT(reporter, | |
61 newStyle.weight() == weight || | |
62 (weight <= 10 && newStyle.weight() == 100 * weight) || | |
63 (weight == 4 && newStyle.weight() == 350) || // GDI weir dness | |
64 (weight == 5 && newStyle.weight() == 400) || // GDI weir dness | |
65 (weight == 0 && newStyle.weight() == 1) || // DW weird ness | |
66 (weight == 1000 && newStyle.weight() == 999) // DW weird ness | |
67 ); | |
68 | |
69 // Some back-ends (GDI) don't support width, ensure these always report 'med ium'. | |
70 REPORTER_ASSERT(reporter, | |
71 newStyle.width() == width || | |
72 newStyle.width() == 5); | |
73 } | |
74 DEF_TEST(TypefaceStyle, reporter) { | |
75 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream("/fonts/Em.ttf")); | |
76 if (!stream) { | |
77 REPORT_FAILURE(reporter, "/fonts/Em.ttf", SkString("Cannot load resource ")); | |
78 return; | |
79 } | |
80 sk_sp<SkData> data(SkData::MakeFromStream(stream.get(), stream->getLength()) ); | |
81 | |
82 for (int weight = 0; weight <= 1000; ++weight) { | |
83 TypefaceStyle_test(reporter, weight, 5, data.get()); | |
84 } | |
85 using SkFS = SkFontStyle; | |
86 for (int width = SkFS::kUltraCondensed_Width; width <= SkFS::kUltaExpanded_W idth; ++width) { | |
87 TypefaceStyle_test(reporter, 400, width, data.get()); | |
88 } | |
89 } | |
90 | |
13 DEF_TEST(Typeface, reporter) { | 91 DEF_TEST(Typeface, reporter) { |
14 | 92 |
15 sk_sp<SkTypeface> t1(SkTypeface::MakeFromName(nullptr, SkFontStyle())); | 93 sk_sp<SkTypeface> t1(SkTypeface::MakeFromName(nullptr, SkFontStyle())); |
16 sk_sp<SkTypeface> t2(SkTypeface::MakeDefault(SkTypeface::kNormal)); | 94 sk_sp<SkTypeface> t2(SkTypeface::MakeDefault(SkTypeface::kNormal)); |
17 | 95 |
18 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get())); | 96 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get())); |
19 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get())); | 97 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get())); |
20 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get())); | 98 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get())); |
21 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0)); | 99 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0)); |
22 REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0)); | 100 REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0)); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); | 166 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); |
89 cache.purgeAll(); | 167 cache.purgeAll(); |
90 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); | 168 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); |
91 } | 169 } |
92 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); | 170 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); |
93 cache.purgeAll(); | 171 cache.purgeAll(); |
94 REPORTER_ASSERT(reporter, count(reporter, cache) == 1); | 172 REPORTER_ASSERT(reporter, count(reporter, cache) == 1); |
95 } | 173 } |
96 REPORTER_ASSERT(reporter, t1->unique()); | 174 REPORTER_ASSERT(reporter, t1->unique()); |
97 } | 175 } |
OLD | NEW |