-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Internal update) SVGSprite no longer loads the same svg file multiple times
- Loading branch information
1 parent
2b1dc11
commit af83d11
Showing
8 changed files
with
154 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[plugin] | ||
|
||
name="SVGSprite" | ||
description="" | ||
description="Rasterize svg dynamically" | ||
author="heppocogne" | ||
version="1.3.1" | ||
version="1.3.2" | ||
script="plugin.gd" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "rawsvg_loader.h" | ||
#include <File.hpp> | ||
|
||
using namespace godot; | ||
|
||
RawSvgLoader *RawSvgLoader::singleton = nullptr; | ||
|
||
void RawSvgLoader::on_library_init() | ||
{ | ||
if (singleton != nullptr) | ||
delete singleton; | ||
singleton = new RawSvgLoader(); | ||
} | ||
|
||
void RawSvgLoader::on_library_terminate() | ||
{ | ||
delete singleton; | ||
singleton = nullptr; | ||
} | ||
|
||
RawSvgLoader *RawSvgLoader::get_singleton() | ||
{ | ||
return singleton; | ||
} | ||
|
||
RawSvgLoader::~RawSvgLoader() | ||
{ | ||
for (auto &pair : docs_cache) | ||
docs_cache[pair.first] = nullptr; | ||
} | ||
|
||
std::unique_ptr<lunasvg::Document> &RawSvgLoader::load(const String &path) | ||
{ | ||
if (docs_cache.count(path) == 0) | ||
{ | ||
Ref<File> ref_f = File::_new(); | ||
Error err; | ||
if (path.ends_with(".rawsvg")) | ||
err = ref_f->open(path, File::READ); | ||
else if (path.ends_with(".rawsvgz")) | ||
err = ref_f->open_compressed(path, File::READ, File::COMPRESSION_DEFLATE); | ||
|
||
if (err == godot::Error::OK) | ||
{ | ||
char *buf = ref_f->get_as_text().alloc_c_string(); | ||
docs_cache[path] = lunasvg::Document::loadFromData(const_cast<const char *>(buf)); | ||
godot::api->godot_free(buf); | ||
if (!docs_cache[path]) | ||
{ | ||
Godot::print_error("invalid svg file:" + path, __func__, __FILE__, __LINE__); | ||
docs_cache[path] = nullptr; | ||
} | ||
} | ||
} | ||
|
||
return docs_cache[path]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef RAWSVG_LOADER_H | ||
#define RAWSVG_LOADER_H | ||
|
||
#include <Godot.hpp> | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <lunasvg.h> | ||
|
||
namespace godot | ||
{ | ||
class RawSvgLoader | ||
{ | ||
static RawSvgLoader *singleton; | ||
RawSvgLoader() {} | ||
std::map<String, std::unique_ptr<lunasvg::Document>> docs_cache; | ||
|
||
public: | ||
static void on_library_init(); | ||
static void on_library_terminate(); | ||
static RawSvgLoader *get_singleton(); | ||
~RawSvgLoader(); | ||
|
||
std::unique_ptr<lunasvg::Document> &load(const String &path); | ||
}; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters