1 /*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkPaint.h"
9 #include "SkUtils.h"
10 #include "Test.h"
11
12 // Unicode Variation Selector ranges: inclusive
13 #define UVS_MIN0 0x180B
14 #define UVS_MAX0 0x180D
15 #define UVS_MIN1 0xFE00
16 #define UVS_MAX1 0xFE0F
17 #define UVS_MIN2 0xE0100
18 #define UVS_MAX2 0xE01EF
19
isUVS(SkUnichar uni)20 static bool isUVS(SkUnichar uni) {
21 return (uni >= UVS_MIN0 && uni <= UVS_MAX0) ||
22 (uni >= UVS_MIN1 && uni <= UVS_MAX1) ||
23 (uni >= UVS_MIN2 && uni <= UVS_MAX2);
24 }
25
test_uvs(skiatest::Reporter * reporter)26 static void test_uvs(skiatest::Reporter* reporter) {
27 // [min, max], [min, max] ... inclusive
28 static const SkUnichar gRanges[] = {
29 UVS_MIN0, UVS_MAX0, UVS_MIN1, UVS_MAX1, UVS_MIN2, UVS_MAX2
30 };
31
32 for (size_t i = 0; i < SK_ARRAY_COUNT(gRanges); i += 2) {
33 for (SkUnichar uni = gRanges[i] - 8; uni <= gRanges[i+1] + 8; ++uni) {
34 bool uvs0 = isUVS(uni);
35 bool uvs1 = SkUnichar_IsVariationSelector(uni);
36 REPORTER_ASSERT(reporter, uvs0 == uvs1);
37 }
38 }
39 }
40
41 // Simple test to ensure that when we call textToGlyphs, we get the same
42 // result (for the same text) when using UTF8, UTF16, UTF32.
43 // TODO: make the text more complex (i.e. incorporate chars>7bits)
test_textencodings(skiatest::Reporter * reporter)44 static void test_textencodings(skiatest::Reporter* reporter) {
45 const char text8[] = "ABCDEFGabcdefg0123456789";
46 uint16_t text16[sizeof(text8)];
47 int32_t text32[sizeof(text8)];
48 size_t len8 = strlen(text8);
49 size_t len16 = len8 * 2;
50 size_t len32 = len8 * 4;
51
52 // expand our 8bit chars to 16 and 32
53 for (size_t i = 0; i < len8; ++i) {
54 text32[i] = text16[i] = text8[i];
55 }
56
57 uint16_t glyphs8[sizeof(text8)];
58 uint16_t glyphs16[sizeof(text8)];
59 uint16_t glyphs32[sizeof(text8)];
60
61 SkPaint paint;
62
63 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
64 int count8 = paint.textToGlyphs(text8, len8, glyphs8);
65
66 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
67 int count16 = paint.textToGlyphs(text16, len16, glyphs16);
68
69 paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
70 int count32 = paint.textToGlyphs(text32, len32, glyphs32);
71
72 REPORTER_ASSERT(reporter, (int)len8 == count8);
73 REPORTER_ASSERT(reporter, (int)len8 == count16);
74 REPORTER_ASSERT(reporter, (int)len8 == count32);
75
76 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
77 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
78 }
79
DEF_TEST(Unicode,reporter)80 DEF_TEST(Unicode, reporter) {
81 test_uvs(reporter);
82 test_textencodings(reporter);
83 }
84