2017-10-27 20:33:53 +03: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 14:07:39 +03: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/. */
|
|
|
|
|
|
|
|
#include "SVGContextPaint.h"
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
2017-01-10 13:28:11 +03:00
|
|
|
#include "gfxUtils.h"
|
2016-07-25 19:34:18 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2019-08-13 07:58:06 +03:00
|
|
|
#include "mozilla/dom/SVGDocument.h"
|
2021-05-20 23:34:40 +03:00
|
|
|
#include "mozilla/extensions/WebExtensionPolicy.h"
|
2019-08-13 07:58:06 +03:00
|
|
|
#include "mozilla/StaticPrefs_svg.h"
|
2020-07-07 16:49:30 +03:00
|
|
|
#include "mozilla/SVGObserverUtils.h"
|
2020-07-11 05:20:20 +03:00
|
|
|
#include "mozilla/SVGUtils.h"
|
2020-07-07 16:49:30 +03:00
|
|
|
#include "SVGPaintServerFrame.h"
|
2016-07-25 15:27:00 +03:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
2017-03-21 05:12:23 +03:00
|
|
|
using namespace mozilla::image;
|
2016-07-22 14:07:39 +03:00
|
|
|
|
2016-07-22 16:56:09 +03:00
|
|
|
namespace mozilla {
|
2016-07-22 14:07:39 +03:00
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
using image::imgDrawingParams;
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
bool SVGContextPaint::IsAllowedForImageFromURI(nsIURI* aURI) {
|
2019-08-13 07:58:06 +03:00
|
|
|
if (StaticPrefs::svg_context_properties_content_enabled()) {
|
2017-04-25 14:47:19 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context paint is pref'ed off for Web content. Ideally we'd have some
|
|
|
|
// easy means to determine whether the frame that has linked to the image
|
|
|
|
// is a frame for a content node that originated from Web content.
|
|
|
|
// Unfortunately different types of anonymous content, about: documents
|
|
|
|
// such as about:reader, etc. that are "our" code that we ship are
|
|
|
|
// sometimes hard to distinguish from real Web content. As a result,
|
|
|
|
// instead of trying to figure out what content is "ours" we instead let
|
|
|
|
// any content provide image context paint, but only if the image is
|
|
|
|
// chrome:// or resource:// do we return true. This should be sufficient
|
|
|
|
// to stop the image context paint feature being useful to (and therefore
|
|
|
|
// used by and relied upon by) Web content. (We don't want Web content to
|
|
|
|
// use this feature because we're not sure that image context paint is a
|
|
|
|
// good mechanism for wider use, or suitable for specification.)
|
|
|
|
//
|
2018-01-24 17:15:51 +03:00
|
|
|
// Because the default favicon used in the browser UI needs context paint, we
|
|
|
|
// also allow it for page-icon:<page-url>. This exposes context paint to
|
|
|
|
// 3rd-party favicons, but only for history and bookmark items. Other places
|
|
|
|
// such as the tab bar don't use the page-icon protocol to load favicons.
|
|
|
|
//
|
2017-07-10 20:34:41 +03:00
|
|
|
// One case that is not covered by chrome:// or resource:// are WebExtensions,
|
|
|
|
// specifically ones that are "ours". WebExtensions are moz-extension://
|
|
|
|
// regardless if the extension is in-tree or not. Since we don't want
|
|
|
|
// extension developers coming to rely on image context paint either, we only
|
2021-06-01 17:46:03 +03:00
|
|
|
// enable context-paint for extensions that are owned by Mozilla
|
|
|
|
// (based on the extension permission "internal:svgContextPropertiesAllowed").
|
2017-04-25 14:47:19 +03:00
|
|
|
//
|
2021-04-08 04:48:07 +03:00
|
|
|
// We also allow this for browser UI icons that are served up from
|
|
|
|
// Mozilla-controlled domains listed in the
|
|
|
|
// svg.context-properties.content.allowed-domains pref.
|
|
|
|
//
|
2017-04-25 14:47:19 +03:00
|
|
|
nsAutoCString scheme;
|
|
|
|
if (NS_SUCCEEDED(aURI->GetScheme(scheme)) &&
|
2018-01-24 17:15:51 +03:00
|
|
|
(scheme.EqualsLiteral("chrome") || scheme.EqualsLiteral("resource") ||
|
|
|
|
scheme.EqualsLiteral("page-icon"))) {
|
2017-04-25 14:47:19 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-10 20:34:41 +03:00
|
|
|
RefPtr<BasePrincipal> principal =
|
2019-07-08 19:37:45 +03:00
|
|
|
BasePrincipal::CreateContentPrincipal(aURI, OriginAttributes());
|
2021-04-08 04:48:07 +03:00
|
|
|
|
2021-05-20 23:34:40 +03:00
|
|
|
RefPtr<extensions::WebExtensionPolicy> addonPolicy = principal->AddonPolicy();
|
|
|
|
if (addonPolicy) {
|
2021-06-01 17:46:03 +03:00
|
|
|
// Only allowed for extensions that have the
|
|
|
|
// internal:svgContextPropertiesAllowed permission (added internally from
|
|
|
|
// to Mozilla-owned extensions, see `isMozillaExtension` function
|
|
|
|
// defined in Extension.jsm for the exact criteria).
|
|
|
|
return addonPolicy->HasPermission(
|
|
|
|
nsGkAtoms::svgContextPropertiesAllowedPermission);
|
2017-07-10 20:34:41 +03:00
|
|
|
}
|
2021-04-08 04:48:07 +03:00
|
|
|
|
|
|
|
bool isInAllowList = false;
|
|
|
|
principal->IsURIInPrefList("svg.context-properties.content.allowed-domains",
|
|
|
|
&isInAllowList);
|
|
|
|
return isInAllowList;
|
2017-04-25 14:47:19 +03:00
|
|
|
}
|
|
|
|
|
2016-07-25 19:34:18 +03:00
|
|
|
/**
|
|
|
|
* Stores in |aTargetPaint| information on how to reconstruct the current
|
|
|
|
* fill or stroke pattern. Will also set the paint opacity to transparent if
|
|
|
|
* the paint is set to "none".
|
|
|
|
* @param aOuterContextPaint pattern information from the outer text context
|
|
|
|
* @param aTargetPaint where to store the current pattern information
|
|
|
|
* @param aFillOrStroke member pointer to the paint we are setting up
|
|
|
|
*/
|
|
|
|
static void SetupInheritablePaint(const DrawTarget* aDrawTarget,
|
|
|
|
const gfxMatrix& aContextMatrix,
|
|
|
|
nsIFrame* aFrame, float& aOpacity,
|
|
|
|
SVGContextPaint* aOuterContextPaint,
|
|
|
|
SVGContextPaintImpl::Paint& aTargetPaint,
|
2019-07-06 11:31:02 +03:00
|
|
|
StyleSVGPaint nsStyleSVG::*aFillOrStroke,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) {
|
2016-07-25 19:34:18 +03:00
|
|
|
const nsStyleSVG* style = aFrame->StyleSVG();
|
2020-07-07 16:49:30 +03:00
|
|
|
SVGPaintServerFrame* ps =
|
2018-09-25 23:16:49 +03:00
|
|
|
SVGObserverUtils::GetAndObservePaintServer(aFrame, aFillOrStroke);
|
2016-07-25 19:34:18 +03:00
|
|
|
|
|
|
|
if (ps) {
|
2017-05-18 23:03:50 +03:00
|
|
|
RefPtr<gfxPattern> pattern =
|
2016-07-25 19:34:18 +03:00
|
|
|
ps->GetPaintServerPattern(aFrame, aDrawTarget, aContextMatrix,
|
2017-05-18 23:03:50 +03:00
|
|
|
aFillOrStroke, aOpacity, aImgParams);
|
2017-03-21 05:12:23 +03:00
|
|
|
|
2016-07-25 19:34:18 +03:00
|
|
|
if (pattern) {
|
|
|
|
aTargetPaint.SetPaintServer(aFrame, aContextMatrix, ps);
|
2017-05-18 23:03:50 +03:00
|
|
|
return;
|
2016-07-25 19:34:18 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-21 05:12:23 +03:00
|
|
|
|
2016-07-25 19:34:18 +03:00
|
|
|
if (aOuterContextPaint) {
|
|
|
|
RefPtr<gfxPattern> pattern;
|
2019-07-06 11:31:02 +03:00
|
|
|
auto tag = SVGContextPaintImpl::Paint::Tag::None;
|
|
|
|
switch ((style->*aFillOrStroke).kind.tag) {
|
|
|
|
case StyleSVGPaintKind::Tag::ContextFill:
|
|
|
|
tag = SVGContextPaintImpl::Paint::Tag::ContextFill;
|
2017-03-21 05:12:23 +03:00
|
|
|
pattern = aOuterContextPaint->GetFillPattern(
|
2017-05-18 23:03:50 +03:00
|
|
|
aDrawTarget, aOpacity, aContextMatrix, aImgParams);
|
2016-07-25 19:34:18 +03:00
|
|
|
break;
|
2019-07-06 11:31:02 +03:00
|
|
|
case StyleSVGPaintKind::Tag::ContextStroke:
|
|
|
|
tag = SVGContextPaintImpl::Paint::Tag::ContextStroke;
|
2017-03-21 05:12:23 +03:00
|
|
|
pattern = aOuterContextPaint->GetStrokePattern(
|
2017-05-18 23:03:50 +03:00
|
|
|
aDrawTarget, aOpacity, aContextMatrix, aImgParams);
|
2016-07-25 19:34:18 +03:00
|
|
|
break;
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
if (pattern) {
|
2019-07-06 11:31:02 +03:00
|
|
|
aTargetPaint.SetContextPaint(aOuterContextPaint, tag);
|
2017-05-18 23:03:50 +03:00
|
|
|
return;
|
2016-07-25 19:34:18 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-21 05:12:23 +03:00
|
|
|
|
2016-07-25 19:34:18 +03:00
|
|
|
nscolor color =
|
2020-07-11 05:20:20 +03:00
|
|
|
SVGUtils::GetFallbackOrPaintColor(*aFrame->Style(), aFillOrStroke);
|
2016-07-25 19:34:18 +03:00
|
|
|
aTargetPaint.SetColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawMode SVGContextPaintImpl::Init(const DrawTarget* aDrawTarget,
|
|
|
|
const gfxMatrix& aContextMatrix,
|
|
|
|
nsIFrame* aFrame,
|
2017-05-18 23:03:50 +03:00
|
|
|
SVGContextPaint* aOuterContextPaint,
|
|
|
|
imgDrawingParams& aImgParams) {
|
2016-07-25 19:34:18 +03:00
|
|
|
DrawMode toDraw = DrawMode(0);
|
|
|
|
|
|
|
|
const nsStyleSVG* style = aFrame->StyleSVG();
|
|
|
|
|
|
|
|
// fill:
|
2019-07-06 11:31:02 +03:00
|
|
|
if (style->mFill.kind.IsNone()) {
|
2016-07-25 19:34:18 +03:00
|
|
|
SetFillOpacity(0.0f);
|
|
|
|
} else {
|
2020-02-23 18:43:03 +03:00
|
|
|
float opacity =
|
2020-07-11 05:20:20 +03:00
|
|
|
SVGUtils::GetOpacity(style->mFillOpacity, aOuterContextPaint);
|
2016-07-25 19:34:18 +03:00
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
SetupInheritablePaint(aDrawTarget, aContextMatrix, aFrame, opacity,
|
|
|
|
aOuterContextPaint, mFillPaint, &nsStyleSVG::mFill,
|
2018-08-15 13:54:27 +03:00
|
|
|
aImgParams);
|
2016-07-25 19:34:18 +03:00
|
|
|
|
|
|
|
SetFillOpacity(opacity);
|
|
|
|
|
|
|
|
toDraw |= DrawMode::GLYPH_FILL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// stroke:
|
2019-07-06 11:31:02 +03:00
|
|
|
if (style->mStroke.kind.IsNone()) {
|
2016-07-25 19:34:18 +03:00
|
|
|
SetStrokeOpacity(0.0f);
|
|
|
|
} else {
|
|
|
|
float opacity =
|
2020-07-11 05:20:20 +03:00
|
|
|
SVGUtils::GetOpacity(style->mStrokeOpacity, aOuterContextPaint);
|
2016-07-25 19:34:18 +03:00
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
SetupInheritablePaint(aDrawTarget, aContextMatrix, aFrame, opacity,
|
|
|
|
aOuterContextPaint, mStrokePaint,
|
2018-08-15 13:54:27 +03:00
|
|
|
&nsStyleSVG::mStroke, aImgParams);
|
2016-07-25 19:34:18 +03:00
|
|
|
|
|
|
|
SetStrokeOpacity(opacity);
|
|
|
|
|
|
|
|
toDraw |= DrawMode::GLYPH_STROKE;
|
|
|
|
}
|
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
return toDraw;
|
2016-07-25 19:34:18 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 16:56:09 +03:00
|
|
|
void SVGContextPaint::InitStrokeGeometry(gfxContext* aContext,
|
|
|
|
float devUnitsPerSVGUnit) {
|
2016-07-22 14:07:39 +03:00
|
|
|
mStrokeWidth = aContext->CurrentLineWidth() / devUnitsPerSVGUnit;
|
|
|
|
aContext->CurrentDash(mDashes, &mDashOffset);
|
|
|
|
for (uint32_t i = 0; i < mDashes.Length(); i++) {
|
|
|
|
mDashes[i] /= devUnitsPerSVGUnit;
|
|
|
|
}
|
|
|
|
mDashOffset /= devUnitsPerSVGUnit;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
SVGContextPaint* SVGContextPaint::GetContextPaint(nsIContent* aContent) {
|
|
|
|
dom::Document* ownerDoc = aContent->OwnerDoc();
|
2018-04-21 19:48:29 +03:00
|
|
|
if (!ownerDoc->IsSVGDocument()) {
|
2016-07-25 19:34:18 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-13 18:20:36 +03:00
|
|
|
const auto* contextPaint =
|
|
|
|
ownerDoc->AsSVGDocument()->GetCurrentContextPaint();
|
2018-04-21 19:48:29 +03:00
|
|
|
MOZ_ASSERT_IF(contextPaint, ownerDoc->IsBeingUsedAsImage());
|
|
|
|
|
|
|
|
// XXX The SVGContextPaint that SVGDocument keeps around is const. We could
|
|
|
|
// and should keep that constness to the SVGContextPaint that we get here
|
|
|
|
// (SVGImageContext is never changed after it is initialized).
|
|
|
|
//
|
2017-01-09 22:12:03 +03:00
|
|
|
// Unfortunately lazy initialization of SVGContextPaint (which is a member of
|
|
|
|
// SVGImageContext, and also conceptually never changes after construction)
|
|
|
|
// prevents some of SVGContextPaint's conceptually const methods from being
|
|
|
|
// const. Trying to fix SVGContextPaint (perhaps by using |mutable|) is a
|
|
|
|
// bit of a headache so for now we punt on that, don't reapply the constness
|
|
|
|
// to the SVGContextPaint here, and trust that no one will add code that
|
|
|
|
// actually modifies the object.
|
2018-04-21 19:48:29 +03:00
|
|
|
return const_cast<SVGContextPaint*>(contextPaint);
|
2016-07-25 19:34:18 +03:00
|
|
|
}
|
|
|
|
|
2016-07-25 18:04:20 +03:00
|
|
|
already_AddRefed<gfxPattern> SVGContextPaintImpl::GetFillPattern(
|
|
|
|
const DrawTarget* aDrawTarget, float aOpacity, const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) {
|
2017-03-24 22:19:18 +03:00
|
|
|
return mFillPaint.GetPattern(aDrawTarget, aOpacity, &nsStyleSVG::mFill, aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
aImgParams);
|
2016-07-25 15:27:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-25 18:04:20 +03:00
|
|
|
already_AddRefed<gfxPattern> SVGContextPaintImpl::GetStrokePattern(
|
|
|
|
const DrawTarget* aDrawTarget, float aOpacity, const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) {
|
2017-03-24 22:19:18 +03:00
|
|
|
return mStrokePaint.GetPattern(aDrawTarget, aOpacity, &nsStyleSVG::mStroke,
|
2017-05-18 23:03:50 +03:00
|
|
|
aCTM, aImgParams);
|
2016-07-25 15:27:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-25 18:04:20 +03:00
|
|
|
already_AddRefed<gfxPattern> SVGContextPaintImpl::Paint::GetPattern(
|
|
|
|
const DrawTarget* aDrawTarget, float aOpacity,
|
2019-07-06 11:31:02 +03:00
|
|
|
StyleSVGPaint nsStyleSVG::*aFillOrStroke, const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) {
|
2016-07-25 15:27:00 +03:00
|
|
|
RefPtr<gfxPattern> pattern;
|
|
|
|
if (mPatternCache.Get(aOpacity, getter_AddRefs(pattern))) {
|
|
|
|
// Set the pattern matrix just in case it was messed with by a previous
|
|
|
|
// caller. We should get the same matrix each time a pattern is constructed
|
|
|
|
// so this should be fine.
|
|
|
|
pattern->SetMatrix(aCTM * mPatternMatrix);
|
2017-05-18 23:03:50 +03:00
|
|
|
return pattern.forget();
|
2016-07-25 15:27:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (mPaintType) {
|
2019-07-06 11:31:02 +03:00
|
|
|
case Tag::None:
|
2020-03-09 17:16:17 +03:00
|
|
|
pattern = new gfxPattern(DeviceColor());
|
2016-07-25 15:27:00 +03:00
|
|
|
mPatternMatrix = gfxMatrix();
|
|
|
|
break;
|
2019-07-06 11:31:02 +03:00
|
|
|
case Tag::Color: {
|
2020-03-09 17:16:17 +03:00
|
|
|
DeviceColor color = ToDeviceColor(mPaintDefinition.mColor);
|
2017-03-24 22:19:18 +03:00
|
|
|
color.a *= aOpacity;
|
2016-07-25 15:27:00 +03:00
|
|
|
pattern = new gfxPattern(color);
|
|
|
|
mPatternMatrix = gfxMatrix();
|
2017-05-18 23:03:50 +03:00
|
|
|
break;
|
2016-07-25 15:27:00 +03:00
|
|
|
}
|
2019-07-06 11:31:02 +03:00
|
|
|
case Tag::PaintServer:
|
2017-03-21 05:12:23 +03:00
|
|
|
pattern = mPaintDefinition.mPaintServerFrame->GetPaintServerPattern(
|
2017-05-18 23:03:50 +03:00
|
|
|
mFrame, aDrawTarget, mContextMatrix, aFillOrStroke, aOpacity,
|
|
|
|
aImgParams);
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2016-07-25 15:27:00 +03:00
|
|
|
// m maps original-user-space to pattern space
|
|
|
|
gfxMatrix m = pattern->GetMatrix();
|
|
|
|
gfxMatrix deviceToOriginalUserSpace = mContextMatrix;
|
|
|
|
if (!deviceToOriginalUserSpace.Invert()) {
|
2016-07-25 19:34:18 +03:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-25 15:27:00 +03:00
|
|
|
// mPatternMatrix maps device space to pattern space via original user
|
2018-11-30 13:46:48 +03:00
|
|
|
// space
|
2016-07-25 15:27:00 +03:00
|
|
|
mPatternMatrix = deviceToOriginalUserSpace * m;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-25 15:27:00 +03:00
|
|
|
pattern->SetMatrix(aCTM * mPatternMatrix);
|
|
|
|
break;
|
2019-07-06 11:31:02 +03:00
|
|
|
case Tag::ContextFill:
|
2017-03-21 05:12:23 +03:00
|
|
|
pattern = mPaintDefinition.mContextPaint->GetFillPattern(
|
2017-05-18 23:03:50 +03:00
|
|
|
aDrawTarget, aOpacity, aCTM, aImgParams);
|
2016-07-25 15:27:00 +03:00
|
|
|
// Don't cache this. mContextPaint will have cached it anyway. If we
|
|
|
|
// cache it, we'll have to compute mPatternMatrix, which is annoying.
|
2017-05-18 23:03:50 +03:00
|
|
|
return pattern.forget();
|
2019-07-06 11:31:02 +03:00
|
|
|
case Tag::ContextStroke:
|
2017-03-21 05:12:23 +03:00
|
|
|
pattern = mPaintDefinition.mContextPaint->GetStrokePattern(
|
2017-05-18 23:03:50 +03:00
|
|
|
aDrawTarget, aOpacity, aCTM, aImgParams);
|
2016-07-25 15:27:00 +03:00
|
|
|
// Don't cache this. mContextPaint will have cached it anyway. If we
|
|
|
|
// cache it, we'll have to compute mPatternMatrix, which is annoying.
|
2017-05-18 23:03:50 +03:00
|
|
|
return pattern.forget();
|
2016-07-25 15:27:00 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "invalid paint type");
|
2017-05-18 23:03:50 +03:00
|
|
|
return nullptr;
|
2016-07-25 15:27:00 +03:00
|
|
|
}
|
|
|
|
|
2021-02-26 12:11:46 +03:00
|
|
|
mPatternCache.InsertOrUpdate(aOpacity, RefPtr{pattern});
|
2017-05-18 23:03:50 +03:00
|
|
|
return pattern.forget();
|
2016-07-25 15:27:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-22 14:07:39 +03:00
|
|
|
AutoSetRestoreSVGContextPaint::AutoSetRestoreSVGContextPaint(
|
2018-04-21 19:48:29 +03:00
|
|
|
const SVGContextPaint& aContextPaint, dom::SVGDocument& aSVGDocument)
|
2016-07-22 14:07:39 +03:00
|
|
|
: mSVGDocument(aSVGDocument),
|
2018-04-21 19:48:29 +03:00
|
|
|
mOuterContextPaint(aSVGDocument.GetCurrentContextPaint()) {
|
|
|
|
MOZ_ASSERT(aSVGDocument.IsBeingUsedAsImage(),
|
2016-07-25 19:34:18 +03:00
|
|
|
"SVGContextPaint::GetContextPaint assumes this");
|
2016-07-22 14:07:39 +03:00
|
|
|
|
2018-04-21 19:48:29 +03:00
|
|
|
mSVGDocument.SetCurrentContextPaint(&aContextPaint);
|
2016-07-22 14:07:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoSetRestoreSVGContextPaint::~AutoSetRestoreSVGContextPaint() {
|
2018-04-21 19:48:29 +03:00
|
|
|
mSVGDocument.SetCurrentContextPaint(mOuterContextPaint);
|
2016-07-22 14:07:39 +03:00
|
|
|
}
|
2016-07-22 16:56:09 +03:00
|
|
|
|
2017-01-10 13:28:11 +03:00
|
|
|
// SVGEmbeddingContextPaint
|
|
|
|
|
2017-02-24 17:55:28 +03:00
|
|
|
already_AddRefed<gfxPattern> SVGEmbeddingContextPaint::GetFillPattern(
|
|
|
|
const DrawTarget* aDrawTarget, float aFillOpacity, const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) {
|
2017-02-24 17:55:28 +03:00
|
|
|
if (!mFill) {
|
2017-05-18 23:03:50 +03:00
|
|
|
return nullptr;
|
2017-02-24 17:55:28 +03:00
|
|
|
}
|
|
|
|
// The gfxPattern that we create below depends on aFillOpacity, and since
|
|
|
|
// different elements in the SVG image may pass in different values for
|
|
|
|
// fill opacities we don't try to cache the gfxPattern that we create.
|
2020-03-09 17:16:17 +03:00
|
|
|
DeviceColor fill = *mFill;
|
2017-02-24 17:55:28 +03:00
|
|
|
fill.a *= aFillOpacity;
|
2017-05-18 23:03:50 +03:00
|
|
|
return do_AddRef(new gfxPattern(fill));
|
2017-01-10 13:28:11 +03:00
|
|
|
}
|
|
|
|
|
2017-02-24 17:55:28 +03:00
|
|
|
already_AddRefed<gfxPattern> SVGEmbeddingContextPaint::GetStrokePattern(
|
|
|
|
const DrawTarget* aDrawTarget, float aStrokeOpacity, const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) {
|
2017-02-24 17:55:28 +03:00
|
|
|
if (!mStroke) {
|
2017-05-18 23:03:50 +03:00
|
|
|
return nullptr;
|
2017-02-24 17:55:28 +03:00
|
|
|
}
|
2020-03-09 17:16:17 +03:00
|
|
|
DeviceColor stroke = *mStroke;
|
2017-02-24 17:55:28 +03:00
|
|
|
stroke.a *= aStrokeOpacity;
|
2017-05-18 23:03:50 +03:00
|
|
|
return do_AddRef(new gfxPattern(stroke));
|
2017-01-10 13:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SVGEmbeddingContextPaint::Hash() const {
|
|
|
|
uint32_t hash = 0;
|
|
|
|
|
2017-02-24 17:55:28 +03:00
|
|
|
if (mFill) {
|
|
|
|
hash = HashGeneric(hash, mFill->ToABGR());
|
|
|
|
} else {
|
|
|
|
// Arbitrary number, just to avoid trivial hash collisions between pairs of
|
|
|
|
// instances where one embedding context has fill set to the same value as
|
|
|
|
// another context has stroke set to.
|
|
|
|
hash = 1;
|
2017-01-10 13:28:11 +03:00
|
|
|
}
|
2017-02-24 17:55:28 +03:00
|
|
|
|
|
|
|
if (mStroke) {
|
|
|
|
hash = HashGeneric(hash, mStroke->ToABGR());
|
2017-01-10 13:28:11 +03:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:47:20 +03:00
|
|
|
if (mFillOpacity != 1.0f) {
|
|
|
|
hash = HashGeneric(hash, mFillOpacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStrokeOpacity != 1.0f) {
|
|
|
|
hash = HashGeneric(hash, mStrokeOpacity);
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:28:11 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2016-07-22 16:56:09 +03:00
|
|
|
} // namespace mozilla
|