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

Side by Side Diff: tests/PaintTest.cpp

Issue 18083023: add charsToGlyphs to SkTypeface (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « src/ports/SkFontHost_mac.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
2 /* 1 /*
3 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
4 * 3 *
5 * 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
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
7
8 #include "Test.h" 8 #include "Test.h"
9 #include "SkPath.h" 9 #include "SkPath.h"
10 #include "SkPaint.h" 10 #include "SkPaint.h"
11 #include "SkLayerDrawLooper.h" 11 #include "SkLayerDrawLooper.h"
12 #include "SkBlurMaskFilter.h" 12 #include "SkBlurMaskFilter.h"
13 #include "SkRandom.h"
14 #include "SkTypeface.h"
15 #include "SkUtils.h"
16
17 static size_t uni_to_utf8(const SkUnichar src[], void* dst, int count) {
18 char* u8 = (char*)dst;
19 for (int i = 0; i < count; ++i) {
20 int n = SkUTF8_FromUnichar(src[i], u8);
21 u8 += n;
22 }
23 return u8 - (char*)dst;
24 }
25
26 static size_t uni_to_utf16(const SkUnichar src[], void* dst, int count) {
27 uint16_t* u16 = (uint16_t*)dst;
28 for (int i = 0; i < count; ++i) {
29 int n = SkUTF16_FromUnichar(src[i], u16);
30 u16 += n;
31 }
32 return (char*)u16 - (char*)dst;
33 }
34
35 static size_t uni_to_utf32(const SkUnichar src[], void* dst, int count) {
36 SkUnichar* u32 = (SkUnichar*)dst;
37 if (src != u32) {
38 memcpy(u32, src, count * sizeof(SkUnichar));
39 }
40 return count * sizeof(SkUnichar);
41 }
42
43 static SkTypeface::Encoding paint2encoding(const SkPaint& paint) {
44 SkPaint::TextEncoding enc = paint.getTextEncoding();
45 SkASSERT(SkPaint::kGlyphID_TextEncoding != enc);
46 return (SkTypeface::Encoding)enc;
47 }
48
49 static int find_first_zero(const uint16_t glyphs[], int count) {
50 for (int i = 0; i < count; ++i) {
51 if (0 == glyphs[i]) {
52 return i;
53 }
54 }
55 return count;
56 }
57
58 static void test_cmap(skiatest::Reporter* reporter) {
59 static const int NGLYPHS = 64;
60
61 SkUnichar src[NGLYPHS];
62 SkUnichar dst[NGLYPHS]; // used for utf8, utf16, utf32 storage
63
64 static const struct {
65 size_t (*fSeedTextProc)(const SkUnichar[], void* dst, int count);
66 SkPaint::TextEncoding fEncoding;
67 } gRec[] = {
68 { uni_to_utf8, SkPaint::kUTF8_TextEncoding },
69 { uni_to_utf16, SkPaint::kUTF16_TextEncoding },
70 { uni_to_utf32, SkPaint::kUTF32_TextEncoding },
71 };
72
73 SkRandom rand;
74 SkPaint paint;
75 paint.setTypeface(SkTypeface::RefDefault())->unref();
76 SkTypeface* face = paint.getTypeface();
77
78 for (int i = 0; i < 1000; ++i) {
79 // generate some random text
80 for (int j = 0; j < NGLYPHS; ++j) {
81 src[j] = ' ' + j;
82 }
83 // inject some random chars, to sometimes abort early
84 src[rand.nextU() & 63] = rand.nextU() & 0xFFF;
85
86 for (size_t k = 0; k < SK_ARRAY_COUNT(gRec); ++k) {
87 paint.setTextEncoding(gRec[k].fEncoding);
88
89 size_t len = gRec[k].fSeedTextProc(src, dst, NGLYPHS);
90
91 uint16_t glyphs0[NGLYPHS], glyphs1[NGLYPHS];
92
93 bool contains = paint.containsText(dst, len);
94 int nglyphs = paint.textToGlyphs(dst, len, glyphs0);
95 int first = face->charsToGlyphs(dst, paint2encoding(paint), glyphs1, NGLYPHS);
96 int index = find_first_zero(glyphs1, NGLYPHS);
97
98 REPORTER_ASSERT(reporter, NGLYPHS == nglyphs);
99 REPORTER_ASSERT(reporter, index == first);
100 REPORTER_ASSERT(reporter,
101 !memcmp(glyphs0, glyphs1, NGLYPHS * sizeof(uint16_t)));
102 if (contains) {
103 REPORTER_ASSERT(reporter, NGLYPHS == first);
104 } else {
105 REPORTER_ASSERT(reporter, NGLYPHS > first);
106 }
107 }
108 }
109 }
13 110
14 // temparary api for bicubic, just be sure we can set/clear it 111 // temparary api for bicubic, just be sure we can set/clear it
15 static void test_bicubic(skiatest::Reporter* reporter) { 112 static void test_bicubic(skiatest::Reporter* reporter) {
16 SkPaint p0; 113 SkPaint p0;
17 REPORTER_ASSERT(reporter, 0 == (p0.getFlags() & SkPaint::kBicubicFilterBitma p_Flag)); 114 REPORTER_ASSERT(reporter, 0 == (p0.getFlags() & SkPaint::kBicubicFilterBitma p_Flag));
18 p0.setFlags(p0.getFlags() | SkPaint::kBicubicFilterBitmap_Flag); 115 p0.setFlags(p0.getFlags() | SkPaint::kBicubicFilterBitmap_Flag);
19 REPORTER_ASSERT(reporter, 0 != (p0.getFlags() & SkPaint::kBicubicFilterBitma p_Flag)); 116 REPORTER_ASSERT(reporter, 0 != (p0.getFlags() & SkPaint::kBicubicFilterBitma p_Flag));
20 SkPaint p1(p0); 117 SkPaint p1(p0);
21 REPORTER_ASSERT(reporter, 0 != (p1.getFlags() & SkPaint::kBicubicFilterBitma p_Flag)); 118 REPORTER_ASSERT(reporter, 0 != (p1.getFlags() & SkPaint::kBicubicFilterBitma p_Flag));
22 p0.reset(); 119 p0.reset();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 224
128 static void TestPaint(skiatest::Reporter* reporter) { 225 static void TestPaint(skiatest::Reporter* reporter) {
129 // TODO add general paint tests 226 // TODO add general paint tests
130 test_copy(reporter); 227 test_copy(reporter);
131 228
132 // regression tests 229 // regression tests
133 regression_cubic(reporter); 230 regression_cubic(reporter);
134 regression_measureText(reporter); 231 regression_measureText(reporter);
135 232
136 test_bicubic(reporter); 233 test_bicubic(reporter);
234 test_cmap(reporter);
137 } 235 }
138 236
139 #include "TestClassDef.h" 237 #include "TestClassDef.h"
140 DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint) 238 DEFINE_TESTCLASS("Paint", TestPaintClass, TestPaint)
OLDNEW
« no previous file with comments | « src/ports/SkFontHost_mac.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698