| 1 |
/* |
| 2 |
* Copyright 2019 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "modules/skottie/include/TextShaper.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkFontMetrics.h" |
| 12 |
#include "include/core/SkFontMgr.h" |
| 13 |
#include "include/core/SkFontTypes.h" |
| 14 |
#include "include/core/SkFourByteTag.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTypeface.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/private/base/SkTArray.h" |
| 22 |
#include "include/private/base/SkTPin.h" |
| 23 |
#include "include/private/base/SkTemplates.h" |
| 24 |
#include "include/private/base/SkTo.h" |
| 25 |
#include "modules/skshaper/include/SkShaper.h" |
| 26 |
#include "modules/skshaper/include/SkShaper_factory.h" |
| 27 |
#include "modules/skunicode/include/SkUnicode.h" |
| 28 |
#include "src/base/SkUTF.h" |
| 29 |
#include "src/core/SkFontPriv.h" |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
#include <memory> |
| 33 |
#include <numeric> |
| 34 |
#include <optional> |
| 35 |
#include <utility> |
| 36 |
|
| 37 |
#if !defined(SK_DISABLE_LEGACY_SHAPER_FACTORY) |
| 38 |
#include "modules/skshaper/utils/FactoryHelpers.h" |
| 39 |
#endif |
| 40 |
|
| 41 |
class SkPaint; |
| 42 |
|
| 43 |
using namespace skia_private; |
| 44 |
|
| 45 |
namespace skottie { |
| 46 |
namespace { |
| 47 |
static bool is_whitespace(char c) { |
| 48 |
// TODO: we've been getting away with this simple heuristic, |
| 49 |
// but ideally we should use SkUicode::isWhiteSpace(). |
| 50 |
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
| 51 |
} |
| 52 |
|
| 53 |
// Helper for interfacing with SkShaper: buffers shaper-fed runs and performs |
| 54 |
// per-line position adjustments (for external line breaking, horizontal alignment, etc). |
| 55 |
class ResultBuilder final : public SkShaper::RunHandler { |
| 56 |
public: |
| 57 |
ResultBuilder(const Shaper::TextDesc& desc, const SkRect& box, const sk_sp<SkFontMgr>& fontmgr, |
| 58 |
const sk_sp<SkShapers::Factory>& shapingFactory) |
| 59 |
: fDesc(desc) |
| 60 |
, fBox(box) |
| 61 |
, fHAlignFactor(HAlignFactor(fDesc.fHAlign)) |
| 62 |
, fFont(fDesc.fTypeface, fDesc.fTextSize) |
| 63 |
, fFontMgr(fontmgr) |
| 64 |
, fShapingFactory(shapingFactory) { |
| 65 |
// If the shaper callback returns null, fallback to the primitive shaper. |
| 66 |
SkASSERT(fShapingFactory); |
| 67 |
fShaper = fShapingFactory->makeShaper(fFontMgr); |
| 68 |
if (!fShaper) { |
| 69 |
fShaper = SkShapers::Primitive::PrimitiveText(); |
| 70 |
fShapingFactory = SkShapers::Primitive::Factory(); |
| 71 |
} |
| 72 |
fFont.setHinting(SkFontHinting::kNone); |
| 73 |
fFont.setSubpixel(true); |
| 74 |
fFont.setLinearMetrics(true); |
| 75 |
fFont.setBaselineSnap(false); |
| 76 |
fFont.setEdging(SkFont::Edging::kAntiAlias); |
| 77 |
} |
| 78 |
|
| 79 |
void beginLine() override { |
| 80 |
fLineGlyphs.reset(0); |
| 81 |
fLinePos.reset(0); |
| 82 |
fLineClusters.reset(0); |
| 83 |
fLineRuns.clear(); |
| 84 |
fLineGlyphCount = 0; |
| 85 |
|
| 86 |
fCurrentPosition = fOffset; |
| 87 |
fPendingLineAdvance = { 0, 0 }; |
| 88 |
|
| 89 |
fLastLineDescent = 0; |
| 90 |
} |
| 91 |
|
| 92 |
void runInfo(const RunInfo& info) override { |
| 93 |
fPendingLineAdvance += info.fAdvance; |
| 94 |
|
| 95 |
SkFontMetrics metrics; |
| 96 |
info.fFont.getMetrics(&metrics); |
| 97 |
if (!fLineCount) { |
| 98 |
fFirstLineAscent = std::min(fFirstLineAscent, metrics.fAscent); |
| 99 |
} |
| 100 |
fLastLineDescent = std::max(fLastLineDescent, metrics.fDescent); |
| 101 |
} |
| 102 |
|
| 103 |
void commitRunInfo() override {} |
| 104 |
|
| 105 |
Buffer runBuffer(const RunInfo& info) override { |
| 106 |
const auto run_start_index = fLineGlyphCount; |
| 107 |
fLineGlyphCount += info.glyphCount; |
| 108 |
|
| 109 |
fLineGlyphs.realloc(fLineGlyphCount); |
| 110 |
fLinePos.realloc(fLineGlyphCount); |
| 111 |
fLineClusters.realloc(fLineGlyphCount); |
| 112 |
fLineRuns.push_back({info.fFont, info.glyphCount}); |
| 113 |
|
| 114 |
SkVector alignmentOffset { fHAlignFactor * (fPendingLineAdvance.x() - fBox.width()), 0 }; |
| 115 |
|
| 116 |
return { |
| 117 |
fLineGlyphs.get() + run_start_index, |
| 118 |
fLinePos.get() + run_start_index, |
| 119 |
nullptr, |
| 120 |
fLineClusters.get() + run_start_index, |
| 121 |
fCurrentPosition + alignmentOffset |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
void commitRunBuffer(const RunInfo& info) override { |
| 126 |
fCurrentPosition += info.fAdvance; |
| 127 |
} |
| 128 |
|
| 129 |
void commitLine() override { |
| 130 |
fOffset.fY += fDesc.fLineHeight; |
| 131 |
|
| 132 |
// Observed AE handling of whitespace, for alignment purposes: |
| 133 |
// |
| 134 |
// - leading whitespace contributes to alignment |
| 135 |
// - trailing whitespace is ignored |
| 136 |
// - auto line breaking retains all separating whitespace on the first line (no artificial |
| 137 |
// leading WS is created). |
| 138 |
auto adjust_trailing_whitespace = [this]() { |
| 139 |
// For left-alignment, trailing WS doesn't make any difference. |
| 140 |
if (fLineRuns.empty() || fDesc.fHAlign == SkTextUtils::Align::kLeft_Align) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
// Technically, trailing whitespace could span multiple runs, but realistically, |
| 145 |
// SkShaper has no reason to split it. Hence we're only checking the last run. |
| 146 |
size_t ws_count = 0; |
| 147 |
for (size_t i = 0; i < fLineRuns.back().fSize; ++i) { |
| 148 |
if (is_whitespace(fUTF8[fLineClusters[SkToInt(fLineGlyphCount - i - 1)]])) { |
| 149 |
++ws_count; |
| 150 |
} else { |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// No trailing whitespace. |
| 156 |
if (!ws_count) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// Compute the cumulative whitespace advance. |
| 161 |
fAdvanceBuffer.resize(ws_count); |
| 162 |
fLineRuns.back().fFont.getWidths( |
| 163 |
{fLineGlyphs.data() + fLineGlyphCount - ws_count, ws_count}, |
| 164 |
{fAdvanceBuffer.data(), ws_count}); |
| 165 |
|
| 166 |
const auto ws_advance = std::accumulate(fAdvanceBuffer.begin(), |
| 167 |
fAdvanceBuffer.end(), |
| 168 |
0.0f); |
| 169 |
|
| 170 |
// Offset needed to compensate for whitespace. |
| 171 |
const auto offset = ws_advance*-fHAlignFactor; |
| 172 |
|
| 173 |
// Shift the whole line horizontally by the computed offset. |
| 174 |
std::transform(fLinePos.data(), |
| 175 |
fLinePos.data() + fLineGlyphCount, |
| 176 |
fLinePos.data(), |
| 177 |
[&offset](SkPoint pos) { return SkPoint{pos.fX + offset, pos.fY}; }); |
| 178 |
}; |
| 179 |
|
| 180 |
adjust_trailing_whitespace(); |
| 181 |
|
| 182 |
const auto commit_proc = (fDesc.fFlags & Shaper::Flags::kFragmentGlyphs) |
| 183 |
? &ResultBuilder::commitFragementedRun |
| 184 |
: &ResultBuilder::commitConsolidatedRun; |
| 185 |
|
| 186 |
size_t run_offset = 0; |
| 187 |
for (const auto& rec : fLineRuns) { |
| 188 |
SkASSERT(run_offset < fLineGlyphCount); |
| 189 |
(this->*commit_proc)(rec, |
| 190 |
fLineGlyphs.get() + run_offset, |
| 191 |
fLinePos.get() + run_offset, |
| 192 |
fLineClusters.get() + run_offset, |
| 193 |
fLineCount); |
| 194 |
run_offset += rec.fSize; |
| 195 |
} |
| 196 |
|
| 197 |
fLineCount++; |
| 198 |
} |
| 199 |
|
| 200 |
Shaper::Result finalize(SkSize* shaped_size) { |
| 201 |
if (!(fDesc.fFlags & Shaper::Flags::kFragmentGlyphs)) { |
| 202 |
// All glyphs (if any) are pending in a single fragment. |
| 203 |
SkASSERT(fResult.fFragments.size() <= 1); |
| 204 |
} |
| 205 |
|
| 206 |
const auto ascent = this->ascent(); |
| 207 |
|
| 208 |
// For visual VAlign modes, we use a hybrid extent box computed as the union of |
| 209 |
// actual visual bounds and the vertical typographical extent. |
| 210 |
// |
| 211 |
// This ensures that |
| 212 |
// |
| 213 |
// a) text doesn't visually overflow the alignment boundaries |
| 214 |
// |
| 215 |
// b) leading/trailing empty lines are still taken into account for alignment purposes |
| 216 |
|
| 217 |
auto extent_box = [&](bool include_typographical_extent) { |
| 218 |
auto box = fResult.computeVisualBounds(); |
| 219 |
|
| 220 |
if (include_typographical_extent) { |
| 221 |
// Hybrid visual alignment mode, based on typographical extent. |
| 222 |
|
| 223 |
// By default, first line is vertically-aligned on a baseline of 0. |
| 224 |
// The typographical height considered for vertical alignment is the distance |
| 225 |
// between the first line top (ascent) to the last line bottom (descent). |
| 226 |
const auto typographical_top = fBox.fTop + ascent, |
| 227 |
typographical_bottom = fBox.fTop + fLastLineDescent + |
| 228 |
fDesc.fLineHeight*(fLineCount > 0 ? fLineCount - 1 : 0ul); |
| 229 |
|
| 230 |
box.fTop = std::min(box.fTop, typographical_top); |
| 231 |
box.fBottom = std::max(box.fBottom, typographical_bottom); |
| 232 |
} |
| 233 |
|
| 234 |
return box; |
| 235 |
}; |
| 236 |
|
| 237 |
// Only compute the extent box when needed. |
| 238 |
std::optional<SkRect> ebox; |
| 239 |
|
| 240 |
// Vertical adjustments. |
| 241 |
float v_offset = -fDesc.fLineShift; |
| 242 |
|
| 243 |
switch (fDesc.fVAlign) { |
| 244 |
case Shaper::VAlign::kTop: |
| 245 |
v_offset -= ascent; |
| 246 |
break; |
| 247 |
case Shaper::VAlign::kTopBaseline: |
| 248 |
// Default behavior. |
| 249 |
break; |
| 250 |
case Shaper::VAlign::kHybridTop: |
| 251 |
case Shaper::VAlign::kVisualTop: |
| 252 |
ebox.emplace(extent_box(fDesc.fVAlign == Shaper::VAlign::kHybridTop)); |
| 253 |
v_offset += fBox.fTop - ebox->fTop; |
| 254 |
break; |
| 255 |
case Shaper::VAlign::kHybridCenter: |
| 256 |
case Shaper::VAlign::kVisualCenter: |
| 257 |
ebox.emplace(extent_box(fDesc.fVAlign == Shaper::VAlign::kHybridCenter)); |
| 258 |
v_offset += fBox.centerY() - ebox->centerY(); |
| 259 |
break; |
| 260 |
case Shaper::VAlign::kHybridBottom: |
| 261 |
case Shaper::VAlign::kVisualBottom: |
| 262 |
ebox.emplace(extent_box(fDesc.fVAlign == Shaper::VAlign::kHybridBottom)); |
| 263 |
v_offset += fBox.fBottom - ebox->fBottom; |
| 264 |
break; |
| 265 |
} |
| 266 |
|
| 267 |
if (shaped_size) { |
| 268 |
if (!ebox.has_value()) { |
| 269 |
ebox.emplace(extent_box(true)); |
| 270 |
} |
| 271 |
*shaped_size = SkSize::Make(ebox->width(), ebox->height()); |
| 272 |
} |
| 273 |
|
| 274 |
if (v_offset) { |
| 275 |
for (auto& fragment : fResult.fFragments) { |
| 276 |
fragment.fOrigin.fY += v_offset; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return std::move(fResult); |
| 281 |
} |
| 282 |
|
| 283 |
void shapeLine(const char* start, const char* end, size_t utf8_offset) { |
| 284 |
if (!fShaper) { |
| 285 |
return; |
| 286 |
} |
| 287 |
|
| 288 |
SkASSERT(start <= end); |
| 289 |
if (start == end) { |
| 290 |
// SkShaper doesn't care for empty lines. |
| 291 |
this->beginLine(); |
| 292 |
this->commitLine(); |
| 293 |
|
| 294 |
// The calls above perform bookkeeping, but they do not add any fragments (since there |
| 295 |
// are no runs to commit). |
| 296 |
// |
| 297 |
// Certain Skottie features (line-based range selectors) do require accurate indexing |
| 298 |
// information even for empty lines though -- so we inject empty fragments solely for |
| 299 |
// line index tracking. |
| 300 |
// |
| 301 |
// Note: we don't add empty fragments in consolidated mode because 1) consolidated mode |
| 302 |
// assumes there is a single result fragment and 2) kFragmentGlyphs is always enabled |
| 303 |
// for cases where line index tracking is relevant. |
| 304 |
// |
| 305 |
// TODO(fmalita): investigate whether it makes sense to move this special case down |
| 306 |
// to commitFragmentedRun(). |
| 307 |
if (fDesc.fFlags & Shaper::Flags::kFragmentGlyphs) { |
| 308 |
fResult.fFragments.push_back({ |
| 309 |
Shaper::ShapedGlyphs(), |
| 310 |
{fBox.x(),fBox.y()}, |
| 311 |
0, 0, |
| 312 |
fLineCount - 1, |
| 313 |
false |
| 314 |
}); |
| 315 |
} |
| 316 |
|
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
// In default paragraph mode (VAlign::kTop), AE clips out lines when the baseline |
| 321 |
// goes below the box lower edge. |
| 322 |
if (fDesc.fVAlign == Shaper::VAlign::kTop) { |
| 323 |
// fOffset is relative to the first line baseline. |
| 324 |
const auto max_offset = fBox.height() + this->ascent(); // NB: ascent is negative |
| 325 |
if (fOffset.y() > max_offset) { |
| 326 |
return; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
const auto shape_width = fDesc.fLinebreak == Shaper::LinebreakPolicy::kExplicit |
| 331 |
? SK_ScalarMax |
| 332 |
: fBox.width(); |
| 333 |
const auto shape_ltr = fDesc.fDirection == Shaper::Direction::kLTR; |
| 334 |
const size_t utf8_bytes = SkToSizeT(end - start); |
| 335 |
|
| 336 |
static constexpr uint8_t kBidiLevelLTR = 0, |
| 337 |
kBidiLevelRTL = 1; |
| 338 |
const auto lang_iter = fDesc.fLocale |
| 339 |
? std::make_unique<SkShaper::TrivialLanguageRunIterator>(fDesc.fLocale, utf8_bytes) |
| 340 |
: SkShaper::MakeStdLanguageRunIterator(start, utf8_bytes); |
| 341 |
#if defined(SKOTTIE_TRIVIAL_FONTRUN_ITER) |
| 342 |
// Chrome Linux/CrOS does not have a fallback-capable fontmgr, and crashes if fallback is |
| 343 |
// triggered. Using a TrivialFontRunIterator avoids the issue (https://crbug.com/1520148). |
| 344 |
const auto font_iter = std::make_unique<SkShaper::TrivialFontRunIterator>(fFont, |
| 345 |
utf8_bytes); |
| 346 |
#else |
| 347 |
const auto font_iter = SkShaper::MakeFontMgrRunIterator( |
| 348 |
start, utf8_bytes, fFont, |
| 349 |
fFontMgr ? fFontMgr : SkFontMgr::RefEmpty(), // used as fallback |
| 350 |
fDesc.fFontFamily, |
| 351 |
fFont.getTypeface()->fontStyle(), |
| 352 |
lang_iter.get()); |
| 353 |
#endif |
| 354 |
|
| 355 |
std::unique_ptr<SkShaper::BiDiRunIterator> bidi_iter = |
| 356 |
fShapingFactory->makeBidiRunIterator(start, utf8_bytes, |
| 357 |
shape_ltr ? kBidiLevelLTR : kBidiLevelRTL); |
| 358 |
if (!bidi_iter) { |
| 359 |
bidi_iter = std::make_unique<SkShaper::TrivialBiDiRunIterator>( |
| 360 |
shape_ltr ? kBidiLevelLTR : kBidiLevelRTL, utf8_bytes); |
| 361 |
} |
| 362 |
|
| 363 |
constexpr SkFourByteTag unknownScript = SkSetFourByteTag('Z', 'z', 'z', 'z'); |
| 364 |
std::unique_ptr<SkShaper::ScriptRunIterator> scpt_iter = |
| 365 |
fShapingFactory->makeScriptRunIterator(start, utf8_bytes, unknownScript); |
| 366 |
if (!scpt_iter) { |
| 367 |
scpt_iter = std::make_unique<SkShaper::TrivialScriptRunIterator>(unknownScript, utf8_bytes); |
| 368 |
} |
| 369 |
|
| 370 |
if (!font_iter || !bidi_iter || !scpt_iter || !lang_iter) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
fUTF8 = start; |
| 375 |
fUTF8Offset = utf8_offset; |
| 376 |
fShaper->shape(start, |
| 377 |
utf8_bytes, |
| 378 |
*font_iter, |
| 379 |
*bidi_iter, |
| 380 |
*scpt_iter, |
| 381 |
*lang_iter, |
| 382 |
nullptr, |
| 383 |
0, |
| 384 |
shape_width, |
| 385 |
this); |
| 386 |
fUTF8 = nullptr; |
| 387 |
} |
| 388 |
|
| 389 |
private: |
| 390 |
void commitFragementedRun(const skottie::Shaper::RunRec& run, |
| 391 |
const SkGlyphID* glyphs, |
| 392 |
const SkPoint* pos, |
| 393 |
const uint32_t* clusters, |
| 394 |
uint32_t line_index) { |
| 395 |
float ascent = 0; |
| 396 |
|
| 397 |
if (fDesc.fFlags & Shaper::Flags::kTrackFragmentAdvanceAscent) { |
| 398 |
SkFontMetrics metrics; |
| 399 |
run.fFont.getMetrics(&metrics); |
| 400 |
ascent = metrics.fAscent; |
| 401 |
|
| 402 |
// Note: we use per-glyph advances for anchoring, but it's unclear whether this |
| 403 |
// is exactly the same as AE. E.g. are 'acute' glyphs anchored separately for fonts |
| 404 |
// in which they're distinct? |
| 405 |
fAdvanceBuffer.resize(run.fSize); |
| 406 |
fFont.getWidths({glyphs, run.fSize}, {fAdvanceBuffer.data(), run.fSize}); |
| 407 |
} |
| 408 |
|
| 409 |
// In fragmented mode we immediately push the glyphs to fResult, |
| 410 |
// one fragment per glyph. Glyph positioning is externalized |
| 411 |
// (positions returned in Fragment::fPos). |
| 412 |
for (size_t i = 0; i < run.fSize; ++i) { |
| 413 |
const auto advance = (fDesc.fFlags & Shaper::Flags::kTrackFragmentAdvanceAscent) |
| 414 |
? fAdvanceBuffer[SkToInt(i)] |
| 415 |
: 0.0f; |
| 416 |
|
| 417 |
fResult.fFragments.push_back({ |
| 418 |
{ |
| 419 |
{ {run.fFont, 1} }, |
| 420 |
{ glyphs[i] }, |
| 421 |
{ {0,0} }, |
| 422 |
fDesc.fFlags & Shaper::kClusters |
| 423 |
? std::vector<size_t>{ fUTF8Offset + clusters[i] } |
| 424 |
: std::vector<size_t>({}), |
| 425 |
}, |
| 426 |
{ fBox.x() + pos[i].fX, fBox.y() + pos[i].fY }, |
| 427 |
advance, ascent, |
| 428 |
line_index, is_whitespace(fUTF8[clusters[i]]) |
| 429 |
}); |
| 430 |
|
| 431 |
// Note: we only check the first code point in the cluster for whitespace. |
| 432 |
// It's unclear whether thers's a saner approach. |
| 433 |
fResult.fMissingGlyphCount += (glyphs[i] == kMissingGlyphID); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
void commitConsolidatedRun(const skottie::Shaper::RunRec& run, |
| 438 |
const SkGlyphID* glyphs, |
| 439 |
const SkPoint* pos, |
| 440 |
const uint32_t* clusters, |
| 441 |
uint32_t) { |
| 442 |
// In consolidated mode we just accumulate glyphs to a single fragment in ResultBuilder. |
| 443 |
// Glyph positions are baked in the fragment runs (Fragment::fPos only reflects the |
| 444 |
// box origin). |
| 445 |
|
| 446 |
if (fResult.fFragments.empty()) { |
| 447 |
fResult.fFragments.push_back({{{}, {}, {}, {}}, {fBox.x(), fBox.y()}, 0, 0, 0, false}); |
| 448 |
} |
| 449 |
|
| 450 |
auto& current_glyphs = fResult.fFragments.back().fGlyphs; |
| 451 |
current_glyphs.fRuns.push_back(run); |
| 452 |
current_glyphs.fGlyphIDs.insert(current_glyphs.fGlyphIDs.end(), glyphs, glyphs + run.fSize); |
| 453 |
current_glyphs.fGlyphPos.insert(current_glyphs.fGlyphPos.end(), pos , pos + run.fSize); |
| 454 |
|
| 455 |
for (size_t i = 0; i < run.fSize; ++i) { |
| 456 |
fResult.fMissingGlyphCount += (glyphs[i] == kMissingGlyphID); |
| 457 |
} |
| 458 |
|
| 459 |
if (fDesc.fFlags & Shaper::kClusters) { |
| 460 |
current_glyphs.fClusters.reserve(current_glyphs.fClusters.size() + run.fSize); |
| 461 |
for (size_t i = 0; i < run.fSize; ++i) { |
| 462 |
current_glyphs.fClusters.push_back(fUTF8Offset + clusters[i]); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
static float HAlignFactor(SkTextUtils::Align align) { |
| 468 |
switch (align) { |
| 469 |
case SkTextUtils::kLeft_Align: return 0.0f; |
| 470 |
case SkTextUtils::kCenter_Align: return -0.5f; |
| 471 |
case SkTextUtils::kRight_Align: return -1.0f; |
| 472 |
} |
| 473 |
return 0.0f; // go home, msvc... |
| 474 |
} |
| 475 |
|
| 476 |
SkScalar ascent() const { |
| 477 |
// Use the explicit ascent, when specified. |
| 478 |
// Note: ascent values are negative (relative to the baseline). |
| 479 |
return fDesc.fAscent ? fDesc.fAscent : fFirstLineAscent; |
| 480 |
} |
| 481 |
|
| 482 |
inline static constexpr SkGlyphID kMissingGlyphID = 0; |
| 483 |
|
| 484 |
const Shaper::TextDesc& fDesc; |
| 485 |
const SkRect& fBox; |
| 486 |
const float fHAlignFactor; |
| 487 |
|
| 488 |
SkFont fFont; |
| 489 |
const sk_sp<SkFontMgr> fFontMgr; |
| 490 |
std::unique_ptr<SkShaper> fShaper; |
| 491 |
sk_sp<SkShapers::Factory> fShapingFactory; |
| 492 |
|
| 493 |
AutoSTMalloc<64, SkGlyphID> fLineGlyphs; |
| 494 |
AutoSTMalloc<64, SkPoint> fLinePos; |
| 495 |
AutoSTMalloc<64, uint32_t> fLineClusters; |
| 496 |
STArray<16, skottie::Shaper::RunRec> fLineRuns; |
| 497 |
size_t fLineGlyphCount = 0; |
| 498 |
|
| 499 |
STArray<64, float, true> fAdvanceBuffer; |
| 500 |
|
| 501 |
SkPoint fCurrentPosition{ 0, 0 }; |
| 502 |
SkPoint fOffset{ 0, 0 }; |
| 503 |
SkVector fPendingLineAdvance{ 0, 0 }; |
| 504 |
uint32_t fLineCount = 0; |
| 505 |
float fFirstLineAscent = 0, |
| 506 |
fLastLineDescent = 0; |
| 507 |
|
| 508 |
const char* fUTF8 = nullptr; // only valid during shapeLine() calls |
| 509 |
size_t fUTF8Offset = 0; // current line offset within the original string |
| 510 |
|
| 511 |
Shaper::Result fResult; |
| 512 |
}; |
| 513 |
|
| 514 |
Shaper::Result ShapeImpl(const SkString& txt, const Shaper::TextDesc& desc, |
| 515 |
const SkRect& box, const sk_sp<SkFontMgr>& fontmgr, |
| 516 |
const sk_sp<SkShapers::Factory>& shapingFactory, |
| 517 |
SkSize* shaped_size) { |
| 518 |
const auto& is_line_break = [](SkUnichar uch) { |
| 519 |
// TODO: other explicit breaks? |
| 520 |
return uch == '\r'; |
| 521 |
}; |
| 522 |
|
| 523 |
const char* ptr = txt.c_str(); |
| 524 |
const char* line_start = ptr; |
| 525 |
const char* begin = ptr; |
| 526 |
const char* end = ptr + txt.size(); |
| 527 |
|
| 528 |
ResultBuilder rbuilder(desc, box, fontmgr, shapingFactory); |
| 529 |
while (ptr < end) { |
| 530 |
if (is_line_break(SkUTF::NextUTF8(&ptr, end))) { |
| 531 |
rbuilder.shapeLine(line_start, ptr - 1, SkToSizeT(line_start - begin)); |
| 532 |
line_start = ptr; |
| 533 |
} |
| 534 |
} |
| 535 |
rbuilder.shapeLine(line_start, ptr, SkToSizeT(line_start - begin)); |
| 536 |
|
| 537 |
return rbuilder.finalize(shaped_size); |
| 538 |
} |
| 539 |
|
| 540 |
bool result_fits(const Shaper::Result& res, const SkSize& res_size, |
| 541 |
const SkRect& box, const Shaper::TextDesc& desc) { |
| 542 |
// optional max line count constraint |
| 543 |
if (desc.fMaxLines) { |
| 544 |
const auto line_count = res.fFragments.empty() |
| 545 |
? 0 |
| 546 |
: res.fFragments.back().fLineIndex + 1; |
| 547 |
if (line_count > desc.fMaxLines) { |
| 548 |
return false; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// geometric constraint |
| 553 |
return res_size.width() <= box.width() && res_size.height() <= box.height(); |
| 554 |
} |
| 555 |
|
| 556 |
Shaper::Result ShapeToFit(const SkString& txt, const Shaper::TextDesc& orig_desc, |
| 557 |
const SkRect& box, const sk_sp<SkFontMgr>& fontmgr, |
| 558 |
const sk_sp<SkShapers::Factory>& shapingFactory) { |
| 559 |
Shaper::Result best_result; |
| 560 |
|
| 561 |
if (box.isEmpty() || orig_desc.fTextSize <= 0) { |
| 562 |
return best_result; |
| 563 |
} |
| 564 |
|
| 565 |
auto desc = orig_desc; |
| 566 |
|
| 567 |
const auto min_scale = std::max(desc.fMinTextSize / desc.fTextSize, 0.0f), |
| 568 |
max_scale = std::max(desc.fMaxTextSize / desc.fTextSize, min_scale); |
| 569 |
|
| 570 |
float in_scale = min_scale, // maximum scale that fits inside |
| 571 |
out_scale = max_scale, // minimum scale that doesn't fit |
| 572 |
try_scale = SkTPin(1.0f, min_scale, max_scale); // current probe |
| 573 |
|
| 574 |
// Perform a binary search for the best vertical fit (SkShaper already handles |
| 575 |
// horizontal fitting), starting with the specified text size. |
| 576 |
// |
| 577 |
// This hybrid loop handles both the binary search (when in/out extremes are known), and an |
| 578 |
// exponential search for the extremes. |
| 579 |
static constexpr size_t kMaxIter = 16; |
| 580 |
for (size_t i = 0; i < kMaxIter; ++i) { |
| 581 |
SkASSERT(try_scale >= in_scale && try_scale <= out_scale); |
| 582 |
desc.fTextSize = try_scale * orig_desc.fTextSize; |
| 583 |
desc.fLineHeight = try_scale * orig_desc.fLineHeight; |
| 584 |
desc.fLineShift = try_scale * orig_desc.fLineShift; |
| 585 |
desc.fAscent = try_scale * orig_desc.fAscent; |
| 586 |
|
| 587 |
SkSize res_size = {0, 0}; |
| 588 |
auto res = ShapeImpl(txt, desc, box, fontmgr, shapingFactory, &res_size); |
| 589 |
|
| 590 |
const auto prev_scale = try_scale; |
| 591 |
if (!result_fits(res, res_size, box, desc)) { |
| 592 |
out_scale = try_scale; |
| 593 |
try_scale = (in_scale == min_scale) |
| 594 |
// initial in_scale not found yet - search exponentially |
| 595 |
? std::max(min_scale, try_scale * 0.5f) |
| 596 |
// in_scale found - binary search |
| 597 |
: (in_scale + out_scale) * 0.5f; |
| 598 |
} else { |
| 599 |
// It fits - so it's a candidate. |
| 600 |
best_result = std::move(res); |
| 601 |
best_result.fScale = try_scale; |
| 602 |
|
| 603 |
in_scale = try_scale; |
| 604 |
try_scale = (out_scale == max_scale) |
| 605 |
// initial out_scale not found yet - search exponentially |
| 606 |
? std::min(max_scale, try_scale * 2) |
| 607 |
// out_scale found - binary search |
| 608 |
: (in_scale + out_scale) * 0.5f; |
| 609 |
} |
| 610 |
|
| 611 |
if (try_scale == prev_scale) { |
| 612 |
// no more progress |
| 613 |
break; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
return best_result; |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
// Applies capitalization rules. |
| 622 |
class AdjustedText { |
| 623 |
public: |
| 624 |
AdjustedText(const SkString& txt, const Shaper::TextDesc& desc, SkUnicode* unicode) |
| 625 |
: fText(txt) { |
| 626 |
switch (desc.fCapitalization) { |
| 627 |
case Shaper::Capitalization::kNone: |
| 628 |
break; |
| 629 |
case Shaper::Capitalization::kUpperCase: |
| 630 |
if (unicode) { |
| 631 |
fText = unicode->toUpper(fText); |
| 632 |
} |
| 633 |
break; |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
operator const SkString&() const { return fText; } |
| 638 |
|
| 639 |
private: |
| 640 |
SkString fText; |
| 641 |
}; |
| 642 |
|
| 643 |
} // namespace |
| 644 |
|
| 645 |
Shaper::Result Shaper::Shape(const SkString& text, const TextDesc& desc, const SkPoint& point, |
| 646 |
const sk_sp<SkFontMgr>& fontmgr, const sk_sp<SkShapers::Factory>& shapingFactory) { |
| 647 |
const AdjustedText adjText(text, desc, shapingFactory->getUnicode()); |
| 648 |
|
| 649 |
return (desc.fResize == ResizePolicy::kScaleToFit || |
| 650 |
desc.fResize == ResizePolicy::kDownscaleToFit) // makes no sense in point mode |
| 651 |
? Result() |
| 652 |
: ShapeImpl(adjText, desc, SkRect::MakeEmpty().makeOffset(point.x(), point.y()), |
| 653 |
fontmgr, shapingFactory, nullptr); |
| 654 |
} |
| 655 |
|
| 656 |
Shaper::Result Shaper::Shape(const SkString& text, const TextDesc& desc, const SkRect& box, |
| 657 |
const sk_sp<SkFontMgr>& fontmgr, const sk_sp<SkShapers::Factory>& shapingFactory) { |
| 658 |
const AdjustedText adjText(text, desc, shapingFactory->getUnicode()); |
| 659 |
|
| 660 |
switch(desc.fResize) { |
| 661 |
case ResizePolicy::kNone: |
| 662 |
return ShapeImpl(adjText, desc, box, fontmgr, shapingFactory, nullptr); |
| 663 |
case ResizePolicy::kScaleToFit: |
| 664 |
return ShapeToFit(adjText, desc, box, fontmgr, shapingFactory); |
| 665 |
case ResizePolicy::kDownscaleToFit: { |
| 666 |
SkSize size; |
| 667 |
auto result = ShapeImpl(adjText, desc, box, fontmgr, shapingFactory, &size); |
| 668 |
|
| 669 |
return result_fits(result, size, box, desc) |
| 670 |
? result |
| 671 |
: ShapeToFit(adjText, desc, box, fontmgr, shapingFactory); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
SkUNREACHABLE; |
| 676 |
} |
| 677 |
|
| 678 |
SkRect Shaper::ShapedGlyphs::computeBounds(BoundsType btype) const { |
| 679 |
auto bounds = SkRect::MakeEmpty(); |
| 680 |
|
| 681 |
AutoSTArray<16, SkRect> glyphBounds; |
| 682 |
|
| 683 |
size_t offset = 0; |
| 684 |
for (const auto& run : fRuns) { |
| 685 |
SkRect font_bounds; |
| 686 |
if (btype == BoundsType::kConservative) { |
| 687 |
font_bounds = SkFontPriv::GetFontBounds(run.fFont); |
| 688 |
|
| 689 |
// Empty font bounds is likely a font bug -- fall back to tight bounds. |
| 690 |
if (font_bounds.isEmpty()) { |
| 691 |
btype = BoundsType::kTight; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
switch (btype) { |
| 696 |
case BoundsType::kConservative: { |
| 697 |
SkRect run_bounds = SkRect::BoundsOrEmpty({fGlyphPos.data() + offset, run.fSize}); |
| 698 |
run_bounds.fLeft += font_bounds.left(); |
| 699 |
run_bounds.fTop += font_bounds.top(); |
| 700 |
run_bounds.fRight += font_bounds.right(); |
| 701 |
run_bounds.fBottom += font_bounds.bottom(); |
| 702 |
|
| 703 |
bounds.join(run_bounds); |
| 704 |
} break; |
| 705 |
case BoundsType::kTight: { |
| 706 |
glyphBounds.reset(SkToInt(run.fSize)); |
| 707 |
run.fFont.getBounds({fGlyphIDs.data() + offset, run.fSize}, glyphBounds, nullptr); |
| 708 |
|
| 709 |
for (size_t i = 0; i < run.fSize; ++i) { |
| 710 |
bounds.join(glyphBounds[SkToInt(i)].makeOffset(fGlyphPos[offset + i])); |
| 711 |
} |
| 712 |
} break; |
| 713 |
} |
| 714 |
|
| 715 |
offset += run.fSize; |
| 716 |
} |
| 717 |
|
| 718 |
return bounds; |
| 719 |
} |
| 720 |
|
| 721 |
void Shaper::ShapedGlyphs::draw(SkCanvas* canvas, |
| 722 |
const SkPoint& origin, |
| 723 |
const SkPaint& paint) const { |
| 724 |
size_t offset = 0; |
| 725 |
for (const auto& run : fRuns) { |
| 726 |
canvas->drawGlyphs({fGlyphIDs.data() + offset, run.fSize}, |
| 727 |
{fGlyphPos.data() + offset, run.fSize}, |
| 728 |
origin, |
| 729 |
run.fFont, |
| 730 |
paint); |
| 731 |
offset += run.fSize; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
SkRect Shaper::Result::computeVisualBounds() const { |
| 736 |
auto bounds = SkRect::MakeEmpty(); |
| 737 |
|
| 738 |
for (const auto& frag: fFragments) { |
| 739 |
bounds.join(frag.fGlyphs.computeBounds(ShapedGlyphs::BoundsType::kTight) |
| 740 |
.makeOffset(frag.fOrigin)); |
| 741 |
} |
| 742 |
|
| 743 |
return bounds; |
| 744 |
} |
| 745 |
|
| 746 |
#if !defined(SK_DISABLE_LEGACY_SHAPER_FACTORY) |
| 747 |
Shaper::Result Shaper::Shape(const SkString& text, const TextDesc& desc, const SkPoint& point, |
| 748 |
const sk_sp<SkFontMgr>& fontmgr) { |
| 749 |
return Shaper::Shape(text, desc, point, fontmgr, SkShapers::BestAvailable()); |
| 750 |
} |
| 751 |
|
| 752 |
Shaper::Result Shaper::Shape(const SkString& text, const TextDesc& desc, const SkRect& box, |
| 753 |
const sk_sp<SkFontMgr>& fontmgr) { |
| 754 |
return Shaper::Shape(text, desc, box, fontmgr, SkShapers::BestAvailable()); |
| 755 |
} |
| 756 |
|
| 757 |
#endif |
| 758 |
|
| 759 |
} // namespace skottie |