[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/include/SkSVGText.h b/modules/svg/include/SkSVGText.h
index 1f45fd3..c446a21 100644
--- a/modules/svg/include/SkSVGText.h
+++ b/modules/svg/include/SkSVGText.h
@@ -23,8 +23,7 @@
protected:
explicit SkSVGTextFragment(SkSVGTag t) : INHERITED(t) {}
- virtual void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*,
- SkSVGXmlSpace) const = 0;
+ virtual void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const = 0;
// Text nodes other than the root <text> element are not rendered directly.
void onRender(const SkSVGRenderContext&) const override {}
@@ -51,7 +50,7 @@
protected:
explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {}
- void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
+ void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
bool parseAndSetAttribute(const char*, const char*) override;
@@ -94,7 +93,7 @@
private:
SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {}
- void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
+ void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
void appendChild(sk_sp<SkSVGNode>) override {}
@@ -111,7 +110,7 @@
private:
SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {}
- void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
+ void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
bool parseAndSetAttribute(const char*, const char*) override;
using INHERITED = SkSVGTextContainer;
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) {
diff --git a/modules/svg/src/SkSVGTextPriv.h b/modules/svg/src/SkSVGTextPriv.h
index 0f7715d..ab89825 100644
--- a/modules/svg/src/SkSVGTextPriv.h
+++ b/modules/svg/src/SkSVGTextPriv.h
@@ -13,6 +13,7 @@
#include "modules/svg/include/SkSVGText.h"
#include "src/core/SkTLazy.h"
+#include <functional>
#include <tuple>
class SkContourMeasure;
@@ -31,6 +32,10 @@
// [1] https://www.w3.org/TR/SVG11/text.html#TextLayoutIntroduction
class SkSVGTextContext final : SkShaper::RunHandler {
public:
+ using ShapedTextCallback = std::function<void(const SkSVGRenderContext&,
+ const sk_sp<SkTextBlob>&,
+ const SkPaint*,
+ const SkPaint*)>;
// Helper for encoding optional positional attributes.
class PosAttrs {
@@ -100,15 +105,19 @@
};
- SkSVGTextContext(const SkSVGRenderContext&, const SkSVGTextPath* = nullptr);
+ SkSVGTextContext(const SkSVGRenderContext&,
+ const ShapedTextCallback&,
+ const SkSVGTextPath* = nullptr);
~SkSVGTextContext() override;
- // Queues codepoints for rendering.
- void appendFragment(const SkString&, const SkSVGRenderContext&, SkSVGXmlSpace);
+ // Shape and queue codepoints for final alignment.
+ void shapeFragment(const SkString&, const SkSVGRenderContext&, SkSVGXmlSpace);
- // Perform actual rendering for queued codepoints.
+ // Perform final adjustments and push shaped blobs to the callback.
void flushChunk(const SkSVGRenderContext& ctx);
+ const ShapedTextCallback& getCallback() const { return fCallback; }
+
private:
struct PositionAdjustment {
SkVector offset;
@@ -173,6 +182,7 @@
// http://www.w3.org/TR/SVG11/text.html#TextLayout
const SkSVGRenderContext& fRenderContext; // original render context
+ const ShapedTextCallback& fCallback;
const std::unique_ptr<SkShaper> fShaper;
std::vector<RunRec> fRuns;
const ScopedPosResolver* fPosResolver = nullptr;
diff --git a/modules/svg/tests/Text.cpp b/modules/svg/tests/Text.cpp
index 83feda4..107395b 100644
--- a/modules/svg/tests/Text.cpp
+++ b/modules/svg/tests/Text.cpp
@@ -144,6 +144,9 @@
},
};
+ const SkSVGTextContext::ShapedTextCallback mock_cb =
+ [](const SkSVGRenderContext&, const sk_sp<SkTextBlob>&, const SkPaint*, const SkPaint*) {};
+
auto test = [&](const PosTestDesc& tst) {
auto a = SkSVGText::Make();
auto b = SkSVGTSpan::Make();
@@ -161,7 +164,7 @@
sk_sp<SkFontMgr> fmgr;
const SkSVGRenderContext ctx(&canvas, fmgr, mapper, lctx, pctx, nullptr);
- SkSVGTextContext tctx(ctx);
+ SkSVGTextContext tctx(ctx, mock_cb);
SkSVGTextContext::ScopedPosResolver pa(*a, lctx, &tctx, tst.offseta);
SkSVGTextContext::ScopedPosResolver pb(*b, lctx, &tctx, tst.offsetb);