1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qcolrpaintgraphrenderer_p.h"
QT_BEGIN_NAMESPACE
QColrPaintGraphRenderer::~QColrPaintGraphRenderer()
{
Q_ASSERT(m_oldPaths.isEmpty());
Q_ASSERT(m_oldTransforms.isEmpty());
delete m_painter;
}
void QColrPaintGraphRenderer::save()
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[enter scope]";
m_oldPaths.append(m_currentPath);
m_oldTransforms.append(m_currentTransform);
if (m_painter != nullptr)
m_painter->save();
}
void QColrPaintGraphRenderer::restore()
{
m_currentPath = m_oldPaths.takeLast();
m_currentTransform = m_oldTransforms.takeLast();
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[exit scope]";
if (m_painter != nullptr)
m_painter->restore();
}
void QColrPaintGraphRenderer::setClip(QRect rect)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[set clip: " << rect
<< "]";
if (!isRendering())
m_boundingRect = m_boundingRect.united(rect);
if (m_painter != nullptr)
m_painter->setClipRect(rect);
}
void QColrPaintGraphRenderer::prependTransform(const QTransform &transform)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[prepend transform: " << transform
<< "]";
m_currentTransform = transform * m_currentTransform;
}
void QColrPaintGraphRenderer::appendPath(const QPainterPath &path)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[append path: " << path.controlPointRect()
<< "]";
QPainterPath transformedPath(m_currentTransform.map(path));
m_currentPath = m_currentPath.united(transformedPath);
if (!isRendering())
m_boundingRect = m_boundingRect.united(transformedPath.boundingRect());
}
void QColrPaintGraphRenderer::setPath(const QPainterPath &path)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[set path]";
m_currentPath.clear();
appendPath(path);
}
void QColrPaintGraphRenderer::setSolidColor(QColor color)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[solid " << color
<< "]";
if (m_painter != nullptr)
m_painter->setBrush(color);
}
void QColrPaintGraphRenderer::setLinearGradient(QPointF p0, QPointF p1, QPointF p2,
QGradient::Spread spread,
const QGradientStops &gradientStops)
{
if (m_painter != nullptr) {
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[linear gradient " << p0 << ", " << p1 << ", " << p2
<< ", spread: " << spread
<< ", stop count: " << gradientStops.size()
<< "]";
// Calculate new start and end point for single vector gradient preferred by Qt
// Find vector perpendicular to p0p2 and project p0p1 onto this to find p3 (final
// stop)
// https://learn.microsoft.com/en-us/typography/opentype/spec/colr#linear-gradients
QVector2D p0p2(p2 - p0);
if (qFuzzyIsNull(p0p2.lengthSquared())) {
qCWarning(lcColrv1) << "Malformed linear gradient in COLRv1 graph. Points:"
<< p0
<< p1
<< p2;
return;
}
// v is perpendicular to p0p2
QVector2D v(p0p2.y(), -p0p2.x());
// u is the vector from p0 to p1
QVector2D u(p1 - p0);
if (qFuzzyIsNull(u.lengthSquared())) {
qCWarning(lcColrv1) << "Malformed linear gradient in COLRv1 graph. Points:"
<< p0
<< p1
<< p2;
return;
}
// We find the projected point p3
QPointF p3((QVector2D(p0) + v * QVector2D::dotProduct(u, v) / v.lengthSquared()).toPointF());
p0 = m_currentTransform.map(p0);
p3 = m_currentTransform.map(p1);
QLinearGradient linearGradient(p0, p3);
linearGradient.setSpread(spread);
linearGradient.setStops(gradientStops);
linearGradient.setCoordinateMode(QGradient::LogicalMode);
m_painter->setBrush(linearGradient);
}
}
void QColrPaintGraphRenderer::setConicalGradient(QPointF center,
qreal startAngle, qreal endAngle,
QGradient::Spread spread,
const QGradientStops &gradientStops)
{
if (m_painter != nullptr) {
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[conical gradient " << center
<< ", startAngle=" << startAngle
<< ", endAngle=" << endAngle
<< ", spread: " << spread
<< ", stop count: " << gradientStops.size()
<< "]";
QConicalGradient conicalGradient(center, startAngle);
conicalGradient.setSpread(spread);
// Adapt stops to actual span since Qt always assumes end angle of 360
// Note: This does not give accurate results for the colors outside the angle span.
// To do this correctly, we would have to insert stops at 0°, 360° and if the spread
// is reflect/repeat, also throughout the uncovered area to get the correct
// rendering. It might however be easier to support this in QConicalGradient itself.
// For now, this is left only semi-supported, as sweep gradients are currently rare.
const qreal multiplier = qFuzzyCompare(endAngle, startAngle)
? 1.0
: (endAngle - startAngle) / 360.0;
QGradientStops adaptedStops;
adaptedStops.reserve(gradientStops.size());
for (const QGradientStop &gradientStop : gradientStops)
adaptedStops.append({gradientStop.first * multiplier, gradientStop.second});
conicalGradient.setStops(adaptedStops);
conicalGradient.setCoordinateMode(QGradient::LogicalMode);
m_painter->setBrush(conicalGradient);
}
}
void QColrPaintGraphRenderer::setRadialGradient(QPointF c0, qreal r0,
QPointF c1, qreal r1,
QGradient::Spread spread,
const QGradientStops &gradientStops)
{
if (m_painter != nullptr) {
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[radial gradient " << c0
<< ", rad=" << r0
<< ", " << c1
<< ", rad=" << r1
<< ", spread: " << spread
<< ", stop count: " << gradientStops.size()
<< "]";
QPointF c0e(c0 + QPointF(r0, 0.0));
QPointF c1e(c1 + QPointF(r1, 0.0));
c0 = m_currentTransform.map(c0);
c0e = m_currentTransform.map(c0e);
c1 = m_currentTransform.map(c1);
c1e = m_currentTransform.map(c1e);
const QVector2D d0(c0e - c0);
const QVector2D d1(c1e - c1);
QRadialGradient gradient(c1, d1.length(), c0, d0.length());
gradient.setCoordinateMode(QGradient::LogicalMode);
gradient.setSpread(spread);
gradient.setStops(gradientStops);
m_painter->setBrush(gradient);
}
}
void QColrPaintGraphRenderer::drawCurrentPath()
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[draw path " << m_currentPath.controlPointRect() << m_currentTransform
<< "]";
if (m_painter != nullptr)
m_painter->drawPath(m_currentPath);
}
void QColrPaintGraphRenderer::beginCalculateBoundingBox()
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[begin calculate bounding box]";
Q_ASSERT(m_painter == nullptr);
m_boundingRect = QRect{};
}
void QColrPaintGraphRenderer::drawImage(const QImage &image)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[draw image: " << image.size() << "]";
if (m_painter != nullptr) {
m_painter->setWorldMatrixEnabled(false);
m_painter->drawImage(QPoint(0, 0), image);
m_painter->setWorldMatrixEnabled(true);
}
}
void QColrPaintGraphRenderer::setCompositionMode(QPainter::CompositionMode mode)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[set composition mode: " << mode << "]";
if (m_painter != nullptr)
m_painter->setCompositionMode(mode);
}
void QColrPaintGraphRenderer::beginRender(qreal pixelSizeScale, const QTransform &transform)
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[begin render scale: " << pixelSizeScale
<< ", transform: " << transform
<< "]";
if (m_boundingRect.isEmpty())
return;
QRect alignedRect(m_boundingRect.toAlignedRect());
m_image = QImage(alignedRect.size(), QImage::Format_ARGB32_Premultiplied);
m_image.fill(Qt::transparent);
Q_ASSERT(m_painter == nullptr);
m_painter = new QPainter;
m_painter->begin(&m_image);
m_painter->setRenderHint(QPainter::Antialiasing);
m_painter->setPen(Qt::NoPen);
m_painter->setBrush(Qt::NoBrush);
m_painter->translate(-alignedRect.topLeft());
// Scale from normalized coordinates
m_painter->scale(pixelSizeScale, pixelSizeScale);
m_painter->setWorldTransform(transform, true);
}
QImage QColrPaintGraphRenderer::endRender()
{
qCDebug(lcColrv1).noquote().nospace()
<< QByteArray().fill(' ', m_oldPaths.size() * 2)
<< "[end image size: " << m_image.size()
<< "]";
Q_ASSERT(m_painter != nullptr);
m_painter->end();
delete m_painter;
m_painter = nullptr;
return m_image;
}
QT_END_NAMESPACE
|