Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tests/TypefaceTest.cpp

Issue 2171163002: Add test for typeface style round trip. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix whitespace, test on Linux. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ports/SkFontHost_win.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 static void TypefaceStyle_test(skiatest::Reporter* reporter, uint16_t weight, ui nt16_t width,
20 SkData* data)
21 {
22 SkSFNTHeader* sfntHeader = static_cast<SkSFNTHeader*>(data->writable_data()) ;
bungeman-skia 2016/07/25 13:23:08 This is what is actually failing on Mac (on the se
23 SkSFNTHeader::TableDirectoryEntry* tableEntry = SkTAfter<SkSFNTHeader::Table DirectoryEntry>(sfntHeader);
24 SkSFNTHeader::TableDirectoryEntry* os2TableEntry = nullptr;
25 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
26 for (int tableEntryIndex = 0; tableEntryIndex < numTables; ++tableEntryIndex ) {
27 if (SkOTTableOS2::TAG == tableEntry[tableEntryIndex].tag) {
28 os2TableEntry = tableEntry + tableEntryIndex;
29 break;
30 }
31 }
32 SkASSERT(os2TableEntry);
33
34 size_t os2TableOffset = SkEndian_SwapBE32(os2TableEntry->offset);
35 SkOTTableOS2_V0* os2Table = SkTAddOffset<SkOTTableOS2_V0>(sfntHeader, os2Tab leOffset);
36 os2Table->usWeightClass.value = SkEndian_SwapBE16(weight);
37 os2Table->usWidthClass.value = static_cast<SkOTTableOS2_V0::WidthClass::Valu e>(SkEndian_SwapBE16(width));
38
39 sk_sp<SkTypeface> newTypeface(SkTypeface::MakeFromStream(new SkMemoryStream( data)));
40 SkASSERT(newTypeface);
41
42 SkFontStyle newStyle = newTypeface->fontStyle();
43 //printf("%d, %f\n", weight, (newStyle.weight() - (float)0x7FFF) / (float)0x 7FFF);
44 //printf("%d, %f\n", width, (newStyle.width() - (float)0x7F) / (float)0x7F);
45 //printf("(%d, %d)\n", weight, newStyle.weight());
46 //printf("%d, %d\n", width, newStyle.width());
47
48 // Some back-ends (CG, GDI, DW) support OS/2 version A which uses 0 - 10 (bu t all differently).
49 REPORTER_ASSERT(reporter,
50 newStyle.weight() == weight ||
51 (weight <= 10 && newStyle.weight() == 100 * weight) ||
52 (weight == 4 && newStyle.weight() == 350) || // GDI weir dness
53 (weight == 5 && newStyle.weight() == 400) || // GDI weir dness
54 (weight == 0 && newStyle.weight() == 1) || // DW weird ness
55 (weight == 1000 && newStyle.weight() == 999) // DW weird ness
56 );
57
58 // Some back-ends (GDI) don't support width, ensure these always report 'med ium'.
59 REPORTER_ASSERT(reporter, newStyle.width() == width || newStyle.width() == 5 );
60 }
61 DEF_TEST(TypefaceStyle, reporter) {
62 sk_sp<SkTypeface> typeface(MakeResourceAsTypeface("/fonts/Em.ttf"));
63 if (!typeface) {
64 REPORT_FAILURE(reporter, "/fonts/Em.ttf", SkString("Cannot load resource "));
65 return;
66 }
67 std::unique_ptr<SkStreamAsset> stream(typeface->openStream(nullptr));
68 SkASSERT(stream);
69 sk_sp<SkData> data(SkData::MakeFromStream(stream.get(), stream->getLength()) );
70
71 for (int weight = 0; weight <= 1000; ++weight) {
72 TypefaceStyle_test(reporter, weight, 4, data.get());
73 }
74 for (int width = SkFontStyle::kUltraCondensed_Width; width <= SkFontStyle::k UltaExpanded_Width; ++width) {
75 TypefaceStyle_test(reporter, 400, width, data.get());
76 }
77 }
78
13 DEF_TEST(Typeface, reporter) { 79 DEF_TEST(Typeface, reporter) {
14 80
15 sk_sp<SkTypeface> t1(SkTypeface::MakeFromName(nullptr, SkFontStyle())); 81 sk_sp<SkTypeface> t1(SkTypeface::MakeFromName(nullptr, SkFontStyle()));
16 sk_sp<SkTypeface> t2(SkTypeface::MakeDefault(SkTypeface::kNormal)); 82 sk_sp<SkTypeface> t2(SkTypeface::MakeDefault(SkTypeface::kNormal));
17 83
18 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get())); 84 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get()));
19 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get())); 85 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get()));
20 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get())); 86 REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t2.get()));
21 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0)); 87 REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), 0));
22 REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0)); 88 REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); 154 REPORTER_ASSERT(reporter, count(reporter, cache) == 2);
89 cache.purgeAll(); 155 cache.purgeAll();
90 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); 156 REPORTER_ASSERT(reporter, count(reporter, cache) == 2);
91 } 157 }
92 REPORTER_ASSERT(reporter, count(reporter, cache) == 2); 158 REPORTER_ASSERT(reporter, count(reporter, cache) == 2);
93 cache.purgeAll(); 159 cache.purgeAll();
94 REPORTER_ASSERT(reporter, count(reporter, cache) == 1); 160 REPORTER_ASSERT(reporter, count(reporter, cache) == 1);
95 } 161 }
96 REPORTER_ASSERT(reporter, t1->unique()); 162 REPORTER_ASSERT(reporter, t1->unique());
97 } 163 }
OLDNEW
« no previous file with comments | « src/ports/SkFontHost_win.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698