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 | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 13 | #include "include/core/SkFont.h" |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 14 | #include "include/core/SkFontMgr.h" |
Tyler Freeman | e9663db | 2020-04-14 14:37:13 -0700 | [diff] [blame] | 15 | #include "include/core/SkFontStyle.h" |
| 16 | #include "include/core/SkString.h" |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 17 | #include "modules/skshaper/include/SkShaper.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 18 | #include "modules/svg/include/SkSVGRenderContext.h" |
| 19 | #include "modules/svg/include/SkSVGValue.h" |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 20 | #include "modules/svg/src/SkSVGTextPriv.h" |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 21 | #include "src/utils/SkUTF.h" |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 22 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 23 | namespace { |
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 | static SkFont ResolveFont(const SkSVGRenderContext& ctx) { |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 26 | auto weight = [](const SkSVGFontWeight& w) { |
| 27 | switch (w.type()) { |
| 28 | case SkSVGFontWeight::Type::k100: return SkFontStyle::kThin_Weight; |
| 29 | case SkSVGFontWeight::Type::k200: return SkFontStyle::kExtraLight_Weight; |
| 30 | case SkSVGFontWeight::Type::k300: return SkFontStyle::kLight_Weight; |
| 31 | case SkSVGFontWeight::Type::k400: return SkFontStyle::kNormal_Weight; |
| 32 | case SkSVGFontWeight::Type::k500: return SkFontStyle::kMedium_Weight; |
| 33 | case SkSVGFontWeight::Type::k600: return SkFontStyle::kSemiBold_Weight; |
| 34 | case SkSVGFontWeight::Type::k700: return SkFontStyle::kBold_Weight; |
| 35 | case SkSVGFontWeight::Type::k800: return SkFontStyle::kExtraBold_Weight; |
| 36 | case SkSVGFontWeight::Type::k900: return SkFontStyle::kBlack_Weight; |
| 37 | case SkSVGFontWeight::Type::kNormal: return SkFontStyle::kNormal_Weight; |
| 38 | case SkSVGFontWeight::Type::kBold: return SkFontStyle::kBold_Weight; |
| 39 | case SkSVGFontWeight::Type::kBolder: return SkFontStyle::kExtraBold_Weight; |
| 40 | case SkSVGFontWeight::Type::kLighter: return SkFontStyle::kLight_Weight; |
| 41 | case SkSVGFontWeight::Type::kInherit: { |
| 42 | SkASSERT(false); |
| 43 | return SkFontStyle::kNormal_Weight; |
| 44 | } |
| 45 | } |
| 46 | SkUNREACHABLE; |
| 47 | }; |
| 48 | |
| 49 | auto slant = [](const SkSVGFontStyle& s) { |
| 50 | switch (s.type()) { |
| 51 | case SkSVGFontStyle::Type::kNormal: return SkFontStyle::kUpright_Slant; |
| 52 | case SkSVGFontStyle::Type::kItalic: return SkFontStyle::kItalic_Slant; |
| 53 | case SkSVGFontStyle::Type::kOblique: return SkFontStyle::kOblique_Slant; |
| 54 | case SkSVGFontStyle::Type::kInherit: { |
| 55 | SkASSERT(false); |
| 56 | return SkFontStyle::kUpright_Slant; |
| 57 | } |
| 58 | } |
| 59 | SkUNREACHABLE; |
| 60 | }; |
| 61 | |
| 62 | const auto& family = ctx.presentationContext().fInherited.fFontFamily->family(); |
| 63 | const SkFontStyle style(weight(*ctx.presentationContext().fInherited.fFontWeight), |
| 64 | SkFontStyle::kNormal_Width, |
| 65 | slant(*ctx.presentationContext().fInherited.fFontStyle)); |
| 66 | |
| 67 | const auto size = |
| 68 | ctx.lengthContext().resolve(ctx.presentationContext().fInherited.fFontSize->size(), |
| 69 | SkSVGLengthContext::LengthType::kVertical); |
| 70 | |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 71 | // TODO: we likely want matchFamilyStyle here, but switching away from legacyMakeTypeface |
| 72 | // changes all the results when using the default fontmgr. |
| 73 | auto tf = ctx.fontMgr()->legacyMakeTypeface(family.c_str(), style); |
| 74 | |
| 75 | SkFont font(std::move(tf), size); |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 76 | font.setHinting(SkFontHinting::kNone); |
| 77 | font.setSubpixel(true); |
| 78 | font.setLinearMetrics(true); |
| 79 | font.setBaselineSnap(false); |
| 80 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 81 | |
| 82 | return font; |
| 83 | } |
| 84 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 85 | static std::vector<float> ResolveLengths(const SkSVGLengthContext& lctx, |
| 86 | const std::vector<SkSVGLength>& lengths, |
| 87 | SkSVGLengthContext::LengthType lt) { |
| 88 | std::vector<float> resolved; |
| 89 | resolved.reserve(lengths.size()); |
| 90 | |
| 91 | for (const auto& l : lengths) { |
| 92 | resolved.push_back(lctx.resolve(l, lt)); |
| 93 | } |
| 94 | |
| 95 | return resolved; |
| 96 | } |
| 97 | |
| 98 | static float ComputeAlignmentFactor(const SkSVGPresentationContext& pctx) { |
| 99 | switch (pctx.fInherited.fTextAnchor->type()) { |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 100 | case SkSVGTextAnchor::Type::kStart : return 0.0f; |
| 101 | case SkSVGTextAnchor::Type::kMiddle: return -0.5f; |
| 102 | case SkSVGTextAnchor::Type::kEnd : return -1.0f; |
| 103 | case SkSVGTextAnchor::Type::kInherit: |
| 104 | SkASSERT(false); |
| 105 | return 0.0f; |
| 106 | } |
| 107 | SkUNREACHABLE; |
| 108 | } |
| 109 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 110 | } // namespace |
| 111 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 112 | SkSVGTextContext::ScopedPosResolver::ScopedPosResolver(const SkSVGTextContainer& txt, |
| 113 | const SkSVGLengthContext& lctx, |
| 114 | SkSVGTextContext* tctx, |
| 115 | size_t charIndexOffset) |
| 116 | : fTextContext(tctx) |
| 117 | , fParent(tctx->fPosResolver) |
| 118 | , fCharIndexOffset(charIndexOffset) |
| 119 | , fX(ResolveLengths(lctx, txt.getX(), SkSVGLengthContext::LengthType::kHorizontal)) |
| 120 | , fY(ResolveLengths(lctx, txt.getY(), SkSVGLengthContext::LengthType::kVertical)) |
| 121 | { |
| 122 | fTextContext->fPosResolver = this; |
| 123 | } |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 124 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 125 | SkSVGTextContext::ScopedPosResolver::ScopedPosResolver(const SkSVGTextContainer& txt, |
| 126 | const SkSVGLengthContext& lctx, |
| 127 | SkSVGTextContext* tctx) |
| 128 | : ScopedPosResolver(txt, lctx, tctx, tctx->fCurrentCharIndex) {} |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 129 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 130 | SkSVGTextContext::ScopedPosResolver::~ScopedPosResolver() { |
| 131 | fTextContext->fPosResolver = fParent; |
| 132 | } |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 133 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 134 | SkSVGTextContext::PosAttrs SkSVGTextContext::ScopedPosResolver::resolve(size_t charIndex) const { |
| 135 | PosAttrs attrs; |
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 | if (charIndex < fLastPosIndex) { |
| 138 | SkASSERT(charIndex >= fCharIndexOffset); |
| 139 | const auto localCharIndex = charIndex - fCharIndexOffset; |
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 | const auto hasAllLocal = localCharIndex < fX.size() && |
| 142 | localCharIndex < fY.size(); |
| 143 | if (!hasAllLocal && fParent) { |
| 144 | attrs = fParent->resolve(charIndex); |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 147 | if (localCharIndex < fX.size()) { |
| 148 | attrs[PosAttrs::kX] = fX[localCharIndex]; |
| 149 | } |
| 150 | if (localCharIndex < fY.size()) { |
| 151 | attrs[PosAttrs::kY] = fY[localCharIndex]; |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 154 | if (!attrs.hasAny()) { |
| 155 | // Once we stop producing explicit position data, there is no reason to |
| 156 | // continue trying for higher indices. We can suppress future lookups. |
| 157 | fLastPosIndex = charIndex; |
| 158 | } |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 161 | return attrs; |
| 162 | } |
| 163 | |
| 164 | SkSVGTextContext::SkSVGTextContext(const SkSVGPresentationContext& pctx, sk_sp<SkFontMgr> fmgr) |
| 165 | : fShaper(SkShaper::Make(std::move(fmgr))) |
| 166 | , fChunkPos{ 0, 0 } |
| 167 | , fChunkAlignmentFactor(ComputeAlignmentFactor(pctx)) |
| 168 | {} |
| 169 | |
| 170 | void SkSVGTextContext::appendFragment(const SkString& txt, const SkSVGRenderContext& ctx, |
| 171 | SkSVGXmlSpace xs) { |
| 172 | // https://www.w3.org/TR/SVG11/text.html#WhiteSpace |
| 173 | // https://www.w3.org/TR/2008/REC-xml-20081126/#NT-S |
| 174 | auto filterWSDefault = [this](SkUnichar ch) -> SkUnichar { |
| 175 | // Remove all newline chars. |
| 176 | if (ch == '\n') { |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | // Convert tab chars to space. |
| 181 | if (ch == '\t') { |
| 182 | ch = ' '; |
| 183 | } |
| 184 | |
| 185 | // Consolidate contiguous space chars and strip leading spaces (fPrevCharSpace |
| 186 | // starts off as true). |
| 187 | if (fPrevCharSpace && ch == ' ') { |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | // TODO: Strip trailing WS? Doing this across chunks would require another buffering |
| 192 | // layer. In general, trailing WS should have no rendering side effects. Skipping |
| 193 | // for now. |
| 194 | return ch; |
| 195 | }; |
| 196 | auto filterWSPreserve = [](SkUnichar ch) -> SkUnichar { |
| 197 | // Convert newline and tab chars to space. |
| 198 | if (ch == '\n' || ch == '\t') { |
| 199 | ch = ' '; |
| 200 | } |
| 201 | return ch; |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 202 | }; |
| 203 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 204 | // Stash paints for access from SkShaper callbacks. |
| 205 | fCurrentFill = ctx.fillPaint(); |
| 206 | fCurrentStroke = ctx.strokePaint(); |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 207 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 208 | const auto font = ResolveFont(ctx); |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 209 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 210 | SkSTArray<128, char, true> filtered; |
| 211 | filtered.reserve_back(SkToInt(txt.size())); |
| 212 | |
| 213 | auto shapePending = [&filtered, &font, this]() { |
| 214 | // TODO: directionality hints? |
| 215 | const auto LTR = true; |
| 216 | // Initiate shaping: this will generate a series of runs via callbacks. |
| 217 | fShaper->shape(filtered.data(), filtered.size(), font, LTR, SK_ScalarMax, this); |
| 218 | filtered.reset(); |
| 219 | }; |
| 220 | |
| 221 | const char* ch_ptr = txt.c_str(); |
| 222 | const char* ch_end = ch_ptr + txt.size(); |
| 223 | |
| 224 | while (ch_ptr < ch_end) { |
| 225 | auto ch = SkUTF::NextUTF8(&ch_ptr, ch_end); |
| 226 | ch = (xs == SkSVGXmlSpace::kDefault) |
| 227 | ? filterWSDefault(ch) |
| 228 | : filterWSPreserve(ch); |
| 229 | |
| 230 | if (ch < 0) { |
| 231 | // invalid utf or char filtered out |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | SkASSERT(fPosResolver); |
| 236 | const auto pos = fPosResolver->resolve(fCurrentCharIndex++); |
| 237 | |
| 238 | // Absolute position adjustments define a new chunk. |
| 239 | // (https://www.w3.org/TR/SVG11/text.html#TextLayoutIntroduction) |
| 240 | if (pos.has(PosAttrs::kX) || pos.has(PosAttrs::kY)) { |
| 241 | shapePending(); |
| 242 | this->flushChunk(ctx); |
| 243 | |
| 244 | // New chunk position. |
| 245 | if (pos.has(PosAttrs::kX)) { |
| 246 | fChunkPos.fX = pos[PosAttrs::kX]; |
| 247 | } |
| 248 | if (pos.has(PosAttrs::kY)) { |
| 249 | fChunkPos.fY = pos[PosAttrs::kY]; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | char utf8_buf[SkUTF::kMaxBytesInUTF8Sequence]; |
| 254 | filtered.push_back_n(SkToInt(SkUTF::ToUTF8(ch, utf8_buf)), utf8_buf); |
| 255 | |
| 256 | fPrevCharSpace = (ch == ' '); |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 257 | } |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 258 | |
| 259 | // Note: at this point we have shaped and buffered the current fragment The active |
| 260 | // text chunk continues until an explicit or implicit flush. |
| 261 | shapePending(); |
| 262 | } |
| 263 | |
| 264 | void SkSVGTextContext::flushChunk(const SkSVGRenderContext& ctx) { |
| 265 | // The final rendering offset is determined by cumulative chunk advances and alignment. |
| 266 | const auto pos = fChunkPos + fChunkAdvance * fChunkAlignmentFactor; |
| 267 | |
| 268 | SkTextBlobBuilder blobBuilder; |
| 269 | |
| 270 | for (const auto& run : fRuns) { |
| 271 | const auto& buf = blobBuilder.allocRunPos(run.font, SkToInt(run.glyphCount)); |
| 272 | std::copy(run.glyphs .get(), run.glyphs .get() + run.glyphCount, buf.glyphs); |
| 273 | std::copy(run.glyphPos.get(), run.glyphPos.get() + run.glyphCount, buf.points()); |
| 274 | |
| 275 | // Technically, blobs with compatible paints could be merged -- |
| 276 | // but likely not worth the effort. |
| 277 | const auto blob = blobBuilder.make(); |
| 278 | if (run.fillPaint) { |
| 279 | ctx.canvas()->drawTextBlob(blob, pos.fX, pos.fY, *run.fillPaint); |
| 280 | } |
| 281 | if (run.strokePaint) { |
| 282 | ctx.canvas()->drawTextBlob(blob, pos.fX, pos.fY, *run.strokePaint); |
| 283 | } |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 284 | } |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 285 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 286 | fChunkPos += fChunkAdvance; |
| 287 | fChunkAdvance = {0,0}; |
| 288 | fChunkAlignmentFactor = ComputeAlignmentFactor(ctx.presentationContext()); |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 289 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 290 | fRuns.clear(); |
| 291 | } |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 292 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 293 | SkShaper::RunHandler::Buffer SkSVGTextContext::runBuffer(const RunInfo& ri) { |
| 294 | SkASSERT(ri.glyphCount); |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 295 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 296 | fRuns.push_back({ |
| 297 | ri.fFont, |
| 298 | fCurrentFill ? std::make_unique<SkPaint>(*fCurrentFill) : nullptr, |
| 299 | fCurrentStroke ? std::make_unique<SkPaint>(*fCurrentStroke) : nullptr, |
| 300 | std::make_unique<SkGlyphID[]>(ri.glyphCount), |
| 301 | std::make_unique<SkPoint[] >(ri.glyphCount), |
| 302 | ri.glyphCount, |
| 303 | ri.fAdvance, |
| 304 | }); |
| 305 | |
| 306 | return { |
| 307 | fRuns.back().glyphs.get(), |
| 308 | fRuns.back().glyphPos.get(), |
| 309 | nullptr, |
| 310 | nullptr, |
| 311 | fChunkAdvance, |
| 312 | }; |
| 313 | } |
| 314 | |
| 315 | void SkSVGTextContext::commitRunBuffer(const RunInfo& ri) { |
| 316 | fChunkAdvance += ri.fAdvance; |
| 317 | } |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 318 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 319 | void SkSVGTextFragment::renderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx, |
| 320 | SkSVGXmlSpace xs) const { |
| 321 | SkSVGRenderContext localContext(ctx, this); |
| 322 | |
| 323 | if (this->onPrepareToRender(&localContext)) { |
| 324 | this->onRenderText(localContext, tctx, xs); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | SkPath SkSVGTextFragment::onAsPath(const SkSVGRenderContext&) const { |
| 329 | // TODO |
| 330 | return SkPath(); |
| 331 | } |
| 332 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 333 | void SkSVGTextContainer::appendChild(sk_sp<SkSVGNode> child) { |
| 334 | // Only allow text nodes. |
| 335 | switch (child->tag()) { |
| 336 | case SkSVGTag::kText: |
| 337 | case SkSVGTag::kTextLiteral: |
| 338 | case SkSVGTag::kTSpan: |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 339 | fChildren.push_back( |
| 340 | sk_sp<SkSVGTextFragment>(static_cast<SkSVGTextFragment*>(child.release()))); |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 341 | break; |
| 342 | default: |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 347 | void SkSVGTextContainer::onRenderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx, |
| 348 | SkSVGXmlSpace) const { |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 349 | const SkSVGTextContext::ScopedPosResolver resolver(*this, ctx.lengthContext(), tctx); |
| 350 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 351 | for (const auto& frag : fChildren) { |
| 352 | // Containers always override xml:space with the local value. |
| 353 | frag->renderText(ctx, tctx, this->getXmlSpace()); |
| 354 | } |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // https://www.w3.org/TR/SVG11/text.html#WhiteSpace |
| 358 | template <> |
| 359 | bool SkSVGAttributeParser::parse(SkSVGXmlSpace* xs) { |
| 360 | static constexpr std::tuple<const char*, SkSVGXmlSpace> gXmlSpaceMap[] = { |
| 361 | {"default" , SkSVGXmlSpace::kDefault }, |
| 362 | {"preserve", SkSVGXmlSpace::kPreserve}, |
| 363 | }; |
| 364 | |
| 365 | return this->parseEnumMap(gXmlSpaceMap, xs) && this->parseEOSToken(); |
| 366 | } |
| 367 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 368 | bool SkSVGTextContainer::parseAndSetAttribute(const char* name, const char* value) { |
| 369 | return INHERITED::parseAndSetAttribute(name, value) || |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 370 | this->setX(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("x", name, value)) || |
| 371 | this->setY(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("y", name, value)) || |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 372 | this->setXmlSpace(SkSVGAttributeParser::parse<SkSVGXmlSpace>("xml:space", name, value)); |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 373 | } |
| 374 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 375 | void SkSVGTextContainer::onRender(const SkSVGRenderContext& ctx) const { |
| 376 | // Root text nodes establish a new text layout context. |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame^] | 377 | SkSVGTextContext tctx(ctx.presentationContext(), ctx.fontMgr()); |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 378 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 379 | this->onRenderText(ctx, &tctx, this->getXmlSpace()); |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 380 | |
Florin Malita | 7dc984a | 2020-12-08 11:37:15 -0500 | [diff] [blame] | 381 | tctx.flushChunk(ctx); |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 384 | void SkSVGTextLiteral::onRenderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx, |
| 385 | SkSVGXmlSpace xs) const { |
| 386 | SkASSERT(tctx); |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 387 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 388 | tctx->appendFragment(this->getText(), ctx, xs); |
Xavier Phan | e29cdaf | 2020-03-26 16:15:14 +0000 | [diff] [blame] | 389 | } |