InlineFormattingContext.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/InlineFormattingContext.h>
  30. #include <LibWeb/Layout/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutBox.h>
  32. #include <LibWeb/Layout/LayoutInline.h>
  33. #include <LibWeb/Layout/LayoutReplaced.h>
  34. namespace Web::Layout {
  35. InlineFormattingContext::InlineFormattingContext(LayoutBox& containing_block)
  36. : FormattingContext(containing_block)
  37. {
  38. }
  39. InlineFormattingContext::~InlineFormattingContext()
  40. {
  41. }
  42. void InlineFormattingContext::run(LayoutMode layout_mode)
  43. {
  44. auto& containing_block = downcast<LayoutBlock>(context_box());
  45. ASSERT(containing_block.children_are_inline());
  46. containing_block.line_boxes().clear();
  47. containing_block.for_each_child([&](auto& child) {
  48. ASSERT(child.is_inline());
  49. if (child.is_absolutely_positioned())
  50. return;
  51. child.split_into_lines(containing_block, layout_mode);
  52. });
  53. for (auto& line_box : containing_block.line_boxes()) {
  54. line_box.trim_trailing_whitespace();
  55. }
  56. // If there's an empty line box at the bottom, just remove it instead of giving it height.
  57. if (!containing_block.line_boxes().is_empty() && containing_block.line_boxes().last().fragments().is_empty())
  58. containing_block.line_boxes().take_last();
  59. auto text_align = containing_block.style().text_align();
  60. float min_line_height = containing_block.specified_style().line_height(containing_block);
  61. float line_spacing = min_line_height - containing_block.specified_style().font().glyph_height();
  62. float content_height = 0;
  63. float max_linebox_width = 0;
  64. for (auto& line_box : containing_block.line_boxes()) {
  65. float max_height = min_line_height;
  66. for (auto& fragment : line_box.fragments()) {
  67. max_height = max(max_height, fragment.height());
  68. }
  69. float x_offset = 0;
  70. float excess_horizontal_space = (float)containing_block.width() - line_box.width();
  71. switch (text_align) {
  72. case CSS::TextAlign::Center:
  73. case CSS::TextAlign::VendorSpecificCenter:
  74. x_offset += excess_horizontal_space / 2;
  75. break;
  76. case CSS::TextAlign::Right:
  77. x_offset += excess_horizontal_space;
  78. break;
  79. case CSS::TextAlign::Left:
  80. case CSS::TextAlign::Justify:
  81. default:
  82. break;
  83. }
  84. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  85. int whitespace_count = 0;
  86. if (text_align == CSS::TextAlign::Justify) {
  87. for (auto& fragment : line_box.fragments()) {
  88. if (fragment.is_justifiable_whitespace()) {
  89. ++whitespace_count;
  90. excess_horizontal_space_including_whitespace += fragment.width();
  91. }
  92. }
  93. }
  94. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  95. for (size_t i = 0; i < line_box.fragments().size(); ++i) {
  96. auto& fragment = line_box.fragments()[i];
  97. // Vertically align everyone's bottom to the line.
  98. // FIXME: Support other kinds of vertical alignment.
  99. fragment.set_offset({ roundf(x_offset + fragment.offset().x()), content_height + (max_height - fragment.height()) - (line_spacing / 2) });
  100. if (text_align == CSS::TextAlign::Justify
  101. && fragment.is_justifiable_whitespace()
  102. && fragment.width() != justified_space_width) {
  103. float diff = justified_space_width - fragment.width();
  104. fragment.set_width(justified_space_width);
  105. // Shift subsequent sibling fragments to the right to adjust for change in width.
  106. for (size_t j = i + 1; j < line_box.fragments().size(); ++j) {
  107. auto offset = line_box.fragments()[j].offset();
  108. offset.move_by(diff, 0);
  109. line_box.fragments()[j].set_offset(offset);
  110. }
  111. }
  112. if (fragment.layout_node().is_box())
  113. dimension_box_on_line(const_cast<LayoutBox&>(downcast<LayoutBox>(fragment.layout_node())), layout_mode);
  114. float final_line_box_width = 0;
  115. for (auto& fragment : line_box.fragments())
  116. final_line_box_width += fragment.width();
  117. line_box.m_width = final_line_box_width;
  118. max_linebox_width = max(max_linebox_width, final_line_box_width);
  119. }
  120. content_height += max_height;
  121. }
  122. if (layout_mode != LayoutMode::Default) {
  123. containing_block.set_width(max_linebox_width);
  124. }
  125. containing_block.set_height(content_height);
  126. }
  127. void InlineFormattingContext::dimension_box_on_line(LayoutBox& box, LayoutMode layout_mode)
  128. {
  129. auto& containing_block = downcast<LayoutBlock>(context_box());
  130. if (box.is_replaced()) {
  131. auto& replaced = const_cast<LayoutReplaced&>(downcast<LayoutReplaced>(box));
  132. replaced.set_width(replaced.calculate_width());
  133. replaced.set_height(replaced.calculate_height());
  134. return;
  135. }
  136. if (box.is_inline_block()) {
  137. auto& inline_block = const_cast<LayoutBlock&>(downcast<LayoutBlock>(box));
  138. if (inline_block.style().width().is_undefined_or_auto()) {
  139. auto result = calculate_shrink_to_fit_widths(inline_block);
  140. // FIXME: (10.3.5) find the available width: in this case, this is the width of the containing
  141. // block minus the used values of 'margin-left', 'border-left-width', 'padding-left',
  142. // 'padding-right', 'border-right-width', 'margin-right', and the widths of any
  143. // relevant scroll bars.
  144. auto available_width = containing_block.width();
  145. auto width = min(max(result.preferred_minimum_width, available_width), result.preferred_width);
  146. inline_block.set_width(width);
  147. } else {
  148. inline_block.set_width(inline_block.style().width().to_px(inline_block));
  149. }
  150. FormattingContext::layout_inside(inline_block, layout_mode);
  151. if (inline_block.style().height().is_undefined_or_auto()) {
  152. // FIXME: (10.6.6) If 'height' is 'auto', the height depends on the element's descendants per 10.6.7.
  153. } else {
  154. inline_block.set_height(inline_block.style().height().to_px(inline_block));
  155. }
  156. return;
  157. }
  158. // Non-replaced, non-inline-block, box on a line!?
  159. // I don't think we should be here. Dump the box tree so we can take a look at it.
  160. dbgln("FIXME: I've been asked to dimension a non-replaced, non-inline-block box on a line:");
  161. dump_tree(box);
  162. }
  163. }