[svg] Generalize text shaping
In preparation for text bounding box support, fission the actual
rendering phase from the shaping/alignment phase:
- rename onRenderText -> onShapeText
- introduce a ShapedTextCallback abstraction for consuming the result
of text processing
- relocate the final rendering step to a ShapedTextCallback impl
Bug: skia:10840
Change-Id: Ia8cc0d9a5a5484972a34042fd782f9e4fada6b12
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/358223
Commit-Queue: Florin Malita <fmalita@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: Tyler Denniston <tdenniston@google.com>
diff --git a/modules/svg/src/SkSVGText.cpp b/modules/svg/src/SkSVGText.cpp
index 7b25578..f81eb7c 100644
--- a/modules/svg/src/SkSVGText.cpp
+++ b/modules/svg/src/SkSVGText.cpp
@@ -223,8 +223,10 @@
fShapeBuffer.reset();
}
-SkSVGTextContext::SkSVGTextContext(const SkSVGRenderContext& ctx, const SkSVGTextPath* tpath)
+SkSVGTextContext::SkSVGTextContext(const SkSVGRenderContext& ctx, const ShapedTextCallback& cb,
+ const SkSVGTextPath* tpath)
: fRenderContext(ctx)
+ , fCallback(cb)
, fShaper(SkShaper::Make(ctx.fontMgr()))
, fChunkAlignmentFactor(ComputeAlignmentFactor(ctx.presentationContext()))
{
@@ -255,8 +257,8 @@
this->flushChunk(fRenderContext);
}
-void SkSVGTextContext::appendFragment(const SkString& txt, const SkSVGRenderContext& ctx,
- SkSVGXmlSpace xs) {
+void SkSVGTextContext::shapeFragment(const SkString& txt, const SkSVGRenderContext& ctx,
+ SkSVGXmlSpace xs) {
// https://www.w3.org/TR/SVG11/text.html#WhiteSpace
// https://www.w3.org/TR/2008/REC-xml-20081126/#NT-S
auto filterWSDefault = [this](SkUnichar ch) -> SkUnichar {
@@ -417,15 +419,7 @@
run.glyhPosAdjust[i]);
}
- // Technically, blobs with compatible paints could be merged --
- // but likely not worth the effort.
- const auto blob = blobBuilder.make();
- if (run.fillPaint) {
- ctx.canvas()->drawTextBlob(blob, 0, 0, *run.fillPaint);
- }
- if (run.strokePaint) {
- ctx.canvas()->drawTextBlob(blob, 0, 0, *run.strokePaint);
- }
+ fCallback(ctx, blobBuilder.make(), run.fillPaint.get(), run.strokePaint.get());
}
fChunkPos += fChunkAdvance;
@@ -480,7 +474,7 @@
SkSVGRenderContext localContext(ctx, this);
if (this->onPrepareToRender(&localContext)) {
- this->onRenderText(localContext, tctx, xs);
+ this->onShapeText(localContext, tctx, xs);
}
}
@@ -503,8 +497,8 @@
}
}
-void SkSVGTextContainer::onRenderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
- SkSVGXmlSpace) const {
+void SkSVGTextContainer::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
+ SkSVGXmlSpace) const {
SkASSERT(tctx);
const SkSVGTextContext::ScopedPosResolver resolver(*this, ctx.lengthContext(), tctx);
@@ -538,26 +532,40 @@
this->setXmlSpace(SkSVGAttributeParser::parse<SkSVGXmlSpace>("xml:space", name, value));
}
-void SkSVGTextLiteral::onRenderText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
- SkSVGXmlSpace xs) const {
+void SkSVGTextLiteral::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* tctx,
+ SkSVGXmlSpace xs) const {
SkASSERT(tctx);
- tctx->appendFragment(this->getText(), ctx, xs);
+ tctx->shapeFragment(this->getText(), ctx, xs);
}
void SkSVGText::onRender(const SkSVGRenderContext& ctx) const {
- // Root <text> nodes establish a text layout context.
- SkSVGTextContext tctx(ctx);
+ const SkSVGTextContext::ShapedTextCallback render_text = [](const SkSVGRenderContext& ctx,
+ const sk_sp<SkTextBlob>& blob,
+ const SkPaint* fill,
+ const SkPaint* stroke) {
+ if (fill) {
+ ctx.canvas()->drawTextBlob(blob, 0, 0, *fill);
+ }
+ if (stroke) {
+ ctx.canvas()->drawTextBlob(blob, 0, 0, *stroke);
+ }
+ };
- this->onRenderText(ctx, &tctx, this->getXmlSpace());
+ // Root <text> nodes establish a text layout context.
+ SkSVGTextContext tctx(ctx, render_text);
+
+ this->onShapeText(ctx, &tctx, this->getXmlSpace());
}
-void SkSVGTextPath::onRenderText(const SkSVGRenderContext& ctx, SkSVGTextContext*,
+void SkSVGTextPath::onShapeText(const SkSVGRenderContext& ctx, SkSVGTextContext* parent_tctx,
SkSVGXmlSpace xs) const {
- // textPath nodes establish a new text layout context.
- SkSVGTextContext tctx(ctx, this);
+ SkASSERT(parent_tctx);
- this->INHERITED::onRenderText(ctx, &tctx, xs);
+ // textPath nodes establish a new text layout context.
+ SkSVGTextContext tctx(ctx, parent_tctx->getCallback(), this);
+
+ this->INHERITED::onShapeText(ctx, &tctx, xs);
}
bool SkSVGTextPath::parseAndSetAttribute(const char* name, const char* value) {