/src/qt/qtbase/src/gui/painting/qoutlinemapper.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "qoutlinemapper_p.h" |
41 | | |
42 | | #include "qbezier_p.h" |
43 | | #include "qmath.h" |
44 | | #include "qpainterpath_p.h" |
45 | | #include "qscopedvaluerollback.h" |
46 | | |
47 | | #include <stdlib.h> |
48 | | |
49 | | QT_BEGIN_NAMESPACE |
50 | | |
51 | 0 | #define qreal_to_fixed_26_6(f) (qRound(f * 64)) |
52 | | |
53 | | |
54 | | |
55 | | |
56 | | static const QRectF boundingRect(const QPointF *points, int pointCount) |
57 | 0 | { |
58 | 0 | const QPointF *e = points; |
59 | 0 | const QPointF *last = points + pointCount; |
60 | 0 | qreal minx, maxx, miny, maxy; |
61 | 0 | minx = maxx = e->x(); |
62 | 0 | miny = maxy = e->y(); |
63 | 0 | while (++e < last) { |
64 | 0 | if (e->x() < minx) |
65 | 0 | minx = e->x(); |
66 | 0 | else if (e->x() > maxx) |
67 | 0 | maxx = e->x(); |
68 | 0 | if (e->y() < miny) |
69 | 0 | miny = e->y(); |
70 | 0 | else if (e->y() > maxy) |
71 | 0 | maxy = e->y(); |
72 | 0 | } |
73 | 0 | return QRectF(QPointF(minx, miny), QPointF(maxx, maxy)); |
74 | 0 | } |
75 | | |
76 | 0 | void QOutlineMapper::curveTo(const QPointF &cp1, const QPointF &cp2, const QPointF &ep) { |
77 | | #ifdef QT_DEBUG_CONVERT |
78 | | printf("QOutlineMapper::curveTo() (%f, %f)\n", ep.x(), ep.y()); |
79 | | #endif |
80 | |
|
81 | 0 | QBezier bezier = QBezier::fromPoints(m_elements.last(), cp1, cp2, ep); |
82 | 0 |
|
83 | 0 | bool outsideClip = false; |
84 | 0 | // Test one point first before doing a full intersection test. |
85 | 0 | if (!QRectF(m_clip_rect).contains(m_transform.map(ep))) { |
86 | 0 | QRectF potentialCurveArea = m_transform.mapRect(bezier.bounds()); |
87 | 0 | outsideClip = !potentialCurveArea.intersects(m_clip_rect); |
88 | 0 | } |
89 | 0 | if (outsideClip) { |
90 | 0 | // The curve is entirely outside the clip rect, so just |
91 | 0 | // approximate it with a line that closes the path. |
92 | 0 | lineTo(ep); |
93 | 0 | } else { |
94 | 0 | bezier.addToPolygon(m_elements, m_curve_threshold); |
95 | 0 | m_element_types.reserve(m_elements.size()); |
96 | 0 | for (int i = m_elements.size() - m_element_types.size(); i; --i) |
97 | 0 | m_element_types << QPainterPath::LineToElement; |
98 | 0 | } |
99 | 0 | Q_ASSERT(m_elements.size() == m_element_types.size()); |
100 | 0 | } |
101 | | |
102 | | |
103 | | QT_FT_Outline *QOutlineMapper::convertPath(const QPainterPath &path) |
104 | 0 | { |
105 | 0 | Q_ASSERT(!path.isEmpty()); |
106 | 0 | int elmCount = path.elementCount(); |
107 | | #ifdef QT_DEBUG_CONVERT |
108 | | printf("QOutlineMapper::convertPath(), size=%d\n", elmCount); |
109 | | #endif |
110 | | beginOutline(path.fillRule()); |
111 | 0 |
|
112 | 0 | for (int index=0; index<elmCount; ++index) { |
113 | 0 | const QPainterPath::Element &elm = path.elementAt(index); |
114 | 0 |
|
115 | 0 | switch (elm.type) { |
116 | 0 |
|
117 | 0 | case QPainterPath::MoveToElement: |
118 | 0 | if (index == elmCount - 1) |
119 | 0 | continue; |
120 | 0 | moveTo(elm); |
121 | 0 | break; |
122 | 0 |
|
123 | 0 | case QPainterPath::LineToElement: |
124 | 0 | lineTo(elm); |
125 | 0 | break; |
126 | 0 |
|
127 | 0 | case QPainterPath::CurveToElement: |
128 | 0 | curveTo(elm, path.elementAt(index + 1), path.elementAt(index + 2)); |
129 | 0 | index += 2; |
130 | 0 | break; |
131 | 0 |
|
132 | 0 | default: |
133 | 0 | break; // This will never hit.. |
134 | 0 | } |
135 | 0 | } |
136 | 0 |
|
137 | 0 | endOutline(); |
138 | 0 | return outline(); |
139 | 0 | } |
140 | | |
141 | | QT_FT_Outline *QOutlineMapper::convertPath(const QVectorPath &path) |
142 | 0 | { |
143 | 0 | int count = path.elementCount(); |
144 | 0 |
|
145 | | #ifdef QT_DEBUG_CONVERT |
146 | | printf("QOutlineMapper::convertPath(VP), size=%d\n", count); |
147 | | #endif |
148 | 0 | beginOutline(path.hasWindingFill() ? Qt::WindingFill : Qt::OddEvenFill); |
149 | 0 |
|
150 | 0 | if (path.elements()) { |
151 | 0 | // TODO: if we do closing of subpaths in convertElements instead we |
152 | 0 | // could avoid this loop |
153 | 0 | const QPainterPath::ElementType *elements = path.elements(); |
154 | 0 | const QPointF *points = reinterpret_cast<const QPointF *>(path.points()); |
155 | 0 |
|
156 | 0 | for (int index = 0; index < count; ++index) { |
157 | 0 | switch (elements[index]) { |
158 | 0 | case QPainterPath::MoveToElement: |
159 | 0 | if (index == count - 1) |
160 | 0 | continue; |
161 | 0 | moveTo(points[index]); |
162 | 0 | break; |
163 | 0 |
|
164 | 0 | case QPainterPath::LineToElement: |
165 | 0 | lineTo(points[index]); |
166 | 0 | break; |
167 | 0 |
|
168 | 0 | case QPainterPath::CurveToElement: |
169 | 0 | curveTo(points[index], points[index+1], points[index+2]); |
170 | 0 | index += 2; |
171 | 0 | break; |
172 | 0 |
|
173 | 0 | default: |
174 | 0 | break; // This will never hit.. |
175 | 0 | } |
176 | 0 | } |
177 | 0 |
|
178 | 0 | } else { |
179 | 0 | // ### We can kill this copying and just use the buffer straight... |
180 | 0 |
|
181 | 0 | m_elements.resize(count); |
182 | 0 | if (count) |
183 | 0 | memcpy(static_cast<void *>(m_elements.data()), static_cast<const void *>(path.points()), count* sizeof(QPointF)); |
184 | 0 |
|
185 | 0 | m_element_types.resize(0); |
186 | 0 | } |
187 | 0 |
|
188 | 0 | endOutline(); |
189 | 0 | return outline(); |
190 | 0 | } |
191 | | |
192 | | |
193 | | void QOutlineMapper::endOutline() |
194 | 0 | { |
195 | 0 | closeSubpath(); |
196 | 0 |
|
197 | 0 | if (m_elements.isEmpty()) { |
198 | 0 | memset(&m_outline, 0, sizeof(m_outline)); |
199 | 0 | return; |
200 | 0 | } |
201 | 0 | |
202 | 0 | QPointF *elements = m_elements.data(); |
203 | 0 |
|
204 | 0 | // Transform the outline |
205 | 0 | if (m_transform.isIdentity()) { |
206 | 0 | // Nothing to do |
207 | 0 | } else if (m_transform.type() < QTransform::TxProject) { |
208 | 0 | for (int i = 0; i < m_elements.size(); ++i) |
209 | 0 | elements[i] = m_transform.map(elements[i]); |
210 | 0 | } else { |
211 | 0 | const QVectorPath vp((qreal *)elements, m_elements.size(), |
212 | 0 | m_element_types.size() ? m_element_types.data() : nullptr); |
213 | 0 | QPainterPath path = vp.convertToPainterPath(); |
214 | 0 | path = m_transform.map(path); |
215 | 0 | if (!(m_outline.flags & QT_FT_OUTLINE_EVEN_ODD_FILL)) |
216 | 0 | path.setFillRule(Qt::WindingFill); |
217 | 0 | if (path.isEmpty()) { |
218 | 0 | m_valid = false; |
219 | 0 | } else { |
220 | 0 | QTransform oldTransform = m_transform; |
221 | 0 | m_transform.reset(); |
222 | 0 | convertPath(path); |
223 | 0 | m_transform = oldTransform; |
224 | 0 | } |
225 | 0 | return; |
226 | 0 | } |
227 | 0 |
|
228 | 0 | controlPointRect = boundingRect(elements, m_elements.size()); |
229 | 0 |
|
230 | | #ifdef QT_DEBUG_CONVERT |
231 | | printf(" - control point rect (%.2f, %.2f) %.2f x %.2f, clip=(%d,%d, %dx%d)\n", |
232 | | controlPointRect.x(), controlPointRect.y(), |
233 | | controlPointRect.width(), controlPointRect.height(), |
234 | | m_clip_rect.x(), m_clip_rect.y(), m_clip_rect.width(), m_clip_rect.height()); |
235 | | #endif |
236 | |
|
237 | 0 |
|
238 | 0 | // Check for out of dev bounds... |
239 | 0 | const bool do_clip = !m_in_clip_elements && ((controlPointRect.left() < -QT_RASTER_COORD_LIMIT |
240 | 0 | || controlPointRect.right() > QT_RASTER_COORD_LIMIT |
241 | 0 | || controlPointRect.top() < -QT_RASTER_COORD_LIMIT |
242 | 0 | || controlPointRect.bottom() > QT_RASTER_COORD_LIMIT |
243 | 0 | || controlPointRect.width() > QT_RASTER_COORD_LIMIT |
244 | 0 | || controlPointRect.height() > QT_RASTER_COORD_LIMIT)); |
245 | 0 |
|
246 | 0 | if (do_clip) { |
247 | 0 | clipElements(elements, elementTypes(), m_elements.size()); |
248 | 0 | } else { |
249 | 0 | convertElements(elements, elementTypes(), m_elements.size()); |
250 | 0 | } |
251 | 0 | } |
252 | | |
253 | | void QOutlineMapper::convertElements(const QPointF *elements, |
254 | | const QPainterPath::ElementType *types, |
255 | | int element_count) |
256 | 0 | { |
257 | 0 |
|
258 | 0 | if (types) { |
259 | 0 | // Translate into FT coords |
260 | 0 | const QPointF *e = elements; |
261 | 0 | for (int i=0; i<element_count; ++i) { |
262 | 0 | switch (*types) { |
263 | 0 | case QPainterPath::MoveToElement: |
264 | 0 | { |
265 | 0 | QT_FT_Vector pt_fixed = { qreal_to_fixed_26_6(e->x()), |
266 | 0 | qreal_to_fixed_26_6(e->y()) }; |
267 | 0 | if (i != 0) |
268 | 0 | m_contours << m_points.size() - 1; |
269 | 0 | m_points << pt_fixed; |
270 | 0 | m_tags << QT_FT_CURVE_TAG_ON; |
271 | 0 | } |
272 | 0 | break; |
273 | 0 |
|
274 | 0 | case QPainterPath::LineToElement: |
275 | 0 | { |
276 | 0 | QT_FT_Vector pt_fixed = { qreal_to_fixed_26_6(e->x()), |
277 | 0 | qreal_to_fixed_26_6(e->y()) }; |
278 | 0 | m_points << pt_fixed; |
279 | 0 | m_tags << QT_FT_CURVE_TAG_ON; |
280 | 0 | } |
281 | 0 | break; |
282 | 0 |
|
283 | 0 | case QPainterPath::CurveToElement: |
284 | 0 | { |
285 | 0 | QT_FT_Vector cp1_fixed = { qreal_to_fixed_26_6(e->x()), |
286 | 0 | qreal_to_fixed_26_6(e->y()) }; |
287 | 0 | ++e; |
288 | 0 | QT_FT_Vector cp2_fixed = { qreal_to_fixed_26_6((e)->x()), |
289 | 0 | qreal_to_fixed_26_6((e)->y()) }; |
290 | 0 | ++e; |
291 | 0 | QT_FT_Vector ep_fixed = { qreal_to_fixed_26_6((e)->x()), |
292 | 0 | qreal_to_fixed_26_6((e)->y()) }; |
293 | 0 |
|
294 | 0 | m_points << cp1_fixed << cp2_fixed << ep_fixed; |
295 | 0 | m_tags << QT_FT_CURVE_TAG_CUBIC |
296 | 0 | << QT_FT_CURVE_TAG_CUBIC |
297 | 0 | << QT_FT_CURVE_TAG_ON; |
298 | 0 |
|
299 | 0 | types += 2; |
300 | 0 | i += 2; |
301 | 0 | } |
302 | 0 | break; |
303 | 0 | default: |
304 | 0 | break; |
305 | 0 | } |
306 | 0 | ++types; |
307 | 0 | ++e; |
308 | 0 | } |
309 | 0 | } else { |
310 | 0 | // Plain polygon... |
311 | 0 | const QPointF *last = elements + element_count; |
312 | 0 | const QPointF *e = elements; |
313 | 0 | while (e < last) { |
314 | 0 | QT_FT_Vector pt_fixed = { qreal_to_fixed_26_6(e->x()), |
315 | 0 | qreal_to_fixed_26_6(e->y()) }; |
316 | 0 | m_points << pt_fixed; |
317 | 0 | m_tags << QT_FT_CURVE_TAG_ON; |
318 | 0 | ++e; |
319 | 0 | } |
320 | 0 | } |
321 | 0 |
|
322 | 0 | // close the very last subpath |
323 | 0 | m_contours << m_points.size() - 1; |
324 | 0 |
|
325 | 0 | m_outline.n_contours = m_contours.size(); |
326 | 0 | m_outline.n_points = m_points.size(); |
327 | 0 |
|
328 | 0 | m_outline.points = m_points.data(); |
329 | 0 | m_outline.tags = m_tags.data(); |
330 | 0 | m_outline.contours = m_contours.data(); |
331 | 0 |
|
332 | | #ifdef QT_DEBUG_CONVERT |
333 | | printf("QOutlineMapper::endOutline\n"); |
334 | | |
335 | | printf(" - contours: %d\n", m_outline.n_contours); |
336 | | for (int i=0; i<m_outline.n_contours; ++i) { |
337 | | printf(" - %d\n", m_outline.contours[i]); |
338 | | } |
339 | | |
340 | | printf(" - points: %d\n", m_outline.n_points); |
341 | | for (int i=0; i<m_outline.n_points; ++i) { |
342 | | printf(" - %d -- %.2f, %.2f, (%d, %d)\n", i, |
343 | | (double) (m_outline.points[i].x / 64.0), |
344 | | (double) (m_outline.points[i].y / 64.0), |
345 | | (int) m_outline.points[i].x, (int) m_outline.points[i].y); |
346 | | } |
347 | | #endif |
348 | | } |
349 | | |
350 | | void QOutlineMapper::clipElements(const QPointF *elements, |
351 | | const QPainterPath::ElementType *types, |
352 | | int element_count) |
353 | 0 | { |
354 | 0 | // We could save a bit of time by actually implementing them fully |
355 | 0 | // instead of going through convenience functionallity, but since |
356 | 0 | // this part of code hardly every used, it shouldn't matter. |
357 | 0 |
|
358 | 0 | QScopedValueRollback<bool> in_clip_elements(m_in_clip_elements, true); |
359 | 0 |
|
360 | 0 | QPainterPath path; |
361 | 0 |
|
362 | 0 | if (!(m_outline.flags & QT_FT_OUTLINE_EVEN_ODD_FILL)) |
363 | 0 | path.setFillRule(Qt::WindingFill); |
364 | 0 |
|
365 | 0 | if (types) { |
366 | 0 | for (int i=0; i<element_count; ++i) { |
367 | 0 | switch (types[i]) { |
368 | 0 | case QPainterPath::MoveToElement: |
369 | 0 | path.moveTo(elements[i]); |
370 | 0 | break; |
371 | 0 |
|
372 | 0 | case QPainterPath::LineToElement: |
373 | 0 | path.lineTo(elements[i]); |
374 | 0 | break; |
375 | 0 |
|
376 | 0 | case QPainterPath::CurveToElement: |
377 | 0 | path.cubicTo(elements[i], elements[i+1], elements[i+2]); |
378 | 0 | i += 2; |
379 | 0 | break; |
380 | 0 | default: |
381 | 0 | break; |
382 | 0 | } |
383 | 0 | } |
384 | 0 | } else { |
385 | 0 | path.moveTo(elements[0]); |
386 | 0 | for (int i=1; i<element_count; ++i) |
387 | 0 | path.lineTo(elements[i]); |
388 | 0 | } |
389 | 0 |
|
390 | 0 | QPainterPath clipPath; |
391 | 0 | clipPath.addRect(m_clip_rect); |
392 | 0 | QPainterPath clippedPath = path.intersected(clipPath); |
393 | 0 | if (clippedPath.isEmpty()) { |
394 | 0 | m_valid = false; |
395 | 0 | } else { |
396 | 0 | QTransform oldTransform = m_transform; |
397 | 0 | m_transform.reset(); |
398 | 0 | convertPath(clippedPath); |
399 | 0 | m_transform = oldTransform; |
400 | 0 | } |
401 | 0 | } |
402 | | |
403 | | QT_END_NAMESPACE |