@@ -27,18 +27,17 @@ class SvgDemo : public yup::Component
2727 SvgDemo ()
2828 {
2929 updateListOfSvgFiles ();
30+ loadDemoFont ();
3031
3132 parseSvgFile (currentSvgFileIndex);
3233 }
3334
34- void resized () override
35- {
36- // drawable.setBounds (getLocalBounds());
37- }
38-
3935 void mouseDown (const yup::MouseEvent& event) override
4036 {
41- ++currentSvgFileIndex;
37+ if (event.isLeftButtonDown ())
38+ ++currentSvgFileIndex;
39+ else if (event.isRightButtonDown ())
40+ --currentSvgFileIndex;
4241
4342 parseSvgFile (currentSvgFileIndex);
4443 }
@@ -59,12 +58,17 @@ class SvgDemo : public yup::Component
5958 .getParentDirectory ()
6059 .getParentDirectory ();
6160
61+ dataDirectory = riveBasePath.getChildFile (" data" );
62+
6263 auto files = riveBasePath.getChildFile (" data/svg" ).findChildFiles (yup::File::findFiles, false , " *.svg" );
6364 if (files.isEmpty ())
6465 return ;
6566
6667 for (const auto & svgFile : files)
68+ {
69+ // if (svgFile.getFileName() == "mozilla2.svg")
6770 svgFiles.add (svgFile);
71+ }
6872 }
6973
7074 void parseSvgFile (int index)
@@ -83,12 +87,97 @@ class SvgDemo : public yup::Component
8387 YUP_DBG (" Showing " << svgFiles[currentSvgFileIndex].getFullPathName ());
8488
8589 drawable.clear ();
86- drawable.parseSVG (svgFiles[currentSvgFileIndex]);
90+ drawable.parseSVG (svgFiles[currentSvgFileIndex], createParseOptions (svgFiles[currentSvgFileIndex]) );
8791
8892 repaint ();
8993 }
9094
95+ void loadDemoFont ()
96+ {
97+ yup::Font font;
98+ if (font.loadFromFile (dataDirectory.getChildFile (" RobotoFlex-VariableFont.ttf" )).wasOk ())
99+ demoFont = std::move (font);
100+ }
101+
102+ std::optional<yup::Image> fetchHttpImage (const yup::String& href)
103+ {
104+ if (! href.startsWithIgnoreCase (" http:" ) && ! href.startsWithIgnoreCase (" https:" ))
105+ return std::nullopt ;
106+
107+ if (httpImageCache.contains (href))
108+ return httpImageCache[href];
109+
110+ yup::MemoryBlock imageData;
111+ int statusCode = 0 ;
112+
113+ auto streamOptions = yup::URL::InputStreamOptions (yup::URL::ParameterHandling::inAddress)
114+ .withConnectionTimeoutMs (5000 )
115+ .withNumRedirectsToFollow (5 )
116+ .withStatusCode (&statusCode)
117+ .withExtraHeaders (" User-Agent: YUP SVG Demo\r\n Accept: image/*\r\n " );
118+
119+ auto stream = yup::URL (href).createInputStream (streamOptions);
120+ if (stream == nullptr )
121+ {
122+ YUP_DBG (" Unable to fetch SVG image href: " << href);
123+ return std::nullopt ;
124+ }
125+
126+ stream->readIntoMemoryBlock (imageData);
127+
128+ if ((statusCode != 0 && (statusCode < 200 || statusCode >= 300 )) || imageData.isEmpty ())
129+ {
130+ YUP_DBG (" Unable to fetch SVG image href: " << href << " status: " << statusCode);
131+ return std::nullopt ;
132+ }
133+
134+ auto imageResult = yup::Image::loadFromData (imageData.asBytes ());
135+ if (imageResult.failed ())
136+ {
137+ YUP_DBG (" Unable to decode SVG image href: " << href << " error: " << imageResult.getErrorMessage ());
138+ return std::nullopt ;
139+ }
140+
141+ auto image = imageResult.getValue ();
142+ httpImageCache.set (href, image);
143+ return image;
144+ }
145+
146+ yup::Drawable::ParseOptions createParseOptions (const yup::File& svgFile)
147+ {
148+ yup::Drawable::ParseOptions options;
149+ options.baseDirectory = svgFile.getParentDirectory ();
150+ options.imageResolver = [this ] (yup::StringRef href, const yup::File&) -> std::optional<yup::Image>
151+ {
152+ return fetchHttpImage (yup::String (href.text ));
153+ };
154+
155+ options.fontResolver = [this ] (yup::StringRef, float fontSize, int weight, bool italic) -> std::optional<yup::Font>
156+ {
157+ if (demoFont)
158+ {
159+ auto font = *demoFont;
160+ font.setAxisValue (" wght" , static_cast <float > (weight));
161+ if (italic)
162+ font.setAxisValue (" slnt" , -10 .0f );
163+ else
164+ font.setAxisValue (" slnt" , 0 .0f );
165+ return font.withHeight (fontSize);
166+ }
167+
168+ if (auto theme = yup::ApplicationTheme::getGlobalTheme ())
169+ return theme->getDefaultFont ().withHeight (fontSize);
170+
171+ return std::nullopt ;
172+ };
173+
174+ return options;
175+ }
176+
91177 yup::Drawable drawable;
92178 yup::Array<yup::File> svgFiles;
179+ yup::File dataDirectory;
180+ std::optional<yup::Font> demoFont;
181+ yup::HashMap<yup::String, yup::Image> httpImageCache;
93182 int currentSvgFileIndex = 0 ;
94183};
0 commit comments