ElementSVG.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/ElementUtilities.h"
  30. #include "../../Include/RmlUi/Core/Geometry.h"
  31. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  32. #include "SVGCache.h"
  33. namespace Rml {
  34. unsigned long ElementSVG::internal_id_counter = 0;
  35. ElementSVG::ElementSVG(const String& tag) : Element(tag) {}
  36. ElementSVG::~ElementSVG()
  37. {
  38. handle.reset();
  39. }
  40. bool ElementSVG::GetIntrinsicDimensions(Vector2f& dimensions, float& ratio)
  41. {
  42. EnsureSourceLoaded();
  43. dimensions = handle ? handle->intrinsic_dimensions : Vector2f(0);
  44. if (HasAttribute("width"))
  45. {
  46. dimensions.x = GetAttribute<float>("width", 0);
  47. }
  48. if (HasAttribute("height"))
  49. {
  50. dimensions.y = GetAttribute<float>("height", 0);
  51. }
  52. if (dimensions.y > 0)
  53. ratio = dimensions.x / dimensions.y;
  54. dimensions *= ElementUtilities::GetDensityIndependentPixelRatio(this);
  55. return true;
  56. }
  57. void ElementSVG::OnRender()
  58. {
  59. EnsureSourceLoaded();
  60. if (handle)
  61. handle->geometry.Render(GetAbsoluteOffset(BoxArea::Content), handle->texture);
  62. }
  63. void ElementSVG::OnResize()
  64. {
  65. svg_dirty = true;
  66. }
  67. void ElementSVG::OnAttributeChange(const ElementAttributes& changed_attributes)
  68. {
  69. Element::OnAttributeChange(changed_attributes);
  70. if (changed_attributes.count("src") || changed_attributes.count("crop-to-content"))
  71. {
  72. svg_dirty = true;
  73. DirtyLayout();
  74. }
  75. if (changed_attributes.find("width") != changed_attributes.end() || changed_attributes.find("height") != changed_attributes.end())
  76. {
  77. DirtyLayout();
  78. }
  79. }
  80. void ElementSVG::OnPropertyChange(const PropertyIdSet& changed_properties)
  81. {
  82. Element::OnPropertyChange(changed_properties);
  83. if (changed_properties.Contains(PropertyId::ImageColor) || changed_properties.Contains(PropertyId::Opacity))
  84. {
  85. svg_dirty = true;
  86. }
  87. }
  88. void ElementSVG::GetInnerRML(String& content) const
  89. {
  90. // If the SVG is from a file source don't add anything to the content string.
  91. const auto source = GetAttribute<String>("src", "");
  92. if (!source.empty())
  93. return;
  94. content += svg_data;
  95. }
  96. void ElementSVG::SetInnerRML(const String& content)
  97. {
  98. // If the SVG is from a file source don't set the svg xml data on the element.
  99. const auto source = GetAttribute<String>("src", "");
  100. if (!source.empty())
  101. return;
  102. if (!HasAttribute("rmlui-svgdata-id"))
  103. SetAttribute("rmlui-svgdata-id", "svgdata:" + std::to_string(internal_id_counter++));
  104. svg_data = "" + content;
  105. svg_dirty = true;
  106. EnsureSourceLoaded();
  107. }
  108. void ElementSVG::EnsureSourceLoaded()
  109. {
  110. if (!svg_dirty)
  111. return;
  112. svg_dirty = false;
  113. const bool crop_to_content = HasAttribute("crop-to-content");
  114. const auto source = GetAttribute<String>("src", "");
  115. if (source.empty())
  116. {
  117. const auto source_id = GetAttribute<String>("rmlui-svgdata-id", "svgdata:undefined");
  118. if (handle)
  119. handle.reset(); // The old handle won't be re-used so clear it.
  120. // Build an svg wrapper tag, copying all but src attribute (expected attributes could be width, height, viewBox, etc.)
  121. String svg_element_source = "<svg ";
  122. ElementAttributes attrs = GetAttributes();
  123. for (auto& attr : attrs)
  124. {
  125. if (attr.first == "src")
  126. continue;
  127. svg_element_source.append(attr.first);
  128. svg_element_source.append("=\"");
  129. svg_element_source.append(StringUtilities::EncodeRml(attr.second.Get<String>()));
  130. svg_element_source.append("\" ");
  131. }
  132. svg_element_source.append(">");
  133. svg_element_source.append(svg_data);
  134. svg_element_source.append("</svg>");
  135. handle = SVG::SVGCache::GetHandle(source_id, svg_element_source, SVG::SVGCache::Data, this, crop_to_content, BoxArea::Content);
  136. }
  137. else
  138. {
  139. handle = SVG::SVGCache::GetHandle(source, source, SVG::SVGCache::File, this, crop_to_content, BoxArea::Content);
  140. }
  141. }
  142. } // namespace Rml