LayoutNG Inline Layout
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.
What is Inline Layout
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.
Current code determines inline formatting context
using slightly different criteria, but this is still to be discussed. See crbug.com/734554.
Following DOM tree is transformed to fragment tree as in the following.
- NGInlineItem (open tag, span)
- NGInlineItem (text, “Hello”)
- NGInlineItem (close tag, span)
- NGPhysicalBoxFragment (anonymous wrapper)
- NGPhysicalBoxFragment (span, may be omitted)
- NGPhysicalTextFragment (“Hello”)
Inline Layout Phases
Inline layout is performed in the following phases:
- Pre-layout converts LayoutObject tree to a concatenated string and a list of NGInlineItem.
- Line breaking breaks it into lines and produces a list of NGInlineItemResult for each line.
- Line box construction produces a fragment tree.
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
TextNodess. 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.
- Measures each item.
- Breaks text NGInlineItem into multiple NGInlineItemResult. The core logic of this part is implemented in ShapingLineBreaker.
- Computes inline size of borders/margins/paddings. The block size of borders/margins/paddings are ignored for inline non-replaced elements, but the inline size affects layout, and hence line breaking. See CSS Calculating widths and margins for more details.
- Determines which item can fit in the line.
- Determines the break opportunities between items. If an item overflows, and there were no break opportunity before it, the item before must also overflow.
ShapingLineBreaker is... TODO(kojii): fill out.
Line Box Construction
NGInlineLayoutAlgorithm::CreateLine() takes a list of NGInlineItemResult and produces NGPhysicalLineBoxFragment for each line.
Lines are then wrapped in an anonymous NGPhysicalBoxFragment so that one NGInlineNode has one corresponding fragment.
This phase consists of following sub-phases:
Bidirectional reordering: Reorder the list of NGInlineItemResult according to UAX#9 Reordering Resolved Levels. See Bidirectional text below.
After this point forward, the list of NGInlineItemResult is in visual order; which is from line-left to line-right. The block direction is still logical, but the inline direction is physical.
Create a NGPhysicalFragment for each NGInlineItemResult in visual (line-left to line-right) order, and place them into NGPhysicalLineBoxFragment.
- A text item produces a NGPhysicalTextFragment.
- An open-tag item pushes a new stack entry of NGInlineBoxState, and a close-tag item pops a stack entry. Performs operations that require the size of the inline box, or ancestor boxes. See Inline Box Tree below.
The inline size of each item was already determined by NGLineBreaker, but the inline position is recomputed because BiDi reordering may have changed it.
In block direction, NGPhysicalFragment is placed as if the baseline is at 0. This is adjusted later, possibly multiple times. See Inline Box Tree and the post-process below.
Post-process the constructed line box. This includes:
- Process all pending operations in Inline Box Tree.
- Moves the baseline to the correct position based on the height of the line box.
- Applies the CSS text-align property.
Inline Box Tree
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.
- Caches common values for an inline box. For instance, the primary font metrics do not change within an inline box.
- Computes the height of an inline box. The height of inline, non-replaced elements depends on the content area, but CSS doesn't define how to compute the content area.
- Creates NGPhysicalBoxFragments when needed. CSS doesn't define when an inline box should have a box, but existing implementations are interoperable that there should be a box when it has borders. Also, background should have a box if it is not empty. Other cases are where paint or scroller will need a box.
- Applies vertical-align to shift baselines. Some values are applicable immediately. Some values need the size of the box, or the parent box. Some values need the size of the root inline box. Depends on the value, the operation is queued to the appropriate stack entry.
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.
Box Fragments in Line Box Fragments
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
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.
Computing baselines in LayoutNG goes the following process.
- Users of baseline should request what kind and type of the baseline they need by calling NGConstraintSpaceBuilder
- Call NGLayoutInputNode
::Layout(), that calls appropriate layout algorithm.
- Each layout algorithm computes baseline according to the requests.
- Users retrieve the result by NGPhysicalBoxFragment
::Baseline(), or by higher level functions such as NGBoxFragment
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.
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
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
This is part of the Line Box Construction phase above.
Interface for Editing
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.
NGCaretNavigator provides functions for inspecting bidi levels and visual ordering of text content, and supports visual left/right caret movements in the text. See design doc for details.