blob: f81eb7c3da16026242e3c35887d695e12b9ebafe [file] [log] [blame]
Xavier Phane29cdaf2020-03-26 16:15:14 +00001/*
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 Malitab3418102020-10-15 18:10:29 -04008#include "modules/svg/include/SkSVGText.h"
Xavier Phane29cdaf2020-03-26 16:15:14 +00009
Florin Malitadec78022020-12-17 16:36:54 -050010#include <limits>
Florin Malita7dc984a2020-12-08 11:37:15 -050011
Xavier Phane29cdaf2020-03-26 16:15:14 +000012#include "include/core/SkCanvas.h"
Florin Malitafc0ea0a2021-01-12 13:27:01 -050013#include "include/core/SkContourMeasure.h"
Florin Malita512ff752020-12-06 11:50:52 -050014#include "include/core/SkFont.h"
Florin Malita39fe8c82020-10-20 10:43:03 -040015#include "include/core/SkFontMgr.h"
Tyler Freemane9663db2020-04-14 14:37:13 -070016#include "include/core/SkFontStyle.h"
Florin Malitae5a21712020-12-30 11:08:53 -050017#include "include/core/SkRSXform.h"
Tyler Freemane9663db2020-04-14 14:37:13 -070018#include "include/core/SkString.h"
Florin Malita7dc984a2020-12-08 11:37:15 -050019#include "modules/skshaper/include/SkShaper.h"
Florin Malitab3418102020-10-15 18:10:29 -040020#include "modules/svg/include/SkSVGRenderContext.h"
21#include "modules/svg/include/SkSVGValue.h"
Florin Malitadec78022020-12-17 16:36:54 -050022#include "modules/svg/src/SkSVGTextPriv.h"
Florin Malita9c1f1be2020-12-09 13:02:50 -050023#include "src/utils/SkUTF.h"
Xavier Phane29cdaf2020-03-26 16:15:14 +000024
Florin Malita512ff752020-12-06 11:50:52 -050025namespace {
Xavier Phane29cdaf2020-03-26 16:15:14 +000026
Florin Malita512ff752020-12-06 11:50:52 -050027static SkFont ResolveFont(const SkSVGRenderContext& ctx) {
Florin Malita39fe8c82020-10-20 10:43:03 -040028 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 Malita7006e152020-11-10 15:24:59 -050073 // 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 Malita39fe8c82020-10-20 10:43:03 -040078 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 Malitadec78022020-12-17 16:36:54 -050087static 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
100static float ComputeAlignmentFactor(const SkSVGPresentationContext& pctx) {
101 switch (pctx.fInherited.fTextAnchor->type()) {
Florin Malita7dc984a2020-12-08 11:37:15 -0500102 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 Malita512ff752020-12-06 11:50:52 -0500112} // namespace
113
Florin Malitadec78022020-12-17 16:36:54 -0500114SkSVGTextContext::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 Malita735ac972020-12-22 11:23:32 -0500123 , fDx(ResolveLengths(lctx, txt.getDx(), SkSVGLengthContext::LengthType::kHorizontal))
124 , fDy(ResolveLengths(lctx, txt.getDy(), SkSVGLengthContext::LengthType::kVertical))
Florin Malita2d059fc2021-01-05 11:53:15 -0500125 , fRotate(txt.getRotate())
Florin Malitadec78022020-12-17 16:36:54 -0500126{
127 fTextContext->fPosResolver = this;
128}
Florin Malita7dc984a2020-12-08 11:37:15 -0500129
Florin Malitadec78022020-12-17 16:36:54 -0500130SkSVGTextContext::ScopedPosResolver::ScopedPosResolver(const SkSVGTextContainer& txt,
131 const SkSVGLengthContext& lctx,
132 SkSVGTextContext* tctx)
133 : ScopedPosResolver(txt, lctx, tctx, tctx->fCurrentCharIndex) {}
Florin Malita9c1f1be2020-12-09 13:02:50 -0500134
Florin Malitadec78022020-12-17 16:36:54 -0500135SkSVGTextContext::ScopedPosResolver::~ScopedPosResolver() {
136 fTextContext->fPosResolver = fParent;
137}
Florin Malita9c1f1be2020-12-09 13:02:50 -0500138
Florin Malitadec78022020-12-17 16:36:54 -0500139SkSVGTextContext::PosAttrs SkSVGTextContext::ScopedPosResolver::resolve(size_t charIndex) const {
140 PosAttrs attrs;
Florin Malita9c1f1be2020-12-09 13:02:50 -0500141
Florin Malitadec78022020-12-17 16:36:54 -0500142 if (charIndex < fLastPosIndex) {
143 SkASSERT(charIndex >= fCharIndexOffset);
144 const auto localCharIndex = charIndex - fCharIndexOffset;
Florin Malita9c1f1be2020-12-09 13:02:50 -0500145
Florin Malitadec78022020-12-17 16:36:54 -0500146 const auto hasAllLocal = localCharIndex < fX.size() &&
Florin Malita735ac972020-12-22 11:23:32 -0500147 localCharIndex < fY.size() &&
148 localCharIndex < fDx.size() &&
Florin Malita2d059fc2021-01-05 11:53:15 -0500149 localCharIndex < fDy.size() &&
150 localCharIndex < fRotate.size();
Florin Malitadec78022020-12-17 16:36:54 -0500151 if (!hasAllLocal && fParent) {
152 attrs = fParent->resolve(charIndex);
Florin Malita9c1f1be2020-12-09 13:02:50 -0500153 }
154
Florin Malitadec78022020-12-17 16:36:54 -0500155 if (localCharIndex < fX.size()) {
156 attrs[PosAttrs::kX] = fX[localCharIndex];
157 }
158 if (localCharIndex < fY.size()) {
159 attrs[PosAttrs::kY] = fY[localCharIndex];
Florin Malita7dc984a2020-12-08 11:37:15 -0500160 }
Florin Malita735ac972020-12-22 11:23:32 -0500161 if (localCharIndex < fDx.size()) {
162 attrs[PosAttrs::kDx] = fDx[localCharIndex];
163 }
164 if (localCharIndex < fDy.size()) {
165 attrs[PosAttrs::kDy] = fDy[localCharIndex];
166 }
Florin Malita7dc984a2020-12-08 11:37:15 -0500167
Florin Malita2d059fc2021-01-05 11:53:15 -0500168 // 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 Malitadec78022020-12-17 16:36:54 -0500194 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 Malita7dc984a2020-12-08 11:37:15 -0500199 }
200
Florin Malitadec78022020-12-17 16:36:54 -0500201 return attrs;
202}
203
Florin Malita2d059fc2021-01-05 11:53:15 -0500204void SkSVGTextContext::ShapeBuffer::append(SkUnichar ch, PositionAdjustment pos) {
Florin Malita735ac972020-12-22 11:23:32 -0500205 // relative pos adjustments are cumulative
206 if (!fUtf8PosAdjust.empty()) {
Florin Malita2d059fc2021-01-05 11:53:15 -0500207 pos.offset += fUtf8PosAdjust.back().offset;
Florin Malita735ac972020-12-22 11:23:32 -0500208 }
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
216void 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 Malitac55c8c12021-01-25 10:37:22 -0500226SkSVGTextContext::SkSVGTextContext(const SkSVGRenderContext& ctx, const ShapedTextCallback& cb,
227 const SkSVGTextPath* tpath)
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500228 : fRenderContext(ctx)
Florin Malitac55c8c12021-01-25 10:37:22 -0500229 , fCallback(cb)
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500230 , fShaper(SkShaper::Make(ctx.fontMgr()))
231 , fChunkAlignmentFactor(ComputeAlignmentFactor(ctx.presentationContext()))
232{
233 if (tpath) {
234 fPathData = std::make_unique<PathData>(ctx, *tpath);
235
236 // https://www.w3.org/TR/SVG11/text.html#TextPathElementStartOffsetAttribute
237 auto resolve_offset = [this](const SkSVGLength& offset) {
238 if (offset.unit() != SkSVGLength::Unit::kPercentage) {
239 // "If a <length> other than a percentage is given, then the ‘startOffset’
240 // represents a distance along the path measured in the current user coordinate
241 // system."
242 return fRenderContext.lengthContext()
243 .resolve(offset, SkSVGLengthContext::LengthType::kHorizontal);
244 }
245
246 // "If a percentage is given, then the ‘startOffset’ represents a percentage distance
247 // along the entire path."
248 return offset.value() * fPathData->length() / 100;
249 };
250
251 // startOffset acts as an initial absolute position
252 fChunkPos.fX = resolve_offset(tpath->getStartOffset());
253 }
254}
255
256SkSVGTextContext::~SkSVGTextContext() {
257 this->flushChunk(fRenderContext);
258}
Florin Malitadec78022020-12-17 16:36:54 -0500259
Florin Malitac55c8c12021-01-25 10:37:22 -0500260void SkSVGTextContext::shapeFragment(const SkString& txt, const SkSVGRenderContext& ctx,
261 SkSVGXmlSpace xs) {
Florin Malitadec78022020-12-17 16:36:54 -0500262 // https://www.w3.org/TR/SVG11/text.html#WhiteSpace
263 // https://www.w3.org/TR/2008/REC-xml-20081126/#NT-S
264 auto filterWSDefault = [this](SkUnichar ch) -> SkUnichar {
265 // Remove all newline chars.
266 if (ch == '\n') {
267 return -1;
268 }
269
270 // Convert tab chars to space.
271 if (ch == '\t') {
272 ch = ' ';
273 }
274
275 // Consolidate contiguous space chars and strip leading spaces (fPrevCharSpace
276 // starts off as true).
277 if (fPrevCharSpace && ch == ' ') {
278 return -1;
279 }
280
281 // TODO: Strip trailing WS? Doing this across chunks would require another buffering
282 // layer. In general, trailing WS should have no rendering side effects. Skipping
283 // for now.
284 return ch;
285 };
286 auto filterWSPreserve = [](SkUnichar ch) -> SkUnichar {
287 // Convert newline and tab chars to space.
288 if (ch == '\n' || ch == '\t') {
289 ch = ' ';
290 }
291 return ch;
Florin Malita7dc984a2020-12-08 11:37:15 -0500292 };
293
Florin Malitadec78022020-12-17 16:36:54 -0500294 // Stash paints for access from SkShaper callbacks.
295 fCurrentFill = ctx.fillPaint();
296 fCurrentStroke = ctx.strokePaint();
Florin Malita7dc984a2020-12-08 11:37:15 -0500297
Florin Malitadec78022020-12-17 16:36:54 -0500298 const auto font = ResolveFont(ctx);
Florin Malita735ac972020-12-22 11:23:32 -0500299 fShapeBuffer.reserve(txt.size());
Florin Malitadec78022020-12-17 16:36:54 -0500300
301 const char* ch_ptr = txt.c_str();
302 const char* ch_end = ch_ptr + txt.size();
303
304 while (ch_ptr < ch_end) {
305 auto ch = SkUTF::NextUTF8(&ch_ptr, ch_end);
306 ch = (xs == SkSVGXmlSpace::kDefault)
307 ? filterWSDefault(ch)
308 : filterWSPreserve(ch);
309
310 if (ch < 0) {
311 // invalid utf or char filtered out
312 continue;
313 }
314
315 SkASSERT(fPosResolver);
316 const auto pos = fPosResolver->resolve(fCurrentCharIndex++);
317
318 // Absolute position adjustments define a new chunk.
319 // (https://www.w3.org/TR/SVG11/text.html#TextLayoutIntroduction)
320 if (pos.has(PosAttrs::kX) || pos.has(PosAttrs::kY)) {
Florin Malita735ac972020-12-22 11:23:32 -0500321 this->shapePendingBuffer(font);
Florin Malitadec78022020-12-17 16:36:54 -0500322 this->flushChunk(ctx);
323
324 // New chunk position.
325 if (pos.has(PosAttrs::kX)) {
326 fChunkPos.fX = pos[PosAttrs::kX];
327 }
328 if (pos.has(PosAttrs::kY)) {
329 fChunkPos.fY = pos[PosAttrs::kY];
330 }
331 }
332
Florin Malita735ac972020-12-22 11:23:32 -0500333 fShapeBuffer.append(ch, {
Florin Malita2d059fc2021-01-05 11:53:15 -0500334 {
335 pos.has(PosAttrs::kDx) ? pos[PosAttrs::kDx] : 0,
336 pos.has(PosAttrs::kDy) ? pos[PosAttrs::kDy] : 0,
337 },
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500338 pos.has(PosAttrs::kRotate) ? SkDegreesToRadians(pos[PosAttrs::kRotate]) : 0,
Florin Malita735ac972020-12-22 11:23:32 -0500339 });
Florin Malitadec78022020-12-17 16:36:54 -0500340
341 fPrevCharSpace = (ch == ' ');
Florin Malita7dc984a2020-12-08 11:37:15 -0500342 }
Florin Malitadec78022020-12-17 16:36:54 -0500343
Florin Malita735ac972020-12-22 11:23:32 -0500344 this->shapePendingBuffer(font);
345
346 // Note: at this point we have shaped and buffered RunRecs for the current fragment.
347 // The active text chunk continues until an explicit or implicit flush.
Florin Malitadec78022020-12-17 16:36:54 -0500348}
349
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500350SkSVGTextContext::PathData::PathData(const SkSVGRenderContext& ctx, const SkSVGTextPath& tpath)
351{
352 const auto ref = ctx.findNodeById(tpath.getHref().fIRI);
353 if (!ref) {
354 return;
355 }
Florin Malitadec78022020-12-17 16:36:54 -0500356
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500357 SkContourMeasureIter cmi(ref->asPath(ctx), false);
358 while (sk_sp<SkContourMeasure> contour = cmi.next()) {
359 fLength += contour->length();
360 fContours.push_back(std::move(contour));
361 }
362}
363
364SkMatrix SkSVGTextContext::PathData::getMatrixAt(float offset) const {
365 if (offset >= 0) {
366 for (const auto& contour : fContours) {
367 const auto contour_len = contour->length();
368 if (offset < contour_len) {
369 SkMatrix m;
370 return contour->getMatrix(offset, &m) ? m : SkMatrix::I();
371 }
372 offset -= contour_len;
373 }
374 }
375
376 // Quick & dirty way to "skip" rendering of glyphs off path.
377 return SkMatrix::Translate(std::numeric_limits<float>::infinity(),
378 std::numeric_limits<float>::infinity());
379}
380
381SkRSXform SkSVGTextContext::computeGlyphXform(SkGlyphID glyph, const SkFont& font,
382 const SkPoint& glyph_pos,
383 const PositionAdjustment& pos_adjust) const {
384 SkPoint pos = fChunkPos + glyph_pos + pos_adjust.offset + fChunkAdvance * fChunkAlignmentFactor;
385 if (!fPathData) {
386 return SkRSXform::MakeFromRadians(/*scale=*/ 1, pos_adjust.rotation, pos.fX, pos.fY, 0, 0);
387 }
388
389 // We're in a textPath scope, reposition the glyph on path.
390 // (https://www.w3.org/TR/SVG11/text.html#TextpathLayoutRules)
391
392 // Path positioning is based on the glyph center (horizontal component).
393 float glyph_width;
394 font.getWidths(&glyph, 1, &glyph_width);
395 auto path_offset = pos.fX + glyph_width * .5f;
396
397 // In addition to the path matrix, the final glyph matrix also includes:
398 //
399 // -- vertical position adjustment "dy" ("dx" is factored into path_offset)
400 // -- glyph origin adjustment (undoing the glyph center offset above)
401 // -- explicit rotation adjustment (composing with the path glyph rotation)
402 const auto m = fPathData->getMatrixAt(path_offset) *
403 SkMatrix::Translate(-glyph_width * .5f, pos_adjust.offset.fY) *
404 SkMatrix::RotateRad(pos_adjust.rotation);
405
406 return SkRSXform::Make(m.getScaleX(), m.getSkewY(), m.getTranslateX(), m.getTranslateY());
407}
408
409void SkSVGTextContext::flushChunk(const SkSVGRenderContext& ctx) {
Florin Malitadec78022020-12-17 16:36:54 -0500410 SkTextBlobBuilder blobBuilder;
411
412 for (const auto& run : fRuns) {
Florin Malitae5a21712020-12-30 11:08:53 -0500413 const auto& buf = blobBuilder.allocRunRSXform(run.font, SkToInt(run.glyphCount));
414 std::copy(run.glyphs.get(), run.glyphs.get() + run.glyphCount, buf.glyphs);
415 for (size_t i = 0; i < run.glyphCount; ++i) {
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500416 buf.xforms()[i] = this->computeGlyphXform(run.glyphs[i],
417 run.font,
418 run.glyphPos[i],
419 run.glyhPosAdjust[i]);
Florin Malitae5a21712020-12-30 11:08:53 -0500420 }
Florin Malitadec78022020-12-17 16:36:54 -0500421
Florin Malitac55c8c12021-01-25 10:37:22 -0500422 fCallback(ctx, blobBuilder.make(), run.fillPaint.get(), run.strokePaint.get());
Florin Malita7dc984a2020-12-08 11:37:15 -0500423 }
Florin Malita7dc984a2020-12-08 11:37:15 -0500424
Florin Malitadec78022020-12-17 16:36:54 -0500425 fChunkPos += fChunkAdvance;
426 fChunkAdvance = {0,0};
427 fChunkAlignmentFactor = ComputeAlignmentFactor(ctx.presentationContext());
Florin Malita7dc984a2020-12-08 11:37:15 -0500428
Florin Malitadec78022020-12-17 16:36:54 -0500429 fRuns.clear();
430}
Florin Malita7dc984a2020-12-08 11:37:15 -0500431
Florin Malitadec78022020-12-17 16:36:54 -0500432SkShaper::RunHandler::Buffer SkSVGTextContext::runBuffer(const RunInfo& ri) {
433 SkASSERT(ri.glyphCount);
Florin Malita9c1f1be2020-12-09 13:02:50 -0500434
Florin Malitadec78022020-12-17 16:36:54 -0500435 fRuns.push_back({
436 ri.fFont,
Florin Malitabde06cc2021-01-19 10:12:37 -0500437 fCurrentFill.isValid() ? std::make_unique<SkPaint>(*fCurrentFill) : nullptr,
438 fCurrentStroke.isValid() ? std::make_unique<SkPaint>(*fCurrentStroke) : nullptr,
Florin Malita736c9922021-01-05 10:51:33 -0500439 std::make_unique<SkGlyphID[] >(ri.glyphCount),
440 std::make_unique<SkPoint[] >(ri.glyphCount),
441 std::make_unique<PositionAdjustment[]>(ri.glyphCount),
Florin Malitadec78022020-12-17 16:36:54 -0500442 ri.glyphCount,
443 ri.fAdvance,
444 });
445
Florin Malita735ac972020-12-22 11:23:32 -0500446 // Ensure sufficient space to temporarily fetch cluster information.
447 fShapeClusterBuffer.resize(std::max(fShapeClusterBuffer.size(), ri.glyphCount));
448
Florin Malitadec78022020-12-17 16:36:54 -0500449 return {
450 fRuns.back().glyphs.get(),
451 fRuns.back().glyphPos.get(),
452 nullptr,
Florin Malita735ac972020-12-22 11:23:32 -0500453 fShapeClusterBuffer.data(),
Florin Malitadec78022020-12-17 16:36:54 -0500454 fChunkAdvance,
455 };
456}
457
458void SkSVGTextContext::commitRunBuffer(const RunInfo& ri) {
Florin Malita2d059fc2021-01-05 11:53:15 -0500459 const auto& current_run = fRuns.back();
460
Florin Malita736c9922021-01-05 10:51:33 -0500461 // stash position adjustments
Florin Malita735ac972020-12-22 11:23:32 -0500462 for (size_t i = 0; i < ri.glyphCount; ++i) {
463 const auto utf8_index = fShapeClusterBuffer[i];
Florin Malita736c9922021-01-05 10:51:33 -0500464 current_run.glyhPosAdjust[i] = fShapeBuffer.fUtf8PosAdjust[SkToInt(utf8_index)];
Florin Malita735ac972020-12-22 11:23:32 -0500465 }
466
Florin Malita736c9922021-01-05 10:51:33 -0500467 // Offset adjustments are cumulative - we only need to advance the current chunk
Florin Malita735ac972020-12-22 11:23:32 -0500468 // with the last value.
Florin Malita2d059fc2021-01-05 11:53:15 -0500469 fChunkAdvance += ri.fAdvance + fShapeBuffer.fUtf8PosAdjust.back().offset;
Florin Malitadec78022020-12-17 16:36:54 -0500470}
Florin Malita512ff752020-12-06 11:50:52 -0500471
Florin Malitaadc68892020-12-15 10:52:26 -0500472void SkSVGTextFragment::renderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
473 SkSVGXmlSpace xs) const {
474 SkSVGRenderContext localContext(ctx, this);
475
476 if (this->onPrepareToRender(&localContext)) {
Florin Malitac55c8c12021-01-25 10:37:22 -0500477 this->onShapeText(localContext, tctx, xs);
Florin Malitaadc68892020-12-15 10:52:26 -0500478 }
479}
480
481SkPath SkSVGTextFragment::onAsPath(const SkSVGRenderContext&) const {
482 // TODO
483 return SkPath();
484}
485
Florin Malita512ff752020-12-06 11:50:52 -0500486void SkSVGTextContainer::appendChild(sk_sp<SkSVGNode> child) {
Florin Malita302ea2e2021-01-24 12:04:17 -0500487 // Only allow text content child nodes.
Florin Malita512ff752020-12-06 11:50:52 -0500488 switch (child->tag()) {
Florin Malita512ff752020-12-06 11:50:52 -0500489 case SkSVGTag::kTextLiteral:
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500490 case SkSVGTag::kTextPath:
Florin Malita512ff752020-12-06 11:50:52 -0500491 case SkSVGTag::kTSpan:
Florin Malitaadc68892020-12-15 10:52:26 -0500492 fChildren.push_back(
493 sk_sp<SkSVGTextFragment>(static_cast<SkSVGTextFragment*>(child.release())));
Florin Malita512ff752020-12-06 11:50:52 -0500494 break;
495 default:
496 break;
497 }
498}
499
Florin Malitac55c8c12021-01-25 10:37:22 -0500500void SkSVGTextContainer::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
501 SkSVGXmlSpace) const {
Florin Malita302ea2e2021-01-24 12:04:17 -0500502 SkASSERT(tctx);
Florin Malita4ea46b72021-01-14 12:17:36 -0500503
Florin Malitadec78022020-12-17 16:36:54 -0500504 const SkSVGTextContext::ScopedPosResolver resolver(*this, ctx.lengthContext(), tctx);
505
Florin Malitaadc68892020-12-15 10:52:26 -0500506 for (const auto& frag : fChildren) {
507 // Containers always override xml:space with the local value.
508 frag->renderText(ctx, tctx, this->getXmlSpace());
509 }
Florin Malita9c1f1be2020-12-09 13:02:50 -0500510}
511
512// https://www.w3.org/TR/SVG11/text.html#WhiteSpace
513template <>
514bool SkSVGAttributeParser::parse(SkSVGXmlSpace* xs) {
515 static constexpr std::tuple<const char*, SkSVGXmlSpace> gXmlSpaceMap[] = {
516 {"default" , SkSVGXmlSpace::kDefault },
517 {"preserve", SkSVGXmlSpace::kPreserve},
518 };
519
520 return this->parseEnumMap(gXmlSpaceMap, xs) && this->parseEOSToken();
521}
522
Florin Malita512ff752020-12-06 11:50:52 -0500523bool SkSVGTextContainer::parseAndSetAttribute(const char* name, const char* value) {
524 return INHERITED::parseAndSetAttribute(name, value) ||
Florin Malitadec78022020-12-17 16:36:54 -0500525 this->setX(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("x", name, value)) ||
526 this->setY(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("y", name, value)) ||
Florin Malita735ac972020-12-22 11:23:32 -0500527 this->setDx(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("dx", name, value)) ||
528 this->setDy(SkSVGAttributeParser::parse<std::vector<SkSVGLength>>("dy", name, value)) ||
Florin Malita2d059fc2021-01-05 11:53:15 -0500529 this->setRotate(SkSVGAttributeParser::parse<std::vector<SkSVGNumberType>>("rotate",
530 name,
531 value)) ||
Florin Malita9c1f1be2020-12-09 13:02:50 -0500532 this->setXmlSpace(SkSVGAttributeParser::parse<SkSVGXmlSpace>("xml:space", name, value));
Florin Malita512ff752020-12-06 11:50:52 -0500533}
534
Florin Malitac55c8c12021-01-25 10:37:22 -0500535void SkSVGTextLiteral::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
536 SkSVGXmlSpace xs) const {
Florin Malitaadc68892020-12-15 10:52:26 -0500537 SkASSERT(tctx);
Florin Malita512ff752020-12-06 11:50:52 -0500538
Florin Malitac55c8c12021-01-25 10:37:22 -0500539 tctx->shapeFragment(this->getText(), ctx, xs);
Xavier Phane29cdaf2020-03-26 16:15:14 +0000540}
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500541
Florin Malita302ea2e2021-01-24 12:04:17 -0500542void SkSVGText::onRender(const SkSVGRenderContext& ctx) const {
Florin Malitac55c8c12021-01-25 10:37:22 -0500543 const SkSVGTextContext::ShapedTextCallback render_text = [](const SkSVGRenderContext& ctx,
544 const sk_sp<SkTextBlob>& blob,
545 const SkPaint* fill,
546 const SkPaint* stroke) {
547 if (fill) {
548 ctx.canvas()->drawTextBlob(blob, 0, 0, *fill);
549 }
550 if (stroke) {
551 ctx.canvas()->drawTextBlob(blob, 0, 0, *stroke);
552 }
553 };
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500554
Florin Malitac55c8c12021-01-25 10:37:22 -0500555 // Root <text> nodes establish a text layout context.
556 SkSVGTextContext tctx(ctx, render_text);
557
558 this->onShapeText(ctx, &tctx, this->getXmlSpace());
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500559}
560
Florin Malitac55c8c12021-01-25 10:37:22 -0500561void SkSVGTextPath::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* parent_tctx,
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500562 SkSVGXmlSpace xs) const {
Florin Malitac55c8c12021-01-25 10:37:22 -0500563 SkASSERT(parent_tctx);
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500564
Florin Malitac55c8c12021-01-25 10:37:22 -0500565 // textPath nodes establish a new text layout context.
566 SkSVGTextContext tctx(ctx, parent_tctx->getCallback(), this);
567
568 this->INHERITED::onShapeText(ctx, &tctx, xs);
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500569}
570
571bool SkSVGTextPath::parseAndSetAttribute(const char* name, const char* value) {
572 return INHERITED::parseAndSetAttribute(name, value) ||
573 this->setHref(SkSVGAttributeParser::parse<SkSVGIRI>("xlink:href", name, value)) ||
574 this->setStartOffset(SkSVGAttributeParser::parse<SkSVGLength>("startOffset", name, value));
575}