UnicodeTest.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include "include/core/SkFont.h"
  8. #include "include/core/SkPaint.h"
  9. #include "src/utils/SkUTF.h"
  10. #include "tests/Test.h"
  11. // Simple test to ensure that when we call textToGlyphs, we get the same
  12. // result (for the same text) when using UTF8, UTF16, UTF32.
  13. // TODO: make the text more complex (i.e. incorporate chars>7bits)
  14. DEF_TEST(Unicode_textencodings, reporter) {
  15. const char text8[] = "ABCDEFGabcdefg0123456789";
  16. uint16_t text16[sizeof(text8)];
  17. int32_t text32[sizeof(text8)];
  18. size_t len8 = strlen(text8);
  19. size_t len16 = len8 * 2;
  20. size_t len32 = len8 * 4;
  21. // expand our 8bit chars to 16 and 32
  22. for (size_t i = 0; i < len8; ++i) {
  23. text32[i] = text16[i] = text8[i];
  24. }
  25. uint16_t glyphs8[sizeof(text8)];
  26. uint16_t glyphs16[sizeof(text8)];
  27. uint16_t glyphs32[sizeof(text8)];
  28. SkFont font;
  29. int count8 = font.textToGlyphs(text8, len8, SkTextEncoding::kUTF8, glyphs8, SK_ARRAY_COUNT(glyphs8));
  30. int count16 = font.textToGlyphs(text16, len16, SkTextEncoding::kUTF16, glyphs16, SK_ARRAY_COUNT(glyphs16));
  31. int count32 = font.textToGlyphs(text32, len32, SkTextEncoding::kUTF32, glyphs32, SK_ARRAY_COUNT(glyphs32));
  32. REPORTER_ASSERT(reporter, (int)len8 == count8);
  33. REPORTER_ASSERT(reporter, (int)len8 == count16);
  34. REPORTER_ASSERT(reporter, (int)len8 == count32);
  35. REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t)));
  36. REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t)));
  37. }
  38. #include "include/core/SkFont.h"
  39. #include "src/core/SkFontPriv.h"
  40. DEF_TEST(glyphs_to_unichars, reporter) {
  41. SkFont font;
  42. const int N = 52;
  43. SkUnichar uni[N];
  44. for (int i = 0; i < 26; ++i) {
  45. uni[i + 0] = i + 'A';
  46. uni[i + 26] = i + 'a';
  47. }
  48. uint16_t glyphs[N];
  49. font.textToGlyphs(uni, sizeof(uni), SkTextEncoding::kUTF32, glyphs, N);
  50. SkUnichar uni2[N];
  51. SkFontPriv::GlyphsToUnichars(font, glyphs, N, uni2);
  52. REPORTER_ASSERT(reporter, memcmp(uni, uni2, sizeof(uni)) == 0);
  53. }