TableGrid.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. template<typename Matcher, typename Callback>
  58. static void for_each_child_box_matching(Box const& parent, Matcher matcher, Callback callback)
  59. {
  60. parent.for_each_child_of_type<Box>([&](Box const& child_box) {
  61. if (matcher(child_box))
  62. callback(child_box);
  63. });
  64. }
  65. private:
  66. size_t m_column_count { 0 };
  67. HashMap<GridPosition, bool> m_occupancy_grid;
  68. };
  69. }
  70. namespace AK {
  71. template<>
  72. struct Traits<Web::Layout::TableGrid::GridPosition> : public GenericTraits<Web::Layout::TableGrid::GridPosition> {
  73. static unsigned hash(Web::Layout::TableGrid::GridPosition const& key)
  74. {
  75. return pair_int_hash(key.x, key.y);
  76. }
  77. };
  78. }