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 .
19 #include <svx/sdr/attribute/sdrallfillattributeshelper.hxx>
20 #include <sdr/primitive2d/sdrattributecreator.hxx>
21 #include <sdr/primitive2d/sdrdecompositiontools.hxx>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
23 #include <basegfx/polygon/b2dpolygon.hxx>
24 #include <basegfx/utils/gradienttools.hxx>
25 #include <drawinglayer/attribute/fillhatchattribute.hxx>
26 #include <drawinglayer/attribute/sdrfillgraphicattribute.hxx>
27 #include <vcl/graph.hxx>
29 //////////////////////////////////////////////////////////////////////////////
31 namespace drawinglayer::attribute
33 void SdrAllFillAttributesHelper::createPrimitive2DSequence(
34 const basegfx::B2DRange
& rPaintRange
,
35 const basegfx::B2DRange
& rDefineRange
)
37 // reset and remember new target range for object geometry
38 maLastPaintRange
= rPaintRange
;
39 maLastDefineRange
= rDefineRange
;
43 maPrimitives
.resize(1);
44 maPrimitives
[0] = drawinglayer::primitive2d::createPolyPolygonFillPrimitive(
45 basegfx::B2DPolyPolygon(
46 basegfx::utils::createPolygonFromRect(
49 maFillAttribute
? *maFillAttribute
: drawinglayer::attribute::SdrFillAttribute(),
50 maFillGradientAttribute
? *maFillGradientAttribute
: drawinglayer::attribute::FillGradientAttribute());
54 SdrAllFillAttributesHelper::SdrAllFillAttributesHelper(const Color
& rColor
)
56 maFillAttribute
= drawinglayer::attribute::SdrFillAttribute(
58 rColor
.GetRGBColor().getBColor(),
59 drawinglayer::attribute::FillGradientAttribute(),
60 drawinglayer::attribute::FillHatchAttribute(),
61 drawinglayer::attribute::SdrFillGraphicAttribute());
64 SdrAllFillAttributesHelper::SdrAllFillAttributesHelper(const SfxItemSet
& rSet
)
66 drawinglayer::primitive2d::createNewSdrFillAttribute(rSet
)),
67 maFillGradientAttribute(
68 drawinglayer::primitive2d::createNewTransparenceGradientAttribute(rSet
))
72 SdrAllFillAttributesHelper::~SdrAllFillAttributesHelper()
76 bool SdrAllFillAttributesHelper::isUsed() const
78 // only depends on fill, FillGradientAttribute alone defines no fill
79 return maFillAttribute
&& !maFillAttribute
->isDefault();
82 bool SdrAllFillAttributesHelper::isTransparent() const
84 if(hasSdrFillAttribute() && 0.0 != maFillAttribute
->getTransparence())
89 if(maFillGradientAttribute
&& !maFillGradientAttribute
->isDefault())
94 if(hasSdrFillAttribute())
96 const Graphic
& rGraphic
= getFillAttribute().getFillGraphic().getFillGraphic();
98 return rGraphic
.IsSupportedGraphic() && rGraphic
.IsTransparent();
104 const drawinglayer::attribute::SdrFillAttribute
& SdrAllFillAttributesHelper::getFillAttribute() const
108 const_cast< SdrAllFillAttributesHelper
* >(this)->maFillAttribute
.emplace();
111 return *maFillAttribute
;
114 const drawinglayer::attribute::FillGradientAttribute
& SdrAllFillAttributesHelper::getFillGradientAttribute() const
116 if(!maFillGradientAttribute
)
118 const_cast< SdrAllFillAttributesHelper
* >(this)->maFillGradientAttribute
.emplace();
121 return *maFillGradientAttribute
;
124 const drawinglayer::primitive2d::Primitive2DContainer
& SdrAllFillAttributesHelper::getPrimitive2DSequence(
125 const basegfx::B2DRange
& rPaintRange
,
126 const basegfx::B2DRange
& rDefineRange
) const
128 if(!maPrimitives
.empty() && (maLastPaintRange
!= rPaintRange
|| maLastDefineRange
!= rDefineRange
))
130 const_cast< SdrAllFillAttributesHelper
* >(this)->maPrimitives
.clear();
133 if(maPrimitives
.empty())
135 const_cast< SdrAllFillAttributesHelper
* >(this)->createPrimitive2DSequence(rPaintRange
, rDefineRange
);
141 basegfx::BColor
SdrAllFillAttributesHelper::getAverageColor(const basegfx::BColor
& rFallback
) const
143 basegfx::BColor
aRetval(rFallback
);
145 if(maFillAttribute
&& !maFillAttribute
->isDefault())
147 const drawinglayer::attribute::FillGradientAttribute
& rFillGradientAttribute
= maFillAttribute
->getGradient();
148 const drawinglayer::attribute::FillHatchAttribute
& rFillHatchAttribute
= maFillAttribute
->getHatch();
149 const drawinglayer::attribute::SdrFillGraphicAttribute
& rSdrFillGraphicAttribute
= maFillAttribute
->getFillGraphic();
150 const drawinglayer::attribute::FillGradientAttribute
& rFillTransparenceGradientAttribute
= getFillGradientAttribute();
151 double fTransparence(maFillAttribute
->getTransparence());
153 if(!rFillTransparenceGradientAttribute
.isDefault())
155 const double fTransA(rFillTransparenceGradientAttribute
.getColorStops().front().getStopColor().luminance());
156 const double fTransB(rFillTransparenceGradientAttribute
.getColorStops().back().getStopColor().luminance());
158 fTransparence
= (fTransA
+ fTransB
) * 0.5;
161 if(!rFillGradientAttribute
.isDefault())
164 const basegfx::BColor
aStart(rFillGradientAttribute
.getColorStops().front().getStopColor());
165 const basegfx::BColor
aEnd(rFillGradientAttribute
.getColorStops().back().getStopColor());
167 aRetval
= basegfx::interpolate(aStart
, aEnd
, 0.5);
169 else if(!rFillHatchAttribute
.isDefault())
172 const basegfx::BColor
& rColor
= rFillHatchAttribute
.getColor();
174 if(rFillHatchAttribute
.isFillBackground())
176 const basegfx::BColor
& rBackgroundColor
= maFillAttribute
->getColor();
178 // mix colors 50%/50%
179 aRetval
= basegfx::interpolate(rColor
, rBackgroundColor
, 0.5);
183 // mix color with fallback color
184 aRetval
= basegfx::interpolate(rColor
, rFallback
, 0.5);
187 else if(!rSdrFillGraphicAttribute
.isDefault())
191 // not used yet by purpose (see SwPageFrm::GetDrawBackgrdColor()),
192 // use fallback (already set)
197 aRetval
= maFillAttribute
->getColor();
200 if(!basegfx::fTools::equalZero(fTransparence
))
202 // blend into transparency
203 aRetval
= basegfx::interpolate(aRetval
, rFallback
, fTransparence
);
207 return aRetval
.clamp();
210 bool SdrAllFillAttributesHelper::needCompleteRepaint() const
212 if(!isUsed() || !hasSdrFillAttribute())
214 // not used or no fill
218 const drawinglayer::attribute::SdrFillAttribute
& rSdrFillAttribute
= getFillAttribute();
220 if(!rSdrFillAttribute
.getHatch().isDefault())
222 // hatch is always top-left aligned, needs no full refreshes
226 if(!rSdrFillAttribute
.getGradient().isDefault())
228 // gradients always scale with the object
232 if(!rSdrFillAttribute
.getFillGraphic().isDefault())
234 // some graphic constellations may not need this, but since most do
235 // (stretch to fill, all but top-left aligned, ...) claim to do by default
243 } // end of namespace
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */