blob: 68f0ecf9907b0533a879145bf252386d04c3d332 [file] [log] [blame]
reed@google.com419f4332011-12-21 15:21:32 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@google.com419f4332011-12-21 15:21:32 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.comdc5f76d2012-03-14 20:05:35 +000010#include "SkPaint.h"
reed@google.com419f4332011-12-21 15:21:32 +000011#include "SkUtils.h"
12
13// Unicode Variation Selector ranges: inclusive
14#define UVS_MIN0 0x180B
15#define UVS_MAX0 0x180D
16#define UVS_MIN1 0xFE00
17#define UVS_MAX1 0xFE0F
18#define UVS_MIN2 0xE0100
19#define UVS_MAX2 0xE01EF
20
21static bool isUVS(SkUnichar uni) {
22 return (uni >= UVS_MIN0 && uni <= UVS_MAX0) ||
23 (uni >= UVS_MIN1 && uni <= UVS_MAX1) ||
24 (uni >= UVS_MIN2 && uni <= UVS_MAX2);
25}
26
27static void test_uvs(skiatest::Reporter* reporter) {
28 // [min, max], [min, max] ... inclusive
29 static const SkUnichar gRanges[] = {
30 UVS_MIN0, UVS_MAX0, UVS_MIN1, UVS_MAX1, UVS_MIN2, UVS_MAX2
31 };
32
33 for (size_t i = 0; i < SK_ARRAY_COUNT(gRanges); i += 2) {
34 for (SkUnichar uni = gRanges[i] - 8; uni <= gRanges[i+1] + 8; ++uni) {
35 bool uvs0 = isUVS(uni);
36 bool uvs1 = SkUnichar_IsVariationSelector(uni);
37 REPORTER_ASSERT(reporter, uvs0 == uvs1);
38 }
39 }
40}
41
reed@google.comdc5f76d2012-03-14 20:05:35 +000042// Simple test to ensure that when we call textToGlyphs, we get the same
43// result (for the same text) when using UTF8, UTF16, UTF32.
44// TODO: make the text more complex (i.e. incorporate chars>7bits)
45static void test_textencodings(skiatest::Reporter* reporter) {
46 const char text8[] = "ABCDEFGabcdefg0123456789";
47 uint16_t text16[sizeof(text8)];
48 int32_t text32[sizeof(text8)];
49 size_t len8 = strlen(text8);
50 size_t len16 = len8 * 2;
51 size_t len32 = len8 * 4;
52
53 // expand our 8bit chars to 16 and 32
54 for (size_t i = 0; i < len8; ++i) {
55 text32[i] = text16[i] = text8[i];
56 }
57
58 uint16_t glyphs8[sizeof(text8)];
59 uint16_t glyphs16[sizeof(text8)];
60 uint16_t glyphs32[sizeof(text8)];
61
62 SkPaint paint;
63
64 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
65 int count8 = paint.textToGlyphs(text8, len8, glyphs8);
66
67 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
68 int count16 = paint.textToGlyphs(text16, len16, glyphs16);
69
70 paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
71 int count32 = paint.textToGlyphs(text32, len32, glyphs32);
72
bsalomon@google.com6f86c3e2012-03-22 21:29:10 +000073 REPORTER_ASSERT(reporter, (int)len8 == count8);
74 REPORTER_ASSERT(reporter, (int)len8 == count16);
75 REPORTER_ASSERT(reporter, (int)len8 == count32);
rmistry@google.comd6176b02012-08-23 18:14:13 +000076
reed@google.comdc5f76d2012-03-14 20:05:35 +000077 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
78 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
79}
80
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000081DEF_TEST(Unicode, reporter) {
reed@google.com419f4332011-12-21 15:21:32 +000082 test_uvs(reporter);
reed@google.comdc5f76d2012-03-14 20:05:35 +000083 test_textencodings(reporter);
reed@google.com419f4332011-12-21 15:21:32 +000084}