GeometryUtilities.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 "../../Include/RmlUi/Core/GeometryUtilities.h"
  29. #include "../../Include/RmlUi/Core/Box.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/FontEngineInterface.h"
  32. #include "../../Include/RmlUi/Core/Geometry.h"
  33. #include "../../Include/RmlUi/Core/Types.h"
  34. #include "GeometryBackgroundBorder.h"
  35. namespace Rml {
  36. GeometryUtilities::GeometryUtilities() {}
  37. GeometryUtilities::~GeometryUtilities() {}
  38. void GeometryUtilities::GenerateQuad(Vertex* vertices, int* indices, Vector2f origin, Vector2f dimensions, ColourbPremultiplied colour,
  39. int index_offset)
  40. {
  41. GenerateQuad(vertices, indices, origin, dimensions, colour, Vector2f(0, 0), Vector2f(1, 1), index_offset);
  42. }
  43. void GeometryUtilities::GenerateQuad(Vertex* vertices, int* indices, Vector2f origin, Vector2f dimensions, ColourbPremultiplied colour,
  44. Vector2f top_left_texcoord, Vector2f bottom_right_texcoord, int index_offset)
  45. {
  46. vertices[0].position = origin;
  47. vertices[0].colour = colour;
  48. vertices[0].tex_coord = top_left_texcoord;
  49. vertices[1].position = Vector2f(origin.x + dimensions.x, origin.y);
  50. vertices[1].colour = colour;
  51. vertices[1].tex_coord = Vector2f(bottom_right_texcoord.x, top_left_texcoord.y);
  52. vertices[2].position = origin + dimensions;
  53. vertices[2].colour = colour;
  54. vertices[2].tex_coord = bottom_right_texcoord;
  55. vertices[3].position = Vector2f(origin.x, origin.y + dimensions.y);
  56. vertices[3].colour = colour;
  57. vertices[3].tex_coord = Vector2f(top_left_texcoord.x, bottom_right_texcoord.y);
  58. indices[0] = index_offset + 0;
  59. indices[1] = index_offset + 3;
  60. indices[2] = index_offset + 1;
  61. indices[3] = index_offset + 1;
  62. indices[4] = index_offset + 3;
  63. indices[5] = index_offset + 2;
  64. }
  65. void GeometryUtilities::GenerateLine(Geometry* geometry, Vector2f position, Vector2f size, ColourbPremultiplied color)
  66. {
  67. Math::SnapToPixelGrid(position, size);
  68. Vector<Vertex>& line_vertices = geometry->GetVertices();
  69. Vector<int>& line_indices = geometry->GetIndices();
  70. const int vertices_i0 = (int)line_vertices.size();
  71. const int indices_i0 = (int)line_indices.size();
  72. line_vertices.resize(line_vertices.size() + 4);
  73. line_indices.resize(line_indices.size() + 6);
  74. GeometryUtilities::GenerateQuad(line_vertices.data() + vertices_i0, line_indices.data() + indices_i0, position, size, color, vertices_i0);
  75. }
  76. void GeometryUtilities::GenerateBackgroundBorder(Geometry* out_geometry, const Box& box, Vector2f offset, Vector4f border_radius,
  77. ColourbPremultiplied background_color, const ColourbPremultiplied border_colors[4])
  78. {
  79. RMLUI_ASSERT(border_colors);
  80. Vector<Vertex>& vertices = out_geometry->GetVertices();
  81. Vector<int>& indices = out_geometry->GetIndices();
  82. EdgeSizes border_widths = {
  83. // TODO: Move rounding to computed values (round border only).
  84. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Top)),
  85. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Right)),
  86. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Bottom)),
  87. Math::Round(box.GetEdge(BoxArea::Border, BoxEdge::Left)),
  88. };
  89. int num_borders = 0;
  90. for (int i = 0; i < 4; i++)
  91. if (border_colors[i].alpha > 0 && border_widths[i] > 0)
  92. num_borders += 1;
  93. const Vector2f padding_size = box.GetSize(BoxArea::Padding).Round();
  94. const bool has_background = (background_color.alpha > 0 && padding_size.x > 0 && padding_size.y > 0);
  95. const bool has_border = (num_borders > 0);
  96. if (!has_background && !has_border)
  97. return;
  98. // Reserve geometry. A conservative estimate, does not take border-radii into account and assumes same-colored borders.
  99. const int estimated_num_vertices = 4 * int(has_background) + 2 * num_borders;
  100. const int estimated_num_triangles = 2 * int(has_background) + 2 * num_borders;
  101. vertices.reserve((int)vertices.size() + estimated_num_vertices);
  102. indices.reserve((int)indices.size() + 3 * estimated_num_triangles);
  103. // Generate the geometry.
  104. GeometryBackgroundBorder geometry(vertices, indices);
  105. const BorderMetrics metrics = GeometryBackgroundBorder::ComputeBorderMetrics(offset.Round(), border_widths, padding_size, border_radius);
  106. if (has_background)
  107. geometry.DrawBackground(metrics, background_color);
  108. if (has_border)
  109. geometry.DrawBorder(metrics, border_widths, border_colors);
  110. #if 0
  111. // Debug draw vertices
  112. if (border_radius != Vector4f(0))
  113. {
  114. const int num_vertices = (int)vertices.size();
  115. const int num_indices = (int)indices.size();
  116. vertices.resize(num_vertices + 4 * num_vertices);
  117. indices.resize(num_indices + 6 * num_indices);
  118. for (int i = 0; i < num_vertices; i++)
  119. {
  120. GeometryUtilities::GenerateQuad(vertices.data() + num_vertices + 4 * i, indices.data() + num_indices + 6 * i, vertices[i].position,
  121. Vector2f(3, 3), ColourbPremultiplied(255, 0, (i % 2) == 0 ? 0 : 255), num_vertices + 4 * i);
  122. }
  123. }
  124. #endif
  125. #ifdef RMLUI_DEBUG
  126. const int num_vertices = (int)vertices.size();
  127. for (int index : indices)
  128. {
  129. RMLUI_ASSERT(index < num_vertices);
  130. }
  131. #endif
  132. }
  133. void GeometryUtilities::GenerateBackground(Geometry* out_geometry, const Box& box, Vector2f offset, Vector4f border_radius,
  134. ColourbPremultiplied color, BoxArea fill_area)
  135. {
  136. RMLUI_ASSERTMSG(fill_area >= BoxArea::Border && fill_area <= BoxArea::Content,
  137. "Rectangle geometry only supports border, padding and content boxes.");
  138. EdgeSizes edge_sizes = {};
  139. for (int area = (int)BoxArea::Border; area < (int)fill_area; area++)
  140. {
  141. // TODO: Move rounding to computed values (round border only).
  142. edge_sizes[0] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Top));
  143. edge_sizes[1] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Right));
  144. edge_sizes[2] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Bottom));
  145. edge_sizes[3] += Math::Round(box.GetEdge(BoxArea(area), BoxEdge::Left));
  146. }
  147. const Vector2f inner_size = box.GetSize(fill_area).Round();
  148. const bool has_background = (color.alpha > 0 && inner_size.x > 0 && inner_size.y > 0);
  149. if (!has_background)
  150. return;
  151. const BorderMetrics metrics = GeometryBackgroundBorder::ComputeBorderMetrics(offset.Round(), edge_sizes, inner_size, border_radius);
  152. Vector<Vertex>& vertices = out_geometry->GetVertices();
  153. Vector<int>& indices = out_geometry->GetIndices();
  154. // Reserve geometry. A conservative estimate, does not take border-radii into account.
  155. vertices.reserve((int)vertices.size() + 4);
  156. indices.reserve((int)indices.size() + 6);
  157. // Generate the geometry
  158. GeometryBackgroundBorder geometry(vertices, indices);
  159. geometry.DrawBackground(metrics, color);
  160. }
  161. } // namespace Rml