-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastsvgimageprovider.cpp
57 lines (45 loc) · 1.21 KB
/
fastsvgimageprovider.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
#if defined(_WIN32)
# include <malloc.h>
# define SIP_ALLOCA(x) _alloca(x)
#else
# include <stdlib.h>
# define SIP_ALLOCA(x) alloca(x)
#endif // SIP_ALLOCA
#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);
do if (!pm.isNull())
{
char* dat;
if (QFile f(id); f.open(QIODevice::ReadOnly))
{
if (auto const fsz(f.size());
fsz == f.read(dat = static_cast<char*>(SIP_ALLOCA(fsz + 1)), fsz))
dat[fsz] = {}; else break;
}
else break;
if (auto const nsi(nsvgParse(dat, "px", 96)); nsi)
{
pm.fill(Qt::transparent);
{
QPainter p(&pm);
p.setRenderHint(QPainter::Antialiasing);
drawSVGImage(&p, nsi, rs.width(), rs.height());
}
nsvgDelete(nsi);
}
} while (false);
return pm;
}