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