/src/libreoffice/drawinglayer/source/primitive2d/softedgeprimitive2d.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 <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx> |
21 | | #include <drawinglayer/primitive2d/softedgeprimitive2d.hxx> |
22 | | #include <drawinglayer/primitive2d/transformprimitive2d.hxx> |
23 | | #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx> |
24 | | #include <basegfx/matrix/b2dhommatrixtools.hxx> |
25 | | #include <drawinglayer/converters.hxx> |
26 | | #include "GlowSoftEgdeShadowTools.hxx" |
27 | | |
28 | | #ifdef DBG_UTIL |
29 | | #include <o3tl/environment.hxx> |
30 | | #include <tools/stream.hxx> |
31 | | #include <vcl/filter/PngImageWriter.hxx> |
32 | | #endif |
33 | | |
34 | | namespace drawinglayer::primitive2d |
35 | | { |
36 | | SoftEdgePrimitive2D::SoftEdgePrimitive2D(double fRadius, Primitive2DContainer&& aChildren) |
37 | 0 | : BufferedDecompositionGroupPrimitive2D(std::move(aChildren)) |
38 | 0 | , mfRadius(fRadius) |
39 | 0 | , mfLastDiscreteSoftRadius(0.0) |
40 | 0 | , maLastClippedRange() |
41 | 0 | { |
42 | | // activate callback to flush buffered decomposition content |
43 | 0 | activateFlushOnTimer(); |
44 | 0 | } |
45 | | |
46 | | bool SoftEdgePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const |
47 | 0 | { |
48 | 0 | if (BufferedDecompositionGroupPrimitive2D::operator==(rPrimitive)) |
49 | 0 | { |
50 | 0 | auto& rCompare = static_cast<const SoftEdgePrimitive2D&>(rPrimitive); |
51 | 0 | return getRadius() == rCompare.getRadius(); |
52 | 0 | } |
53 | | |
54 | 0 | return false; |
55 | 0 | } |
56 | | |
57 | | bool SoftEdgePrimitive2D::prepareValuesAndcheckValidity( |
58 | | basegfx::B2DRange& rSoftRange, basegfx::B2DRange& rClippedRange, |
59 | | basegfx::B2DVector& rDiscreteSoftSize, double& rfDiscreteSoftRadius, |
60 | | const geometry::ViewInformation2D& rViewInformation) const |
61 | 0 | { |
62 | | // no SoftRadius defined, done |
63 | 0 | if (getRadius() <= 0.0) |
64 | 0 | return false; |
65 | | |
66 | | // no geometry, done |
67 | 0 | if (getChildren().empty()) |
68 | 0 | return false; |
69 | | |
70 | | // no pixel target, done |
71 | 0 | if (rViewInformation.getObjectToViewTransformation().isIdentity()) |
72 | 0 | return false; |
73 | | |
74 | | // get geometry range that defines area that needs to be pixelated |
75 | 0 | rSoftRange = getChildren().getB2DRange(rViewInformation); |
76 | | |
77 | | // no range of geometry, done |
78 | 0 | if (rSoftRange.isEmpty()) |
79 | 0 | return false; |
80 | | |
81 | | // initialize ClippedRange to full SoftRange -> all is visible |
82 | 0 | rClippedRange = rSoftRange; |
83 | | |
84 | | // get Viewport and check if used. If empty, all is visible (see |
85 | | // ViewInformation2D definition in viewinformation2d.hxx) |
86 | 0 | if (!rViewInformation.getViewport().isEmpty()) |
87 | 0 | { |
88 | | // if used, extend by SoftRadius to ensure needed parts are included |
89 | | // that are not visible, but influence the visible parts |
90 | 0 | basegfx::B2DRange aVisibleArea(rViewInformation.getViewport()); |
91 | 0 | aVisibleArea.grow(getRadius() * 2); |
92 | | |
93 | | // To do this correctly, it needs to be done in discrete coordinates. |
94 | | // The object may be transformed relative to the original# |
95 | | // ObjectTransformation, e.g. when re-used in shadow |
96 | 0 | aVisibleArea.transform(rViewInformation.getViewTransformation()); |
97 | 0 | rClippedRange.transform(rViewInformation.getObjectToViewTransformation()); |
98 | | |
99 | | // calculate ClippedRange |
100 | 0 | rClippedRange.intersect(aVisibleArea); |
101 | | |
102 | | // if SoftRange is completely outside of VisibleArea, ClippedRange |
103 | | // will be empty and we are done |
104 | 0 | if (rClippedRange.isEmpty()) |
105 | 0 | return false; |
106 | | |
107 | | // convert result back to object coordinates |
108 | 0 | rClippedRange.transform(rViewInformation.getInverseObjectToViewTransformation()); |
109 | 0 | } |
110 | | |
111 | | // calculate discrete pixel size of SoftRange. If it's too small to visualize, we are done |
112 | 0 | rDiscreteSoftSize = rViewInformation.getObjectToViewTransformation() * rSoftRange.getRange(); |
113 | 0 | if (ceil(rDiscreteSoftSize.getX()) < 2.0 || ceil(rDiscreteSoftSize.getY()) < 2.0) |
114 | 0 | return false; |
115 | | |
116 | | // calculate discrete pixel size of SoftRadius. If it's too small to visualize, we are done |
117 | 0 | rfDiscreteSoftRadius = ceil( |
118 | 0 | (rViewInformation.getObjectToViewTransformation() * basegfx::B2DVector(getRadius(), 0)) |
119 | 0 | .getLength()); |
120 | 0 | if (rfDiscreteSoftRadius < 1.0) |
121 | 0 | return false; |
122 | | |
123 | 0 | return true; |
124 | 0 | } |
125 | | |
126 | | void SoftEdgePrimitive2D::create2DDecomposition( |
127 | | Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const |
128 | 0 | { |
129 | | // Use endless while-loop-and-break mechanism due to having multiple |
130 | | // exit scenarios that all have to do the same thing when exiting |
131 | 0 | while (true) |
132 | 0 | { |
133 | 0 | basegfx::B2DRange aSoftRange; |
134 | 0 | basegfx::B2DRange aClippedRange; |
135 | 0 | basegfx::B2DVector aDiscreteSoftSize; |
136 | 0 | double fDiscreteSoftRadius(0.0); |
137 | | |
138 | | // Check various validity details and calculate/prepare values. If false, we are done |
139 | 0 | if (!prepareValuesAndcheckValidity(aSoftRange, aClippedRange, aDiscreteSoftSize, |
140 | 0 | fDiscreteSoftRadius, rViewInformation)) |
141 | 0 | break; |
142 | | |
143 | | // Create embedding transformation from object to top-left zero-aligned |
144 | | // target pixel geometry (discrete form of ClippedRange) |
145 | | // First, move to top-left of SoftRange |
146 | 0 | const sal_uInt32 nDiscreteSoftWidth(ceil(aDiscreteSoftSize.getX())); |
147 | 0 | const sal_uInt32 nDiscreteSoftHeight(ceil(aDiscreteSoftSize.getY())); |
148 | 0 | basegfx::B2DHomMatrix aEmbedding(basegfx::utils::createTranslateB2DHomMatrix( |
149 | 0 | -aClippedRange.getMinX(), -aClippedRange.getMinY())); |
150 | | // Second, scale to discrete bitmap size |
151 | | // Even when using the offset from ClippedRange, we need to use the |
152 | | // scaling from the full representation, thus from SoftRange |
153 | 0 | aEmbedding.scale(nDiscreteSoftWidth / aSoftRange.getWidth(), |
154 | 0 | nDiscreteSoftHeight / aSoftRange.getHeight()); |
155 | | |
156 | | // Embed content graphics to TransformPrimitive2D |
157 | 0 | const primitive2d::Primitive2DReference xEmbedRef( |
158 | 0 | new primitive2d::TransformPrimitive2D(aEmbedding, Primitive2DContainer(getChildren()))); |
159 | 0 | primitive2d::Primitive2DContainer xEmbedSeq{ xEmbedRef }; |
160 | | |
161 | | // Create Bitmap using drawinglayer tooling, including a MaximumQuadraticPixel |
162 | | // limitation to be safe and not go runtime/memory havoc. Use a pretty small |
163 | | // limit due to this is softEdge functionality and will look good with bitmap scaling |
164 | | // anyways. The value of 250.000 square pixels below maybe adapted as needed. |
165 | 0 | const basegfx::B2DVector aDiscreteClippedSize( |
166 | 0 | rViewInformation.getObjectToViewTransformation() * aClippedRange.getRange()); |
167 | 0 | const sal_uInt32 nDiscreteClippedWidth(ceil(aDiscreteClippedSize.getX())); |
168 | 0 | const sal_uInt32 nDiscreteClippedHeight(ceil(aDiscreteClippedSize.getY())); |
169 | 0 | const geometry::ViewInformation2D aViewInformation2D; |
170 | 0 | const sal_uInt32 nMaximumQuadraticPixels(250000); |
171 | | // tdf#156808 force an alpha mask to be created even if it has no alpha |
172 | | // We need an alpha mask, even if it is totally opaque, so that |
173 | | // drawinglayer::primitive2d::ProcessAndBlurAlphaMask() can be called. |
174 | | // Otherwise, blurring of edges will fail in cases like running in a |
175 | | // slideshow or exporting to PDF. |
176 | 0 | const Bitmap aBitmap(::drawinglayer::convertToBitmap( |
177 | 0 | std::move(xEmbedSeq), aViewInformation2D, nDiscreteClippedWidth, nDiscreteClippedHeight, |
178 | 0 | nMaximumQuadraticPixels, true)); |
179 | |
|
180 | 0 | if (aBitmap.IsEmpty()) |
181 | 0 | break; |
182 | | |
183 | | // Get Bitmap and check size. If no content, we are done |
184 | 0 | const Size aBitmapSizePixel(aBitmap.GetSizePixel()); |
185 | 0 | if (!(aBitmapSizePixel.Width() > 0 && aBitmapSizePixel.Height() > 0)) |
186 | 0 | break; |
187 | | |
188 | | // We may have to take a corrective scaling into account when the |
189 | | // MaximumQuadraticPixel limit was used/triggered |
190 | 0 | double fScale(1.0); |
191 | |
|
192 | 0 | if (static_cast<sal_uInt32>(aBitmapSizePixel.Width()) != nDiscreteClippedWidth |
193 | 0 | || static_cast<sal_uInt32>(aBitmapSizePixel.Height()) != nDiscreteClippedHeight) |
194 | 0 | { |
195 | | // scale in X and Y should be the same (see fReduceFactor in convertToBitmapEx), |
196 | | // so adapt numerically to a single scale value, they are integer rounded values |
197 | 0 | const double fScaleX(static_cast<double>(aBitmapSizePixel.Width()) |
198 | 0 | / static_cast<double>(nDiscreteClippedWidth)); |
199 | 0 | const double fScaleY(static_cast<double>(aBitmapSizePixel.Height()) |
200 | 0 | / static_cast<double>(nDiscreteClippedHeight)); |
201 | |
|
202 | 0 | fScale = (fScaleX + fScaleY) * 0.5; |
203 | 0 | } |
204 | | |
205 | | // Get the Alpha and use as base to blur and apply the effect |
206 | 0 | AlphaMask aMask(aBitmap.CreateAlphaMask()); |
207 | 0 | if (aMask.IsEmpty()) // There is no mask, fully opaque |
208 | 0 | break; |
209 | 0 | AlphaMask blurMask(drawinglayer::primitive2d::ProcessAndBlurAlphaMask( |
210 | 0 | aMask, -fDiscreteSoftRadius * fScale, fDiscreteSoftRadius * fScale, 0)); |
211 | 0 | aMask.BlendWith(blurMask); |
212 | | |
213 | | // The end result is the original bitmap with blurred 8-bit alpha mask |
214 | 0 | Bitmap result(aBitmap.CreateColorBitmap(), aMask); |
215 | |
|
216 | | #ifdef DBG_UTIL |
217 | | static bool bDoSaveForVisualControl(false); // loplugin:constvars:ignore |
218 | | if (bDoSaveForVisualControl) |
219 | | { |
220 | | // VCL_DUMP_BMP_PATH should be like C:/path/ or ~/path/ |
221 | | static const OUString sDumpPath(o3tl::getEnvironment(u"VCL_DUMP_BMP_PATH"_ustr)); |
222 | | if (!sDumpPath.isEmpty()) |
223 | | { |
224 | | SvFileStream aNew(sDumpPath + "test_softedge.png", |
225 | | StreamMode::WRITE | StreamMode::TRUNC); |
226 | | vcl::PngImageWriter aPNGWriter(aNew); |
227 | | aPNGWriter.write(result); |
228 | | } |
229 | | } |
230 | | #endif |
231 | | |
232 | | // Independent from discrete sizes of soft alpha creation, always |
233 | | // map and project soft result to geometry range extended by soft |
234 | | // radius, but to the eventually clipped instance (ClippedRange) |
235 | 0 | const primitive2d::Primitive2DReference xEmbedRefBitmap( |
236 | 0 | new BitmapPrimitive2D(result, basegfx::utils::createScaleTranslateB2DHomMatrix( |
237 | 0 | aClippedRange.getWidth(), aClippedRange.getHeight(), |
238 | 0 | aClippedRange.getMinX(), aClippedRange.getMinY()))); |
239 | |
|
240 | 0 | rContainer = primitive2d::Primitive2DContainer{ xEmbedRefBitmap }; |
241 | | |
242 | | // we made it, return |
243 | 0 | return; |
244 | 0 | } |
245 | | |
246 | | // creation failed for some of many possible reasons, use original |
247 | | // content, so the unmodified original geometry will be the result, |
248 | | // just without any softEdge effect |
249 | 0 | rContainer = getChildren(); |
250 | 0 | } |
251 | | |
252 | | void SoftEdgePrimitive2D::get2DDecomposition( |
253 | | Primitive2DDecompositionVisitor& rVisitor, |
254 | | const geometry::ViewInformation2D& rViewInformation) const |
255 | 0 | { |
256 | | // Use endless while-loop-and-break mechanism due to having multiple |
257 | | // exit scenarios that all have to do the same thing when exiting |
258 | 0 | while (true) |
259 | 0 | { |
260 | 0 | basegfx::B2DRange aSoftRange; |
261 | 0 | basegfx::B2DRange aClippedRange; |
262 | 0 | basegfx::B2DVector aDiscreteSoftSize; |
263 | 0 | double fDiscreteSoftRadius(0.0); |
264 | | |
265 | | // Check various validity details and calculate/prepare values. If false, we are done |
266 | 0 | if (!prepareValuesAndcheckValidity(aSoftRange, aClippedRange, aDiscreteSoftSize, |
267 | 0 | fDiscreteSoftRadius, rViewInformation)) |
268 | 0 | break; |
269 | | |
270 | 0 | if (hasBuffered2DDecomposition()) |
271 | 0 | { |
272 | | // First check is to detect if the last created decompose is capable |
273 | | // to represent the now requested visualization (see similar |
274 | | // implementation at GlowPrimitive2D). |
275 | 0 | if (!maLastClippedRange.isEmpty() && !maLastClippedRange.isInside(aClippedRange)) |
276 | 0 | { |
277 | 0 | basegfx::B2DRange aLastClippedRangeAndHairline(maLastClippedRange); |
278 | |
|
279 | 0 | if (!rViewInformation.getObjectToViewTransformation().isIdentity()) |
280 | 0 | { |
281 | | // Grow by view-dependent size of 1/2 pixel |
282 | 0 | const double fHalfPixel((rViewInformation.getInverseObjectToViewTransformation() |
283 | 0 | * basegfx::B2DVector(0.5, 0)) |
284 | 0 | .getLength()); |
285 | 0 | aLastClippedRangeAndHairline.grow(fHalfPixel); |
286 | 0 | } |
287 | |
|
288 | 0 | if (!aLastClippedRangeAndHairline.isInside(aClippedRange)) |
289 | 0 | { |
290 | | // Conditions of last local decomposition have changed, delete |
291 | 0 | const_cast<SoftEdgePrimitive2D*>(this)->setBuffered2DDecomposition( |
292 | 0 | Primitive2DContainer()); |
293 | 0 | } |
294 | 0 | } |
295 | 0 | } |
296 | |
|
297 | 0 | if (hasBuffered2DDecomposition()) |
298 | 0 | { |
299 | | // Second check is to react on changes of the DiscreteSoftRadius when |
300 | | // zooming in/out (see similar implementation at GlowPrimitive2D). |
301 | 0 | bool bFree(mfLastDiscreteSoftRadius <= 0.0 || fDiscreteSoftRadius <= 0.0); |
302 | |
|
303 | 0 | if (!bFree) |
304 | 0 | { |
305 | 0 | const double fDiff(fabs(mfLastDiscreteSoftRadius - fDiscreteSoftRadius)); |
306 | 0 | const double fLen(fabs(mfLastDiscreteSoftRadius) + fabs(fDiscreteSoftRadius)); |
307 | 0 | const double fRelativeChange(fDiff / fLen); |
308 | | |
309 | | // Use a lower value here, soft edge keeps it's content so avoid that it gets too |
310 | | // unsharp in the pixel visualization |
311 | | // Value is in the range of ]0.0 .. 1.0] |
312 | 0 | bFree = fRelativeChange >= 0.075; |
313 | 0 | } |
314 | |
|
315 | 0 | if (bFree) |
316 | 0 | { |
317 | | // Conditions of last local decomposition have changed, delete |
318 | 0 | const_cast<SoftEdgePrimitive2D*>(this)->setBuffered2DDecomposition( |
319 | 0 | Primitive2DContainer()); |
320 | 0 | } |
321 | 0 | } |
322 | |
|
323 | 0 | if (!hasBuffered2DDecomposition()) |
324 | 0 | { |
325 | | // refresh last used DiscreteSoftRadius and ClippedRange to new remembered values |
326 | 0 | const_cast<SoftEdgePrimitive2D*>(this)->mfLastDiscreteSoftRadius = fDiscreteSoftRadius; |
327 | 0 | const_cast<SoftEdgePrimitive2D*>(this)->maLastClippedRange = aClippedRange; |
328 | 0 | } |
329 | | |
330 | | // call parent, that will check for empty, call create2DDecomposition and |
331 | | // set as decomposition |
332 | 0 | BufferedDecompositionGroupPrimitive2D::get2DDecomposition(rVisitor, rViewInformation); |
333 | | |
334 | | // we made it, return |
335 | 0 | return; |
336 | 0 | } |
337 | | |
338 | | // No soft edge needed for some of many possible reasons, use original content |
339 | 0 | rVisitor.visit(getChildren()); |
340 | 0 | } |
341 | | |
342 | | basegfx::B2DRange |
343 | | SoftEdgePrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const |
344 | 0 | { |
345 | | // Hint: Do *not* use GroupPrimitive2D::getB2DRange, that will (unnecessarily) |
346 | | // use the decompose - what works, but is not needed here. |
347 | | // We know the to-be-visualized geometry and the radius it needs to be extended, |
348 | | // so simply calculate the exact needed range. |
349 | 0 | return getChildren().getB2DRange(rViewInformation); |
350 | 0 | } |
351 | | |
352 | | sal_uInt32 SoftEdgePrimitive2D::getPrimitive2DID() const |
353 | 0 | { |
354 | 0 | return PRIMITIVE2D_ID_SOFTEDGEPRIMITIVE2D; |
355 | 0 | } |
356 | | |
357 | | } // end of namespace |
358 | | |
359 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |