This directory contains the inline layout implementation of Blink's new layout engine “LayoutNG”.
This README can be viewed in formatted form here.
Other parts of LayoutNG is explained here.
Inline layout is one of CSS normal flow layout models.
From the CSS2 spec on inline formatting context: an inline formatting context is established by a block container box that contains no block-level boxes.
Following DOM tree is transformed to fragment tree as in the following.
Inline layout is performed in the following phases:
Phase | Input | Output |
---|---|---|
Pre-layout | LayoutObject | NGInlineItem |
Line Breaking | NGInlineItem | NGInlineItemResult |
Line box construction | NGInlineItemResult | NGLogicalLineItem |
Generate fragments | NGLogicalLineItem | NGPhysicalFragment / NGFragmentItem |
Note: There is an idea to merge NGInlineItemResult and NGLogicalLineItem, but this hasn't been happened yet.
This is similar to CSS Text Processing Order of Operations, but not exactly the same, because the spec prioritizes the simple description than being accurate.
For inline layout there is a pre-layout pass that prepares the internal data structures needed to perform line layout.
The pre-layout pass, triggered by calling NGInlineNode::PrepareLayout()
, has three separate steps or stages that are executed in order:
CollectInlines
: Performs a depth-first scan of the container collecting all non-atomic inlines and TextNodes
s. Atomic inlines are represented as a unicode object replacement character but are otherwise skipped. Each non-atomic inline and TextNodes
is fed to a NGInlineItemsBuilder instance which collects the text content for all non-atomic inlines in the container.
During this process white-space is collapsed and normalized according to CSS white-space processing rules.
The CSS text-transform is already applied in LayoutObject tree. The plan is to implement in this phase when LayoutNG builds the tree from DOM.
SegmentText
: Performs BiDi segmentation and resolution. See Bidirectional text below.
ShapeText
: Shapes the resolved BiDi runs using HarfBuzz. TODO(eae): Fill out
NGLineBreaker takes a list of NGInlineItem, measure them, break into lines, and produces a list of NGInlineItemResult for each line.
NGInlineItemResult keeps several information needed during the line box construction, such as inline size and ShapeResult, though the inline position is recomputed later because Bidirectional text may change it.
This phase:
ShapingLineBreaker is... TODO(kojii): fill out.
NGInlineLayoutAlgorithm::CreateLine()
takes a list of NGInlineItemResult and produces a list of NGLogicalLineItem.
This phase consists of following sub-phases:
Create a NGLogicalLineItem for each NGInlineItemResult and determine the positions.
The inline size of each item was already determined by NGLineBreaker, but the inline position is recomputed because BiDi reordering may change them.
In block direction, NGLogicalLineItem is placed as if the baseline is at 0. This is adjusted later, possibly multiple times, for vertical-align and the block offset of the parent inline box.
An open-tag item pushes a new stack entry of NGInlineBoxState, and a close-tag item pops a stack entry. This stack is used to determine the size of the inline box, for vertical-align, and for a few other purposes. Please see Inline Box Tree below.
Process all pending operations in Inline Box Tree.
Bidirectional reordering: Reorder the list of NGLogicalLineItem according to UAX#9 Reordering Resolved Levels. See Bidirectional text below.
After this point forward, the list of NGLogicalLineItem is in visual order; which is from line-left to line-right. The block direction is still logical, but the inline direction is physical.
Applies ellipsizing if needed.
Applies the CSS text-align property.
Moves the baseline to the correct position based on the height of the line box.
A flat list structure is suitable for many inline operations, but some operations require an inline box tree structure. A stack of NGInlineBoxState is constructed from a list of NGInlineItemResult to represent the box tree structure.
This stack:
Because of index-based operations to the list of NGInlineItemResult, the list is append-only during the process. When all operations are done, OnEndPlaceItems()
turns the list into the final fragment tree structure.
Not all inline-level boxes produces NGPhysicalBoxFragments.
NGInlineLayoutAlgorithm determines whether a NGPhysicalBoxFragment is needed or not, such as when a <span>
has borders, and calls NGInlineBoxState::SetNeedsBoxFragment()
.
Since NGPhysicalBoxFragment needs to know its children and size before creating it, NGInlineLayoutStateStack::AddBoxFragmentPlaceholder()
first creates placeholders. We then add children, and adjust positions both horizontally and vertically.
Once all children and their positions and sizes are finalized, NGInlineLayoutStateStack::CreateBoxFragments()
creates NGPhysicalBoxFragment and add children to it.
When all NGLogicalLineItems are ordered and positioned, they are converted to fragments.
Without NGFragmentItem enabled, each NGLogicalLineItem produces a NGPhysicalFragment, added to the NGPhysicalLineBoxFragment.
With NGFragmentItem enabled, each NGLogicalLineItem produces a NGFragmentItem, added to the NGFragmentItems in the containing block of the inline formatting context.
Computing baselines in LayoutNG goes the following process.
::AddBaselineRequest()
.::Layout()
, that calls appropriate layout algorithm.::Baseline()
, or by higher level functions such as NGBoxFragment::BaselineMetrics()
.Algorithms are responsible for checking NGConstraintSpace::BaselineRequests()
, computing requested baselines, and calling NGBoxFragmentBuilder::AddBaseline()
to add them to NGPhysicalBoxFragment.
NGBaselineRequest consists of NGBaselineAlgorithmType and FontBaseline.
In most normal cases, algorithms should decide which box should provide the baseline for the specified NGBaselineAlgorithmType and delegate to it.
FontBaseline currently has only two baseline types, alphabetic and ideographic, and they are hard coded to derive from the CSS writing-mode property today.
We plan to support more baseline types, and allow authors to specify the baseline type, as defined in the CSS dominant-baseline property in future. NGPhysicalLineBoxFragment and NGPhysicalTextFragment should be responsible for computing different baseline types from font metrics.
For performance and memory consumption, Blink ignores some inline-boxes during inline layout because they don't impact layout nor they are needed for paint purposes. An example of this is
<span style="border: 1px solid blue"><b>Text</b></span>
The <b>
has the same size as the inner text-node so it can be ignored for layout purpose, while the <span>
has borders, which impacts paint, so it produces a box fragment. This optimization is called “culled inline” in the code.
LayoutNG uses similar criteria as legacy engine on whether to generate a fragment or not, but LayoutNG generates fragments in a little more cases than legacy in order to improve the accuracy of hit-testing and bounding rects. This is determined by ShouldCreateBoxFragment()
, which is primarily computed during style recalc, but layout may also turn it on when the need is discovered during layout.
One downside of this optimization is that we have extra work to do when querying post-layout information; e.g., hit-testing or asking for bounding rects.
Another downside is making a culled inline box not to be culled requires re-layout even if the change does not affect layout; e.g., adding background. To mitigate multiple layouts by like hover highlighting, ShouldCreateBoxFragment()
will not be reset once it's set.
UAX#9 Unicode Bidirectional Algorithm defines processing algorithm for bidirectional text.
The core logic is implemented in NGBidiParagraph, which is a thin wrapper for ICU BiDi.
In a bird's‐eye view, it consists of two parts:
Before line breaking: Segmenting text and resolve embedding levels as defined in UAX#9 Resolving Embedding Levels.
The core logic uses ICU BiDi ubidi_getLogicalRun()
function.
This is part of the Pre-layout phase above.
After line breaking: Reorder text as defined in UAX#9 Reordering Resolved Levels.
The core logic uses ICU BiDi ubidi_reorderVisual()
function.
This is part of the Line Box Construction phase above.
Initial design doc: Using ICU BiDi in LayoutNG
NGOffsetMapping provides functions for converting between offsets in the text content of an inline formatting context (computed in pre-layout) and DOM positions in the context. See design doc for details.