Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / drawinglayer / source / primitive2d / patternfillprimitive2d.cxx
blob1435e12d9c9fe6c86cb815170e41b3e6fc855ad5
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/polygonprimitive2d.hxx>
24 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/matrix/b2dhommatrixtools.hxx>
27 #include <drawinglayer/texture/texture.hxx>
28 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
29 #include <drawinglayer/geometry/viewinformation2d.hxx>
31 #include <converters.hxx>
33 using namespace com::sun::star;
35 #define MAXIMUM_SQUARE_LENGTH (186.0)
36 #define MINIMUM_SQUARE_LENGTH (16.0)
37 #define MINIMUM_TILES_LENGTH (3)
39 namespace drawinglayer
41 namespace primitive2d
43 void PatternFillPrimitive2D::calculateNeededDiscreteBufferSize(
44 sal_uInt32& rWidth,
45 sal_uInt32& rHeight,
46 const geometry::ViewInformation2D& rViewInformation) const
48 // reset parameters
49 rWidth = rHeight = 0;
51 // check if resolution is in the range which may be buffered
52 const basegfx::B2DPolyPolygon& rMaskPolygon = getMask();
53 const basegfx::B2DRange aMaskRange(rMaskPolygon.getB2DRange());
55 // get discrete rounded up square size of a single tile
56 const basegfx::B2DHomMatrix aMaskRangeTransformation(
57 basegfx::utils::createScaleTranslateB2DHomMatrix(
58 aMaskRange.getRange(),
59 aMaskRange.getMinimum()));
60 const basegfx::B2DHomMatrix aTransform(
61 rViewInformation.getObjectToViewTransformation() * aMaskRangeTransformation);
62 const basegfx::B2DPoint aTopLeft(aTransform * getReferenceRange().getMinimum());
63 const basegfx::B2DPoint aX(aTransform * basegfx::B2DPoint(getReferenceRange().getMaxX(), getReferenceRange().getMinY()));
64 const basegfx::B2DPoint aY(aTransform * basegfx::B2DPoint(getReferenceRange().getMinX(), getReferenceRange().getMaxY()));
65 const double fW(basegfx::B2DVector(aX - aTopLeft).getLength());
66 const double fH(basegfx::B2DVector(aY - aTopLeft).getLength());
67 const double fSquare(fW * fH);
69 if(fSquare > 0.0)
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)
76 // calculate needed number of tiles and check if used more than a minimum count
77 const texture::GeoTexSvxTiled aTiling(getReferenceRange());
78 const sal_uInt32 nTiles(aTiling.getNumberOfTiles());
79 static const sal_uInt32 nMinimumTiles(MINIMUM_TILES_LENGTH * MINIMUM_TILES_LENGTH);
81 if(nTiles >= nMinimumTiles)
83 rWidth = basegfx::fround(ceil(fW));
84 rHeight = basegfx::fround(ceil(fH));
85 static const sal_uInt32 fMinimumSquare(MINIMUM_SQUARE_LENGTH * MINIMUM_SQUARE_LENGTH);
87 if(fSquare < fMinimumSquare)
89 const double fRel(fW/fH);
90 rWidth = basegfx::fround(sqrt(fMinimumSquare * fRel));
91 rHeight = basegfx::fround(sqrt(fMinimumSquare / fRel));
98 Primitive2DContainer PatternFillPrimitive2D::createContent(const geometry::ViewInformation2D& rViewInformation) const
100 Primitive2DContainer aContent;
102 // see if buffering is wanted. If so, create buffered content in given resolution
103 if(0 != mnDiscreteWidth && 0 != mnDiscreteHeight)
105 const geometry::ViewInformation2D aViewInformation2D;
106 const primitive2d::Primitive2DReference xEmbedRef(
107 new primitive2d::TransformPrimitive2D(
108 basegfx::utils::createScaleB2DHomMatrix(mnDiscreteWidth, mnDiscreteHeight),
109 getChildren()));
110 const primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
112 const BitmapEx aBitmapEx(
113 convertToBitmapEx(
114 xEmbedSeq,
115 aViewInformation2D,
116 mnDiscreteWidth,
117 mnDiscreteHeight,
118 mnDiscreteWidth * mnDiscreteHeight));
120 if(!aBitmapEx.IsEmpty())
122 const Size& rBmpPix = aBitmapEx.GetSizePixel();
124 if(rBmpPix.Width() > 0 && rBmpPix.Height() > 0)
126 const primitive2d::Primitive2DReference xEmbedRefBitmap(
127 new primitive2d::BitmapPrimitive2D(
128 aBitmapEx,
129 basegfx::B2DHomMatrix()));
130 aContent = primitive2d::Primitive2DContainer { xEmbedRefBitmap };
135 if(aContent.empty())
137 // buffering was not tried or did fail - reset remembered buffered size
138 // in any case
139 PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
140 pThat->mnDiscreteWidth = pThat->mnDiscreteHeight = 0;
142 // use children as default context
143 aContent = getChildren();
145 // check if content needs to be clipped
146 const basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
147 const basegfx::B2DRange aContentRange(getChildren().getB2DRange(rViewInformation));
149 if(!aUnitRange.isInside(aContentRange))
151 const Primitive2DReference xRef(
152 new MaskPrimitive2D(
153 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aUnitRange)),
154 aContent));
156 aContent = Primitive2DContainer { xRef };
160 return aContent;
163 void PatternFillPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const
165 Primitive2DContainer aRetval;
167 if(!getChildren().empty())
169 if(!getReferenceRange().isEmpty() && getReferenceRange().getWidth() > 0.0 && getReferenceRange().getHeight() > 0.0)
171 const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
173 if(!aMaskRange.isEmpty() && aMaskRange.getWidth() > 0.0 && aMaskRange.getHeight() > 0.0)
175 // create tiling matrices
176 std::vector< basegfx::B2DHomMatrix > aMatrices;
177 texture::GeoTexSvxTiled aTiling(getReferenceRange());
179 aTiling.appendTransformations(aMatrices);
181 // create content
182 const Primitive2DContainer aContent(createContent(rViewInformation));
184 // resize result
185 aRetval.resize(aMatrices.size());
187 // create one primitive for each matrix
188 for(size_t a(0); a < aMatrices.size(); a++)
190 aRetval[a] = new TransformPrimitive2D(
191 aMatrices[a],
192 aContent);
195 // transform result which is in unit coordinates to mask's object coordinates
197 const basegfx::B2DHomMatrix aMaskTransform(
198 basegfx::utils::createScaleTranslateB2DHomMatrix(
199 aMaskRange.getRange(),
200 aMaskRange.getMinimum()));
202 const Primitive2DReference xRef(
203 new TransformPrimitive2D(
204 aMaskTransform,
205 aRetval));
207 aRetval = Primitive2DContainer { xRef };
210 // embed result in mask
212 rContainer.push_back(
213 new MaskPrimitive2D(
214 getMask(),
215 aRetval));
223 PatternFillPrimitive2D::PatternFillPrimitive2D(
224 const basegfx::B2DPolyPolygon& rMask,
225 const Primitive2DContainer& rChildren,
226 const basegfx::B2DRange& rReferenceRange)
227 : BufferedDecompositionPrimitive2D(),
228 maMask(rMask),
229 maChildren(rChildren),
230 maReferenceRange(rReferenceRange),
231 mnDiscreteWidth(0),
232 mnDiscreteHeight(0)
236 bool PatternFillPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
238 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
240 const PatternFillPrimitive2D& rCompare = static_cast< const PatternFillPrimitive2D& >(rPrimitive);
242 return (getMask() == rCompare.getMask()
243 && getChildren() == rCompare.getChildren()
244 && getReferenceRange() == rCompare.getReferenceRange());
247 return false;
250 basegfx::B2DRange PatternFillPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /* rViewInformation */ ) const
252 return getMask().getB2DRange();
255 void PatternFillPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
257 // The existing bufferd decomposition uses a buffer in the remembered
258 // size or none if sizes are zero. Get new needed sizes which depend on
259 // the given ViewInformation
260 bool bResetBuffering = false;
261 sal_uInt32 nW(0);
262 sal_uInt32 nH(0);
263 calculateNeededDiscreteBufferSize(nW, nH, rViewInformation);
264 const bool bBufferingCurrentlyUsed(0 != mnDiscreteWidth && 0 != mnDiscreteHeight);
265 const bool bBufferingNextUsed(0 != nW && 0 != nH);
267 if(bBufferingNextUsed)
269 // buffering is now possible
270 if(bBufferingCurrentlyUsed)
272 if(nW > mnDiscreteWidth || nH > mnDiscreteHeight)
274 // Higher resolution is needed than used in the existing buffered
275 // decomposition - create new one
276 bResetBuffering = true;
278 else if(double(nW * nH) / double(mnDiscreteWidth * mnDiscreteHeight) <= 0.5)
280 // Size has shrunk for 50% or more - it's worth to refresh the buffering
281 // to spare some resources
282 bResetBuffering = true;
285 else
287 // currently no buffering used - reset evtl. unbuffered
288 // decomposition to start buffering
289 bResetBuffering = true;
292 else
294 // buffering is no longer possible
295 if(bBufferingCurrentlyUsed)
297 // reset decomposition to allow creation of unbuffered one
298 bResetBuffering = true;
302 if(bResetBuffering)
304 PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
305 pThat->mnDiscreteWidth = nW;
306 pThat->mnDiscreteHeight = nH;
307 pThat->setBuffered2DDecomposition(Primitive2DContainer());
310 // call parent
311 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
314 sal_Int64 SAL_CALL PatternFillPrimitive2D::estimateUsage()
316 size_t nRet(0);
317 for (auto& it : getChildren())
319 uno::Reference<util::XAccounting> const xAcc(it, uno::UNO_QUERY);
320 if (xAcc.is())
322 nRet += xAcc->estimateUsage();
325 return nRet;
328 // provide unique ID
329 ImplPrimitive2DIDBlock(PatternFillPrimitive2D, PRIMITIVE2D_ID_PATTERNFILLPRIMITIVE2D)
331 } // end of namespace primitive2d
332 } // end of namespace drawinglayer
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */