Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/drawinglayer/source/primitive2d/patternfillprimitive2d.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/patternfillprimitive2d.hxx>
21
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
22
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
23
#include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
24
#include <basegfx/polygon/b2dpolygontools.hxx>
25
#include <basegfx/matrix/b2dhommatrixtools.hxx>
26
#include <texture/texture.hxx>
27
#include <drawinglayer/primitive2d/maskprimitive2d.hxx>
28
#include <drawinglayer/geometry/viewinformation2d.hxx>
29
30
#include <drawinglayer/converters.hxx>
31
#include <utility>
32
33
using namespace com::sun::star;
34
35
0
#define MAXIMUM_SQUARE_LENGTH (186.0)
36
0
#define MINIMUM_SQUARE_LENGTH (16.0)
37
0
#define MINIMUM_TILES_LENGTH (3)
38
39
namespace drawinglayer::primitive2d
40
{
41
        void PatternFillPrimitive2D::calculateNeededDiscreteBufferSize(
42
            sal_uInt32& rWidth,
43
            sal_uInt32& rHeight,
44
            const geometry::ViewInformation2D& rViewInformation) const
45
0
        {
46
            // reset parameters
47
0
            rWidth = rHeight = 0;
48
49
            // check if resolution is in the range which may be buffered
50
0
            const basegfx::B2DPolyPolygon& rMaskPolygon = getMask();
51
0
            const basegfx::B2DRange aMaskRange(rMaskPolygon.getB2DRange());
52
53
            // get discrete rounded up square size of a single tile
54
0
            const basegfx::B2DHomMatrix aMaskRangeTransformation(
55
0
                basegfx::utils::createScaleTranslateB2DHomMatrix(
56
0
                    aMaskRange.getRange(),
57
0
                    aMaskRange.getMinimum()));
58
0
            const basegfx::B2DHomMatrix aTransform(
59
0
                rViewInformation.getObjectToViewTransformation() * aMaskRangeTransformation);
60
0
            const basegfx::B2DPoint aTopLeft(aTransform * getReferenceRange().getMinimum());
61
0
            const basegfx::B2DPoint aX(aTransform * basegfx::B2DPoint(getReferenceRange().getMaxX(), getReferenceRange().getMinY()));
62
0
            const basegfx::B2DPoint aY(aTransform * basegfx::B2DPoint(getReferenceRange().getMinX(), getReferenceRange().getMaxY()));
63
0
            const double fW(basegfx::B2DVector(aX - aTopLeft).getLength());
64
0
            const double fH(basegfx::B2DVector(aY - aTopLeft).getLength());
65
0
            const double fSquare(fW * fH);
66
67
0
            if(fSquare <= 0.0)
68
0
                return;
69
70
            // check if less than a maximum square pixels is used
71
0
            static const sal_uInt32 fMaximumSquare(MAXIMUM_SQUARE_LENGTH * MAXIMUM_SQUARE_LENGTH);
72
73
0
            if(fSquare >= fMaximumSquare)
74
0
                return;
75
76
            // calculate needed number of tiles and check if used more than a minimum count
77
0
            const texture::GeoTexSvxTiled aTiling(getReferenceRange());
78
0
            const sal_uInt32 nTiles(aTiling.getNumberOfTiles());
79
0
            static const sal_uInt32 nMinimumTiles(MINIMUM_TILES_LENGTH * MINIMUM_TILES_LENGTH);
80
81
0
            if(nTiles < nMinimumTiles)
82
0
                return;
83
84
0
            rWidth = basegfx::fround(ceil(fW));
85
0
            rHeight = basegfx::fround(ceil(fH));
86
0
            static const sal_uInt32 fMinimumSquare(MINIMUM_SQUARE_LENGTH * MINIMUM_SQUARE_LENGTH);
87
88
0
            if(fSquare < fMinimumSquare)
89
0
            {
90
0
                const double fRel(fW/fH);
91
0
                rWidth = basegfx::fround(sqrt(fMinimumSquare * fRel));
92
0
                rHeight = basegfx::fround(sqrt(fMinimumSquare / fRel));
93
0
            }
94
0
        }
95
96
        void PatternFillPrimitive2D::getTileSize(
97
            sal_uInt32& rWidth,
98
            sal_uInt32& rHeight,
99
            const geometry::ViewInformation2D& rViewInformation) const
100
0
        {
101
0
            const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
102
103
            // get discrete rounded up square size of a single tile
104
0
            const basegfx::B2DHomMatrix aMaskRangeTransformation(
105
0
                basegfx::utils::createScaleTranslateB2DHomMatrix(
106
0
                    aMaskRange.getRange(),
107
0
                    aMaskRange.getMinimum()));
108
0
            const basegfx::B2DHomMatrix aTransform(
109
0
                rViewInformation.getObjectToViewTransformation() * aMaskRangeTransformation);
110
0
            const basegfx::B2DPoint aTopLeft(aTransform * getReferenceRange().getMinimum());
111
0
            const basegfx::B2DPoint aX(aTransform * basegfx::B2DPoint(getReferenceRange().getMaxX(), getReferenceRange().getMinY()));
112
0
            const basegfx::B2DPoint aY(aTransform * basegfx::B2DPoint(getReferenceRange().getMinX(), getReferenceRange().getMaxY()));
113
0
            const double fW(basegfx::B2DVector(aX - aTopLeft).getLength());
114
0
            const double fH(basegfx::B2DVector(aY - aTopLeft).getLength());
115
116
0
            rWidth = basegfx::fround(ceil(fW));
117
0
            rHeight = basegfx::fround(ceil(fH));
118
0
        }
119
120
        Primitive2DContainer PatternFillPrimitive2D::createContent(const geometry::ViewInformation2D& rViewInformation) const
121
0
        {
122
0
            Primitive2DContainer aContent;
123
124
            // see if buffering is wanted. If so, create buffered content in given resolution
125
0
            if(0 != mnDiscreteWidth && 0 != mnDiscreteHeight)
126
0
            {
127
0
                const geometry::ViewInformation2D aViewInformation2D;
128
0
                primitive2d::Primitive2DContainer xEmbedSeq {
129
0
                        new primitive2d::TransformPrimitive2D(
130
0
                            basegfx::utils::createScaleB2DHomMatrix(mnDiscreteWidth, mnDiscreteHeight),
131
0
                            Primitive2DContainer(getChildren()))
132
0
                };
133
134
0
                const Bitmap aBitmap(
135
0
                    convertToBitmap(
136
0
                        std::move(xEmbedSeq),
137
0
                        aViewInformation2D,
138
0
                        mnDiscreteWidth,
139
0
                        mnDiscreteHeight,
140
0
                        mnDiscreteWidth * mnDiscreteHeight));
141
142
0
                if(!aBitmap.IsEmpty())
143
0
                {
144
0
                    const Size aBmpPix = aBitmap.GetSizePixel();
145
146
0
                    if(aBmpPix.Width() > 0 && aBmpPix.Height() > 0)
147
0
                    {
148
0
                        const primitive2d::Primitive2DReference xEmbedRefBitmap(
149
0
                            new primitive2d::BitmapPrimitive2D(
150
0
                                aBitmap,
151
0
                                basegfx::B2DHomMatrix()));
152
0
                        aContent = primitive2d::Primitive2DContainer { xEmbedRefBitmap };
153
0
                    }
154
0
                }
155
0
            }
156
157
0
            if(aContent.empty())
158
0
            {
159
                // buffering was not tried or did fail - reset remembered buffered size
160
                // in any case
161
0
                PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
162
0
                pThat->mnDiscreteWidth = pThat->mnDiscreteHeight = 0;
163
164
                // use children as default context
165
0
                aContent = getChildren();
166
167
                // check if content needs to be clipped
168
0
                const basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
169
0
                const basegfx::B2DRange aContentRange(aContent.getB2DRange(rViewInformation));
170
171
0
                if(!aUnitRange.isInside(aContentRange))
172
0
                {
173
0
                    const Primitive2DReference xRef(
174
0
                        new MaskPrimitive2D(
175
0
                            basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aUnitRange)),
176
0
                            std::move(aContent)));
177
178
0
                    aContent = Primitive2DContainer { xRef };
179
0
                }
180
0
            }
181
182
0
            return aContent;
183
0
        }
184
185
        //  create buffered content in given resolution
186
        Bitmap PatternFillPrimitive2D::createTileImage(sal_uInt32 nWidth, sal_uInt32 nHeight) const
187
0
        {
188
0
            const geometry::ViewInformation2D aViewInformation2D;
189
0
            Primitive2DContainer aContent(createContent(aViewInformation2D));
190
0
            const primitive2d::Primitive2DReference xEmbedRef(
191
0
                    new primitive2d::TransformPrimitive2D(
192
0
                        basegfx::utils::createScaleB2DHomMatrix(nWidth, nHeight),
193
0
                        std::move(aContent)));
194
0
            primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
195
196
0
            return convertToBitmap(
197
0
                        std::move(xEmbedSeq),
198
0
                        aViewInformation2D,
199
0
                        nWidth,
200
0
                        nHeight,
201
0
                        nWidth * nHeight);
202
0
        }
203
204
        Primitive2DReference PatternFillPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
205
0
        {
206
0
            Primitive2DContainer aRetval;
207
208
0
            if(getChildren().empty())
209
0
                return nullptr;
210
211
0
            if(!(!getReferenceRange().isEmpty() && getReferenceRange().getWidth() > 0.0 && getReferenceRange().getHeight() > 0.0))
212
0
                return nullptr;
213
214
0
            const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
215
216
0
            if(!(!aMaskRange.isEmpty() && aMaskRange.getWidth() > 0.0 && aMaskRange.getHeight() > 0.0))
217
0
                return nullptr;
218
219
            // create tiling matrices
220
0
            std::vector< basegfx::B2DHomMatrix > aMatrices;
221
0
            texture::GeoTexSvxTiled aTiling(getReferenceRange());
222
223
0
            aTiling.appendTransformations(aMatrices);
224
225
            // create content
226
0
            Primitive2DContainer aContent(createContent(rViewInformation));
227
228
            // resize result
229
0
            aRetval.resize(aMatrices.size());
230
231
            // create one primitive for each matrix
232
0
            for(size_t a(0); a < aMatrices.size(); a++)
233
0
            {
234
0
                aRetval[a] = new TransformPrimitive2D(
235
0
                    aMatrices[a],
236
0
                    Primitive2DContainer(aContent));
237
0
            }
238
239
            // transform result which is in unit coordinates to mask's object coordinates
240
0
            {
241
0
                const basegfx::B2DHomMatrix aMaskTransform(
242
0
                    basegfx::utils::createScaleTranslateB2DHomMatrix(
243
0
                        aMaskRange.getRange(),
244
0
                        aMaskRange.getMinimum()));
245
246
0
                aRetval = Primitive2DContainer {
247
0
                        new TransformPrimitive2D(
248
0
                            aMaskTransform,
249
0
                            std::move(aRetval))
250
0
                };
251
0
            }
252
253
            // embed result in mask
254
0
            return
255
0
                new MaskPrimitive2D(
256
0
                    getMask(),
257
0
                    std::move(aRetval));
258
0
        }
259
260
        PatternFillPrimitive2D::PatternFillPrimitive2D(
261
            basegfx::B2DPolyPolygon aMask,
262
            Primitive2DContainer&& rChildren,
263
            const basegfx::B2DRange& rReferenceRange)
264
0
        :   maMask(std::move(aMask)),
265
0
            maChildren(std::move(rChildren)),
266
0
            maReferenceRange(rReferenceRange),
267
0
            mnDiscreteWidth(0),
268
0
            mnDiscreteHeight(0)
269
0
        {
270
0
        }
271
272
        bool PatternFillPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
273
0
        {
274
0
            if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
275
0
            {
276
0
                const PatternFillPrimitive2D& rCompare = static_cast< const PatternFillPrimitive2D& >(rPrimitive);
277
278
0
                return (getMask() == rCompare.getMask()
279
0
                    && getChildren() == rCompare.getChildren()
280
0
                    && getReferenceRange() == rCompare.getReferenceRange());
281
0
            }
282
283
0
            return false;
284
0
        }
285
286
        basegfx::B2DRange PatternFillPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /* rViewInformation */ ) const
287
0
        {
288
0
            return getMask().getB2DRange();
289
0
        }
290
291
        void PatternFillPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
292
0
        {
293
            // The existing buffered decomposition uses a buffer in the remembered
294
            // size or none if sizes are zero. Get new needed sizes which depend on
295
            // the given ViewInformation
296
0
            bool bResetBuffering = false;
297
0
            sal_uInt32 nW(0);
298
0
            sal_uInt32 nH(0);
299
0
            calculateNeededDiscreteBufferSize(nW, nH, rViewInformation);
300
0
            const bool bBufferingCurrentlyUsed(0 != mnDiscreteWidth && 0 != mnDiscreteHeight);
301
0
            const bool bBufferingNextUsed(0 != nW && 0 != nH);
302
303
0
            if(bBufferingNextUsed)
304
0
            {
305
                // buffering is now possible
306
0
                if(bBufferingCurrentlyUsed)
307
0
                {
308
0
                    if(nW > mnDiscreteWidth || nH > mnDiscreteHeight)
309
0
                    {
310
                        // Higher resolution is needed than used in the existing buffered
311
                        // decomposition - create new one
312
0
                        bResetBuffering = true;
313
0
                    }
314
0
                    else if(double(nW * nH) / double(mnDiscreteWidth * mnDiscreteHeight) <= 0.5)
315
0
                    {
316
                        // Size has shrunk for 50% or more - it's worth to refresh the buffering
317
                        // to spare some resources
318
0
                        bResetBuffering = true;
319
0
                    }
320
0
                }
321
0
                else
322
0
                {
323
                    // currently no buffering used - reset evtl. unbuffered
324
                    // decomposition to start buffering
325
0
                    bResetBuffering = true;
326
0
                }
327
0
            }
328
0
            else
329
0
            {
330
                // buffering is no longer possible
331
0
                if(bBufferingCurrentlyUsed)
332
0
                {
333
                    // reset decomposition to allow creation of unbuffered one
334
0
                    bResetBuffering = true;
335
0
                }
336
0
            }
337
338
0
            if(bResetBuffering)
339
0
            {
340
0
                PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
341
0
                pThat->mnDiscreteWidth = nW;
342
0
                pThat->mnDiscreteHeight = nH;
343
0
                pThat->setBuffered2DDecomposition(nullptr);
344
0
            }
345
346
            // call parent
347
0
            BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
348
0
        }
349
350
        sal_Int64 PatternFillPrimitive2D::estimateUsage()
351
0
        {
352
0
            size_t nRet(0);
353
0
            for (auto& it : getChildren())
354
0
                if (it)
355
0
                    nRet += it->estimateUsage();
356
0
            return nRet;
357
0
        }
358
359
        // provide unique ID
360
        sal_uInt32 PatternFillPrimitive2D::getPrimitive2DID() const
361
0
        {
362
0
            return PRIMITIVE2D_ID_PATTERNFILLPRIMITIVE2D;
363
0
        }
364
365
} // end of namespace
366
367
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */