LineBuilder.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/Layout/InlineFormattingContext.h>
  8. namespace Web::Layout {
  9. class LineBuilder {
  10. AK_MAKE_NONCOPYABLE(LineBuilder);
  11. AK_MAKE_NONMOVABLE(LineBuilder);
  12. public:
  13. LineBuilder(InlineFormattingContext&, LayoutState&);
  14. ~LineBuilder();
  15. void break_line(Optional<CSSPixels> next_item_width = {});
  16. void append_box(Box const&, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin);
  17. void append_text_chunk(TextNode const&, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height);
  18. // Returns whether a line break occurred.
  19. bool break_if_needed(CSSPixels next_item_width)
  20. {
  21. if (should_break(next_item_width)) {
  22. break_line(next_item_width);
  23. return true;
  24. }
  25. return false;
  26. }
  27. CSSPixels available_width_for_current_line() const { return m_available_width_for_current_line; }
  28. void update_last_line();
  29. void remove_last_line_if_empty();
  30. CSSPixels current_y() const { return m_current_y; }
  31. void set_current_y(CSSPixels y) { m_current_y = y; }
  32. void recalculate_available_space();
  33. CSSPixels y_for_float_to_be_inserted_here(Box const&);
  34. private:
  35. void begin_new_line(bool increment_y, bool is_first_break_in_sequence = true);
  36. bool should_break(CSSPixels next_item_width);
  37. LineBox& ensure_last_line_box();
  38. InlineFormattingContext& m_context;
  39. LayoutState& m_layout_state;
  40. LayoutState::UsedValues& m_containing_block_state;
  41. CSSPixels m_available_width_for_current_line { 0 };
  42. CSSPixels m_current_y { 0 };
  43. CSSPixels m_max_height_on_current_line { 0 };
  44. bool m_last_line_needs_update { false };
  45. };
  46. }