-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmpbndl_lunasvg.cpp
238 lines (194 loc) · 7.44 KB
/
bmpbndl_lunasvg.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
/////////////////////////////////////////////////////////////////////////////
// Name: bmpbndl_lunasvg.cpp
// Purpose: wxBitmapBundleImpl using LunaSVG to rasterize SVG
// Author: PB
// Created: 2024-01-18
// Copyright: (c) 2024 PB
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#include <wx/bmpbndl.h>
#include <wx/buffer.h>
#if wxUSE_FFILE
#include <wx/ffile.h>
#elif wxUSE_FILE
#include <wx/file.h>
#endif
#include <wx/log.h>
#include <wx/rawbmp.h>
#include <wx/utils.h>
#ifdef __WXMSW__
#include <wx/msw/wrapwin.h>
#endif
#include <memory>
#include <lunasvg.h>
#include "bmpbndl_lunasvg.h"
// ============================================================================
// wxBitmapBundleImplLunaSVG declaration
// ============================================================================
/*
wxBitmapBundleImpl using LunaSVG to rasterize an SVG to wxBitmap at given size.
Modelled after wxBitmapBundleImplSVG using NanoSVG.
*/
class wxBitmapBundleImplLunaSVG : public wxBitmapBundleImpl
{
public:
// data must be 0 terminated, wxBitmapBundleImplLunaSVG doesn't
// take its ownership and it can be deleted after the ctor was called.
wxBitmapBundleImplLunaSVG(const char* data, const wxSize& sizeDef);
// wxBitmapBundleImplLunaSVG doesn't take ownership of data and it
// can be deleted after the ctor was called. len is data length in bytes.
wxBitmapBundleImplLunaSVG(const wxByte* data, size_t len, const wxSize& sizeDef);
virtual wxSize GetDefaultSize() const override;
virtual wxSize GetPreferredBitmapSizeAtScale(double scale) const override;
virtual wxBitmap GetBitmap(const wxSize& size) override;
bool IsOk() const;
private:
std::unique_ptr<lunasvg::Document> m_svgDocument;
const wxSize m_sizeDef;
wxBitmap m_cachedBitmap;
wxBitmap DoRasterize(const wxSize& size);
wxDECLARE_NO_COPY_CLASS(wxBitmapBundleImplLunaSVG);
};
// Creates wxBitmapBundle from in-memory SVG using wxBitmapBundleImplLunaSVG
wxBitmapBundle CreateWithLunaSVGFromMemory(const wxByte* data, size_t len, const wxSize& sizeDef)
{
return wxBitmapBundle::FromImpl(new wxBitmapBundleImplLunaSVG(data, len, sizeDef));
}
// Creates wxBitmapBundle from SVG file using wxBitmapBundleImplLunaSVG
wxBitmapBundle CreateWithLunaSVGFromFile(const wxString& path, const wxSize& sizeDef)
{
#if wxUSE_FFILE
wxFFile file(path, "rb");
#elif wxUSE_FILE
wxFile file(path);
#else
#error "wxWidgets must be built with support for wxFFile or wxFile"
#endif
if ( file.IsOpened() )
{
const wxFileOffset lenAsOfs = file.Length();
if ( lenAsOfs != wxInvalidOffset )
{
const size_t len = static_cast<size_t>(lenAsOfs);
wxMemoryBuffer buf(len);
if ( file.Read(static_cast<char*>(buf.GetWriteBuf(len)), len) == len )
{
buf.UngetWriteBuf(len);
return CreateWithLunaSVGFromMemory(static_cast<wxByte*>(buf.GetData()), len, sizeDef);
}
}
}
return wxBitmapBundle();
}
// ============================================================================
// wxBitmapBundleImplLunaSVG implementation
// ============================================================================
wxBitmapBundleImplLunaSVG::wxBitmapBundleImplLunaSVG(const char* data, const wxSize& sizeDef)
: m_sizeDef(sizeDef)
{
wxCHECK_RET(data != nullptr, "null data");
wxCHECK_RET(sizeDef.GetWidth() > 0 && sizeDef.GetHeight() > 0, "invalid default size");
m_svgDocument = lunasvg::Document::loadFromData(data);
}
wxBitmapBundleImplLunaSVG::wxBitmapBundleImplLunaSVG(const wxByte* data, size_t len, const wxSize& sizeDef)
: m_sizeDef(sizeDef)
{
wxCHECK_RET(data != nullptr, "null data");
wxCHECK_RET(len > 0, "zero length");
wxCHECK_RET(sizeDef.GetWidth() > 0 && sizeDef.GetHeight() > 0, "invalid default size");
m_svgDocument = lunasvg::Document::loadFromData(reinterpret_cast<const char*>(data), len);
}
wxSize wxBitmapBundleImplLunaSVG::GetDefaultSize() const
{
return m_sizeDef;
};
wxSize wxBitmapBundleImplLunaSVG::GetPreferredBitmapSizeAtScale(double scale) const
{
return m_sizeDef*scale;
}
wxBitmap wxBitmapBundleImplLunaSVG::GetBitmap(const wxSize& size)
{
if ( !m_cachedBitmap.IsOk() || m_cachedBitmap.GetSize() != size )
m_cachedBitmap = DoRasterize(size);
return m_cachedBitmap;
}
bool wxBitmapBundleImplLunaSVG::IsOk() const
{
return m_svgDocument != nullptr;
}
wxBitmap wxBitmapBundleImplLunaSVG::DoRasterize(const wxSize& size)
{
if ( IsOk() )
{
const auto scale = wxMin(size.x/m_svgDocument->width(), size.y/m_svgDocument->height());
const auto scaleMatrix = lunasvg::Matrix::scaled(scale, scale);
lunasvg::Bitmap lbmp(size.x, size.y);
lbmp.clear(0);
m_svgDocument->render(lbmp, scaleMatrix);
// conversion to wxBitmap is based on the code in lunasvg::Bitmap::convert()
if ( lbmp.valid() )
{
const auto width = lbmp.width();
const auto height = lbmp.height();
const auto stride = lbmp.stride();
auto rowData = lbmp.data();
wxBitmap bmp(width, height, 32);
// Using SetDIBits() is much slower than wxAlphaPixelData loops?!
//#ifdef __WXMSW__
// if ( bmp.IsDIB() && stride % sizeof(LONG) == 0 )
// {
// const HDC hScreenDC = ::GetDC(nullptr);
// BITMAPINFO bitmapInfo = {0};
// bool success;
//
// bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFO) - sizeof(RGBQUAD);
// bitmapInfo.bmiHeader.biWidth = size.x;
// bitmapInfo.bmiHeader.biHeight = 0 - size.y;
// bitmapInfo.bmiHeader.biPlanes = 1;
// bitmapInfo.bmiHeader.biBitCount = 32;
// bitmapInfo.bmiHeader.biCompression = BI_RGB;
//
// success = ::SetDIBits(hScreenDC, bmp.GetHBITMAP(), 0, bmp.GetHeight(), rowData, &bitmapInfo, DIB_RGB_COLORS) > 0;
// ::ReleaseDC(nullptr, hScreenDC);
// if ( success )
// {
// bmp.UseAlpha();
// return bmp;
// }
// }
//#endif
wxAlphaPixelData bmpdata(bmp);
wxAlphaPixelData::Iterator dst(bmpdata);
for ( std::uint32_t y = 0; y < height; ++y )
{
auto data = rowData;
dst.MoveTo(bmpdata, 0, y);
for ( std::uint32_t x = 0; x < width; ++x, ++dst )
{
auto b = data[0];
auto g = data[1];
auto r = data[2];
auto a = data[3];
#ifndef wxHAS_PREMULTIPLIED_ALPHA
if (a != 0 )
{
r = (r * 255) / a;
g = (g * 255) / a;
b = (b * 255) / a;
}
#endif
dst.Red() = r;
dst.Green() = g;
dst.Blue() = b;
dst.Alpha() = a;
data += 4;
}
rowData += stride;
}
return bmp;
}
else
wxLogDebug("invalid lunasvg::Bitmap");
}
return wxBitmap();
}