-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvgframe.cpp
318 lines (244 loc) · 10.8 KB
/
svgframe.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
317
318
///////////////////////////////////////////////////////////////////////////////
// Name: svgframe.h
// Purpose: wxFrame for testing SVG rasterization with NanoSVG and LunaSVG
// Author: PB
// Created: 2024-01-18
// Copyright: (c) 2024 PB
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/busyinfo.h>
#include <wx/choicdlg.h>
#include <wx/config.h>
#include <wx/dcbuffer.h>
#include <wx/dir.h>
#include <wx/dirdlg.h>
#include <wx/filectrl.h>
#include <wx/filename.h>
#include <wx/numdlg.h>
#include <wx/slider.h>
#include <wx/splitter.h>
#include <wx/statline.h>
#include <wx/utils.h>
#include "svgbench.h"
#include "bmpbndl_lunasvg.h"
#include "svgframe.h"
// ============================================================================
// wxBitmapBundlePanel
// ============================================================================
class wxBitmapBundlePanel : public wxScrolledCanvas
{
public:
wxBitmapBundlePanel(wxWindow* parent, const wxSize& bitmapSize);
void SetBitmapBundle(const wxBitmapBundle& bundle);
void SetBitmapSize(const wxSize& size);
private:
wxBitmapBundle m_bitmapBundle;
wxSize m_bitmapSize;
void OnPaint(wxPaintEvent&);
};
wxBitmapBundlePanel::wxBitmapBundlePanel(wxWindow* parent, const wxSize& bitmapSize)
: wxScrolledCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE),
m_bitmapSize(bitmapSize)
{
wxASSERT(m_bitmapSize.x > 0 && m_bitmapSize.y > 0);
SetScrollRate(FromDIP(4), FromDIP(4));
SetBackgroundStyle(wxBG_STYLE_PAINT);
Bind(wxEVT_PAINT, &wxBitmapBundlePanel::OnPaint, this);
}
void wxBitmapBundlePanel::SetBitmapBundle(const wxBitmapBundle& bundle)
{
m_bitmapBundle = bundle;
Refresh(); Update();
}
void wxBitmapBundlePanel::SetBitmapSize(const wxSize& size)
{
wxCHECK_RET(size.x > 0 && size.y > 0, "invalid bitmapSize");
m_bitmapSize = size;
if ( m_bitmapSize != GetVirtualSize() )
{
InvalidateBestSize();
SetVirtualSize(m_bitmapSize);
}
Refresh(); Update();
}
void wxBitmapBundlePanel::OnPaint(wxPaintEvent&)
{
wxAutoBufferedPaintDC dc(this);
DoPrepareDC(dc);
dc.SetBackground(*wxWHITE);
dc.Clear();
if ( !m_bitmapBundle.IsOk() )
return;
const wxBitmap bitmap(m_bitmapBundle.GetBitmap(m_bitmapSize));
if ( bitmap.IsOk() )
{
wxBrush hatchBrush(*wxBLUE, wxBRUSHSTYLE_CROSSDIAG_HATCH);
wxDCBrushChanger bc(dc, hatchBrush);
wxDCPenChanger pc(dc, *wxBLUE_PEN);
dc.DrawRectangle(wxPoint(0, 0), m_bitmapSize);
dc.DrawBitmap(bitmap, 0, 0, true);
}
}
// ============================================================================
// wxTestSVGFrame
// ============================================================================
wxTestSVG2Frame::wxTestSVG2Frame()
: wxFrame(nullptr, wxID_ANY, "wxTestSVG2")
{
wxConfigBase* config = wxConfigBase::Get();
m_lastSVGFolder = config->Read("lastSVGFolder", m_lastSVGFolder);
m_lastRunCount = config->Read("lastRunCount", m_lastRunCount);
SetIcon(wxICON(wxICON_AAA)); // from wx.rc
wxSplitterWindow* splitterMain = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSP_3D | wxSP_LIVE_UPDATE);
wxPanel* controlPanel = new wxPanel(splitterMain); // for controls
wxPanel* bitmapPanel = new wxPanel(splitterMain); // for wxBitmapBundlePanels
wxBoxSizer* controlPanelSizer = new wxBoxSizer(wxVERTICAL);
controlPanelSizer->Add(new wxStaticText(controlPanel, wxID_ANY, "&Bitmap Size"),
wxSizerFlags().CenterHorizontal().Border(wxALL & ~wxBOTTOM));
m_bitmapSizeSlider = new wxSlider(controlPanel, wxID_ANY, m_bitmapSize.x, 16, 512,
wxDefaultPosition, wxDefaultSize,
wxSL_HORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS );
m_bitmapSizeSlider->SetTickFreq(16);
m_bitmapSizeSlider->Bind(wxEVT_SLIDER, &wxTestSVG2Frame::OnBitmapSizeChanged, this);
controlPanelSizer->Add(m_bitmapSizeSlider, wxSizerFlags().Expand().Border(wxALL & ~wxTOP));
controlPanelSizer->Add(new wxStaticLine(controlPanel), wxSizerFlags().Expand().Border());
wxButton* benchmarkFolderBtn = new wxButton(controlPanel, wxID_ANY, "Benchmark &Current Folder...");
benchmarkFolderBtn->Bind(wxEVT_BUTTON, &wxTestSVG2Frame::OnBenchmarkFolder, this);
controlPanelSizer->Add(benchmarkFolderBtn, wxSizerFlags().Expand().Border());
wxButton* changeFolderBtn = new wxButton(controlPanel, wxID_ANY, "Change &Folder...");
changeFolderBtn->Bind(wxEVT_BUTTON, &wxTestSVG2Frame::OnChangeFolder, this);
controlPanelSizer->Add(changeFolderBtn, wxSizerFlags().Expand().Border());
m_fileCtrl = new wxFileCtrl(controlPanel, wxID_ANY,
m_lastSVGFolder.empty() ? wxGetCwd() : m_lastSVGFolder,
".", "SVG files (*.svg)|*.svg",
wxFC_DEFAULT_STYLE | wxFC_NOSHOWHIDDEN);
m_fileCtrl->Bind(wxEVT_FILECTRL_FILEACTIVATED, &wxTestSVG2Frame::OnFileActivated, this);
m_fileCtrl->Bind(wxEVT_FILECTRL_SELECTIONCHANGED, &wxTestSVG2Frame::OnFileSelected, this);
controlPanelSizer->Add(m_fileCtrl, wxSizerFlags(1).Expand().Border());
controlPanel->SetSizerAndFit(controlPanelSizer);
m_panelNano = new wxBitmapBundlePanel(bitmapPanel, m_bitmapSize);
m_panelLuna = new wxBitmapBundlePanel(bitmapPanel, m_bitmapSize);
wxFlexGridSizer* bitmapPanelSizer = new wxFlexGridSizer(2);
bitmapPanelSizer->Add(new wxStaticText(bitmapPanel, wxID_ANY, "NanoSVG"),
wxSizerFlags().CenterHorizontal().Border(wxALL));
bitmapPanelSizer->Add(new wxStaticText(bitmapPanel, wxID_ANY, "LunaSVG"),
wxSizerFlags().CenterHorizontal().Border(wxALL));
bitmapPanelSizer->Add(m_panelNano, wxSizerFlags(1).Expand().Border());
bitmapPanelSizer->Add(m_panelLuna, wxSizerFlags(1).Expand().Border());
bitmapPanelSizer->AddGrowableCol(0, 1);
bitmapPanelSizer->AddGrowableCol(1, 1);
bitmapPanelSizer->AddGrowableRow(1, 1);
bitmapPanel->SetSizerAndFit(bitmapPanelSizer);
SetMinClientSize(FromDIP(wxSize(800, 600)));
splitterMain->SetMinimumPaneSize(FromDIP(100));
splitterMain->SetSashGravity(0.3);
splitterMain->SplitVertically(controlPanel, bitmapPanel, FromDIP(256));
}
wxTestSVG2Frame::~wxTestSVG2Frame()
{
wxConfigBase* config = wxConfigBase::Get();
config->Write("lastSVGFolder", m_fileCtrl->GetDirectory());
config->Write("lastRunCount", m_lastRunCount);
}
void wxTestSVG2Frame::OnFileSelected(wxFileCtrlEvent& event)
{
const wxFileName fileName(event.GetDirectory(), event.GetFile());
m_panelNano->SetBitmapBundle(wxBitmapBundle::FromSVGFile(fileName.GetFullPath(), m_bitmapSize));
m_panelLuna->SetBitmapBundle(CreateWithLunaSVGFromFile(fileName.GetFullPath(), m_bitmapSize));
}
void wxTestSVG2Frame::OnFileActivated(wxFileCtrlEvent& event)
{
const wxFileName fileName(event.GetDirectory(), event.GetFile());
wxLaunchDefaultApplication(fileName.GetFullPath());
}
void wxTestSVG2Frame::OnBenchmarkFolder(wxCommandEvent&)
{
#ifndef NDEBUG
if ( wxMessageBox("It appears you are running the debug version of the application, "
"which is much slower than the release one. Continue anyway?",
"Warning", wxYES_NO | wxNO_DEFAULT) != wxYES )
{
return;
}
#endif
const wxString dirName = m_fileCtrl->GetDirectory();
wxArrayString dirFiles;
wxArrayString files;
wxArrayInt selections;
{
wxBusyCursor bc;
wxDir::GetAllFiles(dirName, &dirFiles, "*.svg", wxDIR_FILES);
}
if ( dirFiles.empty() )
{
wxLogMessage("No SVG files found in the current folder.");
return;
}
for ( auto& f : dirFiles )
f = wxFileName(f).GetFullName();
dirFiles.Sort(wxNaturalStringSortAscending);
selections.reserve(dirFiles.size());
for ( size_t i = 0; i < dirFiles.size(); ++i )
selections.push_back(i);
if ( wxGetSelectedChoices(selections, wxString::Format("Select Files (%zu files available)", dirFiles.size()),
"Benchmark Rasterization", dirFiles, this) == -1
|| selections.empty() )
{
return;
}
for ( const auto& s : selections )
files.push_back(dirFiles[s]);
static const wxSize bitmapSizes[] =
{ { 16, 16},
{ 24, 24},
{ 32, 32},
{ 48, 48},
{ 64, 64},
{ 96, 96},
{ 128, 128},
{ 256, 256},
{ 512, 512},
{1024,1024}, };
wxArrayString bitmapSizesStrings;
for ( const auto& bs : bitmapSizes)
bitmapSizesStrings.push_back(wxString::Format("%d x %d", bs.x, bs.y));
selections.clear();
selections.push_back(1); //24x24
selections.push_back(3); //48x48
selections.push_back(6); //128x128
if ( wxGetSelectedChoices(selections, "Select Bitmap Sizes", "Benchmark Rasterization", bitmapSizesStrings, this) == -1
|| selections.empty() )
return;
const long runCount = wxGetNumberFromUser("Number of runs (between 10 and 100)", "Number", "Benchmark Rasterization", m_lastRunCount, 10, 100);
if ( runCount == -1 )
return;
m_lastRunCount = runCount;
wxTestSVGRasterizationBenchmark benchmark;
std::vector<wxSize> sizes;
for ( const auto& s : selections )
sizes.push_back(bitmapSizes[s]);
benchmark.Setup(dirName, files, sizes);
wxString report, detailedReport;
bool result = false;
{
wxBusyInfo info(wxString::Format("Benchmarking %zu files at %zu sizes (%ld runs each, %zu runs total), please wait...",
files.size(), sizes.size(), runCount, files.size() * sizes.size() * runCount), this);
result = benchmark.Run(runCount, report, detailedReport);
}
if ( result )
new wxTestSVGBenchmarkReportFrame(this, dirName, report, detailedReport);
}
void wxTestSVG2Frame::OnChangeFolder(wxCommandEvent&)
{
const wxString dir = wxDirSelector("Select Folder", m_fileCtrl->GetDirectory(), wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
if ( !dir.empty() )
m_fileCtrl->SetDirectory(dir);
}
void wxTestSVG2Frame::OnBitmapSizeChanged(wxCommandEvent& event)
{
m_bitmapSize = wxSize(event.GetInt(), event.GetInt());
m_panelNano->SetBitmapSize(m_bitmapSize);
m_panelLuna->SetBitmapSize(m_bitmapSize);
}