image_loader_svg.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. ERR_EXPLAIN(vformat("Can't create image from SVG with scale %s, the resulting image size exceeds max width.", rtos(p_scale)));
  97. ERR_FAIL_COND_V(w > Image::MAX_WIDTH, ERR_PARAMETER_RANGE_ERROR);
  98. int h = (int)(svg_image->height * p_scale * upscale);
  99. ERR_EXPLAIN(vformat("Can't create image from SVG with scale %s, the resulting image size exceeds max height.", rtos(p_scale)));
  100. ERR_FAIL_COND_V(h > Image::MAX_HEIGHT, ERR_PARAMETER_RANGE_ERROR);
  101. PoolVector<uint8_t> dst_image;
  102. dst_image.resize(w * h * 4);
  103. PoolVector<uint8_t>::Write dw = dst_image.write();
  104. rasterizer.rasterize(svg_image, 0, 0, p_scale * upscale, (unsigned char *)dw.ptr(), w, h, w * 4);
  105. dw = PoolVector<uint8_t>::Write();
  106. p_image->create(w, h, false, Image::FORMAT_RGBA8, dst_image);
  107. if (upsample)
  108. p_image->shrink_x2();
  109. nsvgDelete(svg_image);
  110. return OK;
  111. }
  112. Error ImageLoaderSVG::create_image_from_string(Ref<Image> p_image, const char *p_svg_str, float p_scale, bool upsample, bool convert_colors) {
  113. size_t str_len = strlen(p_svg_str);
  114. PoolVector<uint8_t> src_data;
  115. src_data.resize(str_len + 1);
  116. PoolVector<uint8_t>::Write src_w = src_data.write();
  117. memcpy(src_w.ptr(), p_svg_str, str_len + 1);
  118. return _create_image(p_image, &src_data, p_scale, upsample, convert_colors);
  119. }
  120. Error ImageLoaderSVG::load_image(Ref<Image> p_image, FileAccess *f, bool p_force_linear, float p_scale) {
  121. uint32_t size = f->get_len();
  122. PoolVector<uint8_t> src_image;
  123. src_image.resize(size + 1);
  124. PoolVector<uint8_t>::Write src_w = src_image.write();
  125. f->get_buffer(src_w.ptr(), size);
  126. src_w.ptr()[size] = '\0';
  127. return _create_image(p_image, &src_image, p_scale, 1.0);
  128. }
  129. void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const {
  130. p_extensions->push_back("svg");
  131. p_extensions->push_back("svgz");
  132. }
  133. ImageLoaderSVG::ImageLoaderSVG() {
  134. }
  135. ImageLoaderSVG::ReplaceColors ImageLoaderSVG::replace_colors;