-
Notifications
You must be signed in to change notification settings - Fork 0
/
altsvgimageprovider.cpp
50 lines (38 loc) · 1.07 KB
/
altsvgimageprovider.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
#include <QFile>
#include <QPainter>
#include "nanosvg/src/nanosvg.h"
#include "qtnanosvg.hpp"
#include "svgimageprovider.hpp"
//////////////////////////////////////////////////////////////////////////////
SVGImageProvider::SVGImageProvider():
QQuickImageProvider(QQmlImageProviderBase::Pixmap)
{
}
//////////////////////////////////////////////////////////////////////////////
QPixmap SVGImageProvider::requestPixmap(QString const& id, QSize* const sz,
QSize const& rs)
{
QPixmap pm(*sz = rs);
if (!pm.isNull())
{
uchar* dat;
QFile f(id);
if (auto const fsz(f.size()); (fsz > 0) && f.open(QIODevice::ReadOnly) &&
(dat = f.map({}, fsz, QFileDevice::MapPrivateOption)))
{
dat[fsz - 1] = {};
if (auto const nsi = nsvgParse(reinterpret_cast<char*>(dat), "px", 96))
{
pm.fill(Qt::transparent);
{
QPainter p(&pm);
p.setRenderHint(QPainter::Antialiasing);
drawSVGImage(&p, nsi, rs.width(), rs.height());
}
nsvgDelete(nsi);
}
f.unmap(dat);
}
}
return pm;
}