image_loader_svg.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* image_loader_svg.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "core/os/os.h"
  32. #include "core/print_string.h"
  33. #include "core/ustring.h"
  34. 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) {
  35. nsvgRasterize(rasterizer, p_image, p_tx, p_ty, p_scale, p_dst, p_w, p_h, p_stride);
  36. }
  37. SVGRasterizer::SVGRasterizer() {
  38. rasterizer = nsvgCreateRasterizer();
  39. }
  40. SVGRasterizer::~SVGRasterizer() {
  41. nsvgDeleteRasterizer(rasterizer);
  42. }
  43. SVGRasterizer ImageLoaderSVG::rasterizer;
  44. inline void change_nsvg_paint_color(NSVGpaint *p_paint, const uint32_t p_old, const uint32_t p_new) {
  45. if (p_paint->type == NSVG_PAINT_COLOR) {
  46. if (p_paint->color << 8 == p_old << 8) {
  47. p_paint->color = (p_paint->color & 0xFF000000) | (p_new & 0x00FFFFFF);
  48. }
  49. }
  50. if (p_paint->type == NSVG_PAINT_LINEAR_GRADIENT || p_paint->type == NSVG_PAINT_RADIAL_GRADIENT) {
  51. for (int stop_index = 0; stop_index < p_paint->gradient->nstops; stop_index++) {
  52. if (p_paint->gradient->stops[stop_index].color << 8 == p_old << 8) {
  53. p_paint->gradient->stops[stop_index].color = p_new;
  54. }
  55. }
  56. }
  57. }
  58. void ImageLoaderSVG::_convert_colors(NSVGimage *p_svg_image) {
  59. for (NSVGshape *shape = p_svg_image->shapes; shape != NULL; shape = shape->next) {
  60. for (int i = 0; i < replace_colors.old_colors.size(); i++) {
  61. change_nsvg_paint_color(&(shape->stroke), replace_colors.old_colors[i], replace_colors.new_colors[i]);
  62. change_nsvg_paint_color(&(shape->fill), replace_colors.old_colors[i], replace_colors.new_colors[i]);
  63. }
  64. }
  65. }
  66. void ImageLoaderSVG::set_convert_colors(Dictionary *p_replace_color) {
  67. if (p_replace_color) {
  68. Dictionary replace_color = *p_replace_color;
  69. for (int i = 0; i < replace_color.keys().size(); i++) {
  70. Variant o_c = replace_color.keys()[i];
  71. Variant n_c = replace_color[replace_color.keys()[i]];
  72. if (o_c.get_type() == Variant::COLOR && n_c.get_type() == Variant::COLOR) {
  73. Color old_color = o_c;
  74. Color new_color = n_c;
  75. replace_colors.old_colors.push_back(old_color.to_abgr32());
  76. replace_colors.new_colors.push_back(new_color.to_abgr32());
  77. }
  78. }
  79. } else {
  80. replace_colors.old_colors.clear();
  81. replace_colors.new_colors.clear();
  82. }
  83. }
  84. Error ImageLoaderSVG::_create_image(Ref<Image> p_image, const PoolVector<uint8_t> *p_data, float p_scale, bool upsample, bool convert_colors) {
  85. NSVGimage *svg_image;
  86. PoolVector<uint8_t>::Read src_r = p_data->read();
  87. svg_image = nsvgParse((char *)src_r.ptr(), "px", 96);
  88. if (svg_image == NULL) {
  89. ERR_PRINT("SVG Corrupted");
  90. return ERR_FILE_CORRUPT;
  91. }
  92. if (convert_colors)
  93. _convert_colors(svg_image);
  94. float upscale = upsample ? 2.0 : 1.0;
  95. int w = (int)(svg_image->width * p_scale * upscale);
  96. int h = (int)(svg_image->height * p_scale * upscale);
  97. PoolVector<uint8_t> dst_image;
  98. dst_image.resize(w * h * 4);
  99. PoolVector<uint8_t>::Write dw = dst_image.write();
  100. rasterizer.rasterize(svg_image, 0, 0, p_scale * upscale, (unsigned char *)dw.ptr(), w, h, w * 4);
  101. dw = PoolVector<uint8_t>::Write();
  102. p_image->create(w, h, false, Image::FORMAT_RGBA8, dst_image);
  103. if (upsample)
  104. p_image->shrink_x2();
  105. nsvgDelete(svg_image);
  106. return OK;
  107. }
  108. Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *p_svg_str, float p_scale, bool upsample, bool convert_colors) {
  109. size_t str_len = strlen(p_svg_str);
  110. PoolVector<uint8_t> src_data;
  111. src_data.resize(str_len + 1);
  112. PoolVector<uint8_t>::Write src_w = src_data.write();
  113. memcpy(src_w.ptr(), p_svg_str, str_len + 1);
  114. return _create_image(p_image, &src_data, p_scale, upsample, convert_colors);
  115. }
  116. Error ImageLoaderSVG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  117. uint32_t size = f->get_len();
  118. PoolVector<uint8_t> src_image;
  119. src_image.resize(size + 1);
  120. PoolVector<uint8_t>::Write src_w = src_image.write();
  121. f->get_buffer(src_w.ptr(), size);
  122. src_w.ptr()[size] = '\0';
  123. return _create_image(p_image, &src_image, p_scale, 1.0);
  124. }
  125. void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const {
  126. p_extensions->push_back("svg");
  127. p_extensions->push_back("svgz");
  128. }
  129. ImageLoaderSVG::ImageLoaderSVG() {
  130. }
  131. ImageLoaderSVG::ReplaceColors ImageLoaderSVG::replace_colors;