source: trunk/src/opengl/gl2paintengineex/qtriangulatingstroker.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 18.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtOpenGL module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtriangulatingstroker_p.h"
43#include <qmath.h>
44
45QT_BEGIN_NAMESPACE
46
47#define CURVE_FLATNESS Q_PI / 8
48
49
50
51
52void QTriangulatingStroker::endCapOrJoinClosed(const qreal *start, const qreal *cur,
53 bool implicitClose, bool endsAtStart)
54{
55 if (endsAtStart) {
56 join(start + 2);
57 } else if (implicitClose) {
58 join(start);
59 lineTo(start);
60 join(start+2);
61 } else {
62 endCap(cur);
63 }
64 int count = m_vertices.size();
65
66 // Copy the (x, y) values because QDataBuffer::add(const float& t)
67 // may resize the buffer, which will leave t pointing at the
68 // previous buffer's memory region if we don't copy first.
69 float x = m_vertices.at(count-2);
70 float y = m_vertices.at(count-1);
71 m_vertices.add(x);
72 m_vertices.add(y);
73}
74
75
76void QTriangulatingStroker::process(const QVectorPath &path, const QPen &pen)
77{
78 const qreal *pts = path.points();
79 const QPainterPath::ElementType *types = path.elements();
80 int count = path.elementCount();
81 if (count < 2)
82 return;
83
84 float realWidth = qpen_widthf(pen);
85 if (realWidth == 0)
86 realWidth = 1;
87
88 m_width = realWidth / 2;
89
90 bool cosmetic = pen.isCosmetic();
91 if (cosmetic) {
92 m_width = m_width * m_inv_scale;
93 }
94
95 m_join_style = qpen_joinStyle(pen);
96 m_cap_style = qpen_capStyle(pen);
97 m_vertices.reset();
98 m_miter_limit = pen.miterLimit() * qpen_widthf(pen);
99
100 // The curvyness is based on the notion that I originally wanted
101 // roughly one line segment pr 4 pixels. This may seem little, but
102 // because we sample at constantly incrementing B(t) E [0<t<1], we
103 // will get longer segments where the curvature is small and smaller
104 // segments when the curvature is high.
105 //
106 // To get a rough idea of the length of each curve, I pretend that
107 // the curve is a 90 degree arc, whose radius is
108 // qMax(curveBounds.width, curveBounds.height). Based on this
109 // logic we can estimate the length of the outline edges based on
110 // the radius + a pen width and adjusting for scale factors
111 // depending on if the pen is cosmetic or not.
112 //
113 // The curvyness value of PI/14 was based on,
114 // arcLength=2*PI*r/4=PI/2 and splitting length into somewhere
115 // between 3 and 8 where 5 seemed to be give pretty good results
116 // hence: Q_PI/14. Lower divisors will give more detail at the
117 // direct cost of performance.
118
119 // simplfy pens that are thin in device size (2px wide or less)
120 if (realWidth < 2.5 && (cosmetic || m_inv_scale == 1)) {
121 if (m_cap_style == Qt::RoundCap)
122 m_cap_style = Qt::SquareCap;
123 if (m_join_style == Qt::RoundJoin)
124 m_join_style = Qt::MiterJoin;
125 m_curvyness_add = 0.5;
126 m_curvyness_mul = CURVE_FLATNESS / m_inv_scale;
127 m_roundness = 1;
128 } else if (cosmetic) {
129 m_curvyness_add = realWidth / 2;
130 m_curvyness_mul = CURVE_FLATNESS;
131 m_roundness = qMax<int>(4, realWidth * CURVE_FLATNESS);
132 } else {
133 m_curvyness_add = m_width;
134 m_curvyness_mul = CURVE_FLATNESS / m_inv_scale;
135 m_roundness = qMax<int>(4, realWidth * m_curvyness_mul);
136 }
137
138 // Over this level of segmentation, there doesn't seem to be any
139 // benefit, even for huge penWidth
140 if (m_roundness > 24)
141 m_roundness = 24;
142
143 m_sin_theta = qFastSin(Q_PI / m_roundness);
144 m_cos_theta = qFastCos(Q_PI / m_roundness);
145
146 const qreal *endPts = pts + (count<<1);
147 const qreal *startPts;
148
149 Qt::PenCapStyle cap = m_cap_style;
150
151 if (!types) {
152 startPts = pts;
153
154 bool endsAtStart = startPts[0] == *(endPts-2) && startPts[1] == *(endPts-1);
155
156 if (endsAtStart || path.hasImplicitClose())
157 m_cap_style = Qt::FlatCap;
158 moveTo(pts);
159 m_cap_style = cap;
160 pts += 2;
161 lineTo(pts);
162 pts += 2;
163 while (pts < endPts) {
164 join(pts);
165 lineTo(pts);
166 pts += 2;
167 }
168
169 endCapOrJoinClosed(startPts, pts-2, path.hasImplicitClose(), endsAtStart);
170
171 } else {
172 bool endsAtStart;
173 while (pts < endPts) {
174 switch (*types) {
175 case QPainterPath::MoveToElement: {
176 if (pts != path.points())
177 endCapOrJoinClosed(startPts, pts-2, path.hasImplicitClose(), endsAtStart);
178
179 startPts = pts;
180 int end = (endPts - pts) / 2;
181 int i = 2; // Start looking to ahead since we never have two moveto's in a row
182 while (i<end && types[i] != QPainterPath::MoveToElement) {
183 ++i;
184 }
185 endsAtStart = startPts[0] == pts[i*2 - 2] && startPts[1] == pts[i*2 - 1];
186 if (endsAtStart || path.hasImplicitClose())
187 m_cap_style = Qt::FlatCap;
188
189 moveTo(pts);
190 m_cap_style = cap;
191 pts+=2;
192 ++types;
193 break; }
194 case QPainterPath::LineToElement:
195 if (*(types - 1) != QPainterPath::MoveToElement)
196 join(pts);
197 lineTo(pts);
198 pts+=2;
199 ++types;
200 break;
201 case QPainterPath::CurveToElement:
202 if (*(types - 1) != QPainterPath::MoveToElement)
203 join(pts);
204 cubicTo(pts);
205 pts+=6;
206 types+=3;
207 break;
208 default:
209 Q_ASSERT(false);
210 break;
211 }
212 }
213
214 endCapOrJoinClosed(startPts, pts-2, path.hasImplicitClose(), endsAtStart);
215 }
216}
217
218void QTriangulatingStroker::moveTo(const qreal *pts)
219{
220 m_cx = pts[0];
221 m_cy = pts[1];
222
223 float x2 = pts[2];
224 float y2 = pts[3];
225 normalVector(m_cx, m_cy, x2, y2, &m_nvx, &m_nvy);
226
227
228 // To acheive jumps we insert zero-area tringles. This is done by
229 // adding two identical points in both the end of previous strip
230 // and beginning of next strip
231 bool invisibleJump = m_vertices.size();
232
233 switch (m_cap_style) {
234 case Qt::FlatCap:
235 if (invisibleJump) {
236 m_vertices.add(m_cx + m_nvx);
237 m_vertices.add(m_cy + m_nvy);
238 }
239 break;
240 case Qt::SquareCap: {
241 float sx = m_cx - m_nvy;
242 float sy = m_cy + m_nvx;
243 if (invisibleJump) {
244 m_vertices.add(sx + m_nvx);
245 m_vertices.add(sy + m_nvy);
246 }
247 emitLineSegment(sx, sy, m_nvx, m_nvy);
248 break; }
249 case Qt::RoundCap: {
250 QVarLengthArray<float> points;
251 arcPoints(m_cx, m_cy, m_cx + m_nvx, m_cy + m_nvy, m_cx - m_nvx, m_cy - m_nvy, points);
252 m_vertices.resize(m_vertices.size() + points.size() + 2 * int(invisibleJump));
253 int count = m_vertices.size();
254 int front = 0;
255 int end = points.size() / 2;
256 while (front != end) {
257 m_vertices.at(--count) = points[2 * end - 1];
258 m_vertices.at(--count) = points[2 * end - 2];
259 --end;
260 if (front == end)
261 break;
262 m_vertices.at(--count) = points[2 * front + 1];
263 m_vertices.at(--count) = points[2 * front + 0];
264 ++front;
265 }
266
267 if (invisibleJump) {
268 m_vertices.at(count - 1) = m_vertices.at(count + 1);
269 m_vertices.at(count - 2) = m_vertices.at(count + 0);
270 }
271 break; }
272 default: break; // ssssh gcc...
273 }
274 emitLineSegment(m_cx, m_cy, m_nvx, m_nvy);
275}
276
277void QTriangulatingStroker::cubicTo(const qreal *pts)
278{
279 const QPointF *p = (const QPointF *) pts;
280 QBezier bezier = QBezier::fromPoints(*(p - 1), p[0], p[1], p[2]);
281
282 QRectF bounds = bezier.bounds();
283 float rad = qMax(bounds.width(), bounds.height());
284 int threshold = qMin<float>(64, (rad + m_curvyness_add) * m_curvyness_mul);
285 if (threshold < 4)
286 threshold = 4;
287 qreal threshold_minus_1 = threshold - 1;
288 float vx, vy;
289
290 float cx = m_cx, cy = m_cy;
291 float x, y;
292
293 for (int i=1; i<threshold; ++i) {
294 qreal t = qreal(i) / threshold_minus_1;
295 QPointF p = bezier.pointAt(t);
296 x = p.x();
297 y = p.y();
298
299 normalVector(cx, cy, x, y, &vx, &vy);
300
301 emitLineSegment(x, y, vx, vy);
302
303 cx = x;
304 cy = y;
305 }
306
307 m_cx = cx;
308 m_cy = cy;
309
310 m_nvx = vx;
311 m_nvy = vy;
312}
313
314void QTriangulatingStroker::join(const qreal *pts)
315{
316 // Creates a join to the next segment (m_cx, m_cy) -> (pts[0], pts[1])
317 normalVector(m_cx, m_cy, pts[0], pts[1], &m_nvx, &m_nvy);
318
319 switch (m_join_style) {
320 case Qt::BevelJoin:
321 break;
322 case Qt::SvgMiterJoin:
323 case Qt::MiterJoin: {
324 // Find out on which side the join should be.
325 int count = m_vertices.size();
326 float prevNvx = m_vertices.at(count - 2) - m_cx;
327 float prevNvy = m_vertices.at(count - 1) - m_cy;
328 float xprod = prevNvx * m_nvy - prevNvy * m_nvx;
329 float px, py, qx, qy;
330
331 // If the segments are parallel, use bevel join.
332 if (qFuzzyIsNull(xprod))
333 break;
334
335 // Find the corners of the previous and next segment to join.
336 if (xprod < 0) {
337 px = m_vertices.at(count - 2);
338 py = m_vertices.at(count - 1);
339 qx = m_cx - m_nvx;
340 qy = m_cy - m_nvy;
341 } else {
342 px = m_vertices.at(count - 4);
343 py = m_vertices.at(count - 3);
344 qx = m_cx + m_nvx;
345 qy = m_cy + m_nvy;
346 }
347
348 // Find intersection point.
349 float pu = px * prevNvx + py * prevNvy;
350 float qv = qx * m_nvx + qy * m_nvy;
351 float ix = (m_nvy * pu - prevNvy * qv) / xprod;
352 float iy = (prevNvx * qv - m_nvx * pu) / xprod;
353
354 // Check that the distance to the intersection point is less than the miter limit.
355 if ((ix - px) * (ix - px) + (iy - py) * (iy - py) <= m_miter_limit * m_miter_limit) {
356 m_vertices.add(ix);
357 m_vertices.add(iy);
358 m_vertices.add(ix);
359 m_vertices.add(iy);
360 }
361 // else
362 // Do a plain bevel join if the miter limit is exceeded or if
363 // the lines are parallel. This is not what the raster
364 // engine's stroker does, but it is both faster and similar to
365 // what some other graphics API's do.
366
367 break; }
368 case Qt::RoundJoin: {
369 QVarLengthArray<float> points;
370 int count = m_vertices.size();
371 float prevNvx = m_vertices.at(count - 2) - m_cx;
372 float prevNvy = m_vertices.at(count - 1) - m_cy;
373 if (m_nvx * prevNvy - m_nvy * prevNvx < 0) {
374 arcPoints(0, 0, m_nvx, m_nvy, -prevNvx, -prevNvy, points);
375 for (int i = points.size() / 2; i > 0; --i)
376 emitLineSegment(m_cx, m_cy, points[2 * i - 2], points[2 * i - 1]);
377 } else {
378 arcPoints(0, 0, -prevNvx, -prevNvy, m_nvx, m_nvy, points);
379 for (int i = 0; i < points.size() / 2; ++i)
380 emitLineSegment(m_cx, m_cy, points[2 * i + 0], points[2 * i + 1]);
381 }
382 break; }
383 default: break; // gcc warn--
384 }
385
386 emitLineSegment(m_cx, m_cy, m_nvx, m_nvy);
387}
388
389void QTriangulatingStroker::endCap(const qreal *)
390{
391 switch (m_cap_style) {
392 case Qt::FlatCap:
393 break;
394 case Qt::SquareCap:
395 emitLineSegment(m_cx + m_nvy, m_cy - m_nvx, m_nvx, m_nvy);
396 break;
397 case Qt::RoundCap: {
398 QVarLengthArray<float> points;
399 int count = m_vertices.size();
400 arcPoints(m_cx, m_cy, m_vertices.at(count - 2), m_vertices.at(count - 1), m_vertices.at(count - 4), m_vertices.at(count - 3), points);
401 int front = 0;
402 int end = points.size() / 2;
403 while (front != end) {
404 m_vertices.add(points[2 * end - 2]);
405 m_vertices.add(points[2 * end - 1]);
406 --end;
407 if (front == end)
408 break;
409 m_vertices.add(points[2 * front + 0]);
410 m_vertices.add(points[2 * front + 1]);
411 ++front;
412 }
413 break; }
414 default: break; // to shut gcc up...
415 }
416}
417
418void QTriangulatingStroker::arcPoints(float cx, float cy, float fromX, float fromY, float toX, float toY, QVarLengthArray<float> &points)
419{
420 float dx1 = fromX - cx;
421 float dy1 = fromY - cy;
422 float dx2 = toX - cx;
423 float dy2 = toY - cy;
424
425 // while more than 180 degrees left:
426 while (dx1 * dy2 - dx2 * dy1 < 0) {
427 float tmpx = dx1 * m_cos_theta - dy1 * m_sin_theta;
428 float tmpy = dx1 * m_sin_theta + dy1 * m_cos_theta;
429 dx1 = tmpx;
430 dy1 = tmpy;
431 points.append(cx + dx1);
432 points.append(cy + dy1);
433 }
434
435 // while more than 90 degrees left:
436 while (dx1 * dx2 + dy1 * dy2 < 0) {
437 float tmpx = dx1 * m_cos_theta - dy1 * m_sin_theta;
438 float tmpy = dx1 * m_sin_theta + dy1 * m_cos_theta;
439 dx1 = tmpx;
440 dy1 = tmpy;
441 points.append(cx + dx1);
442 points.append(cy + dy1);
443 }
444
445 // while more than 0 degrees left:
446 while (dx1 * dy2 - dx2 * dy1 > 0) {
447 float tmpx = dx1 * m_cos_theta - dy1 * m_sin_theta;
448 float tmpy = dx1 * m_sin_theta + dy1 * m_cos_theta;
449 dx1 = tmpx;
450 dy1 = tmpy;
451 points.append(cx + dx1);
452 points.append(cy + dy1);
453 }
454
455 // remove last point which was rotated beyond [toX, toY].
456 if (!points.isEmpty())
457 points.resize(points.size() - 2);
458}
459
460static void qdashprocessor_moveTo(qreal x, qreal y, void *data)
461{
462 ((QDashedStrokeProcessor *) data)->addElement(QPainterPath::MoveToElement, x, y);
463}
464
465static void qdashprocessor_lineTo(qreal x, qreal y, void *data)
466{
467 ((QDashedStrokeProcessor *) data)->addElement(QPainterPath::LineToElement, x, y);
468}
469
470static void qdashprocessor_cubicTo(qreal, qreal, qreal, qreal, qreal, qreal, void *)
471{
472 Q_ASSERT(0); // The dasher should not produce curves...
473}
474
475QDashedStrokeProcessor::QDashedStrokeProcessor()
476 : m_dash_stroker(0), m_inv_scale(1)
477{
478 m_dash_stroker.setMoveToHook(qdashprocessor_moveTo);
479 m_dash_stroker.setLineToHook(qdashprocessor_lineTo);
480 m_dash_stroker.setCubicToHook(qdashprocessor_cubicTo);
481}
482
483void QDashedStrokeProcessor::process(const QVectorPath &path, const QPen &pen)
484{
485
486 const qreal *pts = path.points();
487 const QPainterPath::ElementType *types = path.elements();
488 int count = path.elementCount();
489
490 m_points.reset();
491 m_types.reset();
492
493 qreal width = qpen_widthf(pen);
494 if (width == 0)
495 width = 1;
496
497 m_dash_stroker.setDashPattern(pen.dashPattern());
498 m_dash_stroker.setStrokeWidth(pen.isCosmetic() ? width * m_inv_scale : width);
499 m_dash_stroker.setMiterLimit(pen.miterLimit());
500 qreal curvyness = sqrt(width) * m_inv_scale / 8;
501
502 if (count < 2)
503 return;
504
505 const qreal *endPts = pts + (count<<1);
506
507 m_dash_stroker.begin(this);
508
509 if (!types) {
510 m_dash_stroker.moveTo(pts[0], pts[1]);
511 pts += 2;
512 while (pts < endPts) {
513 m_dash_stroker.lineTo(pts[0], pts[1]);
514 pts += 2;
515 }
516 } else {
517 while (pts < endPts) {
518 switch (*types) {
519 case QPainterPath::MoveToElement:
520 m_dash_stroker.moveTo(pts[0], pts[1]);
521 pts += 2;
522 ++types;
523 break;
524 case QPainterPath::LineToElement:
525 m_dash_stroker.lineTo(pts[0], pts[1]);
526 pts += 2;
527 ++types;
528 break;
529 case QPainterPath::CurveToElement: {
530 QBezier b = QBezier::fromPoints(*(((const QPointF *) pts) - 1),
531 *(((const QPointF *) pts)),
532 *(((const QPointF *) pts) + 1),
533 *(((const QPointF *) pts) + 2));
534 QRectF bounds = b.bounds();
535 int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * curvyness);
536 if (threshold < 4)
537 threshold = 4;
538 qreal threshold_minus_1 = threshold - 1;
539 for (int i=0; i<threshold; ++i) {
540 QPointF pt = b.pointAt(i / threshold_minus_1);
541 m_dash_stroker.lineTo(pt.x(), pt.y());
542 }
543 pts += 6;
544 types += 3;
545 break; }
546 default: break;
547 }
548 }
549 }
550
551 m_dash_stroker.end();
552}
553
554QT_END_NAMESPACE
555
Note: See TracBrowser for help on using the repository browser.