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