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