| 1 |
// Copyright 2019 Google LLC. |
| 2 |
#include "modules/skparagraph/src/ParagraphBuilderImpl.h" |
| 3 |
|
| 4 |
#include "include/core/SkRefCnt.h" |
| 5 |
#include "include/core/SkTypes.h" |
| 6 |
#include "modules/skparagraph/include/FontCollection.h" |
| 7 |
#include "modules/skparagraph/include/Paragraph.h" |
| 8 |
#include "modules/skparagraph/include/ParagraphBuilder.h" |
| 9 |
#include "modules/skparagraph/include/ParagraphStyle.h" |
| 10 |
#include "modules/skparagraph/include/TextStyle.h" |
| 11 |
#include "modules/skparagraph/src/ParagraphImpl.h" |
| 12 |
#include "modules/skunicode/include/SkUnicode.h" |
| 13 |
#include "src/core/SkStringUtils.h" |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <utility> |
| 17 |
|
| 18 |
namespace skia { |
| 19 |
namespace textlayout { |
| 20 |
|
| 21 |
std::unique_ptr<ParagraphBuilder> ParagraphBuilder::make(const ParagraphStyle& style, |
| 22 |
sk_sp<FontCollection> fontCollection, |
| 23 |
sk_sp<SkUnicode> unicode) { |
| 24 |
return ParagraphBuilderImpl::make(style, std::move(fontCollection), std::move(unicode)); |
| 25 |
} |
| 26 |
|
| 27 |
std::unique_ptr<ParagraphBuilder> ParagraphBuilderImpl::make(const ParagraphStyle& style, |
| 28 |
sk_sp<FontCollection> fontCollection, |
| 29 |
sk_sp<SkUnicode> unicode) { |
| 30 |
return std::make_unique<ParagraphBuilderImpl>(style, std::move(fontCollection), |
| 31 |
std::move(unicode)); |
| 32 |
} |
| 33 |
|
| 34 |
ParagraphBuilderImpl::ParagraphBuilderImpl( |
| 35 |
const ParagraphStyle& style, sk_sp<FontCollection> fontCollection, sk_sp<SkUnicode> unicode) |
| 36 |
: ParagraphBuilder() |
| 37 |
, fUtf8() |
| 38 |
, fFontCollection(std::move(fontCollection)) |
| 39 |
, fParagraphStyle(style) |
| 40 |
, fUnicode(std::move(unicode)) |
| 41 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 42 |
, fTextIsFinalized(false) |
| 43 |
, fUsingClientInfo(false) |
| 44 |
#endif |
| 45 |
{ |
| 46 |
SkASSERT(fFontCollection); |
| 47 |
startStyledBlock(); |
| 48 |
} |
| 49 |
|
| 50 |
ParagraphBuilderImpl::~ParagraphBuilderImpl() = default; |
| 51 |
|
| 52 |
void ParagraphBuilderImpl::pushStyle(const TextStyle& style) { |
| 53 |
fTextStyles.push_back(style); |
| 54 |
if (!fStyledBlocks.empty() && fStyledBlocks.back().fRange.end == fUtf8.size() && |
| 55 |
fStyledBlocks.back().fStyle == style) { |
| 56 |
// Just continue with the same style |
| 57 |
} else { |
| 58 |
// Go with the new style |
| 59 |
startStyledBlock(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void ParagraphBuilderImpl::pop() { |
| 64 |
if (!fTextStyles.empty()) { |
| 65 |
fTextStyles.pop_back(); |
| 66 |
} else { |
| 67 |
// In this case we use paragraph style and skip Pop operation |
| 68 |
SkDEBUGF("SkParagraphBuilder.Pop() called too many times.\n"); |
| 69 |
} |
| 70 |
|
| 71 |
this->startStyledBlock(); |
| 72 |
} |
| 73 |
|
| 74 |
const TextStyle& ParagraphBuilderImpl::internalPeekStyle() { |
| 75 |
if (fTextStyles.empty()) { |
| 76 |
return fParagraphStyle.getTextStyle(); |
| 77 |
} else { |
| 78 |
return fTextStyles.back(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
TextStyle ParagraphBuilderImpl::peekStyle() { |
| 83 |
return this->internalPeekStyle(); |
| 84 |
} |
| 85 |
|
| 86 |
void ParagraphBuilderImpl::addText(const std::u16string& text) { |
| 87 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 88 |
SkASSERT(!fTextIsFinalized); |
| 89 |
#endif |
| 90 |
auto utf8 = SkUnicode::convertUtf16ToUtf8(text); |
| 91 |
fUtf8.append(utf8); |
| 92 |
} |
| 93 |
|
| 94 |
void ParagraphBuilderImpl::addText(const char* text) { |
| 95 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 96 |
SkASSERT(!fTextIsFinalized); |
| 97 |
#endif |
| 98 |
fUtf8.append(text); |
| 99 |
} |
| 100 |
|
| 101 |
void ParagraphBuilderImpl::addText(const char* text, size_t len) { |
| 102 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 103 |
SkASSERT(!fTextIsFinalized); |
| 104 |
#endif |
| 105 |
fUtf8.append(text, len); |
| 106 |
} |
| 107 |
|
| 108 |
void ParagraphBuilderImpl::addPlaceholder(const PlaceholderStyle& placeholderStyle) { |
| 109 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 110 |
SkASSERT(!fTextIsFinalized); |
| 111 |
#endif |
| 112 |
addPlaceholder(placeholderStyle, false); |
| 113 |
} |
| 114 |
|
| 115 |
void ParagraphBuilderImpl::addPlaceholder(const PlaceholderStyle& placeholderStyle, bool lastOne) { |
| 116 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 117 |
// The very last placeholder is added automatically |
| 118 |
// and only AFTER finalize() is called |
| 119 |
SkASSERT(!fTextIsFinalized || lastOne); |
| 120 |
#endif |
| 121 |
if (!fUtf8.isEmpty() && !lastOne) { |
| 122 |
// We keep the very last text style |
| 123 |
this->endRunIfNeeded(); |
| 124 |
} |
| 125 |
|
| 126 |
BlockRange stylesBefore(fPlaceholders.empty() ? 0 : fPlaceholders.back().fBlocksBefore.end + 1, |
| 127 |
fStyledBlocks.size()); |
| 128 |
TextRange textBefore(fPlaceholders.empty() ? 0 : fPlaceholders.back().fRange.end, |
| 129 |
fUtf8.size()); |
| 130 |
auto start = fUtf8.size(); |
| 131 |
auto topStyle = internalPeekStyle(); |
| 132 |
if (!lastOne) { |
| 133 |
pushStyle(topStyle.cloneForPlaceholder()); |
| 134 |
addText(std::u16string(1ull, 0xFFFC)); |
| 135 |
pop(); |
| 136 |
} |
| 137 |
auto end = fUtf8.size(); |
| 138 |
fPlaceholders.emplace_back(start, end, placeholderStyle, topStyle, stylesBefore, textBefore); |
| 139 |
} |
| 140 |
|
| 141 |
void ParagraphBuilderImpl::endRunIfNeeded() { |
| 142 |
if (fStyledBlocks.empty()) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
auto& last = fStyledBlocks.back(); |
| 147 |
if (last.fRange.start == fUtf8.size()) { |
| 148 |
fStyledBlocks.pop_back(); |
| 149 |
} else { |
| 150 |
last.fRange.end = fUtf8.size(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
void ParagraphBuilderImpl::startStyledBlock() { |
| 155 |
endRunIfNeeded(); |
| 156 |
fStyledBlocks.emplace_back(fUtf8.size(), fUtf8.size(), internalPeekStyle()); |
| 157 |
} |
| 158 |
|
| 159 |
void ParagraphBuilderImpl::finalize() { |
| 160 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 161 |
if (fTextIsFinalized) { |
| 162 |
return; |
| 163 |
} |
| 164 |
#endif |
| 165 |
if (!fUtf8.isEmpty()) { |
| 166 |
this->endRunIfNeeded(); |
| 167 |
} |
| 168 |
|
| 169 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 170 |
fTextIsFinalized = true; |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
std::unique_ptr<Paragraph> ParagraphBuilderImpl::Build() { |
| 175 |
this->finalize(); |
| 176 |
// Add one fake placeholder with the rest of the text |
| 177 |
this->addPlaceholder(PlaceholderStyle(), true); |
| 178 |
|
| 179 |
fUTF8IndexForUTF16Index.clear(); |
| 180 |
fUTF16IndexForUTF8Index.clear(); |
| 181 |
|
| 182 |
SkASSERT_RELEASE(fUnicode); |
| 183 |
return std::make_unique<ParagraphImpl>( |
| 184 |
fUtf8, fParagraphStyle, fStyledBlocks, fPlaceholders, fFontCollection, fUnicode); |
| 185 |
} |
| 186 |
|
| 187 |
SkSpan<char> ParagraphBuilderImpl::getText() { |
| 188 |
this->finalize(); |
| 189 |
return SkSpan<char>(fUtf8.isEmpty() ? nullptr : fUtf8.data(), fUtf8.size()); |
| 190 |
} |
| 191 |
|
| 192 |
const ParagraphStyle& ParagraphBuilderImpl::getParagraphStyle() const { |
| 193 |
return fParagraphStyle; |
| 194 |
} |
| 195 |
|
| 196 |
void ParagraphBuilderImpl::ensureUTF16Mapping() { |
| 197 |
fillUTF16MappingOnce([&] { |
| 198 |
SkUnicode::extractUtfConversionMapping( |
| 199 |
this->getText(), |
| 200 |
[&](size_t index) { fUTF8IndexForUTF16Index.emplace_back(index); }, |
| 201 |
[&](size_t index) { fUTF16IndexForUTF8Index.emplace_back(index); }); |
| 202 |
}); |
| 203 |
} |
| 204 |
|
| 205 |
#if !defined(SK_DISABLE_LEGACY_CLIENT_UNICODE) && defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 206 |
void ParagraphBuilderImpl::setWordsUtf8(std::vector<SkUnicode::Position> wordsUtf8) { |
| 207 |
ensureUTF16Mapping(); |
| 208 |
std::vector<SkUnicode::Position> wordsUtf16; |
| 209 |
for (SkUnicode::Position indexUtf8: wordsUtf8) { |
| 210 |
wordsUtf16.emplace_back(fUTF16IndexForUTF8Index[indexUtf8]); |
| 211 |
} |
| 212 |
setWordsUtf16(wordsUtf16); |
| 213 |
} |
| 214 |
|
| 215 |
void ParagraphBuilderImpl::setWordsUtf16(std::vector<SkUnicode::Position> wordsUtf16) { |
| 216 |
fUsingClientInfo = true; |
| 217 |
fWordsUtf16 = std::move(wordsUtf16); |
| 218 |
} |
| 219 |
|
| 220 |
void ParagraphBuilderImpl::setGraphemeBreaksUtf8(std::vector<SkUnicode::Position> graphemeBreaksUtf8) { |
| 221 |
fUsingClientInfo = true; |
| 222 |
fGraphemeBreaksUtf8 = std::move(graphemeBreaksUtf8); |
| 223 |
} |
| 224 |
|
| 225 |
void ParagraphBuilderImpl::setGraphemeBreaksUtf16(std::vector<SkUnicode::Position> graphemeBreaksUtf16) { |
| 226 |
ensureUTF16Mapping(); |
| 227 |
std::vector<SkUnicode::Position> graphemeBreaksUtf8; |
| 228 |
for (SkUnicode::Position indexUtf16: graphemeBreaksUtf16) { |
| 229 |
graphemeBreaksUtf8.emplace_back(fUTF8IndexForUTF16Index[indexUtf16]); |
| 230 |
} |
| 231 |
setGraphemeBreaksUtf8(graphemeBreaksUtf8); |
| 232 |
} |
| 233 |
|
| 234 |
void ParagraphBuilderImpl::setLineBreaksUtf8(std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf8) { |
| 235 |
fUsingClientInfo = true; |
| 236 |
fLineBreaksUtf8 = std::move(lineBreaksUtf8); |
| 237 |
} |
| 238 |
|
| 239 |
void ParagraphBuilderImpl::setLineBreaksUtf16(std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf16) { |
| 240 |
ensureUTF16Mapping(); |
| 241 |
std::vector<SkUnicode::LineBreakBefore> lineBreaksUtf8; |
| 242 |
for (SkUnicode::LineBreakBefore lineBreakUtf16: lineBreaksUtf16) { |
| 243 |
lineBreaksUtf8.emplace_back(SkUnicode::LineBreakBefore( |
| 244 |
fUTF8IndexForUTF16Index[lineBreakUtf16.pos], lineBreakUtf16.breakType)); |
| 245 |
} |
| 246 |
setLineBreaksUtf8(lineBreaksUtf8); |
| 247 |
} |
| 248 |
#endif |
| 249 |
|
| 250 |
void ParagraphBuilderImpl::Reset() { |
| 251 |
|
| 252 |
fTextStyles.clear(); |
| 253 |
fUtf8.reset(); |
| 254 |
fStyledBlocks.clear(); |
| 255 |
fPlaceholders.clear(); |
| 256 |
fUTF8IndexForUTF16Index.clear(); |
| 257 |
fUTF16IndexForUTF8Index.clear(); |
| 258 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 259 |
fWordsUtf16.clear(); |
| 260 |
fGraphemeBreaksUtf8.clear(); |
| 261 |
fLineBreaksUtf8.clear(); |
| 262 |
fTextIsFinalized = false; |
| 263 |
#endif |
| 264 |
startStyledBlock(); |
| 265 |
} |
| 266 |
|
| 267 |
bool ParagraphBuilderImpl::RequiresClientICU() { |
| 268 |
#if defined(SK_UNICODE_CLIENT_IMPLEMENTATION) |
| 269 |
return true; |
| 270 |
#else |
| 271 |
return false; |
| 272 |
#endif |
| 273 |
} |
| 274 |
|
| 275 |
} // namespace textlayout |
| 276 |
} // namespace skia |