2017-10-27 17:33:53 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2016-07-22 11:07:39 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2020-07-14 23:40:05 +00:00
|
|
|
#ifndef LAYOUT_SVG_SVGCONTEXTPAINT_H_
|
|
|
|
#define LAYOUT_SVG_SVGCONTEXTPAINT_H_
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2016-07-25 16:34:18 +00:00
|
|
|
#include "DrawMode.h"
|
2016-07-22 11:07:39 +00:00
|
|
|
#include "gfxMatrix.h"
|
|
|
|
#include "gfxPattern.h"
|
|
|
|
#include "gfxTypes.h"
|
2017-02-24 14:55:28 +00:00
|
|
|
#include "gfxUtils.h"
|
2016-07-22 11:07:39 +00:00
|
|
|
#include "mozilla/AlreadyAddRefed.h"
|
2017-01-10 10:28:11 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-07-22 11:07:39 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2017-01-10 10:28:11 +00:00
|
|
|
#include "nsColor.h"
|
2016-07-25 12:27:00 +00:00
|
|
|
#include "nsStyleStruct.h"
|
2016-07-22 11:07:39 +00:00
|
|
|
#include "nsTArray.h"
|
2017-12-11 15:37:59 +00:00
|
|
|
#include "ImgDrawResult.h"
|
2019-12-09 14:53:50 +00:00
|
|
|
#include "nsRefPtrHashtable.h"
|
2016-07-22 11:07:39 +00:00
|
|
|
|
|
|
|
class gfxContext;
|
2019-01-02 13:05:23 +00:00
|
|
|
|
2016-07-22 13:56:09 +00:00
|
|
|
namespace mozilla {
|
2020-07-07 13:49:30 +00:00
|
|
|
class SVGPaintServerFrame;
|
2016-07-22 13:56:09 +00:00
|
|
|
|
2018-04-21 16:48:29 +00:00
|
|
|
namespace dom {
|
2023-01-17 19:13:41 +00:00
|
|
|
class Document;
|
2018-04-21 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
/**
|
|
|
|
* This class is used to pass information about a context element through to
|
|
|
|
* SVG painting code in order to resolve the 'context-fill' and related
|
|
|
|
* keywords. See:
|
|
|
|
*
|
|
|
|
* https://www.w3.org/TR/SVG2/painting.html#context-paint
|
|
|
|
*
|
|
|
|
* This feature allows the color in an SVG-in-OpenType glyph to come from the
|
|
|
|
* computed style for the text that is being drawn, for example, or for color
|
|
|
|
* in an SVG embedded by an <img> element to come from the embedding <img>
|
|
|
|
* element.
|
2017-01-09 19:12:02 +00:00
|
|
|
*
|
|
|
|
* This class is reference counted so that it can be shared among many similar
|
|
|
|
* SVGImageContext objects. (SVGImageContext objects are frequently
|
|
|
|
* copy-constructed with small modifications, and we'd like for those copies to
|
|
|
|
* be able to share their context-paint data cheaply.) However, in most cases,
|
|
|
|
* SVGContextPaint instances are stored in a local RefPtr and only last for the
|
|
|
|
* duration of a function call.
|
|
|
|
* XXX Note: SVGImageContext doesn't actually have a SVGContextPaint member yet,
|
|
|
|
* but it will in a later patch in the patch series that added this comment.
|
2016-07-22 11:07:39 +00:00
|
|
|
*/
|
2017-01-09 19:12:02 +00:00
|
|
|
class SVGContextPaint : public RefCounted<SVGContextPaint> {
|
2016-07-22 11:07:39 +00:00
|
|
|
protected:
|
2020-07-15 10:37:55 +00:00
|
|
|
using DrawTarget = mozilla::gfx::DrawTarget;
|
|
|
|
using Float = mozilla::gfx::Float;
|
|
|
|
using imgDrawingParams = mozilla::image::imgDrawingParams;
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2017-03-09 12:49:21 +00:00
|
|
|
SVGContextPaint() : mDashOffset(0.0f), mStrokeWidth(0.0f) {}
|
2017-03-21 02:12:23 +00:00
|
|
|
|
2018-11-30 10:46:48 +00:00
|
|
|
public:
|
2017-01-09 19:12:02 +00:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(SVGContextPaint)
|
|
|
|
|
2019-04-17 22:38:02 +00:00
|
|
|
virtual ~SVGContextPaint() = default;
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2017-05-18 20:03:50 +00:00
|
|
|
virtual already_AddRefed<gfxPattern> GetFillPattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aOpacity, const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) = 0;
|
|
|
|
virtual already_AddRefed<gfxPattern> GetStrokePattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aOpacity, const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) = 0;
|
2016-07-22 11:07:39 +00:00
|
|
|
virtual float GetFillOpacity() const = 0;
|
|
|
|
virtual float GetStrokeOpacity() const = 0;
|
|
|
|
|
2017-03-24 19:19:18 +00:00
|
|
|
already_AddRefed<gfxPattern> GetFillPattern(const DrawTarget* aDrawTarget,
|
|
|
|
const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) {
|
|
|
|
return GetFillPattern(aDrawTarget, GetFillOpacity(), aCTM, aImgParams);
|
2016-07-22 11:07:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 19:19:18 +00:00
|
|
|
already_AddRefed<gfxPattern> GetStrokePattern(const DrawTarget* aDrawTarget,
|
|
|
|
const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) {
|
|
|
|
return GetStrokePattern(aDrawTarget, GetStrokeOpacity(), aCTM, aImgParams);
|
2016-07-22 11:07:39 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:34:18 +00:00
|
|
|
static SVGContextPaint* GetContextPaint(nsIContent* aContent);
|
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
// XXX This gets the geometry params from the gfxContext. We should get that
|
|
|
|
// information from the actual paint context!
|
|
|
|
void InitStrokeGeometry(gfxContext* aContext, float devUnitsPerSVGUnit);
|
|
|
|
|
2018-03-11 16:11:24 +00:00
|
|
|
const FallibleTArray<Float>& GetStrokeDashArray() const { return mDashes; }
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2018-03-11 16:11:24 +00:00
|
|
|
Float GetStrokeDashOffset() const { return mDashOffset; }
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2018-03-11 16:11:24 +00:00
|
|
|
Float GetStrokeWidth() const { return mStrokeWidth; }
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2017-01-10 10:28:11 +00:00
|
|
|
virtual uint32_t Hash() const {
|
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"Only VectorImage needs to hash, and that should "
|
|
|
|
"only be operating on our SVGEmbeddingContextPaint "
|
|
|
|
"subclass");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-25 11:47:19 +00:00
|
|
|
/**
|
|
|
|
* Returns true if image context paint is allowed to be used in an image that
|
|
|
|
* has the given URI, else returns false.
|
|
|
|
*/
|
|
|
|
static bool IsAllowedForImageFromURI(nsIURI* aURI);
|
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
private:
|
2016-09-13 12:59:33 +00:00
|
|
|
// Member-vars are initialized in InitStrokeGeometry.
|
2018-03-11 16:11:24 +00:00
|
|
|
FallibleTArray<Float> mDashes;
|
|
|
|
MOZ_INIT_OUTSIDE_CTOR Float mDashOffset;
|
|
|
|
MOZ_INIT_OUTSIDE_CTOR Float mStrokeWidth;
|
2016-07-22 11:07:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RAII class used to temporarily set and remove an SVGContextPaint while a
|
|
|
|
* piece of SVG is being painted. The context paint is set on the SVG's owner
|
2016-07-25 16:34:18 +00:00
|
|
|
* document, as expected by SVGContextPaint::GetContextPaint. Any pre-existing
|
2016-07-22 11:07:39 +00:00
|
|
|
* context paint is restored after this class removes the context paint that it
|
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
class MOZ_RAII AutoSetRestoreSVGContextPaint {
|
|
|
|
public:
|
2023-01-17 19:13:41 +00:00
|
|
|
AutoSetRestoreSVGContextPaint(const SVGContextPaint* aContextPaint,
|
|
|
|
dom::Document* aDocument);
|
2016-07-22 11:07:39 +00:00
|
|
|
~AutoSetRestoreSVGContextPaint();
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
private:
|
2023-01-17 19:13:41 +00:00
|
|
|
dom::Document* mDocument;
|
2016-07-22 11:07:39 +00:00
|
|
|
// The context paint that needs to be restored by our dtor after it removes
|
|
|
|
// aContextPaint:
|
2018-04-21 16:48:29 +00:00
|
|
|
const SVGContextPaint* mOuterContextPaint;
|
2016-07-22 11:07:39 +00:00
|
|
|
};
|
|
|
|
|
2016-07-25 12:27:00 +00:00
|
|
|
/**
|
|
|
|
* This class should be flattened into SVGContextPaint once we get rid of the
|
|
|
|
* other sub-class (SimpleTextContextPaint).
|
|
|
|
*/
|
2016-07-25 15:04:20 +00:00
|
|
|
struct SVGContextPaintImpl : public SVGContextPaint {
|
2016-07-25 12:27:00 +00:00
|
|
|
protected:
|
2020-07-15 10:37:55 +00:00
|
|
|
using DrawTarget = mozilla::gfx::DrawTarget;
|
2017-03-21 02:12:23 +00:00
|
|
|
|
2016-07-25 12:27:00 +00:00
|
|
|
public:
|
2017-03-21 02:12:23 +00:00
|
|
|
DrawMode Init(const DrawTarget* aDrawTarget, const gfxMatrix& aContextMatrix,
|
2017-05-18 20:03:50 +00:00
|
|
|
nsIFrame* aFrame, SVGContextPaint* aOuterContextPaint,
|
|
|
|
imgDrawingParams& aImgParams);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2017-05-18 20:03:50 +00:00
|
|
|
already_AddRefed<gfxPattern> GetFillPattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aOpacity, const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) override;
|
|
|
|
already_AddRefed<gfxPattern> GetStrokePattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aOpacity, const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) override;
|
2016-07-25 12:27:00 +00:00
|
|
|
|
|
|
|
void SetFillOpacity(float aOpacity) { mFillOpacity = aOpacity; }
|
|
|
|
float GetFillOpacity() const override { return mFillOpacity; }
|
|
|
|
|
|
|
|
void SetStrokeOpacity(float aOpacity) { mStrokeOpacity = aOpacity; }
|
|
|
|
float GetStrokeOpacity() const override { return mStrokeOpacity; }
|
|
|
|
|
|
|
|
struct Paint {
|
2019-07-06 08:31:02 +00:00
|
|
|
enum class Tag : uint8_t {
|
|
|
|
None,
|
|
|
|
Color,
|
|
|
|
PaintServer,
|
|
|
|
ContextFill,
|
|
|
|
ContextStroke,
|
|
|
|
};
|
|
|
|
|
|
|
|
Paint() : mPaintDefinition{}, mPaintType(Tag::None) {}
|
2016-07-25 12:27:00 +00:00
|
|
|
|
|
|
|
void SetPaintServer(nsIFrame* aFrame, const gfxMatrix& aContextMatrix,
|
2020-07-07 13:49:30 +00:00
|
|
|
SVGPaintServerFrame* aPaintServerFrame) {
|
2019-07-06 08:31:02 +00:00
|
|
|
mPaintType = Tag::PaintServer;
|
2016-07-25 12:27:00 +00:00
|
|
|
mPaintDefinition.mPaintServerFrame = aPaintServerFrame;
|
|
|
|
mFrame = aFrame;
|
|
|
|
mContextMatrix = aContextMatrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetColor(const nscolor& aColor) {
|
2019-07-06 08:31:02 +00:00
|
|
|
mPaintType = Tag::Color;
|
2016-07-25 12:27:00 +00:00
|
|
|
mPaintDefinition.mColor = aColor;
|
|
|
|
}
|
|
|
|
|
2019-07-06 08:31:02 +00:00
|
|
|
void SetContextPaint(SVGContextPaint* aContextPaint, Tag aTag) {
|
|
|
|
MOZ_ASSERT(aTag == Tag::ContextFill || aTag == Tag::ContextStroke);
|
|
|
|
mPaintType = aTag;
|
2016-07-25 12:27:00 +00:00
|
|
|
mPaintDefinition.mContextPaint = aContextPaint;
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
2020-07-07 13:49:30 +00:00
|
|
|
SVGPaintServerFrame* mPaintServerFrame;
|
2016-07-25 12:27:00 +00:00
|
|
|
SVGContextPaint* mContextPaint;
|
|
|
|
nscolor mColor;
|
|
|
|
} mPaintDefinition;
|
|
|
|
|
2016-09-13 13:12:32 +00:00
|
|
|
// Initialized (if needed) in SetPaintServer():
|
|
|
|
MOZ_INIT_OUTSIDE_CTOR nsIFrame* mFrame;
|
2016-07-25 12:27:00 +00:00
|
|
|
// CTM defining the user space for the pattern we will use.
|
|
|
|
gfxMatrix mContextMatrix;
|
2019-07-06 08:31:02 +00:00
|
|
|
Tag mPaintType;
|
2016-07-25 12:27:00 +00:00
|
|
|
|
|
|
|
// Device-space-to-pattern-space
|
|
|
|
gfxMatrix mPatternMatrix;
|
|
|
|
nsRefPtrHashtable<nsFloatHashKey, gfxPattern> mPatternCache;
|
|
|
|
|
2017-05-18 20:03:50 +00:00
|
|
|
already_AddRefed<gfxPattern> GetPattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aOpacity,
|
2019-07-06 08:31:02 +00:00
|
|
|
StyleSVGPaint nsStyleSVG::*aFillOrStroke, const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams);
|
2016-07-25 12:27:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Paint mFillPaint;
|
|
|
|
Paint mStrokePaint;
|
|
|
|
|
|
|
|
float mFillOpacity;
|
|
|
|
float mStrokeOpacity;
|
|
|
|
};
|
|
|
|
|
2017-01-10 10:28:11 +00:00
|
|
|
/**
|
|
|
|
* This class is used to pass context paint to an SVG image when an element
|
|
|
|
* references that image (e.g. via HTML <img> or SVG <image>, or by referencing
|
|
|
|
* it from a CSS property such as 'background-image'). In this case we only
|
|
|
|
* support context colors and not paint servers.
|
|
|
|
*/
|
|
|
|
class SVGEmbeddingContextPaint : public SVGContextPaint {
|
2020-07-15 10:37:55 +00:00
|
|
|
using DeviceColor = gfx::DeviceColor;
|
2017-02-24 14:55:28 +00:00
|
|
|
|
2017-01-10 10:28:11 +00:00
|
|
|
public:
|
2017-05-18 11:47:20 +00:00
|
|
|
SVGEmbeddingContextPaint() : mFillOpacity(1.0f), mStrokeOpacity(1.0f) {}
|
2017-01-10 10:28:11 +00:00
|
|
|
|
2017-03-01 10:41:14 +00:00
|
|
|
bool operator==(const SVGEmbeddingContextPaint& aOther) const {
|
|
|
|
MOZ_ASSERT(GetStrokeWidth() == aOther.GetStrokeWidth() &&
|
|
|
|
GetStrokeDashOffset() == aOther.GetStrokeDashOffset() &&
|
|
|
|
GetStrokeDashArray() == aOther.GetStrokeDashArray(),
|
|
|
|
"We don't currently include these in the context information "
|
|
|
|
"from an embedding element");
|
2017-05-18 11:47:20 +00:00
|
|
|
return mFill == aOther.mFill && mStroke == aOther.mStroke &&
|
|
|
|
mFillOpacity == aOther.mFillOpacity &&
|
|
|
|
mStrokeOpacity == aOther.mStrokeOpacity;
|
2017-03-01 10:41:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-24 14:55:28 +00:00
|
|
|
void SetFill(nscolor aFill) { mFill.emplace(gfx::ToDeviceColor(aFill)); }
|
2020-03-09 14:16:17 +00:00
|
|
|
const Maybe<DeviceColor>& GetFill() const { return mFill; }
|
2017-02-24 14:55:28 +00:00
|
|
|
void SetStroke(nscolor aStroke) {
|
|
|
|
mStroke.emplace(gfx::ToDeviceColor(aStroke));
|
|
|
|
}
|
2020-03-09 14:16:17 +00:00
|
|
|
const Maybe<DeviceColor>& GetStroke() const { return mStroke; }
|
2017-01-10 10:28:11 +00:00
|
|
|
|
2017-02-24 14:55:28 +00:00
|
|
|
/**
|
|
|
|
* Returns a pattern of type PatternType::COLOR, or else nullptr.
|
|
|
|
*/
|
2017-05-18 20:03:50 +00:00
|
|
|
already_AddRefed<gfxPattern> GetFillPattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aFillOpacity, const gfxMatrix& aCTM,
|
2017-05-18 20:03:50 +00:00
|
|
|
imgDrawingParams& aImgParams) override;
|
2017-01-10 10:28:11 +00:00
|
|
|
|
2017-02-24 14:55:28 +00:00
|
|
|
/**
|
|
|
|
* Returns a pattern of type PatternType::COLOR, or else nullptr.
|
|
|
|
*/
|
2017-05-18 20:03:50 +00:00
|
|
|
already_AddRefed<gfxPattern> GetStrokePattern(
|
2017-03-21 02:12:23 +00:00
|
|
|
const DrawTarget* aDrawTarget, float aStrokeOpacity,
|
2017-05-18 20:03:50 +00:00
|
|
|
const gfxMatrix& aCTM, imgDrawingParams& aImgParams) override;
|
2017-01-10 10:28:11 +00:00
|
|
|
|
2017-05-18 11:47:20 +00:00
|
|
|
void SetFillOpacity(float aOpacity) { mFillOpacity = aOpacity; }
|
2017-01-10 10:28:11 +00:00
|
|
|
float GetFillOpacity() const override { return mFillOpacity; };
|
|
|
|
|
2017-05-18 11:47:20 +00:00
|
|
|
void SetStrokeOpacity(float aOpacity) { mStrokeOpacity = aOpacity; }
|
2017-01-10 10:28:11 +00:00
|
|
|
float GetStrokeOpacity() const override { return mStrokeOpacity; };
|
|
|
|
|
|
|
|
uint32_t Hash() const override;
|
|
|
|
|
|
|
|
private:
|
2020-03-09 14:16:17 +00:00
|
|
|
Maybe<DeviceColor> mFill;
|
|
|
|
Maybe<DeviceColor> mStroke;
|
2017-05-18 11:47:20 +00:00
|
|
|
float mFillOpacity;
|
|
|
|
float mStrokeOpacity;
|
2017-01-10 10:28:11 +00:00
|
|
|
};
|
|
|
|
|
2016-07-22 13:56:09 +00:00
|
|
|
} // namespace mozilla
|
2016-07-22 11:07:39 +00:00
|
|
|
|
2020-07-14 23:40:05 +00:00
|
|
|
#endif // LAYOUT_SVG_SVGCONTEXTPAINT_H_
|