1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkFont.h"
9cb93a386Sopenharmony_ci#include "include/core/SkPaint.h"
10cb93a386Sopenharmony_ci#include "src/utils/SkUTF.h"
11cb93a386Sopenharmony_ci#include "tests/Test.h"
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci// Simple test to ensure that when we call textToGlyphs, we get the same
14cb93a386Sopenharmony_ci// result (for the same text) when using UTF8, UTF16, UTF32.
15cb93a386Sopenharmony_ci// TODO: make the text more complex (i.e. incorporate chars>7bits)
16cb93a386Sopenharmony_ciDEF_TEST(Unicode_textencodings, reporter) {
17cb93a386Sopenharmony_ci    const char text8[] = "ABCDEFGabcdefg0123456789";
18cb93a386Sopenharmony_ci    uint16_t text16[sizeof(text8)];
19cb93a386Sopenharmony_ci    int32_t  text32[sizeof(text8)];
20cb93a386Sopenharmony_ci    size_t len8 = strlen(text8);
21cb93a386Sopenharmony_ci    size_t len16 = len8 * 2;
22cb93a386Sopenharmony_ci    size_t len32 = len8 * 4;
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    // expand our 8bit chars to 16 and 32
25cb93a386Sopenharmony_ci    for (size_t i = 0; i < len8; ++i) {
26cb93a386Sopenharmony_ci        text32[i] = text16[i] = text8[i];
27cb93a386Sopenharmony_ci    }
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci    uint16_t glyphs8[sizeof(text8)];
30cb93a386Sopenharmony_ci    uint16_t glyphs16[sizeof(text8)];
31cb93a386Sopenharmony_ci    uint16_t glyphs32[sizeof(text8)];
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    SkFont font;
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    int count8  = font.textToGlyphs(text8,  len8,  SkTextEncoding::kUTF8,  glyphs8,  SK_ARRAY_COUNT(glyphs8));
36cb93a386Sopenharmony_ci    int count16 = font.textToGlyphs(text16, len16, SkTextEncoding::kUTF16, glyphs16, SK_ARRAY_COUNT(glyphs16));
37cb93a386Sopenharmony_ci    int count32 = font.textToGlyphs(text32, len32, SkTextEncoding::kUTF32, glyphs32, SK_ARRAY_COUNT(glyphs32));
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, (int)len8 == count8);
40cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, (int)len8 == count16);
41cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, (int)len8 == count32);
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
44cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
45cb93a386Sopenharmony_ci}
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci#include "include/core/SkFont.h"
48cb93a386Sopenharmony_ci#include "src/core/SkFontPriv.h"
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ciDEF_TEST(glyphs_to_unichars, reporter) {
51cb93a386Sopenharmony_ci    SkFont font;
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    const int N = 52;
54cb93a386Sopenharmony_ci    SkUnichar uni[N];
55cb93a386Sopenharmony_ci    for (int i = 0; i < 26; ++i) {
56cb93a386Sopenharmony_ci        uni[i +  0] = i + 'A';
57cb93a386Sopenharmony_ci        uni[i + 26] = i + 'a';
58cb93a386Sopenharmony_ci    }
59cb93a386Sopenharmony_ci    uint16_t glyphs[N];
60cb93a386Sopenharmony_ci    font.textToGlyphs(uni, sizeof(uni), SkTextEncoding::kUTF32, glyphs, N);
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    SkUnichar uni2[N];
63cb93a386Sopenharmony_ci    SkFontPriv::GlyphsToUnichars(font, glyphs, N, uni2);
64cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, memcmp(uni, uni2, sizeof(uni)) == 0);
65cb93a386Sopenharmony_ci}
66cb93a386Sopenharmony_ci
67