Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/drawinglayer/source/primitive2d/glowprimitive2d.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
20
#include <drawinglayer/primitive2d/glowprimitive2d.hxx>
21
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
22
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
23
#include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
24
#include <basegfx/matrix/b2dhommatrixtools.hxx>
25
#include <drawinglayer/converters.hxx>
26
#include <vcl/graph.hxx>
27
#include "GlowSoftEgdeShadowTools.hxx"
28
29
#ifdef DBG_UTIL
30
#include <o3tl/environment.hxx>
31
#include <tools/stream.hxx>
32
#include <vcl/filter/PngImageWriter.hxx>
33
#endif
34
35
using namespace com::sun::star;
36
37
namespace drawinglayer::primitive2d
38
{
39
GlowPrimitive2D::GlowPrimitive2D(const Color& rGlowColor, double fRadius,
40
                                 Primitive2DContainer&& rChildren)
41
0
    : BufferedDecompositionGroupPrimitive2D(std::move(rChildren))
42
0
    , maGlowColor(rGlowColor)
43
0
    , mfGlowRadius(fRadius)
44
0
    , mfLastDiscreteGlowRadius(0.0)
45
0
    , maLastClippedRange()
46
0
{
47
    // activate callback to flush buffered decomposition content
48
0
    activateFlushOnTimer();
49
0
}
50
51
bool GlowPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
52
0
{
53
0
    if (BufferedDecompositionGroupPrimitive2D::operator==(rPrimitive))
54
0
    {
55
0
        const GlowPrimitive2D& rCompare = static_cast<const GlowPrimitive2D&>(rPrimitive);
56
57
0
        return (getGlowRadius() == rCompare.getGlowRadius()
58
0
                && getGlowColor() == rCompare.getGlowColor());
59
0
    }
60
61
0
    return false;
62
0
}
63
64
bool GlowPrimitive2D::prepareValuesAndcheckValidity(
65
    basegfx::B2DRange& rGlowRange, basegfx::B2DRange& rClippedRange,
66
    basegfx::B2DVector& rDiscreteGlowSize, double& rfDiscreteGlowRadius,
67
    const geometry::ViewInformation2D& rViewInformation) const
68
0
{
69
    // no GlowRadius defined, done
70
0
    if (getGlowRadius() <= 0.0)
71
0
        return false;
72
73
    // no geometry, done
74
0
    if (getChildren().empty())
75
0
        return false;
76
77
    // no pixel target, done
78
0
    if (rViewInformation.getObjectToViewTransformation().isIdentity())
79
0
        return false;
80
81
    // get geometry range that defines area that needs to be pixelated
82
0
    rGlowRange = getChildren().getB2DRange(rViewInformation);
83
84
    // no range of geometry, done
85
0
    if (rGlowRange.isEmpty())
86
0
        return false;
87
88
    // extend range by GlowRadius in all directions
89
0
    rGlowRange.grow(getGlowRadius());
90
91
    // initialize ClippedRange to full GlowRange -> all is visible
92
0
    rClippedRange = rGlowRange;
93
94
    // get Viewport and check if used. If empty, all is visible (see
95
    // ViewInformation2D definition in viewinformation2d.hxx)
96
0
    if (!rViewInformation.getViewport().isEmpty())
97
0
    {
98
        // if used, extend by GlowRadius to ensure needed parts are included
99
0
        basegfx::B2DRange aVisibleArea(rViewInformation.getViewport());
100
0
        aVisibleArea.grow(getGlowRadius());
101
102
        // To do this correctly, it needs to be done in discrete coordinates.
103
        // The object may be transformed relative to the original#
104
        // ObjectTransformation, e.g. when re-used in shadow
105
0
        aVisibleArea.transform(rViewInformation.getViewTransformation());
106
0
        rClippedRange.transform(rViewInformation.getObjectToViewTransformation());
107
108
        // calculate ClippedRange
109
0
        rClippedRange.intersect(aVisibleArea);
110
111
        // if GlowRange is completely outside of VisibleArea, ClippedRange
112
        // will be empty and we are done
113
0
        if (rClippedRange.isEmpty())
114
0
            return false;
115
116
        // convert result back to object coordinates
117
0
        rClippedRange.transform(rViewInformation.getInverseObjectToViewTransformation());
118
0
    }
119
120
    // calculate discrete pixel size of GlowRange. If it's too small to visualize, we are done
121
0
    rDiscreteGlowSize = rViewInformation.getObjectToViewTransformation() * rGlowRange.getRange();
122
0
    if (ceil(rDiscreteGlowSize.getX()) < 2.0 || ceil(rDiscreteGlowSize.getY()) < 2.0)
123
0
        return false;
124
125
    // calculate discrete pixel size of GlowRadius. If it's too small to visualize, we are done
126
0
    rfDiscreteGlowRadius = ceil(
127
0
        (rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(getGlowRadius(), 0))
128
0
            .getLength());
129
0
    if (rfDiscreteGlowRadius < 1.0)
130
0
        return false;
131
132
0
    return true;
133
0
}
134
135
void GlowPrimitive2D::create2DDecomposition(
136
    Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const
137
0
{
138
0
    basegfx::B2DRange aGlowRange;
139
0
    basegfx::B2DRange aClippedRange;
140
0
    basegfx::B2DVector aDiscreteGlowSize;
141
0
    double fDiscreteGlowRadius(0.0);
142
143
    // Check various validity details and calculate/prepare values. If false, we are done
144
0
    if (!prepareValuesAndcheckValidity(aGlowRange, aClippedRange, aDiscreteGlowSize,
145
0
                                       fDiscreteGlowRadius, rViewInformation))
146
0
        return;
147
148
    // Create embedding transformation from object to top-left zero-aligned
149
    // target pixel geometry (discrete form of ClippedRange)
150
    // First, move to top-left of GlowRange
151
0
    const sal_uInt32 nDiscreteGlowWidth(ceil(aDiscreteGlowSize.getX()));
152
0
    const sal_uInt32 nDiscreteGlowHeight(ceil(aDiscreteGlowSize.getY()));
153
0
    basegfx::B2DHomMatrix aEmbedding(basegfx::utils::createTranslateB2DHomMatrix(
154
0
        -aClippedRange.getMinX(), -aClippedRange.getMinY()));
155
    // Second, scale to discrete bitmap size
156
    // Even when using the offset from ClippedRange, we need to use the
157
    // scaling from the full representation, thus from GlowRange
158
0
    aEmbedding.scale(nDiscreteGlowWidth / aGlowRange.getWidth(),
159
0
                     nDiscreteGlowHeight / aGlowRange.getHeight());
160
161
    // Embed content graphics to TransformPrimitive2D
162
0
    const primitive2d::Primitive2DReference xEmbedRef(
163
0
        new primitive2d::TransformPrimitive2D(aEmbedding, Primitive2DContainer(getChildren())));
164
0
    primitive2d::Primitive2DContainer xEmbedSeq{ xEmbedRef };
165
166
    // Create Bitmap using drawinglayer tooling, including a MaximumQuadraticPixel
167
    // limitation to be safe and not go runtime/memory havoc. Use a pretty small
168
    // limit due to this is glow functionality and will look good with bitmap scaling
169
    // anyways. The value of 250.000 square pixels below maybe adapted as needed.
170
0
    const basegfx::B2DVector aDiscreteClippedSize(rViewInformation.getObjectToViewTransformation()
171
0
                                                  * aClippedRange.getRange());
172
0
    const sal_uInt32 nDiscreteClippedWidth(ceil(aDiscreteClippedSize.getX()));
173
0
    const sal_uInt32 nDiscreteClippedHeight(ceil(aDiscreteClippedSize.getY()));
174
0
    const geometry::ViewInformation2D aViewInformation2D;
175
0
    const sal_uInt32 nMaximumQuadraticPixels(250000);
176
177
    // I have now added a helper that just creates the mask without having
178
    // to render the content, use it, it's faster
179
0
    const AlphaMask aAlpha(::drawinglayer::createAlphaMask(
180
0
        std::move(xEmbedSeq), aViewInformation2D, nDiscreteClippedWidth, nDiscreteClippedHeight,
181
0
        nMaximumQuadraticPixels));
182
183
0
    if (aAlpha.IsEmpty())
184
0
        return;
185
186
0
    const Size aBitmapExSizePixel(aAlpha.GetSizePixel());
187
188
0
    if (aBitmapExSizePixel.Width() <= 0 || aBitmapExSizePixel.Height() <= 0)
189
0
        return;
190
191
    // We may have to take a corrective scaling into account when the
192
    // MaximumQuadraticPixel limit was used/triggered
193
0
    double fScale(1.0);
194
195
0
    if (static_cast<sal_uInt32>(aBitmapExSizePixel.Width()) != nDiscreteClippedWidth
196
0
        || static_cast<sal_uInt32>(aBitmapExSizePixel.Height()) != nDiscreteClippedHeight)
197
0
    {
198
        // scale in X and Y should be the same (see fReduceFactor in createAlphaMask),
199
        // so adapt numerically to a single scale value, they are integer rounded values
200
0
        const double fScaleX(static_cast<double>(aBitmapExSizePixel.Width())
201
0
                             / static_cast<double>(nDiscreteClippedWidth));
202
0
        const double fScaleY(static_cast<double>(aBitmapExSizePixel.Height())
203
0
                             / static_cast<double>(nDiscreteClippedHeight));
204
205
0
        fScale = (fScaleX + fScaleY) * 0.5;
206
0
    }
207
208
    // fDiscreteGlowRadius is the size of the halo from each side of the object. The halo is the
209
    // border of glow color that fades from glow transparency level to fully transparent
210
    // When blurring a sharp boundary (our case), it gets 50% of original intensity, and
211
    // fades to both sides by the blur radius; thus blur radius is half of glow radius.
212
    // Consider glow transparency (initial transparency near the object edge)
213
0
    AlphaMask mask(ProcessAndBlurAlphaMask(aAlpha, fDiscreteGlowRadius * fScale / 2.0,
214
0
                                           fDiscreteGlowRadius * fScale / 2.0,
215
0
                                           255 - getGlowColor().GetAlpha()));
216
217
    // The end result is the bitmap filled with glow color and blurred 8-bit alpha mask
218
0
    Bitmap bmp(aAlpha.GetSizePixel(), vcl::PixelFormat::N24_BPP);
219
0
    bmp.Erase(getGlowColor());
220
0
    Bitmap result(bmp, mask);
221
222
#ifdef DBG_UTIL
223
    static bool bDoSaveForVisualControl(false); // loplugin:constvars:ignore
224
    if (bDoSaveForVisualControl)
225
    {
226
        // VCL_DUMP_BMP_PATH should be like C:/path/ or ~/path/
227
        static const OUString sDumpPath(o3tl::getEnvironment(u"VCL_DUMP_BMP_PATH"_ustr));
228
        if (!sDumpPath.isEmpty())
229
        {
230
            SvFileStream aNew(sDumpPath + "test_glow.png", StreamMode::WRITE | StreamMode::TRUNC);
231
            vcl::PngImageWriter aPNGWriter(aNew);
232
            aPNGWriter.write(result);
233
        }
234
    }
235
#endif
236
237
    // Independent from discrete sizes of glow alpha creation, always
238
    // map and project glow result to geometry range extended by glow
239
    // radius, but to the eventually clipped instance (ClippedRange)
240
0
    const primitive2d::Primitive2DReference xEmbedRefBitmap(
241
0
        new BitmapPrimitive2D(result, basegfx::utils::createScaleTranslateB2DHomMatrix(
242
0
                                          aClippedRange.getWidth(), aClippedRange.getHeight(),
243
0
                                          aClippedRange.getMinX(), aClippedRange.getMinY())));
244
245
0
    rContainer = primitive2d::Primitive2DContainer{ xEmbedRefBitmap };
246
0
}
247
248
// Using tooling class BufferedDecompositionGroupPrimitive2D now, so
249
// no more need to locally do the buffered get2DDecomposition here,
250
// see BufferedDecompositionGroupPrimitive2D::get2DDecomposition
251
void GlowPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor,
252
                                         const geometry::ViewInformation2D& rViewInformation) const
253
0
{
254
0
    basegfx::B2DRange aGlowRange;
255
0
    basegfx::B2DRange aClippedRange;
256
0
    basegfx::B2DVector aDiscreteGlowSize;
257
0
    double fDiscreteGlowRadius(0.0);
258
259
    // Check various validity details and calculate/prepare values. If false, we are done
260
0
    if (!prepareValuesAndcheckValidity(aGlowRange, aClippedRange, aDiscreteGlowSize,
261
0
                                       fDiscreteGlowRadius, rViewInformation))
262
0
        return;
263
264
0
    if (hasBuffered2DDecomposition())
265
0
    {
266
        // First check is to detect if the last created decompose is capable
267
        // to represent the now requested visualization.
268
        // ClippedRange is the needed visualizationArea for the current glow
269
        // effect, LastClippedRange is the one from the existing/last rendering.
270
        // Check if last created area is sufficient and can be re-used
271
0
        if (!maLastClippedRange.isEmpty() && !maLastClippedRange.isInside(aClippedRange))
272
0
        {
273
            // To avoid unnecessary invalidations due to being *very* correct
274
            // with HairLines (which are view-dependent and thus change the
275
            // result(s) here slightly when changing zoom), add a slight unsharp
276
            // component if we have a ViewTransform. The derivation is inside
277
            // the range of half a pixel (due to one pixel hairline)
278
0
            basegfx::B2DRange aLastClippedRangeAndHairline(maLastClippedRange);
279
280
0
            if (!rViewInformation.getObjectToViewTransformation().isIdentity())
281
0
            {
282
                // Grow by view-dependent size of 1/2 pixel
283
0
                const double fHalfPixel((rViewInformation.getInverseObjectToViewTransformation()
284
0
                                         * basegfx::B2DVector(0.5, 0))
285
0
                                            .getLength());
286
0
                aLastClippedRangeAndHairline.grow(fHalfPixel);
287
0
            }
288
289
0
            if (!aLastClippedRangeAndHairline.isInside(aClippedRange))
290
0
            {
291
                // Conditions of last local decomposition have changed, delete
292
0
                const_cast<GlowPrimitive2D*>(this)->setBuffered2DDecomposition(
293
0
                    Primitive2DContainer());
294
0
            }
295
0
        }
296
0
    }
297
298
0
    if (hasBuffered2DDecomposition())
299
0
    {
300
        // Second check is to react on changes of the DiscreteGlowRadius when
301
        // zooming in/out.
302
        // Use the known last and current DiscreteGlowRadius to decide
303
        // if the visualization can be re-used. Be a little 'creative' here
304
        // and make it dependent on a *relative* change - it is not necessary
305
        // to re-create everytime if the exact value is missed since zooming
306
        // pixel-based glow effect is pretty good due to it's smooth nature
307
0
        bool bFree(mfLastDiscreteGlowRadius <= 0.0 || fDiscreteGlowRadius <= 0.0);
308
309
0
        if (!bFree)
310
0
        {
311
0
            const double fDiff(fabs(mfLastDiscreteGlowRadius - fDiscreteGlowRadius));
312
0
            const double fLen(fabs(mfLastDiscreteGlowRadius) + fabs(fDiscreteGlowRadius));
313
0
            const double fRelativeChange(fDiff / fLen);
314
315
            // Use lower fixed values here to change more often, higher to change less often.
316
            // Value is in the range of ]0.0 .. 1.0]
317
0
            bFree = fRelativeChange >= 0.15;
318
0
        }
319
320
0
        if (bFree)
321
0
        {
322
            // Conditions of last local decomposition have changed, delete
323
0
            const_cast<GlowPrimitive2D*>(this)->setBuffered2DDecomposition(Primitive2DContainer());
324
0
        }
325
0
    }
326
327
0
    if (!hasBuffered2DDecomposition())
328
0
    {
329
        // refresh last used DiscreteGlowRadius and ClippedRange to new remembered values
330
0
        const_cast<GlowPrimitive2D*>(this)->mfLastDiscreteGlowRadius = fDiscreteGlowRadius;
331
0
        const_cast<GlowPrimitive2D*>(this)->maLastClippedRange = aClippedRange;
332
0
    }
333
334
    // call parent, that will check for empty, call create2DDecomposition and
335
    // set as decomposition
336
0
    BufferedDecompositionGroupPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
337
0
}
338
339
basegfx::B2DRange
340
GlowPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
341
0
{
342
    // Hint: Do *not* use GroupPrimitive2D::getB2DRange, that will (unnecessarily)
343
    // use the decompose - what works, but is not needed here.
344
    // We know the to-be-visualized geometry and the radius it needs to be extended,
345
    // so simply calculate the exact needed range.
346
0
    basegfx::B2DRange aRetval(getChildren().getB2DRange(rViewInformation));
347
348
    // We need additional space for the glow from all sides
349
0
    aRetval.grow(getGlowRadius());
350
351
0
    return aRetval;
352
0
}
353
354
// provide unique ID
355
0
sal_uInt32 GlowPrimitive2D::getPrimitive2DID() const { return PRIMITIVE2D_ID_GLOWPRIMITIVE2D; }
356
357
} // end of namespace
358
359
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */