Sign in Join
Wiki copytemporary

Fresnel CSS Algorithm: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
1-## Contents
2-
3- * 1 Overview
4- * 2 Delayed computation
5- * 3 ApplyCSS
6- * 3.1 Collecting type stylesheets
7- * 3.2 Parsing stylesheets
8- * 3.3 Collect CSS attributes for each element
9- * 3.4 Box-Modell
10- * 3.5 Layout
11- * 3.5.1 Layout Top-Down
12- * 3.5.2 Layout Final
13- * 4 Flow Layouter
14- * 5 Flex Layouter
15- * 6 Grid Layouter
16-
171# Overview
182
193This page describes the how [[Fresnel]] parses and applies CSS, layouts and renders.
204
215# Delayed computation
226
237Whenever an element or CSS is added, removed, altered TFresnelElement.DomChanged is called. This does not compute anything immediately in contrary to LCL or VCL. Instead a change:
248
259 * calls _TFresnelCustomForm.DomChanged_ to _LayoutQueued_ to true,
2610 * the first change **queues** a call of _TFresnelCustomForm.OnQueuedLayout_.
2711 * which eventually calls _ApplyCSS_ and _Invalidate_ ,
2812 * which queues a _WSDraw_ ,
2913 * which eventually calls _Renderer.Draw_.
3014
3115Each form has its own queued call.
3216
3317If you need to compute a change immediately call _Form.ApplyCSS_.
3418
3519# ApplyCSS
3620
3721The article gives an overview of the order and how the CSS is parsed and computed in Fresnel. The central method is **TFresnelViewport.ApplyCSS**.
3822
3923## Collecting type stylesheets
4024
4125The **Viewport** gathers all used **type stylesheets** via _GetCSSTypeStyle_ and adds them to resolver. These have _origin user-agent_.
4226
4327 * E.g. if there is a _TDiv_ and a _TButton_ on the form, it will use the stylesheets returned by _TDiv.GetCSSTypeStyle_ and _TButton.GetCSSTypeStyle_.
4428 * If there is a TMyButton, with _TMyButton = class(TButton)_ , it will contain both stylesheets. This allows to create a 'button' descendant, adding the 'button' stylesheet only once.
4529
4630## Parsing stylesheets
4731
4832Each form has one **Resolver: TCSSResolver**. The resolver parses all stylesheets:
4933
5034 * Starts with the stylesheets from the used element types, e.g. all used _TFresnelElement_ classes. These have origin user-agent.
5135 * Next it parses the _application_ stylesheets. These have origin user.
5236 * Finally it parses the _form_ stylesheet. This has origin author.
5337 * Checks every attribute:
5438 * The resolver searches all attribute names and stores the numerical ID for later faster lookup.
5539 * Every attribute is registered in the **CSSRegistry** and has a _OnCheck_ method. E.g. the 'display' attribute calls _TFresnelCSSRegistry.CheckDisplay_.
5640 * Every value is checked for syntax. If the value contains a _var_ function, its syntax cannot be checked at this point.
5741 * If the value has a syntax error, then it is marked _invalid_ and will be skipped. A warning can be logged.
5842
5943## Collect CSS attributes for each element
6044
6145The _Viewport_ traverses all nodes (TFresnelElement) to compute basic attributes:
6246
6347 * It calls ComputeInlineStyle, which parses the _Style_ property as _inline style_ , similar to the above.
6448 * It calls ComputeCSSValues:
6549 * Calls **Resolver.Compute**
6650 * Collects all matching rules for this element
6751 * Merges the attributes of these rules and the inline style to a list of attributes. Considering **origin** , **rule-specifity** , **short-hands** , **all** and **!important**.
6852 * Substitutes **var()** calls using the custom-attributes of the element, or the inherited values.
6953 * Applies **shorthands**. E.g. _padding_ is split into _padding-top_ , _padding-right_ , _padding-bottom_ and _padding-left_.
7054 * Returns a list of attributes.
7155 * Substitute the base keywords **initial** , **inherit** , **unset**. Note: _revert_ and _revert-layer_ are currently treated as _unset_.
7256 * Compute base attributes: **visibility, display, position, box-sizing, direction, writing-mode, overflow-x, overflow-y, float**
7357 * if _display:none_ then _ComputedVisibility_ becomes _CSSRegistry.kwCollapse_
7458
7559## Box-Modell
7660
7761 * MarginBox: outer rectangle
7862 * BorderBox: outer rectangle of the border (Note: a border-width is only relevant with a border-style)
7963 * GutterBox: inside the border, the gutter is space for scrollbars. Scrollbars might be painted bigger than the gutter space, overlapping into the clientbox.
8064 * ClientBox: inside the gutterbox, scrollable area.
8165 * child elements left, top are relative to the container's ClientBox
8266 * ContentBox: ClientBox without the padding.
8367 * **width** and **height** are MarginBox minus margins, border and padding (box-sizing: content-box). Keep in mind, that this ignores gutter space.
8468
8569## Layout
8670
8771The _Viewport_ calls **Layouter.Apply** , which is **TViewportLayouter.Apply**.
8872
8973### Layout Top-Down
9074
9175First all elements are traversed top-down.
9276
9377 * Every visible, non collapsed element (TFresnelElement) gets an instance of _TUsedLayoutNode_ in _LayoutNode_.
9478 * Every non static element gets a **z-index**.
9579 * Every block element gets a layouter: **TFLFlowLayouter** , **TFLFlexLayouter** or**TFLGridLayouter**.
9680 * Set LayoutNode.Container
9781 * All used lengths are reset:
9882 * **border-, padding-, margin-top/right/bottom/left** , and **line-height** are set 0.
9983 * **left, top, right, bottom, width, height, min-width, max-width, min-height, max-height** are set to _NaN_.
10084 * **TUsedLayoutNode.ComputeUsedLengths** is called with _NoChildren=true_ (top-down run).
10185 * It computes each length, using only the element's and its parents' attributes.
10286 * Some values can be computed immediately e.g. 'width:100px'
10387 * Some values are relative:
10488 * font-size:150% means 150% of parent's font-size, that means it can be computed top-down.
10589 * line-height:150% means 150% of element's font-size, it can be computed top-down
10690 * line-height:1.2 must be inherited as '1.2', because it can mean different sizes in descendants, that means distinguish _Computed_ and _Used_
10791 * Some values can be deducted. E.g. given _left, right_ , it can deduct _width_. Well, it is more complicated due to _min-width_ , _max-width_ , _display_ , _position_ , and _box-sizing_ , but you get the idea.
10892
10993### Layout Final
11094
11195After the top-down traversal, all fixed or relative to fixed lengths are computed.
11296
11397The elements are again traversed top-down, but now the layouters can use child lengths and compute intrinsic sizes recursively. Intrinsic sizes are the width and/or height of the content ignoring the element's min/max-width/height. The intrinsic sizes are cached, so the recursion runs in linear time.
11498
11599After the layout was computed, each **TFresnelElement.ComputeCSSLayoutFinished** is called.
116100
117101# Flow Layouter
118102
119103 * **block** elements are using a line of their own.
120104 * each block has a block layouter (TFLBlockLayouter) to calculate min-/max-content ignoring outer floats
121105 * the flow-root block computes the final layout, including the layout of its non-flow-root child blocks, so float elements affects the inline elements.
122106 * **inline** elements are added till a line is full and broken at line breaks. See fragmentation. Each series of inline elements build a pseudo block consisting of line boxes.
123107 * elements are vertically aligned at the **baseline**. If an element has no baseline (display: inline flow-root), its bottom is its baseline.
124108 * vertical **padding** , **border** and **margin** have no layout effect on inline-elements, they effect block and float-elements.
125109 * horizontal **padding** , **border** , **margin** effect all elements.
126110 * **margin collapse** : not yet implemented
127111 * between blocks
128112 * child and parent top/bottom-margin collapse if there is no separation (padding, border, block formatting context)
129113 * empty block: no padding, border and content height 0, then margin-top+bottom collapse
130114 * Pos and Pos = Max, Pos and Neg = Sum, Neg and Neg = Min
131115 * **left** , **top** , **right** , **bottom** have no effect on **static** elements
132116 * the **static position** is the default for the other positions, so it must be always computed
133117 * **width** , **height** have **no** effect on static inline elements (unless **flow-root** or **overflow**)
134118 * **direction** : LTR or RTL, start left or right - not yet implemented
135119 * **writing-mode** : not yet implemented
136120 * **text-align** : align lines horizontally: **left** , **right** , **start** , **end** , **center** , **justify** , **match-parent**
137121 * **justify** : the last line is aligned left or right, depending on **direction**
138122 * **vertical-align** : align inline elements vertically, **baseline** , **sub** , **super** , **text-top** , **text-bottom** , **middle**
139123 * this effects the baseline of the child elements
140124 * **float** : **none** , **left** , **right** , Not yet implemented: **inline-start** , **inline-end** , **top** , **bottom**
141125 * removes element from the flow and puts it left or right, making inline elements flow around and following float elements are stacked
142126 * its content size is computed using MaxWidth of current block ignoring other floats (not trying to fit other floats)
143127 * **clear:** : **none** , **left** , **right**. Put block or float element below preceding floats.
144128 * **line-height** : positive float, 1=element's font ascent+descent, % allowed, inherited, default: normal (1.2)
145129 * **padding** : positive float, % uses container's width even for top and bottom
146130 * **border-width** : positive float, no %, only used if border-style is not **none**
147131 * **margin** : float, can be negative, % uses container's width even for top and bottom
148132 * spans:
149133 * can spread over multiple lines - fragmentation into line-boxes:
150134 * all line-boxes get the top and bottom border, only the first box the left border, only the last the right border.
151135 * the UsedClientBox, UsedBorderBox are the union or hull of the line-boxes.
152136 * vertical-align changes the baseline of the child elements
153137 * a span can have position: relative or stick:, the child elements are moved with the span.
154138
155139# Flex Layouter
156140
157141# Grid Layouter
158142
159143---
160144*From the Free Pascal wiki, page "Fresnel CSS Algorithm", as the Internet Archive kept it on 2026-01-08, last changed on 2025-11-15. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history