bump product version to 7.6.3.2-android
[LibreOffice.git] / svx / source / sdr / primitive2d / sdrframeborderprimitive2d.cxx
blobe2f2fa772dd8c4908d4240af3a9ad75441a28b02
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 <svx/sdr/primitive2d/sdrframeborderprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/borderlineprimitive2d.hxx>
22 #include <drawinglayer/geometry/viewinformation2d.hxx>
23 #include <svx/sdr/primitive2d/svx_primitivetypes2d.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <svtools/borderhelper.hxx>
27 namespace
29 double snapToDiscreteUnit(
30 double fValue,
31 double fMinimalDiscreteUnit)
33 if(0.0 != fValue)
35 fValue = std::max(fValue, fMinimalDiscreteUnit);
38 return fValue;
41 class StyleVectorCombination
43 private:
44 struct OffsetAndHalfWidthAndColor
46 double mfOffset;
47 double mfHalfWidth;
48 Color maColor;
50 OffsetAndHalfWidthAndColor(double offset, double halfWidth, Color color) :
51 mfOffset(offset),
52 mfHalfWidth(halfWidth),
53 maColor(color)
57 double mfRefModeOffset;
58 basegfx::B2DVector maB2DVector;
59 double mfAngle;
60 std::vector< OffsetAndHalfWidthAndColor > maOffsets;
62 public:
63 StyleVectorCombination(
64 const svx::frame::Style& rStyle,
65 const basegfx::B2DVector& rB2DVector,
66 double fAngle,
67 bool bMirrored,
68 const Color* pForceColor,
69 double fMinimalDiscreteUnit)
70 : mfRefModeOffset(0.0),
71 maB2DVector(rB2DVector),
72 mfAngle(fAngle)
74 if (!rStyle.IsUsed())
75 return;
77 svx::frame::RefMode aRefMode(rStyle.GetRefMode());
78 Color aPrim(rStyle.GetColorPrim());
79 Color aSecn(rStyle.GetColorSecn());
80 const bool bSecnUsed(0.0 != rStyle.Secn());
82 // Get the single segment line widths. This is the point where the
83 // minimal discrete unit will be used if given (fMinimalDiscreteUnit). If
84 // not given it's 0.0 and thus will have no influence.
85 double fPrim(snapToDiscreteUnit(rStyle.Prim(), fMinimalDiscreteUnit));
86 const double fDist(snapToDiscreteUnit(rStyle.Dist(), fMinimalDiscreteUnit));
87 double fSecn(snapToDiscreteUnit(rStyle.Secn(), fMinimalDiscreteUnit));
89 // Of course also do not use svx::frame::Style::GetWidth() for obvious
90 // reasons.
91 const double fStyleWidth(fPrim + fDist + fSecn);
93 if(bMirrored)
95 switch(aRefMode)
97 case svx::frame::RefMode::Begin: aRefMode = svx::frame::RefMode::End; break;
98 case svx::frame::RefMode::End: aRefMode = svx::frame::RefMode::Begin; break;
99 default: break;
102 if(bSecnUsed)
104 std::swap(aPrim, aSecn);
105 std::swap(fPrim, fSecn);
109 if (svx::frame::RefMode::Centered != aRefMode)
111 const double fHalfWidth(fStyleWidth * 0.5);
113 if (svx::frame::RefMode::Begin == aRefMode)
115 // move aligned below vector
116 mfRefModeOffset = fHalfWidth;
118 else if (svx::frame::RefMode::End == aRefMode)
120 // move aligned above vector
121 mfRefModeOffset = -fHalfWidth;
125 if (bSecnUsed)
127 // both or all three lines used
128 const bool bPrimTransparent(rStyle.GetColorPrim().IsFullyTransparent());
129 const bool bDistTransparent(!rStyle.UseGapColor() || rStyle.GetColorGap().IsFullyTransparent());
130 const bool bSecnTransparent(aSecn.IsFullyTransparent());
132 if(!bPrimTransparent || !bDistTransparent || !bSecnTransparent)
134 const double a(mfRefModeOffset - (fStyleWidth * 0.5));
135 const double b(a + fPrim);
136 const double c(b + fDist);
137 const double d(c + fSecn);
139 maOffsets.push_back(
140 OffsetAndHalfWidthAndColor(
141 (a + b) * 0.5,
142 fPrim * 0.5,
143 nullptr != pForceColor ? *pForceColor : aPrim));
145 maOffsets.push_back(
146 OffsetAndHalfWidthAndColor(
147 (b + c) * 0.5,
148 fDist * 0.5,
149 rStyle.UseGapColor()
150 ? (nullptr != pForceColor ? *pForceColor : rStyle.GetColorGap())
151 : COL_TRANSPARENT));
153 maOffsets.push_back(
154 OffsetAndHalfWidthAndColor(
155 (c + d) * 0.5,
156 fSecn * 0.5,
157 nullptr != pForceColor ? *pForceColor : aSecn));
160 else
162 // one line used, push two values, from outer to inner
163 if(!rStyle.GetColorPrim().IsFullyTransparent())
165 maOffsets.push_back(
166 OffsetAndHalfWidthAndColor(
167 mfRefModeOffset,
168 fPrim * 0.5,
169 nullptr != pForceColor ? *pForceColor : aPrim));
174 double getRefModeOffset() const { return mfRefModeOffset; }
175 const basegfx::B2DVector& getB2DVector() const { return maB2DVector; }
176 double getAngle() const { return mfAngle; }
177 bool empty() const { return maOffsets.empty(); }
178 size_t size() const { return maOffsets.size(); }
180 void getColorAndOffsetAndHalfWidth(size_t nIndex, Color& rColor, double& rfOffset, double& rfHalfWidth) const
182 if(nIndex >= maOffsets.size())
183 return;
184 const OffsetAndHalfWidthAndColor& rCandidate(maOffsets[nIndex]);
185 rfOffset = rCandidate.mfOffset;
186 rfHalfWidth = rCandidate.mfHalfWidth;
187 rColor = rCandidate.maColor;
191 class StyleVectorTable
193 private:
194 std::vector< StyleVectorCombination > maEntries;
196 public:
197 StyleVectorTable()
201 void add(
202 const svx::frame::Style& rStyle,
203 const basegfx::B2DVector& rMyVector,
204 const basegfx::B2DVector& rOtherVector,
205 bool bMirrored,
206 double fMinimalDiscreteUnit)
208 if(!rStyle.IsUsed() || basegfx::areParallel(rMyVector, rOtherVector))
209 return;
211 // create angle between both. angle() needs vectors pointing away from the same point,
212 // so take the mirrored one. Add M_PI to get from -pi..+pi to [0..M_PI_2] for sorting
213 const double fAngle(basegfx::B2DVector(-rMyVector.getX(), -rMyVector.getY()).angle(rOtherVector) + M_PI);
214 maEntries.emplace_back(
215 rStyle,
216 rOtherVector,
217 fAngle,
218 bMirrored,
219 nullptr,
220 fMinimalDiscreteUnit);
223 void sort()
225 // sort inverse from highest to lowest
226 std::sort(
227 maEntries.begin(),
228 maEntries.end(),
229 [](const StyleVectorCombination& a, const StyleVectorCombination& b)
230 { return a.getAngle() > b.getAngle(); });
233 bool empty() const { return maEntries.empty(); }
234 const std::vector< StyleVectorCombination >& getEntries() const{ return maEntries; }
237 struct CutSet
239 double mfOLML;
240 double mfORML;
241 double mfOLMR;
242 double mfORMR;
244 CutSet() : mfOLML(0.0), mfORML(0.0), mfOLMR(0.0), mfORMR(0.0)
248 bool operator<( const CutSet& rOther) const
250 const double fA(mfOLML + mfORML + mfOLMR + mfORMR);
251 const double fB(rOther.mfOLML + rOther.mfORML + rOther.mfOLMR + rOther.mfORMR);
253 return fA < fB;
256 double getSum() const { return mfOLML + mfORML + mfOLMR + mfORMR; }
259 void getCutSet(
260 CutSet& rCutSet,
261 const basegfx::B2DPoint& rLeft,
262 const basegfx::B2DPoint& rRight,
263 const basegfx::B2DVector& rX,
264 const basegfx::B2DPoint& rOtherLeft,
265 const basegfx::B2DPoint& rOtherRight,
266 const basegfx::B2DVector& rOtherX)
268 basegfx::utils::findCut(
269 rLeft,
271 rOtherLeft,
272 rOtherX,
273 CutFlagValue::LINE,
274 &rCutSet.mfOLML);
276 basegfx::utils::findCut(
277 rRight,
279 rOtherLeft,
280 rOtherX,
281 CutFlagValue::LINE,
282 &rCutSet.mfOLMR);
284 basegfx::utils::findCut(
285 rLeft,
287 rOtherRight,
288 rOtherX,
289 CutFlagValue::LINE,
290 &rCutSet.mfORML);
292 basegfx::utils::findCut(
293 rRight,
295 rOtherRight,
296 rOtherX,
297 CutFlagValue::LINE,
298 &rCutSet.mfORMR);
301 struct ExtendSet
303 double mfExtLeft;
304 double mfExtRight;
306 ExtendSet() : mfExtLeft(0.0), mfExtRight(0.0) {}
309 void getExtends(
310 std::vector<ExtendSet>& rExtendSet, // target Left/Right values to fill
311 const basegfx::B2DPoint& rOrigin, // own vector start
312 const StyleVectorCombination& rCombination, // own vector and offsets for lines
313 const basegfx::B2DVector& rPerpendX, // normalized perpendicular to own vector
314 const std::vector< StyleVectorCombination >& rStyleVector) // other vectors emerging in this point
316 if(!(!rCombination.empty() && !rStyleVector.empty() && rCombination.size() == rExtendSet.size()))
317 return;
319 const size_t nOffsetA(rCombination.size());
321 if(1 == nOffsetA)
323 Color aMyColor; double fMyOffset(0.0); double fMyHalfWidth(0.0);
324 rCombination.getColorAndOffsetAndHalfWidth(0, aMyColor, fMyOffset, fMyHalfWidth);
326 if(!aMyColor.IsFullyTransparent())
328 const basegfx::B2DPoint aLeft(rOrigin + (rPerpendX * (fMyOffset - fMyHalfWidth)));
329 const basegfx::B2DPoint aRight(rOrigin + (rPerpendX * (fMyOffset + fMyHalfWidth)));
330 std::vector< CutSet > aCutSets;
332 for(const auto& rStyleCandidate : rStyleVector)
334 const basegfx::B2DVector aOtherPerpend(basegfx::getNormalizedPerpendicular(rStyleCandidate.getB2DVector()));
335 const size_t nOffsetB(rStyleCandidate.size());
337 for(size_t other(0); other < nOffsetB; other++)
339 Color aOtherColor; double fOtherOffset(0.0); double fOtherHalfWidth(0.0);
340 rStyleCandidate.getColorAndOffsetAndHalfWidth(other, aOtherColor, fOtherOffset, fOtherHalfWidth);
342 if(!aOtherColor.IsFullyTransparent())
344 const basegfx::B2DPoint aOtherLeft(rOrigin + (aOtherPerpend * (fOtherOffset - fOtherHalfWidth)));
345 const basegfx::B2DPoint aOtherRight(rOrigin + (aOtherPerpend * (fOtherOffset + fOtherHalfWidth)));
347 CutSet aNewCutSet;
348 getCutSet(aNewCutSet, aLeft, aRight, rCombination.getB2DVector(), aOtherLeft, aOtherRight, rStyleCandidate.getB2DVector());
349 aCutSets.push_back(aNewCutSet);
354 if(!aCutSets.empty())
356 CutSet aCutSet(aCutSets[0]);
357 const size_t nNumCutSets(aCutSets.size());
359 if(1 != nNumCutSets)
361 double fCutSet(aCutSet.getSum());
363 for(size_t a(1); a < nNumCutSets; a++)
365 const CutSet& rCandidate(aCutSets[a]);
366 const double fCandidate(rCandidate.getSum());
368 if(basegfx::fTools::equalZero(fCandidate - fCutSet))
370 // both have equal center point, use medium cut
371 const double fNewOLML(std::max(std::min(rCandidate.mfOLML, rCandidate.mfORML), std::min(aCutSet.mfOLML, aCutSet.mfORML)));
372 const double fNewORML(std::min(std::max(rCandidate.mfOLML, rCandidate.mfORML), std::max(aCutSet.mfOLML, aCutSet.mfORML)));
373 const double fNewOLMR(std::max(std::min(rCandidate.mfOLMR, rCandidate.mfORMR), std::min(aCutSet.mfOLMR, aCutSet.mfORMR)));
374 const double fNewORMR(std::min(std::max(rCandidate.mfOLMR, rCandidate.mfORMR), std::max(aCutSet.mfOLMR, aCutSet.mfORMR)));
375 aCutSet.mfOLML = fNewOLML;
376 aCutSet.mfORML = fNewORML;
377 aCutSet.mfOLMR = fNewOLMR;
378 aCutSet.mfORMR = fNewORMR;
379 fCutSet = aCutSet.getSum();
381 else if(fCandidate < fCutSet)
383 // get minimum
384 fCutSet = fCandidate;
385 aCutSet = rCandidate;
390 ExtendSet& rExt(rExtendSet[0]);
392 rExt.mfExtLeft = std::min(aCutSet.mfOLML, aCutSet.mfORML);
393 rExt.mfExtRight = std::min(aCutSet.mfOLMR, aCutSet.mfORMR);
397 else
399 size_t nVisEdgeUp(0);
400 size_t nVisEdgeDn(0);
402 for(size_t my(0); my < nOffsetA; my++)
404 Color aMyColor; double fMyOffset(0.0); double fMyHalfWidth(0.0);
405 rCombination.getColorAndOffsetAndHalfWidth(my, aMyColor, fMyOffset, fMyHalfWidth);
407 if(!aMyColor.IsFullyTransparent())
409 const basegfx::B2DPoint aLeft(rOrigin + (rPerpendX * (fMyOffset - fMyHalfWidth)));
410 const basegfx::B2DPoint aRight(rOrigin + (rPerpendX * (fMyOffset + fMyHalfWidth)));
411 const bool bUpper(my <= (nOffsetA >> 1));
412 const StyleVectorCombination& rStyleCandidate(bUpper ? rStyleVector.front() : rStyleVector.back());
413 const basegfx::B2DVector aOtherPerpend(basegfx::getNormalizedPerpendicular(rStyleCandidate.getB2DVector()));
414 const size_t nOffsetB(rStyleCandidate.size());
415 std::vector< CutSet > aCutSets;
417 for(size_t other(0); other < nOffsetB; other++)
419 Color aOtherColor; double fOtherOffset(0.0); double fOtherHalfWidth(0.0);
420 rStyleCandidate.getColorAndOffsetAndHalfWidth(other, aOtherColor, fOtherOffset, fOtherHalfWidth);
422 if(!aOtherColor.IsFullyTransparent())
424 const basegfx::B2DPoint aOtherLeft(rOrigin + (aOtherPerpend * (fOtherOffset - fOtherHalfWidth)));
425 const basegfx::B2DPoint aOtherRight(rOrigin + (aOtherPerpend * (fOtherOffset + fOtherHalfWidth)));
426 CutSet aCutSet;
427 getCutSet(aCutSet, aLeft, aRight, rCombination.getB2DVector(), aOtherLeft, aOtherRight, rStyleCandidate.getB2DVector());
428 aCutSets.push_back(aCutSet);
432 if(!aCutSets.empty())
434 // sort: min to start, max to end
435 std::sort(aCutSets.begin(), aCutSets.end());
436 const bool bOtherUpper(rStyleCandidate.getAngle() > M_PI);
438 // check if we need min or max
439 // bUpper bOtherUpper MinMax
440 // t t max
441 // t f min
442 // f f max
443 // f t min
444 const bool bMax(bUpper == bOtherUpper);
445 size_t nBaseIndex(0);
446 const size_t nNumCutSets(aCutSets.size());
448 if(bMax)
450 // access at end
451 nBaseIndex = nNumCutSets - 1 - (bUpper ? nVisEdgeUp : nVisEdgeDn);
453 else
455 // access at start
456 nBaseIndex = bUpper ? nVisEdgeUp : nVisEdgeDn;
459 const size_t nSecuredIndex(std::clamp(nBaseIndex, size_t(0), size_t(nNumCutSets - 1)));
460 const CutSet& rCutSet(aCutSets[nSecuredIndex]);
461 ExtendSet& rExt(rExtendSet[my]);
463 rExt.mfExtLeft = std::min(rCutSet.mfOLML, rCutSet.mfORML);
464 rExt.mfExtRight = std::min(rCutSet.mfOLMR, rCutSet.mfORMR);
467 if(bUpper)
469 nVisEdgeUp++;
471 else
473 nVisEdgeDn++;
481 * Helper method to create the correct drawinglayer::primitive2d::BorderLinePrimitive2D
482 * for the given data, especially the correct drawinglayer::primitive2d::BorderLine entries
483 * including the correctly solved/created LineStartEnd extends
485 * rTarget : Here the evtl. created BorderLinePrimitive2D will be appended
486 * rOrigin : StartPoint of the Borderline
487 * rX : Vector of the Borderline
488 * rBorder : svx::frame::Style of the of the Borderline
489 * rStartStyleVectorTable : All other Borderlines which have to be taken into account because
490 * they have the same StartPoint as the current Borderline. These will be used to calculate
491 * the correct LineStartEnd extends tor the BorderLinePrimitive2D. The definition should be
492 * built up using svx::frame::StyleVectorTable and StyleVectorTable::add and includes:
493 * rStyle : the svx::frame::Style of one other BorderLine
494 * rMyVector : the Vector of the *new* to-be-defined BorderLine, identical to rX
495 * rOtherVector: the Vector of one other BorderLine (may be, but does not need to be normalized),
496 * always *pointing away* from the common StartPoint rOrigin
497 * bMirrored : define if rStyle of one other BorderLine shall be mirrored (e.g. bottom-right edges)
498 * With multiple BorderLines the definitions have to be CounterClockWise. This will be
499 * ensured by StyleVectorTable sorting the entries, but knowing this may allow more efficient
500 * data creation.
501 * rEndStyleVectorTable: All other BorderLines that have the same EndPoint. There are differences to
502 * the Start definitions:
503 * - do not forget to consequently use -rX for rMyVector
504 * - definitions have to be ClockWise for the EndBorderLines, will be ensured by sorting
506 * If you take all this into account, you will get correctly extended BorderLinePrimitive2D
507 * representations for the new to be defined BorderLine. That extensions will overlap nicely
508 * with the corresponding BorderLines and take all multiple line definitions in the ::Style into
509 * account.
510 * The internal solver is *not limited* to ::Style(s) with three parts (Left/Gap/Right), this is
511 * just due to svx::frame::Style's definitions. A new solver based on this one can be created
512 * anytime using more mulötiple borders based on the more flexible
513 * std::vector< drawinglayer::primitive2d::BorderLine > if needed.
515 void CreateBorderPrimitives(
516 drawinglayer::primitive2d::Primitive2DContainer& rTarget, /// target for created primitives
517 const basegfx::B2DPoint& rOrigin, /// start point of borderline
518 const basegfx::B2DVector& rX, /// X-Axis of borderline with length
519 const svx::frame::Style& rBorder, /// Style of borderline
520 const StyleVectorTable& rStartStyleVectorTable, /// Styles and vectors (pointing away) at borderline start, ccw
521 const StyleVectorTable& rEndStyleVectorTable, /// Styles and vectors (pointing away) at borderline end, cw
522 const Color* pForceColor, /// If specified, overrides frame border color.
523 double fMinimalDiscreteUnit) /// minimal discrete unit to use for svx::frame::Style width values
525 // get offset color pairs for style, one per visible line
526 const StyleVectorCombination aCombination(
527 rBorder,
529 0.0,
530 false,
531 pForceColor,
532 fMinimalDiscreteUnit);
534 if(aCombination.empty())
535 return;
537 const basegfx::B2DVector aPerpendX(basegfx::getNormalizedPerpendicular(rX));
538 const bool bHasStartStyles(!rStartStyleVectorTable.empty());
539 const bool bHasEndStyles(!rEndStyleVectorTable.empty());
540 const size_t nOffsets(aCombination.size());
541 std::vector<ExtendSet> aExtendSetStart(nOffsets);
542 std::vector<ExtendSet> aExtendSetEnd(nOffsets);
544 if(bHasStartStyles)
546 // create extends for line starts, use given point/vector and offsets
547 getExtends(aExtendSetStart, rOrigin, aCombination, aPerpendX, rStartStyleVectorTable.getEntries());
550 if(bHasEndStyles)
552 // Create extends for line ends, create inverse point/vector and inverse offsets.
553 const StyleVectorCombination aMirroredCombination(
554 rBorder,
555 -rX,
556 0.0,
557 true,
558 pForceColor,
559 fMinimalDiscreteUnit);
561 getExtends(aExtendSetEnd, rOrigin + rX, aMirroredCombination, -aPerpendX, rEndStyleVectorTable.getEntries());
563 // also need to inverse the result to apply to the correct lines
564 std::reverse(aExtendSetEnd.begin(), aExtendSetEnd.end());
567 std::vector< drawinglayer::primitive2d::BorderLine > aBorderlines;
568 const double fNegLength(-rX.getLength());
570 for(size_t a(0); a < nOffsets; a++)
572 Color aMyColor;
573 double fMyOffset(0.0);
574 double fMyHalfWidth(0.0);
575 aCombination.getColorAndOffsetAndHalfWidth(a, aMyColor, fMyOffset, fMyHalfWidth);
576 const ExtendSet& rExtStart(aExtendSetStart[a]);
577 const ExtendSet& rExtEnd(aExtendSetEnd[a]);
579 if(aMyColor.IsFullyTransparent())
581 aBorderlines.push_back(
582 drawinglayer::primitive2d::BorderLine(
583 fMyHalfWidth * 2.0));
585 else
587 aBorderlines.push_back(
588 drawinglayer::primitive2d::BorderLine(
589 drawinglayer::attribute::LineAttribute(
590 aMyColor.getBColor(),
591 fMyHalfWidth * 2.0),
592 fNegLength * rExtStart.mfExtLeft,
593 fNegLength * rExtStart.mfExtRight,
594 fNegLength * rExtEnd.mfExtRight,
595 fNegLength * rExtEnd.mfExtLeft));
599 static const double fPatScFact(10.0); // 10.0 multiply, see old code
600 std::vector<double> aDashing(svtools::GetLineDashing(rBorder.Type(), rBorder.PatternScale() * fPatScFact));
601 drawinglayer::attribute::StrokeAttribute aStrokeAttribute(std::move(aDashing));
602 const basegfx::B2DPoint aStart(rOrigin + (aPerpendX * aCombination.getRefModeOffset()));
604 rTarget.append(
605 drawinglayer::primitive2d::Primitive2DReference(
606 new drawinglayer::primitive2d::BorderLinePrimitive2D(
607 aStart,
608 aStart + rX,
609 std::move(aBorderlines),
610 std::move(aStrokeAttribute))));
613 double getMinimalNonZeroValue(double fCurrent, double fNew)
615 if(0.0 != fNew)
617 if(0.0 != fCurrent)
619 fCurrent = std::min(fNew, fCurrent);
621 else
623 fCurrent = fNew;
627 return fCurrent;
630 double getMinimalNonZeroBorderWidthFromStyle(double fCurrent, const svx::frame::Style& rStyle)
632 if(rStyle.IsUsed())
634 fCurrent = getMinimalNonZeroValue(fCurrent, rStyle.Prim());
635 fCurrent = getMinimalNonZeroValue(fCurrent, rStyle.Dist());
636 fCurrent = getMinimalNonZeroValue(fCurrent, rStyle.Secn());
639 return fCurrent;
643 namespace drawinglayer::primitive2d
645 SdrFrameBorderData::SdrConnectStyleData::SdrConnectStyleData(
646 const svx::frame::Style& rStyle,
647 const basegfx::B2DVector& rNormalizedPerpendicular,
648 bool bStyleMirrored)
649 : maStyle(rStyle),
650 maNormalizedPerpendicular(rNormalizedPerpendicular),
651 mbStyleMirrored(bStyleMirrored)
655 bool SdrFrameBorderData::SdrConnectStyleData::operator==(const SdrFrameBorderData::SdrConnectStyleData& rCompare) const
657 return mbStyleMirrored == rCompare.mbStyleMirrored
658 && maStyle == rCompare.maStyle
659 && maNormalizedPerpendicular == rCompare.maNormalizedPerpendicular;
662 SdrFrameBorderData::SdrFrameBorderData(
663 const basegfx::B2DPoint& rOrigin,
664 const basegfx::B2DVector& rX,
665 const svx::frame::Style& rStyle,
666 const Color* pForceColor)
667 : maOrigin(rOrigin),
668 maX(rX),
669 maStyle(rStyle),
670 maColor(nullptr != pForceColor ? *pForceColor : Color()),
671 mbForceColor(nullptr != pForceColor)
675 void SdrFrameBorderData::addSdrConnectStyleData(
676 bool bStart,
677 const svx::frame::Style& rStyle,
678 const basegfx::B2DVector& rNormalizedPerpendicular,
679 bool bStyleMirrored)
681 if(rStyle.IsUsed())
683 if(bStart)
685 maStart.emplace_back(rStyle, rNormalizedPerpendicular, bStyleMirrored);
687 else
689 maEnd.emplace_back(rStyle, rNormalizedPerpendicular, bStyleMirrored);
694 void SdrFrameBorderData::create2DDecomposition(
695 Primitive2DContainer& rContainer,
696 double fMinimalDiscreteUnit) const
698 StyleVectorTable aStartVector;
699 StyleVectorTable aEndVector;
700 const basegfx::B2DVector aAxis(-maX);
702 for(const auto& rStart : maStart)
704 aStartVector.add(
705 rStart.getStyle(),
706 maX,
707 rStart.getNormalizedPerpendicular(),
708 rStart.getStyleMirrored(),
709 fMinimalDiscreteUnit);
712 for(const auto& rEnd : maEnd)
714 aEndVector.add(
715 rEnd.getStyle(),
716 aAxis,
717 rEnd.getNormalizedPerpendicular(),
718 rEnd.getStyleMirrored(),
719 fMinimalDiscreteUnit);
722 aStartVector.sort();
723 aEndVector.sort();
725 CreateBorderPrimitives(
726 rContainer,
727 maOrigin,
728 maX,
729 maStyle,
730 aStartVector,
731 aEndVector,
732 mbForceColor ? &maColor : nullptr,
733 fMinimalDiscreteUnit);
736 double SdrFrameBorderData::getMinimalNonZeroBorderWidth() const
738 double fRetval(getMinimalNonZeroBorderWidthFromStyle(0.0, maStyle));
740 for(const auto& rStart : maStart)
742 fRetval = getMinimalNonZeroBorderWidthFromStyle(fRetval, rStart.getStyle());
745 for(const auto& rEnd : maEnd)
747 fRetval = getMinimalNonZeroBorderWidthFromStyle(fRetval, rEnd.getStyle());
750 return fRetval;
754 bool SdrFrameBorderData::operator==(const SdrFrameBorderData& rCompare) const
756 return maOrigin == rCompare.maOrigin
757 && maX == rCompare.maX
758 && maStyle == rCompare.maStyle
759 && maColor == rCompare.maColor
760 && mbForceColor == rCompare.mbForceColor
761 && maStart == rCompare.maStart
762 && maEnd == rCompare.maEnd;
766 void SdrFrameBorderPrimitive2D::create2DDecomposition(
767 Primitive2DContainer& rContainer,
768 const geometry::ViewInformation2D& /*aViewInformation*/) const
770 if(getFrameBorders().empty())
772 return;
775 Primitive2DContainer aRetval;
777 // Check and use the minimal non-zero BorderWidth for decompose
778 // if that is set and wanted
779 const double fMinimalDiscreteUnit(doForceToSingleDiscreteUnit()
780 ? mfMinimalNonZeroBorderWidthUsedForDecompose
781 : 0.0);
784 // decompose all buffered SdrFrameBorderData entries and try to merge them
785 // to reduce existing number of BorderLinePrimitive2D(s)
786 for(const auto& rCandidate : getFrameBorders())
788 // get decomposition on one SdrFrameBorderData entry
789 Primitive2DContainer aPartial;
790 rCandidate.create2DDecomposition(
791 aPartial,
792 fMinimalDiscreteUnit);
794 for(const auto& aCandidatePartial : aPartial)
796 if(aRetval.empty())
798 // no local data yet, just add as 1st entry, done
799 aRetval.append(aCandidatePartial);
801 else
803 bool bDidMerge(false);
805 for(auto& aCandidateRetval : aRetval)
807 // try to merge by appending new data to existing data
808 const drawinglayer::primitive2d::Primitive2DReference aMergeRetvalPartial(
809 drawinglayer::primitive2d::tryMergeBorderLinePrimitive2D(
810 static_cast<BorderLinePrimitive2D*>(aCandidateRetval.get()),
811 static_cast<BorderLinePrimitive2D*>(aCandidatePartial.get())));
813 if(aMergeRetvalPartial.is())
815 // could append, replace existing data with merged data, done
816 aCandidateRetval = aMergeRetvalPartial;
817 bDidMerge = true;
818 break;
821 // try to merge by appending existing data to new data
822 const drawinglayer::primitive2d::Primitive2DReference aMergePartialRetval(
823 drawinglayer::primitive2d::tryMergeBorderLinePrimitive2D(
824 static_cast<BorderLinePrimitive2D*>(aCandidatePartial.get()),
825 static_cast<BorderLinePrimitive2D*>(aCandidateRetval.get())));
827 if(aMergePartialRetval.is())
829 // could append, replace existing data with merged data, done
830 aCandidateRetval = aMergePartialRetval;
831 bDidMerge = true;
832 break;
836 if(!bDidMerge)
838 // no merge after checking all existing data, append as new segment
839 aRetval.append(aCandidatePartial);
846 rContainer.append(std::move(aRetval));
849 SdrFrameBorderPrimitive2D::SdrFrameBorderPrimitive2D(
850 SdrFrameBorderDataVector&& rFrameBorders,
851 bool bForceToSingleDiscreteUnit)
852 : maFrameBorders(std::move(rFrameBorders)),
853 mfMinimalNonZeroBorderWidth(0.0),
854 mfMinimalNonZeroBorderWidthUsedForDecompose(0.0),
855 mbForceToSingleDiscreteUnit(bForceToSingleDiscreteUnit)
857 if(!getFrameBorders().empty() && doForceToSingleDiscreteUnit())
859 // detect used minimal non-zero partial border width
860 for(const auto& rCandidate : getFrameBorders())
862 mfMinimalNonZeroBorderWidth = getMinimalNonZeroValue(
863 mfMinimalNonZeroBorderWidth,
864 rCandidate.getMinimalNonZeroBorderWidth());
869 bool SdrFrameBorderPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
871 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
873 const SdrFrameBorderPrimitive2D& rCompare = static_cast<const SdrFrameBorderPrimitive2D&>(rPrimitive);
875 return getFrameBorders() == rCompare.getFrameBorders()
876 && doForceToSingleDiscreteUnit() == rCompare.doForceToSingleDiscreteUnit();
879 return false;
882 void SdrFrameBorderPrimitive2D::get2DDecomposition(
883 Primitive2DDecompositionVisitor& rVisitor,
884 const geometry::ViewInformation2D& rViewInformation) const
886 if(doForceToSingleDiscreteUnit())
888 // Get the current DiscreteUnit, look at X and Y and use the maximum
889 const basegfx::B2DVector aDiscreteVector(rViewInformation.getInverseObjectToViewTransformation() * basegfx::B2DVector(1.0, 1.0));
890 double fDiscreteUnit(std::min(fabs(aDiscreteVector.getX()), fabs(aDiscreteVector.getY())));
892 if(fDiscreteUnit <= mfMinimalNonZeroBorderWidth)
894 // no need to use it, reset
895 fDiscreteUnit = 0.0;
898 if(fDiscreteUnit != mfMinimalNonZeroBorderWidthUsedForDecompose)
900 // conditions of last local decomposition have changed, delete
901 // possible content
902 if(!getBuffered2DDecomposition().empty())
904 const_cast< SdrFrameBorderPrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DContainer());
907 // remember new conditions
908 const_cast< SdrFrameBorderPrimitive2D* >(this)->mfMinimalNonZeroBorderWidthUsedForDecompose = fDiscreteUnit;
912 // call parent. This will call back ::create2DDecomposition above
913 // where mfMinimalNonZeroBorderWidthUsedForDecompose will be used
914 // when doForceToSingleDiscreteUnit() is true
915 BufferedDecompositionPrimitive2D::get2DDecomposition(rVisitor, rViewInformation);
918 // provide unique ID
919 sal_uInt32 SdrFrameBorderPrimitive2D::getPrimitive2DID() const
921 return PRIMITIVE2D_ID_SDRFRAMEBORDERTPRIMITIVE2D;
924 } // end of namespace
926 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */