LibreOffice Module drawinglayer (master) 1
svggradientprimitive2d.cxx
Go to the documentation of this file.
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
31#include <osl/diagnose.h>
32#include <sal/log.hxx>
33#include <cmath>
34#include <utility>
36
37using namespace com::sun::star;
38
39
40namespace
41{
42 sal_uInt32 calculateStepsForSvgGradient(const basegfx::BColor& rColorA, const basegfx::BColor& rColorB, double fDelta, double fDiscreteUnit)
43 {
44 // use color distance, assume to do every color step (full quality)
45 sal_uInt32 nSteps(basegfx::fround(rColorA.getDistance(rColorB) * 255.0));
46
47 if(nSteps)
48 {
49 // calc discrete length to change color all 1.5 discrete units (pixels)
50 const sal_uInt32 nDistSteps(basegfx::fround(fDelta / (fDiscreteUnit * 1.5)));
51
52 nSteps = std::min(nSteps, nDistSteps);
53 }
54
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));
58
59 return nSteps;
60 }
61} // end of anonymous namespace
62
63
65{
66 void SvgGradientHelper::createSingleGradientEntryFill(Primitive2DContainer& rContainer) const
67 {
68 const SvgGradientEntryVector& rEntries = getGradientEntries();
69 const sal_uInt32 nCount(rEntries.size());
70
71 if(nCount)
72 {
73 const SvgGradientEntry& rSingleEntry = rEntries[nCount - 1];
74 const double fOpacity(rSingleEntry.getOpacity());
75
76 if(fOpacity > 0.0)
77 {
79 new PolyPolygonColorPrimitive2D(
80 getPolyPolygon(),
81 rSingleEntry.getColor()));
82
83 if(fOpacity < 1.0)
84 {
85 Primitive2DContainer aContent { xRef };
86
88 new UnifiedTransparencePrimitive2D(
89 std::move(aContent),
90 1.0 - fOpacity));
91 }
92
93 rContainer.push_back(xRef);
94 }
95 }
96 else
97 {
98 OSL_ENSURE(false, "Single gradient entry construction without entry (!)");
99 }
100 }
101
102 void SvgGradientHelper::checkPreconditions()
103 {
104 mbPreconditionsChecked = true;
105 const SvgGradientEntryVector& rEntries = getGradientEntries();
106
107 if(rEntries.empty())
108 {
109 // no fill at all, done
110 return;
111 }
112
113 // sort maGradientEntries by offset, small to big
114 std::sort(maGradientEntries.begin(), maGradientEntries.end());
115
116 // gradient with at least two colors
117 bool bAllInvisible(true);
118 bool bInvalidEntries(false);
119
120 for(const SvgGradientEntry& rCandidate : rEntries)
121 {
122 if(basegfx::fTools::equalZero(rCandidate.getOpacity()))
123 {
124 // invisible
125 mbFullyOpaque = false;
126 }
127 else if(basegfx::fTools::equal(rCandidate.getOpacity(), 1.0))
128 {
129 // completely opaque
130 bAllInvisible = false;
131 }
132 else
133 {
134 // opacity
135 bAllInvisible = false;
136 mbFullyOpaque = false;
137 }
138
139 if(!basegfx::fTools::betweenOrEqualEither(rCandidate.getOffset(), 0.0, 1.0))
140 {
141 bInvalidEntries = true;
142 }
143 }
144
145 if(bAllInvisible)
146 {
147 // all invisible, nothing to do
148 return;
149 }
150
151 if(bInvalidEntries)
152 {
153 // invalid entries, do nothing
154 SAL_WARN("drawinglayer", "SvgGradientHelper got invalid SvgGradientEntries outside [0.0 .. 1.0]");
155 return;
156 }
157
158 const basegfx::B2DRange aPolyRange(getPolyPolygon().getB2DRange());
159
160 if(aPolyRange.isEmpty())
161 {
162 // no range to fill, nothing to do
163 return;
164 }
165
166 const double fPolyWidth(aPolyRange.getWidth());
167 const double fPolyHeight(aPolyRange.getHeight());
168
169 if(basegfx::fTools::equalZero(fPolyWidth) || basegfx::fTools::equalZero(fPolyHeight))
170 {
171 // no width/height to fill, nothing to do
172 return;
173 }
174
175 mbCreatesContent = true;
176
177 if(1 == rEntries.size())
178 {
179 // fill with single existing color
180 setSingleEntry();
181 }
182 }
183
184 const SvgGradientEntry& SvgGradientHelper::FindEntryLessOrEqual(
185 sal_Int32& rInt,
186 const double fFrac) const
187 {
188 const bool bMirror(SpreadMethod::Reflect == getSpreadMethod() && 0 != rInt % 2);
189 const SvgGradientEntryVector& rCurrent(bMirror ? getMirroredGradientEntries() : getGradientEntries());
190
191 for(SvgGradientEntryVector::const_reverse_iterator aIter(rCurrent.rbegin()); aIter != rCurrent.rend(); ++aIter)
192 {
193 if(basegfx::fTools::lessOrEqual(aIter->getOffset(), fFrac))
194 {
195 return *aIter;
196 }
197 }
198
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();
204 }
205
206 const SvgGradientEntry& SvgGradientHelper::FindEntryMore(
207 sal_Int32& rInt,
208 const double fFrac) const
209 {
210 const bool bMirror(SpreadMethod::Reflect == getSpreadMethod() && 0 != rInt % 2);
211 const SvgGradientEntryVector& rCurrent(bMirror ? getMirroredGradientEntries() : getGradientEntries());
212
213 for(SvgGradientEntryVector::const_iterator aIter(rCurrent.begin()); aIter != rCurrent.end(); ++aIter)
214 {
215 if(basegfx::fTools::more(aIter->getOffset(), fFrac))
216 {
217 return *aIter;
218 }
219 }
220
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();
226 }
227
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
244 {
245 double fInt(0.0);
246 double fFrac(0.0);
247 double fEnd2(0.0);
248
249 if(SpreadMethod::Pad == getSpreadMethod())
250 {
251 if(fStart < 0.0)
252 {
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();
258 }
259
260 if(fEnd > 1.0)
261 {
262 // change fEnd early, but create geometry later (after range below)
263 fEnd2 = fEnd;
264 fEnd = getGradientEntries().back().getOffset();
265 }
266 }
267
268 while(fStart < fEnd)
269 {
270 fFrac = std::modf(fStart, &fInt);
271
272 if(fFrac < 0.0)
273 {
274 fInt -= 1;
275 fFrac = 1.0 + fFrac;
276 }
277
278 sal_Int32 nIntLeft(static_cast<sal_Int32>(fInt));
279 sal_Int32 nIntRight(nIntLeft);
280
281 const SvgGradientEntry& rLeft(FindEntryLessOrEqual(nIntLeft, fFrac));
282 const SvgGradientEntry& rRight(FindEntryMore(nIntRight, fFrac));
283 createAtom(rTargetColor, rTargetOpacity, rLeft, rRight, nIntLeft, nIntRight);
284
285 const double fNextfStart(static_cast<double>(nIntRight) + rRight.getOffset());
286
287 if(basegfx::fTools::more(fNextfStart, fStart))
288 {
289 fStart = fNextfStart;
290 }
291 else
292 {
293 SAL_WARN("drawinglayer", "SvgGradientHelper spread error");
294 fStart += 1.0;
295 }
296 }
297
298 if(fEnd2 > 1.0)
299 {
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));
305 }
306 }
307
308 void SvgGradientHelper::createResult(
309 Primitive2DContainer& rContainer,
310 Primitive2DContainer aTargetColor,
311 Primitive2DContainer aTargetOpacity,
312 const basegfx::B2DHomMatrix& rUnitGradientToObject,
313 bool bInvert) const
314 {
315 Primitive2DContainer aTargetColorEntries(aTargetColor.maybeInvert(bInvert));
316 Primitive2DContainer aTargetOpacityEntries(aTargetOpacity.maybeInvert(bInvert));
317
318 if(aTargetColorEntries.empty())
319 return;
320
321 Primitive2DReference xRefContent;
322
323 if(!aTargetOpacityEntries.empty())
324 {
325 const Primitive2DReference xRefOpacity = new TransparencePrimitive2D(
326 std::move(aTargetColorEntries),
327 std::move(aTargetOpacityEntries));
328
329 xRefContent = new TransformPrimitive2D(
330 rUnitGradientToObject,
331 Primitive2DContainer { xRefOpacity });
332 }
333 else
334 {
335 xRefContent = new TransformPrimitive2D(
336 rUnitGradientToObject,
337 std::move(aTargetColorEntries));
338 }
339
340 rContainer.push_back(new MaskPrimitive2D(
341 getPolyPolygon(),
342 Primitive2DContainer { xRefContent }));
343 }
344
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)
362 {
363 }
364
365 SvgGradientHelper::~SvgGradientHelper()
366 {
367 }
368
369 const SvgGradientEntryVector& SvgGradientHelper::getMirroredGradientEntries() const
370 {
371 if(maMirroredGradientEntries.empty() && !getGradientEntries().empty())
372 {
373 const_cast< SvgGradientHelper* >(this)->createMirroredGradientEntries();
374 }
375
376 return maMirroredGradientEntries;
377 }
378
379 void SvgGradientHelper::createMirroredGradientEntries()
380 {
381 if(!maMirroredGradientEntries.empty() || getGradientEntries().empty())
382 return;
383
384 const sal_uInt32 nCount(getGradientEntries().size());
385 maMirroredGradientEntries.clear();
386 maMirroredGradientEntries.reserve(nCount);
387
388 for(sal_uInt32 a(0); a < nCount; a++)
389 {
390 const SvgGradientEntry& rCandidate = getGradientEntries()[nCount - 1 - a];
391
392 maMirroredGradientEntries.emplace_back(
393 1.0 - rCandidate.getOffset(),
394 rCandidate.getColor(),
395 rCandidate.getOpacity());
396 }
397 }
398
399 bool SvgGradientHelper::operator==(const SvgGradientHelper& rSvgGradientHelper) const
400 {
401 const SvgGradientHelper& rCompare = rSvgGradientHelper;
402
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());
409 }
410
411} // end of namespace drawinglayer::primitive2d
412
413
415{
417 {
418 // call parent
419 SvgGradientHelper::checkPreconditions();
420
421 if(getCreatesContent())
422 {
423 // Check Vector
424 const basegfx::B2DVector aVector(getEnd() - getStart());
425
427 {
428 // fill with single color using last stop color
429 setSingleEntry();
430 }
431 }
432 }
433
435 Primitive2DContainer& rTargetColor,
436 Primitive2DContainer& rTargetOpacity,
437 const SvgGradientEntry& rFrom,
438 const SvgGradientEntry& rTo,
439 sal_Int32 nOffsetFrom,
440 sal_Int32 nOffsetTo) const
441 {
442 // create gradient atom [rFrom.getOffset() .. rTo.getOffset()] with (rFrom.getOffset() > rTo.getOffset())
443 if(rFrom.getOffset() == rTo.getOffset())
444 {
445 OSL_ENSURE(false, "SvgGradient Atom creation with no step width (!)");
446 }
447 else
448 {
449 rTargetColor.push_back(
451 rFrom.getColor(), rFrom.getOffset() + nOffsetFrom,
452 rTo.getColor(), rTo.getOffset() + nOffsetTo));
453
454 if(!getFullyOpaque())
455 {
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);
460
461 rTargetOpacity.push_back(
463 aColorFrom, rFrom.getOffset() + nOffsetFrom,
464 aColorTo, rTo.getOffset() + nOffsetTo));
465 }
466 }
467 }
468
470 {
471 if(!getPreconditionsChecked())
472 {
473 const_cast< SvgLinearGradientPrimitive2D* >(this)->checkPreconditions();
474 }
475
476 if(getSingleEntry())
477 {
478 // fill with last existing color
479 createSingleGradientEntryFill(rContainer);
480 }
481 else if(getCreatesContent())
482 {
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());
488
489 // create ObjectTransform based on polygon range
490 const basegfx::B2DHomMatrix aObjectTransform(
492 fPolyWidth, fPolyHeight,
493 aPolyRange.getMinX(), aPolyRange.getMinY()));
494 basegfx::B2DHomMatrix aUnitGradientToObject;
495
496 if(getUseUnitCoordinates())
497 {
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());
503
504 aUnitGradientToObject.scale(fVectorLength, 1.0);
505 aUnitGradientToObject.rotate(atan2(aVector.getY(), aVector.getX()));
506 aUnitGradientToObject.translate(getStart().getX(), getStart().getY());
507
508 aUnitGradientToObject *= getGradientTransform();
509
510 // create full transform from unit gradient coordinates to object coordinates
511 // including the SvgGradient transformation
512 aUnitGradientToObject *= aObjectTransform;
513 }
514 else
515 {
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);
520
521 aUnitGradientToObject.scale(aVector.getLength(), 1.0);
522 aUnitGradientToObject.rotate(atan2(aVector.getY(), aVector.getX()));
523 aUnitGradientToObject.translate(aStart.getX(), aStart.getY());
524
525 aUnitGradientToObject *= getGradientTransform();
526 }
527
528 // create inverse from it
529 basegfx::B2DHomMatrix aObjectToUnitGradient(aUnitGradientToObject);
530 aObjectToUnitGradient.invert();
531
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());
537
538 // prepare result vectors
539 Primitive2DContainer aTargetColor;
540 Primitive2DContainer aTargetOpacity;
541
542 if(basegfx::fTools::more(aUnitRange.getWidth(), 0.0))
543 {
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(
548 1.0, aUnitRange.getHeight(), 0.0, aUnitRange.getMinY()));
549 aUnitGradientToObject = aUnitGradientToObject * aPreMultiply;
550
551 // create full color run, including all SpreadMethod variants
552 createRun(
553 aTargetColor,
554 aTargetOpacity,
555 aUnitRange.getMinX(),
556 aUnitRange.getMaxX());
557 }
558
559 createResult(rContainer, std::move(aTargetColor), std::move(aTargetOpacity), aUnitGradientToObject);
560 }
561 }
562
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)
573 {
574 }
575
577 {
578 }
579
581 {
582 const SvgGradientHelper* pSvgGradientHelper = dynamic_cast< const SvgGradientHelper* >(&rPrimitive);
583
584 if(pSvgGradientHelper && SvgGradientHelper::operator==(*pSvgGradientHelper))
585 {
586 const SvgLinearGradientPrimitive2D& rCompare = static_cast< const SvgLinearGradientPrimitive2D& >(rPrimitive);
587
588 return (getEnd() == rCompare.getEnd());
589 }
590
591 return false;
592 }
593
595 {
596 // return ObjectRange
597 return getPolyPolygon().getB2DRange();
598 }
599
600 // provide unique ID
602 {
604 }
605
606} // end of namespace drawinglayer::primitive2d
607
608
610{
612 {
613 // call parent
614 SvgGradientHelper::checkPreconditions();
615
616 if(getCreatesContent())
617 {
618 // Check Radius
620 {
621 // fill with single color using last stop color
622 setSingleEntry();
623 }
624 }
625 }
626
628 Primitive2DContainer& rTargetColor,
629 Primitive2DContainer& rTargetOpacity,
630 const SvgGradientEntry& rFrom,
631 const SvgGradientEntry& rTo,
632 sal_Int32 nOffsetFrom,
633 sal_Int32 nOffsetTo) const
634 {
635 // create gradient atom [rFrom.getOffset() .. rTo.getOffset()] with (rFrom.getOffset() > rTo.getOffset())
636 if(rFrom.getOffset() == rTo.getOffset())
637 {
638 OSL_ENSURE(false, "SvgGradient Atom creation with no step width (!)");
639 }
640 else
641 {
642 const double fScaleFrom(rFrom.getOffset() + nOffsetFrom);
643 const double fScaleTo(rTo.getOffset() + nOffsetTo);
644
645 if(isFocalSet())
646 {
647 const basegfx::B2DVector aTranslateFrom(maFocalVector * (maFocalLength - fScaleFrom));
648 const basegfx::B2DVector aTranslateTo(maFocalVector * (maFocalLength - fScaleTo));
649
650 rTargetColor.push_back(
652 rFrom.getColor(), fScaleFrom, aTranslateFrom,
653 rTo.getColor(), fScaleTo, aTranslateTo));
654 }
655 else
656 {
657 rTargetColor.push_back(
659 rFrom.getColor(), fScaleFrom,
660 rTo.getColor(), fScaleTo));
661 }
662
663 if(!getFullyOpaque())
664 {
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);
669
670 if(isFocalSet())
671 {
672 const basegfx::B2DVector aTranslateFrom(maFocalVector * (maFocalLength - fScaleFrom));
673 const basegfx::B2DVector aTranslateTo(maFocalVector * (maFocalLength - fScaleTo));
674
675 rTargetOpacity.push_back(
677 aColorFrom, fScaleFrom, aTranslateFrom,
678 aColorTo, fScaleTo, aTranslateTo));
679 }
680 else
681 {
682 rTargetOpacity.push_back(
684 aColorFrom, fScaleFrom,
685 aColorTo, fScaleTo));
686 }
687 }
688 }
689 }
690
692 {
693 if(!getPreconditionsChecked())
694 {
695 const_cast< SvgRadialGradientPrimitive2D* >(this)->checkPreconditions();
696 }
697
698 if(getSingleEntry())
699 {
700 // fill with last existing color
701 createSingleGradientEntryFill(rContainer);
702 }
703 else if(getCreatesContent())
704 {
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());
710
711 // create ObjectTransform based on polygon range
712 const basegfx::B2DHomMatrix aObjectTransform(
714 fPolyWidth, fPolyHeight,
715 aPolyRange.getMinX(), aPolyRange.getMinY()));
716 basegfx::B2DHomMatrix aUnitGradientToObject;
717
718 if(getUseUnitCoordinates())
719 {
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());
724
725 if(!getGradientTransform().isIdentity())
726 {
727 aUnitGradientToObject = getGradientTransform() * aUnitGradientToObject;
728 }
729
730 // create full transform from unit gradient coordinates to object coordinates
731 // including the SvgGradient transformation
732 aUnitGradientToObject = aObjectTransform * aUnitGradientToObject;
733 }
734 else
735 {
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());
741
742 aUnitGradientToObject.scale(fRadius, fRadius);
743 aUnitGradientToObject.translate(aStart.getX(), aStart.getY());
744
745 aUnitGradientToObject *= getGradientTransform();
746 }
747
748 // create inverse from it
749 basegfx::B2DHomMatrix aObjectToUnitGradient(aUnitGradientToObject);
750 aObjectToUnitGradient.invert();
751
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());
757
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());
764
765 // prepare result vectors
766 Primitive2DContainer aTargetColor;
767 Primitive2DContainer aTargetOpacity;
768
769 if(0.0 < fMax)
770 {
771 // prepare maFocalVector
772 if(isFocalSet())
773 {
774 const_cast< SvgRadialGradientPrimitive2D* >(this)->maFocalLength = fMax;
775 }
776
777 // create full color run, including all SpreadMethod variants
778 createRun(
779 aTargetColor,
780 aTargetOpacity,
781 0.0,
782 fMax);
783 }
784
785 createResult(rContainer, std::move(aTargetColor), std::move(aTargetOpacity), aUnitGradientToObject, true);
786 }
787 }
788
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)
804 {
805 if(pFocal && !pFocal->equal(getStart()))
806 {
807 maFocal = *pFocal;
808 maFocalVector = maFocal - getStart();
809 mbFocalSet = true;
810 }
811 }
812
814 {
815 }
816
818 {
819 const SvgGradientHelper* pSvgGradientHelper = dynamic_cast< const SvgGradientHelper* >(&rPrimitive);
820
821 if(!pSvgGradientHelper || !SvgGradientHelper::operator==(*pSvgGradientHelper))
822 return false;
823
824 const SvgRadialGradientPrimitive2D& rCompare = static_cast< const SvgRadialGradientPrimitive2D& >(rPrimitive);
825
826 if(getRadius() == rCompare.getRadius())
827 {
828 if(isFocalSet() == rCompare.isFocalSet())
829 {
830 if(isFocalSet())
831 {
832 return getFocal() == rCompare.getFocal();
833 }
834 else
835 {
836 return true;
837 }
838 }
839 }
840
841 return false;
842 }
843
845 {
846 // return ObjectRange
847 return getPolyPolygon().getB2DRange();
848 }
849
850 // provide unique ID
852 {
854 }
855
856} // end of namespace drawinglayer::primitive2d
857
858
859// SvgLinearAtomPrimitive2D class
860
862{
864 {
865 const double fDelta(getOffsetB() - getOffsetA());
866
868 return;
869
870 // use one discrete unit for overlap (one pixel)
871 const double fDiscreteUnit(getDiscreteUnit());
872
873 // use color distance and discrete lengths to calculate step count
874 const sal_uInt32 nSteps(calculateStepsForSvgGradient(getColorA(), getColorB(), fDelta, fDiscreteUnit));
875
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;
881
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 (!)
888
889 // prepare polygon in needed width at start position (with discrete overlap)
890 const basegfx::B2DPolygon aPolygon(
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
897
898 // prepare loop (inside to outside, [0.0 .. 1.0[)
899 double fUnitScale(0.0);
900 const double fUnitStep(1.0 / nSteps);
901
902 for(sal_uInt32 a(0); a < nSteps; a++, fUnitScale += fUnitStep)
903 {
904 basegfx::B2DPolygon aNew(aPolygon);
905
906 aNew.transform(basegfx::utils::createTranslateB2DHomMatrix(fDelta * fUnitScale, 0.0));
907 rContainer.push_back(new PolyPolygonColorPrimitive2D(
909 basegfx::interpolate(getColorA(), getColorB(), fUnitScale)));
910 }
911 }
912
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)
920 {
921 if(mfOffsetA > mfOffsetB)
922 {
923 OSL_ENSURE(false, "Wrong offset order (!)");
924 std::swap(mfOffsetA, mfOffsetB);
925 }
926 }
927
929 {
930 if(DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
931 {
932 const SvgLinearAtomPrimitive2D& rCompare = static_cast< const SvgLinearAtomPrimitive2D& >(rPrimitive);
933
934 return (getColorA() == rCompare.getColorA()
935 && getColorB() == rCompare.getColorB()
936 && getOffsetA() == rCompare.getOffsetA()
937 && getOffsetB() == rCompare.getOffsetB());
938 }
939
940 return false;
941 }
942
943 // provide unique ID
945 {
947 }
948
949} // end of namespace drawinglayer::primitive2d
950
951
952// SvgRadialAtomPrimitive2D class
953
955{
957 {
958 const double fDeltaScale(getScaleB() - getScaleA());
959
960 if(basegfx::fTools::equalZero(fDeltaScale))
961 return;
962
963 // use one discrete unit for overlap (one pixel)
964 const double fDiscreteUnit(getDiscreteUnit());
965
966 // use color distance and discrete lengths to calculate step count
967 const sal_uInt32 nSteps(calculateStepsForSvgGradient(getColorA(), getColorB(), fDeltaScale, fDiscreteUnit));
968
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);
972
973 for(sal_uInt32 a(0); a < nSteps; a++, fUnitScale += fUnitStep)
974 {
975 basegfx::B2DHomMatrix aTransform;
976 const double fEndScale(getScaleB() - (fDeltaScale * fUnitScale));
977
978 if(isTranslateSet())
979 {
980 const basegfx::B2DVector aTranslate(
984 fUnitScale));
985
987 fEndScale,
988 fEndScale,
989 aTranslate.getX(),
990 aTranslate.getY());
991 }
992 else
993 {
995 fEndScale,
996 fEndScale);
997 }
998
1000
1001 aNew.transform(aTransform);
1002 rContainer.push_back(new PolyPolygonColorPrimitive2D(
1004 basegfx::interpolate(getColorB(), getColorA(), fUnitScale)));
1005 }
1006 }
1007
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)
1015 {
1016 // check and evtl. set translations
1017 if(!rTranslateA.equal(rTranslateB))
1018 {
1019 mpTranslate.reset( new VectorPair(rTranslateA, rTranslateB) );
1020 }
1021
1022 // scale A and B have to be positive
1023 mfScaleA = std::max(mfScaleA, 0.0);
1024 mfScaleB = std::max(mfScaleB, 0.0);
1025
1026 // scale B has to be bigger than scale A; swap if different
1027 if(mfScaleA > mfScaleB)
1028 {
1029 OSL_ENSURE(false, "Wrong offset order (!)");
1030 std::swap(mfScaleA, mfScaleB);
1031
1032 if(mpTranslate)
1033 {
1034 std::swap(mpTranslate->maTranslateA, mpTranslate->maTranslateB);
1035 }
1036 }
1037 }
1038
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)
1046 {
1047 // scale A and B have to be positive
1048 mfScaleA = std::max(mfScaleA, 0.0);
1049 mfScaleB = std::max(mfScaleB, 0.0);
1050
1051 // scale B has to be bigger than scale A; swap if different
1052 if(mfScaleA > mfScaleB)
1053 {
1054 OSL_ENSURE(false, "Wrong offset order (!)");
1055 std::swap(mfScaleA, mfScaleB);
1056 }
1057 }
1058
1060 {
1061 }
1062
1064 {
1065 if(!DiscreteMetricDependentPrimitive2D::operator==(rPrimitive))
1066 return false;
1067
1068 const SvgRadialAtomPrimitive2D& rCompare = static_cast< const SvgRadialAtomPrimitive2D& >(rPrimitive);
1069
1070 if(getColorA() == rCompare.getColorA()
1071 && getColorB() == rCompare.getColorB()
1072 && getScaleA() == rCompare.getScaleA()
1073 && getScaleB() == rCompare.getScaleB())
1074 {
1075 if(isTranslateSet() && rCompare.isTranslateSet())
1076 {
1077 return (getTranslateA() == rCompare.getTranslateA()
1078 && getTranslateB() == rCompare.getTranslateB());
1079 }
1080 else if(!isTranslateSet() && !rCompare.isTranslateSet())
1081 {
1082 return true;
1083 }
1084 }
1085
1086 return false;
1087 }
1088
1089 // provide unique ID
1091 {
1093 }
1094
1095} // end of namespace
1096
1097/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Point maStart
const Point maEnd
void rotate(double fRadiant)
void translate(double fX, double fY)
void scale(double fX, double fY)
void transform(const basegfx::B2DHomMatrix &rMatrix)
B2DRange getB2DRange() const
void transform(const basegfx::B2DHomMatrix &rMatrix)
B2DPoint getMaximum() const
B2DPoint getMinimum() const
double getLength() const
double getDistance(const BColor &rColor) const
TYPE getMaxX() const
TYPE getWidth() const
TYPE getMinX() const
TYPE getMinY() const
TYPE getMaxY() const
TYPE getHeight() const
bool equal(const Tuple2D< TYPE > &rTup) const
TYPE getX() const
TYPE getY() const
a single GradientStop defining a color and opacity at a distance
virtual void create2DDecomposition(Primitive2DContainer &rContainer, const geometry::ViewInformation2D &rViewInformation) const override
local decomposition.
const basegfx::BColor & getColorA() const
data read access
SvgLinearAtomPrimitive2D(const basegfx::BColor &aColorA, double fOffsetA, const basegfx::BColor &aColorB, double fOffsetB)
constructor
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
virtual void createAtom(Primitive2DContainer &rTargetColor, Primitive2DContainer &rTargetOpacity, const SvgGradientEntry &rFrom, const SvgGradientEntry &rTo, sal_Int32 nOffsetFrom, sal_Int32 nOffsetTo) const override
local helpers
SvgLinearGradientPrimitive2D(const basegfx::B2DHomMatrix &rGradientTransform, const basegfx::B2DPolyPolygon &rPolyPolygon, SvgGradientEntryVector &&rGradientEntries, const basegfx::B2DPoint &rStart, const basegfx::B2DPoint &rEnd, bool bUseUnitCoordinates, SpreadMethod aSpreadMethod)
constructor
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
virtual void create2DDecomposition(Primitive2DContainer &rContainer, const geometry::ViewInformation2D &rViewInformation) const override
local decomposition.
virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &rViewInformation) const override
get range
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
const basegfx::B2DPoint & getEnd() const
data read access
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
std::unique_ptr< VectorPair > mpTranslate
Only used when focal is set.
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
virtual void create2DDecomposition(Primitive2DContainer &rContainer, const geometry::ViewInformation2D &rViewInformation) const override
local decomposition.
const basegfx::BColor & getColorA() const
data read access
SvgRadialAtomPrimitive2D(const basegfx::BColor &aColorA, double fScaleA, const basegfx::B2DVector &rTranslateA, const basegfx::BColor &aColorB, double fScaleB, const basegfx::B2DVector &rTranslateB)
constructor
virtual sal_uInt32 getPrimitive2DID() const override
provide unique ID
SvgRadialGradientPrimitive2D(const basegfx::B2DHomMatrix &rGradientTransform, const basegfx::B2DPolyPolygon &rPolyPolygon, SvgGradientEntryVector &&rGradientEntries, const basegfx::B2DPoint &rStart, double fRadius, bool bUseUnitCoordinates, SpreadMethod aSpreadMethod, const basegfx::B2DPoint *pFocal)
constructor
virtual bool operator==(const BasePrimitive2D &rPrimitive) const override
compare operator
virtual void create2DDecomposition(Primitive2DContainer &rContainer, const geometry::ViewInformation2D &rViewInformation) const override
local decomposition.
virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D &rViewInformation) const override
get range
virtual void createAtom(Primitive2DContainer &rTargetColor, Primitive2DContainer &rTargetOpacity, const SvgGradientEntry &rFrom, const SvgGradientEntry &rTo, sal_Int32 nOffsetFrom, sal_Int32 nOffsetTo) const override
local helpers
basegfx::B2DPoint maFocal
Focal only used when focal is set at all, see constructors.
int nCount
#define PRIMITIVE2D_ID_SVGLINEARATOMPRIMITIVE2D
#define PRIMITIVE2D_ID_SVGRADIALATOMPRIMITIVE2D
#define PRIMITIVE2D_ID_SVGLINEARGRADIENTPRIMITIVE2D
#define PRIMITIVE2D_ID_SVGRADIALGRADIENTPRIMITIVE2D
uno_Any a
#define SAL_WARN(area, stream)
VCL_DLLPUBLIC bool isVCLSkiaEnabled()
bool lessOrEqual(const T &rfValA, const T &rfValB)
bool more(const T &rfValA, const T &rfValB)
bool equalZero(const T &rfVal)
bool betweenOrEqualEither(const T &rfValA, const T &rfValB, const T &rfValC)
bool equal(T const &rfValA, T const &rfValB)
double getLength(const B2DPolygon &rCandidate)
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
B2DPolygon createPolygonFromRect(const B2DRectangle &rRect, double fRadiusX, double fRadiusY)
B2DPolygon const & createPolygonFromUnitCircle(sal_uInt32 nStartQuadrant=0)
B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
B2DHomMatrix createTranslateB2DHomMatrix(double fTranslateX, double fTranslateY)
B2DTuple interpolate(const B2DTuple &rOld1, const B2DTuple &rOld2, double t)
B2IRange fround(const B2DRange &rRange)
size
rtl::Reference< BasePrimitive2D > Primitive2DReference
Definition: CommonTypes.hxx:27
::std::vector< SvgGradientEntry > SvgGradientEntryVector
basegfx::B2DPolyPolygon maPolyPolygon