1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <drawinglayer/primitive2d/patternfillprimitive2d.hxx>
#include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
#include <drawinglayer/primitive2d/transformprimitive2d.hxx>
#include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
#include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <drawinglayer/texture/texture.hxx>
#include <drawinglayer/primitive2d/maskprimitive2d.hxx>
#include <drawinglayer/geometry/viewinformation2d.hxx>
#include <converters.hxx>
using namespace com::sun::star;
#define MAXIMUM_SQUARE_LENGTH (186.0)
#define MINIMUM_SQUARE_LENGTH (16.0)
#define MINIMUM_TILES_LENGTH (3)
namespace drawinglayer
{
namespace primitive2d
{
void PatternFillPrimitive2D::calculateNeededDiscreteBufferSize(
sal_uInt32& rWidth,
sal_uInt32& rHeight,
const geometry::ViewInformation2D& rViewInformation) const
{
// reset parameters
rWidth = rHeight = 0;
// check if resolution is in the range which may be buffered
const basegfx::B2DPolyPolygon& rMaskPolygon = getMask();
const basegfx::B2DRange aMaskRange(rMaskPolygon.getB2DRange());
// get discrete rounded up square size of a single tile
const basegfx::B2DHomMatrix aMaskRangeTransformation(
basegfx::utils::createScaleTranslateB2DHomMatrix(
aMaskRange.getRange(),
aMaskRange.getMinimum()));
const basegfx::B2DHomMatrix aTransform(
rViewInformation.getObjectToViewTransformation() * aMaskRangeTransformation);
const basegfx::B2DPoint aTopLeft(aTransform * getReferenceRange().getMinimum());
const basegfx::B2DPoint aX(aTransform * basegfx::B2DPoint(getReferenceRange().getMaxX(), getReferenceRange().getMinY()));
const basegfx::B2DPoint aY(aTransform * basegfx::B2DPoint(getReferenceRange().getMinX(), getReferenceRange().getMaxY()));
const double fW(basegfx::B2DVector(aX - aTopLeft).getLength());
const double fH(basegfx::B2DVector(aY - aTopLeft).getLength());
const double fSquare(fW * fH);
if(fSquare > 0.0)
{
// check if less than a maximum square pixels is used
static const sal_uInt32 fMaximumSquare(MAXIMUM_SQUARE_LENGTH * MAXIMUM_SQUARE_LENGTH);
if(fSquare < fMaximumSquare)
{
// calculate needed number of tiles and check if used more than a minimum count
const texture::GeoTexSvxTiled aTiling(getReferenceRange());
const sal_uInt32 nTiles(aTiling.getNumberOfTiles());
static const sal_uInt32 nMinimumTiles(MINIMUM_TILES_LENGTH * MINIMUM_TILES_LENGTH);
if(nTiles >= nMinimumTiles)
{
rWidth = basegfx::fround(ceil(fW));
rHeight = basegfx::fround(ceil(fH));
static const sal_uInt32 fMinimumSquare(MINIMUM_SQUARE_LENGTH * MINIMUM_SQUARE_LENGTH);
if(fSquare < fMinimumSquare)
{
const double fRel(fW/fH);
rWidth = basegfx::fround(sqrt(fMinimumSquare * fRel));
rHeight = basegfx::fround(sqrt(fMinimumSquare / fRel));
}
}
}
}
}
Primitive2DContainer PatternFillPrimitive2D::createContent(const geometry::ViewInformation2D& rViewInformation) const
{
Primitive2DContainer aContent;
// see if buffering is wanted. it is wanted, create buffered content in given resolution
if(0 != mnDiscreteWidth && 0 != mnDiscreteHeight)
{
const geometry::ViewInformation2D aViewInformation2D;
const primitive2d::Primitive2DReference xEmbedRef(
new primitive2d::TransformPrimitive2D(
basegfx::utils::createScaleB2DHomMatrix(mnDiscreteWidth, mnDiscreteHeight),
getChildren()));
const primitive2d::Primitive2DContainer xEmbedSeq { xEmbedRef };
const BitmapEx aBitmapEx(
convertToBitmapEx(
xEmbedSeq,
aViewInformation2D,
mnDiscreteWidth,
mnDiscreteHeight,
mnDiscreteWidth * mnDiscreteHeight));
if(!aBitmapEx.IsEmpty())
{
const Size& rBmpPix = aBitmapEx.GetSizePixel();
if(rBmpPix.Width() > 0 && rBmpPix.Height() > 0)
{
const primitive2d::Primitive2DReference xEmbedRefBitmap(
new primitive2d::BitmapPrimitive2D(
aBitmapEx,
basegfx::B2DHomMatrix()));
aContent = primitive2d::Primitive2DContainer { xEmbedRefBitmap };
}
}
}
if(aContent.empty())
{
// buffering was not tried or did fail - reset remembered buffered size
// in any case
PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
pThat->mnDiscreteWidth = pThat->mnDiscreteHeight = 0;
// use children as default context
aContent = getChildren();
// check if content needs to be clipped
const basegfx::B2DRange aUnitRange(0.0, 0.0, 1.0, 1.0);
const basegfx::B2DRange aContentRange(getChildren().getB2DRange(rViewInformation));
if(!aUnitRange.isInside(aContentRange))
{
const Primitive2DReference xRef(
new MaskPrimitive2D(
basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aUnitRange)),
aContent));
aContent = Primitive2DContainer { xRef };
}
}
return aContent;
}
void PatternFillPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const
{
Primitive2DContainer aRetval;
if(!getChildren().empty())
{
if(!getReferenceRange().isEmpty() && getReferenceRange().getWidth() > 0.0 && getReferenceRange().getHeight() > 0.0)
{
const basegfx::B2DRange aMaskRange(getMask().getB2DRange());
if(!aMaskRange.isEmpty() && aMaskRange.getWidth() > 0.0 && aMaskRange.getHeight() > 0.0)
{
// create tiling matrices
std::vector< basegfx::B2DHomMatrix > aMatrices;
texture::GeoTexSvxTiled aTiling(getReferenceRange());
aTiling.appendTransformations(aMatrices);
// create content
const Primitive2DContainer aContent(createContent(rViewInformation));
// resize result
aRetval.resize(aMatrices.size());
// create one primitive for each matrix
for(size_t a(0); a < aMatrices.size(); a++)
{
aRetval[a] = new TransformPrimitive2D(
aMatrices[a],
aContent);
}
// transform result which is in unit coordinates to mask's object coordinates
{
const basegfx::B2DHomMatrix aMaskTransform(
basegfx::utils::createScaleTranslateB2DHomMatrix(
aMaskRange.getRange(),
aMaskRange.getMinimum()));
const Primitive2DReference xRef(
new TransformPrimitive2D(
aMaskTransform,
aRetval));
aRetval = Primitive2DContainer { xRef };
}
// embed result in mask
{
rContainer.push_back(
new MaskPrimitive2D(
getMask(),
aRetval));
}
}
}
}
}
PatternFillPrimitive2D::PatternFillPrimitive2D(
const basegfx::B2DPolyPolygon& rMask,
const Primitive2DContainer& rChildren,
const basegfx::B2DRange& rReferenceRange)
: BufferedDecompositionPrimitive2D(),
maMask(rMask),
maChildren(rChildren),
maReferenceRange(rReferenceRange),
mnDiscreteWidth(0),
mnDiscreteHeight(0)
{
}
bool PatternFillPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
{
if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
{
const PatternFillPrimitive2D& rCompare = static_cast< const PatternFillPrimitive2D& >(rPrimitive);
return (getMask() == rCompare.getMask()
&& getChildren() == rCompare.getChildren()
&& getReferenceRange() == rCompare.getReferenceRange());
}
return false;
}
basegfx::B2DRange PatternFillPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /* rViewInformation */ ) const
{
return getMask().getB2DRange();
}
void PatternFillPrimitive2D::get2DDecomposition(Primitive2DDecompositionVisitor& rVisitor, const geometry::ViewInformation2D& rViewInformation) const
{
// The existing bufferd decomposition uses a buffer in the remembered
// size or none if sizes are zero. Get new needed sizes which depend on
// the given ViewInformation
bool bResetBuffering = false;
sal_uInt32 nW(0);
sal_uInt32 nH(0);
calculateNeededDiscreteBufferSize(nW, nH, rViewInformation);
const bool bBufferingCurrentlyUsed(0 != mnDiscreteWidth && 0 != mnDiscreteHeight);
const bool bBufferingNextUsed(0 != nW && 0 != nH);
if(bBufferingNextUsed)
{
// buffering is now possible
if(bBufferingCurrentlyUsed)
{
if(nW > mnDiscreteWidth || nH > mnDiscreteHeight)
{
// Higher resolution is needed than used in the existing buffered
// decomposition - create new one
bResetBuffering = true;
}
else if(double(nW * nH) / double(mnDiscreteWidth * mnDiscreteHeight) <= 0.5)
{
// Size has shrunk for 50% or more - it's worth to refresh the buffering
// to spare some resources
bResetBuffering = true;
}
}
else
{
// currently no buffering used - reset evtl. unbuffered
// decomposition to start buffering
bResetBuffering = true;
}
}
else
{
// buffering is no longer possible
if(bBufferingCurrentlyUsed)
{
// reset decomposition to allow creation of unbuffered one
bResetBuffering = true;
}
}
if(bResetBuffering)
{
PatternFillPrimitive2D* pThat = const_cast< PatternFillPrimitive2D* >(this);
pThat->mnDiscreteWidth = nW;
pThat->mnDiscreteHeight = nH;
pThat->setBuffered2DDecomposition(Primitive2DContainer());
}
// call parent
BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
}
sal_Int64 SAL_CALL PatternFillPrimitive2D::estimateUsage()
{
size_t nRet(0);
for (auto& it : getChildren())
{
uno::Reference<util::XAccounting> const xAcc(it, uno::UNO_QUERY);
if (xAcc.is())
{
nRet += xAcc->estimateUsage();
}
}
return nRet;
}
// provide unique ID
ImplPrimitive2DIDBlock(PatternFillPrimitive2D, PRIMITIVE2D_ID_PATTERNFILLPRIMITIVE2D)
} // end of namespace primitive2d
} // end of namespace drawinglayer
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|