Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / sdr / attribute / sdrallfillattributeshelper.cxx
blob6a96870a4b7930bb6c15c6f861ff4cce34ca5750
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
41 if(isUsed())
43 maPrimitives.resize(1);
44 maPrimitives[0] = drawinglayer::primitive2d::createPolyPolygonFillPrimitive(
45 basegfx::B2DPolyPolygon(
46 basegfx::utils::createPolygonFromRect(
47 maLastPaintRange)),
48 maLastDefineRange,
49 maFillAttribute ? *maFillAttribute : drawinglayer::attribute::SdrFillAttribute(),
50 maFillGradientAttribute ? *maFillGradientAttribute : drawinglayer::attribute::FillGradientAttribute());
54 SdrAllFillAttributesHelper::SdrAllFillAttributesHelper(const Color& rColor)
56 maFillAttribute = drawinglayer::attribute::SdrFillAttribute(
57 0.0,
58 rColor.GetRGBColor().getBColor(),
59 drawinglayer::attribute::FillGradientAttribute(),
60 drawinglayer::attribute::FillHatchAttribute(),
61 drawinglayer::attribute::SdrFillGraphicAttribute());
64 SdrAllFillAttributesHelper::SdrAllFillAttributesHelper(const SfxItemSet& rSet)
65 : maFillAttribute(
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())
86 return true;
89 if(maFillGradientAttribute && !maFillGradientAttribute->isDefault())
91 return true;
94 if(hasSdrFillAttribute())
96 const Graphic& rGraphic = getFillAttribute().getFillGraphic().getFillGraphic();
98 return rGraphic.IsSupportedGraphic() && rGraphic.IsTransparent();
101 return false;
104 const drawinglayer::attribute::SdrFillAttribute& SdrAllFillAttributesHelper::getFillAttribute() const
106 if(!maFillAttribute)
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);
138 return maPrimitives;
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())
163 // gradient fill
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())
171 // hatch fill
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);
181 else
183 // mix color with fallback color
184 aRetval = basegfx::interpolate(rColor, rFallback, 0.5);
187 else if(!rSdrFillGraphicAttribute.isDefault())
189 // graphic fill
191 // not used yet by purpose (see SwPageFrm::GetDrawBackgrdColor()),
192 // use fallback (already set)
194 else
196 // color fill
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
215 return false;
218 const drawinglayer::attribute::SdrFillAttribute& rSdrFillAttribute = getFillAttribute();
220 if(!rSdrFillAttribute.getHatch().isDefault())
222 // hatch is always top-left aligned, needs no full refreshes
223 return false;
226 if(!rSdrFillAttribute.getGradient().isDefault())
228 // gradients always scale with the object
229 return true;
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
236 return true;
239 // color fill
240 return false;
243 } // end of namespace
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */