2016-07-27 01:46:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSVGRenderContext_DEFINED
|
|
|
|
#define SkSVGRenderContext_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "experimental/svg/model/SkSVGAttribute.h"
|
|
|
|
#include "experimental/svg/model/SkSVGIDMapper.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "src/core/SkTLazy.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
class SkCanvas;
|
2016-08-03 17:21:11 +00:00
|
|
|
class SkSVGLength;
|
|
|
|
|
|
|
|
class SkSVGLengthContext {
|
|
|
|
public:
|
2016-08-17 21:51:03 +00:00
|
|
|
SkSVGLengthContext(const SkSize& viewport, SkScalar dpi = 90)
|
|
|
|
: fViewport(viewport), fDPI(dpi) {}
|
2016-08-03 17:21:11 +00:00
|
|
|
|
|
|
|
enum class LengthType {
|
|
|
|
kHorizontal,
|
|
|
|
kVertical,
|
|
|
|
kOther,
|
|
|
|
};
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
const SkSize& viewPort() const { return fViewport; }
|
2016-08-03 17:21:11 +00:00
|
|
|
void setViewPort(const SkSize& viewport) { fViewport = viewport; }
|
|
|
|
|
|
|
|
SkScalar resolve(const SkSVGLength&, LengthType) const;
|
2016-08-08 18:38:55 +00:00
|
|
|
SkRect resolveRect(const SkSVGLength& x, const SkSVGLength& y,
|
|
|
|
const SkSVGLength& w, const SkSVGLength& h) const;
|
2016-08-03 17:21:11 +00:00
|
|
|
|
|
|
|
private:
|
2016-08-17 21:51:03 +00:00
|
|
|
SkSize fViewport;
|
|
|
|
SkScalar fDPI;
|
2016-08-03 17:21:11 +00:00
|
|
|
};
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
struct SkSVGPresentationContext {
|
2016-08-08 18:38:55 +00:00
|
|
|
SkSVGPresentationContext();
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext(const SkSVGPresentationContext&) = default;
|
|
|
|
SkSVGPresentationContext& operator=(const SkSVGPresentationContext&) = default;
|
2016-08-08 18:38:55 +00:00
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
// Inherited presentation attributes, computed for the current node.
|
|
|
|
SkSVGPresentationAttributes fInherited;
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
// Cached paints, reflecting the current presentation attributes.
|
|
|
|
SkPaint fFillPaint;
|
|
|
|
SkPaint fStrokePaint;
|
2016-08-08 18:38:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkSVGRenderContext {
|
|
|
|
public:
|
2016-09-13 00:06:47 +00:00
|
|
|
SkSVGRenderContext(SkCanvas*, const SkSVGIDMapper&, const SkSVGLengthContext&,
|
|
|
|
const SkSVGPresentationContext&);
|
2016-08-08 18:38:55 +00:00
|
|
|
SkSVGRenderContext(const SkSVGRenderContext&);
|
2017-10-11 18:34:33 +00:00
|
|
|
SkSVGRenderContext(const SkSVGRenderContext&, SkCanvas*);
|
2016-08-08 18:38:55 +00:00
|
|
|
~SkSVGRenderContext();
|
|
|
|
|
|
|
|
const SkSVGLengthContext& lengthContext() const { return *fLengthContext; }
|
|
|
|
SkSVGLengthContext* writableLengthContext() { return fLengthContext.writable(); }
|
|
|
|
|
2016-12-01 18:35:11 +00:00
|
|
|
const SkSVGPresentationContext& presentationContext() const { return *fPresentationContext; }
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
SkCanvas* canvas() const { return fCanvas; }
|
2017-10-11 18:11:16 +00:00
|
|
|
void saveOnce();
|
2016-08-08 18:38:55 +00:00
|
|
|
|
2016-09-20 22:45:57 +00:00
|
|
|
enum ApplyFlags {
|
|
|
|
kLeaf = 1 << 0, // the target node doesn't have descendants
|
|
|
|
};
|
|
|
|
void applyPresentationAttributes(const SkSVGPresentationAttributes&, uint32_t flags);
|
2016-08-11 16:16:29 +00:00
|
|
|
|
2020-04-08 19:02:02 +00:00
|
|
|
// Scoped wrapper that temporarily clears the original node reference.
|
|
|
|
class BorrowedNode {
|
|
|
|
public:
|
|
|
|
explicit BorrowedNode(sk_sp<SkSVGNode>* node)
|
|
|
|
: fOwner(node) {
|
|
|
|
if (fOwner) {
|
|
|
|
fBorrowed = std::move(*fOwner);
|
|
|
|
*fOwner = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~BorrowedNode() {
|
|
|
|
if (fOwner) {
|
|
|
|
*fOwner = std::move(fBorrowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkSVGNode* get() const { return fBorrowed.get(); }
|
|
|
|
const SkSVGNode* operator->() const { return fBorrowed.get(); }
|
|
|
|
const SkSVGNode& operator*() const { return *fBorrowed; }
|
|
|
|
|
|
|
|
operator bool() const { return !!fBorrowed; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// noncopyable
|
|
|
|
BorrowedNode(const BorrowedNode&) = delete;
|
|
|
|
BorrowedNode& operator=(BorrowedNode&) = delete;
|
|
|
|
|
|
|
|
sk_sp<SkSVGNode>* fOwner;
|
|
|
|
sk_sp<SkSVGNode> fBorrowed;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: the id->node association is cleared for the lifetime of the returned value
|
|
|
|
// (effectively breaks reference cycles, assuming appropriate return value scoping).
|
|
|
|
BorrowedNode findNodeById(const SkString&) const;
|
2016-09-13 00:06:47 +00:00
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
const SkPaint* fillPaint() const;
|
|
|
|
const SkPaint* strokePaint() const;
|
|
|
|
|
2016-12-08 21:04:24 +00:00
|
|
|
// The local computed clip path (not inherited).
|
|
|
|
const SkPath* clipPath() const { return fClipPath.getMaybeNull(); }
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
private:
|
|
|
|
// Stack-only
|
|
|
|
void* operator new(size_t) = delete;
|
|
|
|
void* operator new(size_t, void*) = delete;
|
|
|
|
SkSVGRenderContext& operator=(const SkSVGRenderContext&) = delete;
|
|
|
|
|
2016-09-20 22:45:57 +00:00
|
|
|
void applyOpacity(SkScalar opacity, uint32_t flags);
|
2016-12-08 14:26:47 +00:00
|
|
|
void applyClip(const SkSVGClip&);
|
2020-04-20 14:52:21 +00:00
|
|
|
void updatePaintsWithCurrentColor(const SkSVGPresentationAttributes&);
|
2016-09-20 22:45:57 +00:00
|
|
|
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGIDMapper& fIDMapper;
|
2016-08-08 18:38:55 +00:00
|
|
|
SkTCopyOnFirstWrite<SkSVGLengthContext> fLengthContext;
|
|
|
|
SkTCopyOnFirstWrite<SkSVGPresentationContext> fPresentationContext;
|
|
|
|
SkCanvas* fCanvas;
|
|
|
|
// The save count on 'fCanvas' at construction time.
|
|
|
|
// A restoreToCount() will be issued on destruction.
|
|
|
|
int fCanvasSaveCount;
|
2016-12-08 21:04:24 +00:00
|
|
|
|
|
|
|
// clipPath, if present for the current context (not inherited).
|
|
|
|
SkTLazy<SkPath> fClipPath;
|
2016-07-27 01:46:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SkSVGRenderContext_DEFINED
|