ElementSVG.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. void ElementSVG::EnsureSourceLoaded()
  55. {
  56. UpdateCachedData();
  57. }
  58. void ElementSVG::OnRender()
  59. {
  60. UpdateCachedData();
  61. if (handle)
  62. handle->geometry.Render(GetAbsoluteOffset(BoxArea::Content), handle->texture);
  63. }
  64. void ElementSVG::OnResize()
  65. {
  66. svg_dirty = true;
  67. }
  68. void ElementSVG::OnAttributeChange(const ElementAttributes& changed_attributes)
  69. {
  70. Element::OnAttributeChange(changed_attributes);
  71. if (changed_attributes.count("src"))
  72. {
  73. svg_dirty = true;
  74. DirtyLayout();
  75. }
  76. if (changed_attributes.count("crop-to-content"))
  77. {
  78. svg_dirty = true;
  79. DirtyLayout();
  80. }
  81. if (changed_attributes.find("width") != changed_attributes.end() || changed_attributes.find("height") != changed_attributes.end())
  82. {
  83. DirtyLayout();
  84. }
  85. }
  86. void ElementSVG::OnPropertyChange(const PropertyIdSet& changed_properties)
  87. {
  88. Element::OnPropertyChange(changed_properties);
  89. if (changed_properties.Contains(PropertyId::ImageColor) || changed_properties.Contains(PropertyId::Opacity))
  90. {
  91. svg_dirty = true;
  92. }
  93. }
  94. void ElementSVG::UpdateCachedData()
  95. {
  96. if (!svg_dirty)
  97. return;
  98. svg_dirty = false;
  99. const std::string source = GetAttribute<String>("src", "");
  100. if (source.empty())
  101. {
  102. handle.reset();
  103. return;
  104. }
  105. const bool crop_to_content = HasAttribute("crop-to-content");
  106. handle = SVG::SVGCache::GetHandle(source, this, crop_to_content, BoxArea::Content);
  107. }
  108. } // namespace Rml