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