InlineFormattingContext.cpp 10 KB

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