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