NetSurf
Data Structures | Functions
layout_flex.c File Reference

HTML layout implementation: display: flex. More...

#include <string.h>
#include "utils/log.h"
#include "utils/utils.h"
#include "html/box.h"
#include "html/html.h"
#include "html/private.h"
#include "html/box_inspect.h"
#include "html/layout_internal.h"
Include dependency graph for layout_flex.c:

Go to the source code of this file.

Data Structures

struct  flex_item_data
 Flex item data. More...
 
struct  flex_line_data
 Flex line data. More...
 
struct  flex_ctx
 Flex layout context. More...
 
struct  flex_ctx::flex_items
 
struct  flex_ctx::flex_lines
 

Functions

static void layout_flex_ctx__destroy (struct flex_ctx *ctx)
 Destroy a flex layout context. More...
 
static struct flex_ctxlayout_flex_ctx__create (html_content *content, const struct box *flex)
 Create a flex layout context. More...
 
static enum box_side layout_flex__main_start_side (const struct flex_ctx *ctx)
 Find box side representing the start of flex container in main direction. More...
 
static enum box_side layout_flex__main_end_side (const struct flex_ctx *ctx)
 Find box side representing the end of flex container in main direction. More...
 
static bool layout_flex_item (const struct flex_ctx *ctx, const struct flex_item_data *item, int available_width)
 Perform layout on a flex item. More...
 
static bool layout_flex__base_and_main_sizes (const struct flex_ctx *ctx, struct flex_item_data *item, int available_width)
 Calculate an item's base and target main sizes. More...
 
static void layout_flex_ctx__populate_item_data (const struct flex_ctx *ctx, const struct box *flex, int available_width)
 Fill out all item's data in a flex container. More...
 
static bool layout_flex_ctx__ensure_line (struct flex_ctx *ctx)
 Ensure context's lines array has a free space. More...
 
static struct flex_line_datalayout_flex__build_line (struct flex_ctx *ctx, size_t item_index)
 Assigns flex items to the line and returns the line. More...
 
static void layout_flex__item_freeze (struct flex_line_data *line, struct flex_item_data *item)
 Freeze an item on a line. More...
 
static int layout_flex__remaining_free_main (struct flex_ctx *ctx, struct flex_line_data *line, css_fixed *unfrozen_factor_sum, int initial_free_main, int available_main, bool grow)
 Calculate remaining free space and unfrozen item factor sum. More...
 
static int layout_flex__get_min_max_violations (struct flex_ctx *ctx, struct flex_line_data *line)
 Clamp flex item target main size and get min/max violations. More...
 
static void layout_flex__distribute_free_main (struct flex_ctx *ctx, struct flex_line_data *line, css_fixed unfrozen_factor_sum, int remaining_free_main, bool grow)
 Distribute remaining free space proportional to the flex factors. More...
 
static bool layout_flex__resolve_line (struct flex_ctx *ctx, struct flex_line_data *line)
 Resolve flexible item lengths along a line. More...
 
static bool layout_flex__place_line_items_main (struct flex_ctx *ctx, struct flex_line_data *line)
 Position items along a line. More...
 
static bool layout_flex__collect_items_into_lines (struct flex_ctx *ctx)
 Collect items onto lines and place items along the lines. More...
 
static void layout_flex__place_line_items_cross (struct flex_ctx *ctx, struct flex_line_data *line, int extra)
 Align items on a line. More...
 
static void layout_flex__place_lines (struct flex_ctx *ctx)
 Place the lines and align the items on the line. More...
 
bool layout_flex (struct box *flex, int available_width, html_content *content)
 Layout a flex container. More...
 

Detailed Description

HTML layout implementation: display: flex.

Layout is carried out in two stages:

  1. + calculation of minimum / maximum box widths, and
    • determination of whether block level boxes will have >zero height
  2. + layout (position and dimensions)

In most cases the functions for the two stages are a corresponding pair layout_minmax_X() and layout_X().

Definition in file layout_flex.c.

Function Documentation

◆ layout_flex()

bool layout_flex ( struct box flex,
int  available_width,
html_content content 
)

Layout a flex container.

Parameters
[in]flextable to layout
[in]available_widthwidth of containing block
[in]contentmemory pool for any new boxes
Returns
true on success, false on memory exhaustion

Definition at line 1046 of file layout_flex.c.

References AUTO, flex_ctx::available_cross, flex_ctx::available_main, box::border, flex_ctx::cross_size, flex_ctx::flex, box::height, flex_ctx::horizontal, layout_find_dimensions(), layout_flex__collect_items_into_lines(), layout_flex__place_lines(), layout_flex_ctx__create(), layout_flex_ctx__destroy(), layout_flex_ctx__populate_item_data(), flex_ctx::main_size, box::margin, min, NSLOG, box::padding, box::style, flex_ctx::unit_len_ctx, and box::width.

Referenced by layout_absolute(), layout_block_context(), layout_flex_item(), and layout_float().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__base_and_main_sizes()

static bool layout_flex__base_and_main_sizes ( const struct flex_ctx ctx,
struct flex_item_data item,
int  available_width 
)
inlinestatic

Calculate an item's base and target main sizes.

Parameters
[in]ctxFlex layout context
[in]itemItem to get sizes of
[in]available_widthAvailable width in pixels
Returns
true on success false on failure

Definition at line 260 of file layout_flex.c.

References AUTO, flex_item_data::base_size, flex_item_data::basis, flex_item_data::basis_length, flex_item_data::basis_unit, flex_item_data::box, flex_ctx::flex, box::height, flex_ctx::horizontal, layout_flex_item(), lh__delta_outer_main(), lh__delta_outer_width(), flex_item_data::main_size, max, flex_item_data::max_main, box::max_width, min, flex_item_data::min_main, box::min_width, NSLOG, box::style, flex_item_data::target_main_size, flex_ctx::unit_len_ctx, and box::width.

Referenced by layout_flex_ctx__populate_item_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__build_line()

static struct flex_line_data * layout_flex__build_line ( struct flex_ctx ctx,
size_t  item_index 
)
static

Assigns flex items to the line and returns the line.

Parameters
[in]ctxFlex layout context
[in]item_indexIndex to first item to assign to this line
Returns
Pointer to the new line, or NULL on error.

Definition at line 413 of file layout_flex.c.

References AUTO, flex_ctx::available_main, flex_item_data::box, flex_ctx::flex_lines::count, flex_ctx::flex_items::data, flex_ctx::flex_lines::data, flex_ctx::flex, box::height, flex_ctx::horizontal, flex_ctx::item, layout_flex__main_end_side(), layout_flex__main_start_side(), layout_flex_ctx__ensure_line(), lh__box_is_absolute(), lh__delta_outer_main(), flex_item_data::line, flex_ctx::line, line(), flex_item_data::main_size, box::margin, NSLOG, and flex_ctx::wrap.

Referenced by layout_flex__collect_items_into_lines().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__collect_items_into_lines()

static bool layout_flex__collect_items_into_lines ( struct flex_ctx ctx)
static

Collect items onto lines and place items along the lines.

Parameters
[in]ctxFlex layout context
Returns
true on success, false on failure.

Definition at line 898 of file layout_flex.c.

References flex_ctx::flex_items::count, flex_ctx::cross_size, flex_ctx::flex, flex_ctx::item, layout_flex__build_line(), layout_flex__place_line_items_main(), layout_flex__resolve_line(), line(), flex_ctx::main_size, NSLOG, and flex_line_data::pos.

Referenced by layout_flex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__distribute_free_main()

static void layout_flex__distribute_free_main ( struct flex_ctx ctx,
struct flex_line_data line,
css_fixed  unfrozen_factor_sum,
int  remaining_free_main,
bool  grow 
)
inlinestatic

Distribute remaining free space proportional to the flex factors.

Remaining free space may be negative.

Parameters
[in]ctxFlex layout context
[in]lineLine to distribute free space on
[in]unfrozen_factor_sumSum of unfrozen item's flex factors
[in]remaining_free_mainRemaining free space in main direction
[in]growWhether to grow or shrink

Definition at line 622 of file layout_flex.c.

References flex_item_data::base_size, flex_ctx::flex_items::data, flex_item_data::freeze, flex_item_data::grow, flex_ctx::item, layout_flex__item_freeze(), line(), flex_item_data::main_size, result, flex_item_data::shrink, and flex_item_data::target_main_size.

Referenced by layout_flex__resolve_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__get_min_max_violations()

static int layout_flex__get_min_max_violations ( struct flex_ctx ctx,
struct flex_line_data line 
)
inlinestatic

Clamp flex item target main size and get min/max violations.

Parameters
[in]ctxFlex layout context
[in]lineLine to align items on return total violation in pixels

Definition at line 555 of file layout_flex.c.

References flex_item_data::box, flex_ctx::flex_items::data, flex_item_data::freeze, flex_ctx::item, line(), flex_item_data::max_main, flex_item_data::max_violation, flex_item_data::min_main, flex_item_data::min_violation, box::min_width, NSLOG, and flex_item_data::target_main_size.

Referenced by layout_flex__resolve_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__item_freeze()

static void layout_flex__item_freeze ( struct flex_line_data line,
struct flex_item_data item 
)
inlinestatic

Freeze an item on a line.

Parameters
[in]lineLine to containing item
[in]itemItem to freeze

Definition at line 480 of file layout_flex.c.

References flex_item_data::box, flex_item_data::freeze, lh__box_is_absolute(), line(), NSLOG, and flex_item_data::target_main_size.

Referenced by layout_flex__distribute_free_main(), and layout_flex__resolve_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__main_end_side()

static enum box_side layout_flex__main_end_side ( const struct flex_ctx ctx)
static

Find box side representing the end of flex container in main direction.

Parameters
[in]ctxFlex layout context.
Returns
the end side.

Definition at line 199 of file layout_flex.c.

References BOTTOM, flex_ctx::horizontal, LEFT, flex_ctx::main_reversed, RIGHT, and TOP.

Referenced by layout_flex__build_line().

Here is the caller graph for this function:

◆ layout_flex__main_start_side()

static enum box_side layout_flex__main_start_side ( const struct flex_ctx ctx)
static

Find box side representing the start of flex container in main direction.

Parameters
[in]ctxFlex layout context.
Returns
the start side.

Definition at line 183 of file layout_flex.c.

References BOTTOM, flex_ctx::horizontal, LEFT, flex_ctx::main_reversed, RIGHT, and TOP.

Referenced by layout_flex__build_line(), and layout_flex__place_line_items_main().

Here is the caller graph for this function:

◆ layout_flex__place_line_items_cross()

static void layout_flex__place_line_items_cross ( struct flex_ctx ctx,
struct flex_line_data line,
int  extra 
)
static

Align items on a line.

Parameters
[in]ctxFlex layout context
[in]lineLine to align items on
[in]extraExtra line width in pixels

Definition at line 942 of file layout_flex.c.

References box::border, flex_item_data::box, flex_ctx::flex_items::data, fallthrough, flex_ctx::flex, flex_ctx::horizontal, flex_ctx::item, layout_flex_item(), LEFT, lh__box_align_self(), lh__box_size_cross_is_auto(), lh__box_size_cross_ptr(), lh__delta_outer_cross(), lh__non_auto_margin(), line(), box::padding, TOP, box_border::width, box::width, box::x, and box::y.

Referenced by layout_flex__place_lines().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__place_line_items_main()

static bool layout_flex__place_line_items_main ( struct flex_ctx ctx,
struct flex_line_data line 
)
static

Position items along a line.

Parameters
[in]ctxFlex layout context
[in]lineLine to resolve
Returns
true on success, false on failure.

Definition at line 805 of file layout_flex.c.

References AUTO, flex_ctx::available_main, box::border, BOTTOM, flex_item_data::box, flex_ctx::flex_items::data, flex_ctx::flex, flex_ctx::horizontal, flex_ctx::item, layout_flex__main_start_side(), layout_flex_item(), LEFT, lh__box_is_absolute(), lh__box_size_cross(), lh__box_size_main(), lh__delta_outer_cross(), lh__delta_outer_main(), lh__delta_outer_width(), lh__non_auto_margin(), line(), flex_ctx::main_reversed, box::margin, box::padding, RIGHT, flex_item_data::target_main_size, TOP, UNKNOWN_WIDTH, box_border::width, box::width, box::x, and box::y.

Referenced by layout_flex__collect_items_into_lines().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__place_lines()

static void layout_flex__place_lines ( struct flex_ctx ctx)
static

Place the lines and align the items on the line.

Parameters
[in]ctxFlex layout context

Definition at line 1003 of file layout_flex.c.

References AUTO, flex_ctx::available_cross, flex_ctx::flex_lines::count, flex_ctx::cross_size, flex_ctx::flex_lines::data, layout_flex__place_line_items_cross(), flex_ctx::line, line(), and flex_ctx::wrap.

Referenced by layout_flex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__remaining_free_main()

static int layout_flex__remaining_free_main ( struct flex_ctx ctx,
struct flex_line_data line,
css_fixed *  unfrozen_factor_sum,
int  initial_free_main,
int  available_main,
bool  grow 
)
inlinestatic

Calculate remaining free space and unfrozen item factor sum.

Parameters
[in]ctxFlex layout context
[in]lineLine to calculate free space on
[out]unfrozen_factor_sumReturns sum of unfrozen item's flex factors
[in]initial_free_mainInitial free space in main direction
[in]available_mainAvailable space in main direction
[in]growWhether to grow or shrink return remaining free space on line

Definition at line 507 of file layout_flex.c.

References flex_item_data::base_size, flex_ctx::flex_items::data, flex_item_data::freeze, flex_item_data::grow, flex_ctx::item, line(), NSLOG, flex_item_data::shrink, and flex_item_data::target_main_size.

Referenced by layout_flex__resolve_line().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex__resolve_line()

static bool layout_flex__resolve_line ( struct flex_ctx ctx,
struct flex_line_data line 
)
static

Resolve flexible item lengths along a line.

See 9.7 of Tests CSS Flexible Box Layout Module Level 1.

Parameters
[in]ctxFlex layout context
[in]lineLine to resolve
Returns
true on success, false on failure.

Definition at line 706 of file layout_flex.c.

References AUTO, flex_ctx::available_main, flex_item_data::base_size, flex_ctx::flex_items::data, flex_ctx::flex_lines::data, flex_ctx::flex, flex_item_data::freeze, flex_item_data::grow, flex_ctx::item, layout_flex__distribute_free_main(), layout_flex__get_min_max_violations(), layout_flex__item_freeze(), layout_flex__remaining_free_main(), flex_ctx::line, line(), flex_item_data::main_size, flex_item_data::max_violation, flex_item_data::min_violation, NSLOG, flex_item_data::shrink, and flex_item_data::target_main_size.

Referenced by layout_flex__collect_items_into_lines().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex_ctx__create()

static struct flex_ctx * layout_flex_ctx__create ( html_content content,
const struct box flex 
)
static

Create a flex layout context.

Parameters
[in]contentHTML content containing flex box
[in]flexBox to create layout context for
Returns
flex layout context or NULL on error

Definition at line 140 of file layout_flex.c.

References flex_ctx::flex_lines::alloc, box_count_children(), flex_ctx::content, flex_ctx::flex_items::count, flex_ctx::flex_items::data, flex_ctx::flex_lines::data, flex_ctx::flex, flex_ctx::horizontal, flex_ctx::item, layout_flex_ctx__destroy(), lh__flex_direction_reversed(), lh__flex_main_is_horizontal(), flex_ctx::line, flex_ctx::main_reversed, box::style, flex_ctx::unit_len_ctx, and flex_ctx::wrap.

Referenced by layout_flex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex_ctx__destroy()

static void layout_flex_ctx__destroy ( struct flex_ctx ctx)
static

Destroy a flex layout context.

Parameters
[in]ctxFlex layout context

Definition at line 124 of file layout_flex.c.

References flex_ctx::flex_items::data, flex_ctx::flex_lines::data, flex_ctx::item, and flex_ctx::line.

Referenced by layout_flex(), and layout_flex_ctx__create().

Here is the caller graph for this function:

◆ layout_flex_ctx__ensure_line()

static bool layout_flex_ctx__ensure_line ( struct flex_ctx ctx)
static

Ensure context's lines array has a free space.

Parameters
[in]ctxFlex layout context
Returns
true on success false on out of memory

Definition at line 384 of file layout_flex.c.

References flex_ctx::flex_lines::alloc, flex_ctx::flex_lines::count, flex_ctx::flex_lines::data, and flex_ctx::line.

Referenced by layout_flex__build_line().

Here is the caller graph for this function:

◆ layout_flex_ctx__populate_item_data()

static void layout_flex_ctx__populate_item_data ( const struct flex_ctx ctx,
const struct box flex,
int  available_width 
)
static

Fill out all item's data in a flex container.

Parameters
[in]ctxFlex layout context
[in]flexFlex box
[in]available_widthAvailable width in pixels

Definition at line 343 of file layout_flex.c.

References flex_item_data::basis, flex_item_data::basis_length, flex_item_data::basis_unit, flex_item_data::box, box::children, flex_ctx::flex_items::data, flex_item_data::grow, flex_ctx::horizontal, flex_ctx::item, layout_find_dimensions(), layout_flex__base_and_main_sizes(), flex_item_data::max_cross, flex_item_data::max_main, flex_item_data::min_cross, flex_item_data::min_main, box::next, NSLOG, flex_item_data::shrink, flex_ctx::unit_len_ctx, and box::width.

Referenced by layout_flex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ layout_flex_item()

static bool layout_flex_item ( const struct flex_ctx ctx,
const struct flex_item_data item,
int  available_width 
)
static

Perform layout on a flex item.

Parameters
[in]ctxFlex layout context
[in]itemItem to lay out
[in]available_widthAvailable width for item in pixels
Returns
true on success false on failure

Definition at line 217 of file layout_flex.c.

References flex_item_data::box, BOX_BLOCK, BOX_FLEX, BOX_TABLE, flex_ctx::content, box::float_container, layout_block_context(), layout_flex(), layout_table, NSLOG, box::parent, and box::type.

Referenced by layout_flex__base_and_main_sizes(), layout_flex__place_line_items_cross(), and layout_flex__place_line_items_main().

Here is the call graph for this function:
Here is the caller graph for this function: