Add support for svg images in the asset lib.
Pixel based image formats are identified by magic numbers. This is not possible with svg therefore svg parsing is tried and if it succeeded the result is used. WebP and bmp support is added as well. But I could not test it as I am not able to run a local instance of the asset lib and there is no asset using those formats.
This commit is contained in:
parent
c023d41036
commit
66fa776667
@ -43,6 +43,11 @@
|
||||
#include "editor/project_settings_editor.h"
|
||||
#include "scene/gui/menu_button.h"
|
||||
|
||||
#include "modules/modules_enabled.gen.h" // For svg.
|
||||
#ifdef MODULE_SVG_ENABLED
|
||||
#include "modules/svg/image_loader_svg.h"
|
||||
#endif
|
||||
|
||||
static inline void setup_http_request(HTTPRequest *request) {
|
||||
request->set_use_threads(EDITOR_DEF("asset_library/use_threads", true));
|
||||
|
||||
@ -751,13 +756,30 @@ void EditorAssetLibrary::_image_update(bool use_cache, bool final, const PackedB
|
||||
|
||||
uint8_t png_signature[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
|
||||
uint8_t jpg_signature[3] = { 255, 216, 255 };
|
||||
uint8_t webp_signature[4] = { 82, 73, 70, 70 };
|
||||
uint8_t bmp_signature[2] = { 66, 77 };
|
||||
|
||||
if (r) {
|
||||
if ((memcmp(&r[0], &png_signature[0], 8) == 0) && Image::_png_mem_loader_func) {
|
||||
image->copy_internals_from(Image::_png_mem_loader_func(r, len));
|
||||
} else if ((memcmp(&r[0], &jpg_signature[0], 3) == 0) && Image::_jpg_mem_loader_func) {
|
||||
image->copy_internals_from(Image::_jpg_mem_loader_func(r, len));
|
||||
} else if ((memcmp(&r[0], &webp_signature[0], 4) == 0) && Image::_webp_mem_loader_func) {
|
||||
image->copy_internals_from(Image::_webp_mem_loader_func(r, len));
|
||||
} else if ((memcmp(&r[0], &bmp_signature[0], 2) == 0) && Image::_bmp_mem_loader_func) {
|
||||
image->copy_internals_from(Image::_bmp_mem_loader_func(r, len));
|
||||
}
|
||||
#ifdef MODULE_SVG_ENABLED
|
||||
else {
|
||||
ImageLoaderSVG svg_loader;
|
||||
Ref<Image> img = Ref<Image>(memnew(Image));
|
||||
Error err = svg_loader.create_image_from_utf8_buffer(img, image_data, 1.0, false);
|
||||
|
||||
if (err == OK) {
|
||||
image->copy_internals_from(img);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!image->is_empty()) {
|
||||
|
@ -67,19 +67,12 @@ void ImageLoaderSVG::_replace_color_property(const HashMap<Color, Color> &p_colo
|
||||
}
|
||||
}
|
||||
|
||||
Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, String p_string, float p_scale, bool p_upsample, const HashMap<Color, Color> &p_color_map) {
|
||||
Error ImageLoaderSVG::create_image_from_utf8_buffer(Ref<Image> p_image, const PackedByteArray &p_buffer, float p_scale, bool p_upsample) {
|
||||
ERR_FAIL_COND_V_MSG(Math::is_zero_approx(p_scale), ERR_INVALID_PARAMETER, "ImageLoaderSVG: Can't load SVG with a scale of 0.");
|
||||
|
||||
if (p_color_map.size()) {
|
||||
_replace_color_property(p_color_map, "stop-color=\"", p_string);
|
||||
_replace_color_property(p_color_map, "fill=\"", p_string);
|
||||
_replace_color_property(p_color_map, "stroke=\"", p_string);
|
||||
}
|
||||
|
||||
std::unique_ptr<tvg::Picture> picture = tvg::Picture::gen();
|
||||
PackedByteArray bytes = p_string.to_utf8_buffer();
|
||||
|
||||
tvg::Result result = picture->load((const char *)bytes.ptr(), bytes.size(), "svg", true);
|
||||
tvg::Result result = picture->load((const char *)p_buffer.ptr(), p_buffer.size(), "svg", true);
|
||||
if (result != tvg::Result::Success) {
|
||||
return ERR_INVALID_DATA;
|
||||
}
|
||||
@ -149,6 +142,18 @@ Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, String p_stri
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, String p_string, float p_scale, bool p_upsample, const HashMap<Color, Color> &p_color_map) {
|
||||
if (p_color_map.size()) {
|
||||
_replace_color_property(p_color_map, "stop-color=\"", p_string);
|
||||
_replace_color_property(p_color_map, "fill=\"", p_string);
|
||||
_replace_color_property(p_color_map, "stroke=\"", p_string);
|
||||
}
|
||||
|
||||
PackedByteArray bytes = p_string.to_utf8_buffer();
|
||||
|
||||
return create_image_from_utf8_buffer(p_image, bytes, p_scale, p_upsample);
|
||||
}
|
||||
|
||||
void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const {
|
||||
p_extensions->push_back("svg");
|
||||
}
|
||||
|
@ -41,6 +41,7 @@ class ImageLoaderSVG : public ImageFormatLoader {
|
||||
public:
|
||||
static void set_forced_color_map(const HashMap<Color, Color> &p_color_map);
|
||||
|
||||
Error create_image_from_utf8_buffer(Ref<Image> p_image, const PackedByteArray &p_buffer, float p_scale, bool p_upsample);
|
||||
Error create_image_from_string(Ref<Image> p_image, String p_string, float p_scale, bool p_upsample, const HashMap<Color, Color> &p_color_map);
|
||||
|
||||
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) override;
|
||||
|
Loading…
Reference in New Issue
Block a user