Reland "Reland "Small changes for SkText that are not in experimental directory""
This reverts commit b51994990bb4cded2da9efac353d15fc851bf159.
Reason for revert: Fixed the test
Original change's description:
> Revert "Reland "Small changes for SkText that are not in experimental directory""
>
> This reverts commit ce32c8a074cc0b1a081e0104e3b0b050810eb49a.
>
> Reason for revert: Skottie test is broken
>
> Original change's description:
> > Reland "Small changes for SkText that are not in experimental directory"
> >
> > This reverts commit 481a58dfe0eddd6736885b324d3cf14f5c10addb.
> >
> > Reason for revert: Fixing the build
> >
> > Original change's description:
> > > Revert "Small changes for SkText that are not in experimental directory"
> > >
> > > This reverts commit 92f1bc0083bf2fa1d7eca2857c0d837a8dcf1f17.
> > >
> > > Reason for revert: Blocking Android roll.
> > >
> > > Original change's description:
> > > > Small changes for SkText that are not in experimental directory
> > > >
> > > > (also made utf 8<->16 conversion static on SkUnicode)
> > > >
> > > > Change-Id: Ie64d18ad21a35ec10bd0b350fb7887fb78a419d8
> > > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/448140
> > > > Reviewed-by: Mike Reed <reed@google.com>
> > > > Commit-Queue: Julia Lavrova <jlavrova@google.com>
> > >
> > > Change-Id: Ieb9eb0495dddfc0f9e9e74861b24efc0201fd520
> > > No-Presubmit: true
> > > No-Tree-Checks: true
> > > No-Try: true
> > > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/448656
> > > Auto-Submit: Brian Osman <brianosman@google.com>
> > > Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> > > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> >
> > Change-Id: I1e5859c9dd943c701d4c4e648bdc3e44412eca54
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/448676
> > Reviewed-by: Brian Osman <brianosman@google.com>
> > Commit-Queue: Julia Lavrova <jlavrova@google.com>
>
> Change-Id: Ib8e5ec05a5555f34106a0ee61878a8199b82bb82
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/448897
> Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
> Reviewed-by: Florin Malita <fmalita@google.com>
> Commit-Queue: Julia Lavrova <jlavrova@google.com>
Change-Id: I3bc103c0e1c495342a6673e80765b5f26862e8dc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/448898
Reviewed-by: Florin Malita <fmalita@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index f69f621..54dc80f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1981,6 +1981,7 @@
":test",
":tool_utils",
"experimental/skrive:tests",
+ "experimental/sktext:tests",
"modules/skottie:tests",
"modules/skparagraph:tests",
"modules/sksg:tests",
diff --git a/experimental/sktext/src/Line.cpp b/experimental/sktext/src/Line.cpp
index 268487e..a843cc4 100644
--- a/experimental/sktext/src/Line.cpp
+++ b/experimental/sktext/src/Line.cpp
@@ -13,10 +13,10 @@
, fWhitespaces(spaces.textRange())
, fTextWidth(stretch.width())
, fSpacesWidth(spaces.width())
- , fRunsInVisualOrder(std::move(visualOrder))
, fHorizontalOffset(0.0f)
, fVerticalOffset(verticalOffset)
- , fHardLineBreak(hardLineBreak){
+ , fHardLineBreak(hardLineBreak)
+ , fRunsInVisualOrder(std::move(visualOrder)) {
SkASSERT(stretch.isEmpty() ||
spaces.isEmpty() ||
diff --git a/experimental/sktext/src/Text.cpp b/experimental/sktext/src/Text.cpp
index 023b6d2..b82f2d8 100644
--- a/experimental/sktext/src/Text.cpp
+++ b/experimental/sktext/src/Text.cpp
@@ -24,7 +24,7 @@
// Fill out all code unit properties
unicodeText->fCodeUnitProperties.push_back_n(utf16.size() + 1, CodeUnitFlags::kNoCodeUnitFlag);
unicodeText->fUnicode->forEachCodepoint(unicodeText->fText8.c_str(), unicodeText->fText8.size(),
- [&unicodeText, &utf16Index](SkUnichar unichar, int32_t start, int32_t end) {
+ [&unicodeText, &utf16Index](SkUnichar unichar, int32_t start, int32_t end, size_t count) {
for (auto i = start; i < end; ++i) {
unicodeText->fUTF16FromUTF8[i] = utf16Index;
}
@@ -37,7 +37,7 @@
// Get white spaces
// TODO: It's a bug. We need to operate on utf16 indexes everywhere
unicodeText->fUnicode->forEachCodepoint(unicodeText->fText8.c_str(), unicodeText->fText8.size(),
- [&unicodeText](SkUnichar unichar, int32_t start, int32_t end) {
+ [&unicodeText](SkUnichar unichar, int32_t start, int32_t end, size_t count) {
if (unicodeText->fUnicode->isWhitespace(unichar)) {
for (auto i = start; i < end; ++i) {
unicodeText->fCodeUnitProperties[i] |= CodeUnitFlags::kPartOfWhiteSpace;
diff --git a/modules/skparagraph/src/ParagraphImpl.cpp b/modules/skparagraph/src/ParagraphImpl.cpp
index 44ef329..8873bc4 100644
--- a/modules/skparagraph/src/ParagraphImpl.cpp
+++ b/modules/skparagraph/src/ParagraphImpl.cpp
@@ -267,7 +267,7 @@
// Get all spaces
fUnicode->forEachCodepoint(fText.c_str(), fText.size(),
- [this](SkUnichar unichar, int32_t start, int32_t end) {
+ [this](SkUnichar unichar, int32_t start, int32_t end, int32_t count) {
if (fUnicode->isWhitespace(unichar)) {
for (auto i = start; i < end; ++i) {
fCodeUnitProperties[i] |= CodeUnitFlags::kPartOfWhiteSpaceBreak;
diff --git a/modules/skunicode/include/SkUnicode.h b/modules/skunicode/include/SkUnicode.h
index 280bb91..7978922 100644
--- a/modules/skunicode/include/SkUnicode.h
+++ b/modules/skunicode/include/SkUnicode.h
@@ -115,7 +115,6 @@
virtual bool isControl(SkUnichar utf8) = 0;
virtual bool isWhitespace(SkUnichar utf8) = 0;
virtual bool isSpace(SkUnichar utf8) = 0;
- virtual SkString convertUtf16ToUtf8(const std::u16string& utf16) = 0;
virtual SkString toUpper(const SkString&) = 0;
// Methods used in SkShaper and SkText
@@ -138,6 +137,43 @@
virtual bool getGraphemes
(const char utf8[], int utf8Units, std::vector<Position>* results) = 0;
+ static SkString convertUtf16ToUtf8(const char16_t * utf16, int utf16Units) {
+
+ int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, (uint16_t*)utf16, utf16Units);
+ if (utf8Units < 0) {
+ SkDEBUGF("Convert error: Invalid utf16 input");
+ return SkString();
+ }
+ SkAutoTArray<char> utf8(utf8Units);
+ SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8.data(), utf8Units, (uint16_t*)utf16, utf16Units);
+ SkASSERT(dstLen == utf8Units);
+
+ return SkString(utf8.data(), utf8Units);
+ }
+
+ static SkString convertUtf16ToUtf8(const std::u16string& utf16) {
+ return convertUtf16ToUtf8(utf16.c_str(), utf16.size());
+ }
+
+ static std::u16string convertUtf8ToUtf16(const char* utf8, int utf8Units) {
+
+ int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
+ if (utf16Units < 0) {
+ SkDEBUGF("Convert error: Invalid utf8 input");
+ return std::u16string();
+ }
+
+ SkAutoTArray<uint16_t> utf16(utf16Units);
+ SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16.data(), utf16Units, utf8, utf8Units);
+ SkASSERT(dstLen == utf16Units);
+
+ return std::u16string((char16_t *)utf16.data(), utf16Units);
+ }
+
+ static std::u16string convertUtf8ToUtf16(const SkString& utf8) {
+ return convertUtf8ToUtf16(utf8.c_str(), utf8.size());
+ }
+
template <typename Callback>
void forEachCodepoint(const char* utf8, int32_t utf8Units, Callback&& callback) {
const char* current = utf8;
@@ -147,7 +183,9 @@
SkUnichar unichar = SkUTF::NextUTF8(¤t, end);
if (unichar < 0) unichar = 0xFFFD;
auto after = current - utf8;
- callback(unichar, before, after);
+ uint16_t buffer[2];
+ size_t count = SkUTF::ToUTF16(unichar, buffer);
+ callback(unichar, before, after, count);
}
}
diff --git a/modules/skunicode/src/SkUnicode_icu.cpp b/modules/skunicode/src/SkUnicode_icu.cpp
index 7b33f2a..ee196f2 100644
--- a/modules/skunicode/src/SkUnicode_icu.cpp
+++ b/modules/skunicode/src/SkUnicode_icu.cpp
@@ -282,15 +282,11 @@
std::vector<BidiRegion>* bidiRegions) {
// Convert to UTF16 since for now bidi iterator only operates on utf16
- std::unique_ptr<uint16_t[]> utf16;
- auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
- if (utf16Units < 0) {
- return false;
- }
+ auto utf16 = convertUtf8ToUtf16(utf8, utf8Units);
// Create bidi iterator
UErrorCode status = U_ZERO_ERROR;
- SkUnicodeBidi bidi(sk_ubidi_openSized(utf16Units, 0, &status));
+ SkUnicodeBidi bidi(sk_ubidi_openSized(utf16.size(), 0, &status));
if (U_FAILURE(status)) {
SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
return false;
@@ -299,7 +295,7 @@
uint8_t bidiLevel = (dir == TextDirection::kLTR) ? UBIDI_LTR : UBIDI_RTL;
// The required lifetime of utf16 isn't well documented.
// It appears it isn't used after ubidi_setPara except through ubidi_getText.
- sk_ubidi_setPara(bidi.get(), (const UChar*)utf16.get(), utf16Units, bidiLevel, nullptr,
+ sk_ubidi_setPara(bidi.get(), (const UChar*)utf16.c_str(), utf16.size(), bidiLevel, nullptr,
&status);
if (U_FAILURE(status)) {
SkDEBUGF("Bidi error: %s", sk_u_errorName(status));
@@ -417,30 +413,6 @@
return true;
}
- static int utf8ToUtf16(const char* utf8, size_t utf8Units, std::unique_ptr<uint16_t[]>* utf16) {
- int utf16Units = SkUTF::UTF8ToUTF16(nullptr, 0, utf8, utf8Units);
- if (utf16Units < 0) {
- SkDEBUGF("Convert error: Invalid utf8 input");
- return utf16Units;
- }
- *utf16 = std::unique_ptr<uint16_t[]>(new uint16_t[utf16Units]);
- SkDEBUGCODE(int dstLen =) SkUTF::UTF8ToUTF16(utf16->get(), utf16Units, utf8, utf8Units);
- SkASSERT(dstLen == utf16Units);
- return utf16Units;
- }
-
- static int utf16ToUtf8(const uint16_t* utf16, size_t utf16Units, std::unique_ptr<char[]>* utf8) {
- int utf8Units = SkUTF::UTF16ToUTF8(nullptr, 0, utf16, utf16Units);
- if (utf8Units < 0) {
- SkDEBUGF("Convert error: Invalid utf16 input");
- return utf8Units;
- }
- *utf8 = std::unique_ptr<char[]>(new char[utf8Units]);
- SkDEBUGCODE(int dstLen =) SkUTF::UTF16ToUTF8(utf8->get(), utf8Units, utf16, utf16Units);
- SkASSERT(dstLen == utf8Units);
- return utf8Units;
- }
-
public:
~SkUnicode_icu() override { }
std::unique_ptr<SkBidiIterator> makeBidiIterator(const uint16_t text[], int count,
@@ -488,26 +460,12 @@
return property == U_LB_LINE_FEED || property == U_LB_MANDATORY_BREAK;
}
- SkString convertUtf16ToUtf8(const std::u16string& utf16) override {
- std::unique_ptr<char[]> utf8;
- auto utf8Units = SkUnicode_icu::utf16ToUtf8((uint16_t*)utf16.data(), utf16.size(), &utf8);
- if (utf8Units >= 0) {
- return SkString(utf8.get(), utf8Units);
- } else {
- return SkString();
- }
- }
-
SkString toUpper(const SkString& str) override {
// Convert to UTF16 since that's what ICU wants.
- std::unique_ptr<uint16_t[]> str16;
- const auto str16len = utf8ToUtf16(str.c_str(), str.size(), &str16);
- if (str16len <= 0) {
- return SkString();
- }
+ auto str16 = convertUtf8ToUtf16(str.c_str(), str.size());
UErrorCode icu_err = U_ZERO_ERROR;
- const auto upper16len = sk_u_strToUpper(nullptr, 0, (UChar*)(str16.get()), str16len,
+ const auto upper16len = sk_u_strToUpper(nullptr, 0, (UChar*)(str16.c_str()), str16.size(),
nullptr, &icu_err);
if (icu_err != U_BUFFER_OVERFLOW_ERROR || upper16len <= 0) {
return SkString();
@@ -516,17 +474,12 @@
SkAutoSTArray<128, uint16_t> upper16(upper16len);
icu_err = U_ZERO_ERROR;
sk_u_strToUpper((UChar*)(upper16.get()), SkToS32(upper16.size()),
- (UChar*)(str16.get()), str16len,
+ (UChar*)(str16.c_str()), str16.size(),
nullptr, &icu_err);
SkASSERT(!U_FAILURE(icu_err));
// ... and back to utf8 'cause that's what we want.
- std::unique_ptr<char[]> upper8;
- auto upper8len = utf16ToUtf8(upper16.data(), upper16.size(), &upper8);
-
- return upper8len >= 0
- ? SkString(upper8.get(), upper8len)
- : SkString();
+ return convertUtf16ToUtf8((char16_t*)upper16.get(), upper16.size());
}
bool getBidiRegions(const char utf8[],
@@ -551,13 +504,8 @@
bool getWords(const char utf8[], int utf8Units, std::vector<Position>* results) override {
// Convert to UTF16 since we want the results in utf16
- std::unique_ptr<uint16_t[]> utf16;
- auto utf16Units = utf8ToUtf16(utf8, utf8Units, &utf16);
- if (utf16Units < 0) {
- return false;
- }
-
- return extractWords(utf16.get(), utf16Units, results);
+ auto utf16 = convertUtf8ToUtf16(utf8, utf8Units);
+ return extractWords((uint16_t*)utf16.c_str(), utf16.size(), results);
}
bool getGraphemes(const char utf8[], int utf8Units, std::vector<Position>* results) override {