ElementSVG.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/Geometry.h"
  30. #include "../../Include/RmlUi/Core/PropertyIdSet.h"
  31. #include "SVGCache.h"
  32. namespace Rml {
  33. ElementSVG::ElementSVG(const String& tag) : Element(tag) {}
  34. ElementSVG::~ElementSVG()
  35. {
  36. handle.reset();
  37. }
  38. bool ElementSVG::GetIntrinsicDimensions(Vector2f& dimensions, float& ratio)
  39. {
  40. UpdateCachedData();
  41. dimensions = handle ? handle->intrinsic_dimensions : Vector2f(0);
  42. if (HasAttribute("width"))
  43. {
  44. dimensions.x = GetAttribute<float>("width", 0);
  45. }
  46. if (HasAttribute("height"))
  47. {
  48. dimensions.y = GetAttribute<float>("height", 0);
  49. }
  50. if (dimensions.y > 0)
  51. ratio = dimensions.x / dimensions.y;
  52. return true;
  53. }
  54. bool ElementSVG::IsReplaced()
  55. {
  56. return true;
  57. }
  58. void ElementSVG::EnsureSourceLoaded()
  59. {
  60. UpdateCachedData();
  61. }
  62. void ElementSVG::OnRender()
  63. {
  64. UpdateCachedData();
  65. if (handle)
  66. handle->geometry.Render(GetAbsoluteOffset(BoxArea::Content), handle->texture);
  67. }
  68. void ElementSVG::OnResize()
  69. {
  70. svg_dirty = true;
  71. }
  72. void ElementSVG::OnAttributeChange(const ElementAttributes& changed_attributes)
  73. {
  74. Element::OnAttributeChange(changed_attributes);
  75. if (changed_attributes.count("src"))
  76. {
  77. svg_dirty = true;
  78. DirtyLayout();
  79. }
  80. if (changed_attributes.count("crop-to-content"))
  81. {
  82. svg_dirty = true;
  83. DirtyLayout();
  84. }
  85. if (changed_attributes.find("width") != changed_attributes.end() || changed_attributes.find("height") != changed_attributes.end())
  86. {
  87. DirtyLayout();
  88. }
  89. }
  90. void ElementSVG::OnPropertyChange(const PropertyIdSet& changed_properties)
  91. {
  92. Element::OnPropertyChange(changed_properties);
  93. if (changed_properties.Contains(PropertyId::ImageColor) || changed_properties.Contains(PropertyId::Opacity))
  94. {
  95. svg_dirty = true;
  96. }
  97. }
  98. void ElementSVG::UpdateCachedData()
  99. {
  100. if (!svg_dirty)
  101. return;
  102. svg_dirty = false;
  103. const std::string source = GetAttribute<String>("src", "");
  104. if (source.empty())
  105. {
  106. handle.reset();
  107. return;
  108. }
  109. const bool crop_to_content = HasAttribute("crop-to-content");
  110. handle = SVG::SVGCache::GetHandle(source, this, crop_to_content, BoxArea::Content);
  111. }
  112. } // namespace Rml