1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QFIXED_P_H |
41 | #define QFIXED_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of other Qt classes. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtGui/private/qtguiglobal_p.h> |
55 | #include "QtCore/qdebug.h" |
56 | #include "QtCore/qpoint.h" |
57 | #include "QtCore/qsize.h" |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | struct QFixed { |
62 | private: |
63 | constexpr QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation |
64 | public: |
65 | constexpr QFixed() : val(0) {} |
66 | constexpr QFixed(int i) : val(i * 64) {} |
67 | constexpr QFixed(long i) : val(i * 64) {} |
68 | QFixed &operator=(int i) { val = i * 64; return *this; } |
69 | QFixed &operator=(long i) { val = i * 64; return *this; } |
70 | |
71 | constexpr static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); } |
72 | constexpr static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor |
73 | |
74 | constexpr inline int value() const { return val; } |
75 | inline void setValue(int value) { val = value; } |
76 | |
77 | constexpr inline int toInt() const { return (((val)+32) & -64)>>6; } |
78 | constexpr inline qreal toReal() const { return ((qreal)val)/(qreal)64; } |
79 | |
80 | constexpr inline int truncate() const { return val>>6; } |
81 | constexpr inline QFixed round() const { return fromFixed(((val)+32) & -64); } |
82 | constexpr inline QFixed floor() const { return fromFixed((val) & -64); } |
83 | constexpr inline QFixed ceil() const { return fromFixed((val+63) & -64); } |
84 | |
85 | constexpr inline QFixed operator+(int i) const { return fromFixed(val + i * 64); } |
86 | constexpr inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); } |
87 | constexpr inline QFixed operator+(const QFixed &other) const { return fromFixed((val + other.val)); } |
88 | inline QFixed &operator+=(int i) { val += i * 64; return *this; } |
89 | inline QFixed &operator+=(uint i) { val += (i<<6); return *this; } |
90 | inline QFixed &operator+=(const QFixed &other) { val += other.val; return *this; } |
91 | constexpr inline QFixed operator-(int i) const { return fromFixed(val - i * 64); } |
92 | constexpr inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); } |
93 | constexpr inline QFixed operator-(const QFixed &other) const { return fromFixed((val - other.val)); } |
94 | inline QFixed &operator-=(int i) { val -= i * 64; return *this; } |
95 | inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; } |
96 | inline QFixed &operator-=(const QFixed &other) { val -= other.val; return *this; } |
97 | constexpr inline QFixed operator-() const { return fromFixed(-val); } |
98 | |
99 | constexpr inline bool operator==(const QFixed &other) const { return val == other.val; } |
100 | constexpr inline bool operator!=(const QFixed &other) const { return val != other.val; } |
101 | constexpr inline bool operator<(const QFixed &other) const { return val < other.val; } |
102 | constexpr inline bool operator>(const QFixed &other) const { return val > other.val; } |
103 | constexpr inline bool operator<=(const QFixed &other) const { return val <= other.val; } |
104 | constexpr inline bool operator>=(const QFixed &other) const { return val >= other.val; } |
105 | constexpr inline bool operator!() const { return !val; } |
106 | |
107 | inline QFixed &operator/=(int x) { val /= x; return *this; } |
108 | inline QFixed &operator/=(const QFixed &o) { |
109 | if (o.val == 0) { |
110 | val = 0x7FFFFFFFL; |
111 | } else { |
112 | bool neg = false; |
113 | qint64 a = val; |
114 | qint64 b = o.val; |
115 | if (a < 0) { a = -a; neg = true; } |
116 | if (b < 0) { b = -b; neg = !neg; } |
117 | |
118 | int res = (int)(((a << 6) + (b >> 1)) / b); |
119 | |
120 | val = (neg ? -res : res); |
121 | } |
122 | return *this; |
123 | } |
124 | constexpr inline QFixed operator/(int d) const { return fromFixed(val/d); } |
125 | inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); } |
126 | inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; } |
127 | inline QFixed &operator*=(int i) { val *= i; return *this; } |
128 | inline QFixed &operator*=(uint i) { val *= i; return *this; } |
129 | inline QFixed &operator*=(const QFixed &o) { |
130 | bool neg = false; |
131 | qint64 a = val; |
132 | qint64 b = o.val; |
133 | if (a < 0) { a = -a; neg = true; } |
134 | if (b < 0) { b = -b; neg = !neg; } |
135 | |
136 | int res = (int)((a * b + 0x20L) >> 6); |
137 | val = neg ? -res : res; |
138 | return *this; |
139 | } |
140 | constexpr inline QFixed operator*(int i) const { return fromFixed(val * i); } |
141 | constexpr inline QFixed operator*(uint i) const { return fromFixed(val * i); } |
142 | inline QFixed operator*(const QFixed &o) const { QFixed f = *this; return (f *= o); } |
143 | |
144 | private: |
145 | constexpr QFixed(qreal i) : val((int)(i*qreal(64))) {} |
146 | QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; } |
147 | constexpr inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); } |
148 | inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; } |
149 | constexpr inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); } |
150 | inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; } |
151 | inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; } |
152 | constexpr inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); } |
153 | inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; } |
154 | constexpr inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); } |
155 | int val; |
156 | }; |
157 | Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE); |
158 | |
159 | #define QFIXED_MAX (INT_MAX/256) |
160 | |
161 | constexpr inline int qRound(const QFixed &f) { return f.toInt(); } |
162 | constexpr inline int qFloor(const QFixed &f) { return f.floor().truncate(); } |
163 | |
164 | constexpr inline QFixed operator*(int i, const QFixed &d) { return d*i; } |
165 | constexpr inline QFixed operator+(int i, const QFixed &d) { return d+i; } |
166 | constexpr inline QFixed operator-(int i, const QFixed &d) { return -(d-i); } |
167 | constexpr inline QFixed operator*(uint i, const QFixed &d) { return d*i; } |
168 | constexpr inline QFixed operator+(uint i, const QFixed &d) { return d+i; } |
169 | constexpr inline QFixed operator-(uint i, const QFixed &d) { return -(d-i); } |
170 | // constexpr inline QFixed operator*(qreal d, const QFixed &d2) { return d2*d; } |
171 | |
172 | constexpr inline bool operator==(const QFixed &f, int i) { return f.value() == i * 64; } |
173 | constexpr inline bool operator==(int i, const QFixed &f) { return f.value() == i * 64; } |
174 | constexpr inline bool operator!=(const QFixed &f, int i) { return f.value() != i * 64; } |
175 | constexpr inline bool operator!=(int i, const QFixed &f) { return f.value() != i * 64; } |
176 | constexpr inline bool operator<=(const QFixed &f, int i) { return f.value() <= i * 64; } |
177 | constexpr inline bool operator<=(int i, const QFixed &f) { return i * 64 <= f.value(); } |
178 | constexpr inline bool operator>=(const QFixed &f, int i) { return f.value() >= i * 64; } |
179 | constexpr inline bool operator>=(int i, const QFixed &f) { return i * 64 >= f.value(); } |
180 | constexpr inline bool operator<(const QFixed &f, int i) { return f.value() < i * 64; } |
181 | constexpr inline bool operator<(int i, const QFixed &f) { return i * 64 < f.value(); } |
182 | constexpr inline bool operator>(const QFixed &f, int i) { return f.value() > i * 64; } |
183 | constexpr inline bool operator>(int i, const QFixed &f) { return i * 64 > f.value(); } |
184 | |
185 | #ifndef QT_NO_DEBUG_STREAM |
186 | inline QDebug &operator<<(QDebug &dbg, const QFixed &f) |
187 | { return dbg << f.toReal(); } |
188 | #endif |
189 | |
190 | struct QFixedPoint { |
191 | QFixed x; |
192 | QFixed y; |
193 | constexpr inline QFixedPoint() {} |
194 | constexpr inline QFixedPoint(const QFixed &_x, const QFixed &_y) : x(_x), y(_y) {} |
195 | constexpr QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); } |
196 | constexpr static QFixedPoint fromPointF(const QPointF &p) { |
197 | return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y())); |
198 | } |
199 | }; |
200 | Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE); |
201 | |
202 | constexpr inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2) |
203 | { return QFixedPoint(p1.x - p2.x, p1.y - p2.y); } |
204 | constexpr inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2) |
205 | { return QFixedPoint(p1.x + p2.x, p1.y + p2.y); } |
206 | |
207 | struct QFixedSize { |
208 | QFixed width; |
209 | QFixed height; |
210 | constexpr QFixedSize() {} |
211 | constexpr QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {} |
212 | constexpr QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); } |
213 | constexpr static QFixedSize fromSizeF(const QSizeF &s) { |
214 | return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height())); |
215 | } |
216 | }; |
217 | Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE); |
218 | |
219 | QT_END_NAMESPACE |
220 | |
221 | #endif // QTEXTENGINE_P_H |
222 | |