image_loader_svg.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**************************************************************************/
  2. /* image_loader_svg.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "image_loader_svg.h"
  31. #include <nanosvg.h>
  32. #include <nanosvgrast.h>
  33. void SVGRasterizer::rasterize(NSVGimage *p_image, float p_tx, float p_ty, float p_scale, unsigned char *p_dst, int p_w, int p_h, int p_stride) {
  34. nsvgRasterize(rasterizer, p_image, p_tx, p_ty, p_scale, p_dst, p_w, p_h, p_stride);
  35. }
  36. SVGRasterizer::SVGRasterizer() {
  37. rasterizer = nsvgCreateRasterizer();
  38. }
  39. SVGRasterizer::~SVGRasterizer() {
  40. nsvgDeleteRasterizer(rasterizer);
  41. }
  42. SVGRasterizer ImageLoaderSVG::rasterizer;
  43. inline void change_nsvg_paint_color(NSVGpaint *p_paint, const uint32_t p_old, const uint32_t p_new) {
  44. if (p_paint->type == NSVG_PAINT_COLOR) {
  45. if (p_paint->color << 8 == p_old << 8) {
  46. p_paint->color = (p_paint->color & 0xFF000000) | (p_new & 0x00FFFFFF);
  47. }
  48. }
  49. if (p_paint->type == NSVG_PAINT_LINEAR_GRADIENT || p_paint->type == NSVG_PAINT_RADIAL_GRADIENT) {
  50. for (int stop_index = 0; stop_index < p_paint->gradient->nstops; stop_index++) {
  51. if (p_paint->gradient->stops[stop_index].color << 8 == p_old << 8) {
  52. p_paint->gradient->stops[stop_index].color = p_new;
  53. }
  54. }
  55. }
  56. }
  57. void ImageLoaderSVG::_convert_colors(NSVGimage *p_svg_image) {
  58. for (NSVGshape *shape = p_svg_image->shapes; shape != nullptr; shape = shape->next) {
  59. for (int i = 0; i < replace_colors.old_colors.size(); i++) {
  60. change_nsvg_paint_color(&(shape->stroke), replace_colors.old_colors[i], replace_colors.new_colors[i]);
  61. change_nsvg_paint_color(&(shape->fill), replace_colors.old_colors[i], replace_colors.new_colors[i]);
  62. }
  63. }
  64. }
  65. void ImageLoaderSVG::set_convert_colors(Dictionary *p_replace_color) {
  66. if (p_replace_color) {
  67. Dictionary replace_color = *p_replace_color;
  68. for (int i = 0; i < replace_color.keys().size(); i++) {
  69. Variant o_c = replace_color.keys()[i];
  70. Variant n_c = replace_color[replace_color.keys()[i]];
  71. if (o_c.get_type() == Variant::COLOR && n_c.get_type() == Variant::COLOR) {
  72. Color old_color = o_c;
  73. Color new_color = n_c;
  74. replace_colors.old_colors.push_back(old_color.to_abgr32());
  75. replace_colors.new_colors.push_back(new_color.to_abgr32());
  76. }
  77. }
  78. } else {
  79. replace_colors.old_colors.clear();
  80. replace_colors.new_colors.clear();
  81. }
  82. }
  83. Error ImageLoaderSVG::create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample, bool convert_colors) {
  84. NSVGimage *svg_image;
  85. PoolVector<uint8_t>::Read src_r = p_data->read();
  86. svg_image = nsvgParse((char *)src_r.ptr(), "px", 96);
  87. if (svg_image == nullptr) {
  88. ERR_PRINT("SVG Corrupted");
  89. return ERR_FILE_CORRUPT;
  90. }
  91. if (convert_colors) {
  92. _convert_colors(svg_image);
  93. }
  94. const float upscale = upsample ? 2.0 : 1.0;
  95. const int w = (int)(svg_image->width * p_scale * upscale);
  96. ERR_FAIL_COND_V_MSG(w > Image::MAX_WIDTH, ERR_PARAMETER_RANGE_ERROR, vformat("Can't create image from SVG with scale %s, the resulting image size exceeds max width.", rtos(p_scale)));
  97. const int h = (int)(svg_image->height * p_scale * upscale);
  98. ERR_FAIL_COND_V_MSG(h > Image::MAX_HEIGHT, ERR_PARAMETER_RANGE_ERROR, vformat("Can't create image from SVG with scale %s, the resulting image size exceeds max height.", rtos(p_scale)));
  99. PoolVector<uint8_t> dst_image;
  100. dst_image.resize(w * h * 4);
  101. PoolVector<uint8_t>::Write dw = dst_image.write();
  102. rasterizer.rasterize(svg_image, 0, 0, p_scale * upscale, (unsigned char *)dw.ptr(), w, h, w * 4);
  103. dw.release();
  104. p_image->create(w, h, false, Image::FORMAT_RGBA8, dst_image);
  105. if (upsample) {
  106. p_image->shrink_x2();
  107. }
  108. nsvgDelete(svg_image);
  109. return OK;
  110. }
  111. Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *p_svg_str, float p_scale, bool upsample, bool convert_colors) {
  112. size_t str_len = strlen(p_svg_str);
  113. PoolVector<uint8_t> src_data;
  114. src_data.resize(str_len + 1);
  115. PoolVector<uint8_t>::Write src_w = src_data.write();
  116. memcpy(src_w.ptr(), p_svg_str, str_len + 1);
  117. return create_image(p_image, &src_data, p_scale, upsample, convert_colors);
  118. }
  119. Error ImageLoaderSVG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  120. uint64_t size = f->get_len();
  121. PoolVector<uint8_t> src_image;
  122. src_image.resize(size + 1);
  123. PoolVector<uint8_t>::Write src_w = src_image.write();
  124. f->get_buffer(src_w.ptr(), size);
  125. src_w.ptr()[size] = '\0';
  126. return create_image(p_image, &src_image, p_scale, 1.0);
  127. }
  128. void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const {
  129. p_extensions->push_back("svg");
  130. }
  131. ImageLoaderSVG::ImageLoaderSVG() {
  132. }
  133. ImageLoaderSVG::ReplaceColors ImageLoaderSVG::replace_colors;