calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / drawinglayer / source / primitive2d / svggradientprimitive2d.cxx
blob25ee2fd543a07bfe96f3f58d2a00a5db409bb861
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/svggradientprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
22 #include <drawinglayer/primitive2d/PolyPolygonColorPrimitive2D.hxx>
23 #include <drawinglayer/primitive2d/unifiedtransparenceprimitive2d.hxx>
24 #include <basegfx/matrix/b2dhommatrixtools.hxx>
25 #include <basegfx/polygon/b2dpolygontools.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
28 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
29 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
30 #include <drawinglayer/geometry/viewinformation2d.hxx>
31 #include <osl/diagnose.h>
32 #include <sal/log.hxx>
33 #include <cmath>
34 #include <utility>
35 #include <vcl/skia/SkiaHelper.hxx>
37 using namespace com::sun::star;
40 namespace
42 sal_uInt32 calculateStepsForSvgGradient(const basegfx::BColor& rColorA, const basegfx::BColor& rColorB, double fDelta, double fDiscreteUnit)
44 // use color distance, assume to do every color step (full quality)
45 sal_uInt32 nSteps(basegfx::fround(rColorA.getDistance(rColorB) * 255.0));
47 if(nSteps)
49 // calc discrete length to change color all 1.5 discrete units (pixels)
50 const sal_uInt32 nDistSteps(basegfx::fround(fDelta / (fDiscreteUnit * 1.5)));
52 nSteps = std::min(nSteps, nDistSteps);
55 // roughly cut when too big or too small
56 nSteps = std::min(nSteps, sal_uInt32(255));
57 nSteps = std::max(nSteps, sal_uInt32(1));
59 return nSteps;
61 } // end of anonymous namespace
64 namespace drawinglayer::primitive2d
66 void SvgGradientHelper::createSingleGradientEntryFill(Primitive2DContainer& rContainer) const
68 const SvgGradientEntryVector& rEntries = getGradientEntries();
69 const sal_uInt32 nCount(rEntries.size());
71 if(nCount)
73 const SvgGradientEntry& rSingleEntry = rEntries[nCount - 1];
74 const double fOpacity(rSingleEntry.getOpacity());
76 if(fOpacity > 0.0)
78 Primitive2DReference xRef(
79 new PolyPolygonColorPrimitive2D(
80 getPolyPolygon(),
81 rSingleEntry.getColor()));
83 if(fOpacity < 1.0)
85 Primitive2DContainer aContent { xRef };
87 xRef = Primitive2DReference(
88 new UnifiedTransparencePrimitive2D(
89 std::move(aContent),
90 1.0 - fOpacity));
93 rContainer.push_back(xRef);
96 else
98 OSL_ENSURE(false, "Single gradient entry construction without entry (!)");
102 void SvgGradientHelper::checkPreconditions()
104 mbPreconditionsChecked = true;
105 const SvgGradientEntryVector& rEntries = getGradientEntries();
107 if(rEntries.empty())
109 // no fill at all, done
110 return;
113 // sort maGradientEntries by offset, small to big
114 std::sort(maGradientEntries.begin(), maGradientEntries.end());
116 // gradient with at least two colors
117 bool bAllInvisible(true);
118 bool bInvalidEntries(false);
120 for(const SvgGradientEntry& rCandidate : rEntries)
122 if(basegfx::fTools::equalZero(rCandidate.getOpacity()))
124 // invisible
125 mbFullyOpaque = false;
127 else if(basegfx::fTools::equal(rCandidate.getOpacity(), 1.0))
129 // completely opaque
130 bAllInvisible = false;
132 else
134 // opacity
135 bAllInvisible = false;
136 mbFullyOpaque = false;
139 if(!basegfx::fTools::betweenOrEqualEither(rCandidate.getOffset(), 0.0, 1.0))
141 bInvalidEntries = true;
145 if(bAllInvisible)
147 // all invisible, nothing to do
148 return;
151 if(bInvalidEntries)
153 // invalid entries, do nothing
154 SAL_WARN("drawinglayer", "SvgGradientHelper got invalid SvgGradientEntries outside [0.0 .. 1.0]");
155 return;
158 const basegfx::B2DRange aPolyRange(getPolyPolygon().getB2DRange());
160 if(aPolyRange.isEmpty())
162 // no range to fill, nothing to do
163 return;
166 const double fPolyWidth(aPolyRange.getWidth());
167 const double fPolyHeight(aPolyRange.getHeight());
169 if(basegfx::fTools::equalZero(fPolyWidth) || basegfx::fTools::equalZero(fPolyHeight))
171 // no width/height to fill, nothing to do
172 return;
175 mbCreatesContent = true;
177 if(1 == rEntries.size())
179 // fill with single existing color
180 setSingleEntry();
184 const SvgGradientEntry& SvgGradientHelper::FindEntryLessOrEqual(
185 sal_Int32& rInt,
186 const double fFrac) const
188 const bool bMirror(SpreadMethod::Reflect == getSpreadMethod() && 0 != rInt % 2);
189 const SvgGradientEntryVector& rCurrent(bMirror ? getMirroredGradientEntries() : getGradientEntries());
191 for(SvgGradientEntryVector::const_reverse_iterator aIter(rCurrent.rbegin()); aIter != rCurrent.rend(); ++aIter)
193 if(basegfx::fTools::lessOrEqual(aIter->getOffset(), fFrac))
195 return *aIter;
199 // walk over gap to the left, be prepared for missing 0.0/1.0 entries
200 rInt--;
201 const bool bMirror2(SpreadMethod::Reflect == getSpreadMethod() && 0 != rInt % 2);
202 const SvgGradientEntryVector& rCurrent2(bMirror2 ? getMirroredGradientEntries() : getGradientEntries());
203 return rCurrent2.back();
206 const SvgGradientEntry& SvgGradientHelper::FindEntryMore(
207 sal_Int32& rInt,
208 const double fFrac) const
210 const bool bMirror(SpreadMethod::Reflect == getSpreadMethod() && 0 != rInt % 2);
211 const SvgGradientEntryVector& rCurrent(bMirror ? getMirroredGradientEntries() : getGradientEntries());
213 for(SvgGradientEntryVector::const_iterator aIter(rCurrent.begin()); aIter != rCurrent.end(); ++aIter)
215 if(basegfx::fTools::more(aIter->getOffset(), fFrac))
217 return *aIter;
221 // walk over gap to the right, be prepared for missing 0.0/1.0 entries
222 rInt++;
223 const bool bMirror2(SpreadMethod::Reflect == getSpreadMethod() && 0 != rInt % 2);
224 const SvgGradientEntryVector& rCurrent2(bMirror2 ? getMirroredGradientEntries() : getGradientEntries());
225 return rCurrent2.front();
228 // tdf#124424 Adapted creation of color runs to do in a single effort. Previous
229 // version tried to do this from [0.0 .. 1.0] and to re-use transformed versions
230 // in the caller if SpreadMethod was on some repeat mode, but had problems when
231 // e.g. like in the bugdoc from the task a negative-only fStart/fEnd run was
232 // requested in which case it did nothing. Even when reusing the spread might
233 // not have been a full one from [0.0 .. 1.0].
234 // This gets complicated due to mirrored runs, but also for gradient definitions
235 // with missing entries for 0.0 and 1.0 in which case these have to be guessed
236 // to be there with same parametrisation as their nearest existing entries. These
237 // *could* have been added at checkPreconditions() but would then create unnecessary
238 // spreads on zone overlaps.
239 void SvgGradientHelper::createRun(
240 Primitive2DContainer& rTargetColor,
241 Primitive2DContainer& rTargetOpacity,
242 double fStart,
243 double fEnd) const
245 double fInt(0.0);
246 double fFrac(0.0);
247 double fEnd2(0.0);
249 if(SpreadMethod::Pad == getSpreadMethod())
251 if(fStart < 0.0)
253 fFrac = std::modf(fStart, &fInt);
254 const SvgGradientEntry& rFront(getGradientEntries().front());
255 const SvgGradientEntry aTemp(1.0 + fFrac, rFront.getColor(), rFront.getOpacity());
256 createAtom(rTargetColor, rTargetOpacity, aTemp, rFront, static_cast<sal_Int32>(fInt - 1), 0);
257 fStart = rFront.getOffset();
260 if(fEnd > 1.0)
262 // change fEnd early, but create geometry later (after range below)
263 fEnd2 = fEnd;
264 fEnd = getGradientEntries().back().getOffset();
268 while(fStart < fEnd)
270 fFrac = std::modf(fStart, &fInt);
272 if(fFrac < 0.0)
274 fInt -= 1;
275 fFrac = 1.0 + fFrac;
278 sal_Int32 nIntLeft(static_cast<sal_Int32>(fInt));
279 sal_Int32 nIntRight(nIntLeft);
281 const SvgGradientEntry& rLeft(FindEntryLessOrEqual(nIntLeft, fFrac));
282 const SvgGradientEntry& rRight(FindEntryMore(nIntRight, fFrac));
283 createAtom(rTargetColor, rTargetOpacity, rLeft, rRight, nIntLeft, nIntRight);
285 const double fNextfStart(static_cast<double>(nIntRight) + rRight.getOffset());
287 if(basegfx::fTools::more(fNextfStart, fStart))
289 fStart = fNextfStart;
291 else
293 SAL_WARN("drawinglayer", "SvgGradientHelper spread error");
294 fStart += 1.0;
298 if(fEnd2 > 1.0)
300 // create end run for SpreadMethod::Pad late to keep correct creation order
301 fFrac = std::modf(fEnd2, &fInt);
302 const SvgGradientEntry& rBack(getGradientEntries().back());
303 const SvgGradientEntry aTemp(fFrac, rBack.getColor(), rBack.getOpacity());
304 createAtom(rTargetColor, rTargetOpacity, rBack, aTemp, 0, static_cast<sal_Int32>(fInt));
308 void SvgGradientHelper::createResult(
309 Primitive2DContainer& rContainer,
310 Primitive2DContainer aTargetColor,
311 Primitive2DContainer aTargetOpacity,
312 const basegfx::B2DHomMatrix& rUnitGradientToObject,
313 bool bInvert) const
315 Primitive2DContainer aTargetColorEntries(aTargetColor.maybeInvert(bInvert));
316 Primitive2DContainer aTargetOpacityEntries(aTargetOpacity.maybeInvert(bInvert));
318 if(aTargetColorEntries.empty())
319 return;
321 Primitive2DReference xRefContent;
323 if(!aTargetOpacityEntries.empty())
325 const Primitive2DReference xRefOpacity = new TransparencePrimitive2D(
326 std::move(aTargetColorEntries),
327 std::move(aTargetOpacityEntries));
329 xRefContent = new TransformPrimitive2D(
330 rUnitGradientToObject,
331 Primitive2DContainer { xRefOpacity });
333 else
335 xRefContent = new TransformPrimitive2D(
336 rUnitGradientToObject,
337 std::move(aTargetColorEntries));
340 rContainer.push_back(new MaskPrimitive2D(
341 getPolyPolygon(),
342 Primitive2DContainer { xRefContent }));
345 SvgGradientHelper::SvgGradientHelper(
346 basegfx::B2DHomMatrix aGradientTransform,
347 basegfx::B2DPolyPolygon aPolyPolygon,
348 SvgGradientEntryVector&& rGradientEntries,
349 const basegfx::B2DPoint& rStart,
350 bool bUseUnitCoordinates,
351 SpreadMethod aSpreadMethod)
352 : maGradientTransform(std::move(aGradientTransform)),
353 maPolyPolygon(std::move(aPolyPolygon)),
354 maGradientEntries(std::move(rGradientEntries)),
355 maStart(rStart),
356 maSpreadMethod(aSpreadMethod),
357 mbPreconditionsChecked(false),
358 mbCreatesContent(false),
359 mbSingleEntry(false),
360 mbFullyOpaque(true),
361 mbUseUnitCoordinates(bUseUnitCoordinates)
365 SvgGradientHelper::~SvgGradientHelper()
369 const SvgGradientEntryVector& SvgGradientHelper::getMirroredGradientEntries() const
371 if(maMirroredGradientEntries.empty() && !getGradientEntries().empty())
373 const_cast< SvgGradientHelper* >(this)->createMirroredGradientEntries();
376 return maMirroredGradientEntries;
379 void SvgGradientHelper::createMirroredGradientEntries()
381 if(!maMirroredGradientEntries.empty() || getGradientEntries().empty())
382 return;
384 const sal_uInt32 nCount(getGradientEntries().size());
385 maMirroredGradientEntries.clear();
386 maMirroredGradientEntries.reserve(nCount);
388 for(sal_uInt32 a(0); a < nCount; a++)
390 const SvgGradientEntry& rCandidate = getGradientEntries()[nCount - 1 - a];
392 maMirroredGradientEntries.emplace_back(
393 1.0 - rCandidate.getOffset(),
394 rCandidate.getColor(),
395 rCandidate.getOpacity());
399 bool SvgGradientHelper::operator==(const SvgGradientHelper& rSvgGradientHelper) const
401 const SvgGradientHelper& rCompare = rSvgGradientHelper;
403 return (getGradientTransform() == rCompare.getGradientTransform()
404 && getPolyPolygon() == rCompare.getPolyPolygon()
405 && getGradientEntries() == rCompare.getGradientEntries()
406 && getStart() == rCompare.getStart()
407 && getUseUnitCoordinates() == rCompare.getUseUnitCoordinates()
408 && getSpreadMethod() == rCompare.getSpreadMethod());
411 } // end of namespace drawinglayer::primitive2d
414 namespace drawinglayer::primitive2d
416 void SvgLinearGradientPrimitive2D::checkPreconditions()
418 // call parent
419 SvgGradientHelper::checkPreconditions();
421 if(getCreatesContent())
423 // Check Vector
424 const basegfx::B2DVector aVector(getEnd() - getStart());
426 if(basegfx::fTools::equalZero(aVector.getX()) && basegfx::fTools::equalZero(aVector.getY()))
428 // fill with single color using last stop color
429 setSingleEntry();
434 void SvgLinearGradientPrimitive2D::createAtom(
435 Primitive2DContainer& rTargetColor,
436 Primitive2DContainer& rTargetOpacity,
437 const SvgGradientEntry& rFrom,
438 const SvgGradientEntry& rTo,
439 sal_Int32 nOffsetFrom,
440 sal_Int32 nOffsetTo) const
442 // create gradient atom [rFrom.getOffset() .. rTo.getOffset()] with (rFrom.getOffset() > rTo.getOffset())
443 if(rFrom.getOffset() == rTo.getOffset())
445 OSL_ENSURE(false, "SvgGradient Atom creation with no step width (!)");
447 else
449 rTargetColor.push_back(
450 new SvgLinearAtomPrimitive2D(
451 rFrom.getColor(), rFrom.getOffset() + nOffsetFrom,
452 rTo.getColor(), rTo.getOffset() + nOffsetTo));
454 if(!getFullyOpaque())
456 const double fTransFrom(1.0 - rFrom.getOpacity());
457 const double fTransTo(1.0 - rTo.getOpacity());
458 const basegfx::BColor aColorFrom(fTransFrom, fTransFrom, fTransFrom);
459 const basegfx::BColor aColorTo(fTransTo, fTransTo, fTransTo);
461 rTargetOpacity.push_back(
462 new SvgLinearAtomPrimitive2D(
463 aColorFrom, rFrom.getOffset() + nOffsetFrom,
464 aColorTo, rTo.getOffset() + nOffsetTo));
469 void SvgLinearGradientPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
471 if(!getPreconditionsChecked())
473 const_cast< SvgLinearGradientPrimitive2D* >(this)->checkPreconditions();
476 if(getSingleEntry())
478 // fill with last existing color
479 createSingleGradientEntryFill(rContainer);
481 else if(getCreatesContent())
483 // at least two color stops in range [0.0 .. 1.0], sorted, non-null vector, not completely
484 // invisible, width and height to fill are not empty
485 const basegfx::B2DRange aPolyRange(getPolyPolygon().getB2DRange());
486 const double fPolyWidth(aPolyRange.getWidth());
487 const double fPolyHeight(aPolyRange.getHeight());
489 // create ObjectTransform based on polygon range
490 const basegfx::B2DHomMatrix aObjectTransform(
491 basegfx::utils::createScaleTranslateB2DHomMatrix(
492 fPolyWidth, fPolyHeight,
493 aPolyRange.getMinX(), aPolyRange.getMinY()));
494 basegfx::B2DHomMatrix aUnitGradientToObject;
496 if(getUseUnitCoordinates())
498 // interpret in unit coordinate system -> object aspect ratio will scale result
499 // create unit transform from unit vector [0.0 .. 1.0] along the X-Axis to given
500 // gradient vector defined by Start,End
501 const basegfx::B2DVector aVector(getEnd() - getStart());
502 const double fVectorLength(aVector.getLength());
504 aUnitGradientToObject.scale(fVectorLength, 1.0);
505 aUnitGradientToObject.rotate(atan2(aVector.getY(), aVector.getX()));
506 aUnitGradientToObject.translate(getStart().getX(), getStart().getY());
508 aUnitGradientToObject *= getGradientTransform();
510 // create full transform from unit gradient coordinates to object coordinates
511 // including the SvgGradient transformation
512 aUnitGradientToObject *= aObjectTransform;
514 else
516 // interpret in object coordinate system -> object aspect ratio will not scale result
517 const basegfx::B2DPoint aStart(aObjectTransform * getStart());
518 const basegfx::B2DPoint aEnd(aObjectTransform * getEnd());
519 const basegfx::B2DVector aVector(aEnd - aStart);
521 aUnitGradientToObject.scale(aVector.getLength(), 1.0);
522 aUnitGradientToObject.rotate(atan2(aVector.getY(), aVector.getX()));
523 aUnitGradientToObject.translate(aStart.getX(), aStart.getY());
525 aUnitGradientToObject *= getGradientTransform();
528 // create inverse from it
529 basegfx::B2DHomMatrix aObjectToUnitGradient(aUnitGradientToObject);
530 aObjectToUnitGradient.invert();
532 // back-transform polygon to unit gradient coordinates and get
533 // UnitRage. This is the range the gradient has to cover
534 basegfx::B2DPolyPolygon aUnitPoly(getPolyPolygon());
535 aUnitPoly.transform(aObjectToUnitGradient);
536 const basegfx::B2DRange aUnitRange(aUnitPoly.getB2DRange());
538 // prepare result vectors
539 Primitive2DContainer aTargetColor;
540 Primitive2DContainer aTargetOpacity;
542 if(basegfx::fTools::more(aUnitRange.getWidth(), 0.0))
544 // add a pre-multiply to aUnitGradientToObject to allow
545 // multiplication of the polygon(xl, 0.0, xr, 1.0)
546 const basegfx::B2DHomMatrix aPreMultiply(
547 basegfx::utils::createScaleTranslateB2DHomMatrix(
548 1.0, aUnitRange.getHeight(), 0.0, aUnitRange.getMinY()));
549 aUnitGradientToObject = aUnitGradientToObject * aPreMultiply;
551 // create full color run, including all SpreadMethod variants
552 createRun(
553 aTargetColor,
554 aTargetOpacity,
555 aUnitRange.getMinX(),
556 aUnitRange.getMaxX());
559 createResult(rContainer, std::move(aTargetColor), std::move(aTargetOpacity), aUnitGradientToObject);
563 SvgLinearGradientPrimitive2D::SvgLinearGradientPrimitive2D(
564 const basegfx::B2DHomMatrix& rGradientTransform,
565 const basegfx::B2DPolyPolygon& rPolyPolygon,
566 SvgGradientEntryVector&& rGradientEntries,
567 const basegfx::B2DPoint& rStart,
568 const basegfx::B2DPoint& rEnd,
569 bool bUseUnitCoordinates,
570 SpreadMethod aSpreadMethod)
571 : SvgGradientHelper(rGradientTransform, rPolyPolygon, std::move(rGradientEntries), rStart, bUseUnitCoordinates, aSpreadMethod),
572 maEnd(rEnd)
576 SvgLinearGradientPrimitive2D::~SvgLinearGradientPrimitive2D()
580 bool SvgLinearGradientPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
582 const SvgGradientHelper* pSvgGradientHelper = dynamic_cast< const SvgGradientHelper* >(&rPrimitive);
584 if(pSvgGradientHelper && SvgGradientHelper::operator==(*pSvgGradientHelper))
586 const SvgLinearGradientPrimitive2D& rCompare = static_cast< const SvgLinearGradientPrimitive2D& >(rPrimitive);
588 return (getEnd() == rCompare.getEnd());
591 return false;
594 basegfx::B2DRange SvgLinearGradientPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
596 // return ObjectRange
597 return getPolyPolygon().getB2DRange();
600 // provide unique ID
601 sal_uInt32 SvgLinearGradientPrimitive2D::getPrimitive2DID() const
603 return PRIMITIVE2D_ID_SVGLINEARGRADIENTPRIMITIVE2D;
606 } // end of namespace drawinglayer::primitive2d
609 namespace drawinglayer::primitive2d
611 void SvgRadialGradientPrimitive2D::checkPreconditions()
613 // call parent
614 SvgGradientHelper::checkPreconditions();
616 if(getCreatesContent())
618 // Check Radius
619 if(basegfx::fTools::equalZero(getRadius()))
621 // fill with single color using last stop color
622 setSingleEntry();
627 void SvgRadialGradientPrimitive2D::createAtom(
628 Primitive2DContainer& rTargetColor,
629 Primitive2DContainer& rTargetOpacity,
630 const SvgGradientEntry& rFrom,
631 const SvgGradientEntry& rTo,
632 sal_Int32 nOffsetFrom,
633 sal_Int32 nOffsetTo) const
635 // create gradient atom [rFrom.getOffset() .. rTo.getOffset()] with (rFrom.getOffset() > rTo.getOffset())
636 if(rFrom.getOffset() == rTo.getOffset())
638 OSL_ENSURE(false, "SvgGradient Atom creation with no step width (!)");
640 else
642 const double fScaleFrom(rFrom.getOffset() + nOffsetFrom);
643 const double fScaleTo(rTo.getOffset() + nOffsetTo);
645 if(isFocalSet())
647 const basegfx::B2DVector aTranslateFrom(maFocalVector * (maFocalLength - fScaleFrom));
648 const basegfx::B2DVector aTranslateTo(maFocalVector * (maFocalLength - fScaleTo));
650 rTargetColor.push_back(
651 new SvgRadialAtomPrimitive2D(
652 rFrom.getColor(), fScaleFrom, aTranslateFrom,
653 rTo.getColor(), fScaleTo, aTranslateTo));
655 else
657 rTargetColor.push_back(
658 new SvgRadialAtomPrimitive2D(
659 rFrom.getColor(), fScaleFrom,
660 rTo.getColor(), fScaleTo));
663 if(!getFullyOpaque())
665 const double fTransFrom(1.0 - rFrom.getOpacity());
666 const double fTransTo(1.0 - rTo.getOpacity());
667 const basegfx::BColor aColorFrom(fTransFrom, fTransFrom, fTransFrom);
668 const basegfx::BColor aColorTo(fTransTo, fTransTo, fTransTo);
670 if(isFocalSet())
672 const basegfx::B2DVector aTranslateFrom(maFocalVector * (maFocalLength - fScaleFrom));
673 const basegfx::B2DVector aTranslateTo(maFocalVector * (maFocalLength - fScaleTo));
675 rTargetOpacity.push_back(
676 new SvgRadialAtomPrimitive2D(
677 aColorFrom, fScaleFrom, aTranslateFrom,
678 aColorTo, fScaleTo, aTranslateTo));
680 else
682 rTargetOpacity.push_back(
683 new SvgRadialAtomPrimitive2D(
684 aColorFrom, fScaleFrom,
685 aColorTo, fScaleTo));
691 void SvgRadialGradientPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
693 if(!getPreconditionsChecked())
695 const_cast< SvgRadialGradientPrimitive2D* >(this)->checkPreconditions();
698 if(getSingleEntry())
700 // fill with last existing color
701 createSingleGradientEntryFill(rContainer);
703 else if(getCreatesContent())
705 // at least two color stops in range [0.0 .. 1.0], sorted, non-null vector, not completely
706 // invisible, width and height to fill are not empty
707 const basegfx::B2DRange aPolyRange(getPolyPolygon().getB2DRange());
708 const double fPolyWidth(aPolyRange.getWidth());
709 const double fPolyHeight(aPolyRange.getHeight());
711 // create ObjectTransform based on polygon range
712 const basegfx::B2DHomMatrix aObjectTransform(
713 basegfx::utils::createScaleTranslateB2DHomMatrix(
714 fPolyWidth, fPolyHeight,
715 aPolyRange.getMinX(), aPolyRange.getMinY()));
716 basegfx::B2DHomMatrix aUnitGradientToObject;
718 if(getUseUnitCoordinates())
720 // interpret in unit coordinate system -> object aspect ratio will scale result
721 // create unit transform from unit vector to given linear gradient vector
722 aUnitGradientToObject.scale(getRadius(), getRadius());
723 aUnitGradientToObject.translate(getStart().getX(), getStart().getY());
725 if(!getGradientTransform().isIdentity())
727 aUnitGradientToObject = getGradientTransform() * aUnitGradientToObject;
730 // create full transform from unit gradient coordinates to object coordinates
731 // including the SvgGradient transformation
732 aUnitGradientToObject = aObjectTransform * aUnitGradientToObject;
734 else
736 // interpret in object coordinate system -> object aspect ratio will not scale result
737 // use X-Axis with radius, it was already made relative to object width when coming from
738 // SVG import
739 const double fRadius((aObjectTransform * basegfx::B2DVector(getRadius(), 0.0)).getLength());
740 const basegfx::B2DPoint aStart(aObjectTransform * getStart());
742 aUnitGradientToObject.scale(fRadius, fRadius);
743 aUnitGradientToObject.translate(aStart.getX(), aStart.getY());
745 aUnitGradientToObject *= getGradientTransform();
748 // create inverse from it
749 basegfx::B2DHomMatrix aObjectToUnitGradient(aUnitGradientToObject);
750 aObjectToUnitGradient.invert();
752 // back-transform polygon to unit gradient coordinates and get
753 // UnitRage. This is the range the gradient has to cover
754 basegfx::B2DPolyPolygon aUnitPoly(getPolyPolygon());
755 aUnitPoly.transform(aObjectToUnitGradient);
756 const basegfx::B2DRange aUnitRange(aUnitPoly.getB2DRange());
758 // create range which the gradient has to cover to cover the whole given geometry.
759 // For circle, go from 0.0 to max radius in all directions (the corners)
760 double fMax(basegfx::B2DVector(aUnitRange.getMinimum()).getLength());
761 fMax = std::max(fMax, basegfx::B2DVector(aUnitRange.getMaximum()).getLength());
762 fMax = std::max(fMax, basegfx::B2DVector(aUnitRange.getMinX(), aUnitRange.getMaxY()).getLength());
763 fMax = std::max(fMax, basegfx::B2DVector(aUnitRange.getMaxX(), aUnitRange.getMinY()).getLength());
765 // prepare result vectors
766 Primitive2DContainer aTargetColor;
767 Primitive2DContainer aTargetOpacity;
769 if(0.0 < fMax)
771 // prepare maFocalVector
772 if(isFocalSet())
774 const_cast< SvgRadialGradientPrimitive2D* >(this)->maFocalLength = fMax;
777 // create full color run, including all SpreadMethod variants
778 createRun(
779 aTargetColor,
780 aTargetOpacity,
781 0.0,
782 fMax);
785 createResult(rContainer, std::move(aTargetColor), std::move(aTargetOpacity), aUnitGradientToObject, true);
789 SvgRadialGradientPrimitive2D::SvgRadialGradientPrimitive2D(
790 const basegfx::B2DHomMatrix& rGradientTransform,
791 const basegfx::B2DPolyPolygon& rPolyPolygon,
792 SvgGradientEntryVector&& rGradientEntries,
793 const basegfx::B2DPoint& rStart,
794 double fRadius,
795 bool bUseUnitCoordinates,
796 SpreadMethod aSpreadMethod,
797 const basegfx::B2DPoint* pFocal)
798 : SvgGradientHelper(rGradientTransform, rPolyPolygon, std::move(rGradientEntries), rStart, bUseUnitCoordinates, aSpreadMethod),
799 mfRadius(fRadius),
800 maFocal(rStart),
801 maFocalVector(0.0, 0.0),
802 maFocalLength(0.0),
803 mbFocalSet(false)
805 if(pFocal && !pFocal->equal(getStart()))
807 maFocal = *pFocal;
808 maFocalVector = maFocal - getStart();
809 mbFocalSet = true;
813 SvgRadialGradientPrimitive2D::~SvgRadialGradientPrimitive2D()
817 bool SvgRadialGradientPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
819 const SvgGradientHelper* pSvgGradientHelper = dynamic_cast< const SvgGradientHelper* >(&rPrimitive);
821 if(!pSvgGradientHelper || !SvgGradientHelper::operator==(*pSvgGradientHelper))
822 return false;
824 const SvgRadialGradientPrimitive2D& rCompare = static_cast< const SvgRadialGradientPrimitive2D& >(rPrimitive);
826 if(getRadius() == rCompare.getRadius())
828 if(isFocalSet() == rCompare.isFocalSet())
830 if(isFocalSet())
832 return getFocal() == rCompare.getFocal();
834 else
836 return true;
841 return false;
844 basegfx::B2DRange SvgRadialGradientPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
846 // return ObjectRange
847 return getPolyPolygon().getB2DRange();
850 // provide unique ID
851 sal_uInt32 SvgRadialGradientPrimitive2D::getPrimitive2DID() const
853 return PRIMITIVE2D_ID_SVGRADIALGRADIENTPRIMITIVE2D;
856 } // end of namespace drawinglayer::primitive2d
859 // SvgLinearAtomPrimitive2D class
861 namespace drawinglayer::primitive2d
863 void SvgLinearAtomPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
865 const double fDelta(getOffsetB() - getOffsetA());
867 if(basegfx::fTools::equalZero(fDelta))
868 return;
870 // use one discrete unit for overlap (one pixel)
871 const double fDiscreteUnit(getDiscreteUnit());
873 // use color distance and discrete lengths to calculate step count
874 const sal_uInt32 nSteps(calculateStepsForSvgGradient(getColorA(), getColorB(), fDelta, fDiscreteUnit));
876 // HACK: Splitting a gradient into adjacent polygons with gradually changing color is silly.
877 // If antialiasing is used to draw them, the AA-ed adjacent edges won't line up perfectly
878 // because of the AA (see SkiaSalGraphicsImpl::mergePolyPolygonToPrevious()).
879 // Make the polygons a bit wider, so they the partial overlap "fixes" this.
880 const double fixup = SkiaHelper::isVCLSkiaEnabled() ? fDiscreteUnit / 2 : 0;
882 // tdf#117949 Use a small amount of discrete overlap at the edges. Usually this
883 // should be exactly 0.0 and 1.0, but there were cases when this gets clipped
884 // against the mask polygon which got numerically problematic.
885 // This change is unnecessary in that respect, but avoids that numerical havoc
886 // by at the same time doing no real harm AFAIK
887 // TTTT: Remove again when clipping is fixed (!)
889 // prepare polygon in needed width at start position (with discrete overlap)
890 const basegfx::B2DPolygon aPolygon(
891 basegfx::utils::createPolygonFromRect(
892 basegfx::B2DRange(
893 getOffsetA() - fDiscreteUnit,
894 -0.0001, // TTTT -> should be 0.0, see comment above
895 getOffsetA() + (fDelta / nSteps) + fDiscreteUnit + fixup,
896 1.0001))); // TTTT -> should be 1.0, see comment above
898 // prepare loop (inside to outside, [0.0 .. 1.0[)
899 double fUnitScale(0.0);
900 const double fUnitStep(1.0 / nSteps);
902 for(sal_uInt32 a(0); a < nSteps; a++, fUnitScale += fUnitStep)
904 basegfx::B2DPolygon aNew(aPolygon);
906 aNew.transform(basegfx::utils::createTranslateB2DHomMatrix(fDelta * fUnitScale, 0.0));
907 rContainer.push_back(new PolyPolygonColorPrimitive2D(
908 basegfx::B2DPolyPolygon(aNew),
909 basegfx::interpolate(getColorA(), getColorB(), fUnitScale)));
913 SvgLinearAtomPrimitive2D::SvgLinearAtomPrimitive2D(
914 const basegfx::BColor& aColorA, double fOffsetA,
915 const basegfx::BColor& aColorB, double fOffsetB)
916 : maColorA(aColorA),
917 maColorB(aColorB),
918 mfOffsetA(fOffsetA),
919 mfOffsetB(fOffsetB)
921 if(mfOffsetA > mfOffsetB)
923 OSL_ENSURE(false, "Wrong offset order (!)");
924 std::swap(mfOffsetA, mfOffsetB);
928 bool SvgLinearAtomPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
930 if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
932 const SvgLinearAtomPrimitive2D& rCompare = static_cast< const SvgLinearAtomPrimitive2D& >(rPrimitive);
934 return (getColorA() == rCompare.getColorA()
935 && getColorB() == rCompare.getColorB()
936 && getOffsetA() == rCompare.getOffsetA()
937 && getOffsetB() == rCompare.getOffsetB());
940 return false;
943 // provide unique ID
944 sal_uInt32 SvgLinearAtomPrimitive2D::getPrimitive2DID() const
946 return PRIMITIVE2D_ID_SVGLINEARATOMPRIMITIVE2D;
949 } // end of namespace drawinglayer::primitive2d
952 // SvgRadialAtomPrimitive2D class
954 namespace drawinglayer::primitive2d
956 void SvgRadialAtomPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
958 const double fDeltaScale(getScaleB() - getScaleA());
960 if(basegfx::fTools::equalZero(fDeltaScale))
961 return;
963 // use one discrete unit for overlap (one pixel)
964 const double fDiscreteUnit(getDiscreteUnit());
966 // use color distance and discrete lengths to calculate step count
967 const sal_uInt32 nSteps(calculateStepsForSvgGradient(getColorA(), getColorB(), fDeltaScale, fDiscreteUnit));
969 // prepare loop ([0.0 .. 1.0[, full polygons, no polypolygons with holes)
970 double fUnitScale(0.0);
971 const double fUnitStep(1.0 / nSteps);
973 for(sal_uInt32 a(0); a < nSteps; a++, fUnitScale += fUnitStep)
975 basegfx::B2DHomMatrix aTransform;
976 const double fEndScale(getScaleB() - (fDeltaScale * fUnitScale));
978 if(isTranslateSet())
980 const basegfx::B2DVector aTranslate(
981 basegfx::interpolate(
982 getTranslateB(),
983 getTranslateA(),
984 fUnitScale));
986 aTransform = basegfx::utils::createScaleTranslateB2DHomMatrix(
987 fEndScale,
988 fEndScale,
989 aTranslate.getX(),
990 aTranslate.getY());
992 else
994 aTransform = basegfx::utils::createScaleB2DHomMatrix(
995 fEndScale,
996 fEndScale);
999 basegfx::B2DPolygon aNew(basegfx::utils::createPolygonFromUnitCircle());
1001 aNew.transform(aTransform);
1002 rContainer.push_back(new PolyPolygonColorPrimitive2D(
1003 basegfx::B2DPolyPolygon(aNew),
1004 basegfx::interpolate(getColorB(), getColorA(), fUnitScale)));
1008 SvgRadialAtomPrimitive2D::SvgRadialAtomPrimitive2D(
1009 const basegfx::BColor& aColorA, double fScaleA, const basegfx::B2DVector& rTranslateA,
1010 const basegfx::BColor& aColorB, double fScaleB, const basegfx::B2DVector& rTranslateB)
1011 : maColorA(aColorA),
1012 maColorB(aColorB),
1013 mfScaleA(fScaleA),
1014 mfScaleB(fScaleB)
1016 // check and evtl. set translations
1017 if(!rTranslateA.equal(rTranslateB))
1019 mpTranslate.reset( new VectorPair(rTranslateA, rTranslateB) );
1022 // scale A and B have to be positive
1023 mfScaleA = std::max(mfScaleA, 0.0);
1024 mfScaleB = std::max(mfScaleB, 0.0);
1026 // scale B has to be bigger than scale A; swap if different
1027 if(mfScaleA > mfScaleB)
1029 OSL_ENSURE(false, "Wrong offset order (!)");
1030 std::swap(mfScaleA, mfScaleB);
1032 if(mpTranslate)
1034 std::swap(mpTranslate->maTranslateA, mpTranslate->maTranslateB);
1039 SvgRadialAtomPrimitive2D::SvgRadialAtomPrimitive2D(
1040 const basegfx::BColor& aColorA, double fScaleA,
1041 const basegfx::BColor& aColorB, double fScaleB)
1042 : maColorA(aColorA),
1043 maColorB(aColorB),
1044 mfScaleA(fScaleA),
1045 mfScaleB(fScaleB)
1047 // scale A and B have to be positive
1048 mfScaleA = std::max(mfScaleA, 0.0);
1049 mfScaleB = std::max(mfScaleB, 0.0);
1051 // scale B has to be bigger than scale A; swap if different
1052 if(mfScaleA > mfScaleB)
1054 OSL_ENSURE(false, "Wrong offset order (!)");
1055 std::swap(mfScaleA, mfScaleB);
1059 SvgRadialAtomPrimitive2D::~SvgRadialAtomPrimitive2D()
1063 bool SvgRadialAtomPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
1065 if(!DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
1066 return false;
1068 const SvgRadialAtomPrimitive2D& rCompare = static_cast< const SvgRadialAtomPrimitive2D& >(rPrimitive);
1070 if(getColorA() == rCompare.getColorA()
1071 && getColorB() == rCompare.getColorB()
1072 && getScaleA() == rCompare.getScaleA()
1073 && getScaleB() == rCompare.getScaleB())
1075 if(isTranslateSet() && rCompare.isTranslateSet())
1077 return (getTranslateA() == rCompare.getTranslateA()
1078 && getTranslateB() == rCompare.getTranslateB());
1080 else if(!isTranslateSet() && !rCompare.isTranslateSet())
1082 return true;
1086 return false;
1089 // provide unique ID
1090 sal_uInt32 SvgRadialAtomPrimitive2D::getPrimitive2DID() const
1092 return PRIMITIVE2D_ID_SVGRADIALATOMPRIMITIVE2D;
1095 } // end of namespace
1097 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */