tdf#124953: Use rangelist's combined range top-left address...
[LibreOffice.git] / include / svx / svdtrans.hxx
blobc10f9f8fc2f05fbe5e65b443941adb9fca244927
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 #ifndef INCLUDED_SVX_SVDTRANS_HXX
21 #define INCLUDED_SVX_SVDTRANS_HXX
23 #include <rtl/ustring.hxx>
24 #include <sal/log.hxx>
25 #include <svx/svxdllapi.h>
26 #include <tools/fract.hxx>
27 #include <tools/gen.hxx>
28 #include <tools/helpers.hxx>
29 #include <tools/poly.hxx>
30 #include <vcl/field.hxx>
31 #include <vcl/mapmod.hxx>
33 // The DrawingEngine's angles are specified in 1/100th degrees
34 // We need to convert these angles to radians, in order to be able
35 // to process them with trigonometric functions.
36 // This is done, using the constant F_PI18000.
38 // Example usage:
39 // nAngle ... is an angle in 1/100 Deg
41 // Which is converted, by this:
42 // double nSin=sin(nAngle*F_PI18000);
44 // To convert it back, we use division.
46 // That maximum shear angle
47 #define SDRMAXSHEAR 8900
49 class XPolygon;
50 class XPolyPolygon;
52 inline void MovePoly(tools::Polygon& rPoly, const Size& S) { rPoly.Move(S.Width(),S.Height()); }
53 void MoveXPoly(XPolygon& rPoly, const Size& S);
55 SVX_DLLPUBLIC void ResizeRect(tools::Rectangle& rRect, const Point& rRef, const Fraction& xFact, const Fraction& yFact);
56 inline void ResizePoint(Point& rPnt, const Point& rRef, const Fraction& xFract, const Fraction& yFract);
57 void ResizePoly(tools::Polygon& rPoly, const Point& rRef, const Fraction& xFact, const Fraction& yFact);
58 void ResizeXPoly(XPolygon& rPoly, const Point& rRef, const Fraction& xFact, const Fraction& yFact);
60 inline void RotatePoint(Point& rPnt, const Point& rRef, double sn, double cs);
61 SVX_DLLPUBLIC void RotatePoly(tools::Polygon& rPoly, const Point& rRef, double sn, double cs);
62 void RotateXPoly(XPolygon& rPoly, const Point& rRef, double sn, double cs);
63 void RotateXPoly(XPolyPolygon& rPoly, const Point& rRef, double sn, double cs);
65 void MirrorPoint(Point& rPnt, const Point& rRef1, const Point& rRef2);
66 void MirrorXPoly(XPolygon& rPoly, const Point& rRef1, const Point& rRef2);
68 inline void ShearPoint(Point& rPnt, const Point& rRef, double tn, bool bVShear = false);
69 SVX_DLLPUBLIC void ShearPoly(tools::Polygon& rPoly, const Point& rRef, double tn);
70 void ShearXPoly(XPolygon& rPoly, const Point& rRef, double tn, bool bVShear = false);
72 /**
73 * rPnt.X/rPnt.Y is set to rCenter.X or rCenter.Y!
74 * We then only need to rotate rPnt by rCenter.
76 * @return the returned angle is in rad
78 inline double GetCrookAngle(Point& rPnt, const Point& rCenter, const Point& rRad, bool bVertical);
80 /**
81 * The following methods accept a point of an XPolygon, whereas the neighbouring
82 * control points of the actual point are passed in pC1/pC2.
83 * Via rSin/rCos, sin(nAngle) and cos(nAngle) are returned.
85 * @return the returned angle is in rad
87 double CrookRotateXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
88 const Point& rRad, double& rSin, double& rCos, bool bVert);
89 double CrookSlantXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
90 const Point& rRad, double& rSin, double& rCos, bool bVert);
91 double CrookStretchXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
92 const Point& rRad, double& rSin, double& rCos, bool bVert,
93 const tools::Rectangle& rRefRect);
95 void CrookRotatePoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
96 void CrookSlantPoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
97 void CrookStretchPoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert, const tools::Rectangle& rRefRect);
99 void CrookRotatePoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
100 void CrookSlantPoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
101 void CrookStretchPoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert, const tools::Rectangle& rRefRect);
103 /**************************************************************************************************/
104 /* Inline */
105 /**************************************************************************************************/
107 inline void ResizePoint(Point& rPnt, const Point& rRef, const Fraction& xFract, const Fraction& yFract)
109 double nxFract = xFract.IsValid() ? static_cast<double>(xFract) : 1.0;
110 double nyFract = yFract.IsValid() ? static_cast<double>(yFract) : 1.0;
111 rPnt.setX(rRef.X() + FRound( (rPnt.X() - rRef.X()) * nxFract ));
112 rPnt.setY(rRef.Y() + FRound( (rPnt.Y() - rRef.Y()) * nyFract ));
115 inline void RotatePoint(Point& rPnt, const Point& rRef, double sn, double cs)
117 long dx=rPnt.X()-rRef.X();
118 long dy=rPnt.Y()-rRef.Y();
119 rPnt.setX(FRound(rRef.X()+dx*cs+dy*sn));
120 rPnt.setY(FRound(rRef.Y()+dy*cs-dx*sn));
123 inline void ShearPoint(Point& rPnt, const Point& rRef, double tn, bool bVShear)
125 if (!bVShear) { // Horizontal
126 if (rPnt.Y()!=rRef.Y()) { // else not needed
127 rPnt.AdjustX(-FRound((rPnt.Y()-rRef.Y())*tn));
129 } else { // or else vertical
130 if (rPnt.X()!=rRef.X()) { // else not needed
131 rPnt.AdjustY(-FRound((rPnt.X()-rRef.X())*tn));
136 inline double GetCrookAngle(Point& rPnt, const Point& rCenter, const Point& rRad, bool bVertical)
138 double nAngle;
139 if (bVertical) {
140 long dy=rPnt.Y()-rCenter.Y();
141 nAngle=static_cast<double>(dy)/static_cast<double>(rRad.Y());
142 rPnt.setY(rCenter.Y());
143 } else {
144 long dx=rCenter.X()-rPnt.X();
145 nAngle=static_cast<double>(dx)/static_cast<double>(rRad.X());
146 rPnt.setX(rCenter.X());
148 return nAngle;
151 /**************************************************************************************************/
152 /**************************************************************************************************/
155 * The Y axis points down!
156 * The function negates the Y axis, when calculating the angle, such
157 * that GetAngle(Point(0,-1))=90 deg.
158 * GetAngle(Point(0,0)) returns 0.
160 * @return the returned value is in the range of -180.00..179.99 deg
161 * and is in 1/100 deg units
163 SVX_DLLPUBLIC long GetAngle(const Point& rPnt);
165 long NormAngle18000(long a); /// Normalize angle to -180.00..179.99
167 SVX_DLLPUBLIC long NormAngle36000(long a); /// Normalize angle to 0.00..359.99
169 sal_uInt16 GetAngleSector(long nAngle); /// Determine sector within the cartesian coordinate system
172 * Calculates the length of (0,0) via a^2 + b^2 = c^2
173 * In order to avoid overflows, we ignore some decimal places.
175 long GetLen(const Point& rPnt);
178 * The transformation of a rectangle into a polygon, by
179 * using angle parameters from GeoStat. ------------
180 * The point of reference is always the Point 0, meaning /1 2/
181 * the upper left corner of the initial rectangle. / /
182 * When calculating the polygon, the order is first / /
183 * shear and then the rotation. / /
184 * / / \
185 * / / |
186 * A) Initial rectangle aRect B) After applying Shear /0 3/ Rot|
187 * +------------------+ -------------------- ------------------
188 * |0 1| \0 1\ C) After applying Rotate
189 * | | \ \
190 * | | | \ \
191 * |3 2| | \3 2\
192 * +------------------+ | --------------------
193 * |Shr
195 * When converting the polygon back into a rect, the order is necessarily the
196 * other way around:
197 * - Calculating the rotation angle: angle of the line 0-1 in figure C) to the horizontal
198 * - Turning the sheared rect back (we get figure B)
199 * - Determining the width of the rect = length of the line 0-1 in figure B)
200 * - Determining the height of the rect = vertical distance between the points 0 and 3
201 * of figure B)
202 * - Determining the shear angle from the line 0-3 to the perpendicular line.
204 * We need to keep in mind that the polygon can be mirrored when it was
205 * transformed in the mean time (e.g. mirror or resize with negative factor).
206 * In that case, we first need to normalize, by swapping points (0 with 3 and 1
207 * with 2), so that it has the right orientation.
209 * Note: a positive shear angle means a shear with a positive visible curvature
210 * on the screen. Mathematically, that would be a negative curvature, as the
211 * Y axis runs from top to bottom on the screen.
212 * Rotation angle: positive means a visible left rotation.
215 class GeoStat { // Geometric state for a rect
216 public:
217 long nRotationAngle;
218 long nShearAngle;
219 double nTan; // tan(nShearAngle)
220 double nSin; // sin(nRotationAngle)
221 double nCos; // cos(nRotationAngle)
223 GeoStat(): nRotationAngle(0),nShearAngle(0),nTan(0.0),nSin(0.0),nCos(1.0) {}
224 void RecalcSinCos();
225 void RecalcTan();
228 tools::Polygon Rect2Poly(const tools::Rectangle& rRect, const GeoStat& rGeo);
229 void Poly2Rect(const tools::Polygon& rPol, tools::Rectangle& rRect, GeoStat& rGeo);
231 SVX_DLLPUBLIC void OrthoDistance8(const Point& rPt0, Point& rPt, bool bBigOrtho);
232 SVX_DLLPUBLIC void OrthoDistance4(const Point& rPt0, Point& rPt, bool bBigOrtho);
234 // Multiplication and subsequent division
235 // Calculation and intermediate values are in BigInt
236 SVX_DLLPUBLIC long BigMulDiv(long nVal, long nMul, long nDiv);
238 class FrPair {
239 Fraction aX;
240 Fraction aY;
241 public:
242 FrPair(const Fraction& rBoth) : aX(rBoth),aY(rBoth) {}
243 FrPair(const Fraction& rX, const Fraction& rY) : aX(rX),aY(rY) {}
244 FrPair(long nMul, long nDiv) : aX(nMul,nDiv),aY(nMul,nDiv) {}
245 FrPair(long xMul, long xDiv, long yMul, long yDiv): aX(xMul,xDiv),aY(yMul,yDiv) {}
246 const Fraction& X() const { return aX; }
247 const Fraction& Y() const { return aY; }
248 Fraction& X() { return aX; }
249 Fraction& Y() { return aY; }
252 // To convert units of measurement
253 SVX_DLLPUBLIC FrPair GetMapFactor(MapUnit eS, MapUnit eD);
254 FrPair GetMapFactor(FieldUnit eS, FieldUnit eD);
256 inline bool IsMetric(MapUnit eU) {
257 return (eU==MapUnit::Map100thMM || eU==MapUnit::Map10thMM || eU==MapUnit::MapMM || eU==MapUnit::MapCM);
260 inline bool IsInch(MapUnit eU) {
261 return (eU==MapUnit::Map1000thInch || eU==MapUnit::Map100thInch || eU==MapUnit::Map10thInch || eU==MapUnit::MapInch ||
262 eU==MapUnit::MapPoint || eU==MapUnit::MapTwip);
265 inline bool IsMetric(FieldUnit eU) {
266 return (eU == FieldUnit::MM || eU == FieldUnit::CM || eU == FieldUnit::M
267 || eU == FieldUnit::KM || eU == FieldUnit::MM_100TH);
270 inline bool IsInch(FieldUnit eU) {
271 return (eU == FieldUnit::TWIP || eU == FieldUnit::POINT
272 || eU == FieldUnit::PICA || eU == FieldUnit::INCH
273 || eU == FieldUnit::FOOT || eU == FieldUnit::MILE);
276 class SVX_DLLPUBLIC SdrFormatter {
277 long nMul_;
278 long nDiv_;
279 short nComma_;
280 bool bDirty;
281 MapUnit const eSrcMU;
282 MapUnit const eDstMU;
283 private:
284 SVX_DLLPRIVATE void Undirty();
285 public:
286 SdrFormatter(MapUnit eSrc, MapUnit eDst)
287 : nMul_(0)
288 , nDiv_(0)
289 , nComma_(0)
290 , bDirty(true)
291 , eSrcMU(eSrc)
292 , eDstMU(eDst)
295 void TakeStr(long nVal, OUString& rStr) const;
296 static void TakeUnitStr(MapUnit eUnit, OUString& rStr);
297 static void TakeUnitStr(FieldUnit eUnit, OUString& rStr);
298 static OUString GetUnitStr(FieldUnit eUnit) { OUString aStr; TakeUnitStr(eUnit,aStr); return aStr; }
302 #endif // INCLUDED_SVX_SVDTRANS_HXX
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */