blob: 5ed92d4b34a023966d2b47de752c93cc63ae5452 [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.comdc5f76d2012-03-14 20:05:35 +00008#include "SkPaint.h"
reed@google.com419f4332011-12-21 15:21:32 +00009#include "SkUtils.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000010#include "Test.h"
reed@google.com419f4332011-12-21 15:21:32 +000011
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
20static 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
26static 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
reed@google.comdc5f76d2012-03-14 20:05:35 +000041// 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)
44static 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
bsalomon@google.com6f86c3e2012-03-22 21:29:10 +000072 REPORTER_ASSERT(reporter, (int)len8 == count8);
73 REPORTER_ASSERT(reporter, (int)len8 == count16);
74 REPORTER_ASSERT(reporter, (int)len8 == count32);
rmistry@google.comd6176b02012-08-23 18:14:13 +000075
reed@google.comdc5f76d2012-03-14 20:05:35 +000076 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
77 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
78}
79
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +000080DEF_TEST(Unicode, reporter) {
reed@google.com419f4332011-12-21 15:21:32 +000081 test_uvs(reporter);
reed@google.comdc5f76d2012-03-14 20:05:35 +000082 test_textencodings(reporter);
reed@google.com419f4332011-12-21 15:21:32 +000083}