Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/sdr/attribute/sdrallfillattributeshelper.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
#include <svx/sdr/attribute/sdrallfillattributeshelper.hxx>
20
#include <sdr/primitive2d/sdrattributecreator.hxx>
21
#include <sdr/primitive2d/sdrdecompositiontools.hxx>
22
#include <basegfx/polygon/b2dpolypolygon.hxx>
23
#include <basegfx/polygon/b2dpolygontools.hxx>
24
#include <basegfx/polygon/b2dpolygon.hxx>
25
#include <basegfx/utils/bgradient.hxx>
26
#include <drawinglayer/attribute/fillhatchattribute.hxx>
27
#include <drawinglayer/attribute/sdrfillgraphicattribute.hxx>
28
#include <vcl/graph.hxx>
29
30
//////////////////////////////////////////////////////////////////////////////
31
32
namespace drawinglayer::attribute
33
{
34
        void SdrAllFillAttributesHelper::createPrimitive2DSequence(
35
            const basegfx::B2DRange& rPaintRange,
36
            const basegfx::B2DRange& rDefineRange)
37
476
        {
38
            // reset and remember new target range for object geometry
39
476
            maLastPaintRange = rPaintRange;
40
476
            maLastDefineRange = rDefineRange;
41
42
476
            if(isUsed())
43
476
            {
44
476
                maPrimitives = drawinglayer::primitive2d::Primitive2DContainer {
45
476
                    drawinglayer::primitive2d::createPolyPolygonFillPrimitive(
46
476
                        basegfx::B2DPolyPolygon(
47
476
                            basegfx::utils::createPolygonFromRect(
48
476
                                maLastPaintRange)),
49
476
                            maLastDefineRange,
50
476
                        maFillAttribute ? *maFillAttribute : drawinglayer::attribute::SdrFillAttribute(),
51
476
                        maFillGradientAttribute ? *maFillGradientAttribute : drawinglayer::attribute::FillGradientAttribute()) };
52
476
            }
53
476
        }
54
55
        SdrAllFillAttributesHelper::SdrAllFillAttributesHelper(const Color& rColor)
56
262
        {
57
262
            maFillAttribute = drawinglayer::attribute::SdrFillAttribute(
58
262
                    0.0,
59
262
                    rColor.GetRGBColor().getBColor(),
60
262
                    drawinglayer::attribute::FillGradientAttribute(),
61
262
                    drawinglayer::attribute::FillHatchAttribute(),
62
262
                    drawinglayer::attribute::SdrFillGraphicAttribute());
63
262
        }
64
65
        SdrAllFillAttributesHelper::SdrAllFillAttributesHelper(const SfxItemSet& rSet)
66
37.6k
        :   maFillAttribute(
67
37.6k
                    drawinglayer::primitive2d::createNewSdrFillAttribute(rSet)),
68
37.6k
            maFillGradientAttribute(
69
37.6k
                    drawinglayer::primitive2d::createNewTransparenceGradientAttribute(rSet))
70
37.6k
        {
71
37.6k
        }
72
73
        SdrAllFillAttributesHelper::~SdrAllFillAttributesHelper()
74
37.8k
        {
75
37.8k
        }
76
77
        bool SdrAllFillAttributesHelper::isUsed() const
78
1.88M
        {
79
            // only depends on fill, FillGradientAttribute alone defines no fill
80
1.88M
            return maFillAttribute && !maFillAttribute->isDefault();
81
1.88M
        }
82
83
        bool SdrAllFillAttributesHelper::isTransparent() const
84
98
        {
85
98
            if(hasSdrFillAttribute() && 0.0 != maFillAttribute->getTransparence())
86
5
            {
87
5
                return true;
88
5
            }
89
90
93
            if(maFillGradientAttribute && !maFillGradientAttribute->isDefault())
91
0
            {
92
0
                return true;
93
0
            }
94
95
93
            if(hasSdrFillAttribute())
96
93
            {
97
93
                const Graphic& rGraphic = getFillAttribute().getFillGraphic().getFillGraphic();
98
99
93
                return rGraphic.IsSupportedGraphic() && rGraphic.IsTransparent();
100
93
            }
101
102
0
            return false;
103
93
        }
104
105
        const drawinglayer::attribute::SdrFillAttribute& SdrAllFillAttributesHelper::getFillAttribute() const
106
718
        {
107
718
            if(!maFillAttribute)
108
0
            {
109
0
                const_cast< SdrAllFillAttributesHelper* >(this)->maFillAttribute.emplace();
110
0
            }
111
112
718
            return *maFillAttribute;
113
718
        }
114
115
        const drawinglayer::attribute::FillGradientAttribute& SdrAllFillAttributesHelper::getFillGradientAttribute() const
116
84
        {
117
84
            if(!maFillGradientAttribute)
118
0
            {
119
0
                const_cast< SdrAllFillAttributesHelper* >(this)->maFillGradientAttribute.emplace();
120
0
            }
121
122
84
            return *maFillGradientAttribute;
123
84
        }
124
125
        const drawinglayer::primitive2d::Primitive2DContainer& SdrAllFillAttributesHelper::getPrimitive2DSequence(
126
            const basegfx::B2DRange& rPaintRange,
127
            const basegfx::B2DRange& rDefineRange) const
128
476
        {
129
476
            if(!maPrimitives.empty() && (maLastPaintRange != rPaintRange || maLastDefineRange != rDefineRange))
130
0
            {
131
0
                const_cast< SdrAllFillAttributesHelper* >(this)->maPrimitives.clear();
132
0
            }
133
134
476
            if(maPrimitives.empty())
135
476
            {
136
476
                const_cast< SdrAllFillAttributesHelper* >(this)->createPrimitive2DSequence(rPaintRange, rDefineRange);
137
476
            }
138
139
476
            return maPrimitives;
140
476
        }
141
142
        basegfx::BColor SdrAllFillAttributesHelper::getAverageColor(const basegfx::BColor& rFallback) const
143
84
        {
144
84
            basegfx::BColor aRetval(rFallback);
145
146
84
            if(maFillAttribute && !maFillAttribute->isDefault())
147
84
            {
148
84
                const drawinglayer::attribute::FillGradientAttribute& rFillGradientAttribute = maFillAttribute->getGradient();
149
84
                const drawinglayer::attribute::FillHatchAttribute& rFillHatchAttribute = maFillAttribute->getHatch();
150
84
                const drawinglayer::attribute::SdrFillGraphicAttribute& rSdrFillGraphicAttribute = maFillAttribute->getFillGraphic();
151
84
                const drawinglayer::attribute::FillGradientAttribute& rFillTransparenceGradientAttribute = getFillGradientAttribute();
152
84
                double fTransparence(maFillAttribute->getTransparence());
153
154
84
                if(!rFillTransparenceGradientAttribute.isDefault())
155
0
                {
156
0
                    const double fTransA(rFillTransparenceGradientAttribute.getColorStops().front().getStopColor().luminance());
157
0
                    const double fTransB(rFillTransparenceGradientAttribute.getColorStops().back().getStopColor().luminance());
158
159
0
                    fTransparence = (fTransA + fTransB) * 0.5;
160
0
                }
161
162
84
                if(!rFillGradientAttribute.isDefault())
163
0
                {
164
                    // gradient fill
165
0
                    const basegfx::BColor aStart(rFillGradientAttribute.getColorStops().front().getStopColor());
166
0
                    const basegfx::BColor aEnd(rFillGradientAttribute.getColorStops().back().getStopColor());
167
168
0
                    aRetval = basegfx::interpolate(aStart, aEnd, 0.5);
169
0
                }
170
84
                else if(!rFillHatchAttribute.isDefault())
171
0
                {
172
                    // hatch fill
173
0
                    const basegfx::BColor& rColor = rFillHatchAttribute.getColor();
174
175
0
                    if(rFillHatchAttribute.isFillBackground())
176
0
                    {
177
0
                        const basegfx::BColor& rBackgroundColor = maFillAttribute->getColor();
178
179
                        // mix colors 50%/50%
180
0
                        aRetval = basegfx::interpolate(rColor, rBackgroundColor, 0.5);
181
0
                    }
182
0
                    else
183
0
                    {
184
                        // mix color with fallback color
185
0
                        aRetval = basegfx::interpolate(rColor, rFallback, 0.5);
186
0
                    }
187
0
                }
188
84
                else if(!rSdrFillGraphicAttribute.isDefault())
189
0
                {
190
                    // graphic fill
191
192
                    // not used yet by purpose (see SwPageFrm::GetDrawBackgrdColor()),
193
                    // use fallback (already set)
194
0
                }
195
84
                else
196
84
                {
197
                    // color fill
198
84
                    aRetval = maFillAttribute->getColor();
199
84
                }
200
201
84
                if(!basegfx::fTools::equalZero(fTransparence))
202
6
                {
203
                    // blend into transparency
204
6
                    aRetval = basegfx::interpolate(aRetval, rFallback, fTransparence);
205
6
                }
206
84
            }
207
208
84
            return aRetval.clamp();
209
84
        }
210
211
        bool SdrAllFillAttributesHelper::needCompleteRepaint() const
212
223
        {
213
223
            if(!isUsed() || !hasSdrFillAttribute())
214
0
            {
215
                // not used or no fill
216
0
                return false;
217
0
            }
218
219
223
            const drawinglayer::attribute::SdrFillAttribute& rSdrFillAttribute = getFillAttribute();
220
221
223
            if(!rSdrFillAttribute.getHatch().isDefault())
222
0
            {
223
                // hatch is always top-left aligned, needs no full refreshes
224
0
                return false;
225
0
            }
226
227
223
            if(!rSdrFillAttribute.getGradient().isDefault())
228
0
            {
229
                // gradients always scale with the object
230
0
                return true;
231
0
            }
232
233
223
            if(!rSdrFillAttribute.getFillGraphic().isDefault())
234
0
            {
235
                // some graphic constellations may not need this, but since most do
236
                // (stretch to fill, all but top-left aligned, ...) claim to do by default
237
0
                return true;
238
0
            }
239
240
            // color fill
241
223
            return false;
242
223
        }
243
244
} // end of namespace
245
246
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */