1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "SVGImageContext.h"
10 // Keep others in (case-insensitive) order:
12 #include "mozilla/LookAndFeel.h"
13 #include "mozilla/StaticPrefs_svg.h"
14 #include "mozilla/dom/Document.h"
16 #include "nsPresContext.h"
17 #include "nsStyleStruct.h"
18 #include "nsISVGPaintContext.h"
19 #include "mozilla/ServoCSSParser.h"
24 void SVGImageContext::MaybeStoreContextPaint(SVGImageContext
& aContext
,
26 imgIContainer
* aImgContainer
) {
27 return MaybeStoreContextPaint(aContext
, *aFromFrame
->PresContext(),
28 *aFromFrame
->Style(), aImgContainer
);
32 void SVGImageContext::MaybeStoreContextPaint(SVGImageContext
& aContext
,
33 const nsPresContext
& aPresContext
,
34 const ComputedStyle
& aStyle
,
35 imgIContainer
* aImgContainer
) {
36 if (aImgContainer
->GetType() != imgIContainer::TYPE_VECTOR
) {
37 // Avoid this overhead for raster images.
41 if (StaticPrefs::svg_embedder_prefers_color_scheme_content_enabled() ||
42 aPresContext
.Document()->ChromeRulesEnabled()) {
43 auto scheme
= LookAndFeel::ColorSchemeForStyle(
44 *aPresContext
.Document(), aStyle
.StyleUI()->mColorScheme
.bits
,
45 ColorSchemeMode::Preferred
);
46 aContext
.SetColorScheme(Some(scheme
));
49 const nsStyleSVG
* style
= aStyle
.StyleSVG();
50 if (!style
->ExposesContextProperties()) {
51 // Content must have '-moz-context-properties' set to the names of the
52 // properties it wants to expose to images it links to.
56 bool haveContextPaint
= false;
58 auto contextPaint
= MakeRefPtr
<SVGEmbeddingContextPaint
>();
60 if ((style
->mMozContextProperties
.bits
& StyleContextPropertyBits::FILL
) &&
61 style
->mFill
.kind
.IsColor()) {
62 haveContextPaint
= true;
63 contextPaint
->SetFill(style
->mFill
.kind
.AsColor().CalcColor(aStyle
));
65 if ((style
->mMozContextProperties
.bits
& StyleContextPropertyBits::STROKE
) &&
66 style
->mStroke
.kind
.IsColor()) {
67 haveContextPaint
= true;
68 contextPaint
->SetStroke(style
->mStroke
.kind
.AsColor().CalcColor(aStyle
));
70 if (style
->mMozContextProperties
.bits
&
71 StyleContextPropertyBits::FILL_OPACITY
) {
72 haveContextPaint
= true;
73 contextPaint
->SetFillOpacity(style
->mFillOpacity
.IsOpacity()
74 ? style
->mFillOpacity
.AsOpacity()
77 if (style
->mMozContextProperties
.bits
&
78 StyleContextPropertyBits::STROKE_OPACITY
) {
79 haveContextPaint
= true;
80 contextPaint
->SetStrokeOpacity(style
->mStrokeOpacity
.IsOpacity()
81 ? style
->mStrokeOpacity
.AsOpacity()
85 if (haveContextPaint
) {
86 aContext
.mContextPaint
= std::move(contextPaint
);
91 void SVGImageContext::MaybeStoreContextPaint(SVGImageContext
& aContext
,
92 nsISVGPaintContext
* aPaintContext
,
93 imgIContainer
* aImgContainer
) {
94 if (aImgContainer
->GetType() != imgIContainer::TYPE_VECTOR
||
96 // Avoid this overhead for raster images.
100 bool haveContextPaint
= false;
101 auto contextPaint
= MakeRefPtr
<SVGEmbeddingContextPaint
>();
105 if (NS_SUCCEEDED(aPaintContext
->GetStrokeColor(value
)) && !value
.IsEmpty()) {
107 if (ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value
, &color
)) {
108 haveContextPaint
= true;
109 contextPaint
->SetStroke(color
);
113 if (NS_SUCCEEDED(aPaintContext
->GetFillColor(value
)) && !value
.IsEmpty()) {
115 if (ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), value
, &color
)) {
116 haveContextPaint
= true;
117 contextPaint
->SetFill(color
);
121 if (NS_SUCCEEDED(aPaintContext
->GetStrokeOpacity(&opacity
))) {
122 haveContextPaint
= true;
123 contextPaint
->SetStrokeOpacity(opacity
);
126 if (NS_SUCCEEDED(aPaintContext
->GetFillOpacity(&opacity
))) {
127 haveContextPaint
= true;
128 contextPaint
->SetFillOpacity(opacity
);
131 if (haveContextPaint
) {
132 aContext
.mContextPaint
= std::move(contextPaint
);
136 } // namespace mozilla