| 1 |
// Copyright 2019 Google LLC. |
| 2 |
#include "modules/skparagraph/src/OneLineShaper.h" |
| 3 |
|
| 4 |
#include "modules/skparagraph/src/Iterators.h" |
| 5 |
#include "modules/skshaper/include/SkShaper_harfbuzz.h" |
| 6 |
#include "src/base/SkUTF.h" |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
#include <cstdint> |
| 10 |
#include <unordered_set> |
| 11 |
|
| 12 |
using namespace skia_private; |
| 13 |
|
| 14 |
namespace skia { |
| 15 |
namespace textlayout { |
| 16 |
|
| 17 |
void OneLineShaper::commitRunBuffer(const RunInfo&) { |
| 18 |
|
| 19 |
fCurrentRun->commit(); |
| 20 |
|
| 21 |
auto oldUnresolvedCount = fUnresolvedBlocks.size(); |
| 22 |
/* |
| 23 |
SkDebugf("Run [%zu:%zu)\n", fCurrentRun->fTextRange.start, fCurrentRun->fTextRange.end); |
| 24 |
for (size_t i = 0; i < fCurrentRun->size(); ++i) { |
| 25 |
SkDebugf("[%zu] %hu %u %f\n", i, fCurrentRun->fGlyphs[i], fCurrentRun->fClusterIndexes[i], fCurrentRun->fPositions[i].fX); |
| 26 |
} |
| 27 |
*/ |
| 28 |
// Find all unresolved blocks |
| 29 |
sortOutGlyphs([&](GlyphRange block){ |
| 30 |
if (block.width() == 0) { |
| 31 |
return; |
| 32 |
} |
| 33 |
addUnresolvedWithRun(block); |
| 34 |
}); |
| 35 |
|
| 36 |
// Fill all the gaps between unresolved blocks with resolved ones |
| 37 |
if (oldUnresolvedCount == fUnresolvedBlocks.size()) { |
| 38 |
// No unresolved blocks added - we resolved the block with one run entirely |
| 39 |
addFullyResolved(); |
| 40 |
return; |
| 41 |
} else if (oldUnresolvedCount == fUnresolvedBlocks.size() - 1) { |
| 42 |
auto& unresolved = fUnresolvedBlocks.back(); |
| 43 |
if (fCurrentRun->textRange() == unresolved.fText) { |
| 44 |
// Nothing was resolved; preserve the initial run if it makes sense |
| 45 |
auto& front = fUnresolvedBlocks.front(); |
| 46 |
if (front.fRun != nullptr) { |
| 47 |
unresolved.fRun = front.fRun; |
| 48 |
unresolved.fGlyphs = front.fGlyphs; |
| 49 |
} |
| 50 |
return; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
fillGaps(oldUnresolvedCount); |
| 55 |
} |
| 56 |
|
| 57 |
#ifdef SK_DEBUG |
| 58 |
void OneLineShaper::printState() { |
| 59 |
SkDebugf("Resolved: %zu\n", fResolvedBlocks.size()); |
| 60 |
for (auto& resolved : fResolvedBlocks) { |
| 61 |
if (resolved.fRun == nullptr) { |
| 62 |
SkDebugf("[%zu:%zu) unresolved\n", |
| 63 |
resolved.fText.start, resolved.fText.end); |
| 64 |
continue; |
| 65 |
} |
| 66 |
SkString name("???"); |
| 67 |
if (resolved.fRun->fFont.getTypeface() != nullptr) { |
| 68 |
resolved.fRun->fFont.getTypeface()->getFamilyName(&name); |
| 69 |
} |
| 70 |
SkDebugf("[%zu:%zu) ", resolved.fGlyphs.start, resolved.fGlyphs.end); |
| 71 |
SkDebugf("[%zu:%zu) with %s\n", |
| 72 |
resolved.fText.start, resolved.fText.end, |
| 73 |
name.c_str()); |
| 74 |
} |
| 75 |
|
| 76 |
auto size = fUnresolvedBlocks.size(); |
| 77 |
SkDebugf("Unresolved: %zu\n", size); |
| 78 |
for (const auto& unresolved : fUnresolvedBlocks) { |
| 79 |
SkDebugf("[%zu:%zu)\n", unresolved.fText.start, unresolved.fText.end); |
| 80 |
} |
| 81 |
} |
| 82 |
#endif |
| 83 |
|
| 84 |
void OneLineShaper::fillGaps(size_t startingCount) { |
| 85 |
// Fill out gaps between all unresolved blocks |
| 86 |
TextRange resolvedTextLimits = fCurrentRun->fTextRange; |
| 87 |
if (!fCurrentRun->leftToRight()) { |
| 88 |
std::swap(resolvedTextLimits.start, resolvedTextLimits.end); |
| 89 |
} |
| 90 |
TextIndex resolvedTextStart = resolvedTextLimits.start; |
| 91 |
GlyphIndex resolvedGlyphsStart = 0; |
| 92 |
|
| 93 |
auto begin = fUnresolvedBlocks.begin(); |
| 94 |
auto end = fUnresolvedBlocks.end(); |
| 95 |
begin += startingCount; // Skip the old ones, do the new ones |
| 96 |
TextRange prevText = EMPTY_TEXT; |
| 97 |
for (; begin != end; ++begin) { |
| 98 |
auto& unresolved = *begin; |
| 99 |
|
| 100 |
if (unresolved.fText == prevText) { |
| 101 |
// Clean up repetitive blocks that appear inside the same grapheme block |
| 102 |
unresolved.fText = EMPTY_TEXT; |
| 103 |
continue; |
| 104 |
} else { |
| 105 |
prevText = unresolved.fText; |
| 106 |
} |
| 107 |
|
| 108 |
TextRange resolvedText(resolvedTextStart, fCurrentRun->leftToRight() ? unresolved.fText.start : unresolved.fText.end); |
| 109 |
if (resolvedText.width() > 0) { |
| 110 |
if (!fCurrentRun->leftToRight()) { |
| 111 |
std::swap(resolvedText.start, resolvedText.end); |
| 112 |
} |
| 113 |
|
| 114 |
GlyphRange resolvedGlyphs(resolvedGlyphsStart, unresolved.fGlyphs.start); |
| 115 |
RunBlock resolved(fCurrentRun, resolvedText, resolvedGlyphs, resolvedGlyphs.width()); |
| 116 |
|
| 117 |
if (resolvedGlyphs.width() == 0) { |
| 118 |
// Extend the unresolved block with an empty resolved |
| 119 |
if (unresolved.fText.end <= resolved.fText.start) { |
| 120 |
unresolved.fText.end = resolved.fText.end; |
| 121 |
} |
| 122 |
if (unresolved.fText.start >= resolved.fText.end) { |
| 123 |
unresolved.fText.start = resolved.fText.start; |
| 124 |
} |
| 125 |
} else { |
| 126 |
fResolvedBlocks.emplace_back(resolved); |
| 127 |
} |
| 128 |
} |
| 129 |
resolvedGlyphsStart = unresolved.fGlyphs.end; |
| 130 |
resolvedTextStart = fCurrentRun->leftToRight() |
| 131 |
? unresolved.fText.end |
| 132 |
: unresolved.fText.start; |
| 133 |
} |
| 134 |
|
| 135 |
TextRange resolvedText(resolvedTextStart,resolvedTextLimits.end); |
| 136 |
if (resolvedText.width() > 0) { |
| 137 |
if (!fCurrentRun->leftToRight()) { |
| 138 |
std::swap(resolvedText.start, resolvedText.end); |
| 139 |
} |
| 140 |
|
| 141 |
GlyphRange resolvedGlyphs(resolvedGlyphsStart, fCurrentRun->size()); |
| 142 |
RunBlock resolved(fCurrentRun, resolvedText, resolvedGlyphs, resolvedGlyphs.width()); |
| 143 |
fResolvedBlocks.emplace_back(resolved); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void OneLineShaper::finish(const Block& block, SkScalar height, SkScalar& advanceX) { |
| 148 |
auto blockText = block.fRange; |
| 149 |
|
| 150 |
// Add all unresolved blocks to resolved blocks |
| 151 |
while (!fUnresolvedBlocks.empty()) { |
| 152 |
auto unresolved = fUnresolvedBlocks.front(); |
| 153 |
fUnresolvedBlocks.pop_front(); |
| 154 |
if (unresolved.fText.width() == 0) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
fResolvedBlocks.emplace_back(unresolved); |
| 158 |
fUnresolvedGlyphs += unresolved.fGlyphs.width(); |
| 159 |
fParagraph->addUnresolvedCodepoints(unresolved.fText); |
| 160 |
} |
| 161 |
|
| 162 |
// Sort all pieces by text |
| 163 |
std::sort(fResolvedBlocks.begin(), fResolvedBlocks.end(), |
| 164 |
[](const RunBlock& a, const RunBlock& b) { |
| 165 |
return a.fText.start < b.fText.start; |
| 166 |
}); |
| 167 |
|
| 168 |
// Go through all of them |
| 169 |
size_t lastTextEnd = blockText.start; |
| 170 |
for (auto& resolvedBlock : fResolvedBlocks) { |
| 171 |
|
| 172 |
if (resolvedBlock.fText.end <= blockText.start) { |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
if (resolvedBlock.fRun != nullptr) { |
| 177 |
fParagraph->fFontSwitches.emplace_back(resolvedBlock.fText.start, resolvedBlock.fRun->fFont); |
| 178 |
} |
| 179 |
|
| 180 |
auto run = resolvedBlock.fRun; |
| 181 |
auto glyphs = resolvedBlock.fGlyphs; |
| 182 |
auto text = resolvedBlock.fText; |
| 183 |
if (lastTextEnd != text.start) { |
| 184 |
SkDEBUGF("Text ranges mismatch: ...:%zu] - [%zu:%zu] (%zu-%zu)\n", |
| 185 |
lastTextEnd, text.start, text.end, glyphs.start, glyphs.end); |
| 186 |
SkASSERT(false); |
| 187 |
} |
| 188 |
lastTextEnd = text.end; |
| 189 |
|
| 190 |
if (resolvedBlock.isFullyResolved()) { |
| 191 |
// Just move the entire run |
| 192 |
resolvedBlock.fRun->fIndex = this->fParagraph->fRuns.size(); |
| 193 |
this->fParagraph->fRuns.emplace_back(*resolvedBlock.fRun); |
| 194 |
resolvedBlock.fRun.reset(); |
| 195 |
continue; |
| 196 |
} else if (run == nullptr) { |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
auto runAdvance = SkVector::Make(run->posX(glyphs.end) - run->posX(glyphs.start), run->fAdvance.fY); |
| 201 |
const SkShaper::RunHandler::RunInfo info = { |
| 202 |
run->fFont, |
| 203 |
run->fBidiLevel, |
| 204 |
run->fScript, |
| 205 |
run->fLanguage.c_str(), |
| 206 |
runAdvance, |
| 207 |
glyphs.width(), |
| 208 |
SkShaper::RunHandler::Range(text.start - run->fClusterStart, text.width()) |
| 209 |
}; |
| 210 |
this->fParagraph->fRuns.emplace_back( |
| 211 |
this->fParagraph, |
| 212 |
info, |
| 213 |
run->fClusterStart, |
| 214 |
height, |
| 215 |
block.fStyle.getHalfLeading(), |
| 216 |
block.fStyle.getBaselineShift(), |
| 217 |
this->fParagraph->fRuns.size(), |
| 218 |
advanceX |
| 219 |
); |
| 220 |
auto piece = &this->fParagraph->fRuns.back(); |
| 221 |
|
| 222 |
// TODO: Optimize copying |
| 223 |
SkPoint zero = {run->fPositions[glyphs.start].fX, 0}; |
| 224 |
for (size_t i = glyphs.start; i <= glyphs.end; ++i) { |
| 225 |
|
| 226 |
auto index = i - glyphs.start; |
| 227 |
if (i < glyphs.end) { |
| 228 |
// There are only n glyphs in a run, not n+1. |
| 229 |
piece->fGlyphs[index] = run->fGlyphs[i]; |
| 230 |
|
| 231 |
// fClusterIndexes n+1 is already set to the end of the run. |
| 232 |
// Do not attempt to overwrite this value with the cluster index |
| 233 |
// that starts the next Run. |
| 234 |
// It is assumed later that all clusters in a Run are contained by the Run. |
| 235 |
piece->fClusterIndexes[index] = run->fClusterIndexes[i]; |
| 236 |
} |
| 237 |
piece->fPositions[index] = run->fPositions[i] - zero; |
| 238 |
piece->fOffsets[index] = run->fOffsets[i]; |
| 239 |
piece->addX(index, advanceX); |
| 240 |
} |
| 241 |
|
| 242 |
// Carve out the line text out of the entire run text |
| 243 |
fAdvance.fX += runAdvance.fX; |
| 244 |
fAdvance.fY = std::max(fAdvance.fY, runAdvance.fY); |
| 245 |
} |
| 246 |
|
| 247 |
advanceX = fAdvance.fX; |
| 248 |
if (lastTextEnd != blockText.end) { |
| 249 |
SkDEBUGF("Last range mismatch: %zu - %zu\n", lastTextEnd, blockText.end); |
| 250 |
SkASSERT(false); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
// Make it [left:right) regardless of a text direction |
| 255 |
TextRange OneLineShaper::normalizeTextRange(GlyphRange glyphRange) { |
| 256 |
|
| 257 |
if (fCurrentRun->leftToRight()) { |
| 258 |
return TextRange(clusterIndex(glyphRange.start), clusterIndex(glyphRange.end)); |
| 259 |
} else { |
| 260 |
return TextRange(clusterIndex(glyphRange.end - 1), |
| 261 |
glyphRange.start > 0 |
| 262 |
? clusterIndex(glyphRange.start - 1) |
| 263 |
: fCurrentRun->fTextRange.end); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void OneLineShaper::addFullyResolved() { |
| 268 |
if (this->fCurrentRun->size() == 0) { |
| 269 |
return; |
| 270 |
} |
| 271 |
RunBlock resolved(fCurrentRun, |
| 272 |
this->fCurrentRun->fTextRange, |
| 273 |
GlyphRange(0, this->fCurrentRun->size()), |
| 274 |
this->fCurrentRun->size()); |
| 275 |
fResolvedBlocks.emplace_back(resolved); |
| 276 |
} |
| 277 |
|
| 278 |
void OneLineShaper::addUnresolvedWithRun(GlyphRange glyphRange) { |
| 279 |
auto extendedText = this->clusteredText(glyphRange); // It also modifies glyphRange if needed |
| 280 |
RunBlock unresolved(fCurrentRun, extendedText, glyphRange, 0); |
| 281 |
if (unresolved.fGlyphs.width() == fCurrentRun->size()) { |
| 282 |
SkASSERT(unresolved.fText.width() == fCurrentRun->fTextRange.width()); |
| 283 |
} else if (!fUnresolvedBlocks.empty()) { |
| 284 |
auto& lastUnresolved = fUnresolvedBlocks.back(); |
| 285 |
if (lastUnresolved.fRun != nullptr && |
| 286 |
lastUnresolved.fRun->fIndex == fCurrentRun->fIndex) { |
| 287 |
|
| 288 |
if (lastUnresolved.fText.end == unresolved.fText.start) { |
| 289 |
// Two pieces next to each other - can join them |
| 290 |
lastUnresolved.fText.end = unresolved.fText.end; |
| 291 |
lastUnresolved.fGlyphs.end = glyphRange.end; |
| 292 |
return; |
| 293 |
} else if(lastUnresolved.fText == unresolved.fText) { |
| 294 |
// Nothing was resolved; ignore it |
| 295 |
return; |
| 296 |
} else if (lastUnresolved.fText.contains(unresolved.fText)) { |
| 297 |
// We get here for the very first unresolved piece |
| 298 |
return; |
| 299 |
} else if (lastUnresolved.fText.intersects(unresolved.fText)) { |
| 300 |
// Few pieces of the same unresolved text block can ignore the second one |
| 301 |
lastUnresolved.fGlyphs.start = std::min(lastUnresolved.fGlyphs.start, glyphRange.start); |
| 302 |
lastUnresolved.fGlyphs.end = std::max(lastUnresolved.fGlyphs.end, glyphRange.end); |
| 303 |
lastUnresolved.fText = this->clusteredText(lastUnresolved.fGlyphs); |
| 304 |
return; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
fUnresolvedBlocks.emplace_back(unresolved); |
| 309 |
} |
| 310 |
|
| 311 |
// Glue whitespaces to the next/prev unresolved blocks |
| 312 |
// (so we don't have chinese text with english whitespaces broken into millions of tiny runs) |
| 313 |
void OneLineShaper::sortOutGlyphs(std::function<void(GlyphRange)>&& sortOutUnresolvedBLock) { |
| 314 |
|
| 315 |
GlyphRange block = EMPTY_RANGE; |
| 316 |
bool graphemeResolved = false; |
| 317 |
TextIndex graphemeStart = EMPTY_INDEX; |
| 318 |
for (size_t i = 0; i < fCurrentRun->size(); ++i) { |
| 319 |
|
| 320 |
ClusterIndex ci = clusterIndex(i); |
| 321 |
// Removing all pretty optimizations for whitespaces |
| 322 |
// because they get in a way of grapheme rounding |
| 323 |
// Inspect the glyph |
| 324 |
auto glyph = fCurrentRun->fGlyphs[i]; |
| 325 |
|
| 326 |
GraphemeIndex gi = fParagraph->findPreviousGraphemeBoundary(ci); |
| 327 |
if ((fCurrentRun->leftToRight() ? gi > graphemeStart : gi < graphemeStart) || graphemeStart == EMPTY_INDEX) { |
| 328 |
// This is the Flutter change |
| 329 |
// Do not count control codepoints as unresolved |
| 330 |
bool isControl8 = fParagraph->codeUnitHasProperty(ci, |
| 331 |
SkUnicode::CodeUnitFlags::kControl); |
| 332 |
// We only count glyph resolved if all the glyphs in its grapheme are resolved |
| 333 |
graphemeResolved = glyph != 0 || isControl8; |
| 334 |
graphemeStart = gi; |
| 335 |
} else if (glyph == 0) { |
| 336 |
// Found unresolved glyph - the entire grapheme is unresolved now |
| 337 |
graphemeResolved = false; |
| 338 |
} |
| 339 |
|
| 340 |
if (!graphemeResolved) { // Unresolved glyph and not control codepoint |
| 341 |
if (block.start == EMPTY_INDEX) { |
| 342 |
// Start new unresolved block |
| 343 |
block.start = i; |
| 344 |
block.end = EMPTY_INDEX; |
| 345 |
} else { |
| 346 |
// Keep skipping unresolved block |
| 347 |
} |
| 348 |
} else { // Resolved glyph or control codepoint |
| 349 |
if (block.start == EMPTY_INDEX) { |
| 350 |
// Keep skipping resolved code points |
| 351 |
} else { |
| 352 |
// This is the end of unresolved block |
| 353 |
block.end = i; |
| 354 |
sortOutUnresolvedBLock(block); |
| 355 |
block = EMPTY_RANGE; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// One last block could have been left |
| 361 |
if (block.start != EMPTY_INDEX) { |
| 362 |
block.end = fCurrentRun->size(); |
| 363 |
sortOutUnresolvedBLock(block); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
void OneLineShaper::iterateThroughFontStyles(TextRange textRange, |
| 368 |
SkSpan<Block> styleSpan, |
| 369 |
const ShapeSingleFontVisitor& visitor) { |
| 370 |
Block combinedBlock; |
| 371 |
TArray<SkShaper::Feature> features; |
| 372 |
|
| 373 |
auto addFeatures = [&features](const Block& block) { |
| 374 |
for (auto& ff : block.fStyle.getFontFeatures()) { |
| 375 |
if (ff.fName.size() != 4) { |
| 376 |
SkDEBUGF("Incorrect font feature: %s=%d\n", ff.fName.c_str(), ff.fValue); |
| 377 |
continue; |
| 378 |
} |
| 379 |
SkShaper::Feature feature = { |
| 380 |
SkSetFourByteTag(ff.fName[0], ff.fName[1], ff.fName[2], ff.fName[3]), |
| 381 |
SkToU32(ff.fValue), |
| 382 |
block.fRange.start, |
| 383 |
block.fRange.end |
| 384 |
}; |
| 385 |
features.emplace_back(feature); |
| 386 |
} |
| 387 |
// Disable ligatures if letter spacing is enabled. |
| 388 |
if (block.fStyle.getLetterSpacing() > 0) { |
| 389 |
features.emplace_back(SkShaper::Feature{ |
| 390 |
SkSetFourByteTag('l', 'i', 'g', 'a'), 0, block.fRange.start, block.fRange.end |
| 391 |
}); |
| 392 |
} |
| 393 |
}; |
| 394 |
|
| 395 |
for (auto& block : styleSpan) { |
| 396 |
BlockRange blockRange(std::max(block.fRange.start, textRange.start), std::min(block.fRange.end, textRange.end)); |
| 397 |
if (blockRange.empty()) { |
| 398 |
continue; |
| 399 |
} |
| 400 |
SkASSERT(combinedBlock.fRange.width() == 0 || combinedBlock.fRange.end == block.fRange.start); |
| 401 |
|
| 402 |
if (!combinedBlock.fRange.empty()) { |
| 403 |
if (block.fStyle.matchOneAttribute(StyleType::kFont, combinedBlock.fStyle)) { |
| 404 |
combinedBlock.add(blockRange); |
| 405 |
addFeatures(block); |
| 406 |
continue; |
| 407 |
} |
| 408 |
// Resolve all characters in the block for this style |
| 409 |
visitor(combinedBlock, features); |
| 410 |
} |
| 411 |
|
| 412 |
combinedBlock.fRange = blockRange; |
| 413 |
combinedBlock.fStyle = block.fStyle; |
| 414 |
features.clear(); |
| 415 |
addFeatures(block); |
| 416 |
} |
| 417 |
|
| 418 |
visitor(combinedBlock, features); |
| 419 |
#ifdef SK_DEBUG |
| 420 |
//printState(); |
| 421 |
#endif |
| 422 |
} |
| 423 |
|
| 424 |
void OneLineShaper::matchResolvedFonts(const TextStyle& textStyle, |
| 425 |
const TypefaceVisitor& visitor) { |
| 426 |
std::vector<sk_sp<SkTypeface>> typefaces = fParagraph->fFontCollection->findTypefaces(textStyle.getFontFamilies(), textStyle.getFontStyle(), textStyle.getFontArguments()); |
| 427 |
|
| 428 |
for (const auto& typeface : typefaces) { |
| 429 |
if (visitor(typeface) == Resolved::Everything) { |
| 430 |
// Resolved everything |
| 431 |
return; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
if (fParagraph->fFontCollection->fontFallbackEnabled()) { |
| 436 |
// Give fallback a clue |
| 437 |
// Some unresolved subblocks might be resolved with different fallback fonts |
| 438 |
std::vector<RunBlock> hopelessBlocks; |
| 439 |
while (!fUnresolvedBlocks.empty()) { |
| 440 |
auto unresolvedRange = fUnresolvedBlocks.front().fText; |
| 441 |
auto unresolvedText = fParagraph->text(unresolvedRange); |
| 442 |
const char* ch = unresolvedText.data(); |
| 443 |
const char* chEnd = unresolvedText.data() + unresolvedText.size(); |
| 444 |
// We have the global cache for all already found typefaces for SkUnichar |
| 445 |
// but we still need to keep track of all SkUnichars used in this unresolved block |
| 446 |
THashSet<SkUnichar> alreadyTriedCodepoints; |
| 447 |
THashSet<SkTypefaceID> alreadyTriedTypefaces; |
| 448 |
while (true) { |
| 449 |
if (ch == chEnd) { |
| 450 |
// Not a single codepoint could be resolved but we finished the block |
| 451 |
hopelessBlocks.push_back(fUnresolvedBlocks.front()); |
| 452 |
fUnresolvedBlocks.pop_front(); |
| 453 |
break; |
| 454 |
} |
| 455 |
|
| 456 |
// See if we can switch to the next DIFFERENT codepoint/emoji |
| 457 |
SkUnichar codepoint = -1; |
| 458 |
SkUnichar emojiStart = -1; |
| 459 |
// We may loop until we find a new codepoint/emoji run |
| 460 |
while (ch != chEnd) { |
| 461 |
emojiStart = OneLineShaper::getEmojiSequenceStart( |
| 462 |
fParagraph->fUnicode.get(), |
| 463 |
&ch, |
| 464 |
chEnd); |
| 465 |
if (emojiStart != -1) { |
| 466 |
// We do not keep a cache of emoji runs, but we need to move the cursor |
| 467 |
break; |
| 468 |
} else { |
| 469 |
codepoint = SkUTF::NextUTF8WithReplacement(&ch, chEnd); |
| 470 |
if (!alreadyTriedCodepoints.contains(codepoint)) { |
| 471 |
alreadyTriedCodepoints.add(codepoint); |
| 472 |
break; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
SkASSERT(codepoint != -1 || emojiStart != -1); |
| 478 |
|
| 479 |
sk_sp<SkTypeface> typeface = nullptr; |
| 480 |
if (emojiStart == -1) { |
| 481 |
// First try to find in in a cache |
| 482 |
FontKey fontKey(codepoint, textStyle.getFontStyle(), textStyle.getLocale(), textStyle.getFontArguments()); |
| 483 |
auto found = fFallbackFonts.find(fontKey); |
| 484 |
if (found != nullptr) { |
| 485 |
typeface = *found; |
| 486 |
} |
| 487 |
if (typeface == nullptr) { |
| 488 |
typeface = fParagraph->fFontCollection->defaultFallback( |
| 489 |
codepoint, |
| 490 |
textStyle.getFontFamilies(), |
| 491 |
textStyle.getFontStyle(), |
| 492 |
textStyle.getLocale(), |
| 493 |
textStyle.getFontArguments()); |
| 494 |
if (typeface != nullptr) { |
| 495 |
fFallbackFonts.set(fontKey, typeface); |
| 496 |
} |
| 497 |
} |
| 498 |
} else { |
| 499 |
typeface = fParagraph->fFontCollection->defaultEmojiFallback( |
| 500 |
emojiStart, |
| 501 |
textStyle.getFontStyle(), |
| 502 |
textStyle.getLocale()); |
| 503 |
} |
| 504 |
|
| 505 |
if (typeface == nullptr) { |
| 506 |
// There is no fallback font for this character, |
| 507 |
// so move on to the next character. |
| 508 |
continue; |
| 509 |
} |
| 510 |
|
| 511 |
// Check if we already tried this font on this text range |
| 512 |
if (!alreadyTriedTypefaces.contains(typeface->uniqueID())) { |
| 513 |
alreadyTriedTypefaces.add(typeface->uniqueID()); |
| 514 |
} else { |
| 515 |
continue; |
| 516 |
} |
| 517 |
|
| 518 |
auto resolvedBlocksBefore = fResolvedBlocks.size(); |
| 519 |
auto resolved = visitor(typeface); |
| 520 |
if (resolved == Resolved::Everything) { |
| 521 |
if (hopelessBlocks.empty()) { |
| 522 |
// Resolved everything, no need to try another font |
| 523 |
return; |
| 524 |
} else if (resolvedBlocksBefore < fResolvedBlocks.size()) { |
| 525 |
// There are some resolved blocks |
| 526 |
resolved = Resolved::Something; |
| 527 |
} else { |
| 528 |
// All blocks are hopeless |
| 529 |
resolved = Resolved::Nothing; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
if (resolved == Resolved::Something) { |
| 534 |
// Resolved something, no need to try another codepoint |
| 535 |
break; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
// Return hopeless blocks back |
| 541 |
for (auto& block : hopelessBlocks) { |
| 542 |
fUnresolvedBlocks.emplace_front(block); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
bool OneLineShaper::iterateThroughShapingRegions(const ShapeVisitor& shape) { |
| 548 |
|
| 549 |
size_t bidiIndex = 0; |
| 550 |
|
| 551 |
SkScalar advanceX = 0; |
| 552 |
for (auto& placeholder : fParagraph->fPlaceholders) { |
| 553 |
|
| 554 |
if (placeholder.fTextBefore.width() > 0) { |
| 555 |
// Shape the text by bidi regions |
| 556 |
while (bidiIndex < fParagraph->fBidiRegions.size()) { |
| 557 |
SkUnicode::BidiRegion& bidiRegion = fParagraph->fBidiRegions[bidiIndex]; |
| 558 |
auto start = std::max(bidiRegion.start, placeholder.fTextBefore.start); |
| 559 |
auto end = std::min(bidiRegion.end, placeholder.fTextBefore.end); |
| 560 |
|
| 561 |
// Set up the iterators (the style iterator points to a bigger region that it could |
| 562 |
TextRange textRange(start, end); |
| 563 |
auto blockRange = fParagraph->findAllBlocks(textRange); |
| 564 |
if (!blockRange.empty()) { |
| 565 |
SkSpan<Block> styleSpan(fParagraph->blocks(blockRange)); |
| 566 |
|
| 567 |
// Shape the text between placeholders |
| 568 |
if (!shape(textRange, styleSpan, advanceX, start, bidiRegion.level)) { |
| 569 |
return false; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
if (end == bidiRegion.end) { |
| 574 |
++bidiIndex; |
| 575 |
} else /*if (end == placeholder.fTextBefore.end)*/ { |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
if (placeholder.fRange.width() == 0) { |
| 582 |
continue; |
| 583 |
} |
| 584 |
|
| 585 |
// Get the placeholder font |
| 586 |
std::vector<sk_sp<SkTypeface>> typefaces = fParagraph->fFontCollection->findTypefaces( |
| 587 |
placeholder.fTextStyle.getFontFamilies(), |
| 588 |
placeholder.fTextStyle.getFontStyle(), |
| 589 |
placeholder.fTextStyle.getFontArguments()); |
| 590 |
sk_sp<SkTypeface> typeface = typefaces.empty() ? nullptr : typefaces.front(); |
| 591 |
SkFont font(typeface, placeholder.fTextStyle.getFontSize()); |
| 592 |
|
| 593 |
font.setEdging(placeholder.fTextStyle.getFontEdging()); |
| 594 |
font.setHinting(placeholder.fTextStyle.getFontHinting()); |
| 595 |
font.setSubpixel(placeholder.fTextStyle.getSubpixel()); |
| 596 |
|
| 597 |
// "Shape" the placeholder |
| 598 |
uint8_t bidiLevel = (bidiIndex < fParagraph->fBidiRegions.size()) |
| 599 |
? fParagraph->fBidiRegions[bidiIndex].level |
| 600 |
: 2; |
| 601 |
const SkShaper::RunHandler::RunInfo runInfo = { |
| 602 |
font, |
| 603 |
bidiLevel, |
| 604 |
0, |
| 605 |
"", |
| 606 |
SkPoint::Make(placeholder.fStyle.fWidth, placeholder.fStyle.fHeight), |
| 607 |
1, |
| 608 |
SkShaper::RunHandler::Range(0, placeholder.fRange.width()) |
| 609 |
}; |
| 610 |
auto& run = fParagraph->fRuns.emplace_back(this->fParagraph, |
| 611 |
runInfo, |
| 612 |
placeholder.fRange.start, |
| 613 |
0.0f, |
| 614 |
0.0f, |
| 615 |
false, |
| 616 |
fParagraph->fRuns.size(), |
| 617 |
advanceX); |
| 618 |
|
| 619 |
run.fPositions[0] = { advanceX, 0 }; |
| 620 |
run.fOffsets[0] = {0, 0}; |
| 621 |
run.fClusterIndexes[0] = 0; |
| 622 |
run.fPlaceholderIndex = &placeholder - fParagraph->fPlaceholders.begin(); |
| 623 |
advanceX += placeholder.fStyle.fWidth; |
| 624 |
} |
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
bool OneLineShaper::shape() { |
| 629 |
|
| 630 |
// The text can be broken into many shaping sequences |
| 631 |
// (by place holders, possibly, by hard line breaks or tabs, too) |
| 632 |
auto limitlessWidth = std::numeric_limits<SkScalar>::max(); |
| 633 |
|
| 634 |
auto result = iterateThroughShapingRegions( |
| 635 |
[this, limitlessWidth] |
| 636 |
(TextRange textRange, SkSpan<Block> styleSpan, SkScalar& advanceX, TextIndex textStart, uint8_t defaultBidiLevel) { |
| 637 |
|
| 638 |
// Set up the shaper and shape the next |
| 639 |
auto shaper = SkShapers::HB::ShapeDontWrapOrReorder(fParagraph->fUnicode, |
| 640 |
SkFontMgr::RefEmpty()); // no fallback |
| 641 |
if (shaper == nullptr) { |
| 642 |
// For instance, loadICU does not work. We have to stop the process |
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
iterateThroughFontStyles(textRange, styleSpan, |
| 647 |
[this, &shaper, defaultBidiLevel, limitlessWidth, &advanceX] |
| 648 |
(Block block, TArray<SkShaper::Feature> features) { |
| 649 |
auto blockSpan = SkSpan<Block>(&block, 1); |
| 650 |
|
| 651 |
// Start from the beginning (hoping that it's a simple case one block - one run) |
| 652 |
fHeight = block.fStyle.getHeightOverride() ? block.fStyle.getHeight() : 0; |
| 653 |
fUseHalfLeading = block.fStyle.getHalfLeading(); |
| 654 |
fBaselineShift = block.fStyle.getBaselineShift(); |
| 655 |
fAdvance = SkVector::Make(advanceX, 0); |
| 656 |
fCurrentText = block.fRange; |
| 657 |
fUnresolvedBlocks.emplace_back(RunBlock(block.fRange)); |
| 658 |
|
| 659 |
this->matchResolvedFonts(block.fStyle, [&](sk_sp<SkTypeface> typeface) { |
| 660 |
|
| 661 |
// Create one more font to try |
| 662 |
SkFont font(std::move(typeface), block.fStyle.getFontSize()); |
| 663 |
font.setEdging(block.fStyle.getFontEdging()); |
| 664 |
font.setHinting(block.fStyle.getFontHinting()); |
| 665 |
font.setSubpixel(block.fStyle.getSubpixel()); |
| 666 |
|
| 667 |
if (fParagraph->paragraphStyle().fakeMissingFontStyles()) { |
| 668 |
// Apply fake bold and/or italic settings to the font if the |
| 669 |
// typeface's attributes do not match the intended font style. |
| 670 |
int wantedWeight = block.fStyle.getFontStyle().weight(); |
| 671 |
bool fakeBold = |
| 672 |
wantedWeight >= SkFontStyle::kSemiBold_Weight && |
| 673 |
wantedWeight - font.getTypeface()->fontStyle().weight() >= 200; |
| 674 |
bool fakeItalic = |
| 675 |
block.fStyle.getFontStyle().slant() == SkFontStyle::kItalic_Slant && |
| 676 |
font.getTypeface()->fontStyle().slant() != SkFontStyle::kItalic_Slant; |
| 677 |
font.setEmbolden(fakeBold); |
| 678 |
font.setSkewX(fakeItalic ? -SK_Scalar1 / 4 : 0); |
| 679 |
} |
| 680 |
|
| 681 |
// Walk through all the currently unresolved blocks |
| 682 |
// (ignoring those that appear later) |
| 683 |
auto resolvedCount = fResolvedBlocks.size(); |
| 684 |
auto unresolvedCount = fUnresolvedBlocks.size(); |
| 685 |
while (unresolvedCount-- > 0) { |
| 686 |
auto unresolvedRange = fUnresolvedBlocks.front().fText; |
| 687 |
if (unresolvedRange == EMPTY_TEXT) { |
| 688 |
// Duplicate blocks should be ignored |
| 689 |
fUnresolvedBlocks.pop_front(); |
| 690 |
continue; |
| 691 |
} |
| 692 |
auto unresolvedText = fParagraph->text(unresolvedRange); |
| 693 |
|
| 694 |
SkShaper::TrivialFontRunIterator fontIter(font, unresolvedText.size()); |
| 695 |
LangIterator langIter(unresolvedText, blockSpan, |
| 696 |
fParagraph->paragraphStyle().getTextStyle()); |
| 697 |
SkShaper::TrivialBiDiRunIterator bidiIter(defaultBidiLevel, unresolvedText.size()); |
| 698 |
auto scriptIter = SkShapers::HB::ScriptRunIterator(unresolvedText.data(), |
| 699 |
unresolvedText.size()); |
| 700 |
fCurrentText = unresolvedRange; |
| 701 |
|
| 702 |
// Map the block's features to subranges within the unresolved range. |
| 703 |
TArray<SkShaper::Feature> adjustedFeatures(features.size()); |
| 704 |
for (const SkShaper::Feature& feature : features) { |
| 705 |
SkRange<size_t> featureRange(feature.start, feature.end); |
| 706 |
if (unresolvedRange.intersects(featureRange)) { |
| 707 |
SkRange<size_t> adjustedRange = unresolvedRange.intersection(featureRange); |
| 708 |
adjustedRange.Shift(-static_cast<std::make_signed_t<size_t>>(unresolvedRange.start)); |
| 709 |
adjustedFeatures.push_back({feature.tag, feature.value, adjustedRange.start, adjustedRange.end}); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
shaper->shape(unresolvedText.data(), unresolvedText.size(), |
| 714 |
fontIter, bidiIter,*scriptIter, langIter, |
| 715 |
adjustedFeatures.data(), adjustedFeatures.size(), |
| 716 |
limitlessWidth, this); |
| 717 |
|
| 718 |
// Take off the queue the block we tried to resolved - |
| 719 |
// whatever happened, we have now smaller pieces of it to deal with |
| 720 |
fUnresolvedBlocks.pop_front(); |
| 721 |
} |
| 722 |
|
| 723 |
if (fUnresolvedBlocks.empty()) { |
| 724 |
// In some cases it does not mean everything |
| 725 |
// (when we excluded some hopeless blocks from the list) |
| 726 |
return Resolved::Everything; |
| 727 |
} else if (resolvedCount < fResolvedBlocks.size()) { |
| 728 |
return Resolved::Something; |
| 729 |
} else { |
| 730 |
return Resolved::Nothing; |
| 731 |
} |
| 732 |
}); |
| 733 |
|
| 734 |
this->finish(block, fHeight, advanceX); |
| 735 |
}); |
| 736 |
|
| 737 |
return true; |
| 738 |
}); |
| 739 |
|
| 740 |
return result; |
| 741 |
} |
| 742 |
|
| 743 |
// When we extend TextRange to the grapheme edges, we also extend glyphs range |
| 744 |
TextRange OneLineShaper::clusteredText(GlyphRange& glyphs) { |
| 745 |
|
| 746 |
enum class Dir { left, right }; |
| 747 |
enum class Pos { inclusive, exclusive }; |
| 748 |
|
| 749 |
// [left: right) |
| 750 |
auto findBaseChar = [&](TextIndex index, Dir dir) -> TextIndex { |
| 751 |
|
| 752 |
if (dir == Dir::right) { |
| 753 |
while (index < fCurrentRun->fTextRange.end) { |
| 754 |
if (this->fParagraph->codeUnitHasProperty(index, |
| 755 |
SkUnicode::CodeUnitFlags::kGraphemeStart)) { |
| 756 |
return index; |
| 757 |
} |
| 758 |
++index; |
| 759 |
} |
| 760 |
return fCurrentRun->fTextRange.end; |
| 761 |
} else { |
| 762 |
while (index > fCurrentRun->fTextRange.start) { |
| 763 |
if (this->fParagraph->codeUnitHasProperty(index, |
| 764 |
SkUnicode::CodeUnitFlags::kGraphemeStart)) { |
| 765 |
return index; |
| 766 |
} |
| 767 |
--index; |
| 768 |
} |
| 769 |
return fCurrentRun->fTextRange.start; |
| 770 |
} |
| 771 |
}; |
| 772 |
|
| 773 |
TextRange textRange(normalizeTextRange(glyphs)); |
| 774 |
textRange.start = findBaseChar(textRange.start, Dir::left); |
| 775 |
textRange.end = findBaseChar(textRange.end, Dir::right); |
| 776 |
|
| 777 |
// Correct the glyphRange in case we extended the text to the grapheme edges |
| 778 |
// TODO: code it without if (as a part of LTR/RTL refactoring) |
| 779 |
if (fCurrentRun->leftToRight()) { |
| 780 |
while (glyphs.start > 0 && clusterIndex(glyphs.start) > textRange.start) { |
| 781 |
glyphs.start--; |
| 782 |
} |
| 783 |
while (glyphs.end < fCurrentRun->size() && clusterIndex(glyphs.end) < textRange.end) { |
| 784 |
glyphs.end++; |
| 785 |
} |
| 786 |
} else { |
| 787 |
while (glyphs.start > 0 && clusterIndex(glyphs.start - 1) < textRange.end) { |
| 788 |
glyphs.start--; |
| 789 |
} |
| 790 |
while (glyphs.end < fCurrentRun->size() && clusterIndex(glyphs.end) > textRange.start) { |
| 791 |
glyphs.end++; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
return { textRange.start, textRange.end }; |
| 796 |
} |
| 797 |
|
| 798 |
bool OneLineShaper::FontKey::operator==(const OneLineShaper::FontKey& other) const { |
| 799 |
return fUnicode == other.fUnicode && fFontStyle == other.fFontStyle && fLocale == other.fLocale && fFontArgs == other.fFontArgs; |
| 800 |
} |
| 801 |
|
| 802 |
uint32_t OneLineShaper::FontKey::Hasher::operator()(const OneLineShaper::FontKey& key) const { |
| 803 |
return SkGoodHash()(key.fUnicode) ^ |
| 804 |
SkGoodHash()(key.fFontStyle) ^ |
| 805 |
SkGoodHash()(key.fLocale) ^ |
| 806 |
std::hash<std::optional<FontArguments>>()(key.fFontArgs); |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
// By definition any emoji_sequence starts from a codepoint that has |
| 811 |
// UCHAR_EMOJI property. |
| 812 |
// If the first codepoint does not have UCHAR_EMOJI_COMPONENT property, |
| 813 |
// we have an emoji sequence right away. |
| 814 |
// In two (and only two) cases an emoji sequence starts with a codepoint |
| 815 |
// that also has UCHAR_EMOJI_COMPONENT property. |
| 816 |
// emoji_flag_sequence := regional_indicator regional_indicator |
| 817 |
// emoji_keycap_sequence := [0-9#*] \x{FE0F 20E3} |
| 818 |
// These two cases require additional checks of the next codepoint(s). |
| 819 |
SkUnichar OneLineShaper::getEmojiSequenceStart(SkUnicode* unicode, const char** begin, const char* end) { |
| 820 |
const char* next = *begin; |
| 821 |
auto codepoint1 = SkUTF::NextUTF8WithReplacement(&next, end); |
| 822 |
|
| 823 |
if (!unicode->isEmoji(codepoint1)) { |
| 824 |
// This is not a basic emoji nor it an emoji sequence |
| 825 |
return -1; |
| 826 |
} |
| 827 |
|
| 828 |
if (!unicode->isEmojiComponent(codepoint1)) { |
| 829 |
// This is an emoji sequence start |
| 830 |
*begin = next; |
| 831 |
return codepoint1; |
| 832 |
} |
| 833 |
|
| 834 |
// Now we need to look at the next codepoint to see what is going on |
| 835 |
const char* last = next; |
| 836 |
auto codepoint2 = SkUTF::NextUTF8WithReplacement(&last, end); |
| 837 |
|
| 838 |
// emoji_flag_sequence |
| 839 |
if (unicode->isRegionalIndicator(codepoint2)) { |
| 840 |
// We expect a second regional indicator here |
| 841 |
if (unicode->isRegionalIndicator(codepoint2)) { |
| 842 |
*begin = next; |
| 843 |
return codepoint1; |
| 844 |
} else { |
| 845 |
// That really should not happen assuming correct UTF8 text |
| 846 |
return -1; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
// emoji_keycap_sequence |
| 851 |
if (codepoint2 == 0xFE0F) { |
| 852 |
auto codepoint3 = SkUTF::NextUTF8WithReplacement(&last, end); |
| 853 |
if (codepoint3 == 0x20E3) { |
| 854 |
*begin = next; |
| 855 |
return codepoint1; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
return -1; |
| 860 |
} |
| 861 |
|
| 862 |
} // namespace textlayout |
| 863 |
} // namespace skia |