1// Copyright (C) 2020 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 QTRIANGULATINGSTROKER_P_H
5#define QTRIANGULATINGSTROKER_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 <QtCore/qmath.h>
19#include <QtGui/private/qtguiglobal_p.h>
20#include <private/qdatabuffer_p.h>
21#include <qvarlengtharray.h>
22#include <private/qvectorpath_p.h>
23#include <private/qbezier_p.h>
24#include <private/qnumeric_p.h>
25#include <private/qmath_p.h>
26
27QT_BEGIN_NAMESPACE
28
29class Q_GUI_EXPORT QTriangulatingStroker
30{
31public:
32 QTriangulatingStroker() : m_vertices(0), m_cx(0), m_cy(0), m_nvx(0), m_nvy(0), m_width(1), m_miter_limit(2),
33 m_roundness(0), m_sin_theta(0), m_cos_theta(0), m_inv_scale(1), m_curvyness_mul(1), m_curvyness_add(0),
34 m_join_style(Qt::BevelJoin), m_cap_style(Qt::SquareCap) {}
35
36 void process(const QVectorPath &path, const QPen &pen, const QRectF &clip, QPainter::RenderHints hints);
37
38 inline int vertexCount() const { return m_vertices.size(); }
39 inline const float *vertices() const { return m_vertices.data(); }
40
41 inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
42
43private:
44 inline void emitLineSegment(float x, float y, float nx, float ny);
45 void moveTo(const qreal *pts);
46 inline void lineTo(const qreal *pts);
47 void cubicTo(const qreal *pts);
48 void join(const qreal *pts);
49 inline void normalVector(float x1, float y1, float x2, float y2, float *nx, float *ny);
50 void endCap(const qreal *pts);
51 void arcPoints(float cx, float cy, float fromX, float fromY, float toX, float toY, QVarLengthArray<float> &points);
52 void endCapOrJoinClosed(const qreal *start, const qreal *cur, bool implicitClose, bool endsAtStart);
53
54
55 QDataBuffer<float> m_vertices;
56
57 float m_cx, m_cy; // current points
58 float m_nvx, m_nvy; // normal vector...
59 float m_width;
60 qreal m_miter_limit;
61
62 int m_roundness; // Number of line segments in a round join
63 qreal m_sin_theta; // sin(m_roundness / 360);
64 qreal m_cos_theta; // cos(m_roundness / 360);
65 qreal m_inv_scale;
66 float m_curvyness_mul;
67 float m_curvyness_add;
68
69 Qt::PenJoinStyle m_join_style;
70 Qt::PenCapStyle m_cap_style;
71};
72
73class Q_GUI_EXPORT QDashedStrokeProcessor
74{
75public:
76 QDashedStrokeProcessor();
77
78 void process(const QVectorPath &path, const QPen &pen, const QRectF &clip, QPainter::RenderHints hints);
79
80 inline void addElement(QPainterPath::ElementType type, qreal x, qreal y) {
81 m_points.add(t: x);
82 m_points.add(t: y);
83 m_types.add(t: type);
84 }
85
86 inline int elementCount() const { return m_types.size(); }
87 inline qreal *points() const { return m_points.data(); }
88 inline QPainterPath::ElementType *elementTypes() const { return m_types.data(); }
89
90 inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
91
92private:
93 QDataBuffer<qreal> m_points;
94 QDataBuffer<QPainterPath::ElementType> m_types;
95 QDashStroker m_dash_stroker;
96 qreal m_inv_scale;
97};
98
99inline void QTriangulatingStroker::normalVector(float x1, float y1, float x2, float y2,
100 float *nx, float *ny)
101{
102 const float dx = x2 - x1;
103 const float dy = y2 - y1;
104 const float pw = m_width / qHypot(x: dx, y: dy);
105
106 *nx = -dy * pw;
107 *ny = dx * pw;
108}
109
110inline void QTriangulatingStroker::emitLineSegment(float x, float y, float vx, float vy)
111{
112 m_vertices.add(t: x + vx);
113 m_vertices.add(t: y + vy);
114 m_vertices.add(t: x - vx);
115 m_vertices.add(t: y - vy);
116}
117
118void QTriangulatingStroker::lineTo(const qreal *pts)
119{
120 emitLineSegment(x: pts[0], y: pts[1], vx: m_nvx, vy: m_nvy);
121 m_cx = pts[0];
122 m_cy = pts[1];
123}
124
125QT_END_NAMESPACE
126
127#endif
128

source code of qtbase/src/gui/painting/qtriangulatingstroker_p.h