ElementSVG.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 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/Core.h"
  30. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  31. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  32. #include "../../Include/RmlUi/Core/ElementDocument.h"
  33. #include "../../Include/RmlUi/Core/SystemInterface.h"
  34. #include "../../Include/RmlUi/Core/FileInterface.h"
  35. #include "../../Include/RmlUi/Core/Math.h"
  36. #include <cmath>
  37. #include <lunasvg.h>
  38. namespace Rml {
  39. ElementSVG::ElementSVG(const String& tag) : Element(tag), geometry(this)
  40. {
  41. }
  42. ElementSVG::~ElementSVG()
  43. {
  44. }
  45. bool ElementSVG::GetIntrinsicDimensions(Vector2f& dimensions, float& ratio)
  46. {
  47. if (source_dirty)
  48. LoadSource();
  49. dimensions = intrinsic_dimensions;
  50. if (dimensions.y > 0)
  51. ratio = dimensions.x / dimensions.y;
  52. return true;
  53. }
  54. void ElementSVG::OnRender()
  55. {
  56. if (svg_document)
  57. {
  58. if (geometry_dirty)
  59. GenerateGeometry();
  60. UpdateTexture();
  61. geometry.Render(GetAbsoluteOffset(Box::CONTENT));
  62. }
  63. }
  64. void ElementSVG::OnResize()
  65. {
  66. geometry_dirty = true;
  67. texture_size_dirty = true;
  68. }
  69. void ElementSVG::OnAttributeChange(const ElementAttributes& changed_attributes)
  70. {
  71. Element::OnAttributeChange(changed_attributes);
  72. if (changed_attributes.count("src"))
  73. {
  74. source_dirty = true;
  75. DirtyLayout();
  76. }
  77. }
  78. void ElementSVG::OnPropertyChange(const PropertyIdSet& changed_properties)
  79. {
  80. Element::OnPropertyChange(changed_properties);
  81. if (changed_properties.Contains(PropertyId::ImageColor) ||
  82. changed_properties.Contains(PropertyId::Opacity)) {
  83. geometry_dirty = true;
  84. }
  85. }
  86. void ElementSVG::GenerateGeometry()
  87. {
  88. geometry.Release(true);
  89. Vector< Vertex >& vertices = geometry.GetVertices();
  90. Vector< int >& indices = geometry.GetIndices();
  91. vertices.resize(4);
  92. indices.resize(6);
  93. Vector2f texcoords[2] = {
  94. {0.0f, 0.0f},
  95. {1.0f, 1.0f}
  96. };
  97. const ComputedValues& computed = GetComputedValues();
  98. const float opacity = computed.opacity;
  99. Colourb quad_colour = computed.image_color;
  100. quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);
  101. const Vector2f render_dimensions_f = GetBox().GetSize(Box::CONTENT).Round();
  102. render_dimensions.x = int(render_dimensions_f.x);
  103. render_dimensions.y = int(render_dimensions_f.y);
  104. GeometryUtilities::GenerateQuad(&vertices[0], &indices[0], Vector2f(0, 0), render_dimensions_f, quad_colour, texcoords[0], texcoords[1]);
  105. geometry_dirty = false;
  106. }
  107. bool ElementSVG::LoadSource()
  108. {
  109. source_dirty = false;
  110. intrinsic_dimensions = Vector2f{};
  111. geometry.SetTexture(nullptr);
  112. svg_document.reset();
  113. const String attribute_src = GetAttribute<String>("src", "");
  114. if (attribute_src.empty())
  115. return false;
  116. String path = attribute_src;
  117. String directory;
  118. if (ElementDocument* document = GetOwnerDocument())
  119. {
  120. const String document_source_url = StringUtilities::Replace(document->GetSourceURL(), '|', ':');
  121. GetSystemInterface()->JoinPath(path, document_source_url, attribute_src);
  122. GetSystemInterface()->JoinPath(directory, document_source_url, "");
  123. }
  124. String svg_data;
  125. if (path.empty() || !GetFileInterface()->LoadFile(path, svg_data))
  126. {
  127. Log::Message(Rml::Log::Type::LT_WARNING, "Could not load SVG file %s", path.c_str());
  128. return false;
  129. }
  130. // We use a reset-release approach here in case clients use a non-std unique_ptr (lunasvg uses std::unique_ptr)
  131. svg_document.reset(lunasvg::Document::loadFromData(svg_data).release());
  132. if (!svg_document)
  133. {
  134. Log::Message(Rml::Log::Type::LT_WARNING, "Could not load SVG data from file %s", path.c_str());
  135. return false;
  136. }
  137. intrinsic_dimensions.x = Math::Max(float(svg_document->width()), 1.0f);
  138. intrinsic_dimensions.y = Math::Max(float(svg_document->height()), 1.0f);
  139. return true;
  140. }
  141. void ElementSVG::UpdateTexture()
  142. {
  143. if (!svg_document || !texture_size_dirty)
  144. return;
  145. // Callback for generating texture.
  146. auto p_callback = [this](const String& /*name*/, UniquePtr<const byte[]>& data, Vector2i& dimensions) -> bool {
  147. RMLUI_ASSERT(svg_document);
  148. const size_t total_bytes = 4 * render_dimensions.x * render_dimensions.y;
  149. lunasvg::Bitmap bitmap = svg_document->renderToBitmap(render_dimensions.x, render_dimensions.y);
  150. data.reset(new byte[total_bytes]);
  151. memcpy((void*)data.get(), bitmap.data(), total_bytes);
  152. dimensions = render_dimensions;
  153. return true;
  154. };
  155. texture.Set("svg", p_callback);
  156. geometry.SetTexture(&texture);
  157. texture_size_dirty = false;
  158. }
  159. } // namespace Rml