Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / layout / svg / SVGImageContext.cpp
blobc15a4f34f8c35b086227f6ceb66020297013f1a8
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/. */
7 // Main header first:
8 #include "SVGImageContext.h"
10 // Keep others in (case-insensitive) order:
11 #include "gfxUtils.h"
12 #include "mozilla/LookAndFeel.h"
13 #include "mozilla/StaticPrefs_svg.h"
14 #include "mozilla/dom/Document.h"
15 #include "nsIFrame.h"
16 #include "nsPresContext.h"
17 #include "nsStyleStruct.h"
18 #include "nsISVGPaintContext.h"
19 #include "mozilla/ServoCSSParser.h"
21 namespace mozilla {
23 /* static */
24 void SVGImageContext::MaybeStoreContextPaint(SVGImageContext& aContext,
25 nsIFrame* aFromFrame,
26 imgIContainer* aImgContainer) {
27 return MaybeStoreContextPaint(aContext, *aFromFrame->PresContext(),
28 *aFromFrame->Style(), aImgContainer);
31 /* static */
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.
38 return;
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.
53 return;
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()
75 : 1.0f);
77 if (style->mMozContextProperties.bits &
78 StyleContextPropertyBits::STROKE_OPACITY) {
79 haveContextPaint = true;
80 contextPaint->SetStrokeOpacity(style->mStrokeOpacity.IsOpacity()
81 ? style->mStrokeOpacity.AsOpacity()
82 : 1.0f);
85 if (haveContextPaint) {
86 aContext.mContextPaint = std::move(contextPaint);
90 /* static */
91 void SVGImageContext::MaybeStoreContextPaint(SVGImageContext& aContext,
92 nsISVGPaintContext* aPaintContext,
93 imgIContainer* aImgContainer) {
94 if (aImgContainer->GetType() != imgIContainer::TYPE_VECTOR ||
95 !aPaintContext) {
96 // Avoid this overhead for raster images.
97 return;
100 bool haveContextPaint = false;
101 auto contextPaint = MakeRefPtr<SVGEmbeddingContextPaint>();
102 nsCString value;
103 float opacity;
105 if (NS_SUCCEEDED(aPaintContext->GetStrokeColor(value)) && !value.IsEmpty()) {
106 nscolor color;
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()) {
114 nscolor color;
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