Bump version to 6.4-15
[LibreOffice.git] / include / svx / svdtrans.hxx
blob0bc9bee518c5f7ff6359a1c336f2e663be870aa1
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 <svx/svxdllapi.h>
25 #include <tools/fldunit.hxx>
26 #include <tools/fract.hxx>
27 #include <tools/gen.hxx>
28 #include <tools/helpers.hxx>
29 #include <tools/mapunit.hxx>
30 #include <tools/poly.hxx>
32 // The DrawingEngine's angles are specified in 1/100th degrees
33 // We need to convert these angles to radians, in order to be able
34 // to process them with trigonometric functions.
35 // This is done, using the constant F_PI18000.
37 // Example usage:
38 // nAngle ... is an angle in 1/100 Deg
40 // Which is converted, by this:
41 // double nSin=sin(nAngle*F_PI18000);
43 // To convert it back, we use division.
45 // That maximum shear angle
46 #define SDRMAXSHEAR 8900
48 class XPolygon;
49 class XPolyPolygon;
51 inline void MovePoly(tools::Polygon& rPoly, const Size& S) { rPoly.Move(S.Width(),S.Height()); }
52 void MoveXPoly(XPolygon& rPoly, const Size& S);
54 SVX_DLLPUBLIC void ResizeRect(tools::Rectangle& rRect, const Point& rRef, const Fraction& xFact, const Fraction& yFact);
55 inline void ResizePoint(Point& rPnt, const Point& rRef, const Fraction& xFract, const Fraction& yFract);
56 void ResizePoly(tools::Polygon& rPoly, const Point& rRef, const Fraction& xFact, const Fraction& yFact);
57 void ResizeXPoly(XPolygon& rPoly, const Point& rRef, const Fraction& xFact, const Fraction& yFact);
59 inline void RotatePoint(Point& rPnt, const Point& rRef, double sn, double cs);
60 SVX_DLLPUBLIC void RotatePoly(tools::Polygon& rPoly, const Point& rRef, double sn, double cs);
61 void RotateXPoly(XPolygon& rPoly, const Point& rRef, double sn, double cs);
62 void RotateXPoly(XPolyPolygon& rPoly, const Point& rRef, double sn, double cs);
64 void MirrorPoint(Point& rPnt, const Point& rRef1, const Point& rRef2);
65 void MirrorXPoly(XPolygon& rPoly, const Point& rRef1, const Point& rRef2);
67 inline void ShearPoint(Point& rPnt, const Point& rRef, double tn, bool bVShear = false);
68 SVX_DLLPUBLIC void ShearPoly(tools::Polygon& rPoly, const Point& rRef, double tn);
69 void ShearXPoly(XPolygon& rPoly, const Point& rRef, double tn, bool bVShear = false);
71 /**
72 * rPnt.X/rPnt.Y is set to rCenter.X or rCenter.Y!
73 * We then only need to rotate rPnt by rCenter.
75 * @return the returned angle is in rad
77 inline double GetCrookAngle(Point& rPnt, const Point& rCenter, const Point& rRad, bool bVertical);
79 /**
80 * The following methods accept a point of an XPolygon, whereas the neighbouring
81 * control points of the actual point are passed in pC1/pC2.
82 * Via rSin/rCos, sin(nAngle) and cos(nAngle) are returned.
84 * @return the returned angle is in rad
86 double CrookRotateXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
87 const Point& rRad, double& rSin, double& rCos, bool bVert);
88 double CrookSlantXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
89 const Point& rRad, double& rSin, double& rCos, bool bVert);
90 double CrookStretchXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
91 const Point& rRad, double& rSin, double& rCos, bool bVert,
92 const tools::Rectangle& rRefRect);
94 void CrookRotatePoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
95 void CrookSlantPoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
96 void CrookStretchPoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert, const tools::Rectangle& rRefRect);
98 void CrookRotatePoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
99 void CrookSlantPoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert);
100 void CrookStretchPoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert, const tools::Rectangle& rRefRect);
102 /**************************************************************************************************/
103 /* Inline */
104 /**************************************************************************************************/
106 inline void ResizePoint(Point& rPnt, const Point& rRef, const Fraction& xFract, const Fraction& yFract)
108 double nxFract = xFract.IsValid() ? static_cast<double>(xFract) : 1.0;
109 double nyFract = yFract.IsValid() ? static_cast<double>(yFract) : 1.0;
110 rPnt.setX(rRef.X() + FRound( (rPnt.X() - rRef.X()) * nxFract ));
111 rPnt.setY(rRef.Y() + FRound( (rPnt.Y() - rRef.Y()) * nyFract ));
114 inline void RotatePoint(Point& rPnt, const Point& rRef, double sn, double cs)
116 long dx=rPnt.X()-rRef.X();
117 long dy=rPnt.Y()-rRef.Y();
118 rPnt.setX(FRound(rRef.X()+dx*cs+dy*sn));
119 rPnt.setY(FRound(rRef.Y()+dy*cs-dx*sn));
122 inline void ShearPoint(Point& rPnt, const Point& rRef, double tn, bool bVShear)
124 if (!bVShear) { // Horizontal
125 if (rPnt.Y()!=rRef.Y()) { // else not needed
126 rPnt.AdjustX(-FRound((rPnt.Y()-rRef.Y())*tn));
128 } else { // or else vertical
129 if (rPnt.X()!=rRef.X()) { // else not needed
130 rPnt.AdjustY(-FRound((rPnt.X()-rRef.X())*tn));
135 inline double GetCrookAngle(Point& rPnt, const Point& rCenter, const Point& rRad, bool bVertical)
137 double nAngle;
138 if (bVertical) {
139 long dy=rPnt.Y()-rCenter.Y();
140 nAngle=static_cast<double>(dy)/static_cast<double>(rRad.Y());
141 rPnt.setY(rCenter.Y());
142 } else {
143 long dx=rCenter.X()-rPnt.X();
144 nAngle=static_cast<double>(dx)/static_cast<double>(rRad.X());
145 rPnt.setX(rCenter.X());
147 return nAngle;
150 /**************************************************************************************************/
151 /**************************************************************************************************/
154 * The Y axis points down!
155 * The function negates the Y axis, when calculating the angle, such
156 * that GetAngle(Point(0,-1))=90 deg.
157 * GetAngle(Point(0,0)) returns 0.
159 * @return the returned value is in the range of -180.00..179.99 deg
160 * and is in 1/100 deg units
162 SVX_DLLPUBLIC long GetAngle(const Point& rPnt);
164 long NormAngle18000(long a); /// Normalize angle to -180.00..179.99
166 SVX_DLLPUBLIC long NormAngle36000(long a); /// Normalize angle to 0.00..359.99
168 sal_uInt16 GetAngleSector(long nAngle); /// Determine sector within the cartesian coordinate system
171 * Calculates the length of (0,0) via a^2 + b^2 = c^2
172 * In order to avoid overflows, we ignore some decimal places.
174 long GetLen(const Point& rPnt);
177 * The transformation of a rectangle into a polygon, by
178 * using angle parameters from GeoStat. ------------
179 * The point of reference is always the Point 0, meaning /1 2/
180 * the upper left corner of the initial rectangle. / /
181 * When calculating the polygon, the order is first / /
182 * shear and then the rotation. / /
183 * / / \
184 * / / |
185 * A) Initial rectangle aRect B) After applying Shear /0 3/ Rot|
186 * +------------------+ -------------------- ------------------
187 * |0 1| \0 1\ C) After applying Rotate
188 * | | \ \
189 * | | | \ \
190 * |3 2| | \3 2\
191 * +------------------+ | --------------------
192 * |Shr
194 * When converting the polygon back into a rect, the order is necessarily the
195 * other way around:
196 * - Calculating the rotation angle: angle of the line 0-1 in figure C) to the horizontal
197 * - Turning the sheared rect back (we get figure B)
198 * - Determining the width of the rect = length of the line 0-1 in figure B)
199 * - Determining the height of the rect = vertical distance between the points 0 and 3
200 * of figure B)
201 * - Determining the shear angle from the line 0-3 to the perpendicular line.
203 * We need to keep in mind that the polygon can be mirrored when it was
204 * transformed in the meantime (e.g. mirror or resize with negative factor).
205 * In that case, we first need to normalize, by swapping points (0 with 3 and 1
206 * with 2), so that it has the right orientation.
208 * Note: a positive shear angle means a shear with a positive visible curvature
209 * on the screen. Mathematically, that would be a negative curvature, as the
210 * Y axis runs from top to bottom on the screen.
211 * Rotation angle: positive means a visible left rotation.
214 class GeoStat { // Geometric state for a rect
215 public:
216 long nRotationAngle;
217 long nShearAngle;
218 double nTan; // tan(nShearAngle)
219 double nSin; // sin(nRotationAngle)
220 double nCos; // cos(nRotationAngle)
222 GeoStat(): nRotationAngle(0),nShearAngle(0),nTan(0.0),nSin(0.0),nCos(1.0) {}
223 void RecalcSinCos();
224 void RecalcTan();
227 tools::Polygon Rect2Poly(const tools::Rectangle& rRect, const GeoStat& rGeo);
228 void Poly2Rect(const tools::Polygon& rPol, tools::Rectangle& rRect, GeoStat& rGeo);
230 void OrthoDistance8(const Point& rPt0, Point& rPt, bool bBigOrtho);
231 void OrthoDistance4(const Point& rPt0, Point& rPt, bool bBigOrtho);
233 // Multiplication and subsequent division
234 // Calculation and intermediate values are in BigInt
235 SVX_DLLPUBLIC long BigMulDiv(long nVal, long nMul, long nDiv);
237 class FrPair {
238 Fraction aX;
239 Fraction aY;
240 public:
241 FrPair(const Fraction& rBoth) : aX(rBoth),aY(rBoth) {}
242 FrPair(const Fraction& rX, const Fraction& rY) : aX(rX),aY(rY) {}
243 FrPair(long nMul, long nDiv) : aX(nMul,nDiv),aY(nMul,nDiv) {}
244 FrPair(long xMul, long xDiv, long yMul, long yDiv): aX(xMul,xDiv),aY(yMul,yDiv) {}
245 const Fraction& X() const { return aX; }
246 const Fraction& Y() const { return aY; }
247 Fraction& X() { return aX; }
248 Fraction& Y() { return aY; }
251 // To convert units of measurement
252 SVX_DLLPUBLIC FrPair GetMapFactor(MapUnit eS, MapUnit eD);
253 FrPair GetMapFactor(FieldUnit eS, FieldUnit eD);
255 inline bool IsMetric(MapUnit eU) {
256 return (eU==MapUnit::Map100thMM || eU==MapUnit::Map10thMM || eU==MapUnit::MapMM || eU==MapUnit::MapCM);
259 inline bool IsInch(MapUnit eU) {
260 return (eU==MapUnit::Map1000thInch || eU==MapUnit::Map100thInch || eU==MapUnit::Map10thInch || eU==MapUnit::MapInch ||
261 eU==MapUnit::MapPoint || eU==MapUnit::MapTwip);
264 inline bool IsMetric(FieldUnit eU) {
265 return (eU == FieldUnit::MM || eU == FieldUnit::CM || eU == FieldUnit::M
266 || eU == FieldUnit::KM || eU == FieldUnit::MM_100TH);
269 inline bool IsInch(FieldUnit eU) {
270 return (eU == FieldUnit::TWIP || eU == FieldUnit::POINT
271 || eU == FieldUnit::PICA || eU == FieldUnit::INCH
272 || eU == FieldUnit::FOOT || eU == FieldUnit::MILE);
275 class SVX_DLLPUBLIC SdrFormatter {
276 long nMul_;
277 long nDiv_;
278 short nComma_;
279 bool bDirty;
280 MapUnit const eSrcMU;
281 MapUnit const eDstMU;
282 private:
283 SVX_DLLPRIVATE void Undirty();
284 public:
285 SdrFormatter(MapUnit eSrc, MapUnit eDst)
286 : nMul_(0)
287 , nDiv_(0)
288 , nComma_(0)
289 , bDirty(true)
290 , eSrcMU(eSrc)
291 , eDstMU(eDst)
294 OUString GetStr(long nVal) const;
295 static OUString GetUnitStr(MapUnit eUnit);
296 static OUString GetUnitStr(FieldUnit eUnit);
300 #endif // INCLUDED_SVX_SVDTRANS_HXX
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */