1 | // Copyright (C) 2023 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSVGHELPER_P_H |
5 | #define QSVGHELPER_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include "qtsvgglobal_p.h" |
19 | |
20 | #include <QRectF> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | class Q_SVG_EXPORT QSvgRectF : public QRectF |
25 | { |
26 | public: |
27 | QSvgRectF(const QRectF &r = QRectF(), |
28 | QtSvg::UnitTypes unitX = QtSvg::UnitTypes::userSpaceOnUse, |
29 | QtSvg::UnitTypes unitY = QtSvg::UnitTypes::userSpaceOnUse, |
30 | QtSvg::UnitTypes unitW = QtSvg::UnitTypes::userSpaceOnUse, |
31 | QtSvg::UnitTypes unitH = QtSvg::UnitTypes::userSpaceOnUse) |
32 | : QRectF(r) |
33 | , m_unitX(unitX) |
34 | , m_unitY(unitY) |
35 | , m_unitW(unitW) |
36 | , m_unitH(unitH) |
37 | {} |
38 | |
39 | QPointF translationRelativeToBoundingBox(const QRectF &boundingBox) const { |
40 | QPointF result; |
41 | |
42 | if (m_unitX == QtSvg::UnitTypes::objectBoundingBox) |
43 | result.setX(x() * boundingBox.width()); |
44 | else |
45 | result.setX(x()); |
46 | if (m_unitY == QtSvg::UnitTypes::objectBoundingBox) |
47 | result.setY(y() * boundingBox.height()); |
48 | else |
49 | result.setY(y()); |
50 | return result; |
51 | } |
52 | |
53 | QRectF resolveRelativeLengths(const QRectF &localRect) const { |
54 | QRectF result; |
55 | if (m_unitX == QtSvg::UnitTypes::objectBoundingBox) |
56 | result.setX(localRect.x() + x() * localRect.width()); |
57 | else |
58 | result.setX(x()); |
59 | if (m_unitY == QtSvg::UnitTypes::objectBoundingBox) |
60 | result.setY(localRect.y() + y() * localRect.height()); |
61 | else |
62 | result.setY(y()); |
63 | if (m_unitW == QtSvg::UnitTypes::objectBoundingBox) |
64 | result.setWidth(localRect.width() * width()); |
65 | else |
66 | result.setWidth(width()); |
67 | if (m_unitH == QtSvg::UnitTypes::objectBoundingBox) |
68 | result.setHeight(localRect.height() * height()); |
69 | else |
70 | result.setHeight(height()); |
71 | return result; |
72 | } |
73 | |
74 | QRectF resolveRelativeLengths(const QRectF &localRect, QtSvg::UnitTypes units) const { |
75 | QRectF result; |
76 | if (units == QtSvg::UnitTypes::objectBoundingBox || |
77 | m_unitX == QtSvg::UnitTypes::objectBoundingBox) |
78 | result.setX(localRect.x() + x() * localRect.width()); |
79 | else |
80 | result.setX(x()); |
81 | if (units == QtSvg::UnitTypes::objectBoundingBox || |
82 | m_unitY == QtSvg::UnitTypes::objectBoundingBox) |
83 | result.setY(localRect.y() + y() * localRect.height()); |
84 | else |
85 | result.setY(y()); |
86 | if (units == QtSvg::UnitTypes::objectBoundingBox || |
87 | m_unitW == QtSvg::UnitTypes::objectBoundingBox) |
88 | result.setWidth(localRect.width() * width()); |
89 | else |
90 | result.setWidth(width()); |
91 | if (units == QtSvg::UnitTypes::objectBoundingBox || |
92 | m_unitH == QtSvg::UnitTypes::objectBoundingBox) |
93 | result.setHeight(localRect.height() * height()); |
94 | else |
95 | result.setHeight(height()); |
96 | |
97 | return result; |
98 | } |
99 | |
100 | QtSvg::UnitTypes unitX() const {return m_unitX;} |
101 | QtSvg::UnitTypes unitY() const {return m_unitY;} |
102 | QtSvg::UnitTypes unitW() const {return m_unitW;} |
103 | QtSvg::UnitTypes unitH() const {return m_unitH;} |
104 | |
105 | void setUnitX(QtSvg::UnitTypes unit) {m_unitX = unit;} |
106 | void setUnitY(QtSvg::UnitTypes unit) {m_unitY = unit;} |
107 | void setUnitW(QtSvg::UnitTypes unit) {m_unitW = unit;} |
108 | void setUnitH(QtSvg::UnitTypes unit) {m_unitH = unit;} |
109 | |
110 | |
111 | #ifndef QT_NO_DEBUG_STREAM |
112 | friend QDebug operator<<(QDebug debug, const QSvgRectF &r) |
113 | { |
114 | debug.space(); |
115 | debug.nospace() << "QSvgRectF(" << r.x() |
116 | << (r.unitX() == QtSvg::UnitTypes::unknown ? "(?)" : |
117 | (r.unitX() == QtSvg::UnitTypes::objectBoundingBox ? "(%)" : "" )) |
118 | << "," << r.y() |
119 | << (r.unitY() == QtSvg::UnitTypes::unknown ? "(?)" : |
120 | (r.unitY() == QtSvg::UnitTypes::objectBoundingBox ? "(%)" : "" )) |
121 | << " " << r.width() |
122 | << (r.unitW() == QtSvg::UnitTypes::unknown ? "(?)" : |
123 | (r.unitW() == QtSvg::UnitTypes::objectBoundingBox ? "(%)" : "" )) |
124 | << "x" << r.height() |
125 | << (r.unitH() == QtSvg::UnitTypes::unknown ? "(?)" : |
126 | (r.unitH() == QtSvg::UnitTypes::objectBoundingBox ? "(%)" : "" )) |
127 | << ")" ; |
128 | return debug; |
129 | } |
130 | #endif |
131 | |
132 | protected: |
133 | QtSvg::UnitTypes m_unitX, |
134 | m_unitY, |
135 | m_unitW, |
136 | m_unitH; |
137 | }; |
138 | |
139 | QT_END_NAMESPACE |
140 | |
141 | #endif // QSVGHELPER_P_H |
142 | |