skia2/modules/svg/include/SkSVGText.h
Florin Malita fc0ea0a4a5 [svg] TextPath support
- introduce SkSVGTextPath and update the text render logic to
    instantiate new SkSVGTextContexts in onRenderText() overrides
    instead of renderText() root -- this is to observe spec semantics
    [1] requiring <text> and <textPath> to always start a new chunk,
    regardless of their relative nesting.
  - expand SkSVGTextContext to also store PathData when used in the
    scope of a textPath
  - PathData caches SkContourMeasures, for path position lookup
  - update flushChunk() to apply path glyph adjustments [2]:

    * the horizontal glyph position (including relative offset dx),
      adjusted for the glyph center yields a path offset
    * if the offset is outside the path range, the glyph is skipped
    * otherwise the position is determined based on the path matrix
      at the computed offset

   - to support the logic above, the chunk starting position is no
     longer used as a global blob offset but instead is folded into
     individual glyph RSXforms (and the blob always draws at {0,0})

[1] https://www.w3.org/TR/SVG11/text.html#TextLayout
[2] https://www.w3.org/TR/SVG11/text.html#TextpathLayoutRules

Bug: skia:10840
Change-Id: I462eada7c086646afdc1bc84f08ec2368613f1c0
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/349397
Commit-Queue: Florin Malita <fmalita@google.com>
Reviewed-by: Tyler Denniston <tdenniston@google.com>
2021-01-13 14:37:39 +00:00

121 lines
3.4 KiB
C++

/*
* Copyright 2019 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSVGText_DEFINED
#define SkSVGText_DEFINED
#include <vector>
#include "modules/svg/include/SkSVGTransformableNode.h"
#include "modules/svg/include/SkSVGTypes.h"
class SkSVGTextContext;
// Base class for text-rendering nodes.
class SkSVGTextFragment : public SkSVGTransformableNode {
public:
void renderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const;
protected:
explicit SkSVGTextFragment(SkSVGTag t) : INHERITED(t) {}
virtual void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*,
SkSVGXmlSpace) const = 0;
private:
SkPath onAsPath(const SkSVGRenderContext&) const final;
using INHERITED = SkSVGTransformableNode;
};
// Base class for nestable text containers (<text>, <tspan>, etc).
class SkSVGTextContainer : public SkSVGTextFragment {
public:
SVG_ATTR(X, std::vector<SkSVGLength>, {})
SVG_ATTR(Y, std::vector<SkSVGLength>, {})
SVG_ATTR(Dx, std::vector<SkSVGLength>, {})
SVG_ATTR(Dy, std::vector<SkSVGLength>, {})
SVG_ATTR(Rotate, std::vector<SkSVGNumberType>, {})
SVG_ATTR(XmlSpace, SkSVGXmlSpace, SkSVGXmlSpace::kDefault)
void appendChild(sk_sp<SkSVGNode>) final;
protected:
explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {}
void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
bool parseAndSetAttribute(const char*, const char*) override;
private:
void onRender(const SkSVGRenderContext&) const final;
std::vector<sk_sp<SkSVGTextFragment>> fChildren;
using INHERITED = SkSVGTextFragment;
};
class SkSVGText final : public SkSVGTextContainer {
public:
static sk_sp<SkSVGText> Make() { return sk_sp<SkSVGText>(new SkSVGText()); }
private:
SkSVGText() : INHERITED(SkSVGTag::kText) {}
void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
using INHERITED = SkSVGTextContainer;
};
class SkSVGTSpan final : public SkSVGTextContainer {
public:
static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); }
private:
SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {}
using INHERITED = SkSVGTextContainer;
};
class SkSVGTextLiteral final : public SkSVGTextFragment {
public:
static sk_sp<SkSVGTextLiteral> Make() {
return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral());
}
SVG_ATTR(Text, SkSVGStringType, SkSVGStringType())
private:
SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {}
void onRender(const SkSVGRenderContext&) const override {}
void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
void appendChild(sk_sp<SkSVGNode>) override {}
using INHERITED = SkSVGTextFragment;
};
class SkSVGTextPath final : public SkSVGTextContainer {
public:
static sk_sp<SkSVGTextPath> Make() { return sk_sp<SkSVGTextPath>(new SkSVGTextPath()); }
SVG_ATTR(Href , SkSVGIRI , {SkString()} )
SVG_ATTR(StartOffset, SkSVGLength, SkSVGLength(0))
private:
SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {}
void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
bool parseAndSetAttribute(const char*, const char*) override;
using INHERITED = SkSVGTextContainer;
};
#endif // SkSVGText_DEFINED