package toffee
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/toffee.style/Style/Compact_length/index.html
Module Style.Compact_lengthSource
Compact representation of CSS length values.
This module provides memory-efficient storage of CSS length values using tagged float representation. A single float value encodes both the numeric value and its type (length, percentage, auto, etc.) using low-order bits as tags.
On 64-bit platforms, the implementation stores a 32-bit float value in the upper 32 bits and an 8-bit tag in the lower 8 bits of a 64-bit float, preserving full f32 precision. On 32-bit platforms, a different encoding is used with reduced precision.
This encoding allows toffee's style types to remain compact while supporting the full range of CSS dimension values.
Conventions
The following conventions apply throughout toffee's style modules:
- Percentage range: Percentages are represented in [0.0, 1.0], not [0.0, 100.0]. For example, 50% is
0.5. - Abstract units: Length values are in abstract units. The interpretation (pixels, logical pixels, mm, etc.) is application-defined.
- f32 precision: Resolution operations use 32-bit float precision to match Rust taffy's arithmetic behavior and ensure consistent layout results across implementations.
Compact length value represented as a tagged float.
The low-order bits encode the value type, while the remaining bits store the numeric value. This representation is opaque; use the constructor and inspection functions to create and examine values.
calc_resolver index parent_size resolves a calc expression.
The index identifies the calc expression, and parent_size provides context for percentage resolution. Returns the computed length value.
Constructors
length value creates an absolute length.
The value represents a length in abstract units. The interpretation of these units (pixels, logical pixels, millimeters, etc.) is application-defined.
percent value creates a percentage length.
Percentage values are resolved relative to the size of the containing block. See Conventions for the percentage range.
auto represents automatic sizing.
The dimension is computed according to algorithm-specific rules regarding the default size of boxes.
fr value creates a fractional unit length.
Fractional units (fr) divide available space proportionally in CSS Grid layouts. The value is the numerator of the fraction; the denominator is the sum of all fr values in that grid dimension.
min_content represents intrinsic minimum size.
This is the smallest size that can fit the item's contents with all soft line-wrapping opportunities taken.
max_content represents intrinsic maximum size.
This is the smallest size that can fit the item's contents with no soft line-wrapping opportunities taken.
fit_content_px limit creates a fit-content length with pixel limit.
The size is computed as max(min_content, min(max_content, limit)), clamping limit by the min-content and max-content sizes.
fit_content_percent limit creates a fit-content length with percentage limit.
The size is computed as max(min_content, min(max_content, limit)), where limit is resolved as a percentage of the containing block, then clamped by the min-content and max-content sizes.
The limit value must be in the range 0.0, 1.0.
calc index creates a calc expression reference.
The index is an opaque handle to the actual calc expression, used with a calc_resolver to compute the final value.
Constants
Inspection
is_fit_content_px t returns true if t is fit-content with a pixel limit.
is_fit_content_percent t returns true if t is fit-content with a percentage limit.
is_length_or_percentage t returns true if t is either an absolute length or percentage.
is_intrinsic t returns true if t is auto, min-content, max-content, or fit-content.
is_max_or_fit_content t returns true if t is max-content or any fit-content variant.
is_max_content_alike t returns true if t behaves like max-content.
Returns true for auto, max-content, and fit-content variants. Per CSS Grid specification: "In all cases, treat auto and fit-content() as max-content, except where specified otherwise for fit-content()."
is_min_or_max_content t returns true if t is min-content or max-content.
uses_percentage t returns true if t depends on parent size.
Returns true for percentage values, fit-content with percentage limits, and calc expressions.
Value Extraction
get_tag t extracts the internal tag from t.
This is a low-level function for internal use by other style modules.
get_value t extracts the numeric value without validation.
This is a low-level function for internal use. Unlike value, this does not raise for auto/min-content/max-content, returning an undefined value instead.
Tag constants
These are internal constants for low-level tag inspection.
Tag value for absolute lengths.
Tag value for percentages.
Tag value for fit-content with pixel limit.
Tag value for fit-content with percentage limit.
value t extracts the numeric value from t.
Raises Failure if t is auto, min-content, max-content, or calc, which have no associated numeric value.
get_calc_index t extracts the calc expression index.
Behavior is undefined if t is not a calc expression. Use is_calc to check before calling.
resolved_percentage_size t parent_size resolves percentage to absolute length.
Returns Some value if t is a percentage, computing value * parent_size with f32 precision. Returns None otherwise.
resolved_percentage_size_with_calc t parent_size calc_resolver resolves percentage or calc to absolute length.
Returns Some value if t is a percentage or calc expression. For percentages, computes value * parent_size with f32 precision. For calc expressions, applies calc_resolver. Returns None otherwise.
Conversion
to_string t returns a human-readable representation.
Examples: "10px", "50%", "auto", "2fr", "min-content", "max-content", "fit-content(200px)", "calc(#0)".
Comparison
equal a b tests equality of compact length values.
For numeric values, uses floating-point epsilon comparison. For calc expressions, compares indices.
compare a b provides total ordering of compact length values.
Calc expressions sort before all other values. Within each type, values are ordered numerically where applicable.
pp fmt t pretty-prints t to fmt.
Uses the same format as to_string.