GeometryUtilities.cpp 7.6 KB

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