1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/helplineprimitive2d.hxx>
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
23 #include <basegfx/polygon/b2dpolygonclipper.hxx>
24 #include <basegfx/tools/canvastools.hxx>
25 #include <drawinglayer/geometry/viewinformation2d.hxx>
26 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
28 //////////////////////////////////////////////////////////////////////////////
30 using namespace com::sun::star
;
32 //////////////////////////////////////////////////////////////////////////////
34 namespace drawinglayer
38 Primitive2DSequence
HelplinePrimitive2D::create2DDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
40 std::vector
< BasePrimitive2D
* > aTempPrimitiveTarget
;
42 if(!rViewInformation
.getViewport().isEmpty() && !getDirection().equalZero())
44 // position to view coordinates, DashLen and DashLen in logic
45 const basegfx::B2DPoint
aViewPosition(rViewInformation
.getObjectToViewTransformation() * getPosition());
49 default : // HELPLINESTYLE2D_POINT
51 const double fViewFixValue(15.0);
52 basegfx::B2DVector
aNormalizedDirection(getDirection());
53 aNormalizedDirection
.normalize();
54 aNormalizedDirection
*= fViewFixValue
;
55 const basegfx::B2DPoint
aStartA(aViewPosition
- aNormalizedDirection
);
56 const basegfx::B2DPoint
aEndA(aViewPosition
+ aNormalizedDirection
);
57 basegfx::B2DPolygon aLineA
;
58 aLineA
.append(aStartA
);
60 aLineA
.transform(rViewInformation
.getInverseObjectToViewTransformation());
61 PolygonMarkerPrimitive2D
* pNewA
= new PolygonMarkerPrimitive2D(aLineA
, getRGBColA(), getRGBColB(), getDiscreteDashLength());
62 aTempPrimitiveTarget
.push_back(pNewA
);
64 const basegfx::B2DVector
aPerpendicularNormalizedDirection(basegfx::getPerpendicular(aNormalizedDirection
));
65 const basegfx::B2DPoint
aStartB(aViewPosition
- aPerpendicularNormalizedDirection
);
66 const basegfx::B2DPoint
aEndB(aViewPosition
+ aPerpendicularNormalizedDirection
);
67 basegfx::B2DPolygon aLineB
;
68 aLineB
.append(aStartB
);
70 aLineB
.transform(rViewInformation
.getInverseObjectToViewTransformation());
71 PolygonMarkerPrimitive2D
* pNewB
= new PolygonMarkerPrimitive2D(aLineB
, getRGBColA(), getRGBColB(), getDiscreteDashLength());
72 aTempPrimitiveTarget
.push_back(pNewB
);
76 case HELPLINESTYLE2D_LINE
:
78 basegfx::B2DPolygon aLine
;
80 if(basegfx::areParallel(getDirection(), basegfx::B2DVector(1.0, 0.0)))
82 // parallel to X-Axis, get cuts with Y-Axes
83 const double fCutA((rViewInformation
.getDiscreteViewport().getMinX() - aViewPosition
.getX()) / getDirection().getX());
84 const double fCutB((rViewInformation
.getDiscreteViewport().getMaxX() - aViewPosition
.getX()) / getDirection().getX());
85 const basegfx::B2DPoint
aPosA(aViewPosition
+ (fCutA
* getDirection()));
86 const basegfx::B2DPoint
aPosB(aViewPosition
+ (fCutB
* getDirection()));
87 const bool bBothLeft(aPosA
.getX() < rViewInformation
.getDiscreteViewport().getMinX() && aPosB
.getX() < rViewInformation
.getDiscreteViewport().getMinX());
88 const bool bBothRight(aPosA
.getX() > rViewInformation
.getDiscreteViewport().getMaxX() && aPosB
.getX() < rViewInformation
.getDiscreteViewport().getMaxX());
90 if(!bBothLeft
&& !bBothRight
)
98 // get cuts with X-Axes
99 const double fCutA((rViewInformation
.getDiscreteViewport().getMinY() - aViewPosition
.getY()) / getDirection().getY());
100 const double fCutB((rViewInformation
.getDiscreteViewport().getMaxY() - aViewPosition
.getY()) / getDirection().getY());
101 const basegfx::B2DPoint
aPosA(aViewPosition
+ (fCutA
* getDirection()));
102 const basegfx::B2DPoint
aPosB(aViewPosition
+ (fCutB
* getDirection()));
103 const bool bBothAbove(aPosA
.getY() < rViewInformation
.getDiscreteViewport().getMinY() && aPosB
.getY() < rViewInformation
.getDiscreteViewport().getMinY());
104 const bool bBothBelow(aPosA
.getY() > rViewInformation
.getDiscreteViewport().getMaxY() && aPosB
.getY() < rViewInformation
.getDiscreteViewport().getMaxY());
106 if(!bBothAbove
&& !bBothBelow
)
115 // clip against visible area
116 const basegfx::B2DPolyPolygon
aResult(basegfx::tools::clipPolygonOnRange(aLine
, rViewInformation
.getDiscreteViewport(), true, true));
118 for(sal_uInt32
a(0L); a
< aResult
.count(); a
++)
120 basegfx::B2DPolygon
aPart(aResult
.getB2DPolygon(a
));
121 aPart
.transform(rViewInformation
.getInverseObjectToViewTransformation());
122 PolygonMarkerPrimitive2D
* pNew
= new PolygonMarkerPrimitive2D(aPart
, getRGBColA(), getRGBColB(), getDiscreteDashLength());
123 aTempPrimitiveTarget
.push_back(pNew
);
132 // prepare return value
133 Primitive2DSequence
aRetval(aTempPrimitiveTarget
.size());
135 for(sal_uInt32
a(0L); a
< aTempPrimitiveTarget
.size(); a
++)
137 const Primitive2DReference
xRef(aTempPrimitiveTarget
[a
]);
144 HelplinePrimitive2D::HelplinePrimitive2D(
145 const basegfx::B2DPoint
& rPosition
,
146 const basegfx::B2DVector
& rDirection
,
147 HelplineStyle2D eStyle
,
148 const basegfx::BColor
& rRGBColA
,
149 const basegfx::BColor
& rRGBColB
,
150 double fDiscreteDashLength
)
151 : BufferedDecompositionPrimitive2D(),
152 maPosition(rPosition
),
153 maDirection(rDirection
),
157 mfDiscreteDashLength(fDiscreteDashLength
),
158 maLastObjectToViewTransformation(),
163 bool HelplinePrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
165 if(BufferedDecompositionPrimitive2D::operator==(rPrimitive
))
167 const HelplinePrimitive2D
& rCompare
= (HelplinePrimitive2D
&)rPrimitive
;
169 return (getPosition() == rCompare
.getPosition()
170 && getDirection() == rCompare
.getDirection()
171 && getStyle() == rCompare
.getStyle()
172 && getRGBColA() == rCompare
.getRGBColA()
173 && getRGBColB() == rCompare
.getRGBColB()
174 && getDiscreteDashLength() == rCompare
.getDiscreteDashLength());
180 Primitive2DSequence
HelplinePrimitive2D::get2DDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
182 ::osl::MutexGuard
aGuard( m_aMutex
);
184 if(getBuffered2DDecomposition().hasElements())
186 if(maLastViewport
!= rViewInformation
.getViewport() || maLastObjectToViewTransformation
!= rViewInformation
.getObjectToViewTransformation())
188 // conditions of last local decomposition have changed, delete
189 const_cast< HelplinePrimitive2D
* >(this)->setBuffered2DDecomposition(Primitive2DSequence());
193 if(!getBuffered2DDecomposition().hasElements())
195 // remember ViewRange and ViewTransformation
196 const_cast< HelplinePrimitive2D
* >(this)->maLastObjectToViewTransformation
= rViewInformation
.getObjectToViewTransformation();
197 const_cast< HelplinePrimitive2D
* >(this)->maLastViewport
= rViewInformation
.getViewport();
200 // use parent implementation
201 return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation
);
205 ImplPrimitive2DIDBlock(HelplinePrimitive2D
, PRIMITIVE2D_ID_HELPLINEPRIMITIVE2D
)
207 } // end of namespace primitive2d
208 } // end of namespace drawinglayer
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */