calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / drawinglayer / source / primitive2d / patternfillprimitive2d.cxx
blob2021fc28360d4e7f55088b3644ae4f0924403366
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 .
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 #include <toolkit/helper/vclunohelper.hxx>
31 #include <drawinglayer/converters.hxx>
32 #include <utility>
34 using namespace com::sun::star;
36 #define MAXIMUM_SQUARE_LENGTH (186.0)
37 #define MINIMUM_SQUARE_LENGTH (16.0)
38 #define MINIMUM_TILES_LENGTH (3)
40 namespace drawinglayer::primitive2d
42 void PatternFillPrimitive2D::calculateNeededDiscreteBufferSize(
43 sal_uInt32& rWidth,
44 sal_uInt32& rHeight,
45 const geometry::ViewInformation2D& rViewInformation) const
47 // reset parameters
48 rWidth = rHeight = 0;
50 // check if resolution is in the range which may be buffered
51 const basegfx::B2DPolyPolygon& rMaskPolygon = getMask();
52 const basegfx::B2DRange aMaskRange(rMaskPolygon.getB2DRange());
54 // get discrete rounded up square size of a single tile
55 const basegfx::B2DHomMatrix aMaskRangeTransformation(
56 basegfx::utils::createScaleTranslateB2DHomMatrix(
57 aMaskRange.getRange(),
58 aMaskRange.getMinimum()));
59 const basegfx::B2DHomMatrix aTransform(
60 rViewInformation.getObjectToViewTransformation() * aMaskRangeTransformation);
61 const basegfx::B2DPoint aTopLeft(aTransform * getReferenceRange().getMinimum());
62 const basegfx::B2DPoint aX(aTransform * basegfx::B2DPoint(getReferenceRange().getMaxX(), getReferenceRange().getMinY()));
63 const basegfx::B2DPoint aY(aTransform * basegfx::B2DPoint(getReferenceRange().getMinX(), getReferenceRange().getMaxY()));
64 const double fW(basegfx::B2DVector(aX - aTopLeft).getLength());
65 const double fH(basegfx::B2DVector(aY - aTopLeft).getLength());
66 const double fSquare(fW * fH);
68 if(fSquare <= 0.0)
69 return;
71 // check if less than a maximum square pixels is used
72 static const sal_uInt32 fMaximumSquare(MAXIMUM_SQUARE_LENGTH * MAXIMUM_SQUARE_LENGTH);
74 if(fSquare >= fMaximumSquare)
75 return;
77 // calculate needed number of tiles and check if used more than a minimum count
78 const texture::GeoTexSvxTiled aTiling(getReferenceRange());
79 const sal_uInt32 nTiles(aTiling.getNumberOfTiles());
80 static const sal_uInt32 nMinimumTiles(MINIMUM_TILES_LENGTH * MINIMUM_TILES_LENGTH);
82 if(nTiles < nMinimumTiles)
83 return;
85 rWidth = basegfx::fround(ceil(fW));
86 rHeight = basegfx::fround(ceil(fH));
87 static const sal_uInt32 fMinimumSquare(MINIMUM_SQUARE_LENGTH * MINIMUM_SQUARE_LENGTH);
89 if(fSquare < fMinimumSquare)
91 const double fRel(fW/fH);
92 rWidth = basegfx::fround(sqrt(fMinimumSquare * fRel));
93 rHeight = basegfx::fround(sqrt(fMinimumSquare / fRel));
97 void PatternFillPrimitive2D::getTileSize(
98 sal_uInt32& rWidth,
99 sal_uInt32& rHeight,
100 const geometry::ViewInformation2D& rViewInformation) const
102 const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
104 // get discrete rounded up square size of a single tile
105 const basegfx::B2DHomMatrix aMaskRangeTransformation(
106 basegfx::utils::createScaleTranslateB2DHomMatrix(
107 aMaskRange.getRange(),
108 aMaskRange.getMinimum()));
109 const basegfx::B2DHomMatrix aTransform(
110 rViewInformation.getObjectToViewTransformation() * aMaskRangeTransformation);
111 const basegfx::B2DPoint aTopLeft(aTransform * getReferenceRange().getMinimum());
112 const basegfx::B2DPoint aX(aTransform * basegfx::B2DPoint(getReferenceRange().getMaxX(), getReferenceRange().getMinY()));
113 const basegfx::B2DPoint aY(aTransform * basegfx::B2DPoint(getReferenceRange().getMinX(), getReferenceRange().getMaxY()));
114 const double fW(basegfx::B2DVector(aX - aTopLeft).getLength());
115 const double fH(basegfx::B2DVector(aY - aTopLeft).getLength());
117 rWidth = basegfx::fround(ceil(fW));
118 rHeight = basegfx::fround(ceil(fH));
121 Primitive2DContainer PatternFillPrimitive2D::createContent(const geometry::ViewInformation2D& rViewInformation) const
123 Primitive2DContainer aContent;
125 // see if buffering is wanted. If so, create buffered content in given resolution
126 if(0 != mnDiscreteWidth && 0 != mnDiscreteHeight)
128 const geometry::ViewInformation2D aViewInformation2D;
129 primitive2d::Primitive2DReference xEmbedRef(
130 new primitive2d::TransformPrimitive2D(
131 basegfx::utils::createScaleB2DHomMatrix(mnDiscreteWidth, mnDiscreteHeight),
132 Primitive2DContainer(getChildren())));
133 primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
135 const BitmapEx aBitmapEx(
136 convertToBitmapEx(
137 std::move(xEmbedSeq),
138 aViewInformation2D,
139 mnDiscreteWidth,
140 mnDiscreteHeight,
141 mnDiscreteWidth * mnDiscreteHeight));
143 if(!aBitmapEx.IsEmpty())
145 const Size& rBmpPix = aBitmapEx.GetSizePixel();
147 if(rBmpPix.Width() > 0 && rBmpPix.Height() > 0)
149 const primitive2d::Primitive2DReference xEmbedRefBitmap(
150 new primitive2d::BitmapPrimitive2D(
151 aBitmapEx,
152 basegfx::B2DHomMatrix()));
153 aContent = primitive2d::Primitive2DContainer { xEmbedRefBitmap };
158 if(aContent.empty())
160 // buffering was not tried or did fail - reset remembered buffered size
161 // in any case
162 PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
163 pThat->mnDiscreteWidth = pThat->mnDiscreteHeight = 0;
165 // use children as default context
166 aContent = getChildren();
168 // check if content needs to be clipped
169 const basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
170 const basegfx::B2DRange aContentRange(aContent.getB2DRange(rViewInformation));
172 if(!aUnitRange.isInside(aContentRange))
174 const Primitive2DReference xRef(
175 new MaskPrimitive2D(
176 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aUnitRange)),
177 std::move(aContent)));
179 aContent = Primitive2DContainer { xRef };
183 return aContent;
186 // create buffered content in given resolution
187 BitmapEx PatternFillPrimitive2D::createTileImage(sal_uInt32 nWidth, sal_uInt32 nHeight) const
189 const geometry::ViewInformation2D aViewInformation2D;
190 Primitive2DContainer aContent(createContent(aViewInformation2D));
191 const primitive2d::Primitive2DReference xEmbedRef(
192 new primitive2d::TransformPrimitive2D(
193 basegfx::utils::createScaleB2DHomMatrix(nWidth, nHeight),
194 std::move(aContent)));
195 primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
197 return convertToBitmapEx(
198 std::move(xEmbedSeq),
199 aViewInformation2D,
200 nWidth,
201 nHeight,
202 nWidth * nHeight);
205 void PatternFillPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const
207 Primitive2DContainer aRetval;
209 if(getChildren().empty())
210 return;
212 if(!(!getReferenceRange().isEmpty() && getReferenceRange().getWidth() > 0.0 && getReferenceRange().getHeight() > 0.0))
213 return;
215 const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
217 if(!(!aMaskRange.isEmpty() && aMaskRange.getWidth() > 0.0 && aMaskRange.getHeight() > 0.0))
218 return;
220 // create tiling matrices
221 std::vector< basegfx::B2DHomMatrix > aMatrices;
222 texture::GeoTexSvxTiled aTiling(getReferenceRange());
224 aTiling.appendTransformations(aMatrices);
226 // create content
227 Primitive2DContainer aContent(createContent(rViewInformation));
229 // resize result
230 aRetval.resize(aMatrices.size());
232 // create one primitive for each matrix
233 for(size_t a(0); a < aMatrices.size(); a++)
235 aRetval[a] = new TransformPrimitive2D(
236 aMatrices[a],
237 Primitive2DContainer(aContent));
240 // transform result which is in unit coordinates to mask's object coordinates
242 const basegfx::B2DHomMatrix aMaskTransform(
243 basegfx::utils::createScaleTranslateB2DHomMatrix(
244 aMaskRange.getRange(),
245 aMaskRange.getMinimum()));
247 Primitive2DReference xRef(
248 new TransformPrimitive2D(
249 aMaskTransform,
250 std::move(aRetval)));
252 aRetval = Primitive2DContainer { xRef };
255 // embed result in mask
257 rContainer.push_back(
258 new MaskPrimitive2D(
259 getMask(),
260 std::move(aRetval)));
264 PatternFillPrimitive2D::PatternFillPrimitive2D(
265 basegfx::B2DPolyPolygon aMask,
266 Primitive2DContainer&& rChildren,
267 const basegfx::B2DRange& rReferenceRange)
268 : maMask(std::move(aMask)),
269 maChildren(std::move(rChildren)),
270 maReferenceRange(rReferenceRange),
271 mnDiscreteWidth(0),
272 mnDiscreteHeight(0)
276 bool PatternFillPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
278 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
280 const PatternFillPrimitive2D& rCompare = static_cast< const PatternFillPrimitive2D& >(rPrimitive);
282 return (getMask() == rCompare.getMask()
283 && getChildren() == rCompare.getChildren()
284 && getReferenceRange() == rCompare.getReferenceRange());
287 return false;
290 basegfx::B2DRange PatternFillPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /* rViewInformation */ ) const
292 return getMask().getB2DRange();
295 void PatternFillPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
297 // The existing buffered decomposition uses a buffer in the remembered
298 // size or none if sizes are zero. Get new needed sizes which depend on
299 // the given ViewInformation
300 bool bResetBuffering = false;
301 sal_uInt32 nW(0);
302 sal_uInt32 nH(0);
303 calculateNeededDiscreteBufferSize(nW, nH, rViewInformation);
304 const bool bBufferingCurrentlyUsed(0 != mnDiscreteWidth && 0 != mnDiscreteHeight);
305 const bool bBufferingNextUsed(0 != nW && 0 != nH);
307 if(bBufferingNextUsed)
309 // buffering is now possible
310 if(bBufferingCurrentlyUsed)
312 if(nW > mnDiscreteWidth || nH > mnDiscreteHeight)
314 // Higher resolution is needed than used in the existing buffered
315 // decomposition - create new one
316 bResetBuffering = true;
318 else if(double(nW * nH) / double(mnDiscreteWidth * mnDiscreteHeight) <= 0.5)
320 // Size has shrunk for 50% or more - it's worth to refresh the buffering
321 // to spare some resources
322 bResetBuffering = true;
325 else
327 // currently no buffering used - reset evtl. unbuffered
328 // decomposition to start buffering
329 bResetBuffering = true;
332 else
334 // buffering is no longer possible
335 if(bBufferingCurrentlyUsed)
337 // reset decomposition to allow creation of unbuffered one
338 bResetBuffering = true;
342 if(bResetBuffering)
344 PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
345 pThat->mnDiscreteWidth = nW;
346 pThat->mnDiscreteHeight = nH;
347 pThat->setBuffered2DDecomposition(Primitive2DContainer());
350 // call parent
351 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
354 sal_Int64 PatternFillPrimitive2D::estimateUsage()
356 size_t nRet(0);
357 for (auto& it : getChildren())
358 if (it)
359 nRet += it->estimateUsage();
360 return nRet;
363 // provide unique ID
364 sal_uInt32 PatternFillPrimitive2D::getPrimitive2DID() const
366 return PRIMITIVE2D_ID_PATTERNFILLPRIMITIVE2D;
369 } // end of namespace
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */