static void TestUTF(skiatest::Reporter* reporter) { static const struct { const char* fUtf8; SkUnichar fUni; } gTest[] = { { "a", 'a' }, { "\x7f", 0x7f }, { "\xC2\x80", 0x80 }, { "\xC3\x83", (3 << 6) | 3 }, { "\xDF\xBF", 0x7ff }, { "\xE0\xA0\x80", 0x800 }, { "\xE0\xB0\xB8", 0xC38 }, { "\xE3\x83\x83", (3 << 12) | (3 << 6) | 3 }, { "\xEF\xBF\xBF", 0xFFFF }, { "\xF0\x90\x80\x80", 0x10000 }, { "\xF3\x83\x83\x83", (3 << 18) | (3 << 12) | (3 << 6) | 3 } }; for (size_t i = 0; i < SK_ARRAY_COUNT(gTest); i++) { const char* p = gTest[i].fUtf8; int n = SkUTF8_CountUnichars(p); SkUnichar u0 = SkUTF8_ToUnichar(gTest[i].fUtf8); SkUnichar u1 = SkUTF8_NextUnichar(&p); REPORTER_ASSERT(reporter, n == 1); REPORTER_ASSERT(reporter, u0 == u1); REPORTER_ASSERT(reporter, u0 == gTest[i].fUni); REPORTER_ASSERT(reporter, p - gTest[i].fUtf8 == (int)strlen(gTest[i].fUtf8)); } test_utf16(reporter); test_search(reporter); test_refptr(reporter); }
int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding, uint16_t glyphs[], int maxGlyphCount) const { if (0 == byteLength) { return 0; } SkASSERT(text); int count = 0; // fix uninitialized warning (even though the switch is complete!) switch (encoding) { case kUTF8_SkTextEncoding: count = SkUTF8_CountUnichars((const char*)text, byteLength); break; case kUTF16_SkTextEncoding: count = SkUTF16_CountUnichars((const uint16_t*)text, SkToInt(byteLength >> 1)); break; case kUTF32_SkTextEncoding: count = SkToInt(byteLength >> 2); break; case kGlyphID_SkTextEncoding: count = SkToInt(byteLength >> 1); break; } if (nullptr == glyphs) { return count; } // TODO: unify/eliminate SkTypeface::Encoding with SkTextEncoding SkTypeface::Encoding typeface_encoding; switch (encoding) { case kUTF8_SkTextEncoding: typeface_encoding = SkTypeface::kUTF8_Encoding; break; case kUTF16_SkTextEncoding: typeface_encoding = SkTypeface::kUTF16_Encoding; break; case kUTF32_SkTextEncoding: typeface_encoding = SkTypeface::kUTF32_Encoding; break; default: SkASSERT(kGlyphID_SkTextEncoding == encoding); // we can early exit, since we already have glyphIDs memcpy(glyphs, text, count << 1); return count; } (void)fTypeface->charsToGlyphs(text, typeface_encoding, glyphs, count); return count; }
int SkPaintPriv::ValidCountText(const void* text, size_t length, SkPaint::TextEncoding encoding) { if (length == 0) { return 0; } switch (encoding) { case SkPaint::kUTF8_TextEncoding: return SkUTF8_CountUnichars(text, length); case SkPaint::kUTF16_TextEncoding: return SkUTF16_CountUnichars(text, length); case SkPaint::kUTF32_TextEncoding: return SkUTF32_CountUnichars(text, length); case SkPaint::kGlyphID_TextEncoding: if (SkIsAlign2(intptr_t(text)) && SkIsAlign2(length)) { return length >> 1; } break; } return 0; }
bool SkDisplayEvent::setProperty(int index, SkScriptValue& value) { SkASSERT(index == SK_PROPERTY(key) || index == SK_PROPERTY(keys)); SkASSERT(value.fType == SkType_String); SkString* string = value.fOperand.fString; const char* chars = string->c_str(); int count = SkUTF8_CountUnichars(chars); SkASSERT(count >= 1); code = (SkKey) SkUTF8_NextUnichar(&chars); fMax = code; SkASSERT(count == 1 || index == SK_PROPERTY(keys)); if (--count > 0) { SkASSERT(*chars == '-'); chars++; fMax = (SkKey) SkUTF8_NextUnichar(&chars); SkASSERT(fMax >= code); } return true; }