blob: 1328fdcb87ffe149149fc4237213e67458f3c222 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkFont.h"
9#include "include/core/SkPaint.h"
10#include "src/utils/SkUTF.h"
11#include "tests/Test.h"
reed@google.com419f4332011-12-21 15:21:32 +000012
reed@google.comdc5f76d2012-03-14 20:05:35 +000013// Simple test to ensure that when we call textToGlyphs, we get the same
14// result (for the same text) when using UTF8, UTF16, UTF32.
15// TODO: make the text more complex (i.e. incorporate chars>7bits)
Hal Canaryf107a2f2018-07-25 16:52:48 -040016DEF_TEST(Unicode_textencodings, reporter) {
reed@google.comdc5f76d2012-03-14 20:05:35 +000017 const char text8[] = "ABCDEFGabcdefg0123456789";
18 uint16_t text16[sizeof(text8)];
19 int32_t text32[sizeof(text8)];
20 size_t len8 = strlen(text8);
21 size_t len16 = len8 * 2;
22 size_t len32 = len8 * 4;
23
24 // expand our 8bit chars to 16 and 32
25 for (size_t i = 0; i < len8; ++i) {
26 text32[i] = text16[i] = text8[i];
27 }
28
29 uint16_t glyphs8[sizeof(text8)];
30 uint16_t glyphs16[sizeof(text8)];
31 uint16_t glyphs32[sizeof(text8)];
32
Mike Reed2e6db182018-12-15 13:45:33 -050033 SkFont font;
reed@google.comdc5f76d2012-03-14 20:05:35 +000034
Ben Wagner51e15a62019-05-07 15:38:46 -040035 int count8 = font.textToGlyphs(text8, len8, SkTextEncoding::kUTF8, glyphs8, SK_ARRAY_COUNT(glyphs8));
36 int count16 = font.textToGlyphs(text16, len16, SkTextEncoding::kUTF16, glyphs16, SK_ARRAY_COUNT(glyphs16));
37 int count32 = font.textToGlyphs(text32, len32, SkTextEncoding::kUTF32, glyphs32, SK_ARRAY_COUNT(glyphs32));
reed@google.comdc5f76d2012-03-14 20:05:35 +000038
bsalomon@google.com6f86c3e2012-03-22 21:29:10 +000039 REPORTER_ASSERT(reporter, (int)len8 == count8);
40 REPORTER_ASSERT(reporter, (int)len8 == count16);
41 REPORTER_ASSERT(reporter, (int)len8 == count32);
rmistry@google.comd6176b02012-08-23 18:14:13 +000042
reed@google.comdc5f76d2012-03-14 20:05:35 +000043 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
44 REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
45}
Mike Reeda0b54cd2018-12-10 15:26:09 -050046
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "include/core/SkFont.h"
48#include "src/core/SkFontPriv.h"
Mike Reeda0b54cd2018-12-10 15:26:09 -050049
50DEF_TEST(glyphs_to_unichars, reporter) {
51 SkFont font;
52
53 const int N = 52;
54 SkUnichar uni[N];
55 for (int i = 0; i < 26; ++i) {
56 uni[i + 0] = i + 'A';
57 uni[i + 26] = i + 'a';
58 }
59 uint16_t glyphs[N];
Ben Wagner51e15a62019-05-07 15:38:46 -040060 font.textToGlyphs(uni, sizeof(uni), SkTextEncoding::kUTF32, glyphs, N);
Mike Reeda0b54cd2018-12-10 15:26:09 -050061
62 SkUnichar uni2[N];
63 SkFontPriv::GlyphsToUnichars(font, glyphs, N, uni2);
64 REPORTER_ASSERT(reporter, memcmp(uni, uni2, sizeof(uni)) == 0);
65}
66