/src/libreoffice/canvas/source/vcl/canvashelper_texturefill.cxx
Line | Count | Source |
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 | | |
20 | | #include <sal/config.h> |
21 | | |
22 | | #include <cstdlib> |
23 | | #include <tuple> |
24 | | |
25 | | #include <basegfx/matrix/b2dhommatrix.hxx> |
26 | | #include <basegfx/numeric/ftools.hxx> |
27 | | #include <basegfx/point/b2dpoint.hxx> |
28 | | #include <basegfx/polygon/b2dpolygon.hxx> |
29 | | #include <basegfx/polygon/b2dpolygontools.hxx> |
30 | | #include <basegfx/range/b2drectangle.hxx> |
31 | | #include <basegfx/utils/canvastools.hxx> |
32 | | #include <basegfx/utils/keystoplerp.hxx> |
33 | | #include <basegfx/utils/lerp.hxx> |
34 | | #include <basegfx/utils/tools.hxx> |
35 | | #include <com/sun/star/rendering/TexturingMode.hpp> |
36 | | #include <rtl/math.hxx> |
37 | | #include <comphelper/diagnose_ex.hxx> |
38 | | #include <tools/poly.hxx> |
39 | | #include <vcl/alpha.hxx> |
40 | | #include <vcl/bitmap.hxx> |
41 | | #include <vcl/canvastools.hxx> |
42 | | #include <vcl/virdev.hxx> |
43 | | #include <vcl/gradient.hxx> |
44 | | |
45 | | #include <canvas/canvastools.hxx> |
46 | | #include <parametricpolypolygon.hxx> |
47 | | |
48 | | #include "canvashelper.hxx" |
49 | | #include "impltools.hxx" |
50 | | |
51 | | |
52 | | using namespace ::com::sun::star; |
53 | | |
54 | | namespace vclcanvas |
55 | | { |
56 | | namespace |
57 | | { |
58 | | bool textureFill( OutputDevice& rOutDev, |
59 | | const GraphicObject& rGraphic, |
60 | | const ::Point& rPosPixel, |
61 | | const ::Size& rNextTileX, |
62 | | const ::Size& rNextTileY, |
63 | | sal_Int32 nTilesX, |
64 | | sal_Int32 nTilesY, |
65 | | const ::Size& rTileSize, |
66 | | const GraphicAttr& rAttr) |
67 | 0 | { |
68 | 0 | bool bRet( false ); |
69 | 0 | Point aCurrPos; |
70 | 0 | int nX, nY; |
71 | |
|
72 | 0 | for( nY=0; nY < nTilesY; ++nY ) |
73 | 0 | { |
74 | 0 | aCurrPos.setX( rPosPixel.X() + nY*rNextTileY.Width() ); |
75 | 0 | aCurrPos.setY( rPosPixel.Y() + nY*rNextTileY.Height() ); |
76 | |
|
77 | 0 | for( nX=0; nX < nTilesX; ++nX ) |
78 | 0 | { |
79 | | // update return value. This method should return true, if |
80 | | // at least one of the looped Draws succeeded. |
81 | 0 | bRet |= rGraphic.Draw(rOutDev, |
82 | 0 | aCurrPos, |
83 | 0 | rTileSize, |
84 | 0 | &rAttr); |
85 | |
|
86 | 0 | aCurrPos.AdjustX(rNextTileX.Width() ); |
87 | 0 | aCurrPos.AdjustY(rNextTileX.Height() ); |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | 0 | return bRet; |
92 | 0 | } |
93 | | |
94 | | |
95 | | /** Fill linear or axial gradient |
96 | | |
97 | | Since most of the code for linear and axial gradients are |
98 | | the same, we've a unified method here |
99 | | */ |
100 | | void fillLinearGradient( OutputDevice& rOutDev, |
101 | | const ::basegfx::B2DHomMatrix& rTextureTransform, |
102 | | const ::tools::Rectangle& rBounds, |
103 | | unsigned int nStepCount, |
104 | | const ::canvas::ParametricPolyPolygon::Values& rValues, |
105 | | const std::vector< ::Color >& rColors ) |
106 | 0 | { |
107 | | // determine general position of gradient in relation to |
108 | | // the bound rect |
109 | | // ===================================================== |
110 | |
|
111 | 0 | ::basegfx::B2DPoint aLeftTop( 0.0, 0.0 ); |
112 | 0 | ::basegfx::B2DPoint aLeftBottom( 0.0, 1.0 ); |
113 | 0 | ::basegfx::B2DPoint aRightTop( 1.0, 0.0 ); |
114 | 0 | ::basegfx::B2DPoint aRightBottom( 1.0, 1.0 ); |
115 | |
|
116 | 0 | aLeftTop *= rTextureTransform; |
117 | 0 | aLeftBottom *= rTextureTransform; |
118 | 0 | aRightTop *= rTextureTransform; |
119 | 0 | aRightBottom*= rTextureTransform; |
120 | | |
121 | | // calc length of bound rect diagonal |
122 | 0 | const ::basegfx::B2DVector aBoundRectDiagonal( |
123 | 0 | vcl::unotools::b2DPointFromPoint( rBounds.TopLeft() ) - |
124 | 0 | vcl::unotools::b2DPointFromPoint( rBounds.BottomRight() ) ); |
125 | 0 | const double nDiagonalLength( aBoundRectDiagonal.getLength() ); |
126 | | |
127 | | // create direction of gradient: |
128 | | // _______ |
129 | | // | | | |
130 | | // -> | | | ... |
131 | | // | | | |
132 | | // ------- |
133 | 0 | ::basegfx::B2DVector aDirection( aRightTop - aLeftTop ); |
134 | 0 | aDirection.normalize(); |
135 | | |
136 | | // now, we potentially have to enlarge our gradient area |
137 | | // atop and below the transformed [0,1]x[0,1] unit rect, |
138 | | // for the gradient to fill the complete bound rect. |
139 | 0 | ::basegfx::utils::infiniteLineFromParallelogram( aLeftTop, |
140 | 0 | aLeftBottom, |
141 | 0 | aRightTop, |
142 | 0 | aRightBottom, |
143 | 0 | vcl::unotools::b2DRectangleFromRectangle(rBounds) ); |
144 | | |
145 | | |
146 | | // render gradient |
147 | | // =============== |
148 | | |
149 | | // First try to use directly VCL's DrawGradient(), as that one is generally |
150 | | // a better choice than here decomposing to polygons. The VCL API allows |
151 | | // only 2 colors, but that should generally do. |
152 | | // Do not use nStepCount, it limits optimized implementations, and it's computed |
153 | | // by vclcanvas based on number of colors, so it's practically irrelevant. |
154 | | |
155 | | // 2 colors and 2 stops (at 0 and 1) is a linear gradient: |
156 | 0 | if( rColors.size() == 2 && rValues.maStops.size() == 2 && rValues.maStops[0] == 0 && rValues.maStops[1] == 1) |
157 | 0 | { |
158 | | // tdf#144073 and tdf#147645: use bounds and angle for gradient |
159 | | // Passing an expanded, rotated polygon noticeably modifies the |
160 | | // drawing of the gradient in a slideshow due to moving of the |
161 | | // starting and ending colors far off the edges of the drawing |
162 | | // surface. So try another way and set the angle of the |
163 | | // gradient and draw only the unadjusted bounds. |
164 | 0 | Gradient vclGradient( css::awt::GradientStyle_LINEAR, rColors[ 0 ], rColors[ 1 ] ); |
165 | 0 | double fRotate = atan2( aDirection.getY(), aDirection.getX() ); |
166 | 0 | const double nAngleInTenthOfDegrees = 3600.0 - basegfx::rad2deg<10>( fRotate ) + 900.0; |
167 | 0 | vclGradient.SetAngle( Degree10( ::basegfx::fround( nAngleInTenthOfDegrees ) ) ); |
168 | 0 | rOutDev.DrawGradient( rBounds, vclGradient ); |
169 | 0 | return; |
170 | 0 | } |
171 | | // 3 colors with first and last being equal and 3 stops (at 0, 0.5 and 1) is an axial gradient: |
172 | 0 | if( rColors.size() == 3 && rColors[ 0 ] == rColors[ 2 ] |
173 | 0 | && rValues.maStops.size() == 3 && rValues.maStops[0] == 0 |
174 | 0 | && rValues.maStops[1] == 0.5 && rValues.maStops[2] == 1) |
175 | 0 | { |
176 | | // tdf#144073 and tdf#147645: use bounds and angle for gradient |
177 | | // Passing an expanded, rotated polygon noticeably modifies the |
178 | | // drawing of the gradient in a slideshow due to moving of the |
179 | | // starting and ending colors far off the edges of the drawing |
180 | | // surface. So try another way and set the angle of the |
181 | | // gradient and draw only the unadjusted bounds. |
182 | 0 | Gradient vclGradient( css::awt::GradientStyle_AXIAL, rColors[ 1 ], rColors[ 0 ] ); |
183 | 0 | double fRotate = atan2( aDirection.getY(), aDirection.getX() ); |
184 | 0 | const double nAngleInTenthOfDegrees = 3600.0 - basegfx::rad2deg<10>( fRotate ) + 900.0; |
185 | 0 | vclGradient.SetAngle( Degree10( ::basegfx::fround( nAngleInTenthOfDegrees ) ) ); |
186 | 0 | rOutDev.DrawGradient( rBounds, vclGradient ); |
187 | 0 | return; |
188 | 0 | } |
189 | | |
190 | | // for linear gradients, it's easy to render |
191 | | // non-overlapping polygons: just split the gradient into |
192 | | // nStepCount small strips. Prepare the strip now. |
193 | | |
194 | | // For performance reasons, we create a temporary VCL |
195 | | // polygon here, keep it all the way and only change the |
196 | | // vertex values in the loop below (as ::Polygon is a |
197 | | // pimpl class, creating one every loop turn would really |
198 | | // stress the mem allocator) |
199 | 0 | ::tools::Polygon aTempPoly( static_cast<sal_uInt16>(5) ); |
200 | |
|
201 | 0 | OSL_ENSURE( nStepCount >= 3, |
202 | 0 | "fillLinearGradient(): stepcount smaller than 3" ); |
203 | | |
204 | | |
205 | | // fill initial strip (extending two times the bound rect's |
206 | | // diagonal to the 'left' |
207 | | |
208 | | |
209 | | // calculate left edge, by moving left edge of the |
210 | | // gradient rect two times the bound rect's diagonal to |
211 | | // the 'left'. Since we postpone actual rendering into the |
212 | | // loop below, we set the _right_ edge here, which will be |
213 | | // readily copied into the left edge in the loop below |
214 | 0 | const ::basegfx::B2DPoint aPoint1( aLeftTop - 2.0*nDiagonalLength*aDirection ); |
215 | 0 | aTempPoly[1] = ::Point( ::basegfx::fround<::tools::Long>( aPoint1.getX() ), |
216 | 0 | ::basegfx::fround<::tools::Long>( aPoint1.getY() ) ); |
217 | |
|
218 | 0 | const ::basegfx::B2DPoint aPoint2( aLeftBottom - 2.0*nDiagonalLength*aDirection ); |
219 | 0 | aTempPoly[2] = ::Point( ::basegfx::fround<::tools::Long>( aPoint2.getX() ), |
220 | 0 | ::basegfx::fround<::tools::Long>( aPoint2.getY() ) ); |
221 | | |
222 | | |
223 | | // iteratively render all other strips |
224 | | |
225 | | |
226 | | // ensure that nStepCount matches color stop parity, to |
227 | | // have a well-defined middle color e.g. for axial |
228 | | // gradients. |
229 | 0 | if( (rColors.size() % 2) != (nStepCount % 2) ) |
230 | 0 | ++nStepCount; |
231 | |
|
232 | 0 | rOutDev.SetLineColor(); |
233 | |
|
234 | 0 | basegfx::utils::KeyStopLerp aLerper(rValues.maStops); |
235 | | |
236 | | // only iterate nStepCount-1 steps, as the last strip is |
237 | | // explicitly painted below |
238 | 0 | for( unsigned int i=0; i<nStepCount-1; ++i ) |
239 | 0 | { |
240 | 0 | std::ptrdiff_t nIndex; |
241 | 0 | double fAlpha; |
242 | 0 | std::tie(nIndex,fAlpha)=aLerper.lerp(double(i)/nStepCount); |
243 | |
|
244 | 0 | rOutDev.SetFillColor( |
245 | 0 | Color( static_cast<sal_uInt8>(basegfx::utils::lerp(rColors[nIndex].GetRed(),rColors[nIndex+1].GetRed(),fAlpha)), |
246 | 0 | static_cast<sal_uInt8>(basegfx::utils::lerp(rColors[nIndex].GetGreen(),rColors[nIndex+1].GetGreen(),fAlpha)), |
247 | 0 | static_cast<sal_uInt8>(basegfx::utils::lerp(rColors[nIndex].GetBlue(),rColors[nIndex+1].GetBlue(),fAlpha)) )); |
248 | | |
249 | | // copy right edge of polygon to left edge (and also |
250 | | // copy the closing point) |
251 | 0 | aTempPoly[0] = aTempPoly[4] = aTempPoly[1]; |
252 | 0 | aTempPoly[3] = aTempPoly[2]; |
253 | | |
254 | | // calculate new right edge, from interpolating |
255 | | // between start and end line. Note that i is |
256 | | // increased by one, to account for the fact that we |
257 | | // calculate the right border here (whereas the fill |
258 | | // color is governed by the left edge) |
259 | 0 | const ::basegfx::B2DPoint aPoint3( |
260 | 0 | (nStepCount - i-1)/double(nStepCount)*aLeftTop + |
261 | 0 | (i+1)/double(nStepCount)*aRightTop ); |
262 | 0 | aTempPoly[1] = ::Point( ::basegfx::fround<::tools::Long>( aPoint3.getX() ), |
263 | 0 | ::basegfx::fround<::tools::Long>( aPoint3.getY() ) ); |
264 | |
|
265 | 0 | const ::basegfx::B2DPoint aPoint4( |
266 | 0 | (nStepCount - i-1)/double(nStepCount)*aLeftBottom + |
267 | 0 | (i+1)/double(nStepCount)*aRightBottom ); |
268 | 0 | aTempPoly[2] = ::Point( ::basegfx::fround<::tools::Long>( aPoint4.getX() ), |
269 | 0 | ::basegfx::fround<::tools::Long>( aPoint4.getY() ) ); |
270 | |
|
271 | 0 | rOutDev.DrawPolygon( aTempPoly ); |
272 | 0 | } |
273 | | |
274 | | // fill final strip (extending two times the bound rect's |
275 | | // diagonal to the 'right' |
276 | | |
277 | | |
278 | | // copy right edge of polygon to left edge (and also |
279 | | // copy the closing point) |
280 | 0 | aTempPoly[0] = aTempPoly[4] = aTempPoly[1]; |
281 | 0 | aTempPoly[3] = aTempPoly[2]; |
282 | | |
283 | | // calculate new right edge, by moving right edge of the |
284 | | // gradient rect two times the bound rect's diagonal to |
285 | | // the 'right'. |
286 | 0 | const ::basegfx::B2DPoint aPoint3( aRightTop + 2.0*nDiagonalLength*aDirection ); |
287 | 0 | aTempPoly[0] = aTempPoly[4] = ::Point( ::basegfx::fround<::tools::Long>( aPoint3.getX() ), |
288 | 0 | ::basegfx::fround<::tools::Long>( aPoint3.getY() ) ); |
289 | |
|
290 | 0 | const ::basegfx::B2DPoint aPoint4( aRightBottom + 2.0*nDiagonalLength*aDirection ); |
291 | 0 | aTempPoly[3] = ::Point( ::basegfx::fround<::tools::Long>( aPoint4.getX() ), |
292 | 0 | ::basegfx::fround<::tools::Long>( aPoint4.getY() ) ); |
293 | |
|
294 | 0 | rOutDev.SetFillColor( rColors.back() ); |
295 | |
|
296 | 0 | rOutDev.DrawPolygon( aTempPoly ); |
297 | 0 | } |
298 | | |
299 | | void fillPolygonalGradient( OutputDevice& rOutDev, |
300 | | const ::basegfx::B2DHomMatrix& rTextureTransform, |
301 | | const ::tools::Rectangle& rBounds, |
302 | | unsigned int nStepCount, |
303 | | const ::canvas::ParametricPolyPolygon::Values& rValues, |
304 | | const std::vector< ::Color >& rColors ) |
305 | 0 | { |
306 | 0 | const ::basegfx::B2DPolygon& rGradientPoly( rValues.maGradientPoly ); |
307 | |
|
308 | 0 | ENSURE_OR_THROW( rGradientPoly.count() > 2, |
309 | 0 | "fillPolygonalGradient(): polygon without area given" ); |
310 | | |
311 | | // For performance reasons, we create a temporary VCL polygon |
312 | | // here, keep it all the way and only change the vertex values |
313 | | // in the loop below (as ::Polygon is a pimpl class, creating |
314 | | // one every loop turn would really stress the mem allocator) |
315 | 0 | ::basegfx::B2DPolygon aOuterPoly( rGradientPoly ); |
316 | 0 | ::basegfx::B2DPolygon aInnerPoly; |
317 | | |
318 | | // subdivide polygon _before_ rendering, would otherwise have |
319 | | // to be performed on every loop turn. |
320 | 0 | if( aOuterPoly.areControlPointsUsed() ) |
321 | 0 | aOuterPoly = ::basegfx::utils::adaptiveSubdivideByAngle(aOuterPoly); |
322 | |
|
323 | 0 | aInnerPoly = aOuterPoly; |
324 | | |
325 | | // only transform outer polygon _after_ copying it into |
326 | | // aInnerPoly, because inner polygon has to be scaled before |
327 | | // the actual texture transformation takes place |
328 | 0 | aOuterPoly.transform( rTextureTransform ); |
329 | | |
330 | | // determine overall transformation for inner polygon (might |
331 | | // have to be prefixed by anisotrophic scaling) |
332 | 0 | ::basegfx::B2DHomMatrix aInnerPolygonTransformMatrix; |
333 | | |
334 | | |
335 | | // apply scaling (possibly anisotrophic) to inner polygon |
336 | | |
337 | | |
338 | | // scale inner polygon according to aspect ratio: for |
339 | | // wider-than-tall bounds (nAspectRatio > 1.0), the inner |
340 | | // polygon, representing the gradient focus, must have |
341 | | // non-zero width. Specifically, a bound rect twice as wide as |
342 | | // tall has a focus polygon of half its width. |
343 | 0 | const double nAspectRatio( rValues.mnAspectRatio ); |
344 | 0 | if( nAspectRatio > 1.0 ) |
345 | 0 | { |
346 | | // width > height case |
347 | 0 | aInnerPolygonTransformMatrix.scale( 1.0 - 1.0/nAspectRatio, |
348 | 0 | 0.0 ); |
349 | 0 | } |
350 | 0 | else if( nAspectRatio < 1.0 ) |
351 | 0 | { |
352 | | // width < height case |
353 | 0 | aInnerPolygonTransformMatrix.scale( 0.0, |
354 | 0 | 1.0 - nAspectRatio ); |
355 | 0 | } |
356 | 0 | else |
357 | 0 | { |
358 | | // isotrophic case |
359 | 0 | aInnerPolygonTransformMatrix.scale( 0.0, 0.0 ); |
360 | 0 | } |
361 | | |
362 | | // and finally, add texture transform to it. |
363 | 0 | aInnerPolygonTransformMatrix *= rTextureTransform; |
364 | | |
365 | | // apply final matrix to polygon |
366 | 0 | aInnerPoly.transform( aInnerPolygonTransformMatrix ); |
367 | | |
368 | |
|
369 | 0 | const sal_uInt32 nNumPoints( aOuterPoly.count() ); |
370 | 0 | ::tools::Polygon aTempPoly( static_cast<sal_uInt16>(nNumPoints+1) ); |
371 | | |
372 | | // increase number of steps by one: polygonal gradients have |
373 | | // the outermost polygon rendered in rColor2, and the |
374 | | // innermost in rColor1. The innermost polygon will never |
375 | | // have zero area, thus, we must divide the interval into |
376 | | // nStepCount+1 steps. For example, to create 3 steps: |
377 | | |
378 | | // | | |
379 | | // |-------|-------|-------| |
380 | | // | | |
381 | | // 3 2 1 0 |
382 | | |
383 | | // This yields 4 tick marks, where 0 is never attained (since |
384 | | // zero-area polygons typically don't display perceivable |
385 | | // color). |
386 | 0 | ++nStepCount; |
387 | |
|
388 | 0 | rOutDev.SetLineColor(); |
389 | |
|
390 | 0 | basegfx::utils::KeyStopLerp aLerper(rValues.maStops); |
391 | | |
392 | | // fill background |
393 | 0 | rOutDev.SetFillColor( rColors.front() ); |
394 | 0 | rOutDev.DrawRect( rBounds ); |
395 | | |
396 | | // render polygon |
397 | | // ============== |
398 | |
|
399 | 0 | for( unsigned int i=1,p; i<nStepCount; ++i ) |
400 | 0 | { |
401 | 0 | const double fT( i/double(nStepCount) ); |
402 | |
|
403 | 0 | std::ptrdiff_t nIndex; |
404 | 0 | double fAlpha; |
405 | 0 | std::tie(nIndex,fAlpha)=aLerper.lerp(fT); |
406 | | |
407 | | // lerp color |
408 | 0 | rOutDev.SetFillColor( |
409 | 0 | Color( static_cast<sal_uInt8>(basegfx::utils::lerp(rColors[nIndex].GetRed(),rColors[nIndex+1].GetRed(),fAlpha)), |
410 | 0 | static_cast<sal_uInt8>(basegfx::utils::lerp(rColors[nIndex].GetGreen(),rColors[nIndex+1].GetGreen(),fAlpha)), |
411 | 0 | static_cast<sal_uInt8>(basegfx::utils::lerp(rColors[nIndex].GetBlue(),rColors[nIndex+1].GetBlue(),fAlpha)) )); |
412 | | |
413 | | // scale and render polygon, by interpolating between |
414 | | // outer and inner polygon. |
415 | |
|
416 | 0 | for( p=0; p<nNumPoints; ++p ) |
417 | 0 | { |
418 | 0 | const ::basegfx::B2DPoint& rOuterPoint( aOuterPoly.getB2DPoint(p) ); |
419 | 0 | const ::basegfx::B2DPoint& rInnerPoint( aInnerPoly.getB2DPoint(p) ); |
420 | |
|
421 | 0 | aTempPoly[static_cast<sal_uInt16>(p)] = ::Point( |
422 | 0 | basegfx::fround<::tools::Long>( fT*rInnerPoint.getX() + (1-fT)*rOuterPoint.getX() ), |
423 | 0 | basegfx::fround<::tools::Long>( fT*rInnerPoint.getY() + (1-fT)*rOuterPoint.getY() ) ); |
424 | 0 | } |
425 | | |
426 | | // close polygon explicitly |
427 | 0 | aTempPoly[static_cast<sal_uInt16>(p)] = aTempPoly[0]; |
428 | | |
429 | | // TODO(P1): compare with vcl/source/gdi/outdev4.cxx, |
430 | | // OutputDevice::ImplDrawComplexGradient(), there's a note |
431 | | // that on some VDev's, rendering disjunct poly-polygons |
432 | | // is faster! |
433 | 0 | rOutDev.DrawPolygon( aTempPoly ); |
434 | 0 | } |
435 | 0 | } |
436 | | |
437 | | void doGradientFill( OutputDevice& rOutDev, |
438 | | const ::canvas::ParametricPolyPolygon::Values& rValues, |
439 | | const std::vector< ::Color >& rColors, |
440 | | const ::basegfx::B2DHomMatrix& rTextureTransform, |
441 | | const ::tools::Rectangle& rBounds, |
442 | | unsigned int nStepCount ) |
443 | 0 | { |
444 | 0 | switch( rValues.meType ) |
445 | 0 | { |
446 | 0 | case ::canvas::ParametricPolyPolygon::GradientType::Linear: |
447 | 0 | fillLinearGradient( rOutDev, |
448 | 0 | rTextureTransform, |
449 | 0 | rBounds, |
450 | 0 | nStepCount, |
451 | 0 | rValues, |
452 | 0 | rColors ); |
453 | 0 | break; |
454 | | |
455 | 0 | case ::canvas::ParametricPolyPolygon::GradientType::Elliptical: |
456 | 0 | case ::canvas::ParametricPolyPolygon::GradientType::Rectangular: |
457 | 0 | fillPolygonalGradient( rOutDev, |
458 | 0 | rTextureTransform, |
459 | 0 | rBounds, |
460 | 0 | nStepCount, |
461 | 0 | rValues, |
462 | 0 | rColors ); |
463 | 0 | break; |
464 | | |
465 | 0 | default: |
466 | 0 | ENSURE_OR_THROW( false, |
467 | 0 | "CanvasHelper::doGradientFill(): Unexpected case" ); |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | | int numColorSteps( const ::Color& rColor1, const ::Color& rColor2 ) |
472 | 0 | { |
473 | 0 | return std::max( |
474 | 0 | std::abs( rColor1.GetRed() - rColor2.GetRed() ), |
475 | 0 | std::max( |
476 | 0 | std::abs( rColor1.GetGreen() - rColor2.GetGreen() ), |
477 | 0 | std::abs( rColor1.GetBlue() - rColor2.GetBlue() ) ) ); |
478 | 0 | } |
479 | | |
480 | | bool gradientFill( OutputDevice& rOutDev, |
481 | | OutputDevice* p2ndOutDev, |
482 | | const ::canvas::ParametricPolyPolygon::Values& rValues, |
483 | | const std::vector< ::Color >& rColors, |
484 | | const ::tools::PolyPolygon& rPoly, |
485 | | const rendering::ViewState& viewState, |
486 | | const rendering::RenderState& renderState, |
487 | | const rendering::Texture& texture, |
488 | | int nTransparency ) |
489 | 0 | { |
490 | | // TODO(T2): It is maybe necessary to lock here, should |
491 | | // maGradientPoly someday cease to be const. But then, beware of |
492 | | // deadlocks, canvashelper calls this method with locked own |
493 | | // mutex. |
494 | | |
495 | | // calc step size |
496 | |
|
497 | 0 | int nColorSteps = 0; |
498 | 0 | for( size_t i=0; i<rColors.size()-1; ++i ) |
499 | 0 | nColorSteps += numColorSteps(rColors[i],rColors[i+1]); |
500 | |
|
501 | 0 | ::basegfx::B2DHomMatrix aTotalTransform; |
502 | 0 | const int nStepCount= |
503 | 0 | ::canvastools::calcGradientStepCount(aTotalTransform, |
504 | 0 | viewState, |
505 | 0 | renderState, |
506 | 0 | texture, |
507 | 0 | nColorSteps); |
508 | |
|
509 | 0 | rOutDev.SetLineColor(); |
510 | | |
511 | | // determine maximal bound rect of texture-filled |
512 | | // polygon |
513 | 0 | const ::tools::Rectangle aPolygonDeviceRectOrig( |
514 | 0 | rPoly.GetBoundRect() ); |
515 | |
|
516 | 0 | if( vclcanvastools::isRectangle( rPoly ) ) |
517 | 0 | { |
518 | | // use optimized output path |
519 | | |
520 | | |
521 | | // this distinction really looks like a |
522 | | // micro-optimization, but in fact greatly speeds up |
523 | | // especially complex gradients. That's because when using |
524 | | // clipping, we can output polygons instead of |
525 | | // poly-polygons, and don't have to output the gradient |
526 | | // twice for XOR |
527 | |
|
528 | 0 | rOutDev.Push( vcl::PushFlags::CLIPREGION ); |
529 | 0 | rOutDev.IntersectClipRegion( aPolygonDeviceRectOrig ); |
530 | 0 | doGradientFill( rOutDev, |
531 | 0 | rValues, |
532 | 0 | rColors, |
533 | 0 | aTotalTransform, |
534 | 0 | aPolygonDeviceRectOrig, |
535 | 0 | nStepCount ); |
536 | 0 | rOutDev.Pop(); |
537 | |
|
538 | 0 | if( p2ndOutDev && nTransparency < 253 ) |
539 | 0 | { |
540 | | // HACK. Normally, CanvasHelper does not care about |
541 | | // actually what mp2ndOutDev is... well, here we do & |
542 | | // assume a 1bpp target - everything beyond 97% |
543 | | // transparency is fully transparent |
544 | 0 | p2ndOutDev->SetFillColor( COL_BLACK ); |
545 | 0 | p2ndOutDev->DrawRect( aPolygonDeviceRectOrig ); |
546 | 0 | } |
547 | 0 | } |
548 | 0 | else |
549 | 0 | { |
550 | 0 | const vcl::Region aPolyClipRegion( rPoly ); |
551 | |
|
552 | 0 | rOutDev.Push( vcl::PushFlags::CLIPREGION ); |
553 | 0 | rOutDev.IntersectClipRegion( aPolyClipRegion ); |
554 | |
|
555 | 0 | doGradientFill( rOutDev, |
556 | 0 | rValues, |
557 | 0 | rColors, |
558 | 0 | aTotalTransform, |
559 | 0 | aPolygonDeviceRectOrig, |
560 | 0 | nStepCount ); |
561 | 0 | rOutDev.Pop(); |
562 | |
|
563 | 0 | if( p2ndOutDev && nTransparency < 253 ) |
564 | 0 | { |
565 | | // HACK. Normally, CanvasHelper does not care about |
566 | | // actually what mp2ndOutDev is... well, here we do & |
567 | | // assume a 1bpp target - everything beyond 97% |
568 | | // transparency is fully transparent |
569 | 0 | p2ndOutDev->SetFillColor( COL_BLACK ); |
570 | 0 | p2ndOutDev->DrawPolyPolygon( rPoly ); |
571 | 0 | } |
572 | 0 | } |
573 | |
|
574 | | #ifdef DEBUG_CANVAS_CANVASHELPER_TEXTUREFILL |
575 | | // extra-verbosity |
576 | | { |
577 | | ::basegfx::B2DRectangle aRect(0.0, 0.0, 1.0, 1.0); |
578 | | ::basegfx::B2DHomMatrix aTextureTransform; |
579 | | ::basegfx::B2DRectangle aTextureDeviceRect = ::canvastools::calcTransformedRectBounds( |
580 | | aRect, |
581 | | aTextureTransform ); |
582 | | rOutDev.SetLineColor( COL_RED ); |
583 | | rOutDev.SetFillColor(); |
584 | | rOutDev.DrawRect( vcl::unotools::rectangleFromB2DRectangle( aTextureDeviceRect ) ); |
585 | | |
586 | | rOutDev.SetLineColor( COL_BLUE ); |
587 | | ::tools::Polygon aPoly1( |
588 | | vcl::unotools::rectangleFromB2DRectangle( aRect )); |
589 | | ::basegfx::B2DPolygon aPoly2( aPoly1.getB2DPolygon() ); |
590 | | aPoly2.transform( aTextureTransform ); |
591 | | ::tools::Polygon aPoly3( aPoly2 ); |
592 | | rOutDev.DrawPolygon( aPoly3 ); |
593 | | } |
594 | | #endif |
595 | |
|
596 | 0 | return true; |
597 | 0 | } |
598 | | } |
599 | | |
600 | | uno::Reference< rendering::XCachedPrimitive > CanvasHelper::fillTexturedPolyPolygon( const rendering::XCanvas* pCanvas, |
601 | | const uno::Reference< rendering::XPolyPolygon2D >& xPolyPolygon, |
602 | | const rendering::ViewState& viewState, |
603 | | const rendering::RenderState& renderState, |
604 | | const uno::Sequence< rendering::Texture >& textures ) |
605 | 0 | { |
606 | 0 | ENSURE_ARG_OR_THROW( xPolyPolygon.is(), |
607 | 0 | "CanvasHelper::fillPolyPolygon(): polygon is NULL"); |
608 | 0 | ENSURE_ARG_OR_THROW( textures.hasElements(), |
609 | 0 | "CanvasHelper::fillTexturedPolyPolygon: empty texture sequence"); |
610 | |
|
611 | 0 | if( mpOutDevProvider ) |
612 | 0 | { |
613 | 0 | vclcanvastools::OutDevStateKeeper aStateKeeper( mpProtectedOutDevProvider ); |
614 | |
|
615 | 0 | const int nTransparency( setupOutDevState( viewState, renderState, IGNORE_COLOR ) ); |
616 | 0 | ::tools::PolyPolygon aPolyPoly( vclcanvastools::mapPolyPolygon( |
617 | 0 | ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPolyPolygon), |
618 | 0 | viewState, renderState ) ); |
619 | | |
620 | | // TODO(F1): Multi-texturing |
621 | 0 | if( textures[0].Gradient.is() ) |
622 | 0 | { |
623 | | // try to cast XParametricPolyPolygon2D reference to |
624 | | // our implementation class. |
625 | 0 | ::canvas::ParametricPolyPolygon* pGradient = |
626 | 0 | dynamic_cast< ::canvas::ParametricPolyPolygon* >( textures[0].Gradient.get() ); |
627 | |
|
628 | 0 | if( pGradient && pGradient->getValues().maColors.hasElements() ) |
629 | 0 | { |
630 | | // copy state from Gradient polypoly locally |
631 | | // (given object might change!) |
632 | 0 | const ::canvas::ParametricPolyPolygon::Values aValues( |
633 | 0 | pGradient->getValues() ); |
634 | |
|
635 | 0 | if( aValues.maColors.getLength() < 2 ) |
636 | 0 | { |
637 | 0 | rendering::RenderState aTempState=renderState; |
638 | 0 | aTempState.DeviceColor = aValues.maColors[0]; |
639 | 0 | fillPolyPolygon(pCanvas, xPolyPolygon, viewState, aTempState); |
640 | 0 | } |
641 | 0 | else |
642 | 0 | { |
643 | 0 | std::vector< ::Color > aColors(aValues.maColors.getLength()); |
644 | 0 | std::transform(&aValues.maColors[0], |
645 | 0 | &aValues.maColors[0]+aValues.maColors.getLength(), |
646 | 0 | aColors.begin(), |
647 | 0 | [](const uno::Sequence< double >& aColor) { |
648 | 0 | return vcl::unotools::stdColorSpaceSequenceToColor( aColor ); |
649 | 0 | } ); |
650 | | |
651 | | // TODO(E1): Return value |
652 | | // TODO(F1): FillRule |
653 | 0 | gradientFill( mpOutDevProvider->getOutDev(), |
654 | 0 | mp2ndOutDevProvider ? &mp2ndOutDevProvider->getOutDev() : nullptr, |
655 | 0 | aValues, |
656 | 0 | aColors, |
657 | 0 | aPolyPoly, |
658 | 0 | viewState, |
659 | 0 | renderState, |
660 | 0 | textures[0], |
661 | 0 | nTransparency ); |
662 | 0 | } |
663 | 0 | } |
664 | 0 | else |
665 | 0 | { |
666 | | // TODO(F1): The generic case is missing here |
667 | 0 | ENSURE_OR_THROW( false, |
668 | 0 | "CanvasHelper::fillTexturedPolyPolygon(): unknown parametric polygon encountered" ); |
669 | 0 | } |
670 | 0 | } |
671 | 0 | else if( textures[0].Bitmap.is() ) |
672 | 0 | { |
673 | 0 | geometry::IntegerSize2D aBmpSize( textures[0].Bitmap->getSize() ); |
674 | |
|
675 | 0 | ENSURE_ARG_OR_THROW( aBmpSize.Width != 0 && |
676 | 0 | aBmpSize.Height != 0, |
677 | 0 | "CanvasHelper::fillTexturedPolyPolygon(): zero-sized texture bitmap" ); |
678 | | |
679 | | // determine maximal bound rect of texture-filled |
680 | | // polygon |
681 | 0 | const ::tools::Rectangle aPolygonDeviceRect( |
682 | 0 | aPolyPoly.GetBoundRect() ); |
683 | | |
684 | | |
685 | | // first of all, determine whether we have a |
686 | | // drawBitmap() in disguise |
687 | | // ========================================= |
688 | |
|
689 | 0 | const bool bRectangularPolygon( vclcanvastools::isRectangle( aPolyPoly ) ); |
690 | |
|
691 | 0 | ::basegfx::B2DHomMatrix aTotalTransform; |
692 | 0 | ::canvastools::mergeViewAndRenderTransform(aTotalTransform, |
693 | 0 | viewState, |
694 | 0 | renderState); |
695 | 0 | ::basegfx::B2DHomMatrix aTextureTransform; |
696 | 0 | ::basegfx::unotools::homMatrixFromAffineMatrix( aTextureTransform, |
697 | 0 | textures[0].AffineTransform ); |
698 | |
|
699 | 0 | aTotalTransform *= aTextureTransform; |
700 | |
|
701 | 0 | const ::basegfx::B2DRectangle aRect(0.0, 0.0, 1.0, 1.0); |
702 | 0 | ::basegfx::B2DRectangle aTextureDeviceRect = ::canvastools::calcTransformedRectBounds( |
703 | 0 | aRect, |
704 | 0 | aTotalTransform ); |
705 | |
|
706 | 0 | const ::tools::Rectangle aIntegerTextureDeviceRect( |
707 | 0 | vcl::unotools::rectangleFromB2DRectangle( aTextureDeviceRect ) ); |
708 | |
|
709 | 0 | if( bRectangularPolygon && |
710 | 0 | aIntegerTextureDeviceRect == aPolygonDeviceRect ) |
711 | 0 | { |
712 | 0 | rendering::RenderState aLocalState( renderState ); |
713 | 0 | ::canvastools::appendToRenderState(aLocalState, |
714 | 0 | aTextureTransform); |
715 | 0 | ::basegfx::B2DHomMatrix aScaleCorrection; |
716 | 0 | aScaleCorrection.scale( 1.0/aBmpSize.Width, |
717 | 0 | 1.0/aBmpSize.Height ); |
718 | 0 | ::canvastools::appendToRenderState(aLocalState, |
719 | 0 | aScaleCorrection); |
720 | | |
721 | | // need alpha modulation? |
722 | 0 | if( !::rtl::math::approxEqual( textures[0].Alpha, |
723 | 0 | 1.0 ) ) |
724 | 0 | { |
725 | | // setup alpha modulation values |
726 | 0 | aLocalState.DeviceColor.realloc(4); |
727 | 0 | double* pColor = aLocalState.DeviceColor.getArray(); |
728 | 0 | pColor[0] = |
729 | 0 | pColor[1] = |
730 | 0 | pColor[2] = 0.0; |
731 | 0 | pColor[3] = textures[0].Alpha; |
732 | |
|
733 | 0 | return drawBitmapModulated( pCanvas, |
734 | 0 | textures[0].Bitmap, |
735 | 0 | viewState, |
736 | 0 | aLocalState ); |
737 | 0 | } |
738 | 0 | else |
739 | 0 | { |
740 | 0 | return drawBitmap( pCanvas, |
741 | 0 | textures[0].Bitmap, |
742 | 0 | viewState, |
743 | 0 | aLocalState ); |
744 | 0 | } |
745 | 0 | } |
746 | 0 | else |
747 | 0 | { |
748 | | // No easy mapping to drawBitmap() - calculate |
749 | | // texturing parameters |
750 | | // =========================================== |
751 | |
|
752 | 0 | ::Bitmap aBmp( vclcanvastools::bitmapFromXBitmap( textures[0].Bitmap ) ); |
753 | | |
754 | | // scale down bitmap to [0,1]x[0,1] rect, as required |
755 | | // from the XCanvas interface. |
756 | 0 | ::basegfx::B2DHomMatrix aScaling; |
757 | 0 | ::basegfx::B2DHomMatrix aPureTotalTransform; // pure view*render*texture transform |
758 | 0 | aScaling.scale( 1.0/aBmpSize.Width, |
759 | 0 | 1.0/aBmpSize.Height ); |
760 | |
|
761 | 0 | aTotalTransform = aTextureTransform * aScaling; |
762 | 0 | aPureTotalTransform = aTextureTransform; |
763 | | |
764 | | // combine with view and render transform |
765 | 0 | ::basegfx::B2DHomMatrix aMatrix; |
766 | 0 | ::canvastools::mergeViewAndRenderTransform(aMatrix, viewState, renderState); |
767 | | |
768 | | // combine all three transformations into one |
769 | | // global texture-to-device-space transformation |
770 | 0 | aTotalTransform *= aMatrix; |
771 | 0 | aPureTotalTransform *= aMatrix; |
772 | | |
773 | | // analyze transformation, and setup an |
774 | | // appropriate GraphicObject |
775 | 0 | ::basegfx::B2DVector aScale; |
776 | 0 | ::basegfx::B2DPoint aOutputPos; |
777 | 0 | double nRotate; |
778 | 0 | double nShearX; |
779 | 0 | aTotalTransform.decompose( aScale, aOutputPos, nRotate, nShearX ); |
780 | |
|
781 | 0 | GraphicAttr aGrfAttr; |
782 | 0 | GraphicObjectSharedPtr pGrfObj; |
783 | |
|
784 | 0 | if( ::basegfx::fTools::equalZero( nShearX ) ) |
785 | 0 | { |
786 | | // no shear, GraphicObject is enough (the |
787 | | // GraphicObject only supports scaling, rotation |
788 | | // and translation) |
789 | | |
790 | | // #i75339# don't apply mirror flags, having |
791 | | // negative size values is enough to make |
792 | | // GraphicObject flip the bitmap |
793 | | |
794 | | // The angle has to be mapped from radian to tenths of |
795 | | // degrees with the orientation reversed: [0,2Pi) -> |
796 | | // (3600,0]. Note that the original angle may have |
797 | | // values outside the [0,2Pi) interval. |
798 | 0 | const double nAngleInTenthOfDegrees (3600.0 - basegfx::rad2deg<10>(nRotate)); |
799 | 0 | aGrfAttr.SetRotation( Degree10(::basegfx::fround(nAngleInTenthOfDegrees)) ); |
800 | |
|
801 | 0 | pGrfObj = std::make_shared<GraphicObject>( aBmp ); |
802 | 0 | } |
803 | 0 | else |
804 | 0 | { |
805 | | // modify output position, to account for the fact |
806 | | // that transformBitmap() always normalizes its output |
807 | | // bitmap into the smallest enclosing box. |
808 | 0 | ::basegfx::B2DRectangle aDestRect = ::canvastools::calcTransformedRectBounds( |
809 | 0 | ::basegfx::B2DRectangle(0, |
810 | 0 | 0, |
811 | 0 | aBmpSize.Width, |
812 | 0 | aBmpSize.Height), |
813 | 0 | aMatrix ); |
814 | |
|
815 | 0 | aOutputPos.setX( aDestRect.getMinX() ); |
816 | 0 | aOutputPos.setY( aDestRect.getMinY() ); |
817 | | |
818 | | // complex transformation, use generic affine bitmap |
819 | | // transformation |
820 | 0 | aBmp = vclcanvastools::transformBitmap( aBmp, aTotalTransform); |
821 | |
|
822 | 0 | pGrfObj = std::make_shared<GraphicObject>( aBmp ); |
823 | | |
824 | | // clear scale values, generated bitmap already |
825 | | // contains scaling |
826 | 0 | aScale.setX( 1.0 ); aScale.setY( 1.0 ); |
827 | | |
828 | | // update bitmap size, bitmap has changed above. |
829 | 0 | aBmpSize = vcl::unotools::integerSize2DFromSize(aBmp.GetSizePixel()); |
830 | 0 | } |
831 | | |
832 | | |
833 | | // render texture tiled into polygon |
834 | | // ================================= |
835 | | |
836 | | // calc device space direction vectors. We employ |
837 | | // the following approach for tiled output: the |
838 | | // texture bitmap is output in texture space |
839 | | // x-major order, i.e. tile neighbors in texture |
840 | | // space x direction are rendered back-to-back in |
841 | | // device coordinate space (after the full device |
842 | | // transformation). Thus, the aNextTile* vectors |
843 | | // denote the output position updates in device |
844 | | // space, to get from one tile to the next. |
845 | 0 | ::basegfx::B2DVector aNextTileX( 1.0, 0.0 ); |
846 | 0 | ::basegfx::B2DVector aNextTileY( 0.0, 1.0 ); |
847 | 0 | aNextTileX *= aPureTotalTransform; |
848 | 0 | aNextTileY *= aPureTotalTransform; |
849 | |
|
850 | 0 | ::basegfx::B2DHomMatrix aInverseTextureTransform( aPureTotalTransform ); |
851 | |
|
852 | 0 | ENSURE_ARG_OR_THROW( aInverseTextureTransform.isInvertible(), |
853 | 0 | "CanvasHelper::fillTexturedPolyPolygon(): singular texture matrix" ); |
854 | |
|
855 | 0 | aInverseTextureTransform.invert(); |
856 | | |
857 | | // calc bound rect of extended texture area in |
858 | | // device coordinates. Therefore, we first calc |
859 | | // the area of the polygon bound rect in texture |
860 | | // space. To maintain texture phase, this bound |
861 | | // rect is then extended to integer coordinates |
862 | | // (extended, because shrinking might leave some |
863 | | // inner polygon areas unfilled). |
864 | | // Finally, the bound rect is transformed back to |
865 | | // device coordinate space, were we determine the |
866 | | // start point from it. |
867 | 0 | ::basegfx::B2DRectangle aTextureSpacePolygonRect = ::canvastools::calcTransformedRectBounds( |
868 | 0 | vcl::unotools::b2DRectangleFromRectangle(aPolygonDeviceRect), |
869 | 0 | aInverseTextureTransform ); |
870 | | |
871 | | // calc left, top of extended polygon rect in |
872 | | // texture space, create one-texture instance rect |
873 | | // from it (i.e. rect from start point extending |
874 | | // 1.0 units to the right and 1.0 units to the |
875 | | // bottom). Note that the rounding employed here |
876 | | // is a bit subtle, since we need to round up/down |
877 | | // as _soon_ as any fractional amount is |
878 | | // encountered. This is to ensure that the full |
879 | | // polygon area is filled with texture tiles. |
880 | 0 | const sal_Int32 nX1( ::canvastools::roundDown( aTextureSpacePolygonRect.getMinX() ) ); |
881 | 0 | const sal_Int32 nY1( ::canvastools::roundDown( aTextureSpacePolygonRect.getMinY() ) ); |
882 | 0 | const sal_Int32 nX2( ::canvastools::roundUp( aTextureSpacePolygonRect.getMaxX() ) ); |
883 | 0 | const sal_Int32 nY2( ::canvastools::roundUp( aTextureSpacePolygonRect.getMaxY() ) ); |
884 | 0 | const ::basegfx::B2DRectangle aSingleTextureRect( |
885 | 0 | nX1, nY1, |
886 | 0 | nX1 + 1.0, |
887 | 0 | nY1 + 1.0 ); |
888 | | |
889 | | // and convert back to device space |
890 | 0 | ::basegfx::B2DRectangle aSingleDeviceTextureRect = ::canvastools::calcTransformedRectBounds( |
891 | 0 | aSingleTextureRect, |
892 | 0 | aPureTotalTransform ); |
893 | |
|
894 | 0 | const ::Point aPtRepeat( vcl::unotools::pointFromB2DPoint( |
895 | 0 | aSingleDeviceTextureRect.getMinimum() ) ); |
896 | 0 | const ::Size aSz( ::basegfx::fround<::tools::Long>( aScale.getX() * aBmpSize.Width ), |
897 | 0 | ::basegfx::fround<::tools::Long>( aScale.getY() * aBmpSize.Height ) ); |
898 | 0 | const ::Size aIntegerNextTileX( vcl::unotools::sizeFromB2DSize(aNextTileX) ); |
899 | 0 | const ::Size aIntegerNextTileY( vcl::unotools::sizeFromB2DSize(aNextTileY) ); |
900 | |
|
901 | 0 | const ::Point aPt( textures[0].RepeatModeX == rendering::TexturingMode::NONE ? |
902 | 0 | ::basegfx::fround<::tools::Long>( aOutputPos.getX() ) : aPtRepeat.X(), |
903 | 0 | textures[0].RepeatModeY == rendering::TexturingMode::NONE ? |
904 | 0 | ::basegfx::fround<::tools::Long>( aOutputPos.getY() ) : aPtRepeat.Y() ); |
905 | 0 | const sal_Int32 nTilesX( textures[0].RepeatModeX == rendering::TexturingMode::NONE ? |
906 | 0 | 1 : nX2 - nX1 ); |
907 | 0 | const sal_Int32 nTilesY( textures[0].RepeatModeX == rendering::TexturingMode::NONE ? |
908 | 0 | 1 : nY2 - nY1 ); |
909 | |
|
910 | 0 | OutputDevice& rOutDev( mpOutDevProvider->getOutDev() ); |
911 | |
|
912 | 0 | if( bRectangularPolygon ) |
913 | 0 | { |
914 | | // use optimized output path |
915 | | |
916 | | |
917 | | // this distinction really looks like a |
918 | | // micro-optimization, but in fact greatly speeds up |
919 | | // especially complex fills. That's because when using |
920 | | // clipping, we can output polygons instead of |
921 | | // poly-polygons, and don't have to output the gradient |
922 | | // twice for XOR |
923 | | |
924 | | // setup alpha modulation |
925 | 0 | if( !::rtl::math::approxEqual( textures[0].Alpha, |
926 | 0 | 1.0 ) ) |
927 | 0 | { |
928 | | // TODO(F1): Note that the GraphicManager has |
929 | | // a subtle difference in how it calculates |
930 | | // the resulting alpha value: it's using the |
931 | | // inverse alpha values (i.e. 'transparency'), |
932 | | // and calculates transOrig + transModulate, |
933 | | // instead of transOrig + transModulate - |
934 | | // transOrig*transModulate (which would be |
935 | | // equivalent to the origAlpha*modulateAlpha |
936 | | // the DX canvas performs) |
937 | 0 | aGrfAttr.SetAlpha( |
938 | 0 | static_cast< sal_uInt8 >( |
939 | 0 | ::basegfx::fround( 255.0 * textures[0].Alpha ) ) ); |
940 | 0 | } |
941 | |
|
942 | 0 | rOutDev.IntersectClipRegion( aPolygonDeviceRect ); |
943 | 0 | textureFill( rOutDev, |
944 | 0 | *pGrfObj, |
945 | 0 | aPt, |
946 | 0 | aIntegerNextTileX, |
947 | 0 | aIntegerNextTileY, |
948 | 0 | nTilesX, |
949 | 0 | nTilesY, |
950 | 0 | aSz, |
951 | 0 | aGrfAttr ); |
952 | |
|
953 | 0 | if( mp2ndOutDevProvider ) |
954 | 0 | { |
955 | 0 | OutputDevice& r2ndOutDev( mp2ndOutDevProvider->getOutDev() ); |
956 | 0 | r2ndOutDev.IntersectClipRegion( aPolygonDeviceRect ); |
957 | 0 | textureFill( r2ndOutDev, |
958 | 0 | *pGrfObj, |
959 | 0 | aPt, |
960 | 0 | aIntegerNextTileX, |
961 | 0 | aIntegerNextTileY, |
962 | 0 | nTilesX, |
963 | 0 | nTilesY, |
964 | 0 | aSz, |
965 | 0 | aGrfAttr ); |
966 | 0 | } |
967 | 0 | } |
968 | 0 | else |
969 | 0 | { |
970 | | // output texture the hard way: XORing out the |
971 | | // polygon |
972 | | // =========================================== |
973 | |
|
974 | 0 | if( !::rtl::math::approxEqual( textures[0].Alpha, |
975 | 0 | 1.0 ) ) |
976 | 0 | { |
977 | | // uh-oh. alpha blending is required, |
978 | | // cannot do direct XOR, but have to |
979 | | // prepare the filled polygon within a |
980 | | // VDev |
981 | 0 | ScopedVclPtrInstance< VirtualDevice > pVDev( rOutDev ); |
982 | 0 | pVDev->SetOutputSizePixel( aPolygonDeviceRect.GetSize() ); |
983 | | |
984 | | // shift output to origin of VDev |
985 | 0 | const ::Point aOutPos( aPt - aPolygonDeviceRect.TopLeft() ); |
986 | 0 | aPolyPoly.Translate( ::Point( -aPolygonDeviceRect.Left(), |
987 | 0 | -aPolygonDeviceRect.Top() ) ); |
988 | |
|
989 | 0 | const vcl::Region aPolyClipRegion( aPolyPoly ); |
990 | |
|
991 | 0 | pVDev->SetClipRegion( aPolyClipRegion ); |
992 | 0 | textureFill( *pVDev, |
993 | 0 | *pGrfObj, |
994 | 0 | aOutPos, |
995 | 0 | aIntegerNextTileX, |
996 | 0 | aIntegerNextTileY, |
997 | 0 | nTilesX, |
998 | 0 | nTilesY, |
999 | 0 | aSz, |
1000 | 0 | aGrfAttr ); |
1001 | | |
1002 | | // output VDev content alpha-blended to |
1003 | | // target position. |
1004 | 0 | const ::Point aEmptyPoint; |
1005 | 0 | Bitmap aContentBmp( |
1006 | 0 | pVDev->GetBitmap( aEmptyPoint, |
1007 | 0 | pVDev->GetOutputSizePixel() ) ); |
1008 | |
|
1009 | 0 | sal_uInt8 nCol( static_cast< sal_uInt8 >( |
1010 | 0 | ::basegfx::fround( 255.0*( 1.0 - textures[0].Alpha ) ) ) ); |
1011 | 0 | AlphaMask aAlpha( pVDev->GetOutputSizePixel(), |
1012 | 0 | &nCol ); |
1013 | |
|
1014 | 0 | Bitmap aOutputBmp( aContentBmp.CreateColorBitmap(), aAlpha ); |
1015 | 0 | rOutDev.DrawBitmap( aPolygonDeviceRect.TopLeft(), |
1016 | 0 | aOutputBmp ); |
1017 | |
|
1018 | 0 | if( mp2ndOutDevProvider ) |
1019 | 0 | mp2ndOutDevProvider->getOutDev().DrawBitmap( aPolygonDeviceRect.TopLeft(), |
1020 | 0 | aOutputBmp ); |
1021 | 0 | } |
1022 | 0 | else |
1023 | 0 | { |
1024 | 0 | const vcl::Region aPolyClipRegion( aPolyPoly ); |
1025 | |
|
1026 | 0 | rOutDev.Push( vcl::PushFlags::CLIPREGION ); |
1027 | 0 | rOutDev.IntersectClipRegion( aPolyClipRegion ); |
1028 | |
|
1029 | 0 | textureFill( rOutDev, |
1030 | 0 | *pGrfObj, |
1031 | 0 | aPt, |
1032 | 0 | aIntegerNextTileX, |
1033 | 0 | aIntegerNextTileY, |
1034 | 0 | nTilesX, |
1035 | 0 | nTilesY, |
1036 | 0 | aSz, |
1037 | 0 | aGrfAttr ); |
1038 | 0 | rOutDev.Pop(); |
1039 | |
|
1040 | 0 | if( mp2ndOutDevProvider ) |
1041 | 0 | { |
1042 | 0 | OutputDevice& r2ndOutDev( mp2ndOutDevProvider->getOutDev() ); |
1043 | 0 | auto popIt = r2ndOutDev.ScopedPush(vcl::PushFlags::CLIPREGION); |
1044 | |
|
1045 | 0 | r2ndOutDev.IntersectClipRegion( aPolyClipRegion ); |
1046 | 0 | textureFill( r2ndOutDev, |
1047 | 0 | *pGrfObj, |
1048 | 0 | aPt, |
1049 | 0 | aIntegerNextTileX, |
1050 | 0 | aIntegerNextTileY, |
1051 | 0 | nTilesX, |
1052 | 0 | nTilesY, |
1053 | 0 | aSz, |
1054 | 0 | aGrfAttr ); |
1055 | 0 | } |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | } |
1059 | 0 | } |
1060 | 0 | } |
1061 | | |
1062 | | // TODO(P1): Provide caching here. |
1063 | 0 | return uno::Reference< rendering::XCachedPrimitive >(nullptr); |
1064 | 0 | } |
1065 | | |
1066 | | } |
1067 | | |
1068 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |