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
59QT_BEGIN_NAMESPACE
60
61struct QFixed {
62private:
63 Q_DECL_CONSTEXPR QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
64public:
65 Q_DECL_CONSTEXPR QFixed() : val(0) {}
66 Q_DECL_CONSTEXPR QFixed(int i) : val(i * 64) {}
67 Q_DECL_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 Q_DECL_CONSTEXPR static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
72 Q_DECL_CONSTEXPR static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
73
74 Q_DECL_CONSTEXPR inline int value() const { return val; }
75 inline void setValue(int value) { val = value; }
76
77 Q_DECL_CONSTEXPR inline int toInt() const { return (((val)+32) & -64)>>6; }
78 Q_DECL_CONSTEXPR inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
79
80 Q_DECL_CONSTEXPR inline int truncate() const { return val>>6; }
81 Q_DECL_CONSTEXPR inline QFixed round() const { return fromFixed(((val)+32) & -64); }
82 Q_DECL_CONSTEXPR inline QFixed floor() const { return fromFixed((val) & -64); }
83 Q_DECL_CONSTEXPR inline QFixed ceil() const { return fromFixed((val+63) & -64); }
84
85 Q_DECL_CONSTEXPR inline QFixed operator+(int i) const { return fromFixed(val + i * 64); }
86 Q_DECL_CONSTEXPR inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
87 Q_DECL_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 Q_DECL_CONSTEXPR inline QFixed operator-(int i) const { return fromFixed(val - i * 64); }
92 Q_DECL_CONSTEXPR inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
93 Q_DECL_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 Q_DECL_CONSTEXPR inline QFixed operator-() const { return fromFixed(-val); }
98
99 Q_DECL_CONSTEXPR inline bool operator==(const QFixed &other) const { return val == other.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 { 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 Q_DECL_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 Q_DECL_CONSTEXPR inline QFixed operator*(int i) const { return fromFixed(val * i); }
141 Q_DECL_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
144private:
145 Q_DECL_CONSTEXPR QFixed(qreal i) : val((int)(i*qreal(64))) {}
146 QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; }
147 Q_DECL_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 Q_DECL_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 Q_DECL_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 Q_DECL_CONSTEXPR inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
155 int val;
156};
157Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE);
158
159#define QFIXED_MAX (INT_MAX/256)
160
161Q_DECL_CONSTEXPR inline int qRound(const QFixed &f) { return f.toInt(); }
162Q_DECL_CONSTEXPR inline int qFloor(const QFixed &f) { return f.floor().truncate(); }
163
164Q_DECL_CONSTEXPR inline QFixed operator*(int i, const QFixed &d) { return d*i; }
165Q_DECL_CONSTEXPR inline QFixed operator+(int i, const QFixed &d) { return d+i; }
166Q_DECL_CONSTEXPR inline QFixed operator-(int i, const QFixed &d) { return -(d-i); }
167Q_DECL_CONSTEXPR inline QFixed operator*(uint i, const QFixed &d) { return d*i; }
168Q_DECL_CONSTEXPR inline QFixed operator+(uint i, const QFixed &d) { return d+i; }
169Q_DECL_CONSTEXPR inline QFixed operator-(uint i, const QFixed &d) { return -(d-i); }
170// Q_DECL_CONSTEXPR inline QFixed operator*(qreal d, const QFixed &d2) { return d2*d; }
171
172Q_DECL_CONSTEXPR inline bool operator==(const QFixed &f, int i) { return f.value() == i * 64; }
173Q_DECL_CONSTEXPR inline bool operator==(int i, const QFixed &f) { return f.value() == i * 64; }
174Q_DECL_CONSTEXPR inline bool operator!=(const QFixed &f, int i) { return f.value() != i * 64; }
175Q_DECL_CONSTEXPR inline bool operator!=(int i, const QFixed &f) { return f.value() != i * 64; }
176Q_DECL_CONSTEXPR inline bool operator<=(const QFixed &f, int i) { return f.value() <= i * 64; }
177Q_DECL_CONSTEXPR inline bool operator<=(int i, const QFixed &f) { return i * 64 <= f.value(); }
178Q_DECL_CONSTEXPR inline bool operator>=(const QFixed &f, int i) { return f.value() >= i * 64; }
179Q_DECL_CONSTEXPR inline bool operator>=(int i, const QFixed &f) { return i * 64 >= f.value(); }
180Q_DECL_CONSTEXPR inline bool operator<(const QFixed &f, int i) { return f.value() < i * 64; }
181Q_DECL_CONSTEXPR inline bool operator<(int i, const QFixed &f) { return i * 64 < f.value(); }
182Q_DECL_CONSTEXPR inline bool operator>(const QFixed &f, int i) { return f.value() > i * 64; }
183Q_DECL_CONSTEXPR inline bool operator>(int i, const QFixed &f) { return i * 64 > f.value(); }
184
185#ifndef QT_NO_DEBUG_STREAM
186inline QDebug &operator<<(QDebug &dbg, const QFixed &f)
187{ return dbg << f.toReal(); }
188#endif
189
190struct QFixedPoint {
191 QFixed x;
192 QFixed y;
193 Q_DECL_CONSTEXPR inline QFixedPoint() {}
194 Q_DECL_CONSTEXPR inline QFixedPoint(const QFixed &_x, const QFixed &_y) : x(_x), y(_y) {}
195 Q_DECL_CONSTEXPR QPointF toPointF() const { return QPointF(x.toReal(), y.toReal()); }
196 Q_DECL_CONSTEXPR static QFixedPoint fromPointF(const QPointF &p) {
197 return QFixedPoint(QFixed::fromReal(p.x()), QFixed::fromReal(p.y()));
198 }
199};
200Q_DECLARE_TYPEINFO(QFixedPoint, Q_PRIMITIVE_TYPE);
201
202Q_DECL_CONSTEXPR inline QFixedPoint operator-(const QFixedPoint &p1, const QFixedPoint &p2)
203{ return QFixedPoint(p1.x - p2.x, p1.y - p2.y); }
204Q_DECL_CONSTEXPR inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
205{ return QFixedPoint(p1.x + p2.x, p1.y + p2.y); }
206
207struct QFixedSize {
208 QFixed width;
209 QFixed height;
210 Q_DECL_CONSTEXPR QFixedSize() {}
211 Q_DECL_CONSTEXPR QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
212 Q_DECL_CONSTEXPR QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
213 Q_DECL_CONSTEXPR static QFixedSize fromSizeF(const QSizeF &s) {
214 return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
215 }
216};
217Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE);
218
219QT_END_NAMESPACE
220
221#endif // QTEXTENGINE_P_H
222