reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@google.com | dc5f76d | 2012-03-14 20:05:35 +0000 | [diff] [blame] | 8 | #include "SkPaint.h" |
reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 9 | #include "SkUtils.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 10 | #include "Test.h" |
reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 11 | |
| 12 | // Unicode Variation Selector ranges: inclusive |
| 13 | #define UVS_MIN0 0x180B |
| 14 | #define UVS_MAX0 0x180D |
| 15 | #define UVS_MIN1 0xFE00 |
| 16 | #define UVS_MAX1 0xFE0F |
| 17 | #define UVS_MIN2 0xE0100 |
| 18 | #define UVS_MAX2 0xE01EF |
| 19 | |
| 20 | static bool isUVS(SkUnichar uni) { |
| 21 | return (uni >= UVS_MIN0 && uni <= UVS_MAX0) || |
| 22 | (uni >= UVS_MIN1 && uni <= UVS_MAX1) || |
| 23 | (uni >= UVS_MIN2 && uni <= UVS_MAX2); |
| 24 | } |
| 25 | |
| 26 | static void test_uvs(skiatest::Reporter* reporter) { |
| 27 | // [min, max], [min, max] ... inclusive |
| 28 | static const SkUnichar gRanges[] = { |
| 29 | UVS_MIN0, UVS_MAX0, UVS_MIN1, UVS_MAX1, UVS_MIN2, UVS_MAX2 |
| 30 | }; |
| 31 | |
| 32 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRanges); i += 2) { |
| 33 | for (SkUnichar uni = gRanges[i] - 8; uni <= gRanges[i+1] + 8; ++uni) { |
| 34 | bool uvs0 = isUVS(uni); |
| 35 | bool uvs1 = SkUnichar_IsVariationSelector(uni); |
| 36 | REPORTER_ASSERT(reporter, uvs0 == uvs1); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
reed@google.com | dc5f76d | 2012-03-14 20:05:35 +0000 | [diff] [blame] | 41 | // Simple test to ensure that when we call textToGlyphs, we get the same |
| 42 | // result (for the same text) when using UTF8, UTF16, UTF32. |
| 43 | // TODO: make the text more complex (i.e. incorporate chars>7bits) |
| 44 | static void test_textencodings(skiatest::Reporter* reporter) { |
| 45 | const char text8[] = "ABCDEFGabcdefg0123456789"; |
| 46 | uint16_t text16[sizeof(text8)]; |
| 47 | int32_t text32[sizeof(text8)]; |
| 48 | size_t len8 = strlen(text8); |
| 49 | size_t len16 = len8 * 2; |
| 50 | size_t len32 = len8 * 4; |
| 51 | |
| 52 | // expand our 8bit chars to 16 and 32 |
| 53 | for (size_t i = 0; i < len8; ++i) { |
| 54 | text32[i] = text16[i] = text8[i]; |
| 55 | } |
| 56 | |
| 57 | uint16_t glyphs8[sizeof(text8)]; |
| 58 | uint16_t glyphs16[sizeof(text8)]; |
| 59 | uint16_t glyphs32[sizeof(text8)]; |
| 60 | |
| 61 | SkPaint paint; |
| 62 | |
| 63 | paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); |
| 64 | int count8 = paint.textToGlyphs(text8, len8, glyphs8); |
| 65 | |
| 66 | paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); |
| 67 | int count16 = paint.textToGlyphs(text16, len16, glyphs16); |
| 68 | |
| 69 | paint.setTextEncoding(SkPaint::kUTF32_TextEncoding); |
| 70 | int count32 = paint.textToGlyphs(text32, len32, glyphs32); |
| 71 | |
bsalomon@google.com | 6f86c3e | 2012-03-22 21:29:10 +0000 | [diff] [blame] | 72 | REPORTER_ASSERT(reporter, (int)len8 == count8); |
| 73 | REPORTER_ASSERT(reporter, (int)len8 == count16); |
| 74 | REPORTER_ASSERT(reporter, (int)len8 == count32); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 75 | |
reed@google.com | dc5f76d | 2012-03-14 20:05:35 +0000 | [diff] [blame] | 76 | REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs16, count8 * sizeof(uint16_t))); |
| 77 | REPORTER_ASSERT(reporter, !memcmp(glyphs8, glyphs32, count8 * sizeof(uint16_t))); |
| 78 | } |
| 79 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 80 | DEF_TEST(Unicode, reporter) { |
reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 81 | test_uvs(reporter); |
reed@google.com | dc5f76d | 2012-03-14 20:05:35 +0000 | [diff] [blame] | 82 | test_textencodings(reporter); |
reed@google.com | 419f433 | 2011-12-21 15:21:32 +0000 | [diff] [blame] | 83 | } |