LibreOffice Module drawinglayer (master) 1
patternfillprimitive2d.cxx
Go to the documentation of this file.
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
26#include <texture/texture.hxx>
30
32#include <utility>
33
34using namespace com::sun::star;
35
36#define MAXIMUM_SQUARE_LENGTH (186.0)
37#define MINIMUM_SQUARE_LENGTH (16.0)
38#define MINIMUM_TILES_LENGTH (3)
39
41{
43 sal_uInt32& rWidth,
44 sal_uInt32& rHeight,
45 const geometry::ViewInformation2D& rViewInformation) const
46 {
47 // reset parameters
48 rWidth = rHeight = 0;
49
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());
53
54 // get discrete rounded up square size of a single tile
55 const basegfx::B2DHomMatrix aMaskRangeTransformation(
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);
67
68 if(fSquare <= 0.0)
69 return;
70
71 // check if less than a maximum square pixels is used
72 static const sal_uInt32 fMaximumSquare(MAXIMUM_SQUARE_LENGTH * MAXIMUM_SQUARE_LENGTH);
73
74 if(fSquare >= fMaximumSquare)
75 return;
76
77 // calculate needed number of tiles and check if used more than a minimum count
79 const sal_uInt32 nTiles(aTiling.getNumberOfTiles());
80 static const sal_uInt32 nMinimumTiles(MINIMUM_TILES_LENGTH * MINIMUM_TILES_LENGTH);
81
82 if(nTiles < nMinimumTiles)
83 return;
84
85 rWidth = basegfx::fround(ceil(fW));
86 rHeight = basegfx::fround(ceil(fH));
87 static const sal_uInt32 fMinimumSquare(MINIMUM_SQUARE_LENGTH * MINIMUM_SQUARE_LENGTH);
88
89 if(fSquare < fMinimumSquare)
90 {
91 const double fRel(fW/fH);
92 rWidth = basegfx::fround(sqrt(fMinimumSquare * fRel));
93 rHeight = basegfx::fround(sqrt(fMinimumSquare / fRel));
94 }
95 }
96
98 sal_uInt32& rWidth,
99 sal_uInt32& rHeight,
100 const geometry::ViewInformation2D& rViewInformation) const
101 {
102 const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
103
104 // get discrete rounded up square size of a single tile
105 const basegfx::B2DHomMatrix aMaskRangeTransformation(
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());
116
117 rWidth = basegfx::fround(ceil(fW));
118 rHeight = basegfx::fround(ceil(fH));
119 }
120
122 {
123 Primitive2DContainer aContent;
124
125 // see if buffering is wanted. If so, create buffered content in given resolution
126 if(0 != mnDiscreteWidth && 0 != mnDiscreteHeight)
127 {
128 const geometry::ViewInformation2D aViewInformation2D;
133 primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
134
135 const BitmapEx aBitmapEx(
137 std::move(xEmbedSeq),
138 aViewInformation2D,
142
143 if(!aBitmapEx.IsEmpty())
144 {
145 const Size& rBmpPix = aBitmapEx.GetSizePixel();
146
147 if(rBmpPix.Width() > 0 && rBmpPix.Height() > 0)
148 {
149 const primitive2d::Primitive2DReference xEmbedRefBitmap(
151 aBitmapEx,
153 aContent = primitive2d::Primitive2DContainer { xEmbedRefBitmap };
154 }
155 }
156 }
157
158 if(aContent.empty())
159 {
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;
164
165 // use children as default context
166 aContent = getChildren();
167
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));
171
172 if(!aUnitRange.isInside(aContentRange))
173 {
174 const Primitive2DReference xRef(
175 new MaskPrimitive2D(
177 std::move(aContent)));
178
179 aContent = Primitive2DContainer { xRef };
180 }
181 }
182
183 return aContent;
184 }
185
186 // create buffered content in given resolution
187 BitmapEx PatternFillPrimitive2D::createTileImage(sal_uInt32 nWidth, sal_uInt32 nHeight) const
188 {
189 const geometry::ViewInformation2D aViewInformation2D;
190 Primitive2DContainer aContent(createContent(aViewInformation2D));
191 const primitive2d::Primitive2DReference xEmbedRef(
194 std::move(aContent)));
195 primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
196
197 return convertToBitmapEx(
198 std::move(xEmbedSeq),
199 aViewInformation2D,
200 nWidth,
201 nHeight,
202 nWidth * nHeight);
203 }
204
206 {
207 Primitive2DContainer aRetval;
208
209 if(getChildren().empty())
210 return;
211
212 if(!(!getReferenceRange().isEmpty() && getReferenceRange().getWidth() > 0.0 && getReferenceRange().getHeight() > 0.0))
213 return;
214
215 const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
216
217 if(!(!aMaskRange.isEmpty() && aMaskRange.getWidth() > 0.0 && aMaskRange.getHeight() > 0.0))
218 return;
219
220 // create tiling matrices
221 std::vector< basegfx::B2DHomMatrix > aMatrices;
223
224 aTiling.appendTransformations(aMatrices);
225
226 // create content
227 Primitive2DContainer aContent(createContent(rViewInformation));
228
229 // resize result
230 aRetval.resize(aMatrices.size());
231
232 // create one primitive for each matrix
233 for(size_t a(0); a < aMatrices.size(); a++)
234 {
235 aRetval[a] = new TransformPrimitive2D(
236 aMatrices[a],
237 Primitive2DContainer(aContent));
238 }
239
240 // transform result which is in unit coordinates to mask's object coordinates
241 {
242 const basegfx::B2DHomMatrix aMaskTransform(
244 aMaskRange.getRange(),
245 aMaskRange.getMinimum()));
246
249 aMaskTransform,
250 std::move(aRetval)));
251
252 aRetval = Primitive2DContainer { xRef };
253 }
254
255 // embed result in mask
256 {
257 rContainer.push_back(
258 new MaskPrimitive2D(
259 getMask(),
260 std::move(aRetval)));
261 }
262 }
263
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)
273 {
274 }
275
277 {
278 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
279 {
280 const PatternFillPrimitive2D& rCompare = static_cast< const PatternFillPrimitive2D& >(rPrimitive);
281
282 return (getMask() == rCompare.getMask()
283 && getChildren() == rCompare.getChildren()
284 && getReferenceRange() == rCompare.getReferenceRange());
285 }
286
287 return false;
288 }
289
291 {
292 return getMask().getB2DRange();
293 }
294
296 {
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);
306
307 if(bBufferingNextUsed)
308 {
309 // buffering is now possible
310 if(bBufferingCurrentlyUsed)
311 {
312 if(nW > mnDiscreteWidth || nH > mnDiscreteHeight)
313 {
314 // Higher resolution is needed than used in the existing buffered
315 // decomposition - create new one
316 bResetBuffering = true;
317 }
318 else if(double(nW * nH) / double(mnDiscreteWidth * mnDiscreteHeight) <= 0.5)
319 {
320 // Size has shrunk for 50% or more - it's worth to refresh the buffering
321 // to spare some resources
322 bResetBuffering = true;
323 }
324 }
325 else
326 {
327 // currently no buffering used - reset evtl. unbuffered
328 // decomposition to start buffering
329 bResetBuffering = true;
330 }
331 }
332 else
333 {
334 // buffering is no longer possible
335 if(bBufferingCurrentlyUsed)
336 {
337 // reset decomposition to allow creation of unbuffered one
338 bResetBuffering = true;
339 }
340 }
341
342 if(bResetBuffering)
343 {
344 PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
345 pThat->mnDiscreteWidth = nW;
346 pThat->mnDiscreteHeight = nH;
348 }
349
350 // call parent
352 }
353
355 {
356 size_t nRet(0);
357 for (auto& it : getChildren())
358 if (it)
359 nRet += it->estimateUsage();
360 return nRet;
361 }
362
363 // provide unique ID
365 {
367 }
368
369} // end of namespace
370
371/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< Reference< XAnimationNode > > maChildren
bool IsEmpty() const
const Size & GetSizePixel() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
B2DRange getB2DRange() const
B2DVector getRange() const
B2DPoint getMinimum() const
TYPE getWidth() const
bool isInside(const Tuple2D< TYPE > &rTuple) const
bool isEmpty() const
TYPE getHeight() const
const basegfx::B2DHomMatrix & getObjectToViewTransformation() const
On-demand prepared Object to View transformation and its inverse for convenience.
virtual void get2DDecomposition(Primitive2DDecompositionVisitor &rVisitor, const geometry::ViewInformation2D &rViewInformation) const override
The getDecomposition default implementation will on demand use create2DDecomposition() if maBuffered2...
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
virtual void get2DDecomposition(Primitive2DDecompositionVisitor &rVisitor, const geometry::ViewInformation2D &rViewInformation) const override
overload to react on evtl. buffered content
void getTileSize(sal_uInt32 &rWidth, sal_uInt32 &rHeight, const geometry::ViewInformation2D &rViewInformation) const
helper that is capable to calculate the needed discrete buffer size for eventually buffered content
virtual void create2DDecomposition(Primitive2DContainer &rContainer, const geometry::ViewInformation2D &rViewInformation) const override
create local decomposition
PatternFillPrimitive2D(basegfx::B2DPolyPolygon aMask, Primitive2DContainer &&rChildren, const basegfx::B2DRange &rReferenceRange)
constructor
void calculateNeededDiscreteBufferSize(sal_uInt32 &rWidth, sal_uInt32 &rHeight, const geometry::ViewInformation2D &rViewInformation) const
helper that is capable to calculate the needed discrete buffer size for eventually buffered content
virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &rViewInformation) const override
get range
const basegfx::B2DPolyPolygon & getMask() const
data read access
Primitive2DContainer createContent(const geometry::ViewInformation2D &rViewInformation) const
helper which creates the content - checks if clipping is needed and eventually creates buffered conte...
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
sal_uInt32 mnDiscreteWidth
values holding the discrete buffer size
BitmapEx createTileImage(sal_uInt32 nWidth, sal_uInt32 nHeight) const
helper which creates the content - checks if clipping is needed and eventually creates buffered conte...
basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &aViewInformation) const
sal_uInt32 getNumberOfTiles() const
Definition: texture.cxx:990
void appendTransformations(::std::vector< basegfx::B2DHomMatrix > &rMatrices) const
Definition: texture.cxx:997
#define PRIMITIVE2D_ID_PATTERNFILLPRIMITIVE2D
uno_Any a
double getLength(const B2DPolygon &rCandidate)
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
B2DPolygon createPolygonFromRect(const B2DRectangle &rRect, double fRadiusX, double fRadiusY)
B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
B2IRange fround(const B2DRange &rRange)
BitmapEx convertToBitmapEx(drawinglayer::primitive2d::Primitive2DContainer &&rSeq, const geometry::ViewInformation2D &rViewInformation2D, sal_uInt32 nDiscreteWidth, sal_uInt32 nDiscreteHeight, sal_uInt32 nMaxSquarePixels)
Definition: converters.cxx:156
#define MAXIMUM_SQUARE_LENGTH
#define MINIMUM_SQUARE_LENGTH
#define MINIMUM_TILES_LENGTH