InlineFormattingContext.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/CSS/Length.h>
  27. #include <LibWeb/DOM/Node.h>
  28. #include <LibWeb/Dump.h>
  29. #include <LibWeb/Layout/BlockBox.h>
  30. #include <LibWeb/Layout/BlockFormattingContext.h>
  31. #include <LibWeb/Layout/Box.h>
  32. #include <LibWeb/Layout/InlineFormattingContext.h>
  33. #include <LibWeb/Layout/InlineNode.h>
  34. #include <LibWeb/Layout/ReplacedBox.h>
  35. namespace Web::Layout {
  36. InlineFormattingContext::InlineFormattingContext(Box& containing_block, FormattingContext* parent)
  37. : FormattingContext(containing_block, parent)
  38. {
  39. }
  40. InlineFormattingContext::~InlineFormattingContext()
  41. {
  42. }
  43. void InlineFormattingContext::run(LayoutMode layout_mode)
  44. {
  45. auto& containing_block = downcast<BlockBox>(context_box());
  46. ASSERT(containing_block.children_are_inline());
  47. containing_block.line_boxes().clear();
  48. containing_block.for_each_child([&](auto& child) {
  49. ASSERT(child.is_inline());
  50. if (child.is_absolutely_positioned())
  51. return;
  52. child.split_into_lines(containing_block, layout_mode);
  53. });
  54. for (auto& line_box : containing_block.line_boxes()) {
  55. line_box.trim_trailing_whitespace();
  56. }
  57. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  58. if (!containing_block.line_boxes().is_empty() && containing_block.line_boxes().last().fragments().is_empty())
  59. containing_block.line_boxes().take_last();
  60. auto text_align = containing_block.style().text_align();
  61. float min_line_height = containing_block.specified_style().line_height(containing_block);
  62. float line_spacing = min_line_height - containing_block.specified_style().font().glyph_height();
  63. float content_height = 0;
  64. float max_linebox_width = 0;
  65. for (auto& line_box : containing_block.line_boxes()) {
  66. float max_height = min_line_height;
  67. for (auto& fragment : line_box.fragments()) {
  68. max_height = max(max_height, fragment.height());
  69. }
  70. float x_offset = 0;
  71. float excess_horizontal_space = (float)containing_block.width() - line_box.width();
  72. switch (text_align) {
  73. case CSS::TextAlign::Center:
  74. case CSS::TextAlign::VendorSpecificCenter:
  75. x_offset += excess_horizontal_space / 2;
  76. break;
  77. case CSS::TextAlign::Right:
  78. x_offset += excess_horizontal_space;
  79. break;
  80. case CSS::TextAlign::Left:
  81. case CSS::TextAlign::Justify:
  82. default:
  83. break;
  84. }
  85. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  86. int whitespace_count = 0;
  87. if (text_align == CSS::TextAlign::Justify) {
  88. for (auto& fragment : line_box.fragments()) {
  89. if (fragment.is_justifiable_whitespace()) {
  90. ++whitespace_count;
  91. excess_horizontal_space_including_whitespace += fragment.width();
  92. }
  93. }
  94. }
  95. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  96. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  97. auto& fragment = line_box.fragments()[i];
  98. if (fragment.type() == LineBoxFragment::Type::Leading || fragment.type() == LineBoxFragment::Type::Trailing) {
  99. fragment.set_height(max_height);
  100. } else {
  101. fragment.set_height(max(min_line_height, fragment.height()));
  102. }
  103. // Vertically align everyone's bottom to the line.
  104. // FIXME: Support other kinds of vertical alignment.
  105. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) - (line_spacing / 2) });
  106. if (text_align == CSS::TextAlign::Justify
  107. && fragment.is_justifiable_whitespace()
  108. && fragment.width() != justified_space_width) {
  109. float diff = justified_space_width - fragment.width();
  110. fragment.set_width(justified_space_width);
  111. // Shift subsequent sibling fragments to the right to adjust for change in width.
  112. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  113. auto offset = line_box.fragments()[j].offset();
  114. offset.move_by(diff, 0);
  115. line_box.fragments()[j].set_offset(offset);
  116. }
  117. }
  118. if (is<Box>(fragment.layout_node()))
  119. dimension_box_on_line(downcast<Box>(fragment.layout_node()), layout_mode);
  120. float final_line_box_width = 0;
  121. for (auto& fragment : line_box.fragments())
  122. final_line_box_width += fragment.width();
  123. line_box.m_width = final_line_box_width;
  124. max_linebox_width = max(max_linebox_width, final_line_box_width);
  125. }
  126. content_height += max_height;
  127. }
  128. if (layout_mode != LayoutMode::Default) {
  129. containing_block.set_width(max_linebox_width);
  130. }
  131. containing_block.set_height(content_height);
  132. }
  133. void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode)
  134. {
  135. auto& containing_block = downcast<BlockBox>(context_box());
  136. if (box.is_replaced()) {
  137. auto& replaced = const_cast<ReplacedBox&>(downcast<ReplacedBox>(box));
  138. replaced.set_width(replaced.calculate_width());
  139. replaced.set_height(replaced.calculate_height());
  140. return;
  141. }
  142. if (box.is_inline_block()) {
  143. auto& inline_block = const_cast<BlockBox&>(downcast<BlockBox>(box));
  144. if (inline_block.style().width().is_undefined_or_auto()) {
  145. auto result = calculate_shrink_to_fit_widths(inline_block);
  146. auto margin_left = inline_block.style().margin().left.resolved(CSS::Length::make_px(0), containing_block, containing_block.width()).to_px(inline_block);
  147. auto border_left_width = inline_block.style().border_left().width;
  148. auto padding_left = inline_block.style().padding().left.resolved(CSS::Length::make_px(0), containing_block, containing_block.width()).to_px(inline_block);
  149. auto margin_right = inline_block.style().margin().right.resolved(CSS::Length::make_px(0), containing_block, containing_block.width()).to_px(inline_block);
  150. auto border_right_width = inline_block.style().border_right().width;
  151. auto padding_right = inline_block.style().padding().right.resolved(CSS::Length::make_px(0), containing_block, containing_block.width()).to_px(inline_block);
  152. auto available_width = containing_block.width()
  153. - margin_left
  154. - border_left_width
  155. - padding_left
  156. - padding_right
  157. - border_right_width
  158. - margin_right;
  159. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  160. inline_block.set_width(width);
  161. } else {
  162. inline_block.set_width(inline_block.style().width().resolved(CSS::Length::make_px(0), containing_block, containing_block.width()).to_px(inline_block));
  163. }
  164. layout_inside(inline_block, layout_mode);
  165. if (inline_block.style().height().is_undefined_or_auto()) {
  166. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  167. } else {
  168. inline_block.set_height(inline_block.style().height().resolved(CSS::Length::make_px(0), containing_block, containing_block.height()).to_px(inline_block));
  169. }
  170. return;
  171. }
  172. // Non-replaced, non-inline-block, box on a line!?
  173. // I don't think we should be here. Dump the box tree so we can take a look at it.
  174. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  175. dump_tree(box);
  176. }
  177. }