InlineLevelIterator.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Font/FontVariant.h>
  7. #include <LibWeb/Layout/BreakNode.h>
  8. #include <LibWeb/Layout/InlineFormattingContext.h>
  9. #include <LibWeb/Layout/InlineLevelIterator.h>
  10. #include <LibWeb/Layout/InlineNode.h>
  11. #include <LibWeb/Layout/ListItemMarkerBox.h>
  12. #include <LibWeb/Layout/ReplacedBox.h>
  13. namespace Web::Layout {
  14. InlineLevelIterator::InlineLevelIterator(Layout::InlineFormattingContext& inline_formatting_context, Layout::LayoutState& layout_state, Layout::BlockContainer const& containing_block, LayoutState::UsedValues const& containing_block_used_values, LayoutMode layout_mode)
  15. : m_inline_formatting_context(inline_formatting_context)
  16. , m_layout_state(layout_state)
  17. , m_containing_block(containing_block)
  18. , m_containing_block_used_values(containing_block_used_values)
  19. , m_next_node(containing_block.first_child())
  20. , m_layout_mode(layout_mode)
  21. {
  22. skip_to_next();
  23. }
  24. void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const& node)
  25. {
  26. if (!m_extra_leading_metrics.has_value())
  27. m_extra_leading_metrics = ExtraBoxMetrics {};
  28. // FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..
  29. auto& used_values = m_layout_state.get_mutable(node);
  30. auto const& computed_values = node.computed_values();
  31. used_values.margin_left = computed_values.margin().left().to_px(node, m_containing_block_used_values.content_width());
  32. used_values.border_left = computed_values.border_left().width;
  33. used_values.padding_left = computed_values.padding().left().to_px(node, m_containing_block_used_values.content_width());
  34. used_values.border_top = computed_values.border_top().width;
  35. used_values.border_bottom = computed_values.border_bottom().width;
  36. used_values.padding_bottom = computed_values.padding().bottom().to_px(node, m_containing_block_used_values.content_width());
  37. used_values.padding_top = computed_values.padding().top().to_px(node, m_containing_block_used_values.content_width());
  38. m_extra_leading_metrics->margin += used_values.margin_left;
  39. m_extra_leading_metrics->border += used_values.border_left;
  40. m_extra_leading_metrics->padding += used_values.padding_left;
  41. // Now's our chance to resolve the inset properties for this node.
  42. m_inline_formatting_context.compute_inset(node, m_inline_formatting_context.content_box_rect(m_containing_block_used_values).size());
  43. m_box_model_node_stack.append(node);
  44. }
  45. void InlineLevelIterator::exit_node_with_box_model_metrics()
  46. {
  47. if (!m_extra_trailing_metrics.has_value())
  48. m_extra_trailing_metrics = ExtraBoxMetrics {};
  49. auto& node = m_box_model_node_stack.last();
  50. auto& used_values = m_layout_state.get_mutable(node);
  51. auto const& computed_values = node->computed_values();
  52. used_values.margin_right = computed_values.margin().right().to_px(node, m_containing_block_used_values.content_width());
  53. used_values.border_right = computed_values.border_right().width;
  54. used_values.padding_right = computed_values.padding().right().to_px(node, m_containing_block_used_values.content_width());
  55. m_extra_trailing_metrics->margin += used_values.margin_right;
  56. m_extra_trailing_metrics->border += used_values.border_right;
  57. m_extra_trailing_metrics->padding += used_values.padding_right;
  58. m_box_model_node_stack.take_last();
  59. }
  60. // This is similar to Layout::Node::next_in_pre_order() but will not descend into inline-block nodes.
  61. Layout::Node const* InlineLevelIterator::next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within)
  62. {
  63. if (current.first_child()
  64. && current.first_child()->display().is_inline_outside()
  65. && current.display().is_flow_inside()
  66. && !current.is_replaced_box()) {
  67. if (!current.is_box() || !static_cast<Box const&>(current).is_out_of_flow(m_inline_formatting_context))
  68. return current.first_child();
  69. }
  70. Layout::Node const* node = &current;
  71. Layout::Node const* next = nullptr;
  72. while (!(next = node->next_sibling())) {
  73. node = node->parent();
  74. // If node is the last node on the "box model node stack", pop it off.
  75. if (!m_box_model_node_stack.is_empty()
  76. && m_box_model_node_stack.last() == node) {
  77. exit_node_with_box_model_metrics();
  78. }
  79. if (!node || node == stay_within)
  80. return nullptr;
  81. }
  82. // If node is the last node on the "box model node stack", pop it off.
  83. if (!m_box_model_node_stack.is_empty()
  84. && m_box_model_node_stack.last() == node) {
  85. exit_node_with_box_model_metrics();
  86. }
  87. return next;
  88. }
  89. void InlineLevelIterator::compute_next()
  90. {
  91. if (m_next_node == nullptr)
  92. return;
  93. do {
  94. m_next_node = next_inline_node_in_pre_order(*m_next_node, m_containing_block);
  95. if (m_next_node && m_next_node->is_svg_mask_box()) {
  96. // NOTE: It is possible to encounter SVGMaskBox nodes while doing layout of formatting context established by <foreignObject> with a mask.
  97. // We should skip and let SVGFormattingContext take care of them.
  98. m_next_node = m_next_node->next_sibling();
  99. }
  100. } while (m_next_node && (!m_next_node->is_inline() && !m_next_node->is_out_of_flow(m_inline_formatting_context)));
  101. }
  102. void InlineLevelIterator::skip_to_next()
  103. {
  104. if (m_next_node
  105. && is<Layout::NodeWithStyleAndBoxModelMetrics>(*m_next_node)
  106. && m_next_node->display().is_flow_inside()
  107. && !m_next_node->is_out_of_flow(m_inline_formatting_context)
  108. && !m_next_node->is_replaced_box())
  109. enter_node_with_box_model_metrics(static_cast<Layout::NodeWithStyleAndBoxModelMetrics const&>(*m_next_node));
  110. m_current_node = m_next_node;
  111. compute_next();
  112. }
  113. Optional<InlineLevelIterator::Item> InlineLevelIterator::next()
  114. {
  115. if (m_lookahead_items.is_empty())
  116. return next_without_lookahead();
  117. return m_lookahead_items.dequeue();
  118. }
  119. CSSPixels InlineLevelIterator::next_non_whitespace_sequence_width()
  120. {
  121. CSSPixels next_width = 0;
  122. for (;;) {
  123. auto next_item_opt = next_without_lookahead();
  124. if (!next_item_opt.has_value())
  125. break;
  126. m_lookahead_items.enqueue(next_item_opt.release_value());
  127. auto& next_item = m_lookahead_items.tail();
  128. if (next_item.type == InlineLevelIterator::Item::Type::ForcedBreak)
  129. break;
  130. if (next_item.node->computed_values().white_space() != CSS::WhiteSpace::Nowrap) {
  131. if (next_item.type != InlineLevelIterator::Item::Type::Text)
  132. break;
  133. if (next_item.is_collapsible_whitespace)
  134. break;
  135. auto& next_text_node = verify_cast<Layout::TextNode>(*(next_item.node));
  136. auto next_view = next_text_node.text_for_rendering().bytes_as_string_view().substring_view(next_item.offset_in_node, next_item.length_in_node);
  137. if (next_view.is_whitespace())
  138. break;
  139. }
  140. next_width += next_item.border_box_width();
  141. }
  142. return next_width;
  143. }
  144. Gfx::GlyphRun::TextType InlineLevelIterator::resolve_text_direction_from_context()
  145. {
  146. VERIFY(m_text_node_context.has_value());
  147. Optional<Gfx::GlyphRun::TextType> next_known_direction;
  148. for (size_t i = 0;; ++i) {
  149. auto peek = m_text_node_context->chunk_iterator.peek(i);
  150. if (!peek.has_value())
  151. break;
  152. if (peek->text_type == Gfx::GlyphRun::TextType::Ltr || peek->text_type == Gfx::GlyphRun::TextType::Rtl) {
  153. next_known_direction = peek->text_type;
  154. break;
  155. }
  156. }
  157. auto last_known_direction = m_text_node_context->last_known_direction;
  158. if (last_known_direction.has_value() && next_known_direction.has_value() && *last_known_direction != *next_known_direction) {
  159. switch (m_containing_block->computed_values().direction()) {
  160. case CSS::Direction::Ltr:
  161. return Gfx::GlyphRun::TextType::Ltr;
  162. case CSS::Direction::Rtl:
  163. return Gfx::GlyphRun::TextType::Rtl;
  164. }
  165. }
  166. if (last_known_direction.has_value())
  167. return *last_known_direction;
  168. if (next_known_direction.has_value())
  169. return *next_known_direction;
  170. return Gfx::GlyphRun::TextType::ContextDependent;
  171. }
  172. HashMap<StringView, u8> InlineLevelIterator::shape_features_map() const
  173. {
  174. HashMap<StringView, u8> features;
  175. auto& computed_values = m_current_node->computed_values();
  176. // 6.4 https://drafts.csswg.org/css-fonts/#font-variant-ligatures-prop
  177. auto ligature_or_null = computed_values.font_variant_ligatures();
  178. if (ligature_or_null.has_value()) {
  179. auto ligature = ligature_or_null.release_value();
  180. if (ligature.none) {
  181. /* nothing */
  182. } else {
  183. switch (ligature.common) {
  184. case Gfx::FontVariantLigatures::Common::Common:
  185. // Enables display of common ligatures (OpenType features: liga, clig).
  186. features.set("liga"sv, 1);
  187. features.set("clig"sv, 1);
  188. break;
  189. case Gfx::FontVariantLigatures::Common::NoCommon:
  190. // Disables display of common ligatures (OpenType features: liga, clig).
  191. features.set("liga"sv, 0);
  192. features.set("clig"sv, 0);
  193. break;
  194. case Gfx::FontVariantLigatures::Common::Unset:
  195. break;
  196. }
  197. switch (ligature.discretionary) {
  198. case Gfx::FontVariantLigatures::Discretionary::Discretionary:
  199. // Enables display of discretionary ligatures (OpenType feature: dlig).
  200. features.set("dlig"sv, 1);
  201. break;
  202. case Gfx::FontVariantLigatures::Discretionary::NoDiscretionary:
  203. // Disables display of discretionary ligatures (OpenType feature: dlig).
  204. features.set("dlig"sv, 0);
  205. break;
  206. case Gfx::FontVariantLigatures::Discretionary::Unset:
  207. break;
  208. }
  209. switch (ligature.historical) {
  210. case Gfx::FontVariantLigatures::Historical::Historical:
  211. // Enables display of historical ligatures (OpenType feature: hlig).
  212. features.set("hlig"sv, 1);
  213. break;
  214. case Gfx::FontVariantLigatures::Historical::NoHistorical:
  215. // Disables display of historical ligatures (OpenType feature: hlig).
  216. features.set("hlig"sv, 0);
  217. break;
  218. case Gfx::FontVariantLigatures::Historical::Unset:
  219. break;
  220. }
  221. switch (ligature.contextual) {
  222. case Gfx::FontVariantLigatures::Contextual::Contextual:
  223. // Enables display of contextual ligatures (OpenType feature: calt).
  224. features.set("calt"sv, 1);
  225. break;
  226. case Gfx::FontVariantLigatures::Contextual::NoContextual:
  227. // Disables display of contextual ligatures (OpenType feature: calt).
  228. features.set("calt"sv, 0);
  229. break;
  230. case Gfx::FontVariantLigatures::Contextual::Unset:
  231. break;
  232. }
  233. }
  234. } else {
  235. // A value of normal specifies that common default features are enabled, as described in detail in the next section.
  236. features.set("liga"sv, 1);
  237. features.set("clig"sv, 1);
  238. }
  239. // 6.5 https://drafts.csswg.org/css-fonts/#font-variant-position-prop
  240. switch (computed_values.font_variant_position()) {
  241. case CSS::FontVariantPosition::Normal:
  242. // None of the features listed below are enabled.
  243. break;
  244. case CSS::FontVariantPosition::Sub:
  245. // Enables display of subscripts (OpenType feature: subs).
  246. features.set("subs"sv, 1);
  247. break;
  248. case CSS::FontVariantPosition::Super:
  249. // Enables display of superscripts (OpenType feature: sups).
  250. features.set("sups"sv, 1);
  251. break;
  252. default:
  253. break;
  254. }
  255. // 6.6 https://drafts.csswg.org/css-fonts/#font-variant-caps-prop
  256. switch (computed_values.font_variant_caps()) {
  257. case CSS::FontVariantCaps::Normal:
  258. // None of the features listed below are enabled.
  259. break;
  260. case CSS::FontVariantCaps::SmallCaps:
  261. // Enables display of small capitals (OpenType feature: smcp). Small-caps glyphs typically use the form of uppercase letters but are reduced to the size of lowercase letters.
  262. features.set("smcp"sv, 1);
  263. break;
  264. case CSS::FontVariantCaps::AllSmallCaps:
  265. // Enables display of small capitals for both upper and lowercase letters (OpenType features: c2sc, smcp).
  266. features.set("c2sc"sv, 1);
  267. features.set("smcp"sv, 1);
  268. break;
  269. case CSS::FontVariantCaps::PetiteCaps:
  270. // Enables display of petite capitals (OpenType feature: pcap).
  271. features.set("pcap"sv, 1);
  272. break;
  273. case CSS::FontVariantCaps::AllPetiteCaps:
  274. // Enables display of petite capitals for both upper and lowercase letters (OpenType features: c2pc, pcap).
  275. features.set("c2pc"sv, 1);
  276. features.set("pcap"sv, 1);
  277. break;
  278. case CSS::FontVariantCaps::Unicase:
  279. // Enables display of mixture of small capitals for uppercase letters with normal lowercase letters (OpenType feature: unic).
  280. features.set("unic"sv, 1);
  281. break;
  282. case CSS::FontVariantCaps::TitlingCaps:
  283. // Enables display of titling capitals (OpenType feature: titl).
  284. features.set("titl"sv, 1);
  285. break;
  286. default:
  287. break;
  288. }
  289. // 6.7 https://drafts.csswg.org/css-fonts/#font-variant-numeric-prop
  290. auto numeric_or_null = computed_values.font_variant_numeric();
  291. if (numeric_or_null.has_value()) {
  292. auto numeric = numeric_or_null.release_value();
  293. if (numeric.figure == Gfx::FontVariantNumeric::Figure::Oldstyle) {
  294. // Enables display of old-style numerals (OpenType feature: onum).
  295. features.set("onum"sv, 1);
  296. } else if (numeric.figure == Gfx::FontVariantNumeric::Figure::Lining) {
  297. // Enables display of lining numerals (OpenType feature: lnum).
  298. features.set("lnum"sv, 1);
  299. }
  300. if (numeric.spacing == Gfx::FontVariantNumeric::Spacing::Proportional) {
  301. // Enables display of proportional numerals (OpenType feature: pnum).
  302. features.set("pnum"sv, 1);
  303. } else if (numeric.spacing == Gfx::FontVariantNumeric::Spacing::Tabular) {
  304. // Enables display of tabular numerals (OpenType feature: tnum).
  305. features.set("tnum"sv, 1);
  306. }
  307. if (numeric.fraction == Gfx::FontVariantNumeric::Fraction::Diagonal) {
  308. // Enables display of diagonal fractions (OpenType feature: frac).
  309. features.set("frac"sv, 1);
  310. } else if (numeric.fraction == Gfx::FontVariantNumeric::Fraction::Stacked) {
  311. // Enables display of stacked fractions (OpenType feature: afrc).
  312. features.set("afrc"sv, 1);
  313. features.set("afrc"sv, 1);
  314. }
  315. if (numeric.ordinal) {
  316. // Enables display of letter forms used with ordinal numbers (OpenType feature: ordn).
  317. features.set("ordn"sv, 1);
  318. }
  319. if (numeric.slashed_zero) {
  320. // Enables display of slashed zeros (OpenType feature: zero).
  321. features.set("zero"sv, 1);
  322. }
  323. }
  324. // 6.10 https://drafts.csswg.org/css-fonts/#font-variant-east-asian-prop
  325. auto east_asian_or_null = computed_values.font_variant_east_asian();
  326. if (east_asian_or_null.has_value()) {
  327. auto east_asian = east_asian_or_null.release_value();
  328. switch (east_asian.variant) {
  329. case Gfx::FontVariantEastAsian::Variant::Jis78:
  330. // Enables display of JIS78 forms (OpenType feature: jp78).
  331. features.set("jp78"sv, 1);
  332. break;
  333. case Gfx::FontVariantEastAsian::Variant::Jis83:
  334. // Enables display of JIS83 forms (OpenType feature: jp83).
  335. features.set("jp83"sv, 1);
  336. break;
  337. case Gfx::FontVariantEastAsian::Variant::Jis90:
  338. // Enables display of JIS90 forms (OpenType feature: jp90).
  339. features.set("jp90"sv, 1);
  340. break;
  341. case Gfx::FontVariantEastAsian::Variant::Jis04:
  342. // Enables display of JIS04 forms (OpenType feature: jp04).
  343. features.set("jp04"sv, 1);
  344. break;
  345. case Gfx::FontVariantEastAsian::Variant::Simplified:
  346. // Enables display of simplified forms (OpenType feature: smpl).
  347. features.set("smpl"sv, 1);
  348. break;
  349. case Gfx::FontVariantEastAsian::Variant::Traditional:
  350. // Enables display of traditional forms (OpenType feature: trad).
  351. features.set("trad"sv, 1);
  352. break;
  353. default:
  354. break;
  355. }
  356. switch (east_asian.width) {
  357. case Gfx::FontVariantEastAsian::Width::FullWidth:
  358. // Enables display of full-width forms (OpenType feature: fwid).
  359. features.set("fwid"sv, 1);
  360. break;
  361. case Gfx::FontVariantEastAsian::Width::Proportional:
  362. // Enables display of proportional-width forms (OpenType feature: pwid).
  363. features.set("pwid"sv, 1);
  364. break;
  365. default:
  366. break;
  367. }
  368. if (east_asian.ruby) {
  369. // Enables display of ruby forms (OpenType feature: ruby).
  370. features.set("ruby"sv, 1);
  371. }
  372. }
  373. return features;
  374. }
  375. Gfx::ShapeFeatures InlineLevelIterator::create_and_merge_font_features() const
  376. {
  377. HashMap<StringView, u8> merged_features;
  378. auto& computed_values = m_inline_formatting_context.containing_block().computed_values();
  379. // https://www.w3.org/TR/css-fonts-3/#feature-precedence
  380. // FIXME 1. Font features enabled by default, including features required for a given script.
  381. // FIXME 2. If the font is defined via an @font-face rule, the font features implied by the font-feature-settings descriptor in the @font-face rule.
  382. // 3. Font features implied by the value of the ‘font-variant’ property, the related ‘font-variant’ subproperties and any other CSS property that uses OpenType features (e.g. the ‘font-kerning’ property).
  383. for (auto& it : shape_features_map()) {
  384. merged_features.set(it.key, it.value);
  385. }
  386. // FIXME 4. Feature settings determined by properties other than ‘font-variant’ or ‘font-feature-settings’. For example, setting a non-default value for the ‘letter-spacing’ property disables common ligatures.
  387. // 5. Font features implied by the value of ‘font-feature-settings’ property.
  388. auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(*m_current_node.ptr());
  389. auto font_feature_settings = computed_values.font_feature_settings();
  390. if (font_feature_settings.has_value()) {
  391. auto const& feature_settings = font_feature_settings.value();
  392. for (auto const& [key, feature_value] : feature_settings) {
  393. merged_features.set(key, feature_value.resolved(resolution_context));
  394. }
  395. }
  396. Gfx::ShapeFeatures shape_features;
  397. shape_features.ensure_capacity(merged_features.size());
  398. for (auto& it : merged_features) {
  399. shape_features.append({ { it.key[0], it.key[1], it.key[2], it.key[3] }, static_cast<u32>(it.value) });
  400. }
  401. return shape_features;
  402. }
  403. Optional<InlineLevelIterator::Item> InlineLevelIterator::next_without_lookahead()
  404. {
  405. if (!m_current_node)
  406. return {};
  407. if (is<Layout::TextNode>(*m_current_node)) {
  408. auto& text_node = static_cast<Layout::TextNode const&>(*m_current_node);
  409. if (!m_text_node_context.has_value())
  410. enter_text_node(text_node);
  411. auto chunk_opt = m_text_node_context->chunk_iterator.next();
  412. if (!chunk_opt.has_value()) {
  413. m_text_node_context = {};
  414. skip_to_next();
  415. return next_without_lookahead();
  416. }
  417. if (!m_text_node_context->chunk_iterator.peek(0).has_value())
  418. m_text_node_context->is_last_chunk = true;
  419. auto& chunk = chunk_opt.value();
  420. auto text_type = chunk.text_type;
  421. if (text_type == Gfx::GlyphRun::TextType::Ltr || text_type == Gfx::GlyphRun::TextType::Rtl)
  422. m_text_node_context->last_known_direction = text_type;
  423. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  424. m_text_node_context->is_last_chunk = true;
  425. if (chunk.is_all_whitespace)
  426. text_type = Gfx::GlyphRun::TextType::EndPadding;
  427. }
  428. if (text_type == Gfx::GlyphRun::TextType::ContextDependent)
  429. text_type = resolve_text_direction_from_context();
  430. if (m_text_node_context->do_respect_linebreaks && chunk.has_breaking_newline) {
  431. return Item {
  432. .type = Item::Type::ForcedBreak,
  433. };
  434. }
  435. auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(text_node);
  436. auto letter_spacing = text_node.computed_values().letter_spacing().resolved(resolution_context).to_px(text_node);
  437. auto word_spacing = text_node.computed_values().word_spacing().resolved(resolution_context).to_px(text_node);
  438. auto x = 0.0f;
  439. if (chunk.has_breaking_tab) {
  440. CSSPixels accumulated_width;
  441. // make sure to account for any fragments that take up a portion of the measured tab stop distance
  442. auto fragments = m_containing_block_used_values.line_boxes.last().fragments();
  443. for (auto const& frag : fragments) {
  444. accumulated_width += frag.width();
  445. }
  446. // https://drafts.csswg.org/css-text/#tab-size-property
  447. auto tab_size = text_node.computed_values().tab_size();
  448. auto resolution_context = CSS::Length::ResolutionContext::for_layout_node(text_node);
  449. CSSPixels tab_width;
  450. tab_width = tab_size.visit(
  451. [&](CSS::LengthOrCalculated const& t) -> CSSPixels {
  452. auto value = t.resolved(resolution_context);
  453. return value.to_px(text_node);
  454. },
  455. [&](CSS::NumberOrCalculated const& n) -> CSSPixels {
  456. auto tab_number = n.resolved(text_node);
  457. return CSSPixels::nearest_value_for(tab_number * (chunk.font->glyph_width(' ') + word_spacing.to_float() + letter_spacing.to_float()));
  458. });
  459. // https://drafts.csswg.org/css-text/#white-space-phase-2
  460. // if fragments have added to the width, calculate the net distance to the next tab stop, otherwise the shift will just be the tab width
  461. auto tab_stop_dist = accumulated_width > 0 ? (ceil((accumulated_width / tab_width)) * tab_width) - accumulated_width : tab_width;
  462. auto ch_width = chunk.font->glyph_width('0');
  463. // If this distance is less than 0.5ch, then the subsequent tab stop is used instead
  464. if (tab_stop_dist < ch_width * 0.5)
  465. tab_stop_dist += tab_width;
  466. // account for consecutive tabs
  467. auto num_of_tabs = 0;
  468. for (auto code_point : chunk.view) {
  469. if (code_point != '\t')
  470. break;
  471. num_of_tabs++;
  472. }
  473. tab_stop_dist = tab_stop_dist * num_of_tabs;
  474. // remove tabs, we don't want to render them when we shape the text
  475. chunk.view = chunk.view.substring_view(num_of_tabs);
  476. x = tab_stop_dist.to_float();
  477. }
  478. auto shape_features = create_and_merge_font_features();
  479. auto glyph_run = Gfx::shape_text({ x, 0 }, letter_spacing.to_float(), chunk.view, chunk.font, text_type, shape_features);
  480. CSSPixels chunk_width = CSSPixels::nearest_value_for(glyph_run->width());
  481. // NOTE: We never consider `content: ""` to be collapsible whitespace.
  482. bool is_generated_empty_string = text_node.is_generated() && chunk.length == 0;
  483. Item item {
  484. .type = Item::Type::Text,
  485. .node = &text_node,
  486. .glyph_run = move(glyph_run),
  487. .offset_in_node = chunk.start,
  488. .length_in_node = chunk.length,
  489. .width = chunk_width,
  490. .is_collapsible_whitespace = m_text_node_context->do_collapse && chunk.is_all_whitespace && !is_generated_empty_string,
  491. };
  492. add_extra_box_model_metrics_to_item(item, m_text_node_context->is_first_chunk, m_text_node_context->is_last_chunk);
  493. return item;
  494. }
  495. if (m_current_node->is_absolutely_positioned()) {
  496. auto& node = *m_current_node;
  497. skip_to_next();
  498. return Item {
  499. .type = Item::Type::AbsolutelyPositionedElement,
  500. .node = &node,
  501. };
  502. }
  503. if (m_current_node->is_floating()) {
  504. auto& node = *m_current_node;
  505. skip_to_next();
  506. return Item {
  507. .type = Item::Type::FloatingElement,
  508. .node = &node,
  509. };
  510. }
  511. if (is<Layout::BreakNode>(*m_current_node)) {
  512. auto& node = *m_current_node;
  513. skip_to_next();
  514. return Item {
  515. .type = Item::Type::ForcedBreak,
  516. .node = &node,
  517. };
  518. }
  519. if (is<Layout::ListItemMarkerBox>(*m_current_node)) {
  520. skip_to_next();
  521. return next_without_lookahead();
  522. }
  523. if (!is<Layout::Box>(*m_current_node)) {
  524. skip_to_next();
  525. return next_without_lookahead();
  526. }
  527. if (is<Layout::ReplacedBox>(*m_current_node)) {
  528. auto& replaced_box = static_cast<Layout::ReplacedBox const&>(*m_current_node);
  529. // FIXME: This const_cast is gross.
  530. const_cast<Layout::ReplacedBox&>(replaced_box).prepare_for_replaced_layout();
  531. }
  532. auto& box = verify_cast<Layout::Box>(*m_current_node);
  533. auto& box_state = m_layout_state.get(box);
  534. m_inline_formatting_context.dimension_box_on_line(box, m_layout_mode);
  535. skip_to_next();
  536. auto item = Item {
  537. .type = Item::Type::Element,
  538. .node = &box,
  539. .offset_in_node = 0,
  540. .length_in_node = 0,
  541. .width = box_state.content_width(),
  542. .padding_start = box_state.padding_left,
  543. .padding_end = box_state.padding_right,
  544. .border_start = box_state.border_left,
  545. .border_end = box_state.border_right,
  546. .margin_start = box_state.margin_left,
  547. .margin_end = box_state.margin_right,
  548. };
  549. add_extra_box_model_metrics_to_item(item, true, true);
  550. return item;
  551. }
  552. void InlineLevelIterator::enter_text_node(Layout::TextNode const& text_node)
  553. {
  554. bool do_collapse = true;
  555. bool do_wrap_lines = true;
  556. bool do_respect_linebreaks = false;
  557. if (text_node.computed_values().white_space() == CSS::WhiteSpace::Nowrap) {
  558. do_collapse = true;
  559. do_wrap_lines = false;
  560. do_respect_linebreaks = false;
  561. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::Pre) {
  562. do_collapse = false;
  563. do_wrap_lines = false;
  564. do_respect_linebreaks = true;
  565. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreLine) {
  566. do_collapse = true;
  567. do_wrap_lines = true;
  568. do_respect_linebreaks = true;
  569. } else if (text_node.computed_values().white_space() == CSS::WhiteSpace::PreWrap) {
  570. do_collapse = false;
  571. do_wrap_lines = true;
  572. do_respect_linebreaks = true;
  573. }
  574. if (text_node.dom_node().is_editable() && !text_node.dom_node().is_uninteresting_whitespace_node())
  575. do_collapse = false;
  576. m_text_node_context = TextNodeContext {
  577. .do_collapse = do_collapse,
  578. .do_wrap_lines = do_wrap_lines,
  579. .do_respect_linebreaks = do_respect_linebreaks,
  580. .is_first_chunk = true,
  581. .is_last_chunk = false,
  582. .chunk_iterator = TextNode::ChunkIterator { text_node, do_wrap_lines, do_respect_linebreaks },
  583. };
  584. }
  585. void InlineLevelIterator::add_extra_box_model_metrics_to_item(Item& item, bool add_leading_metrics, bool add_trailing_metrics)
  586. {
  587. if (add_leading_metrics && m_extra_leading_metrics.has_value()) {
  588. item.margin_start += m_extra_leading_metrics->margin;
  589. item.border_start += m_extra_leading_metrics->border;
  590. item.padding_start += m_extra_leading_metrics->padding;
  591. m_extra_leading_metrics = {};
  592. }
  593. if (add_trailing_metrics && m_extra_trailing_metrics.has_value()) {
  594. item.margin_end += m_extra_trailing_metrics->margin;
  595. item.border_end += m_extra_trailing_metrics->border;
  596. item.padding_end += m_extra_trailing_metrics->padding;
  597. m_extra_trailing_metrics = {};
  598. }
  599. }
  600. }