| 1 |
// Copyright 2019 Google LLC. |
| 2 |
|
| 3 |
#include "modules/skparagraph/src/TextLine.h" |
| 4 |
|
| 5 |
#include "include/core/SkBlurTypes.h" |
| 6 |
#include "include/core/SkFont.h" |
| 7 |
#include "include/core/SkFontMetrics.h" |
| 8 |
#include "include/core/SkMaskFilter.h" |
| 9 |
#include "include/core/SkPaint.h" |
| 10 |
#include "include/core/SkSpan.h" |
| 11 |
#include "include/core/SkString.h" |
| 12 |
#include "include/core/SkTextBlob.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkTemplates.h" |
| 15 |
#include "include/private/base/SkTo.h" |
| 16 |
#include "modules/skparagraph/include/DartTypes.h" |
| 17 |
#include "modules/skparagraph/include/Metrics.h" |
| 18 |
#include "modules/skparagraph/include/ParagraphPainter.h" |
| 19 |
#include "modules/skparagraph/include/ParagraphStyle.h" |
| 20 |
#include "modules/skparagraph/include/TextShadow.h" |
| 21 |
#include "modules/skparagraph/include/TextStyle.h" |
| 22 |
#include "modules/skparagraph/src/Decorations.h" |
| 23 |
#include "modules/skparagraph/src/ParagraphImpl.h" |
| 24 |
#include "modules/skparagraph/src/ParagraphPainterImpl.h" |
| 25 |
#include "modules/skshaper/include/SkShaper.h" |
| 26 |
#include "modules/skshaper/include/SkShaper_harfbuzz.h" |
| 27 |
#include "modules/skshaper/include/SkShaper_skunicode.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <iterator> |
| 31 |
#include <limits> |
| 32 |
#include <map> |
| 33 |
#include <memory> |
| 34 |
#include <tuple> |
| 35 |
#include <type_traits> |
| 36 |
#include <utility> |
| 37 |
|
| 38 |
using namespace skia_private; |
| 39 |
|
| 40 |
namespace skia { |
| 41 |
namespace textlayout { |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
// TODO: deal with all the intersection functionality |
| 46 |
TextRange intersected(const TextRange& a, const TextRange& b) { |
| 47 |
if (a.start == b.start && a.end == b.end) return a; |
| 48 |
auto begin = std::max(a.start, b.start); |
| 49 |
auto end = std::min(a.end, b.end); |
| 50 |
return end >= begin ? TextRange(begin, end) : EMPTY_TEXT; |
| 51 |
} |
| 52 |
|
| 53 |
SkScalar littleRound(SkScalar a) { |
| 54 |
// This rounding is done to match Flutter tests. Must be removed.. |
| 55 |
return SkScalarRoundToScalar(a * 100.0)/100.0; |
| 56 |
} |
| 57 |
|
| 58 |
TextRange operator*(const TextRange& a, const TextRange& b) { |
| 59 |
if (a.start == b.start && a.end == b.end) return a; |
| 60 |
auto begin = std::max(a.start, b.start); |
| 61 |
auto end = std::min(a.end, b.end); |
| 62 |
return end > begin ? TextRange(begin, end) : EMPTY_TEXT; |
| 63 |
} |
| 64 |
|
| 65 |
int compareRound(SkScalar a, SkScalar b, bool applyRoundingHack) { |
| 66 |
// There is a rounding error that gets bigger when maxWidth gets bigger |
| 67 |
// VERY long zalgo text (> 100000) on a VERY long line (> 10000) |
| 68 |
// Canvas scaling affects it |
| 69 |
// Letter spacing affects it |
| 70 |
// It has to be relative to be useful |
| 71 |
auto base = std::max(SkScalarAbs(a), SkScalarAbs(b)); |
| 72 |
auto diff = SkScalarAbs(a - b); |
| 73 |
if (nearlyZero(base) || diff / base < 0.001f) { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
auto ra = a; |
| 78 |
auto rb = b; |
| 79 |
|
| 80 |
if (applyRoundingHack) { |
| 81 |
ra = littleRound(a); |
| 82 |
rb = littleRound(b); |
| 83 |
} |
| 84 |
if (ra < rb) { |
| 85 |
return -1; |
| 86 |
} else { |
| 87 |
return 1; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
} // namespace |
| 92 |
|
| 93 |
TextLine::TextLine(ParagraphImpl* owner, |
| 94 |
SkVector offset, |
| 95 |
SkVector advance, |
| 96 |
BlockRange blocks, |
| 97 |
TextRange textExcludingSpaces, |
| 98 |
TextRange text, |
| 99 |
TextRange textIncludingNewlines, |
| 100 |
ClusterRange clusters, |
| 101 |
ClusterRange clustersWithGhosts, |
| 102 |
SkScalar widthWithSpaces, |
| 103 |
InternalLineMetrics sizes) |
| 104 |
: fOwner(owner) |
| 105 |
, fBlockRange(blocks) |
| 106 |
, fTextExcludingSpaces(textExcludingSpaces) |
| 107 |
, fText(text) |
| 108 |
, fTextIncludingNewlines(textIncludingNewlines) |
| 109 |
, fClusterRange(clusters) |
| 110 |
, fGhostClusterRange(clustersWithGhosts) |
| 111 |
, fRunsInVisualOrder() |
| 112 |
, fAdvance(advance) |
| 113 |
, fOffset(offset) |
| 114 |
, fShift(0.0) |
| 115 |
, fWidthWithSpaces(widthWithSpaces) |
| 116 |
, fEllipsis(nullptr) |
| 117 |
, fSizes(sizes) |
| 118 |
, fHasBackground(false) |
| 119 |
, fHasShadows(false) |
| 120 |
, fHasDecorations(false) |
| 121 |
, fAscentStyle(LineMetricStyle::CSS) |
| 122 |
, fDescentStyle(LineMetricStyle::CSS) |
| 123 |
, fTextBlobCachePopulated(false) { |
| 124 |
// Reorder visual runs |
| 125 |
auto& start = owner->cluster(fGhostClusterRange.start); |
| 126 |
auto& end = owner->cluster(fGhostClusterRange.end - 1); |
| 127 |
size_t numRuns = end.runIndex() - start.runIndex() + 1; |
| 128 |
|
| 129 |
for (BlockIndex index = fBlockRange.start; index < fBlockRange.end; ++index) { |
| 130 |
auto b = fOwner->styles().begin() + index; |
| 131 |
if (b->fStyle.hasBackground()) { |
| 132 |
fHasBackground = true; |
| 133 |
} |
| 134 |
if (b->fStyle.getDecorationType() != TextDecoration::kNoDecoration) { |
| 135 |
fHasDecorations = true; |
| 136 |
} |
| 137 |
if (b->fStyle.getShadowNumber() > 0) { |
| 138 |
fHasShadows = true; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Get the logical order |
| 143 |
|
| 144 |
// This is just chosen to catch the common/fast cases. Feel free to tweak. |
| 145 |
constexpr int kPreallocCount = 4; |
| 146 |
AutoSTArray<kPreallocCount, SkUnicode::BidiLevel> runLevels(numRuns); |
| 147 |
std::vector<RunIndex> placeholdersInOriginalOrder; |
| 148 |
size_t runLevelsIndex = 0; |
| 149 |
// Placeholders must be laid out using the original order in which they were added |
| 150 |
// in the input. The API does not provide a way to indicate that a placeholder |
| 151 |
// position was moved due to bidi reordering. |
| 152 |
for (auto runIndex = start.runIndex(); runIndex <= end.runIndex(); ++runIndex) { |
| 153 |
auto& run = fOwner->run(runIndex); |
| 154 |
runLevels[runLevelsIndex++] = run.fBidiLevel; |
| 155 |
fMaxRunMetrics.add( |
| 156 |
InternalLineMetrics(run.correctAscent(), run.correctDescent(), run.fFontMetrics.fLeading)); |
| 157 |
if (run.isPlaceholder()) { |
| 158 |
placeholdersInOriginalOrder.push_back(runIndex); |
| 159 |
} |
| 160 |
} |
| 161 |
SkASSERT(runLevelsIndex == numRuns); |
| 162 |
|
| 163 |
AutoSTArray<kPreallocCount, int32_t> logicalOrder(numRuns); |
| 164 |
|
| 165 |
// TODO: hide all these logic in SkUnicode? |
| 166 |
fOwner->getUnicode()->reorderVisual(runLevels.data(), numRuns, logicalOrder.data()); |
| 167 |
auto firstRunIndex = start.runIndex(); |
| 168 |
auto placeholderIter = placeholdersInOriginalOrder.begin(); |
| 169 |
for (auto index : logicalOrder) { |
| 170 |
auto runIndex = firstRunIndex + index; |
| 171 |
if (fOwner->run(runIndex).isPlaceholder()) { |
| 172 |
fRunsInVisualOrder.push_back(*placeholderIter++); |
| 173 |
} else { |
| 174 |
fRunsInVisualOrder.push_back(runIndex); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// TODO: This is the fix for flutter. Must be removed... |
| 179 |
for (auto cluster = &start; cluster <= &end; ++cluster) { |
| 180 |
if (!cluster->run().isPlaceholder() && !cluster->run().isCursiveScript()) { |
| 181 |
fShift += cluster->getHalfLetterSpacing(); |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void TextLine::paint(ParagraphPainter* painter, SkScalar x, SkScalar y) { |
| 188 |
if (fHasBackground) { |
| 189 |
this->iterateThroughVisualRuns(false, |
| 190 |
[painter, x, y, this] |
| 191 |
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) { |
| 192 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 193 |
TextAdjustment::GlyphCluster, run, runOffsetInLine, textRange, StyleType::kBackground, |
| 194 |
[painter, x, y, this](TextRange textRange, const TextStyle& style, const ClipContext& context) { |
| 195 |
this->paintBackground(painter, x, y, textRange, style, context); |
| 196 |
}); |
| 197 |
return true; |
| 198 |
}); |
| 199 |
} |
| 200 |
|
| 201 |
if (fHasShadows) { |
| 202 |
this->iterateThroughVisualRuns(false, |
| 203 |
[painter, x, y, this] |
| 204 |
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) { |
| 205 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 206 |
TextAdjustment::GlyphCluster, run, runOffsetInLine, textRange, StyleType::kShadow, |
| 207 |
[painter, x, y, this] |
| 208 |
(TextRange textRange, const TextStyle& style, const ClipContext& context) { |
| 209 |
this->paintShadow(painter, x, y, textRange, style, context); |
| 210 |
}); |
| 211 |
return true; |
| 212 |
}); |
| 213 |
} |
| 214 |
|
| 215 |
this->ensureTextBlobCachePopulated(); |
| 216 |
|
| 217 |
for (auto& record : fTextBlobCache) { |
| 218 |
record.paint(painter, x, y); |
| 219 |
} |
| 220 |
|
| 221 |
if (fHasDecorations) { |
| 222 |
this->iterateThroughVisualRuns(false, |
| 223 |
[painter, x, y, this] |
| 224 |
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) { |
| 225 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 226 |
TextAdjustment::GlyphCluster, run, runOffsetInLine, textRange, StyleType::kDecorations, |
| 227 |
[painter, x, y, this] |
| 228 |
(TextRange textRange, const TextStyle& style, const ClipContext& context) { |
| 229 |
this->paintDecorations(painter, x, y, textRange, style, context); |
| 230 |
}); |
| 231 |
return true; |
| 232 |
}); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void TextLine::ensureTextBlobCachePopulated() { |
| 237 |
if (fTextBlobCachePopulated) { |
| 238 |
return; |
| 239 |
} |
| 240 |
if (fBlockRange.width() == 1 && |
| 241 |
fRunsInVisualOrder.size() == 1 && |
| 242 |
fEllipsis == nullptr && |
| 243 |
fOwner->run(fRunsInVisualOrder[0]).placeholderStyle() == nullptr) { |
| 244 |
if (fClusterRange.width() == 0) { |
| 245 |
return; |
| 246 |
} |
| 247 |
// Most common and most simple case |
| 248 |
const auto& style = fOwner->block(fBlockRange.start).fStyle; |
| 249 |
const auto& run = fOwner->run(fRunsInVisualOrder[0]); |
| 250 |
auto clip = SkRect::MakeXYWH(0.0f, this->sizes().runTop(&run, this->fAscentStyle), |
| 251 |
fAdvance.fX, |
| 252 |
run.calculateHeight(this->fAscentStyle, this->fDescentStyle)); |
| 253 |
|
| 254 |
auto& start = fOwner->cluster(fClusterRange.start); |
| 255 |
auto& end = fOwner->cluster(fClusterRange.end - 1); |
| 256 |
SkASSERT(start.runIndex() == end.runIndex()); |
| 257 |
GlyphRange glyphs; |
| 258 |
if (run.leftToRight()) { |
| 259 |
glyphs = GlyphRange(start.startPos(), |
| 260 |
end.isHardBreak() ? end.startPos() : end.endPos()); |
| 261 |
} else { |
| 262 |
glyphs = GlyphRange(end.startPos(), |
| 263 |
start.isHardBreak() ? start.startPos() : start.endPos()); |
| 264 |
} |
| 265 |
ClipContext context = {/*run=*/&run, |
| 266 |
/*pos=*/glyphs.start, |
| 267 |
/*size=*/glyphs.width(), |
| 268 |
/*fTextShift=*/-run.positionX(glyphs.start), // starting position |
| 269 |
/*clip=*/clip, // entire line |
| 270 |
/*fExcludedTrailingSpaces=*/0.0f, // no need for that |
| 271 |
/*clippingNeeded=*/false}; // no need for that |
| 272 |
this->buildTextBlob(fTextExcludingSpaces, style, context); |
| 273 |
} else { |
| 274 |
this->iterateThroughVisualRuns(false, |
| 275 |
[this](const Run* run, |
| 276 |
SkScalar runOffsetInLine, |
| 277 |
TextRange textRange, |
| 278 |
SkScalar* runWidthInLine) { |
| 279 |
if (run->placeholderStyle() != nullptr) { |
| 280 |
*runWidthInLine = run->advance().fX; |
| 281 |
return true; |
| 282 |
} |
| 283 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 284 |
TextAdjustment::GlyphCluster, |
| 285 |
run, |
| 286 |
runOffsetInLine, |
| 287 |
textRange, |
| 288 |
StyleType::kForeground, |
| 289 |
[this](TextRange textRange, const TextStyle& style, const ClipContext& context) { |
| 290 |
this->buildTextBlob(textRange, style, context); |
| 291 |
}); |
| 292 |
return true; |
| 293 |
}); |
| 294 |
} |
| 295 |
fTextBlobCachePopulated = true; |
| 296 |
} |
| 297 |
|
| 298 |
void TextLine::format(TextAlign align, SkScalar maxWidth) { |
| 299 |
SkScalar delta = maxWidth - this->width(); |
| 300 |
if (delta <= 0) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
// We do nothing for left align |
| 305 |
if (align == TextAlign::kJustify) { |
| 306 |
if (!this->endsWithHardLineBreak()) { |
| 307 |
this->justify(maxWidth); |
| 308 |
} else if (fOwner->paragraphStyle().getTextDirection() == TextDirection::kRtl) { |
| 309 |
// Justify -> Right align |
| 310 |
fShift = delta; |
| 311 |
} |
| 312 |
} else if (align == TextAlign::kRight) { |
| 313 |
fShift = delta; |
| 314 |
} else if (align == TextAlign::kCenter) { |
| 315 |
fShift = delta / 2; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
void TextLine::scanStyles(StyleType styleType, const RunStyleVisitor& visitor) { |
| 320 |
if (this->empty()) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
this->iterateThroughVisualRuns( |
| 325 |
false, |
| 326 |
[this, visitor, styleType]( |
| 327 |
const Run* run, SkScalar runOffset, TextRange textRange, SkScalar* width) { |
| 328 |
*width = this->iterateThroughSingleRunByStyles( |
| 329 |
TextAdjustment::GlyphCluster, |
| 330 |
run, |
| 331 |
runOffset, |
| 332 |
textRange, |
| 333 |
styleType, |
| 334 |
[visitor](TextRange textRange, |
| 335 |
const TextStyle& style, |
| 336 |
const ClipContext& context) { |
| 337 |
visitor(textRange, style, context); |
| 338 |
}); |
| 339 |
return true; |
| 340 |
}); |
| 341 |
} |
| 342 |
|
| 343 |
SkRect TextLine::extendHeight(const ClipContext& context) const { |
| 344 |
SkRect result = context.clip; |
| 345 |
result.fBottom += std::max(this->fMaxRunMetrics.height() - this->height(), 0.0f); |
| 346 |
return result; |
| 347 |
} |
| 348 |
|
| 349 |
void TextLine::buildTextBlob(TextRange textRange, const TextStyle& style, const ClipContext& context) { |
| 350 |
if (context.run->placeholderStyle() != nullptr) { |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
fTextBlobCache.emplace_back(); |
| 355 |
TextBlobRecord& record = fTextBlobCache.back(); |
| 356 |
|
| 357 |
if (style.hasForeground()) { |
| 358 |
record.fPaint = style.getForegroundPaintOrID(); |
| 359 |
} else { |
| 360 |
std::get<SkPaint>(record.fPaint).setColor(style.getColor()); |
| 361 |
} |
| 362 |
record.fVisitor_Run = context.run; |
| 363 |
record.fVisitor_Pos = context.pos; |
| 364 |
|
| 365 |
// TODO: This is the change for flutter, must be removed later |
| 366 |
SkTextBlobBuilder builder; |
| 367 |
context.run->copyTo(builder, SkToU32(context.pos), context.size); |
| 368 |
record.fClippingNeeded = context.clippingNeeded; |
| 369 |
if (context.clippingNeeded) { |
| 370 |
record.fClipRect = extendHeight(context).makeOffset(this->offset()); |
| 371 |
} else { |
| 372 |
record.fClipRect = context.clip.makeOffset(this->offset()); |
| 373 |
} |
| 374 |
|
| 375 |
SkASSERT(nearlyEqual(context.run->baselineShift(), style.getBaselineShift())); |
| 376 |
SkScalar correctedBaseline = SkScalarFloorToScalar(this->baseline() + style.getBaselineShift() + 0.5); |
| 377 |
record.fBlob = builder.make(); |
| 378 |
if (record.fBlob != nullptr) { |
| 379 |
record.fBounds.joinPossiblyEmptyRect(record.fBlob->bounds()); |
| 380 |
} |
| 381 |
|
| 382 |
record.fOffset = SkPoint::Make(this->offset().fX + context.fTextShift, |
| 383 |
this->offset().fY + correctedBaseline); |
| 384 |
} |
| 385 |
|
| 386 |
void TextLine::TextBlobRecord::paint(ParagraphPainter* painter, SkScalar x, SkScalar y) { |
| 387 |
if (fClippingNeeded) { |
| 388 |
painter->save(); |
| 389 |
painter->clipRect(fClipRect.makeOffset(x, y)); |
| 390 |
} |
| 391 |
painter->drawTextBlob(fBlob, x + fOffset.x(), y + fOffset.y(), fPaint); |
| 392 |
if (fClippingNeeded) { |
| 393 |
painter->restore(); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
void TextLine::paintBackground(ParagraphPainter* painter, |
| 398 |
SkScalar x, |
| 399 |
SkScalar y, |
| 400 |
TextRange textRange, |
| 401 |
const TextStyle& style, |
| 402 |
const ClipContext& context) const { |
| 403 |
if (style.hasBackground()) { |
| 404 |
painter->drawRect(context.clip.makeOffset(this->offset() + SkPoint::Make(x, y)), |
| 405 |
style.getBackgroundPaintOrID()); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
void TextLine::paintShadow(ParagraphPainter* painter, |
| 410 |
SkScalar x, |
| 411 |
SkScalar y, |
| 412 |
TextRange textRange, |
| 413 |
const TextStyle& style, |
| 414 |
const ClipContext& context) const { |
| 415 |
SkScalar correctedBaseline = SkScalarFloorToScalar(this->baseline() + style.getBaselineShift() + 0.5); |
| 416 |
|
| 417 |
for (TextShadow shadow : style.getShadows()) { |
| 418 |
if (!shadow.hasShadow()) continue; |
| 419 |
|
| 420 |
SkTextBlobBuilder builder; |
| 421 |
context.run->copyTo(builder, context.pos, context.size); |
| 422 |
|
| 423 |
if (context.clippingNeeded) { |
| 424 |
painter->save(); |
| 425 |
SkRect clip = extendHeight(context); |
| 426 |
clip.offset(x, y); |
| 427 |
clip.offset(this->offset()); |
| 428 |
painter->clipRect(clip); |
| 429 |
} |
| 430 |
auto blob = builder.make(); |
| 431 |
painter->drawTextShadow(blob, |
| 432 |
x + this->offset().fX + shadow.fOffset.x() + context.fTextShift, |
| 433 |
y + this->offset().fY + shadow.fOffset.y() + correctedBaseline, |
| 434 |
shadow.fColor, |
| 435 |
SkDoubleToScalar(shadow.fBlurSigma)); |
| 436 |
if (context.clippingNeeded) { |
| 437 |
painter->restore(); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
void TextLine::paintDecorations(ParagraphPainter* painter, SkScalar x, SkScalar y, TextRange textRange, const TextStyle& style, const ClipContext& context) const { |
| 443 |
ParagraphPainterAutoRestore ppar(painter); |
| 444 |
painter->translate(x + this->offset().fX, y + this->offset().fY + style.getBaselineShift()); |
| 445 |
Decorations decorations; |
| 446 |
SkScalar correctedBaseline = SkScalarFloorToScalar(-this->sizes().rawAscent() + style.getBaselineShift() + 0.5); |
| 447 |
decorations.paint(painter, style, context, correctedBaseline); |
| 448 |
} |
| 449 |
|
| 450 |
void TextLine::justify(SkScalar maxWidth) { |
| 451 |
int whitespacePatches = 0; |
| 452 |
SkScalar textLen = 0; |
| 453 |
SkScalar whitespaceLen = 0; |
| 454 |
bool whitespacePatch = false; |
| 455 |
// Take leading whitespaces width but do not increment a whitespace patch number |
| 456 |
bool leadingWhitespaces = false; |
| 457 |
this->iterateThroughClustersInGlyphsOrder(false, false, |
| 458 |
[&](const Cluster* cluster, ClusterIndex index, bool ghost) { |
| 459 |
if (cluster->isWhitespaceBreak()) { |
| 460 |
if (index == 0) { |
| 461 |
leadingWhitespaces = true; |
| 462 |
} else if (!whitespacePatch && !leadingWhitespaces) { |
| 463 |
// We only count patches BETWEEN words, not before |
| 464 |
++whitespacePatches; |
| 465 |
} |
| 466 |
whitespacePatch = !leadingWhitespaces; |
| 467 |
whitespaceLen += cluster->width(); |
| 468 |
} else if (cluster->isIdeographic()) { |
| 469 |
// Whitespace break before and after |
| 470 |
if (!whitespacePatch && index != 0) { |
| 471 |
// We only count patches BETWEEN words, not before |
| 472 |
++whitespacePatches; // before |
| 473 |
} |
| 474 |
whitespacePatch = true; |
| 475 |
leadingWhitespaces = false; |
| 476 |
++whitespacePatches; // after |
| 477 |
} else { |
| 478 |
whitespacePatch = false; |
| 479 |
leadingWhitespaces = false; |
| 480 |
} |
| 481 |
textLen += cluster->width(); |
| 482 |
return true; |
| 483 |
}); |
| 484 |
|
| 485 |
if (whitespacePatch) { |
| 486 |
// We only count patches BETWEEN words, not after |
| 487 |
--whitespacePatches; |
| 488 |
} |
| 489 |
if (whitespacePatches == 0) { |
| 490 |
if (fOwner->paragraphStyle().getTextDirection() == TextDirection::kRtl) { |
| 491 |
// Justify -> Right align |
| 492 |
fShift = maxWidth - textLen; |
| 493 |
} |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
SkScalar step = (maxWidth - textLen + whitespaceLen) / whitespacePatches; |
| 498 |
SkScalar shift = 0.0f; |
| 499 |
SkScalar prevShift = 0.0f; |
| 500 |
|
| 501 |
// Deal with the ghost spaces |
| 502 |
auto ghostShift = maxWidth - this->fAdvance.fX; |
| 503 |
// Spread the extra whitespaces |
| 504 |
whitespacePatch = false; |
| 505 |
// Do not break on leading whitespaces |
| 506 |
leadingWhitespaces = false; |
| 507 |
this->iterateThroughClustersInGlyphsOrder(false, true, [&](const Cluster* cluster, ClusterIndex index, bool ghost) { |
| 508 |
|
| 509 |
if (ghost) { |
| 510 |
if (cluster->run().leftToRight()) { |
| 511 |
this->shiftCluster(cluster, ghostShift, ghostShift); |
| 512 |
} |
| 513 |
return true; |
| 514 |
} |
| 515 |
|
| 516 |
if (cluster->isWhitespaceBreak()) { |
| 517 |
if (index == 0) { |
| 518 |
leadingWhitespaces = true; |
| 519 |
} else if (!whitespacePatch && !leadingWhitespaces) { |
| 520 |
shift += step; |
| 521 |
whitespacePatch = true; |
| 522 |
--whitespacePatches; |
| 523 |
} |
| 524 |
shift -= cluster->width(); |
| 525 |
} else if (cluster->isIdeographic()) { |
| 526 |
if (!whitespacePatch && index != 0) { |
| 527 |
shift += step; |
| 528 |
--whitespacePatches; |
| 529 |
} |
| 530 |
whitespacePatch = false; |
| 531 |
leadingWhitespaces = false; |
| 532 |
} else { |
| 533 |
whitespacePatch = false; |
| 534 |
leadingWhitespaces = false; |
| 535 |
} |
| 536 |
this->shiftCluster(cluster, shift, prevShift); |
| 537 |
prevShift = shift; |
| 538 |
// We skip ideographic whitespaces |
| 539 |
if (!cluster->isWhitespaceBreak() && cluster->isIdeographic()) { |
| 540 |
shift += step; |
| 541 |
whitespacePatch = true; |
| 542 |
--whitespacePatches; |
| 543 |
} |
| 544 |
return true; |
| 545 |
}); |
| 546 |
|
| 547 |
if (whitespacePatch && whitespacePatches < 0) { |
| 548 |
whitespacePatches++; |
| 549 |
shift -= step; |
| 550 |
} |
| 551 |
|
| 552 |
SkAssertResult(nearlyEqual(shift, maxWidth - textLen)); |
| 553 |
SkASSERT(whitespacePatches == 0); |
| 554 |
|
| 555 |
this->fWidthWithSpaces += ghostShift; |
| 556 |
this->fAdvance.fX = maxWidth; |
| 557 |
} |
| 558 |
|
| 559 |
void TextLine::shiftCluster(const Cluster* cluster, SkScalar shift, SkScalar prevShift) { |
| 560 |
|
| 561 |
auto& run = cluster->run(); |
| 562 |
auto start = cluster->startPos(); |
| 563 |
auto end = cluster->endPos(); |
| 564 |
|
| 565 |
if (end == run.size()) { |
| 566 |
// Set the same shift for the fake last glyph (to avoid all extra checks) |
| 567 |
++end; |
| 568 |
} |
| 569 |
|
| 570 |
if (run.fJustificationShifts.empty()) { |
| 571 |
// Do not fill this array until needed |
| 572 |
run.fJustificationShifts.push_back_n(run.size() + 1, { 0, 0 }); |
| 573 |
} |
| 574 |
|
| 575 |
for (size_t pos = start; pos < end; ++pos) { |
| 576 |
run.fJustificationShifts[pos] = { shift, prevShift }; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
void TextLine::createEllipsis(SkScalar maxWidth, const SkString& ellipsis, bool) { |
| 581 |
// Replace some clusters with the ellipsis |
| 582 |
// Go through the clusters in the reverse logical order |
| 583 |
// taking off cluster by cluster until the ellipsis fits |
| 584 |
SkScalar width = fAdvance.fX; |
| 585 |
RunIndex lastRun = EMPTY_RUN; |
| 586 |
std::unique_ptr<Run> ellipsisRun; |
| 587 |
for (auto clusterIndex = fGhostClusterRange.end; clusterIndex > fGhostClusterRange.start; --clusterIndex) { |
| 588 |
auto& cluster = fOwner->cluster(clusterIndex - 1); |
| 589 |
// Shape the ellipsis if the run has changed |
| 590 |
if (lastRun != cluster.runIndex()) { |
| 591 |
ellipsisRun = this->shapeEllipsis(ellipsis, &cluster); |
| 592 |
if (ellipsisRun->advance().fX > maxWidth) { |
| 593 |
// Ellipsis is bigger than the entire line; no way we can add it at all |
| 594 |
// BUT! We can keep scanning in case the next run will give us better results |
| 595 |
lastRun = EMPTY_RUN; |
| 596 |
continue; |
| 597 |
} else { |
| 598 |
// We may need to continue |
| 599 |
lastRun = cluster.runIndex(); |
| 600 |
} |
| 601 |
} |
| 602 |
// See if it fits |
| 603 |
if (width + ellipsisRun->advance().fX > maxWidth) { |
| 604 |
width -= cluster.width(); |
| 605 |
// Continue if the ellipsis does not fit |
| 606 |
continue; |
| 607 |
} |
| 608 |
// We found enough room for the ellipsis |
| 609 |
fAdvance.fX = width; |
| 610 |
fEllipsis = std::move(ellipsisRun); |
| 611 |
fEllipsis->setOwner(fOwner); |
| 612 |
|
| 613 |
// Let's update the line |
| 614 |
fClusterRange.end = clusterIndex; |
| 615 |
fGhostClusterRange.end = fClusterRange.end; |
| 616 |
fEllipsis->fClusterStart = cluster.textRange().start; |
| 617 |
fText.end = cluster.textRange().end; |
| 618 |
fTextIncludingNewlines.end = cluster.textRange().end; |
| 619 |
fTextExcludingSpaces.end = cluster.textRange().end; |
| 620 |
break; |
| 621 |
} |
| 622 |
|
| 623 |
if (!fEllipsis) { |
| 624 |
// Weird situation: ellipsis does not fit; no ellipsis then |
| 625 |
fClusterRange.end = fClusterRange.start; |
| 626 |
fGhostClusterRange.end = fClusterRange.start; |
| 627 |
fText.end = fText.start; |
| 628 |
fTextIncludingNewlines.end = fTextIncludingNewlines.start; |
| 629 |
fTextExcludingSpaces.end = fTextExcludingSpaces.start; |
| 630 |
fAdvance.fX = 0; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
std::unique_ptr<Run> TextLine::shapeEllipsis(const SkString& ellipsis, const Cluster* cluster) { |
| 635 |
|
| 636 |
class ShapeHandler final : public SkShaper::RunHandler { |
| 637 |
public: |
| 638 |
ShapeHandler(SkScalar lineHeight, bool useHalfLeading, SkScalar baselineShift, const SkString& ellipsis) |
| 639 |
: fRun(nullptr), fLineHeight(lineHeight), fUseHalfLeading(useHalfLeading), fBaselineShift(baselineShift), fEllipsis(ellipsis) {} |
| 640 |
std::unique_ptr<Run> run() & { return std::move(fRun); } |
| 641 |
|
| 642 |
private: |
| 643 |
void beginLine() override {} |
| 644 |
|
| 645 |
void runInfo(const RunInfo&) override {} |
| 646 |
|
| 647 |
void commitRunInfo() override {} |
| 648 |
|
| 649 |
Buffer runBuffer(const RunInfo& info) override { |
| 650 |
SkASSERT(!fRun); |
| 651 |
fRun = std::make_unique<Run>(nullptr, info, 0, fLineHeight, fUseHalfLeading, fBaselineShift, 0, 0); |
| 652 |
return fRun->newRunBuffer(); |
| 653 |
} |
| 654 |
|
| 655 |
void commitRunBuffer(const RunInfo& info) override { |
| 656 |
fRun->fAdvance.fX = info.fAdvance.fX; |
| 657 |
fRun->fAdvance.fY = fRun->advance().fY; |
| 658 |
fRun->fPlaceholderIndex = std::numeric_limits<size_t>::max(); |
| 659 |
fRun->fEllipsis = true; |
| 660 |
} |
| 661 |
|
| 662 |
void commitLine() override {} |
| 663 |
|
| 664 |
std::unique_ptr<Run> fRun; |
| 665 |
SkScalar fLineHeight; |
| 666 |
bool fUseHalfLeading; |
| 667 |
SkScalar fBaselineShift; |
| 668 |
SkString fEllipsis; |
| 669 |
}; |
| 670 |
|
| 671 |
const Run& run = cluster->run(); |
| 672 |
TextStyle textStyle = fOwner->paragraphStyle().getTextStyle(); |
| 673 |
for (auto i = fBlockRange.start; i < fBlockRange.end; ++i) { |
| 674 |
auto& block = fOwner->block(i); |
| 675 |
if (run.leftToRight() && cluster->textRange().end <= block.fRange.end) { |
| 676 |
textStyle = block.fStyle; |
| 677 |
break; |
| 678 |
} else if (!run.leftToRight() && cluster->textRange().start <= block.fRange.end) { |
| 679 |
textStyle = block.fStyle; |
| 680 |
break; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
auto shaped = [&](sk_sp<SkTypeface> typeface, sk_sp<SkFontMgr> fallback) -> std::unique_ptr<Run> { |
| 685 |
ShapeHandler handler(run.heightMultiplier(), run.useHalfLeading(), run.baselineShift(), ellipsis); |
| 686 |
SkFont font(std::move(typeface), textStyle.getFontSize()); |
| 687 |
font.setEdging(textStyle.getFontEdging()); |
| 688 |
font.setHinting(textStyle.getFontHinting()); |
| 689 |
font.setSubpixel(textStyle.getSubpixel()); |
| 690 |
|
| 691 |
std::unique_ptr<SkShaper> shaper = SkShapers::HB::ShapeDontWrapOrReorder( |
| 692 |
fOwner->getUnicode(), fallback ? fallback : SkFontMgr::RefEmpty()); |
| 693 |
|
| 694 |
const SkBidiIterator::Level defaultLevel = SkBidiIterator::kLTR; |
| 695 |
const char* utf8 = ellipsis.c_str(); |
| 696 |
size_t utf8Bytes = ellipsis.size(); |
| 697 |
|
| 698 |
std::unique_ptr<SkShaper::BiDiRunIterator> bidi = SkShapers::unicode::BidiRunIterator( |
| 699 |
fOwner->getUnicode(), utf8, utf8Bytes, defaultLevel); |
| 700 |
SkASSERT(bidi); |
| 701 |
|
| 702 |
std::unique_ptr<SkShaper::LanguageRunIterator> language = |
| 703 |
SkShaper::MakeStdLanguageRunIterator(utf8, utf8Bytes); |
| 704 |
SkASSERT(language); |
| 705 |
|
| 706 |
std::unique_ptr<SkShaper::ScriptRunIterator> script = |
| 707 |
SkShapers::HB::ScriptRunIterator(utf8, utf8Bytes); |
| 708 |
SkASSERT(script); |
| 709 |
|
| 710 |
std::unique_ptr<SkShaper::FontRunIterator> fontRuns = SkShaper::MakeFontMgrRunIterator( |
| 711 |
utf8, utf8Bytes, font, fallback ? fallback : SkFontMgr::RefEmpty()); |
| 712 |
SkASSERT(fontRuns); |
| 713 |
|
| 714 |
shaper->shape(utf8, |
| 715 |
utf8Bytes, |
| 716 |
*fontRuns, |
| 717 |
*bidi, |
| 718 |
*script, |
| 719 |
*language, |
| 720 |
nullptr, |
| 721 |
0, |
| 722 |
std::numeric_limits<SkScalar>::max(), |
| 723 |
&handler); |
| 724 |
auto ellipsisRun = handler.run(); |
| 725 |
ellipsisRun->fTextRange = TextRange(0, ellipsis.size()); |
| 726 |
ellipsisRun->fOwner = fOwner; |
| 727 |
return ellipsisRun; |
| 728 |
}; |
| 729 |
|
| 730 |
// Check the current font |
| 731 |
auto ellipsisRun = shaped(run.fFont.refTypeface(), nullptr); |
| 732 |
if (ellipsisRun->isResolved()) { |
| 733 |
return ellipsisRun; |
| 734 |
} |
| 735 |
|
| 736 |
// Check all allowed fonts |
| 737 |
std::vector<sk_sp<SkTypeface>> typefaces = fOwner->fontCollection()->findTypefaces( |
| 738 |
textStyle.getFontFamilies(), textStyle.getFontStyle(), textStyle.getFontArguments()); |
| 739 |
for (const auto& typeface : typefaces) { |
| 740 |
ellipsisRun = shaped(typeface, nullptr); |
| 741 |
if (ellipsisRun->isResolved()) { |
| 742 |
return ellipsisRun; |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
// Try the fallback |
| 747 |
if (fOwner->fontCollection()->fontFallbackEnabled()) { |
| 748 |
const char* ch = ellipsis.c_str(); |
| 749 |
SkUnichar unicode = SkUTF::NextUTF8WithReplacement(&ch, |
| 750 |
ellipsis.c_str() |
| 751 |
+ ellipsis.size()); |
| 752 |
// We do not expect emojis in ellipsis so if they appeat there |
| 753 |
// they will not be resolved with the pretiest color emoji font |
| 754 |
auto typeface = fOwner->fontCollection()->defaultFallback( |
| 755 |
unicode, |
| 756 |
textStyle.getFontFamilies(), |
| 757 |
textStyle.getFontStyle(), |
| 758 |
textStyle.getLocale(), |
| 759 |
textStyle.getFontArguments()); |
| 760 |
if (typeface) { |
| 761 |
ellipsisRun = shaped(typeface, fOwner->fontCollection()->getFallbackManager()); |
| 762 |
if (ellipsisRun->isResolved()) { |
| 763 |
return ellipsisRun; |
| 764 |
} |
| 765 |
} |
| 766 |
} |
| 767 |
return ellipsisRun; |
| 768 |
} |
| 769 |
|
| 770 |
TextLine::ClipContext TextLine::measureTextInsideOneRun(TextRange textRange, |
| 771 |
const Run* run, |
| 772 |
SkScalar runOffsetInLine, |
| 773 |
SkScalar textOffsetInRunInLine, |
| 774 |
bool includeGhostSpaces, |
| 775 |
TextAdjustment textAdjustment) const { |
| 776 |
ClipContext result = { run, 0, run->size(), 0, SkRect::MakeEmpty(), 0, false }; |
| 777 |
|
| 778 |
if (run->fEllipsis) { |
| 779 |
// Both ellipsis and placeholders can only be measured as one glyph |
| 780 |
result.fTextShift = runOffsetInLine; |
| 781 |
result.clip = SkRect::MakeXYWH(runOffsetInLine, |
| 782 |
sizes().runTop(run, this->fAscentStyle), |
| 783 |
run->advance().fX, |
| 784 |
run->calculateHeight(this->fAscentStyle,this->fDescentStyle)); |
| 785 |
return result; |
| 786 |
} else if (run->isPlaceholder()) { |
| 787 |
result.fTextShift = runOffsetInLine; |
| 788 |
if (SkIsFinite(run->fFontMetrics.fAscent)) { |
| 789 |
result.clip = SkRect::MakeXYWH(runOffsetInLine, |
| 790 |
sizes().runTop(run, this->fAscentStyle), |
| 791 |
run->advance().fX, |
| 792 |
run->calculateHeight(this->fAscentStyle,this->fDescentStyle)); |
| 793 |
} else { |
| 794 |
result.clip = SkRect::MakeXYWH(runOffsetInLine, run->fFontMetrics.fAscent, run->advance().fX, 0); |
| 795 |
} |
| 796 |
return result; |
| 797 |
} else if (textRange.empty()) { |
| 798 |
return result; |
| 799 |
} |
| 800 |
|
| 801 |
TextRange originalTextRange(textRange); // We need it for proportional measurement |
| 802 |
// Find [start:end] clusters for the text |
| 803 |
while (true) { |
| 804 |
// Update textRange by cluster edges (shift start up to the edge of the cluster) |
| 805 |
// TODO: remove this limitation? |
| 806 |
TextRange updatedTextRange; |
| 807 |
bool found; |
| 808 |
std::tie(found, updatedTextRange.start, updatedTextRange.end) = |
| 809 |
run->findLimitingGlyphClusters(textRange); |
| 810 |
if (!found) { |
| 811 |
return result; |
| 812 |
} |
| 813 |
|
| 814 |
if ((textAdjustment & TextAdjustment::Grapheme) == 0) { |
| 815 |
textRange = updatedTextRange; |
| 816 |
break; |
| 817 |
} |
| 818 |
|
| 819 |
// Update text range by grapheme edges (shift start up to the edge of the grapheme) |
| 820 |
std::tie(found, updatedTextRange.start, updatedTextRange.end) = |
| 821 |
run->findLimitingGraphemes(updatedTextRange); |
| 822 |
if (updatedTextRange == textRange) { |
| 823 |
break; |
| 824 |
} |
| 825 |
|
| 826 |
// Some clusters are inside graphemes and we need to adjust them |
| 827 |
//SkDebugf("Correct range: [%d:%d) -> [%d:%d)\n", textRange.start, textRange.end, startIndex, endIndex); |
| 828 |
textRange = updatedTextRange; |
| 829 |
|
| 830 |
// Move the start until it's on the grapheme edge (and glypheme, too) |
| 831 |
} |
| 832 |
Cluster* start = &fOwner->cluster(fOwner->clusterIndex(textRange.start)); |
| 833 |
Cluster* end = &fOwner->cluster(fOwner->clusterIndex(textRange.end - (textRange.width() == 0 ? 0 : 1))); |
| 834 |
|
| 835 |
if (!run->leftToRight()) { |
| 836 |
std::swap(start, end); |
| 837 |
} |
| 838 |
result.pos = start->startPos(); |
| 839 |
result.size = (end->isHardBreak() ? end->startPos() : end->endPos()) - start->startPos(); |
| 840 |
auto textStartInRun = run->positionX(start->startPos()); |
| 841 |
auto textStartInLine = runOffsetInLine + textOffsetInRunInLine; |
| 842 |
if (!run->leftToRight()) { |
| 843 |
std::swap(start, end); |
| 844 |
} |
| 845 |
/* |
| 846 |
if (!run->fJustificationShifts.empty()) { |
| 847 |
SkDebugf("Justification for [%d:%d)\n", textRange.start, textRange.end); |
| 848 |
for (auto i = result.pos; i < result.pos + result.size; ++i) { |
| 849 |
auto j = run->fJustificationShifts[i]; |
| 850 |
SkDebugf("[%d] = %f %f\n", i, j.fX, j.fY); |
| 851 |
} |
| 852 |
} |
| 853 |
*/ |
| 854 |
// Calculate the clipping rectangle for the text with cluster edges |
| 855 |
// There are 2 cases: |
| 856 |
// EOL (when we expect the last cluster clipped without any spaces) |
| 857 |
// Anything else (when we want the cluster width contain all the spaces - |
| 858 |
// coming from letter spacing or word spacing or justification) |
| 859 |
result.clip = |
| 860 |
SkRect::MakeXYWH(0, |
| 861 |
sizes().runTop(run, this->fAscentStyle), |
| 862 |
run->calculateWidth(result.pos, result.pos + result.size, false), |
| 863 |
run->calculateHeight(this->fAscentStyle,this->fDescentStyle)); |
| 864 |
|
| 865 |
// Correct the width in case the text edges don't match clusters |
| 866 |
// TODO: This is where we get smart about selecting a part of a cluster |
| 867 |
// by shaping each grapheme separately and then use the result sizes |
| 868 |
// to calculate the proportions |
| 869 |
auto leftCorrection = start->sizeToChar(originalTextRange.start); |
| 870 |
auto rightCorrection = end->sizeFromChar(originalTextRange.end - 1); |
| 871 |
/* |
| 872 |
SkDebugf("[%d: %d) => [%d: %d), @%d, %d: [%f:%f) + [%f:%f) = ", // جَآَهُ |
| 873 |
originalTextRange.start, originalTextRange.end, textRange.start, textRange.end, |
| 874 |
result.pos, result.size, |
| 875 |
result.clip.fLeft, result.clip.fRight, leftCorrection, rightCorrection); |
| 876 |
*/ |
| 877 |
result.clippingNeeded = leftCorrection != 0 || rightCorrection != 0; |
| 878 |
if (run->leftToRight()) { |
| 879 |
result.clip.fLeft += leftCorrection; |
| 880 |
result.clip.fRight -= rightCorrection; |
| 881 |
textStartInLine -= leftCorrection; |
| 882 |
} else { |
| 883 |
result.clip.fRight -= leftCorrection; |
| 884 |
result.clip.fLeft += rightCorrection; |
| 885 |
textStartInLine -= rightCorrection; |
| 886 |
} |
| 887 |
|
| 888 |
result.clip.offset(textStartInLine, 0); |
| 889 |
//SkDebugf("@%f[%f:%f)\n", textStartInLine, result.clip.fLeft, result.clip.fRight); |
| 890 |
|
| 891 |
if (compareRound(result.clip.fRight, fAdvance.fX, fOwner->getApplyRoundingHack()) > 0 && !includeGhostSpaces) { |
| 892 |
// There are few cases when we need it. |
| 893 |
// The most important one: we measure the text with spaces at the end (or at the beginning in RTL) |
| 894 |
// and we should ignore these spaces |
| 895 |
if (fOwner->paragraphStyle().getTextDirection() == TextDirection::kLtr) { |
| 896 |
// We only use this member for LTR |
| 897 |
result.fExcludedTrailingSpaces = std::max(result.clip.fRight - fAdvance.fX, 0.0f); |
| 898 |
result.clippingNeeded = true; |
| 899 |
result.clip.fRight = fAdvance.fX; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
if (result.clip.width() < 0) { |
| 904 |
// Weird situation when glyph offsets move the glyph to the left |
| 905 |
// (happens with zalgo texts, for instance) |
| 906 |
result.clip.fRight = result.clip.fLeft; |
| 907 |
} |
| 908 |
|
| 909 |
// The text must be aligned with the lineOffset |
| 910 |
result.fTextShift = textStartInLine - textStartInRun; |
| 911 |
|
| 912 |
return result; |
| 913 |
} |
| 914 |
|
| 915 |
void TextLine::iterateThroughClustersInGlyphsOrder(bool reversed, |
| 916 |
bool includeGhosts, |
| 917 |
const ClustersVisitor& visitor) const { |
| 918 |
// Walk through the clusters in the logical order (or reverse) |
| 919 |
SkSpan<const size_t> runs(fRunsInVisualOrder.data(), fRunsInVisualOrder.size()); |
| 920 |
bool ignore = false; |
| 921 |
ClusterIndex index = 0; |
| 922 |
directional_for_each(runs, !reversed, [&](decltype(runs[0]) r) { |
| 923 |
if (ignore) return; |
| 924 |
auto run = this->fOwner->run(r); |
| 925 |
auto trimmedRange = fClusterRange.intersection(run.clusterRange()); |
| 926 |
auto trailedRange = fGhostClusterRange.intersection(run.clusterRange()); |
| 927 |
SkASSERT(trimmedRange.start == trailedRange.start); |
| 928 |
|
| 929 |
auto trailed = fOwner->clusters(trailedRange); |
| 930 |
auto trimmed = fOwner->clusters(trimmedRange); |
| 931 |
directional_for_each(trailed, reversed != run.leftToRight(), [&](Cluster& cluster) { |
| 932 |
if (ignore) return; |
| 933 |
bool ghost = &cluster >= trimmed.data() + trimmed.size(); |
| 934 |
if (!includeGhosts && ghost) { |
| 935 |
return; |
| 936 |
} |
| 937 |
if (!visitor(&cluster, index++, ghost)) { |
| 938 |
|
| 939 |
ignore = true; |
| 940 |
return; |
| 941 |
} |
| 942 |
}); |
| 943 |
}); |
| 944 |
} |
| 945 |
|
| 946 |
SkScalar TextLine::iterateThroughSingleRunByStyles(TextAdjustment textAdjustment, |
| 947 |
const Run* run, |
| 948 |
SkScalar runOffset, |
| 949 |
TextRange textRange, |
| 950 |
StyleType styleType, |
| 951 |
const RunStyleVisitor& visitor) const { |
| 952 |
auto correctContext = [&](TextRange textRange, SkScalar textOffsetInRun) -> ClipContext { |
| 953 |
auto result = this->measureTextInsideOneRun( |
| 954 |
textRange, run, runOffset, textOffsetInRun, false, textAdjustment); |
| 955 |
if (styleType == StyleType::kDecorations) { |
| 956 |
// Decorations are drawn based on the real font metrics (regardless of styles and strut) |
| 957 |
result.clip.fTop = this->sizes().runTop(run, LineMetricStyle::CSS); |
| 958 |
result.clip.fBottom = result.clip.fTop + |
| 959 |
run->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS); |
| 960 |
} |
| 961 |
return result; |
| 962 |
}; |
| 963 |
|
| 964 |
if (run->fEllipsis) { |
| 965 |
// Extra efforts to get the ellipsis text style |
| 966 |
ClipContext clipContext = correctContext(run->textRange(), 0.0f); |
| 967 |
TextRange testRange(run->fClusterStart, run->fClusterStart + run->textRange().width()); |
| 968 |
for (BlockIndex index = fBlockRange.start; index < fBlockRange.end; ++index) { |
| 969 |
auto block = fOwner->styles().begin() + index; |
| 970 |
auto intersect = intersected(block->fRange, testRange); |
| 971 |
if (intersect.width() > 0) { |
| 972 |
visitor(testRange, block->fStyle, clipContext); |
| 973 |
return run->advance().fX; |
| 974 |
} |
| 975 |
} |
| 976 |
SkASSERT(false); |
| 977 |
} |
| 978 |
|
| 979 |
if (styleType == StyleType::kNone) { |
| 980 |
ClipContext clipContext = correctContext(textRange, 0.0f); |
| 981 |
// The placehoder can have height=0 or (exclusively) width=0 and still be a thing |
| 982 |
if (clipContext.clip.height() > 0.0f || clipContext.clip.width() > 0.0f) { |
| 983 |
visitor(textRange, TextStyle(), clipContext); |
| 984 |
return clipContext.clip.width(); |
| 985 |
} else { |
| 986 |
return 0; |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
TextIndex start = EMPTY_INDEX; |
| 991 |
size_t size = 0; |
| 992 |
const TextStyle* prevStyle = nullptr; |
| 993 |
SkScalar textOffsetInRun = 0; |
| 994 |
|
| 995 |
const BlockIndex blockRangeSize = fBlockRange.end - fBlockRange.start; |
| 996 |
for (BlockIndex index = 0; index <= blockRangeSize; ++index) { |
| 997 |
|
| 998 |
TextRange intersect; |
| 999 |
TextStyle* style = nullptr; |
| 1000 |
if (index < blockRangeSize) { |
| 1001 |
auto block = fOwner->styles().begin() + |
| 1002 |
(run->leftToRight() ? fBlockRange.start + index : fBlockRange.end - index - 1); |
| 1003 |
|
| 1004 |
// Get the text |
| 1005 |
intersect = intersected(block->fRange, textRange); |
| 1006 |
if (intersect.width() == 0) { |
| 1007 |
if (start == EMPTY_INDEX) { |
| 1008 |
// This style is not applicable to the text yet |
| 1009 |
continue; |
| 1010 |
} else { |
| 1011 |
// We have found all the good styles already |
| 1012 |
// but we need to process the last one of them |
| 1013 |
intersect = TextRange(start, start + size); |
| 1014 |
index = fBlockRange.end; |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
// Get the style |
| 1018 |
style = &block->fStyle; |
| 1019 |
if (start != EMPTY_INDEX && style->matchOneAttribute(styleType, *prevStyle)) { |
| 1020 |
size += intersect.width(); |
| 1021 |
// RTL text intervals move backward |
| 1022 |
start = std::min(intersect.start, start); |
| 1023 |
continue; |
| 1024 |
} else if (start == EMPTY_INDEX ) { |
| 1025 |
// First time only |
| 1026 |
prevStyle = style; |
| 1027 |
size = intersect.width(); |
| 1028 |
start = intersect.start; |
| 1029 |
continue; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} else if (prevStyle != nullptr) { |
| 1033 |
// This is the last style |
| 1034 |
} else { |
| 1035 |
break; |
| 1036 |
} |
| 1037 |
|
| 1038 |
// We have the style and the text |
| 1039 |
auto runStyleTextRange = TextRange(start, start + size); |
| 1040 |
ClipContext clipContext = correctContext(runStyleTextRange, textOffsetInRun); |
| 1041 |
textOffsetInRun += clipContext.clip.width(); |
| 1042 |
if (clipContext.clip.height() == 0) { |
| 1043 |
continue; |
| 1044 |
} |
| 1045 |
visitor(runStyleTextRange, *prevStyle, clipContext); |
| 1046 |
|
| 1047 |
// Start all over again |
| 1048 |
prevStyle = style; |
| 1049 |
start = intersect.start; |
| 1050 |
size = intersect.width(); |
| 1051 |
} |
| 1052 |
return textOffsetInRun; |
| 1053 |
} |
| 1054 |
|
| 1055 |
void TextLine::iterateThroughVisualRuns(bool includingGhostSpaces, const RunVisitor& visitor) const { |
| 1056 |
|
| 1057 |
// Walk through all the runs that intersect with the line in visual order |
| 1058 |
SkScalar width = 0; |
| 1059 |
SkScalar runOffset = 0; |
| 1060 |
SkScalar totalWidth = 0; |
| 1061 |
auto textRange = includingGhostSpaces ? this->textWithNewlines() : this->trimmedText(); |
| 1062 |
|
| 1063 |
if (this->ellipsis() != nullptr && fOwner->paragraphStyle().getTextDirection() == TextDirection::kRtl) { |
| 1064 |
runOffset = this->ellipsis()->offset().fX; |
| 1065 |
if (visitor(ellipsis(), runOffset, ellipsis()->textRange(), &width)) { |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
for (auto& runIndex : fRunsInVisualOrder) { |
| 1070 |
|
| 1071 |
const auto run = &this->fOwner->run(runIndex); |
| 1072 |
auto lineIntersection = intersected(run->textRange(), textRange); |
| 1073 |
if (lineIntersection.width() == 0 && this->width() != 0) { |
| 1074 |
// TODO: deal with empty runs in a better way |
| 1075 |
continue; |
| 1076 |
} |
| 1077 |
if (!run->leftToRight() && runOffset == 0 && includingGhostSpaces) { |
| 1078 |
// runOffset does not take in account a possibility |
| 1079 |
// that RTL run could start before the line (trailing spaces) |
| 1080 |
// so we need to do runOffset -= "trailing whitespaces length" |
| 1081 |
TextRange whitespaces = intersected( |
| 1082 |
TextRange(fTextExcludingSpaces.end, fTextIncludingNewlines.end), run->fTextRange); |
| 1083 |
if (whitespaces.width() > 0) { |
| 1084 |
auto whitespacesLen = measureTextInsideOneRun(whitespaces, run, runOffset, 0, true, TextAdjustment::GlyphCluster).clip.width(); |
| 1085 |
runOffset -= whitespacesLen; |
| 1086 |
} |
| 1087 |
} |
| 1088 |
runOffset += width; |
| 1089 |
totalWidth += width; |
| 1090 |
if (!visitor(run, runOffset, lineIntersection, &width)) { |
| 1091 |
return; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
|
| 1095 |
runOffset += width; |
| 1096 |
totalWidth += width; |
| 1097 |
|
| 1098 |
if (this->ellipsis() != nullptr && fOwner->paragraphStyle().getTextDirection() == TextDirection::kLtr) { |
| 1099 |
if (visitor(ellipsis(), runOffset, ellipsis()->textRange(), &width)) { |
| 1100 |
totalWidth += width; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
if (!includingGhostSpaces && compareRound(totalWidth, this->width(), fOwner->getApplyRoundingHack()) != 0) { |
| 1105 |
// This is a very important assert! |
| 1106 |
// It asserts that 2 different ways of calculation come with the same results |
| 1107 |
SkDEBUGFAILF("ASSERT: %f != %f\n", totalWidth, this->width()); |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
SkVector TextLine::offset() const { |
| 1112 |
return fOffset + SkVector::Make(fShift, 0); |
| 1113 |
} |
| 1114 |
|
| 1115 |
LineMetrics TextLine::getMetrics() const { |
| 1116 |
LineMetrics result; |
| 1117 |
SkASSERT(fOwner); |
| 1118 |
|
| 1119 |
// Fill out the metrics |
| 1120 |
fOwner->ensureUTF16Mapping(); |
| 1121 |
result.fStartIndex = fOwner->getUTF16Index(fTextExcludingSpaces.start); |
| 1122 |
result.fEndExcludingWhitespaces = fOwner->getUTF16Index(fTextExcludingSpaces.end); |
| 1123 |
result.fEndIndex = fOwner->getUTF16Index(fText.end); |
| 1124 |
result.fEndIncludingNewline = fOwner->getUTF16Index(fTextIncludingNewlines.end); |
| 1125 |
result.fHardBreak = endsWithHardLineBreak(); |
| 1126 |
result.fAscent = - fMaxRunMetrics.ascent(); |
| 1127 |
result.fDescent = fMaxRunMetrics.descent(); |
| 1128 |
result.fUnscaledAscent = - fMaxRunMetrics.ascent(); // TODO: implement |
| 1129 |
result.fHeight = fAdvance.fY; |
| 1130 |
result.fWidth = fAdvance.fX; |
| 1131 |
if (fOwner->getApplyRoundingHack()) { |
| 1132 |
result.fHeight = littleRound(result.fHeight); |
| 1133 |
result.fWidth = littleRound(result.fWidth); |
| 1134 |
} |
| 1135 |
result.fLeft = this->offset().fX; |
| 1136 |
// This is Flutter definition of a baseline |
| 1137 |
result.fBaseline = this->offset().fY + this->height() - this->sizes().descent(); |
| 1138 |
result.fLineNumber = this - fOwner->lines().data(); |
| 1139 |
|
| 1140 |
// Fill out the style parts |
| 1141 |
this->iterateThroughVisualRuns(false, |
| 1142 |
[this, &result] |
| 1143 |
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) { |
| 1144 |
if (run->placeholderStyle() != nullptr) { |
| 1145 |
*runWidthInLine = run->advance().fX; |
| 1146 |
return true; |
| 1147 |
} |
| 1148 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 1149 |
TextAdjustment::GlyphCluster, run, runOffsetInLine, textRange, StyleType::kForeground, |
| 1150 |
[&result, &run](TextRange textRange, const TextStyle& style, const ClipContext& context) { |
| 1151 |
SkFontMetrics fontMetrics; |
| 1152 |
run->fFont.getMetrics(&fontMetrics); |
| 1153 |
StyleMetrics styleMetrics(&style, fontMetrics); |
| 1154 |
result.fLineMetrics.emplace(textRange.start, styleMetrics); |
| 1155 |
}); |
| 1156 |
return true; |
| 1157 |
}); |
| 1158 |
|
| 1159 |
return result; |
| 1160 |
} |
| 1161 |
|
| 1162 |
bool TextLine::isFirstLine() const { |
| 1163 |
return this == &fOwner->lines().front(); |
| 1164 |
} |
| 1165 |
|
| 1166 |
bool TextLine::isLastLine() const { |
| 1167 |
return this == &fOwner->lines().back(); |
| 1168 |
} |
| 1169 |
|
| 1170 |
bool TextLine::endsWithHardLineBreak() const { |
| 1171 |
// TODO: For some reason Flutter imagines a hard line break at the end of the last line. |
| 1172 |
// To be removed... |
| 1173 |
return (fGhostClusterRange.width() > 0 && fOwner->cluster(fGhostClusterRange.end - 1).isHardBreak()) || |
| 1174 |
fEllipsis != nullptr || |
| 1175 |
fGhostClusterRange.end == fOwner->clusters().size() - 1; |
| 1176 |
} |
| 1177 |
|
| 1178 |
void TextLine::getRectsForRange(TextRange textRange0, |
| 1179 |
RectHeightStyle rectHeightStyle, |
| 1180 |
RectWidthStyle rectWidthStyle, |
| 1181 |
std::vector<TextBox>& boxes) const |
| 1182 |
{ |
| 1183 |
const Run* lastRun = nullptr; |
| 1184 |
auto startBox = boxes.size(); |
| 1185 |
this->iterateThroughVisualRuns(true, |
| 1186 |
[textRange0, rectHeightStyle, rectWidthStyle, &boxes, &lastRun, startBox, this] |
| 1187 |
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) { |
| 1188 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 1189 |
TextAdjustment::GraphemeGluster, run, runOffsetInLine, textRange, StyleType::kNone, |
| 1190 |
[run, runOffsetInLine, textRange0, rectHeightStyle, rectWidthStyle, &boxes, &lastRun, startBox, this] |
| 1191 |
(TextRange textRange, const TextStyle& style, const TextLine::ClipContext& lineContext) { |
| 1192 |
|
| 1193 |
auto intersect = textRange * textRange0; |
| 1194 |
if (intersect.empty()) { |
| 1195 |
return true; |
| 1196 |
} |
| 1197 |
|
| 1198 |
auto paragraphStyle = fOwner->paragraphStyle(); |
| 1199 |
|
| 1200 |
// Found a run that intersects with the text |
| 1201 |
auto context = this->measureTextInsideOneRun( |
| 1202 |
intersect, run, runOffsetInLine, 0, true, TextAdjustment::GraphemeGluster); |
| 1203 |
SkRect clip = context.clip; |
| 1204 |
clip.offset(lineContext.fTextShift - context.fTextShift, 0); |
| 1205 |
|
| 1206 |
switch (rectHeightStyle) { |
| 1207 |
case RectHeightStyle::kMax: |
| 1208 |
// TODO: Change it once flutter rolls into google3 |
| 1209 |
// (probably will break things if changed before) |
| 1210 |
clip.fBottom = this->height(); |
| 1211 |
clip.fTop = this->sizes().delta(); |
| 1212 |
break; |
| 1213 |
case RectHeightStyle::kIncludeLineSpacingTop: { |
| 1214 |
clip.fBottom = this->height(); |
| 1215 |
clip.fTop = this->sizes().delta(); |
| 1216 |
auto verticalShift = this->sizes().rawAscent() - this->sizes().ascent(); |
| 1217 |
if (isFirstLine()) { |
| 1218 |
clip.fTop += verticalShift; |
| 1219 |
} |
| 1220 |
break; |
| 1221 |
} |
| 1222 |
case RectHeightStyle::kIncludeLineSpacingMiddle: { |
| 1223 |
clip.fBottom = this->height(); |
| 1224 |
clip.fTop = this->sizes().delta(); |
| 1225 |
auto verticalShift = this->sizes().rawAscent() - this->sizes().ascent(); |
| 1226 |
clip.offset(0, verticalShift / 2.0); |
| 1227 |
if (isFirstLine()) { |
| 1228 |
clip.fTop += verticalShift / 2.0; |
| 1229 |
} |
| 1230 |
if (isLastLine()) { |
| 1231 |
clip.fBottom -= verticalShift / 2.0; |
| 1232 |
} |
| 1233 |
break; |
| 1234 |
} |
| 1235 |
case RectHeightStyle::kIncludeLineSpacingBottom: { |
| 1236 |
clip.fBottom = this->height(); |
| 1237 |
clip.fTop = this->sizes().delta(); |
| 1238 |
auto verticalShift = this->sizes().rawAscent() - this->sizes().ascent(); |
| 1239 |
clip.offset(0, verticalShift); |
| 1240 |
if (isLastLine()) { |
| 1241 |
clip.fBottom -= verticalShift; |
| 1242 |
} |
| 1243 |
break; |
| 1244 |
} |
| 1245 |
case RectHeightStyle::kStrut: { |
| 1246 |
const auto& strutStyle = paragraphStyle.getStrutStyle(); |
| 1247 |
if (strutStyle.getStrutEnabled() |
| 1248 |
&& strutStyle.getFontSize() > 0) { |
| 1249 |
auto strutMetrics = fOwner->strutMetrics(); |
| 1250 |
auto top = this->baseline(); |
| 1251 |
clip.fTop = top + strutMetrics.ascent(); |
| 1252 |
clip.fBottom = top + strutMetrics.descent(); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
break; |
| 1256 |
case RectHeightStyle::kTight: { |
| 1257 |
if (run->fHeightMultiplier <= 0) { |
| 1258 |
break; |
| 1259 |
} |
| 1260 |
const auto effectiveBaseline = this->baseline() + this->sizes().delta(); |
| 1261 |
clip.fTop = effectiveBaseline + run->ascent(); |
| 1262 |
clip.fBottom = effectiveBaseline + run->descent(); |
| 1263 |
} |
| 1264 |
break; |
| 1265 |
default: |
| 1266 |
SkASSERT(false); |
| 1267 |
break; |
| 1268 |
} |
| 1269 |
|
| 1270 |
// Separate trailing spaces and move them in the default order of the paragraph |
| 1271 |
// in case the run order and the paragraph order don't match |
| 1272 |
SkRect trailingSpaces = SkRect::MakeEmpty(); |
| 1273 |
if (this->trimmedText().end <this->textWithNewlines().end && // Line has trailing space |
| 1274 |
this->textWithNewlines().end == intersect.end && // Range is at the end of the line |
| 1275 |
this->trimmedText().end > intersect.start) // Range has more than just spaces |
| 1276 |
{ |
| 1277 |
auto delta = this->spacesWidth(); |
| 1278 |
trailingSpaces = SkRect::MakeXYWH(0, 0, 0, 0); |
| 1279 |
// There are trailing spaces in this run |
| 1280 |
if (paragraphStyle.getTextAlign() == TextAlign::kJustify && isLastLine()) |
| 1281 |
{ |
| 1282 |
// TODO: this is just a patch. Make it right later (when it's clear what and how) |
| 1283 |
trailingSpaces = clip; |
| 1284 |
if(run->leftToRight()) { |
| 1285 |
trailingSpaces.fLeft = this->width(); |
| 1286 |
clip.fRight = this->width(); |
| 1287 |
} else { |
| 1288 |
trailingSpaces.fRight = 0; |
| 1289 |
clip.fLeft = 0; |
| 1290 |
} |
| 1291 |
} else if (paragraphStyle.getTextDirection() == TextDirection::kRtl && |
| 1292 |
!run->leftToRight()) |
| 1293 |
{ |
| 1294 |
// Split |
| 1295 |
trailingSpaces = clip; |
| 1296 |
trailingSpaces.fLeft = - delta; |
| 1297 |
trailingSpaces.fRight = 0; |
| 1298 |
clip.fLeft += delta; |
| 1299 |
} else if (paragraphStyle.getTextDirection() == TextDirection::kLtr && |
| 1300 |
run->leftToRight()) |
| 1301 |
{ |
| 1302 |
// Split |
| 1303 |
trailingSpaces = clip; |
| 1304 |
trailingSpaces.fLeft = this->width(); |
| 1305 |
trailingSpaces.fRight = trailingSpaces.fLeft + delta; |
| 1306 |
clip.fRight -= delta; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
|
| 1310 |
clip.offset(this->offset()); |
| 1311 |
if (trailingSpaces.width() > 0) { |
| 1312 |
trailingSpaces.offset(this->offset()); |
| 1313 |
} |
| 1314 |
|
| 1315 |
// Check if we can merge two boxes instead of adding a new one |
| 1316 |
auto merge = [&lastRun, &context, &boxes](SkRect clip) { |
| 1317 |
bool mergedBoxes = false; |
| 1318 |
if (!boxes.empty() && |
| 1319 |
lastRun != nullptr && |
| 1320 |
context.run->leftToRight() == lastRun->leftToRight() && |
| 1321 |
lastRun->placeholderStyle() == nullptr && |
| 1322 |
context.run->placeholderStyle() == nullptr && |
| 1323 |
nearlyEqual(lastRun->heightMultiplier(), |
| 1324 |
context.run->heightMultiplier()) && |
| 1325 |
lastRun->font() == context.run->font()) |
| 1326 |
{ |
| 1327 |
auto& lastBox = boxes.back(); |
| 1328 |
if (nearlyEqual(lastBox.rect.fTop, clip.fTop) && |
| 1329 |
nearlyEqual(lastBox.rect.fBottom, clip.fBottom) && |
| 1330 |
(nearlyEqual(lastBox.rect.fLeft, clip.fRight) || |
| 1331 |
nearlyEqual(lastBox.rect.fRight, clip.fLeft))) |
| 1332 |
{ |
| 1333 |
lastBox.rect.fLeft = std::min(lastBox.rect.fLeft, clip.fLeft); |
| 1334 |
lastBox.rect.fRight = std::max(lastBox.rect.fRight, clip.fRight); |
| 1335 |
mergedBoxes = true; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
lastRun = context.run; |
| 1339 |
return mergedBoxes; |
| 1340 |
}; |
| 1341 |
|
| 1342 |
if (!merge(clip)) { |
| 1343 |
boxes.emplace_back(clip, context.run->getTextDirection()); |
| 1344 |
} |
| 1345 |
if (!nearlyZero(trailingSpaces.width()) && !merge(trailingSpaces)) { |
| 1346 |
boxes.emplace_back(trailingSpaces, paragraphStyle.getTextDirection()); |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (rectWidthStyle == RectWidthStyle::kMax && !isLastLine()) { |
| 1350 |
// Align the very left/right box horizontally |
| 1351 |
auto lineStart = this->offset().fX; |
| 1352 |
auto lineEnd = this->offset().fX + this->width(); |
| 1353 |
auto left = boxes[startBox]; |
| 1354 |
auto right = boxes.back(); |
| 1355 |
if (left.rect.fLeft > lineStart && left.direction == TextDirection::kRtl) { |
| 1356 |
left.rect.fRight = left.rect.fLeft; |
| 1357 |
left.rect.fLeft = 0; |
| 1358 |
boxes.insert(boxes.begin() + startBox + 1, left); |
| 1359 |
} |
| 1360 |
if (right.direction == TextDirection::kLtr && |
| 1361 |
right.rect.fRight >= lineEnd && |
| 1362 |
right.rect.fRight < fOwner->widthWithTrailingSpaces()) { |
| 1363 |
right.rect.fLeft = right.rect.fRight; |
| 1364 |
right.rect.fRight = fOwner->widthWithTrailingSpaces(); |
| 1365 |
boxes.emplace_back(right); |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
return true; |
| 1370 |
}); |
| 1371 |
return true; |
| 1372 |
}); |
| 1373 |
if (fOwner->getApplyRoundingHack()) { |
| 1374 |
for (auto& r : boxes) { |
| 1375 |
r.rect.fLeft = littleRound(r.rect.fLeft); |
| 1376 |
r.rect.fRight = littleRound(r.rect.fRight); |
| 1377 |
r.rect.fTop = littleRound(r.rect.fTop); |
| 1378 |
r.rect.fBottom = littleRound(r.rect.fBottom); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
} |
| 1382 |
|
| 1383 |
PositionWithAffinity TextLine::getGlyphPositionAtCoordinate(SkScalar dx) { |
| 1384 |
|
| 1385 |
if (SkScalarNearlyZero(this->width()) && SkScalarNearlyZero(this->spacesWidth())) { |
| 1386 |
// TODO: this is one of the flutter changes that have to go away eventually |
| 1387 |
// Empty line is a special case in txtlib (but only when there are no spaces, too) |
| 1388 |
auto utf16Index = fOwner->getUTF16Index(this->fTextExcludingSpaces.end); |
| 1389 |
return { SkToS32(utf16Index) , kDownstream }; |
| 1390 |
} |
| 1391 |
|
| 1392 |
PositionWithAffinity result(0, Affinity::kDownstream); |
| 1393 |
this->iterateThroughVisualRuns(true, |
| 1394 |
[this, dx, &result] |
| 1395 |
(const Run* run, SkScalar runOffsetInLine, TextRange textRange, SkScalar* runWidthInLine) { |
| 1396 |
if (run->isEllipsis()) { |
| 1397 |
auto utf16Index = fOwner->getUTF16Index(this->fText.end); |
| 1398 |
result = { SkToS32(utf16Index) , kDownstream }; |
| 1399 |
return false; |
| 1400 |
} |
| 1401 |
|
| 1402 |
bool keepLooking = true; |
| 1403 |
*runWidthInLine = this->iterateThroughSingleRunByStyles( |
| 1404 |
TextAdjustment::GraphemeGluster, run, runOffsetInLine, textRange, StyleType::kNone, |
| 1405 |
[this, run, dx, &result, &keepLooking] |
| 1406 |
(TextRange textRange, const TextStyle& style, const TextLine::ClipContext& context0) { |
| 1407 |
|
| 1408 |
SkScalar offsetX = this->offset().fX; |
| 1409 |
ClipContext context = context0; |
| 1410 |
|
| 1411 |
// Correct the clip size because libtxt counts trailing spaces |
| 1412 |
if (run->leftToRight()) { |
| 1413 |
context.clip.fRight += context.fExcludedTrailingSpaces; // extending clip to the right |
| 1414 |
} else { |
| 1415 |
// Clip starts from 0; we cannot extend it to the left from that |
| 1416 |
} |
| 1417 |
// However, we need to offset the clip |
| 1418 |
context.clip.offset(offsetX, 0.0f); |
| 1419 |
|
| 1420 |
// This patch will help us to avoid a floating point error |
| 1421 |
if (SkScalarNearlyEqual(context.clip.fRight, dx, 0.01f)) { |
| 1422 |
context.clip.fRight = dx; |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (dx <= context.clip.fLeft) { |
| 1426 |
// All the other runs are placed right of this one |
| 1427 |
auto utf16Index = fOwner->getUTF16Index(context.run->globalClusterIndex(context.pos)); |
| 1428 |
if (run->leftToRight()) { |
| 1429 |
result = { SkToS32(utf16Index), kDownstream}; |
| 1430 |
keepLooking = false; |
| 1431 |
} else { |
| 1432 |
result = { SkToS32(utf16Index + 1), kUpstream}; |
| 1433 |
// If we haven't reached the end of the run we need to keep looking |
| 1434 |
keepLooking = context.pos != 0; |
| 1435 |
} |
| 1436 |
// For RTL we go another way |
| 1437 |
return !run->leftToRight(); |
| 1438 |
} |
| 1439 |
|
| 1440 |
if (dx >= context.clip.fRight) { |
| 1441 |
// We have to keep looking ; just in case keep the last one as the closest |
| 1442 |
auto utf16Index = fOwner->getUTF16Index(context.run->globalClusterIndex(context.pos + context.size)); |
| 1443 |
if (run->leftToRight()) { |
| 1444 |
result = {SkToS32(utf16Index), kUpstream}; |
| 1445 |
} else { |
| 1446 |
result = {SkToS32(utf16Index), kDownstream}; |
| 1447 |
} |
| 1448 |
// For RTL we go another way |
| 1449 |
return run->leftToRight(); |
| 1450 |
} |
| 1451 |
|
| 1452 |
// So we found the run that contains our coordinates |
| 1453 |
// Find the glyph position in the run that is the closest left of our point |
| 1454 |
// TODO: binary search |
| 1455 |
size_t found = context.pos; |
| 1456 |
for (size_t index = context.pos; index < context.pos + context.size; ++index) { |
| 1457 |
// TODO: this rounding is done to match Flutter tests. Must be removed.. |
| 1458 |
auto end = context.run->positionX(index) + context.fTextShift + offsetX; |
| 1459 |
if (fOwner->getApplyRoundingHack()) { |
| 1460 |
end = littleRound(end); |
| 1461 |
} |
| 1462 |
if (end > dx) { |
| 1463 |
break; |
| 1464 |
} else if (end == dx && !context.run->leftToRight()) { |
| 1465 |
// When we move RTL variable end points to the beginning of the code point which is included |
| 1466 |
found = index; |
| 1467 |
break; |
| 1468 |
} |
| 1469 |
found = index; |
| 1470 |
} |
| 1471 |
|
| 1472 |
SkScalar glyphemePosLeft = context.run->positionX(found) + context.fTextShift + offsetX; |
| 1473 |
SkScalar glyphemesWidth = context.run->positionX(found + 1) - context.run->positionX(found); |
| 1474 |
|
| 1475 |
// Find the grapheme range that contains the point |
| 1476 |
auto clusterIndex8 = context.run->globalClusterIndex(found); |
| 1477 |
auto clusterEnd8 = context.run->globalClusterIndex(found + 1); |
| 1478 |
auto graphemes = fOwner->countSurroundingGraphemes({clusterIndex8, clusterEnd8}); |
| 1479 |
|
| 1480 |
SkScalar center = glyphemePosLeft + glyphemesWidth / 2; |
| 1481 |
if (graphemes.size() > 1) { |
| 1482 |
// Calculate the position proportionally based on grapheme count |
| 1483 |
SkScalar averageGraphemeWidth = glyphemesWidth / graphemes.size(); |
| 1484 |
SkScalar delta = dx - glyphemePosLeft; |
| 1485 |
int graphemeIndex = SkScalarNearlyZero(averageGraphemeWidth) |
| 1486 |
? 0 |
| 1487 |
: SkScalarFloorToInt(delta / averageGraphemeWidth); |
| 1488 |
auto graphemeCenter = glyphemePosLeft + graphemeIndex * averageGraphemeWidth + |
| 1489 |
averageGraphemeWidth / 2; |
| 1490 |
auto graphemeUtf8Index = graphemes[graphemeIndex]; |
| 1491 |
if ((dx < graphemeCenter) == context.run->leftToRight()) { |
| 1492 |
size_t utf16Index = fOwner->getUTF16Index(graphemeUtf8Index); |
| 1493 |
result = { SkToS32(utf16Index), kDownstream }; |
| 1494 |
} else { |
| 1495 |
size_t utf16Index = fOwner->getUTF16Index(graphemeUtf8Index + 1); |
| 1496 |
result = { SkToS32(utf16Index), kUpstream }; |
| 1497 |
} |
| 1498 |
// Keep UTF16 index as is |
| 1499 |
} else if ((dx < center) == context.run->leftToRight()) { |
| 1500 |
size_t utf16Index = fOwner->getUTF16Index(clusterIndex8); |
| 1501 |
result = { SkToS32(utf16Index), kDownstream }; |
| 1502 |
} else { |
| 1503 |
size_t utf16Index = context.run->leftToRight() |
| 1504 |
? fOwner->getUTF16Index(clusterEnd8) |
| 1505 |
: fOwner->getUTF16Index(clusterIndex8) + 1; |
| 1506 |
result = { SkToS32(utf16Index), kUpstream }; |
| 1507 |
} |
| 1508 |
|
| 1509 |
return keepLooking = false; |
| 1510 |
|
| 1511 |
}); |
| 1512 |
return keepLooking; |
| 1513 |
} |
| 1514 |
); |
| 1515 |
return result; |
| 1516 |
} |
| 1517 |
|
| 1518 |
void TextLine::getRectsForPlaceholders(std::vector<TextBox>& boxes) { |
| 1519 |
this->iterateThroughVisualRuns( |
| 1520 |
true, |
| 1521 |
[&boxes, this](const Run* run, SkScalar runOffset, TextRange textRange, |
| 1522 |
SkScalar* width) { |
| 1523 |
auto context = this->measureTextInsideOneRun( |
| 1524 |
textRange, run, runOffset, 0, true, TextAdjustment::GraphemeGluster); |
| 1525 |
*width = context.clip.width(); |
| 1526 |
|
| 1527 |
if (textRange.width() == 0) { |
| 1528 |
return true; |
| 1529 |
} |
| 1530 |
if (!run->isPlaceholder()) { |
| 1531 |
return true; |
| 1532 |
} |
| 1533 |
|
| 1534 |
SkRect clip = context.clip; |
| 1535 |
clip.offset(this->offset()); |
| 1536 |
|
| 1537 |
if (fOwner->getApplyRoundingHack()) { |
| 1538 |
clip.fLeft = littleRound(clip.fLeft); |
| 1539 |
clip.fRight = littleRound(clip.fRight); |
| 1540 |
clip.fTop = littleRound(clip.fTop); |
| 1541 |
clip.fBottom = littleRound(clip.fBottom); |
| 1542 |
} |
| 1543 |
boxes.emplace_back(clip, run->getTextDirection()); |
| 1544 |
return true; |
| 1545 |
}); |
| 1546 |
} |
| 1547 |
} // namespace textlayout |
| 1548 |
} // namespace skia |