ElementSVG.cpp 3.5 KB

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