skia2/tests/UnicodeTest.cpp
Mike Reed a0b54cd589 add test (and fix) for glyphsToUnichars
Bug: skia:
Change-Id: I2defcea7e9a37bdd203acc4ca97fc47a7f57cd43
Reviewed-on: https://skia-review.googlesource.com/c/176221
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2018-12-10 21:11:49 +00:00

71 lines
2.1 KiB
C++

/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkPaint.h"
#include "SkUTF.h"
#include "Test.h"
// Simple test to ensure that when we call textToGlyphs, we get the same
// result (for the same text) when using UTF8, UTF16, UTF32.
// TODO: make the text more complex (i.e. incorporate chars>7bits)
DEF_TEST(Unicode_textencodings, reporter) {
const char text8[] = "ABCDEFGabcdefg0123456789";
uint16_t text16[sizeof(text8)];
int32_t text32[sizeof(text8)];
size_t len8 = strlen(text8);
size_t len16 = len8 * 2;
size_t len32 = len8 * 4;
// expand our 8bit chars to 16 and 32
for (size_t i = 0; i < len8; ++i) {
text32[i] = text16[i] = text8[i];
}
uint16_t glyphs8[sizeof(text8)];
uint16_t glyphs16[sizeof(text8)];
uint16_t glyphs32[sizeof(text8)];
SkPaint paint;
paint.setTextEncoding(kUTF8_SkTextEncoding);
int count8 = paint.textToGlyphs(text8, len8, glyphs8);
paint.setTextEncoding(kUTF16_SkTextEncoding);
int count16 = paint.textToGlyphs(text16, len16, glyphs16);
paint.setTextEncoding(kUTF32_SkTextEncoding);
int count32 = paint.textToGlyphs(text32, len32, glyphs32);
REPORTER_ASSERT(reporter, (int)len8 == count8);
REPORTER_ASSERT(reporter, (int)len8 == count16);
REPORTER_ASSERT(reporter, (int)len8 == count32);
REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
}
#include "SkFont.h"
#include "SkFontPriv.h"
DEF_TEST(glyphs_to_unichars, reporter) {
SkFont font;
const int N = 52;
SkUnichar uni[N];
for (int i = 0; i < 26; ++i) {
uni[i + 0] = i + 'A';
uni[i + 26] = i + 'a';
}
uint16_t glyphs[N];
font.textToGlyphs(uni, sizeof(uni), kUTF32_SkTextEncoding, glyphs, N);
SkUnichar uni2[N];
SkFontPriv::GlyphsToUnichars(font, glyphs, N, uni2);
REPORTER_ASSERT(reporter, memcmp(uni, uni2, sizeof(uni)) == 0);
}