1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <drawinglayer/attribute/fillgradientattribute.hxx>
21 #include <basegfx/utils/gradienttools.hxx>
23 namespace drawinglayer::attribute
25 class ImpFillGradientAttribute
33 basegfx::BColorStops maColorStops
;
34 css::awt::GradientStyle meStyle
;
37 ImpFillGradientAttribute(
38 css::awt::GradientStyle eStyle
,
43 const basegfx::BColorStops
& rColorStops
,
49 maColorStops(rColorStops
), // copy ColorStops
53 // Correct the local ColorStops. That will guarantee that the
54 // content does contain no offsets < 0.0, > 1.0 or double
55 // ones, also secures sorted arrangement and checks for
56 // double colors, too (see there for more information).
57 // This is what the usages of this in primitives need.
58 // Since FillGradientAttribute is read-only doing this
59 // once here in the constructor is sufficient
60 maColorStops
.sortAndCorrect();
62 // sortAndCorrectColorStops is rigid and can return
63 // an empty result. To keep things simple, add a single
65 if (maColorStops
.empty())
67 maColorStops
.emplace_back(0.0, basegfx::BColor());
71 ImpFillGradientAttribute()
77 meStyle(css::awt::GradientStyle_LINEAR
),
80 // always add a fallback color, see above
81 maColorStops
.emplace_back(0.0, basegfx::BColor());
85 css::awt::GradientStyle
getStyle() const { return meStyle
; }
86 double getBorder() const { return mfBorder
; }
87 double getOffsetX() const { return mfOffsetX
; }
88 double getOffsetY() const { return mfOffsetY
; }
89 double getAngle() const { return mfAngle
; }
90 const basegfx::BColorStops
& getColorStops() const { return maColorStops
; }
91 sal_uInt16
getSteps() const { return mnSteps
; }
93 bool operator==(const ImpFillGradientAttribute
& rCandidate
) const
95 return (getStyle() == rCandidate
.getStyle()
96 && getBorder() == rCandidate
.getBorder()
97 && getOffsetX() == rCandidate
.getOffsetX()
98 && getOffsetY() == rCandidate
.getOffsetY()
99 && getAngle() == rCandidate
.getAngle()
100 && getColorStops() == rCandidate
.getColorStops()
101 && getSteps() == rCandidate
.getSteps());
107 FillGradientAttribute::ImplType
& theGlobalDefault()
109 static FillGradientAttribute::ImplType SINGLETON
;
114 FillGradientAttribute::FillGradientAttribute(
115 css::awt::GradientStyle eStyle
,
120 const basegfx::BColorStops
& rColorStops
,
122 : mpFillGradientAttribute(ImpFillGradientAttribute(
123 eStyle
, fBorder
, fOffsetX
, fOffsetY
, fAngle
, rColorStops
, nSteps
))
127 FillGradientAttribute::FillGradientAttribute()
128 : mpFillGradientAttribute(theGlobalDefault())
132 FillGradientAttribute::FillGradientAttribute(const FillGradientAttribute
&) = default;
134 FillGradientAttribute::FillGradientAttribute(FillGradientAttribute
&&) = default;
136 FillGradientAttribute::~FillGradientAttribute() = default;
138 bool FillGradientAttribute::isDefault() const
140 return mpFillGradientAttribute
.same_object(theGlobalDefault());
143 // MCGR: Check if rendering cannot be handled by old vcl stuff
144 bool FillGradientAttribute::cannotBeHandledByVCL() const
146 // MCGR: If GradientStops are used, use decomposition since vcl is not able
147 // to render multi-color gradients
148 if (getColorStops().size() != 2)
153 // MCGR: If GradientStops do not start and stop at traditional Start/EndColor,
154 // use decomposition since vcl is not able to render this
155 if (!getColorStops().empty())
157 if (!basegfx::fTools::equalZero(getColorStops().front().getStopOffset())
158 || !basegfx::fTools::equal(getColorStops().back().getStopOffset(), 1.0))
164 // VCL should be able to handle all styles, but for tdf#133477 the VCL result
165 // is different from processing the gradient manually by drawinglayer
166 // (and the Writer unittest for it fails). Keep using the drawinglayer code
167 // until somebody founds out what's wrong and fixes it.
168 if (getStyle() != css::awt::GradientStyle_LINEAR
169 && getStyle() != css::awt::GradientStyle_AXIAL
170 && getStyle() != css::awt::GradientStyle_RADIAL
)
178 FillGradientAttribute
& FillGradientAttribute::operator=(const FillGradientAttribute
&) = default;
180 FillGradientAttribute
& FillGradientAttribute::operator=(FillGradientAttribute
&&) = default;
182 bool FillGradientAttribute::operator==(const FillGradientAttribute
& rCandidate
) const
184 // tdf#87509 default attr is always != non-default attr, even with same values
185 if(rCandidate
.isDefault() != isDefault())
188 return rCandidate
.mpFillGradientAttribute
== mpFillGradientAttribute
;
191 const basegfx::BColorStops
& FillGradientAttribute::getColorStops() const
193 return mpFillGradientAttribute
->getColorStops();
196 double FillGradientAttribute::getBorder() const
198 return mpFillGradientAttribute
->getBorder();
201 double FillGradientAttribute::getOffsetX() const
203 return mpFillGradientAttribute
->getOffsetX();
206 double FillGradientAttribute::getOffsetY() const
208 return mpFillGradientAttribute
->getOffsetY();
211 double FillGradientAttribute::getAngle() const
213 return mpFillGradientAttribute
->getAngle();
216 css::awt::GradientStyle
FillGradientAttribute::getStyle() const
218 return mpFillGradientAttribute
->getStyle();
221 sal_uInt16
FillGradientAttribute::getSteps() const
223 return mpFillGradientAttribute
->getSteps();
226 } // end of namespace
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */