aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkPaint.cpp
diff options
context:
space:
mode:
authorGravatar Hal Canary <halcanary@google.com>2018-07-25 16:52:48 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-07-31 20:11:19 +0000
commitf107a2fd014cd39c489060f2cd1b99cd49c7d0be (patch)
tree5c324821344901869203bbe055be8d3f69f696cb /src/core/SkPaint.cpp
parent1935aa3d27cd4ed4aef2dc04360f247a541d4b00 (diff)
SkUTF
Create new header and namespace, `SkUTF` where we are putting all of our robust, well documented UTF-8, UTF-16, and UTF-32 functions: `SkUTF::{Count,Next,To}UTF{8,16,32}()`. SkUTF.h and SkUTF.cpp do not depend on the rest of Skia and are suitable for re-use in other modules. Some of the old UTF-{8,16} functions still live in SkUtils.h; their use will be phased out in future CLs. Also added more unit testing and cleaned up old tests. Removed functions that were unused outside of tests or used only once. Change-Id: Iaa59b8705abccf9c4ba082f855da368a0bad8380 Reviewed-on: https://skia-review.googlesource.com/143306 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Hal Canary <halcanary@google.com>
Diffstat (limited to 'src/core/SkPaint.cpp')
-rw-r--r--src/core/SkPaint.cpp104
1 files changed, 27 insertions, 77 deletions
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index 9b8431e591..15aa44eebc 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -399,9 +399,9 @@ int SkPaint::countText(const void* text, size_t byteLength) const {
SkASSERT(text != nullptr);
switch (this->getTextEncoding()) {
case kUTF8_TextEncoding:
- return SkUTF8_CountUnichars(text, byteLength);
+ return SkUTF::CountUTF8((const char*)text, byteLength);
case kUTF16_TextEncoding:
- return SkUTF16_CountUnichars(text, byteLength);
+ return SkUTF::CountUTF16((const uint16_t*)text, byteLength);
case kUTF32_TextEncoding:
return SkToInt(byteLength >> 2);
case kGlyphID_TextEncoding:
@@ -413,6 +413,13 @@ int SkPaint::countText(const void* text, size_t byteLength) const {
return 0;
}
+static SkTypeface::Encoding to_encoding(SkPaint::TextEncoding e) {
+ static_assert((int)SkTypeface::kUTF8_Encoding == (int)SkPaint::kUTF8_TextEncoding, "");
+ static_assert((int)SkTypeface::kUTF16_Encoding == (int)SkPaint::kUTF16_TextEncoding, "");
+ static_assert((int)SkTypeface::kUTF32_Encoding == (int)SkPaint::kUTF32_TextEncoding, "");
+ return (SkTypeface::Encoding)e;
+}
+
int SkPaint::textToGlyphs(const void* textData, size_t byteLength, uint16_t glyphs[]) const {
SkASSERT(textData != nullptr);
@@ -431,38 +438,16 @@ int SkPaint::textToGlyphs(const void* textData, size_t byteLength, uint16_t glyp
auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(*this);
- const char* text = (const char*)textData;
- const char* stop = text + byteLength;
+ const void* stop = (const char*)textData + byteLength;
uint16_t* gptr = glyphs;
+ const SkTypeface::Encoding encoding = to_encoding(this->getTextEncoding());
- switch (this->getTextEncoding()) {
- case SkPaint::kUTF8_TextEncoding:
- while (text < stop) {
- SkUnichar u = SkUTF8_NextUnicharWithError(&text, stop);
- if (u < 0) {
- return 0; // bad UTF-8 sequence
- }
- *gptr++ = cache->unicharToGlyph(u);
- }
- break;
- case SkPaint::kUTF16_TextEncoding: {
- const uint16_t* text16 = (const uint16_t*)text;
- const uint16_t* stop16 = (const uint16_t*)stop;
- while (text16 < stop16) {
- *gptr++ = cache->unicharToGlyph(SkUTF16_NextUnichar(&text16, stop16));
- }
- break;
- }
- case kUTF32_TextEncoding: {
- const int32_t* text32 = (const int32_t*)text;
- const int32_t* stop32 = (const int32_t*)stop;
- while (text32 < stop32) {
- *gptr++ = cache->unicharToGlyph(*text32++);
- }
- break;
+ while (textData < stop) {
+ SkUnichar unichar = SkUTFN_Next(encoding, &textData, stop);
+ if (unichar < 0) {
+ return 0; // bad UTF-N sequence
}
- default:
- SkDEBUGFAIL("unknown text encoding");
+ *gptr++ = cache->unicharToGlyph(unichar);
}
return SkToInt(gptr - glyphs);
}
@@ -487,41 +472,12 @@ bool SkPaint::containsText(const void* textData, size_t byteLength) const {
}
auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(*this);
-
- switch (this->getTextEncoding()) {
- case SkPaint::kUTF8_TextEncoding: {
- const char* text = static_cast<const char*>(textData);
- const char* stop = text + byteLength;
- while (text < stop) {
- if (0 == cache->unicharToGlyph(SkUTF8_NextUnichar(&text, stop))) {
- return false;
- }
- }
- break;
- }
- case SkPaint::kUTF16_TextEncoding: {
- const uint16_t* text = static_cast<const uint16_t*>(textData);
- const uint16_t* stop = text + (byteLength >> 1);
- while (text < stop) {
- if (0 == cache->unicharToGlyph(SkUTF16_NextUnichar(&text, stop))) {
- return false;
- }
- }
- break;
- }
- case SkPaint::kUTF32_TextEncoding: {
- const int32_t* text = static_cast<const int32_t*>(textData);
- const int32_t* stop = text + (byteLength >> 2);
- while (text < stop) {
- if (0 == cache->unicharToGlyph(*text++)) {
- return false;
- }
- }
- break;
- }
- default:
- SkDEBUGFAIL("unknown text encoding");
+ const void* stop = (const char*)textData + byteLength;
+ const SkTypeface::Encoding encoding = to_encoding(this->getTextEncoding());
+ while (textData < stop) {
+ if (0 == cache->unicharToGlyph(SkUTFN_Next(encoding, &textData, stop))) {
return false;
+ }
}
return true;
}
@@ -551,7 +507,7 @@ static const SkGlyph& sk_getMetrics_utf8_next(SkGlyphCache* cache,
SkASSERT(cache != nullptr);
SkASSERT(text != nullptr);
- return cache->getUnicharMetrics(SkUTF8_NextUnichar(text, stop));
+ return cache->getUnicharMetrics(SkUTF::NextUTF8(text, stop));
}
static const SkGlyph& sk_getMetrics_utf16_next(SkGlyphCache* cache,
@@ -561,7 +517,7 @@ static const SkGlyph& sk_getMetrics_utf16_next(SkGlyphCache* cache,
SkASSERT(text != nullptr);
return cache->getUnicharMetrics(
- SkUTF16_NextUnichar((const uint16_t**)text, (const uint16_t*)stop));
+ SkUTF::NextUTF16((const uint16_t**)text, (const uint16_t*)stop));
}
static const SkGlyph& sk_getMetrics_utf32_next(SkGlyphCache* cache,
@@ -570,10 +526,7 @@ static const SkGlyph& sk_getMetrics_utf32_next(SkGlyphCache* cache,
SkASSERT(cache != nullptr);
SkASSERT(text != nullptr);
- const int32_t* ptr = *(const int32_t**)text;
- SkUnichar uni = *ptr++;
- *text = (const char*)ptr;
- return cache->getUnicharMetrics(uni);
+ return cache->getUnicharMetrics(SkUTF::NextUTF32((const int32_t**)text, (const int32_t*)stop));
}
static const SkGlyph& sk_getMetrics_glyph_next(SkGlyphCache* cache,
@@ -595,7 +548,7 @@ static const SkGlyph& sk_getAdvance_utf8_next(SkGlyphCache* cache,
SkASSERT(cache != nullptr);
SkASSERT(text != nullptr);
- return cache->getUnicharAdvance(SkUTF8_NextUnichar(text, stop));
+ return cache->getUnicharAdvance(SkUTF::NextUTF8(text, stop));
}
static const SkGlyph& sk_getAdvance_utf16_next(SkGlyphCache* cache,
@@ -605,7 +558,7 @@ static const SkGlyph& sk_getAdvance_utf16_next(SkGlyphCache* cache,
SkASSERT(text != nullptr);
return cache->getUnicharAdvance(
- SkUTF16_NextUnichar((const uint16_t**)text, (const uint16_t*)stop));
+ SkUTF::NextUTF16((const uint16_t**)text, (const uint16_t*)stop));
}
static const SkGlyph& sk_getAdvance_utf32_next(SkGlyphCache* cache,
@@ -614,10 +567,7 @@ static const SkGlyph& sk_getAdvance_utf32_next(SkGlyphCache* cache,
SkASSERT(cache != nullptr);
SkASSERT(text != nullptr);
- const int32_t* ptr = *(const int32_t**)text;
- SkUnichar uni = *ptr++;
- *text = (const char*)ptr;
- return cache->getUnicharAdvance(uni);
+ return cache->getUnicharAdvance(SkUTF::NextUTF32((const int32_t**)text, (const int32_t*)stop));
}
static const SkGlyph& sk_getAdvance_glyph_next(SkGlyphCache* cache,