LineBuilder.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. enum class ForcedBreak {
  16. No,
  17. Yes,
  18. };
  19. void break_line(ForcedBreak, Optional<CSSPixels> next_item_width = {});
  20. void append_box(Box const&, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin);
  21. 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);
  22. // Returns whether a line break occurred.
  23. bool break_if_needed(CSSPixels next_item_width)
  24. {
  25. if (should_break(next_item_width)) {
  26. break_line(LineBuilder::ForcedBreak::No, next_item_width);
  27. return true;
  28. }
  29. return false;
  30. }
  31. CSSPixels available_width_for_current_line() const { return m_available_width_for_current_line; }
  32. void update_last_line();
  33. void remove_last_line_if_empty();
  34. CSSPixels current_y() const { return m_current_y; }
  35. void set_current_y(CSSPixels y) { m_current_y = y; }
  36. void recalculate_available_space();
  37. CSSPixels y_for_float_to_be_inserted_here(Box const&);
  38. private:
  39. void begin_new_line(bool increment_y, bool is_first_break_in_sequence = true);
  40. bool should_break(CSSPixels next_item_width);
  41. LineBox& ensure_last_line_box();
  42. InlineFormattingContext& m_context;
  43. LayoutState& m_layout_state;
  44. LayoutState::UsedValues& m_containing_block_state;
  45. CSSPixels m_available_width_for_current_line { 0 };
  46. CSSPixels m_current_y { 0 };
  47. CSSPixels m_max_height_on_current_line { 0 };
  48. CSSPixels m_text_indent { 0 };
  49. bool m_last_line_needs_update { false };
  50. };
  51. }