TableGrid.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2023, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <LibWeb/Layout/Box.h>
  9. namespace Web::Layout {
  10. class TableGrid {
  11. public:
  12. struct GridPosition {
  13. size_t x;
  14. size_t y;
  15. inline bool operator==(GridPosition const&) const = default;
  16. };
  17. struct Row {
  18. JS::NonnullGCPtr<Box const> box;
  19. CSSPixels base_height { 0 };
  20. CSSPixels reference_height { 0 };
  21. CSSPixels final_height { 0 };
  22. CSSPixels baseline { 0 };
  23. CSSPixels min_size { 0 };
  24. CSSPixels max_size { 0 };
  25. bool has_intrinsic_percentage { false };
  26. double intrinsic_percentage { 0 };
  27. // Store whether the row is constrained: https://www.w3.org/TR/css-tables-3/#constrainedness
  28. bool is_constrained { false };
  29. };
  30. struct Cell {
  31. JS::NonnullGCPtr<Box const> box;
  32. size_t column_index;
  33. size_t row_index;
  34. size_t column_span;
  35. size_t row_span;
  36. CSSPixels baseline { 0 };
  37. CSSPixels outer_min_width { 0 };
  38. CSSPixels outer_max_width { 0 };
  39. CSSPixels outer_min_height { 0 };
  40. CSSPixels outer_max_height { 0 };
  41. };
  42. // Calculate and return the grid and also rows and cells as output parameters.
  43. static TableGrid calculate_row_column_grid(Box const&, Vector<Cell>&, Vector<Row>&);
  44. // Overload for callers that don't care about rows and cells (currently the layout tree builder).
  45. static TableGrid calculate_row_column_grid(Box const& box);
  46. size_t column_count() const { return m_column_count; }
  47. HashMap<GridPosition, bool> const& occupancy_grid() const { return m_occupancy_grid; }
  48. static bool is_table_row_group(Box const& box)
  49. {
  50. auto const& display = box.display();
  51. return display.is_table_row_group() || display.is_table_header_group() || display.is_table_footer_group();
  52. }
  53. static bool is_table_row(Box const& box)
  54. {
  55. return box.display().is_table_row();
  56. }
  57. static bool is_table_column_group(Box const& box)
  58. {
  59. return box.display().is_table_column_group();
  60. }
  61. template<typename Matcher, typename Callback>
  62. static void for_each_child_box_matching(Box const& parent, Matcher matcher, Callback callback)
  63. {
  64. parent.for_each_child_of_type<Box>([&](Box const& child_box) {
  65. if (matcher(child_box))
  66. callback(child_box);
  67. });
  68. }
  69. private:
  70. size_t m_column_count { 0 };
  71. HashMap<GridPosition, bool> m_occupancy_grid;
  72. };
  73. }
  74. namespace AK {
  75. template<>
  76. struct Traits<Web::Layout::TableGrid::GridPosition> : public DefaultTraits<Web::Layout::TableGrid::GridPosition> {
  77. static unsigned hash(Web::Layout::TableGrid::GridPosition const& key)
  78. {
  79. return pair_int_hash(key.x, key.y);
  80. }
  81. };
  82. }