ElementSVG.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/SVG/ElementSVG.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/ElementDocument.h"
  32. #include "../../Include/RmlUi/Core/FileInterface.h"
  33. #include "../../Include/RmlUi/Core/Math.h"
  34. #include "../../Include/RmlUi/Core/MeshUtilities.h"
  35. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  36. #include "../../Include/RmlUi/Core/RenderManager.h"
  37. #include "../../Include/RmlUi/Core/SystemInterface.h"
  38. #include <cmath>
  39. #include <lunasvg.h>
  40. #include <string.h>
  41. namespace Rml {
  42. ElementSVG::ElementSVG(const String& tag) : Element(tag) {}
  43. ElementSVG::~ElementSVG() {}
  44. bool ElementSVG::GetIntrinsicDimensions(Vector2f& dimensions, float& ratio)
  45. {
  46. EnsureSourceLoaded();
  47. dimensions = intrinsic_dimensions;
  48. if (HasAttribute("width"))
  49. {
  50. dimensions.x = GetAttribute<float>("width", -1);
  51. }
  52. if (HasAttribute("height"))
  53. {
  54. dimensions.y = GetAttribute<float>("height", -1);
  55. }
  56. if (dimensions.y > 0)
  57. ratio = dimensions.x / dimensions.y;
  58. return true;
  59. }
  60. void ElementSVG::EnsureSourceLoaded()
  61. {
  62. if (source_dirty)
  63. LoadSource();
  64. }
  65. void ElementSVG::OnRender()
  66. {
  67. if (svg_document)
  68. {
  69. if (geometry_dirty)
  70. GenerateGeometry();
  71. UpdateTexture();
  72. geometry.Render(GetAbsoluteOffset(BoxArea::Content), Texture(texture));
  73. }
  74. }
  75. void ElementSVG::OnResize()
  76. {
  77. geometry_dirty = true;
  78. texture_dirty = true;
  79. }
  80. void ElementSVG::OnAttributeChange(const ElementAttributes& changed_attributes)
  81. {
  82. Element::OnAttributeChange(changed_attributes);
  83. if (changed_attributes.count("src"))
  84. {
  85. source_dirty = true;
  86. DirtyLayout();
  87. }
  88. if (changed_attributes.find("width") != changed_attributes.end() || changed_attributes.find("height") != changed_attributes.end())
  89. {
  90. DirtyLayout();
  91. }
  92. }
  93. void ElementSVG::OnPropertyChange(const PropertyIdSet& changed_properties)
  94. {
  95. Element::OnPropertyChange(changed_properties);
  96. if (changed_properties.Contains(PropertyId::ImageColor) || changed_properties.Contains(PropertyId::Opacity))
  97. {
  98. geometry_dirty = true;
  99. }
  100. }
  101. void ElementSVG::GenerateGeometry()
  102. {
  103. const ComputedValues& computed = GetComputedValues();
  104. ColourbPremultiplied quad_colour = computed.image_color().ToPremultiplied(computed.opacity());
  105. const Vector2f render_dimensions_f = GetBox().GetSize(BoxArea::Content).Round();
  106. render_dimensions = Vector2i(render_dimensions_f);
  107. Mesh mesh = geometry.Release(Geometry::ReleaseMode::ClearMesh);
  108. MeshUtilities::GenerateQuad(mesh, Vector2f(0), render_dimensions_f, quad_colour, Vector2f(0), Vector2f(1));
  109. geometry = GetRenderManager()->MakeGeometry(std::move(mesh));
  110. geometry_dirty = false;
  111. }
  112. bool ElementSVG::LoadSource()
  113. {
  114. source_dirty = false;
  115. texture_dirty = true;
  116. intrinsic_dimensions = Vector2f{};
  117. texture = {};
  118. svg_document.reset();
  119. const String attribute_src = GetAttribute<String>("src", "");
  120. if (attribute_src.empty())
  121. return false;
  122. String path = attribute_src;
  123. String directory;
  124. if (ElementDocument* document = GetOwnerDocument())
  125. {
  126. const String document_source_url = StringUtilities::Replace(document->GetSourceURL(), '|', ':');
  127. GetSystemInterface()->JoinPath(path, document_source_url, attribute_src);
  128. GetSystemInterface()->JoinPath(directory, document_source_url, "");
  129. }
  130. String svg_data;
  131. if (path.empty() || !GetFileInterface()->LoadFile(path, svg_data))
  132. {
  133. Log::Message(Rml::Log::Type::LT_WARNING, "Could not load SVG file %s", path.c_str());
  134. return false;
  135. }
  136. // We use a reset-release approach here in case clients use a non-std unique_ptr (lunasvg uses std::unique_ptr)
  137. svg_document.reset(lunasvg::Document::loadFromData(svg_data).release());
  138. if (!svg_document)
  139. {
  140. Log::Message(Rml::Log::Type::LT_WARNING, "Could not load SVG data from file %s", path.c_str());
  141. return false;
  142. }
  143. intrinsic_dimensions.x = Math::Max(float(svg_document->width()), 1.0f);
  144. intrinsic_dimensions.y = Math::Max(float(svg_document->height()), 1.0f);
  145. return true;
  146. }
  147. void ElementSVG::UpdateTexture()
  148. {
  149. if (!svg_document || !texture_dirty)
  150. return;
  151. RenderManager* render_manager = GetRenderManager();
  152. if (!render_manager)
  153. return;
  154. // Callback for generating texture.
  155. auto texture_callback = [this](const CallbackTextureInterface& texture_interface) -> bool {
  156. RMLUI_ASSERT(svg_document);
  157. lunasvg::Bitmap bitmap = svg_document->renderToBitmap(render_dimensions.x, render_dimensions.y);
  158. if (!bitmap.valid() || !bitmap.data())
  159. {
  160. Log::Message(Rml::Log::Type::LT_WARNING, "Could not render SVG to bitmap: %s", GetAttribute<String>("src", "").c_str());
  161. return false;
  162. }
  163. // Swap red and blue channels, assuming LunaSVG v2.3.2 or newer, to convert to RmlUi's expected RGBA-ordering.
  164. const size_t bitmap_byte_size = bitmap.width() * bitmap.height() * 4;
  165. uint8_t* bitmap_data = bitmap.data();
  166. for (size_t i = 0; i < bitmap_byte_size; i += 4)
  167. std::swap(bitmap_data[i], bitmap_data[i + 2]);
  168. if (!texture_interface.GenerateTexture({reinterpret_cast<const Rml::byte*>(bitmap.data()), bitmap_byte_size}, render_dimensions))
  169. {
  170. Log::Message(Rml::Log::Type::LT_WARNING, "Could not generate texture for SVG: %s", GetAttribute<String>("src", "").c_str());
  171. return false;
  172. }
  173. return true;
  174. };
  175. texture = render_manager->MakeCallbackTexture(std::move(texture_callback));
  176. texture_dirty = false;
  177. }
  178. } // namespace Rml