forked from musescore/MuseScore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpainterprovider.cpp
316 lines (272 loc) · 8.6 KB
/
qpainterprovider.cpp
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
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2021 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "qpainterprovider.h"
#include <QPainter>
#include <QRawFont>
#include <QTextLayout>
#include <QTextLine>
#include <QGlyphRun>
#include <QStaticText>
#include "fontcompat.h"
#include "utils/drawlogger.h"
#include "log.h"
using namespace mu::draw;
QPainterProvider::QPainterProvider(QPainter* painter, bool overship)
: m_painter(painter), m_overship(overship), m_drawObjectsLogger(new DrawObjectsLogger()),
m_font(mu::draw::fromQFont(m_painter->font())),
m_pen(Pen::fromQPen(m_painter->pen())),
m_brush(Brush::fromQBrush(m_painter->brush()))
{
}
QPainterProvider::~QPainterProvider()
{
if (m_overship) {
delete m_painter;
}
delete m_drawObjectsLogger;
}
IPaintProviderPtr QPainterProvider::make(QPaintDevice* dp)
{
return std::make_shared<QPainterProvider>(new QPainter(dp), true);
}
IPaintProviderPtr QPainterProvider::make(QPainter* qp, bool overship)
{
return std::make_shared<QPainterProvider>(qp, overship);
}
QPaintDevice* QPainterProvider::device() const
{
return m_painter->device();
}
QPainter* QPainterProvider::qpainter() const
{
return m_painter;
}
void QPainterProvider::beginTarget(const std::string&)
{
}
void QPainterProvider::beforeEndTargetHook(Painter*)
{
}
bool QPainterProvider::endTarget(bool endDraw)
{
if (endDraw) {
return m_painter->end();
}
return true;
}
bool QPainterProvider::isActive() const
{
return m_painter->isActive();
}
void QPainterProvider::beginObject(const std::string& name, const PointF& pagePos)
{
m_drawObjectsLogger->beginObject(name, pagePos);
}
void QPainterProvider::endObject()
{
m_drawObjectsLogger->endObject();
}
void QPainterProvider::setAntialiasing(bool arg)
{
m_painter->setRenderHint(QPainter::Antialiasing, arg);
m_painter->setRenderHint(QPainter::TextAntialiasing, arg);
}
void QPainterProvider::setCompositionMode(CompositionMode mode)
{
auto toQPainter = [](CompositionMode mode) {
switch (mode) {
case CompositionMode::SourceOver: return QPainter::CompositionMode_SourceOver;
case CompositionMode::HardLight: return QPainter::CompositionMode_HardLight;
}
return QPainter::CompositionMode_SourceOver;
};
m_painter->setCompositionMode(toQPainter(mode));
}
void QPainterProvider::setFont(const Font& font)
{
if (m_font != font) {
m_painter->setFont(mu::draw::toQFont(font));
m_font = font;
}
}
const Font& QPainterProvider::font() const
{
return m_font;
}
void QPainterProvider::setPen(const Pen& pen)
{
m_pen = pen;
m_painter->setPen(Pen::toQPen(m_pen));
}
void QPainterProvider::setNoPen()
{
m_pen = Pen(PenStyle::NoPen);
m_painter->setPen(Pen::toQPen(m_pen));
}
const Pen& QPainterProvider::pen() const
{
return m_pen;
}
void QPainterProvider::setBrush(const Brush& brush)
{
m_brush = brush;
m_painter->setBrush(Brush::toQBrush(m_brush));
}
const Brush& QPainterProvider::brush() const
{
return m_brush;
}
void QPainterProvider::save()
{
m_painter->save();
}
void QPainterProvider::restore()
{
m_painter->restore();
m_font = mu::draw::fromQFont(m_painter->font());
m_pen = Pen::fromQPen(m_painter->pen());
m_brush = Brush::fromQBrush(m_painter->brush());
}
void QPainterProvider::setTransform(const QTransform& transform)
{
m_transform = transform;
m_painter->setTransform(transform);
}
const QTransform& QPainterProvider::transform() const
{
return m_painter->transform();
}
// drawing functions
void QPainterProvider::drawPath(const QPainterPath& path)
{
m_painter->drawPath(path);
}
void QPainterProvider::drawPolygon(const PointF* points, size_t pointCount, PolygonMode mode)
{
static_assert(sizeof(QPointF) == sizeof(PointF), "sizeof(QPointF) and sizeof(PointF) must be equal");
const QPointF* qpoints = reinterpret_cast<const QPointF*>(points);
switch (mode) {
case PolygonMode::OddEven: {
m_painter->drawPolygon(qpoints, int(pointCount), Qt::OddEvenFill);
} break;
case PolygonMode::Winding: {
m_painter->drawPolygon(qpoints, int(pointCount), Qt::WindingFill);
} break;
case PolygonMode::Convex: {
m_painter->drawConvexPolygon(qpoints, int(pointCount));
} break;
case PolygonMode::Polyline: {
m_painter->drawPolyline(qpoints, int(pointCount));
} break;
}
}
void QPainterProvider::drawText(const PointF& point, const QString& text)
{
m_painter->drawText(point.toQPointF(), text);
}
void QPainterProvider::drawText(const RectF& rect, int flags, const QString& text)
{
m_painter->drawText(rect.toQRectF(), flags, text);
}
void QPainterProvider::drawTextWorkaround(const Font& f, const PointF& pos, const QString& text)
{
m_painter->save();
qreal mm = m_painter->worldTransform().m11();
qreal dx = m_painter->worldTransform().dx();
qreal dy = m_painter->worldTransform().dy();
// diagonal elements will now be changed to 1.0
m_painter->setWorldTransform(QTransform(1.0, 0.0, 0.0, 1.0, dx, dy));
// correction factor for bold text drawing, due to the change of the diagonal elements
qreal factor = 1.0 / mm;
QFont fnew(mu::draw::toQFont(f), m_painter->device());
fnew.setPointSizeF(f.pointSizeF() / factor);
QRawFont fRaw = QRawFont::fromFont(fnew);
QTextLayout textLayout(text, mu::draw::toQFont(f), m_painter->device());
textLayout.beginLayout();
while (true) {
QTextLine line = textLayout.createLine();
if (!line.isValid()) {
break;
}
}
textLayout.endLayout();
// glyphruns with correct positions, but potentially wrong glyphs
// (see bug https://musescore.org/en/node/117191 regarding positions and DPI)
QList<QGlyphRun> glyphruns = textLayout.glyphRuns();
qreal offset = 0;
// glyphrun drawing has an offset equal to the max ascent of the text fragment
for (int i = 0; i < glyphruns.length(); i++) {
qreal value = glyphruns.at(i).rawFont().ascent() / factor;
if (value > offset) {
offset = value;
}
}
for (int i = 0; i < glyphruns.length(); i++) {
QVector<QPointF> positions1 = glyphruns.at(i).positions();
QVector<QPointF> positions2;
// calculate the new positions for the scaled geometry
for (int j = 0; j < positions1.length(); j++) {
QPointF newPoint = positions1.at(j) / factor;
positions2.append(newPoint);
}
QGlyphRun glyphrun2 = glyphruns.at(i);
glyphrun2.setPositions(positions2);
// change the glyphs with the correct glyphs
// and account for glyph substitution
if (glyphrun2.rawFont().familyName() != fnew.family()) {
QFont f2(fnew);
f2.setFamily(glyphrun2.rawFont().familyName());
glyphrun2.setRawFont(QRawFont::fromFont(f2));
} else {
glyphrun2.setRawFont(fRaw);
}
m_painter->drawGlyphRun(QPointF(pos.x() / factor, pos.y() / factor - offset), glyphrun2);
positions2.clear();
}
// Restore the QPainter to its former state
m_painter->setWorldTransform(QTransform(mm, 0.0, 0.0, mm, dx, dy));
m_painter->restore();
}
void QPainterProvider::drawSymbol(const PointF& point, uint ucs4Code)
{
static QHash<uint, QString> cache;
if (!cache.contains(ucs4Code)) {
cache[ucs4Code] = QString::fromUcs4(&ucs4Code, 1);
}
m_painter->drawText(QPointF(point.x(), point.y()), cache[ucs4Code]);
}
void QPainterProvider::drawPixmap(const PointF& point, const QPixmap& pm)
{
m_painter->drawPixmap(QPointF(point.x(), point.y()), pm);
}
void QPainterProvider::drawTiledPixmap(const RectF& rect, const QPixmap& pm, const PointF& offset)
{
m_painter->drawTiledPixmap(rect.toQRectF(), pm, QPointF(offset.x(), offset.y()));
}
void QPainterProvider::setClipRect(const RectF& rect)
{
m_painter->setClipRect(rect.toQRectF());
}
void QPainterProvider::setClipping(bool enable)
{
m_painter->setClipping(enable);
}