1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
49 // This file is not part of the Qt API. It exists for the convenience
50 // of other Qt classes. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "QtCore/qdebug.h"
57 #include "QtCore/qpoint.h"
58 #include "QtCore/qsize.h"
64 Q_DECL_CONSTEXPR QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
66 Q_DECL_CONSTEXPR QFixed() : val(0) {}
67 Q_DECL_CONSTEXPR QFixed(int i) : val(i<<6) {}
68 Q_DECL_CONSTEXPR QFixed(long i) : val(i<<6) {}
69 QFixed &operator=(int i) { val = (i<<6); return *this; }
70 QFixed &operator=(long i) { val = (i<<6); return *this; }
72 Q_DECL_CONSTEXPR static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
73 Q_DECL_CONSTEXPR static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
75 Q_DECL_CONSTEXPR inline int value() const { return val; }
76 inline void setValue(int value) { val = value; }
78 Q_DECL_CONSTEXPR inline int toInt() const { return (((val)+32) & -64)>>6; }
79 Q_DECL_CONSTEXPR inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
81 Q_DECL_CONSTEXPR inline int truncate() const { return val>>6; }
82 Q_DECL_CONSTEXPR inline QFixed round() const { return fromFixed(((val)+32) & -64); }
83 Q_DECL_CONSTEXPR inline QFixed floor() const { return fromFixed((val) & -64); }
84 Q_DECL_CONSTEXPR inline QFixed ceil() const { return fromFixed((val+63) & -64); }
86 Q_DECL_CONSTEXPR inline QFixed operator+(int i) const { return fromFixed((val + (i<<6))); }
87 Q_DECL_CONSTEXPR inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
88 Q_DECL_CONSTEXPR inline QFixed operator+(const QFixed &other) const { return fromFixed((val + other.val)); }
89 inline QFixed &operator+=(int i) { val += (i<<6); return *this; }
90 inline QFixed &operator+=(uint i) { val += (i<<6); return *this; }
91 inline QFixed &operator+=(const QFixed &other) { val += other.val; return *this; }
92 Q_DECL_CONSTEXPR inline QFixed operator-(int i) const { return fromFixed((val - (i<<6))); }
93 Q_DECL_CONSTEXPR inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
94 Q_DECL_CONSTEXPR inline QFixed operator-(const QFixed &other) const { return fromFixed((val - other.val)); }
95 inline QFixed &operator-=(int i) { val -= (i<<6); return *this; }
96 inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; }
97 inline QFixed &operator-=(const QFixed &other) { val -= other.val; return *this; }
98 Q_DECL_CONSTEXPR inline QFixed operator-() const { return fromFixed(-val); }
100 Q_DECL_CONSTEXPR inline bool operator==(const QFixed &other) const { return val == other.val; }
101 Q_DECL_CONSTEXPR inline bool operator!=(const QFixed &other) const { return val != other.val; }
102 Q_DECL_CONSTEXPR inline bool operator<(const QFixed &other) const { return val < other.val; }
103 Q_DECL_CONSTEXPR inline bool operator>(const QFixed &other) const { return val > other.val; }
104 Q_DECL_CONSTEXPR inline bool operator<=(const QFixed &other) const { return val <= other.val; }
105 Q_DECL_CONSTEXPR inline bool operator>=(const QFixed &other) const { return val >= other.val; }
106 Q_DECL_CONSTEXPR inline bool operator!() const { return !val; }
108 inline QFixed &operator/=(int x) { val /= x; return *this; }
109 inline QFixed &operator/=(const QFixed &o) {
116 if (a < 0) { a = -a; neg = true; }
117 if (b < 0) { b = -b; neg = !neg; }
119 int res = (int)(((a << 6) + (b >> 1)) / b);
121 val = (neg ? -res : res);
125 Q_DECL_CONSTEXPR inline QFixed operator/(int d) const { return fromFixed(val/d); }
126 inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); }
127 inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; }
128 inline QFixed &operator*=(int i) { val *= i; return *this; }
129 inline QFixed &operator*=(uint i) { val *= i; return *this; }
130 inline QFixed &operator*=(const QFixed &o) {
134 if (a < 0) { a = -a; neg = true; }
135 if (b < 0) { b = -b; neg = !neg; }
137 int res = (int)((a * b + 0x20L) >> 6);
138 val = neg ? -res : res;
141 Q_DECL_CONSTEXPR inline QFixed operator*(int i) const { return fromFixed(val * i); }
142 Q_DECL_CONSTEXPR inline QFixed operator*(uint i) const { return fromFixed(val * i); }
143 inline QFixed operator*(const QFixed &o) const { QFixed f = *this; return (f *= o); }
146 Q_DECL_CONSTEXPR QFixed(qreal i) : val((int)(i*qreal(64))) {}
147 QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; }
148 Q_DECL_CONSTEXPR inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); }
149 inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; }
150 Q_DECL_CONSTEXPR inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); }
151 inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; }
152 inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; }
153 Q_DECL_CONSTEXPR inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); }
154 inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; }
155 Q_DECL_CONSTEXPR inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
158 Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE);
160 #define QFIXED_MAX (INT_MAX/256)
162 Q_DECL_CONSTEXPR inline int qRound(const QFixed &f) { return f.toInt(); }
163 Q_DECL_CONSTEXPR inline int qFloor(const QFixed &f) { return f.floor().truncate(); }
165 Q_DECL_CONSTEXPR inline QFixed operator*(int i, const QFixed &d) { return d*i; }
166 Q_DECL_CONSTEXPR inline QFixed operator+(int i, const QFixed &d) { return d+i; }
167 Q_DECL_CONSTEXPR inline QFixed operator-(int i, const QFixed &d) { return -(d-i); }
168 Q_DECL_CONSTEXPR inline QFixed operator*(uint i, const QFixed &d) { return d*i; }
169 Q_DECL_CONSTEXPR inline QFixed operator+(uint i, const QFixed &d) { return d+i; }
170 Q_DECL_CONSTEXPR inline QFixed operator-(uint i, const QFixed &d) { return -(d-i); }
171 // Q_DECL_CONSTEXPR inline QFixed operator*(qreal d, const QFixed &d2) { return d2*d; }
173 Q_DECL_CONSTEXPR inline bool operator==(const QFixed &f, int i) { return f.value() == (i<<6); }
174 Q_DECL_CONSTEXPR inline bool operator==(int i, const QFixed &f) { return f.value() == (i<<6); }
175 Q_DECL_CONSTEXPR inline bool operator!=(const QFixed &f, int i) { return f.value() != (i<<6); }
176 Q_DECL_CONSTEXPR inline bool operator!=(int i, const QFixed &f) { return f.value() != (i<<6); }
177 Q_DECL_CONSTEXPR inline bool operator<=(const QFixed &f, int i) { return f.value() <= (i<<6); }
178 Q_DECL_CONSTEXPR inline bool operator<=(int i, const QFixed &f) { return (i<<6) <= f.value(); }
179 Q_DECL_CONSTEXPR inline bool operator>=(const QFixed &f, int i) { return f.value() >= (i<<6); }
180 Q_DECL_CONSTEXPR inline bool operator>=(int i, const QFixed &f) { return (i<<6) >= f.value(); }
181 Q_DECL_CONSTEXPR inline bool operator<(const QFixed &f, int i) { return f.value() < (i<<6); }
182 Q_DECL_CONSTEXPR inline bool operator<(int i, const QFixed &f) { return (i<<6) < f.value(); }
183 Q_DECL_CONSTEXPR inline bool operator>(const QFixed &f, int i) { return f.value() > (i<<6); }
184 Q_DECL_CONSTEXPR inline bool operator>(int i, const QFixed &f) { return (i<<6) > f.value(); }
186 #ifndef QT_NO_DEBUG_STREAM
187 inline QDebug &operator<<(QDebug &dbg, const QFixed &f)
188 { return dbg << f.toReal(); }
194 Q_DECL_CONSTEXPR inline QFixedPoint() {}
195 Q_DECL_CONSTEXPR inline QFixedPoint(const QFixed &_x, const QFixed &_y) : x(_x), y(_y) {}
196 Q_DECL_CONSTEXPR QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); }
197 Q_DECL_CONSTEXPR static QFixedPoint fromPointF(const QPointF &p) {
198 return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y()));
201 Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE);
203 Q_DECL_CONSTEXPR inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
204 { return QFixedPoint(p1.x - p2.x, p1.y - p2.y); }
205 Q_DECL_CONSTEXPR inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
206 { return QFixedPoint(p1.x + p2.x, p1.y + p2.y); }
211 Q_DECL_CONSTEXPR QFixedSize() {}
212 Q_DECL_CONSTEXPR QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
213 Q_DECL_CONSTEXPR QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
214 Q_DECL_CONSTEXPR static QFixedSize fromSizeF(const QSizeF &s) {
215 return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
218 Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE);
222 #endif // QTEXTENGINE_P_H