InlineFormattingContext.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. struct AvailableSpaceForLineInfo {
  44. float left { 0 };
  45. float right { 0 };
  46. };
  47. static AvailableSpaceForLineInfo available_space_for_line(const InlineFormattingContext& context, size_t line_index)
  48. {
  49. AvailableSpaceForLineInfo info;
  50. // FIXME: This is a total hack guess since we don't actually know the final y position of lines here!
  51. float line_height = context.containing_block().specified_style().line_height(context.containing_block());
  52. float y = (line_index * line_height);
  53. auto& bfc = static_cast<const BlockFormattingContext&>(*context.parent());
  54. for (ssize_t i = bfc.left_floating_boxes().size() - 1; i >= 0; --i) {
  55. auto& floating_box = *bfc.left_floating_boxes().at(i);
  56. auto rect = floating_box.margin_box_as_relative_rect();
  57. if (rect.contains_vertically(y)) {
  58. info.left = rect.right() + 1;
  59. break;
  60. }
  61. }
  62. info.right = context.containing_block().width();
  63. for (ssize_t i = bfc.right_floating_boxes().size() - 1; i >= 0; --i) {
  64. auto& floating_box = *bfc.right_floating_boxes().at(i);
  65. auto rect = floating_box.margin_box_as_relative_rect();
  66. if (rect.contains_vertically(y)) {
  67. info.right = rect.left() - 1;
  68. break;
  69. }
  70. }
  71. return info;
  72. }
  73. float InlineFormattingContext::available_width_at_line(size_t line_index) const
  74. {
  75. auto info = available_space_for_line(*this, line_index);
  76. return info.right - info.left;
  77. }
  78. void InlineFormattingContext::run(Box&, LayoutMode layout_mode)
  79. {
  80. ASSERT(containing_block().children_are_inline());
  81. containing_block().line_boxes().clear();
  82. containing_block().for_each_child([&](auto& child) {
  83. ASSERT(child.is_inline());
  84. if (child.is_absolutely_positioned())
  85. return;
  86. child.split_into_lines(*this, layout_mode);
  87. });
  88. for (auto& line_box : containing_block().line_boxes()) {
  89. line_box.trim_trailing_whitespace();
  90. }
  91. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  92. if (!containing_block().line_boxes().is_empty() && containing_block().line_boxes().last().fragments().is_empty())
  93. containing_block().line_boxes().take_last();
  94. auto text_align = containing_block().style().text_align();
  95. float min_line_height = containing_block().specified_style().line_height(containing_block());
  96. float content_height = 0;
  97. float max_linebox_width = 0;
  98. for (size_t line_index = 0; line_index < containing_block().line_boxes().size(); ++line_index) {
  99. auto& line_box = containing_block().line_boxes()[line_index];
  100. float max_height = min_line_height;
  101. for (auto& fragment : line_box.fragments()) {
  102. max_height = max(max_height, fragment.height());
  103. }
  104. float x_offset = available_space_for_line(*this, line_index).left;
  105. float excess_horizontal_space = (float)containing_block().width() - line_box.width();
  106. switch (text_align) {
  107. case CSS::TextAlign::Center:
  108. case CSS::TextAlign::VendorSpecificCenter:
  109. x_offset += excess_horizontal_space / 2;
  110. break;
  111. case CSS::TextAlign::Right:
  112. x_offset += excess_horizontal_space;
  113. break;
  114. case CSS::TextAlign::Left:
  115. case CSS::TextAlign::Justify:
  116. default:
  117. break;
  118. }
  119. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  120. int whitespace_count = 0;
  121. if (text_align == CSS::TextAlign::Justify) {
  122. for (auto& fragment : line_box.fragments()) {
  123. if (fragment.is_justifiable_whitespace()) {
  124. ++whitespace_count;
  125. excess_horizontal_space_including_whitespace += fragment.width();
  126. }
  127. }
  128. }
  129. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  130. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  131. auto& fragment = line_box.fragments()[i];
  132. if (fragment.type() == LineBoxFragment::Type::Leading || fragment.type() == LineBoxFragment::Type::Trailing) {
  133. fragment.set_height(max_height);
  134. } else {
  135. fragment.set_height(max(min_line_height, fragment.height()));
  136. }
  137. // Vertically align everyone's bottom to the line.
  138. // FIXME: Support other kinds of vertical alignment.
  139. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) });
  140. if (text_align == CSS::TextAlign::Justify
  141. && fragment.is_justifiable_whitespace()
  142. && fragment.width() != justified_space_width) {
  143. float diff = justified_space_width - fragment.width();
  144. fragment.set_width(justified_space_width);
  145. // Shift subsequent sibling fragments to the right to adjust for change in width.
  146. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  147. auto offset = line_box.fragments()[j].offset();
  148. offset.move_by(diff, 0);
  149. line_box.fragments()[j].set_offset(offset);
  150. }
  151. }
  152. }
  153. if (!line_box.fragments().is_empty()) {
  154. float left_edge = line_box.fragments().first().offset().x();
  155. float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width();
  156. float final_line_box_width = right_edge - left_edge;
  157. line_box.m_width = final_line_box_width;
  158. max_linebox_width = max(max_linebox_width, final_line_box_width);
  159. }
  160. content_height += max_height;
  161. }
  162. if (layout_mode != LayoutMode::Default) {
  163. containing_block().set_width(max_linebox_width);
  164. }
  165. containing_block().set_height(content_height);
  166. }
  167. void InlineFormattingContext::dimension_box_on_line(Box& box, LayoutMode layout_mode)
  168. {
  169. if (box.is_replaced()) {
  170. auto& replaced = downcast<ReplacedBox>(box);
  171. replaced.set_width(compute_width_for_replaced_element(replaced));
  172. replaced.set_height(compute_height_for_replaced_element(replaced));
  173. return;
  174. }
  175. if (box.is_inline_block()) {
  176. auto& inline_block = const_cast<BlockBox&>(downcast<BlockBox>(box));
  177. if (inline_block.style().width().is_undefined_or_auto()) {
  178. auto result = calculate_shrink_to_fit_widths(inline_block);
  179. auto margin_left = inline_block.style().margin().left.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  180. auto border_left_width = inline_block.style().border_left().width;
  181. auto padding_left = inline_block.style().padding().left.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  182. auto margin_right = inline_block.style().margin().right.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  183. auto border_right_width = inline_block.style().border_right().width;
  184. auto padding_right = inline_block.style().padding().right.resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block);
  185. auto available_width = containing_block().width()
  186. - margin_left
  187. - border_left_width
  188. - padding_left
  189. - padding_right
  190. - border_right_width
  191. - margin_right;
  192. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  193. inline_block.set_width(width);
  194. } else {
  195. inline_block.set_width(inline_block.style().width().resolved_or_zero(inline_block, containing_block().width()).to_px(inline_block));
  196. }
  197. layout_inside(inline_block, layout_mode);
  198. if (inline_block.style().height().is_undefined_or_auto()) {
  199. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  200. } else {
  201. inline_block.set_height(inline_block.style().height().resolved_or_zero(inline_block, containing_block().height()).to_px(inline_block));
  202. }
  203. return;
  204. }
  205. // Non-replaced, non-inline-block, box on a line!?
  206. // I don't think we should be here. Dump the box tree so we can take a look at it.
  207. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  208. dump_tree(box);
  209. }
  210. }