1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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(
45 const geometry::ViewInformation2D
& rViewInformation
) const
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
);
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
)
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
)
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(
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(
137 std::move(xEmbedSeq
),
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(
152 basegfx::B2DHomMatrix()));
153 aContent
= primitive2d::Primitive2DContainer
{ xEmbedRefBitmap
};
160 // buffering was not tried or did fail - reset remembered buffered size
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(
176 basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aUnitRange
)),
177 std::move(aContent
)));
179 aContent
= Primitive2DContainer
{ xRef
};
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
),
205 void PatternFillPrimitive2D::create2DDecomposition(Primitive2DContainer
& rContainer
, const geometry::ViewInformation2D
& rViewInformation
) const
207 Primitive2DContainer aRetval
;
209 if(getChildren().empty())
212 if(!(!getReferenceRange().isEmpty() && getReferenceRange().getWidth() > 0.0 && getReferenceRange().getHeight() > 0.0))
215 const basegfx::B2DRange
aMaskRange(getMask().getB2DRange());
217 if(!(!aMaskRange
.isEmpty() && aMaskRange
.getWidth() > 0.0 && aMaskRange
.getHeight() > 0.0))
220 // create tiling matrices
221 std::vector
< basegfx::B2DHomMatrix
> aMatrices
;
222 texture::GeoTexSvxTiled
aTiling(getReferenceRange());
224 aTiling
.appendTransformations(aMatrices
);
227 Primitive2DContainer
aContent(createContent(rViewInformation
));
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(
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(
250 std::move(aRetval
)));
252 aRetval
= Primitive2DContainer
{ xRef
};
255 // embed result in mask
257 rContainer
.push_back(
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
),
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());
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;
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;
327 // currently no buffering used - reset evtl. unbuffered
328 // decomposition to start buffering
329 bResetBuffering
= true;
334 // buffering is no longer possible
335 if(bBufferingCurrentlyUsed
)
337 // reset decomposition to allow creation of unbuffered one
338 bResetBuffering
= true;
344 PatternFillPrimitive2D
* pThat
= const_cast< PatternFillPrimitive2D
* >(this);
345 pThat
->mnDiscreteWidth
= nW
;
346 pThat
->mnDiscreteHeight
= nH
;
347 pThat
->setBuffered2DDecomposition(Primitive2DContainer());
351 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor
, rViewInformation
);
354 sal_Int64
PatternFillPrimitive2D::estimateUsage()
357 for (auto& it
: getChildren())
359 nRet
+= it
->estimateUsage();
364 sal_uInt32
PatternFillPrimitive2D::getPrimitive2DID() const
366 return PRIMITIVE2D_ID_PATTERNFILLPRIMITIVE2D
;
369 } // end of namespace
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */