LayoutBlock.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/CSS/StyleResolver.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/Layout/LayoutBlock.h>
  5. #include <LibHTML/Layout/LayoutInline.h>
  6. #include <LibHTML/Layout/LayoutReplaced.h>
  7. #include <LibHTML/Layout/LayoutText.h>
  8. #include <math.h>
  9. LayoutBlock::LayoutBlock(const Node* node, NonnullRefPtr<StyleProperties> style)
  10. : LayoutBox(node, move(style))
  11. {
  12. }
  13. LayoutBlock::~LayoutBlock()
  14. {
  15. }
  16. LayoutNode& LayoutBlock::inline_wrapper()
  17. {
  18. if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
  19. append_child(adopt(*new LayoutBlock(nullptr, style_for_anonymous_block())));
  20. last_child()->set_children_are_inline(true);
  21. }
  22. return *last_child();
  23. }
  24. void LayoutBlock::layout()
  25. {
  26. compute_width();
  27. compute_position();
  28. if (children_are_inline())
  29. layout_inline_children();
  30. else
  31. layout_block_children();
  32. compute_height();
  33. }
  34. void LayoutBlock::layout_block_children()
  35. {
  36. ASSERT(!children_are_inline());
  37. float content_height = 0;
  38. for_each_child([&](auto& child) {
  39. // FIXME: What should we do here? Something like a <table> might have a bunch of useless text children..
  40. if (child.is_inline())
  41. return;
  42. auto& child_block = static_cast<LayoutBlock&>(child);
  43. child_block.layout();
  44. content_height = child_block.rect().bottom() + child_block.box_model().full_margin().bottom - rect().top();
  45. });
  46. rect().set_height(content_height);
  47. }
  48. void LayoutBlock::layout_inline_children()
  49. {
  50. ASSERT(children_are_inline());
  51. m_line_boxes.clear();
  52. for_each_child([&](auto& child) {
  53. ASSERT(child.is_inline());
  54. child.split_into_lines(*this);
  55. });
  56. for (auto& line_box : m_line_boxes) {
  57. line_box.trim_trailing_whitespace();
  58. }
  59. float min_line_height = style().line_height();
  60. float line_spacing = min_line_height - style().font().glyph_height();
  61. float content_height = 0;
  62. // FIXME: This should be done by the CSS parser!
  63. CSS::ValueID text_align = CSS::ValueID::Left;
  64. auto text_align_string = style().string_or_fallback(CSS::PropertyID::TextAlign, "left");
  65. if (text_align_string == "center")
  66. text_align = CSS::ValueID::Center;
  67. else if (text_align_string == "left")
  68. text_align = CSS::ValueID::Left;
  69. else if (text_align_string == "right")
  70. text_align = CSS::ValueID::Right;
  71. else if (text_align_string == "justify")
  72. text_align = CSS::ValueID::Justify;
  73. for (auto& line_box : m_line_boxes) {
  74. float max_height = min_line_height;
  75. for (auto& fragment : line_box.fragments()) {
  76. max_height = max(max_height, fragment.rect().height());
  77. }
  78. float x_offset = x();
  79. float excess_horizontal_space = (float)width() - line_box.width();
  80. switch (text_align) {
  81. case CSS::ValueID::Center:
  82. x_offset += excess_horizontal_space / 2;
  83. break;
  84. case CSS::ValueID::Right:
  85. x_offset += excess_horizontal_space;
  86. break;
  87. case CSS::ValueID::Left:
  88. case CSS::ValueID::Justify:
  89. default:
  90. break;
  91. }
  92. float excess_horizontal_space_including_whitespace = excess_horizontal_space;
  93. int whitespace_count = 0;
  94. if (text_align == CSS::ValueID::Justify) {
  95. for (auto& fragment : line_box.fragments()) {
  96. if (fragment.is_justifiable_whitespace()) {
  97. ++whitespace_count;
  98. excess_horizontal_space_including_whitespace += fragment.rect().width();
  99. }
  100. }
  101. }
  102. float justified_space_width = whitespace_count ? (excess_horizontal_space_including_whitespace / (float)whitespace_count) : 0;
  103. for (int i = 0; i < line_box.fragments().size(); ++i) {
  104. auto& fragment = line_box.fragments()[i];
  105. // Vertically align everyone's bottom to the line.
  106. // FIXME: Support other kinds of vertical alignment.
  107. fragment.rect().set_x(roundf(x_offset + fragment.rect().x()));
  108. fragment.rect().set_y(y() + content_height + (max_height - fragment.rect().height()) - (line_spacing / 2));
  109. if (text_align == CSS::ValueID::Justify) {
  110. if (fragment.is_justifiable_whitespace()) {
  111. if (fragment.rect().width() != justified_space_width) {
  112. float diff = justified_space_width - fragment.rect().width();
  113. fragment.rect().set_width(justified_space_width);
  114. // Shift subsequent sibling fragments to the right to adjust for change in width.
  115. for (int j = i + 1; j < line_box.fragments().size(); ++j) {
  116. line_box.fragments()[j].rect().move_by(diff, 0);
  117. }
  118. }
  119. }
  120. }
  121. if (is<LayoutReplaced>(fragment.layout_node()))
  122. const_cast<LayoutReplaced&>(to<LayoutReplaced>(fragment.layout_node())).set_rect(fragment.rect());
  123. float final_line_box_width = 0;
  124. for (auto& fragment : line_box.fragments())
  125. final_line_box_width += fragment.rect().width();
  126. line_box.m_width = final_line_box_width;
  127. }
  128. content_height += max_height;
  129. }
  130. rect().set_height(content_height);
  131. }
  132. void LayoutBlock::compute_width()
  133. {
  134. auto& style = this->style();
  135. auto auto_value = Length();
  136. auto zero_value = Length(0, Length::Type::Absolute);
  137. Length margin_left;
  138. Length margin_right;
  139. Length border_left;
  140. Length border_right;
  141. Length padding_left;
  142. Length padding_right;
  143. auto try_compute_width = [&](const auto& a_width) {
  144. Length width = a_width;
  145. #ifdef HTML_DEBUG
  146. dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
  147. dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
  148. #endif
  149. margin_left = style.length_or_fallback(CSS::PropertyID::MarginLeft, zero_value);
  150. margin_right = style.length_or_fallback(CSS::PropertyID::MarginRight, zero_value);
  151. border_left = style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, zero_value);
  152. border_right = style.length_or_fallback(CSS::PropertyID::BorderRightWidth, zero_value);
  153. padding_left = style.length_or_fallback(CSS::PropertyID::PaddingLeft, zero_value);
  154. padding_right = style.length_or_fallback(CSS::PropertyID::PaddingRight, zero_value);
  155. float total_px = 0;
  156. for (auto& value : { margin_left, border_left, padding_left, width, padding_right, border_right, margin_right }) {
  157. total_px += value.to_px();
  158. }
  159. #ifdef HTML_DEBUG
  160. dbg() << "Total: " << total_px;
  161. #endif
  162. // 10.3.3 Block-level, non-replaced elements in normal flow
  163. // If 'width' is not 'auto' and 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' (plus any of 'margin-left' or 'margin-right' that are not 'auto') is larger than the width of the containing block, then any 'auto' values for 'margin-left' or 'margin-right' are, for the following rules, treated as zero.
  164. if (width.is_auto() && total_px > containing_block()->width()) {
  165. if (margin_left.is_auto())
  166. margin_left = zero_value;
  167. if (margin_right.is_auto())
  168. margin_right = zero_value;
  169. }
  170. // 10.3.3 cont'd.
  171. auto underflow_px = containing_block()->width() - total_px;
  172. if (width.is_auto()) {
  173. if (margin_left.is_auto())
  174. margin_left = zero_value;
  175. if (margin_right.is_auto())
  176. margin_right = zero_value;
  177. if (underflow_px >= 0) {
  178. width = Length(underflow_px, Length::Type::Absolute);
  179. } else {
  180. width = zero_value;
  181. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  182. }
  183. } else {
  184. if (!margin_left.is_auto() && !margin_right.is_auto()) {
  185. margin_right = Length(margin_right.to_px() + underflow_px, Length::Type::Absolute);
  186. } else if (!margin_left.is_auto() && margin_right.is_auto()) {
  187. margin_right = Length(underflow_px, Length::Type::Absolute);
  188. } else if (margin_left.is_auto() && !margin_right.is_auto()) {
  189. margin_left = Length(underflow_px, Length::Type::Absolute);
  190. } else { // margin_left.is_auto() && margin_right.is_auto()
  191. auto half_of_the_underflow = Length(underflow_px / 2, Length::Type::Absolute);
  192. margin_left = half_of_the_underflow;
  193. margin_right = half_of_the_underflow;
  194. }
  195. }
  196. return width;
  197. };
  198. auto specified_width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  199. // 1. The tentative used width is calculated (without 'min-width' and 'max-width')
  200. auto used_width = try_compute_width(specified_width);
  201. // 2. The tentative used width is greater than 'max-width', the rules above are applied again,
  202. // but this time using the computed value of 'max-width' as the computed value for 'width'.
  203. auto specified_max_width = style.length_or_fallback(CSS::PropertyID::MaxWidth, auto_value);
  204. if (!specified_max_width.is_auto()) {
  205. if (used_width.to_px() > specified_max_width.to_px()) {
  206. used_width = try_compute_width(specified_max_width);
  207. }
  208. }
  209. // 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
  210. // but this time using the value of 'min-width' as the computed value for 'width'.
  211. auto specified_min_width = style.length_or_fallback(CSS::PropertyID::MinWidth, auto_value);
  212. if (!specified_min_width.is_auto()) {
  213. if (used_width.to_px() < specified_min_width.to_px()) {
  214. used_width = try_compute_width(specified_min_width);
  215. }
  216. }
  217. rect().set_width(used_width.to_px());
  218. box_model().margin().left = margin_left;
  219. box_model().margin().right = margin_right;
  220. box_model().border().left = border_left;
  221. box_model().border().right = border_right;
  222. box_model().padding().left = padding_left;
  223. box_model().padding().right = padding_right;
  224. }
  225. void LayoutBlock::compute_position()
  226. {
  227. auto& style = this->style();
  228. auto auto_value = Length();
  229. auto zero_value = Length(0, Length::Type::Absolute);
  230. auto width = style.length_or_fallback(CSS::PropertyID::Width, auto_value);
  231. box_model().margin().top = style.length_or_fallback(CSS::PropertyID::MarginTop, zero_value);
  232. box_model().margin().bottom = style.length_or_fallback(CSS::PropertyID::MarginBottom, zero_value);
  233. box_model().border().top = style.length_or_fallback(CSS::PropertyID::BorderTopWidth, zero_value);
  234. box_model().border().bottom = style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, zero_value);
  235. box_model().padding().top = style.length_or_fallback(CSS::PropertyID::PaddingTop, zero_value);
  236. box_model().padding().bottom = style.length_or_fallback(CSS::PropertyID::PaddingBottom, zero_value);
  237. rect().set_x(containing_block()->x() + box_model().margin().left.to_px() + box_model().border().left.to_px() + box_model().padding().left.to_px());
  238. float top_border = -1;
  239. if (previous_sibling() != nullptr) {
  240. auto& previous_sibling_rect = previous_sibling()->rect();
  241. auto& previous_sibling_style = previous_sibling()->box_model();
  242. top_border = previous_sibling_rect.y() + previous_sibling_rect.height();
  243. top_border += previous_sibling_style.full_margin().bottom;
  244. } else {
  245. top_border = containing_block()->y();
  246. }
  247. rect().set_y(top_border + box_model().full_margin().top);
  248. }
  249. void LayoutBlock::compute_height()
  250. {
  251. auto& style = this->style();
  252. auto height_property = style.property(CSS::PropertyID::Height);
  253. if (!height_property.has_value())
  254. return;
  255. auto height_length = height_property.value()->to_length();
  256. if (height_length.is_absolute())
  257. rect().set_height(height_length.to_px());
  258. }
  259. void LayoutBlock::render(RenderingContext& context)
  260. {
  261. if (!is_visible())
  262. return;
  263. LayoutBox::render(context);
  264. if (children_are_inline()) {
  265. for (auto& line_box : m_line_boxes) {
  266. for (auto& fragment : line_box.fragments()) {
  267. if (context.should_show_line_box_borders())
  268. context.painter().draw_rect(enclosing_int_rect(fragment.rect()), Color::Green);
  269. fragment.render(context);
  270. }
  271. }
  272. }
  273. }
  274. HitTestResult LayoutBlock::hit_test(const Point& position) const
  275. {
  276. if (!children_are_inline())
  277. return LayoutBox::hit_test(position);
  278. HitTestResult result;
  279. for (auto& line_box : m_line_boxes) {
  280. for (auto& fragment : line_box.fragments()) {
  281. if (enclosing_int_rect(fragment.rect()).contains(position)) {
  282. return { fragment.layout_node(), fragment.text_index_at(position.x()) };
  283. }
  284. }
  285. }
  286. return {};
  287. }
  288. NonnullRefPtr<StyleProperties> LayoutBlock::style_for_anonymous_block() const
  289. {
  290. auto new_style = StyleProperties::create();
  291. style().for_each_property([&](auto property_id, auto& value) {
  292. if (StyleResolver::is_inherited_property(property_id))
  293. new_style->set_property(property_id, value);
  294. });
  295. return new_style;
  296. }
  297. LineBox& LayoutBlock::ensure_last_line_box()
  298. {
  299. if (m_line_boxes.is_empty())
  300. m_line_boxes.append(LineBox());
  301. return m_line_boxes.last();
  302. }
  303. LineBox& LayoutBlock::add_line_box()
  304. {
  305. m_line_boxes.append(LineBox());
  306. return m_line_boxes.last();
  307. }