• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkFont.h"
9 #include "SkPaint.h"
10 #include "SkUTF.h"
11 #include "Test.h"
12 
13 // 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)
DEF_TEST(Unicode_textencodings,reporter)16 DEF_TEST(Unicode_textencodings, reporter) {
17     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 
33     SkFont font;
34 
35     int count8  = font.textToGlyphs(text8,  len8,  kUTF8_SkTextEncoding,  glyphs8,  SK_ARRAY_COUNT(glyphs8));
36     int count16 = font.textToGlyphs(text16, len16, kUTF16_SkTextEncoding, glyphs16, SK_ARRAY_COUNT(glyphs16));
37     int count32 = font.textToGlyphs(text32, len32, kUTF32_SkTextEncoding, glyphs32, SK_ARRAY_COUNT(glyphs32));
38 
39     REPORTER_ASSERT(reporter, (int)len8 == count8);
40     REPORTER_ASSERT(reporter, (int)len8 == count16);
41     REPORTER_ASSERT(reporter, (int)len8 == count32);
42 
43     REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
44     REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
45 }
46 
47 #include "SkFont.h"
48 #include "SkFontPriv.h"
49 
DEF_TEST(glyphs_to_unichars,reporter)50 DEF_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];
60     font.textToGlyphs(uni, sizeof(uni), kUTF32_SkTextEncoding, glyphs, N);
61 
62     SkUnichar uni2[N];
63     SkFontPriv::GlyphsToUnichars(font, glyphs, N, uni2);
64     REPORTER_ASSERT(reporter, memcmp(uni, uni2, sizeof(uni)) == 0);
65 }
66 
67