xref: /aosp_15_r20/external/skia/tests/UnicodeTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkFont.h"
9 #include "include/core/SkFontTypes.h"
10 #include "src/base/SkUTF.h"
11 #include "src/core/SkFontPriv.h"
12 #include "tests/Test.h"
13 #include "tools/fonts/FontToolUtils.h"
14 
15 #include <cstdint>
16 #include <cstring>
17 #include <string>
18 
19 // Simple test to ensure that when we call textToGlyphs, we get the same
20 // result (for the same text) when using UTF8, UTF16, UTF32.
21 // TODO: make the text more complex (i.e. incorporate chars>7bits)
DEF_TEST(Unicode_textencodings,reporter)22 DEF_TEST(Unicode_textencodings, reporter) {
23     const char text8[] = "ABCDEFGabcdefg0123456789";
24     uint16_t text16[sizeof(text8)];
25     int32_t  text32[sizeof(text8)];
26     size_t len8 = strlen(text8);
27     size_t len16 = len8 * 2;
28     size_t len32 = len8 * 4;
29 
30     // expand our 8bit chars to 16 and 32
31     for (size_t i = 0; i < len8; ++i) {
32         text32[i] = text16[i] = text8[i];
33     }
34 
35     uint16_t glyphs8[sizeof(text8)];
36     uint16_t glyphs16[sizeof(text8)];
37     uint16_t glyphs32[sizeof(text8)];
38 
39     SkFont font = ToolUtils::DefaultFont();
40 
41     int count8  = font.textToGlyphs(text8,  len8,  SkTextEncoding::kUTF8,  glyphs8,  std::size(glyphs8));
42     int count16 = font.textToGlyphs(text16, len16, SkTextEncoding::kUTF16, glyphs16, std::size(glyphs16));
43     int count32 = font.textToGlyphs(text32, len32, SkTextEncoding::kUTF32, glyphs32, std::size(glyphs32));
44 
45     REPORTER_ASSERT(reporter, (int)len8 == count8);
46     REPORTER_ASSERT(reporter, (int)len8 == count16);
47     REPORTER_ASSERT(reporter, (int)len8 == count32);
48 
49     REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
50     REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
51 }
52 
DEF_TEST(glyphs_to_unichars,reporter)53 DEF_TEST(glyphs_to_unichars, reporter) {
54     SkFont font = ToolUtils::DefaultFont();
55 
56     const int N = 52;
57     SkUnichar uni[N];
58     for (int i = 0; i < 26; ++i) {
59         uni[i +  0] = i + 'A';
60         uni[i + 26] = i + 'a';
61     }
62     uint16_t glyphs[N];
63     font.textToGlyphs(uni, sizeof(uni), SkTextEncoding::kUTF32, glyphs, N);
64 
65     SkUnichar uni2[N];
66     SkFontPriv::GlyphsToUnichars(font, glyphs, N, uni2);
67     REPORTER_ASSERT(reporter, memcmp(uni, uni2, sizeof(uni)) == 0);
68 }
69 
70