| 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/svg/include/SkSVGText.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkContourMeasure.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkFontMgr.h" |
| 14 |
#include "include/core/SkFontStyle.h" |
| 15 |
#include "include/core/SkFontTypes.h" |
| 16 |
#include "include/core/SkMatrix.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkPathBuilder.h" |
| 19 |
#include "include/core/SkPoint.h" |
| 20 |
#include "include/core/SkRSXform.h" |
| 21 |
#include "include/core/SkScalar.h" |
| 22 |
#include "include/core/SkString.h" |
| 23 |
#include "include/core/SkTextBlob.h" |
| 24 |
#include "include/core/SkTypeface.h" |
| 25 |
#include "include/core/SkTypes.h" |
| 26 |
#include "include/private/base/SkTArray.h" |
| 27 |
#include "include/private/base/SkTemplates.h" |
| 28 |
#include "include/private/base/SkTo.h" |
| 29 |
#include "modules/skshaper/include/SkShaper.h" |
| 30 |
#include "modules/svg/include/SkSVGAttribute.h" |
| 31 |
#include "modules/svg/include/SkSVGAttributeParser.h" |
| 32 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 33 |
#include "modules/svg/src/SkSVGTextPriv.h" |
| 34 |
#include "src/base/SkUTF.h" |
| 35 |
#include "src/core/SkTextBlobPriv.h" |
| 36 |
|
| 37 |
#include <algorithm> |
| 38 |
#include <cstddef> |
| 39 |
#include <cstdint> |
| 40 |
#include <functional> |
| 41 |
#include <limits> |
| 42 |
#include <memory> |
| 43 |
#include <tuple> |
| 44 |
#include <utility> |
| 45 |
|
| 46 |
using namespace skia_private; |
| 47 |
|
| 48 |
namespace { |
| 49 |
|
| 50 |
static SkFont ResolveFont(const SkSVGRenderContext& ctx) { |
| 51 |
auto weight = [](const SkSVGFontWeight& w) { |
| 52 |
switch (w.type()) { |
| 53 |
case SkSVGFontWeight::Type::k100: return SkFontStyle::kThin_Weight; |
| 54 |
case SkSVGFontWeight::Type::k200: return SkFontStyle::kExtraLight_Weight; |
| 55 |
case SkSVGFontWeight::Type::k300: return SkFontStyle::kLight_Weight; |
| 56 |
case SkSVGFontWeight::Type::k400: return SkFontStyle::kNormal_Weight; |
| 57 |
case SkSVGFontWeight::Type::k500: return SkFontStyle::kMedium_Weight; |
| 58 |
case SkSVGFontWeight::Type::k600: return SkFontStyle::kSemiBold_Weight; |
| 59 |
case SkSVGFontWeight::Type::k700: return SkFontStyle::kBold_Weight; |
| 60 |
case SkSVGFontWeight::Type::k800: return SkFontStyle::kExtraBold_Weight; |
| 61 |
case SkSVGFontWeight::Type::k900: return SkFontStyle::kBlack_Weight; |
| 62 |
case SkSVGFontWeight::Type::kNormal: return SkFontStyle::kNormal_Weight; |
| 63 |
case SkSVGFontWeight::Type::kBold: return SkFontStyle::kBold_Weight; |
| 64 |
case SkSVGFontWeight::Type::kBolder: return SkFontStyle::kExtraBold_Weight; |
| 65 |
case SkSVGFontWeight::Type::kLighter: return SkFontStyle::kLight_Weight; |
| 66 |
case SkSVGFontWeight::Type::kInherit: { |
| 67 |
SkASSERT(false); |
| 68 |
return SkFontStyle::kNormal_Weight; |
| 69 |
} |
| 70 |
} |
| 71 |
SkUNREACHABLE; |
| 72 |
}; |
| 73 |
|
| 74 |
auto slant = [](const SkSVGFontStyle& s) { |
| 75 |
switch (s.type()) { |
| 76 |
case SkSVGFontStyle::Type::kNormal: return SkFontStyle::kUpright_Slant; |
| 77 |
case SkSVGFontStyle::Type::kItalic: return SkFontStyle::kItalic_Slant; |
| 78 |
case SkSVGFontStyle::Type::kOblique: return SkFontStyle::kOblique_Slant; |
| 79 |
case SkSVGFontStyle::Type::kInherit: { |
| 80 |
SkASSERT(false); |
| 81 |
return SkFontStyle::kUpright_Slant; |
| 82 |
} |
| 83 |
} |
| 84 |
SkUNREACHABLE; |
| 85 |
}; |
| 86 |
|
| 87 |
const auto& family = ctx.presentationContext().fInherited.fFontFamily->family(); |
| 88 |
const SkFontStyle style(weight(*ctx.presentationContext().fInherited.fFontWeight), |
| 89 |
SkFontStyle::kNormal_Width, |
| 90 |
slant(*ctx.presentationContext().fInherited.fFontStyle)); |
| 91 |
|
| 92 |
const auto size = |
| 93 |
ctx.lengthContext().resolve(ctx.presentationContext().fInherited.fFontSize->size(), |
| 94 |
SkSVGLengthContext::LengthType::kVertical); |
| 95 |
|
| 96 |
// TODO: we likely want matchFamilyStyle here, but switching away from legacyMakeTypeface |
| 97 |
// changes all the results when using the default fontmgr. |
| 98 |
auto tf = ctx.fontMgr()->legacyMakeTypeface(family.c_str(), style); |
| 99 |
if (!tf) { |
| 100 |
tf = ctx.fontMgr()->legacyMakeTypeface(nullptr, style); |
| 101 |
} |
| 102 |
SkASSERT(tf); |
| 103 |
SkFont font(std::move(tf), size); |
| 104 |
font.setHinting(SkFontHinting::kNone); |
| 105 |
font.setSubpixel(true); |
| 106 |
font.setLinearMetrics(true); |
| 107 |
font.setBaselineSnap(false); |
| 108 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 109 |
|
| 110 |
return font; |
| 111 |
} |
| 112 |
|
| 113 |
static std::vector<float> ResolveLengths(const SkSVGLengthContext& lctx, |
| 114 |
const std::vector<SkSVGLength>& lengths, |
| 115 |
SkSVGLengthContext::LengthType lt) { |
| 116 |
std::vector<float> resolved; |
| 117 |
resolved.reserve(lengths.size()); |
| 118 |
|
| 119 |
for (const auto& l : lengths) { |
| 120 |
resolved.push_back(lctx.resolve(l, lt)); |
| 121 |
} |
| 122 |
|
| 123 |
return resolved; |
| 124 |
} |
| 125 |
|
| 126 |
static float ComputeAlignmentFactor(const SkSVGPresentationContext& pctx) { |
| 127 |
switch (pctx.fInherited.fTextAnchor->type()) { |
| 128 |
case SkSVGTextAnchor::Type::kStart : return 0.0f; |
| 129 |
case SkSVGTextAnchor::Type::kMiddle: return -0.5f; |
| 130 |
case SkSVGTextAnchor::Type::kEnd : return -1.0f; |
| 131 |
case SkSVGTextAnchor::Type::kInherit: |
| 132 |
SkASSERT(false); |
| 133 |
return 0.0f; |
| 134 |
} |
| 135 |
SkUNREACHABLE; |
| 136 |
} |
| 137 |
|
| 138 |
} // namespace |
| 139 |
|
| 140 |
SkSVGTextContext::ScopedPosResolver::ScopedPosResolver(const SkSVGTextContainer& txt, |
| 141 |
const SkSVGLengthContext& lctx, |
| 142 |
SkSVGTextContext* tctx, |
| 143 |
size_t charIndexOffset) |
| 144 |
: fTextContext(tctx) |
| 145 |
, fParent(tctx->fPosResolver) |
| 146 |
, fCharIndexOffset(charIndexOffset) |
| 147 |
, fX(ResolveLengths(lctx, txt.getX(), SkSVGLengthContext::LengthType::kHorizontal)) |
| 148 |
, fY(ResolveLengths(lctx, txt.getY(), SkSVGLengthContext::LengthType::kVertical)) |
| 149 |
, fDx(ResolveLengths(lctx, txt.getDx(), SkSVGLengthContext::LengthType::kHorizontal)) |
| 150 |
, fDy(ResolveLengths(lctx, txt.getDy(), SkSVGLengthContext::LengthType::kVertical)) |
| 151 |
, fRotate(txt.getRotate()) |
| 152 |
{ |
| 153 |
fTextContext->fPosResolver = this; |
| 154 |
} |
| 155 |
|
| 156 |
SkSVGTextContext::ScopedPosResolver::ScopedPosResolver(const SkSVGTextContainer& txt, |
| 157 |
const SkSVGLengthContext& lctx, |
| 158 |
SkSVGTextContext* tctx) |
| 159 |
: ScopedPosResolver(txt, lctx, tctx, tctx->fCurrentCharIndex) {} |
| 160 |
|
| 161 |
SkSVGTextContext::ScopedPosResolver::~ScopedPosResolver() { |
| 162 |
fTextContext->fPosResolver = fParent; |
| 163 |
} |
| 164 |
|
| 165 |
SkSVGTextContext::PosAttrs SkSVGTextContext::ScopedPosResolver::resolve(size_t charIndex) const { |
| 166 |
PosAttrs attrs; |
| 167 |
|
| 168 |
if (charIndex < fLastPosIndex) { |
| 169 |
SkASSERT(charIndex >= fCharIndexOffset); |
| 170 |
const auto localCharIndex = charIndex - fCharIndexOffset; |
| 171 |
|
| 172 |
const auto hasAllLocal = localCharIndex < fX.size() && |
| 173 |
localCharIndex < fY.size() && |
| 174 |
localCharIndex < fDx.size() && |
| 175 |
localCharIndex < fDy.size() && |
| 176 |
localCharIndex < fRotate.size(); |
| 177 |
if (!hasAllLocal && fParent) { |
| 178 |
attrs = fParent->resolve(charIndex); |
| 179 |
} |
| 180 |
|
| 181 |
if (localCharIndex < fX.size()) { |
| 182 |
attrs[PosAttrs::kX] = fX[localCharIndex]; |
| 183 |
} |
| 184 |
if (localCharIndex < fY.size()) { |
| 185 |
attrs[PosAttrs::kY] = fY[localCharIndex]; |
| 186 |
} |
| 187 |
if (localCharIndex < fDx.size()) { |
| 188 |
attrs[PosAttrs::kDx] = fDx[localCharIndex]; |
| 189 |
} |
| 190 |
if (localCharIndex < fDy.size()) { |
| 191 |
attrs[PosAttrs::kDy] = fDy[localCharIndex]; |
| 192 |
} |
| 193 |
|
| 194 |
// Rotation semantics are interestingly different [1]: |
| 195 |
// |
| 196 |
// - values are not cumulative |
| 197 |
// - if explicit values are present at any level in the ancestor chain, those take |
| 198 |
// precedence (closest ancestor) |
| 199 |
// - last specified value applies to all remaining chars (closest ancestor) |
| 200 |
// - these rules apply at node scope (not chunk scope) |
| 201 |
// |
| 202 |
// This means we need to discriminate between explicit rotation (rotate value provided for |
| 203 |
// current char) and implicit rotation (ancestor has some values - but not for the requested |
| 204 |
// char - we use the last specified value). |
| 205 |
// |
| 206 |
// [1] https://www.w3.org/TR/SVG11/text.html#TSpanElementRotateAttribute |
| 207 |
if (!fRotate.empty()) { |
| 208 |
if (localCharIndex < fRotate.size()) { |
| 209 |
// Explicit rotation value overrides anything in the ancestor chain. |
| 210 |
attrs[PosAttrs::kRotate] = fRotate[localCharIndex]; |
| 211 |
attrs.setImplicitRotate(false); |
| 212 |
} else if (!attrs.has(PosAttrs::kRotate) || attrs.isImplicitRotate()){ |
| 213 |
// Local implicit rotation (last specified value) overrides ancestor implicit |
| 214 |
// rotation. |
| 215 |
attrs[PosAttrs::kRotate] = fRotate.back(); |
| 216 |
attrs.setImplicitRotate(true); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (!attrs.hasAny()) { |
| 221 |
// Once we stop producing explicit position data, there is no reason to |
| 222 |
// continue trying for higher indices. We can suppress future lookups. |
| 223 |
fLastPosIndex = charIndex; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return attrs; |
| 228 |
} |
| 229 |
|
| 230 |
void SkSVGTextContext::ShapeBuffer::append(SkUnichar ch, PositionAdjustment pos) { |
| 231 |
// relative pos adjustments are cumulative |
| 232 |
if (!fUtf8PosAdjust.empty()) { |
| 233 |
pos.offset += fUtf8PosAdjust.back().offset; |
| 234 |
} |
| 235 |
|
| 236 |
char utf8_buf[SkUTF::kMaxBytesInUTF8Sequence]; |
| 237 |
const auto utf8_len = SkToInt(SkUTF::ToUTF8(ch, utf8_buf)); |
| 238 |
fUtf8 .push_back_n(utf8_len, utf8_buf); |
| 239 |
fUtf8PosAdjust.push_back_n(utf8_len, pos); |
| 240 |
} |
| 241 |
|
| 242 |
void SkSVGTextContext::shapePendingBuffer(const SkSVGRenderContext& ctx, const SkFont& font) { |
| 243 |
const char* utf8 = fShapeBuffer.fUtf8.data(); |
| 244 |
size_t utf8Bytes = fShapeBuffer.fUtf8.size(); |
| 245 |
|
| 246 |
std::unique_ptr<SkShaper::FontRunIterator> font_runs = |
| 247 |
SkShaper::MakeFontMgrRunIterator(utf8, utf8Bytes, font, ctx.fontMgr()); |
| 248 |
if (!font_runs) { |
| 249 |
return; |
| 250 |
} |
| 251 |
if (!fForcePrimitiveShaping) { |
| 252 |
// Try to use the passed in shaping callbacks to shape, for example, using harfbuzz and ICU. |
| 253 |
const uint8_t defaultLTR = 0; |
| 254 |
std::unique_ptr<SkShaper::BiDiRunIterator> bidi = |
| 255 |
ctx.makeBidiRunIterator(utf8, utf8Bytes, defaultLTR); |
| 256 |
std::unique_ptr<SkShaper::LanguageRunIterator> language = |
| 257 |
SkShaper::MakeStdLanguageRunIterator(utf8, utf8Bytes); |
| 258 |
std::unique_ptr<SkShaper::ScriptRunIterator> script = ctx.makeScriptRunIterator(utf8, utf8Bytes); |
| 259 |
|
| 260 |
if (bidi && script && language) { |
| 261 |
fShaper->shape(utf8, |
| 262 |
utf8Bytes, |
| 263 |
*font_runs, |
| 264 |
*bidi, |
| 265 |
*script, |
| 266 |
*language, |
| 267 |
nullptr, |
| 268 |
0, |
| 269 |
SK_ScalarMax, |
| 270 |
this); |
| 271 |
fShapeBuffer.reset(); |
| 272 |
return; |
| 273 |
} // If any of the callbacks fail, we'll fallback to the primitive shaping. |
| 274 |
} |
| 275 |
|
| 276 |
// bidi, script, and lang are all unused so we can construct them with empty data. |
| 277 |
SkShaper::TrivialBiDiRunIterator trivial_bidi{0, 0}; |
| 278 |
SkShaper::TrivialScriptRunIterator trivial_script{0, 0}; |
| 279 |
SkShaper::TrivialLanguageRunIterator trivial_lang{nullptr, 0}; |
| 280 |
fShaper->shape(utf8, |
| 281 |
utf8Bytes, |
| 282 |
*font_runs, |
| 283 |
trivial_bidi, |
| 284 |
trivial_script, |
| 285 |
trivial_lang, |
| 286 |
nullptr, |
| 287 |
0, |
| 288 |
SK_ScalarMax, |
| 289 |
this); |
| 290 |
fShapeBuffer.reset(); |
| 291 |
} |
| 292 |
|
| 293 |
SkSVGTextContext::SkSVGTextContext(const SkSVGRenderContext& ctx, |
| 294 |
const ShapedTextCallback& cb, |
| 295 |
const SkSVGTextPath* tpath) |
| 296 |
: fRenderContext(ctx) |
| 297 |
, fCallback(cb) |
| 298 |
, fShaper(ctx.makeShaper()) |
| 299 |
, fChunkAlignmentFactor(ComputeAlignmentFactor(ctx.presentationContext())) { |
| 300 |
// If the shaper callback returns null, fallback to the primitive shaper and |
| 301 |
// signal that we should not use the other callbacks in shapePendingBuffer |
| 302 |
if (!fShaper) { |
| 303 |
fShaper = SkShapers::Primitive::PrimitiveText(); |
| 304 |
fForcePrimitiveShaping = true; |
| 305 |
} |
| 306 |
if (tpath) { |
| 307 |
fPathData = std::make_unique<PathData>(ctx, *tpath); |
| 308 |
|
| 309 |
// https://www.w3.org/TR/SVG11/text.html#TextPathElementStartOffsetAttribute |
| 310 |
auto resolve_offset = [this](const SkSVGLength& offset) { |
| 311 |
if (offset.unit() != SkSVGLength::Unit::kPercentage) { |
| 312 |
// "If a <length> other than a percentage is given, then the ‘startOffset’ |
| 313 |
// represents a distance along the path measured in the current user coordinate |
| 314 |
// system." |
| 315 |
return fRenderContext.lengthContext() |
| 316 |
.resolve(offset, SkSVGLengthContext::LengthType::kHorizontal); |
| 317 |
} |
| 318 |
|
| 319 |
// "If a percentage is given, then the ‘startOffset’ represents a percentage distance |
| 320 |
// along the entire path." |
| 321 |
return offset.value() * fPathData->length() / 100; |
| 322 |
}; |
| 323 |
|
| 324 |
// startOffset acts as an initial absolute position |
| 325 |
fChunkPos.fX = resolve_offset(tpath->getStartOffset()); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
SkSVGTextContext::~SkSVGTextContext() { |
| 330 |
this->flushChunk(fRenderContext); |
| 331 |
} |
| 332 |
|
| 333 |
void SkSVGTextContext::shapeFragment(const SkString& txt, const SkSVGRenderContext& ctx, |
| 334 |
SkSVGXmlSpace xs) { |
| 335 |
// https://www.w3.org/TR/SVG11/text.html#WhiteSpace |
| 336 |
// https://www.w3.org/TR/2008/REC-xml-20081126/#NT-S |
| 337 |
auto filterWSDefault = [this](SkUnichar ch) -> SkUnichar { |
| 338 |
// Remove all newline chars. |
| 339 |
if (ch == '\n') { |
| 340 |
return -1; |
| 341 |
} |
| 342 |
|
| 343 |
// Convert tab chars to space. |
| 344 |
if (ch == '\t') { |
| 345 |
ch = ' '; |
| 346 |
} |
| 347 |
|
| 348 |
// Consolidate contiguous space chars and strip leading spaces (fPrevCharSpace |
| 349 |
// starts off as true). |
| 350 |
if (fPrevCharSpace && ch == ' ') { |
| 351 |
return -1; |
| 352 |
} |
| 353 |
|
| 354 |
// TODO: Strip trailing WS? Doing this across chunks would require another buffering |
| 355 |
// layer. In general, trailing WS should have no rendering side effects. Skipping |
| 356 |
// for now. |
| 357 |
return ch; |
| 358 |
}; |
| 359 |
auto filterWSPreserve = [](SkUnichar ch) -> SkUnichar { |
| 360 |
// Convert newline and tab chars to space. |
| 361 |
if (ch == '\n' || ch == '\t') { |
| 362 |
ch = ' '; |
| 363 |
} |
| 364 |
return ch; |
| 365 |
}; |
| 366 |
|
| 367 |
// Stash paints for access from SkShaper callbacks. |
| 368 |
fCurrentFill = ctx.fillPaint(); |
| 369 |
fCurrentStroke = ctx.strokePaint(); |
| 370 |
|
| 371 |
const auto font = ResolveFont(ctx); |
| 372 |
fShapeBuffer.reserve(txt.size()); |
| 373 |
|
| 374 |
const char* ch_ptr = txt.c_str(); |
| 375 |
const char* ch_end = ch_ptr + txt.size(); |
| 376 |
|
| 377 |
while (ch_ptr < ch_end) { |
| 378 |
auto ch = SkUTF::NextUTF8(&ch_ptr, ch_end); |
| 379 |
ch = (xs == SkSVGXmlSpace::kDefault) |
| 380 |
? filterWSDefault(ch) |
| 381 |
: filterWSPreserve(ch); |
| 382 |
|
| 383 |
if (ch < 0) { |
| 384 |
// invalid utf or char filtered out |
| 385 |
continue; |
| 386 |
} |
| 387 |
|
| 388 |
SkASSERT(fPosResolver); |
| 389 |
const auto pos = fPosResolver->resolve(fCurrentCharIndex++); |
| 390 |
|
| 391 |
// Absolute position adjustments define a new chunk. |
| 392 |
// (https://www.w3.org/TR/SVG11/text.html#TextLayoutIntroduction) |
| 393 |
if (pos.has(PosAttrs::kX) || pos.has(PosAttrs::kY)) { |
| 394 |
this->shapePendingBuffer(ctx, font); |
| 395 |
this->flushChunk(ctx); |
| 396 |
|
| 397 |
// New chunk position. |
| 398 |
if (pos.has(PosAttrs::kX)) { |
| 399 |
fChunkPos.fX = pos[PosAttrs::kX]; |
| 400 |
} |
| 401 |
if (pos.has(PosAttrs::kY)) { |
| 402 |
fChunkPos.fY = pos[PosAttrs::kY]; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
fShapeBuffer.append(ch, { |
| 407 |
{ |
| 408 |
pos.has(PosAttrs::kDx) ? pos[PosAttrs::kDx] : 0, |
| 409 |
pos.has(PosAttrs::kDy) ? pos[PosAttrs::kDy] : 0, |
| 410 |
}, |
| 411 |
pos.has(PosAttrs::kRotate) ? SkDegreesToRadians(pos[PosAttrs::kRotate]) : 0, |
| 412 |
}); |
| 413 |
|
| 414 |
fPrevCharSpace = (ch == ' '); |
| 415 |
} |
| 416 |
|
| 417 |
this->shapePendingBuffer(ctx, font); |
| 418 |
|
| 419 |
// Note: at this point we have shaped and buffered RunRecs for the current fragment. |
| 420 |
// The active text chunk continues until an explicit or implicit flush. |
| 421 |
} |
| 422 |
|
| 423 |
SkSVGTextContext::PathData::PathData(const SkSVGRenderContext& ctx, const SkSVGTextPath& tpath) |
| 424 |
{ |
| 425 |
const auto ref = ctx.findNodeById(tpath.getHref()); |
| 426 |
if (!ref) { |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
SkContourMeasureIter cmi(ref->asPath(ctx), false); |
| 431 |
while (sk_sp<SkContourMeasure> contour = cmi.next()) { |
| 432 |
fLength += contour->length(); |
| 433 |
fContours.push_back(std::move(contour)); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
SkMatrix SkSVGTextContext::PathData::getMatrixAt(float offset) const { |
| 438 |
if (offset >= 0) { |
| 439 |
for (const auto& contour : fContours) { |
| 440 |
const auto contour_len = contour->length(); |
| 441 |
if (offset < contour_len) { |
| 442 |
SkMatrix m; |
| 443 |
return contour->getMatrix(offset, &m) ? m : SkMatrix::I(); |
| 444 |
} |
| 445 |
offset -= contour_len; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
// Quick & dirty way to "skip" rendering of glyphs off path. |
| 450 |
return SkMatrix::Translate(std::numeric_limits<float>::infinity(), |
| 451 |
std::numeric_limits<float>::infinity()); |
| 452 |
} |
| 453 |
|
| 454 |
SkRSXform SkSVGTextContext::computeGlyphXform(SkGlyphID glyph, const SkFont& font, |
| 455 |
const SkPoint& glyph_pos, |
| 456 |
const PositionAdjustment& pos_adjust) const { |
| 457 |
SkPoint pos = fChunkPos + glyph_pos + pos_adjust.offset + fChunkAdvance * fChunkAlignmentFactor; |
| 458 |
if (!fPathData) { |
| 459 |
return SkRSXform::MakeFromRadians(/*scale=*/ 1, pos_adjust.rotation, pos.fX, pos.fY, 0, 0); |
| 460 |
} |
| 461 |
|
| 462 |
// We're in a textPath scope, reposition the glyph on path. |
| 463 |
// (https://www.w3.org/TR/SVG11/text.html#TextpathLayoutRules) |
| 464 |
|
| 465 |
// Path positioning is based on the glyph center (horizontal component). |
| 466 |
float glyph_width = font.getWidth(glyph); |
| 467 |
auto path_offset = pos.fX + glyph_width * .5f; |
| 468 |
|
| 469 |
// In addition to the path matrix, the final glyph matrix also includes: |
| 470 |
// |
| 471 |
// -- vertical position adjustment "dy" ("dx" is factored into path_offset) |
| 472 |
// -- glyph origin adjustment (undoing the glyph center offset above) |
| 473 |
// -- explicit rotation adjustment (composing with the path glyph rotation) |
| 474 |
const auto m = fPathData->getMatrixAt(path_offset) * |
| 475 |
SkMatrix::Translate(-glyph_width * .5f, pos_adjust.offset.fY) * |
| 476 |
SkMatrix::RotateRad(pos_adjust.rotation); |
| 477 |
|
| 478 |
return SkRSXform::Make(m.getScaleX(), m.getSkewY(), m.getTranslateX(), m.getTranslateY()); |
| 479 |
} |
| 480 |
|
| 481 |
void SkSVGTextContext::flushChunk(const SkSVGRenderContext& ctx) { |
| 482 |
SkTextBlobBuilder blobBuilder; |
| 483 |
|
| 484 |
for (const auto& run : fRuns) { |
| 485 |
const auto& buf = blobBuilder.allocRunRSXform(run.font, SkToInt(run.glyphCount)); |
| 486 |
std::copy(run.glyphs.get(), run.glyphs.get() + run.glyphCount, buf.glyphs); |
| 487 |
for (size_t i = 0; i < run.glyphCount; ++i) { |
| 488 |
buf.xforms()[i] = this->computeGlyphXform(run.glyphs[i], |
| 489 |
run.font, |
| 490 |
run.glyphPos[i], |
| 491 |
run.glyhPosAdjust[i]); |
| 492 |
} |
| 493 |
|
| 494 |
fCallback(ctx, blobBuilder.make(), run.fillPaint.get(), run.strokePaint.get()); |
| 495 |
} |
| 496 |
|
| 497 |
fChunkPos += fChunkAdvance; |
| 498 |
fChunkAdvance = {0,0}; |
| 499 |
fChunkAlignmentFactor = ComputeAlignmentFactor(ctx.presentationContext()); |
| 500 |
|
| 501 |
fRuns.clear(); |
| 502 |
} |
| 503 |
|
| 504 |
SkShaper::RunHandler::Buffer SkSVGTextContext::runBuffer(const RunInfo& ri) { |
| 505 |
SkASSERT(ri.glyphCount); |
| 506 |
|
| 507 |
fRuns.push_back({ |
| 508 |
ri.fFont, |
| 509 |
fCurrentFill.has_value() ? std::make_unique<SkPaint>(*fCurrentFill) : nullptr, |
| 510 |
fCurrentStroke.has_value() ? std::make_unique<SkPaint>(*fCurrentStroke) : nullptr, |
| 511 |
std::make_unique<SkGlyphID[] >(ri.glyphCount), |
| 512 |
std::make_unique<SkPoint[] >(ri.glyphCount), |
| 513 |
std::make_unique<PositionAdjustment[]>(ri.glyphCount), |
| 514 |
ri.glyphCount, |
| 515 |
ri.fAdvance, |
| 516 |
}); |
| 517 |
|
| 518 |
// Ensure sufficient space to temporarily fetch cluster information. |
| 519 |
fShapeClusterBuffer.resize(std::max(fShapeClusterBuffer.size(), ri.glyphCount)); |
| 520 |
|
| 521 |
return { |
| 522 |
fRuns.back().glyphs.get(), |
| 523 |
fRuns.back().glyphPos.get(), |
| 524 |
nullptr, |
| 525 |
fShapeClusterBuffer.data(), |
| 526 |
fChunkAdvance, |
| 527 |
}; |
| 528 |
} |
| 529 |
|
| 530 |
void SkSVGTextContext::commitRunBuffer(const RunInfo& ri) { |
| 531 |
const auto& current_run = fRuns.back(); |
| 532 |
|
| 533 |
// stash position adjustments |
| 534 |
for (size_t i = 0; i < ri.glyphCount; ++i) { |
| 535 |
const auto utf8_index = fShapeClusterBuffer[i]; |
| 536 |
current_run.glyhPosAdjust[i] = fShapeBuffer.fUtf8PosAdjust[SkToInt(utf8_index)]; |
| 537 |
} |
| 538 |
|
| 539 |
fChunkAdvance += ri.fAdvance; |
| 540 |
} |
| 541 |
|
| 542 |
void SkSVGTextContext::commitLine() { |
| 543 |
if (!fShapeBuffer.fUtf8PosAdjust.empty()) { |
| 544 |
// Offset adjustments are cumulative - only advance the current chunk with the last value. |
| 545 |
fChunkAdvance += fShapeBuffer.fUtf8PosAdjust.back().offset; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
void SkSVGTextFragment::renderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx, |
| 550 |
SkSVGXmlSpace xs) const { |
| 551 |
// N.B.: unlike regular elements, text fragments do not establish a new OBB scope -- they |
| 552 |
// always defer to the root <text> element for OBB resolution. |
| 553 |
SkSVGRenderContext localContext(ctx); |
| 554 |
|
| 555 |
if (this->onPrepareToRender(&localContext)) { |
| 556 |
this->onShapeText(localContext, tctx, xs); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
SkPath SkSVGTextFragment::onAsPath(const SkSVGRenderContext&) const { |
| 561 |
// TODO |
| 562 |
return SkPath(); |
| 563 |
} |
| 564 |
|
| 565 |
void SkSVGTextContainer::appendChild(sk_sp<SkSVGNode> child) { |
| 566 |
// Only allow text content child nodes. |
| 567 |
switch (child->tag()) { |
| 568 |
case SkSVGTag::kTextLiteral: |
| 569 |
case SkSVGTag::kTextPath: |
| 570 |
case SkSVGTag::kTSpan: |
| 571 |
fChildren.push_back( |
| 572 |
sk_sp<SkSVGTextFragment>(static_cast<SkSVGTextFragment*>(child.release()))); |
| 573 |
break; |
| 574 |
default: |
| 575 |
break; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
void SkSVGTextContainer::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx, |
| 580 |
SkSVGXmlSpace) const { |
| 581 |
SkASSERT(tctx); |
| 582 |
|
| 583 |
const SkSVGTextContext::ScopedPosResolver resolver(*this, ctx.lengthContext(), tctx); |
| 584 |
|
| 585 |
for (const auto& frag : fChildren) { |
| 586 |
// Containers always override xml:space with the local value. |
| 587 |
frag->renderText(ctx, tctx, this->getXmlSpace()); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
// https://www.w3.org/TR/SVG11/text.html#WhiteSpace |
| 592 |
template <> |
| 593 |
bool SkSVGAttributeParser::parse(SkSVGXmlSpace* xs) { |
| 594 |
static constexpr std::tuple<const char*, SkSVGXmlSpace> gXmlSpaceMap[] = { |
| 595 |
{"default" , SkSVGXmlSpace::kDefault }, |
| 596 |
{"preserve", SkSVGXmlSpace::kPreserve}, |
| 597 |
}; |
| 598 |
|
| 599 |
return this->parseEnumMap(gXmlSpaceMap, xs) && this->parseEOSToken(); |
| 600 |
} |
| 601 |
|
| 602 |
bool SkSVGTextContainer::parseAndSetAttribute(const char* name, const char* value) { |
| 603 |
return INHERITED::parseAndSetAttribute(name, value) || |
| 604 |
this->setX(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("x", name, value)) || |
| 605 |
this->setY(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("y", name, value)) || |
| 606 |
this->setDx(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("dx", name, value)) || |
| 607 |
this->setDy(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("dy", name, value)) || |
| 608 |
this->setRotate(SkSVGAttributeParser::parse<std::vector<SkSVGNumberType>>("rotate", |
| 609 |
name, |
| 610 |
value)) || |
| 611 |
this->setXmlSpace(SkSVGAttributeParser::parse<SkSVGXmlSpace>("xml:space", name, value)); |
| 612 |
} |
| 613 |
|
| 614 |
void SkSVGTextLiteral::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx, |
| 615 |
SkSVGXmlSpace xs) const { |
| 616 |
SkASSERT(tctx); |
| 617 |
|
| 618 |
tctx->shapeFragment(this->getText(), ctx, xs); |
| 619 |
} |
| 620 |
|
| 621 |
void SkSVGText::onRender(const SkSVGRenderContext& ctx) const { |
| 622 |
const SkSVGTextContext::ShapedTextCallback render_text = [](const SkSVGRenderContext& ctx, |
| 623 |
const sk_sp<SkTextBlob>& blob, |
| 624 |
const SkPaint* fill, |
| 625 |
const SkPaint* stroke) { |
| 626 |
if (fill) { |
| 627 |
ctx.canvas()->drawTextBlob(blob, 0, 0, *fill); |
| 628 |
} |
| 629 |
if (stroke) { |
| 630 |
ctx.canvas()->drawTextBlob(blob, 0, 0, *stroke); |
| 631 |
} |
| 632 |
}; |
| 633 |
|
| 634 |
// Root <text> nodes establish a text layout context. |
| 635 |
SkSVGTextContext tctx(ctx, render_text); |
| 636 |
|
| 637 |
this->onShapeText(ctx, &tctx, this->getXmlSpace()); |
| 638 |
} |
| 639 |
|
| 640 |
SkRect SkSVGText::onTransformableObjectBoundingBox(const SkSVGRenderContext& ctx) const { |
| 641 |
SkRect bounds = SkRect::MakeEmpty(); |
| 642 |
|
| 643 |
const SkSVGTextContext::ShapedTextCallback compute_bounds = |
| 644 |
[&bounds](const SkSVGRenderContext& ctx, const sk_sp<SkTextBlob>& blob, const SkPaint*, |
| 645 |
const SkPaint*) { |
| 646 |
if (!blob) { |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
AutoSTArray<64, SkRect> glyphBounds; |
| 651 |
|
| 652 |
for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) { |
| 653 |
const auto nglyphs = it.glyphCount(); |
| 654 |
glyphBounds.reset(SkToInt(nglyphs)); |
| 655 |
it.font().getBounds({it.glyphs(), nglyphs}, {glyphBounds.get(), nglyphs}, nullptr); |
| 656 |
|
| 657 |
SkASSERT(it.positioning() == SkTextBlobRunIterator::kRSXform_Positioning); |
| 658 |
SkMatrix m; |
| 659 |
for (uint32_t i = 0; i < it.glyphCount(); ++i) { |
| 660 |
m.setRSXform(it.xforms()[i]); |
| 661 |
bounds.join(m.mapRect(glyphBounds[i])); |
| 662 |
} |
| 663 |
} |
| 664 |
}; |
| 665 |
|
| 666 |
{ |
| 667 |
SkSVGTextContext tctx(ctx, compute_bounds); |
| 668 |
this->onShapeText(ctx, &tctx, this->getXmlSpace()); |
| 669 |
} |
| 670 |
|
| 671 |
return bounds; |
| 672 |
} |
| 673 |
|
| 674 |
SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const { |
| 675 |
SkPathBuilder builder; |
| 676 |
|
| 677 |
const SkSVGTextContext::ShapedTextCallback as_path = |
| 678 |
[&builder](const SkSVGRenderContext& ctx, const sk_sp<SkTextBlob>& blob, const SkPaint*, |
| 679 |
const SkPaint*) { |
| 680 |
if (!blob) { |
| 681 |
return; |
| 682 |
} |
| 683 |
|
| 684 |
for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) { |
| 685 |
struct GetPathsCtx { |
| 686 |
SkPathBuilder& builder; |
| 687 |
const SkRSXform* xform; |
| 688 |
} get_paths_ctx {builder, it.xforms()}; |
| 689 |
|
| 690 |
it.font().getPaths({it.glyphs(), it.glyphCount()}, [](const SkPath* path, |
| 691 |
const SkMatrix& matrix, |
| 692 |
void* raw_ctx) { |
| 693 |
auto* get_paths_ctx = static_cast<GetPathsCtx*>(raw_ctx); |
| 694 |
const auto& glyph_rsx = *get_paths_ctx->xform++; |
| 695 |
|
| 696 |
if (!path) { |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
SkMatrix glyph_matrix; |
| 701 |
glyph_matrix.setRSXform(glyph_rsx); |
| 702 |
glyph_matrix.preConcat(matrix); |
| 703 |
|
| 704 |
get_paths_ctx->builder.addPath(path->makeTransform(glyph_matrix)); |
| 705 |
}, &get_paths_ctx); |
| 706 |
} |
| 707 |
}; |
| 708 |
|
| 709 |
{ |
| 710 |
SkSVGTextContext tctx(ctx, as_path); |
| 711 |
this->onShapeText(ctx, &tctx, this->getXmlSpace()); |
| 712 |
} |
| 713 |
|
| 714 |
return this->mapToParent(builder.detach()); |
| 715 |
} |
| 716 |
|
| 717 |
void SkSVGTextPath::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* parent_tctx, |
| 718 |
SkSVGXmlSpace xs) const { |
| 719 |
SkASSERT(parent_tctx); |
| 720 |
|
| 721 |
// textPath nodes establish a new text layout context. |
| 722 |
SkSVGTextContext tctx(ctx, parent_tctx->getCallback(), this); |
| 723 |
|
| 724 |
this->INHERITED::onShapeText(ctx, &tctx, xs); |
| 725 |
} |
| 726 |
|
| 727 |
bool SkSVGTextPath::parseAndSetAttribute(const char* name, const char* value) { |
| 728 |
return INHERITED::parseAndSetAttribute(name, value) || |
| 729 |
this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", name, value)) || |
| 730 |
this->setStartOffset(SkSVGAttributeParser::parse<SkSVGLength>("startOffset", name, value)); |
| 731 |
} |