-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSvg.h
More file actions
183 lines (144 loc) · 5.8 KB
/
Svg.h
File metadata and controls
183 lines (144 loc) · 5.8 KB
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
/*
==============================================================================
This file is part of the YUP library.
Copyright (c) 2025 - kunitoki@gmail.com
YUP is an open source library subject to open-source licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#pragma once
class SvgDemo : public yup::Component
{
public:
SvgDemo()
{
updateListOfSvgFiles();
loadDemoFont();
parseSvgFile (currentSvgFileIndex);
}
void mouseDown (const yup::MouseEvent& event) override
{
if (event.isLeftButtonDown())
++currentSvgFileIndex;
else if (event.isRightButtonDown())
--currentSvgFileIndex;
parseSvgFile (currentSvgFileIndex);
}
void paint (yup::Graphics& g) override
{
g.setFillColor (findColor (yup::DocumentWindow::Style::backgroundColorId).value_or (yup::Colors::dimgray));
g.fillAll();
drawable.paint (g, getLocalBounds());
}
private:
void updateListOfSvgFiles()
{
yup::File riveBasePath = yup::File (__FILE__)
.getParentDirectory()
.getParentDirectory()
.getParentDirectory();
dataDirectory = riveBasePath.getChildFile ("data");
auto files = riveBasePath.getChildFile ("data/svg").findChildFiles (yup::File::findFiles, false, "*.svg");
if (files.isEmpty())
return;
for (const auto& svgFile : files)
{
//if (svgFile.getFileName() == "mozilla2.svg")
svgFiles.add (svgFile);
}
}
void parseSvgFile (int index)
{
if (svgFiles.isEmpty())
return;
if (index < 0)
index = svgFiles.size() - 1;
if (index >= svgFiles.size())
index = 0;
currentSvgFileIndex = index;
YUP_DBG ("Showing " << svgFiles[currentSvgFileIndex].getFullPathName());
drawable.clear();
drawable.parseSVG (svgFiles[currentSvgFileIndex], createParseOptions (svgFiles[currentSvgFileIndex]));
repaint();
}
void loadDemoFont()
{
yup::Font font;
if (font.loadFromFile (dataDirectory.getChildFile ("RobotoFlex-VariableFont.ttf")).wasOk())
demoFont = std::move (font);
}
std::optional<yup::Image> fetchHttpImage (const yup::String& href)
{
if (! href.startsWithIgnoreCase ("http:") && ! href.startsWithIgnoreCase ("https:"))
return std::nullopt;
if (httpImageCache.contains (href))
return httpImageCache[href];
yup::MemoryBlock imageData;
int statusCode = 0;
auto streamOptions = yup::URL::InputStreamOptions (yup::URL::ParameterHandling::inAddress)
.withConnectionTimeoutMs (5000)
.withNumRedirectsToFollow (5)
.withStatusCode (&statusCode)
.withExtraHeaders ("User-Agent: YUP SVG Demo\r\nAccept: image/*\r\n");
auto stream = yup::URL (href).createInputStream (streamOptions);
if (stream == nullptr)
{
YUP_DBG ("Unable to fetch SVG image href: " << href);
return std::nullopt;
}
stream->readIntoMemoryBlock (imageData);
if ((statusCode != 0 && (statusCode < 200 || statusCode >= 300)) || imageData.isEmpty())
{
YUP_DBG ("Unable to fetch SVG image href: " << href << " status: " << statusCode);
return std::nullopt;
}
auto imageResult = yup::Image::loadFromData (imageData.asBytes());
if (imageResult.failed())
{
YUP_DBG ("Unable to decode SVG image href: " << href << " error: " << imageResult.getErrorMessage());
return std::nullopt;
}
auto image = imageResult.getValue();
httpImageCache.set (href, image);
return image;
}
yup::Drawable::ParseOptions createParseOptions (const yup::File& svgFile)
{
yup::Drawable::ParseOptions options;
options.baseDirectory = svgFile.getParentDirectory();
options.imageResolver = [this] (yup::StringRef href, const yup::File&) -> std::optional<yup::Image>
{
return fetchHttpImage (yup::String (href.text));
};
options.fontResolver = [this] (yup::StringRef, float fontSize, int weight, bool italic) -> std::optional<yup::Font>
{
if (demoFont)
{
auto font = *demoFont;
font.setAxisValue ("wght", static_cast<float> (weight));
if (italic)
font.setAxisValue ("slnt", -10.0f);
else
font.setAxisValue ("slnt", 0.0f);
return font.withHeight (fontSize);
}
if (auto theme = yup::ApplicationTheme::getGlobalTheme())
return theme->getDefaultFont().withHeight (fontSize);
return std::nullopt;
};
return options;
}
yup::Drawable drawable;
yup::Array<yup::File> svgFiles;
yup::File dataDirectory;
std::optional<yup::Font> demoFont;
yup::HashMap<yup::String, yup::Image> httpImageCache;
int currentSvgFileIndex = 0;
};