image_loader_svg.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/memory.h"
  32. #include "core/variant/variant.h"
  33. #include <thorvg.h>
  34. void ImageLoaderSVG::_replace_color_property(const HashMap<Color, Color> &p_color_map, const String &p_prefix, String &r_string) {
  35. // Replace colors in the SVG based on what is passed in `p_color_map`.
  36. // Used to change the colors of editor icons based on the used theme.
  37. // The strings being replaced are typically of the form:
  38. // fill="#5abbef"
  39. // But can also be 3-letter codes, include alpha, be "none" or a named color
  40. // string ("blue"). So we convert to Godot Color to compare with `p_color_map`.
  41. const int prefix_len = p_prefix.length();
  42. int pos = r_string.find(p_prefix);
  43. while (pos != -1) {
  44. pos += prefix_len; // Skip prefix.
  45. int end_pos = r_string.find("\"", pos);
  46. ERR_FAIL_COND_MSG(end_pos == -1, vformat("Malformed SVG string after property \"%s\".", p_prefix));
  47. const String color_code = r_string.substr(pos, end_pos - pos);
  48. if (color_code != "none" && !color_code.begins_with("url(")) {
  49. const Color color = Color(color_code); // Handles both HTML codes and named colors.
  50. if (p_color_map.has(color)) {
  51. r_string = r_string.left(pos) + "#" + p_color_map[color].to_html(false) + r_string.substr(end_pos);
  52. }
  53. }
  54. // Search for other occurrences.
  55. pos = r_string.find(p_prefix, pos);
  56. }
  57. }
  58. void 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) {
  59. ERR_FAIL_COND(Math::is_zero_approx(p_scale));
  60. if (p_color_map.size()) {
  61. _replace_color_property(p_color_map, "stop-color=\"", p_string);
  62. _replace_color_property(p_color_map, "fill=\"", p_string);
  63. _replace_color_property(p_color_map, "stroke=\"", p_string);
  64. }
  65. std::unique_ptr<tvg::Picture> picture = tvg::Picture::gen();
  66. PackedByteArray bytes = p_string.to_utf8_buffer();
  67. tvg::Result result = picture->load((const char *)bytes.ptr(), bytes.size(), "svg", true);
  68. if (result != tvg::Result::Success) {
  69. return;
  70. }
  71. float fw, fh;
  72. picture->size(&fw, &fh);
  73. uint32_t width = MIN(round(fw * p_scale), 16 * 1024);
  74. uint32_t height = MIN(round(fh * p_scale), 16 * 1024);
  75. picture->size(width, height);
  76. std::unique_ptr<tvg::SwCanvas> sw_canvas = tvg::SwCanvas::gen();
  77. // Note: memalloc here, be sure to memfree before any return.
  78. uint32_t *buffer = (uint32_t *)memalloc(sizeof(uint32_t) * width * height);
  79. tvg::Result res = sw_canvas->target(buffer, width, width, height, tvg::SwCanvas::ARGB8888_STRAIGHT);
  80. if (res != tvg::Result::Success) {
  81. memfree(buffer);
  82. ERR_FAIL_MSG("ImageLoaderSVG can't create image.");
  83. }
  84. res = sw_canvas->push(std::move(picture));
  85. if (res != tvg::Result::Success) {
  86. memfree(buffer);
  87. ERR_FAIL_MSG("ImageLoaderSVG can't create image.");
  88. }
  89. res = sw_canvas->draw();
  90. if (res != tvg::Result::Success) {
  91. memfree(buffer);
  92. ERR_FAIL_MSG("ImageLoaderSVG can't create image.");
  93. }
  94. res = sw_canvas->sync();
  95. if (res != tvg::Result::Success) {
  96. memfree(buffer);
  97. ERR_FAIL_MSG("ImageLoaderSVG can't create image.");
  98. }
  99. Vector<uint8_t> image;
  100. image.resize(width * height * sizeof(uint32_t));
  101. for (uint32_t y = 0; y < height; y++) {
  102. for (uint32_t x = 0; x < width; x++) {
  103. uint32_t n = buffer[y * width + x];
  104. const size_t offset = sizeof(uint32_t) * width * y + sizeof(uint32_t) * x;
  105. image.write[offset + 0] = (n >> 16) & 0xff;
  106. image.write[offset + 1] = (n >> 8) & 0xff;
  107. image.write[offset + 2] = n & 0xff;
  108. image.write[offset + 3] = (n >> 24) & 0xff;
  109. }
  110. }
  111. res = sw_canvas->clear(true);
  112. memfree(buffer);
  113. p_image->create(width, height, false, Image::FORMAT_RGBA8, image);
  114. }
  115. void ImageLoaderSVG::get_recognized_extensions(List<String> *p_extensions) const {
  116. p_extensions->push_back("svg");
  117. }
  118. Error ImageLoaderSVG::load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, uint32_t p_flags, float p_scale) {
  119. String svg = p_fileaccess->get_as_utf8_string();
  120. create_image_from_string(p_image, svg, p_scale, false, HashMap<Color, Color>());
  121. ERR_FAIL_COND_V(p_image->is_empty(), FAILED);
  122. if (p_flags & FLAG_FORCE_LINEAR) {
  123. p_image->srgb_to_linear();
  124. }
  125. return OK;
  126. }