ElementBackgroundBorder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-2023 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 "ElementBackgroundBorder.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/ComputedValues.h"
  31. #include "../../Include/RmlUi/Core/Context.h"
  32. #include "../../Include/RmlUi/Core/DecorationTypes.h"
  33. #include "../../Include/RmlUi/Core/Element.h"
  34. #include "../../Include/RmlUi/Core/GeometryUtilities.h"
  35. #include "GeometryBoxShadow.h"
  36. namespace Rml {
  37. ElementBackgroundBorder::ElementBackgroundBorder() {}
  38. void ElementBackgroundBorder::Render(Element* element)
  39. {
  40. if (background_dirty || border_dirty)
  41. {
  42. for (auto& background : backgrounds)
  43. background.second.geometry.Release(true);
  44. GenerateGeometry(element);
  45. background_dirty = false;
  46. border_dirty = false;
  47. }
  48. Geometry* shadow_geometry = GetGeometry(BackgroundType::BoxShadow);
  49. if (shadow_geometry && *shadow_geometry)
  50. shadow_geometry->Render(element->GetAbsoluteOffset(BoxArea::Border));
  51. else if (Geometry* geometry = GetGeometry(BackgroundType::BackgroundBorder))
  52. geometry->Render(element->GetAbsoluteOffset(BoxArea::Border));
  53. }
  54. void ElementBackgroundBorder::DirtyBackground()
  55. {
  56. background_dirty = true;
  57. }
  58. void ElementBackgroundBorder::DirtyBorder()
  59. {
  60. border_dirty = true;
  61. }
  62. Geometry* ElementBackgroundBorder::GetClipGeometry(Element* element, BoxArea clip_area)
  63. {
  64. BackgroundType type = {};
  65. switch (clip_area)
  66. {
  67. case Rml::BoxArea::Border: type = BackgroundType::ClipBorder; break;
  68. case Rml::BoxArea::Padding: type = BackgroundType::ClipPadding; break;
  69. case Rml::BoxArea::Content: type = BackgroundType::ClipContent; break;
  70. default: RMLUI_ERROR; return nullptr;
  71. }
  72. Geometry& geometry = GetOrCreateBackground(type).geometry;
  73. if (!geometry)
  74. {
  75. const Box& box = element->GetBox();
  76. const Vector4f border_radius = element->GetComputedValues().border_radius();
  77. GeometryUtilities::GenerateBackground(&geometry, box, {}, border_radius, ColourbPremultiplied(255), clip_area);
  78. }
  79. return &geometry;
  80. }
  81. Geometry* ElementBackgroundBorder::GetGeometry(BackgroundType type)
  82. {
  83. auto it = backgrounds.find(type);
  84. if (it != backgrounds.end())
  85. return &it->second.geometry;
  86. return nullptr;
  87. }
  88. ElementBackgroundBorder::Background& ElementBackgroundBorder::GetOrCreateBackground(BackgroundType type)
  89. {
  90. auto it = backgrounds.find(type);
  91. if (it != backgrounds.end())
  92. return it->second;
  93. return backgrounds.emplace(type, Background{}).first->second;
  94. }
  95. void ElementBackgroundBorder::GenerateGeometry(Element* element)
  96. {
  97. const ComputedValues& computed = element->GetComputedValues();
  98. const bool has_box_shadow = computed.has_box_shadow();
  99. const float opacity = computed.opacity();
  100. // Apply opacity except if we have a box shadow. In the latter case the background is rendered opaquely into the box-shadow texture, while
  101. // opacity is applied to the entire box-shadow texture when that is rendered.
  102. bool apply_opacity = (!has_box_shadow && opacity < 1.f);
  103. auto ConvertColor = [=](Colourb color) {
  104. if (apply_opacity)
  105. return color.ToPremultiplied(opacity);
  106. else
  107. return color.ToPremultiplied();
  108. };
  109. ColourbPremultiplied background_color = ConvertColor(computed.background_color());
  110. ColourbPremultiplied border_colors[4] = {
  111. ConvertColor(computed.border_top_color()),
  112. ConvertColor(computed.border_right_color()),
  113. ConvertColor(computed.border_bottom_color()),
  114. ConvertColor(computed.border_left_color()),
  115. };
  116. const Vector4f border_radius = computed.border_radius();
  117. Geometry& geometry = GetOrCreateBackground(BackgroundType::BackgroundBorder).geometry;
  118. RMLUI_ASSERT(!geometry);
  119. for (int i = 0; i < element->GetNumBoxes(); i++)
  120. {
  121. Vector2f offset;
  122. const Box& box = element->GetBox(i, offset);
  123. GeometryUtilities::GenerateBackgroundBorder(&geometry, box, offset, border_radius, background_color, border_colors);
  124. }
  125. if (has_box_shadow)
  126. {
  127. Geometry& background_border_geometry = *GetGeometry(BackgroundType::BackgroundBorder);
  128. const Property* p_box_shadow = element->GetLocalProperty(PropertyId::BoxShadow);
  129. RMLUI_ASSERT(p_box_shadow->value.GetType() == Variant::BOXSHADOWLIST);
  130. BoxShadowList shadow_list = p_box_shadow->value.Get<BoxShadowList>();
  131. // Generate the geometry for the box-shadow texture.
  132. Background& shadow_background = GetOrCreateBackground(BackgroundType::BoxShadow);
  133. Geometry& shadow_geometry = shadow_background.geometry;
  134. Texture& shadow_texture = shadow_background.texture;
  135. GeometryBoxShadow::Generate(shadow_geometry, shadow_texture, element, background_border_geometry, std::move(shadow_list), border_radius,
  136. computed.opacity());
  137. }
  138. }
  139. } // namespace Rml