mirror of
https://github.com/lc-soft/LCUI.git
synced 2026-11-30 16:48:21 +01:00
407 lines
16 KiB
C
407 lines
16 KiB
C
/*
|
|
* lib/css/include/css/computed.h
|
|
*
|
|
* Copyright (c) 2023-2026, Liu Chao <hello@lcui.dev> All rights reserved.
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*
|
|
* This file is part of LCUI, distributed under the MIT License found
|
|
* in the LICENSE.TXT file in the root directory of this source tree.
|
|
*/
|
|
|
|
#ifndef LIBCSS_INCLUDE_CSS_COMPUTED_H
|
|
#define LIBCSS_INCLUDE_CSS_COMPUTED_H
|
|
|
|
#include "common.h"
|
|
#include "types.h"
|
|
|
|
LIBCSS_BEGIN_DECLS
|
|
|
|
#define IS_CSS_LENGTH(S, PROP_KEY) (S)->type_bits.PROP_KEY == CSS_LENGTH_SET
|
|
|
|
#define IS_CSS_FIXED_LENGTH(S, PROP_KEY) \
|
|
((S)->type_bits.PROP_KEY == CSS_LENGTH_SET && \
|
|
(S)->unit_bits.PROP_KEY == CSS_UNIT_PX)
|
|
|
|
#define IS_CSS_PERCENTAGE(S, PROP_KEY) \
|
|
((S)->type_bits.PROP_KEY == CSS_LENGTH_SET && \
|
|
(S)->unit_bits.PROP_KEY == CSS_UNIT_PERCENT)
|
|
|
|
#define CSS_SET_FIXED_LENGTH(S, PROP_KEY, PROP_VALUE) \
|
|
do { \
|
|
(S)->PROP_KEY = PROP_VALUE; \
|
|
(S)->type_bits.PROP_KEY = CSS_LENGTH_SET; \
|
|
(S)->unit_bits.PROP_KEY = CSS_UNIT_PX; \
|
|
} while (0);
|
|
|
|
#define CSS_COPY_LENGTH(DEST, SRC, PROP_KEY) \
|
|
do { \
|
|
(DEST)->PROP_KEY = (SRC)->PROP_KEY; \
|
|
(DEST)->type_bits.PROP_KEY = (SRC)->type_bits.PROP_KEY; \
|
|
(DEST)->unit_bits.PROP_KEY = (SRC)->unit_bits.PROP_KEY; \
|
|
} while (0);
|
|
|
|
/* Copy a value that has both a numeric field and a type_bits classifier. */
|
|
#define CSS_COPY_VALUE_TYPE(DEST, SRC, PROP_KEY) \
|
|
do { \
|
|
(DEST)->PROP_KEY = (SRC)->PROP_KEY; \
|
|
(DEST)->type_bits.PROP_KEY = (SRC)->type_bits.PROP_KEY; \
|
|
} while (0);
|
|
|
|
/* Copy a value that has both a numeric field and a unit_bits classifier. */
|
|
#define CSS_COPY_VALUE_UNIT(DEST, SRC, PROP_KEY) \
|
|
do { \
|
|
(DEST)->PROP_KEY = (SRC)->PROP_KEY; \
|
|
(DEST)->unit_bits.PROP_KEY = (SRC)->unit_bits.PROP_KEY; \
|
|
} while (0);
|
|
|
|
/* Copy a value field only (e.g. plain color without metadata). */
|
|
#define CSS_COPY_VALUE(DEST, SRC, PROP_KEY) (DEST)->PROP_KEY = (SRC)->PROP_KEY
|
|
|
|
/* Copy a type_bits-only classifier. */
|
|
#define CSS_COPY_TYPE_BITS(DEST, SRC, PROP_KEY) \
|
|
(DEST)->type_bits.PROP_KEY = (SRC)->type_bits.PROP_KEY
|
|
|
|
/* Property equality macros mirroring the copy shapes above. */
|
|
#define CSS_LENGTH_EQUAL(A, B, PROP_KEY) \
|
|
((A)->PROP_KEY == (B)->PROP_KEY && \
|
|
(A)->type_bits.PROP_KEY == (B)->type_bits.PROP_KEY && \
|
|
(A)->unit_bits.PROP_KEY == (B)->unit_bits.PROP_KEY)
|
|
|
|
#define CSS_VALUE_TYPE_EQUAL(A, B, PROP_KEY) \
|
|
((A)->PROP_KEY == (B)->PROP_KEY && \
|
|
(A)->type_bits.PROP_KEY == (B)->type_bits.PROP_KEY)
|
|
|
|
#define CSS_VALUE_UNIT_EQUAL(A, B, PROP_KEY) \
|
|
((A)->PROP_KEY == (B)->PROP_KEY && \
|
|
(A)->unit_bits.PROP_KEY == (B)->unit_bits.PROP_KEY)
|
|
|
|
#define CSS_VALUE_EQUAL(A, B, PROP_KEY) ((A)->PROP_KEY == (B)->PROP_KEY)
|
|
|
|
#define CSS_TYPE_BITS_EQUAL(A, B, PROP_KEY) \
|
|
((A)->type_bits.PROP_KEY == (B)->type_bits.PROP_KEY)
|
|
|
|
/* Compares an owned string field plus its type_bits classifier. */
|
|
#define CSS_STRING_TYPE_EQUAL(A, B, PROP_KEY) \
|
|
(CSS_TYPE_BITS_EQUAL(A, B, PROP_KEY) && \
|
|
((A)->PROP_KEY == (B)->PROP_KEY || \
|
|
((A)->PROP_KEY && (B)->PROP_KEY && \
|
|
strcmp((A)->PROP_KEY, (B)->PROP_KEY) == 0)))
|
|
|
|
LIBCSS_INLINE bool is_css_display_inline(const css_computed_style_t *s)
|
|
{
|
|
return s->type_bits.display == CSS_DISPLAY_INLINE ||
|
|
s->type_bits.display == CSS_DISPLAY_INLINE_BLOCK ||
|
|
s->type_bits.display == CSS_DISPLAY_INLINE_FLEX;
|
|
}
|
|
|
|
LIBCSS_INLINE bool is_css_display_flex(const css_computed_style_t *s)
|
|
{
|
|
return s->type_bits.display == CSS_DISPLAY_FLEX ||
|
|
s->type_bits.display == CSS_DISPLAY_INLINE_FLEX;
|
|
}
|
|
|
|
LIBCSS_INLINE bool is_css_display_block(const css_computed_style_t *s)
|
|
{
|
|
return s->type_bits.display == CSS_DISPLAY_BLOCK;
|
|
}
|
|
|
|
LIBCSS_INLINE bool is_css_border_box_sizing(const css_computed_style_t *s)
|
|
{
|
|
return s->type_bits.box_sizing == CSS_BOX_SIZING_BORDER_BOX;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_padding_x(const css_computed_style_t *s)
|
|
{
|
|
return s->padding_left + s->padding_right;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_padding_y(const css_computed_style_t *s)
|
|
{
|
|
return s->padding_top + s->padding_bottom;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_margin_x(const css_computed_style_t *s)
|
|
{
|
|
return s->margin_left + s->margin_right;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_margin_y(const css_computed_style_t *s)
|
|
{
|
|
return s->margin_top + s->margin_bottom;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_border_x(const css_computed_style_t *s)
|
|
{
|
|
return s->border_left_width + s->border_right_width;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_border_y(const css_computed_style_t *s)
|
|
{
|
|
return s->border_top_width + s->border_bottom_width;
|
|
}
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_padding_top(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_padding_right(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_padding_bottom(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_padding_left(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_margin_top(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_margin_right(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_margin_bottom(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_margin_left(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_top(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_right(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_bottom(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_left(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_display(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_flex_direction(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_vertical_align(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_box_sizing(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_position(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_min_width(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_min_height(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_max_width(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_max_height(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_width(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_height(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_border_top_style(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_border_right_style(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_border_bottom_style(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_border_left_style(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_border_top_color(
|
|
const css_computed_style_t *s, css_color_value_t *value);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_border_right_color(
|
|
const css_computed_style_t *s, css_color_value_t *value);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_border_bottom_color(
|
|
const css_computed_style_t *s, css_color_value_t *value);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_border_left_color(
|
|
const css_computed_style_t *s, css_color_value_t *value);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_background_position_x(
|
|
const css_computed_style_t *s, css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_background_position_y(
|
|
const css_computed_style_t *s, css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_background_width(const css_computed_style_t *s,
|
|
css_numeric_value_t *value, css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_background_height(const css_computed_style_t *s,
|
|
css_numeric_value_t *value, css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_background_image(
|
|
const css_computed_style_t *s, css_image_value_t *value);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_box_shadow_x(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_box_shadow_y(const css_computed_style_t *s,
|
|
css_numeric_value_t *value,
|
|
css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_box_shadow_blur(const css_computed_style_t *s,
|
|
css_numeric_value_t *value, css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t
|
|
css_computed_box_shadow_spread(const css_computed_style_t *s,
|
|
css_numeric_value_t *value, css_unit_t *unit);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_box_shadow_color(
|
|
const css_computed_style_t *s, css_color_value_t *value);
|
|
|
|
LIBCSS_PUBLIC uint8_t css_computed_visibility(const css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC void css_computed_style_destroy(css_computed_style_t *s);
|
|
|
|
LIBCSS_PUBLIC void css_computed_style_copy_property(
|
|
css_computed_style_t *dest, const css_computed_style_t *src,
|
|
css_prop_key_t key);
|
|
|
|
LIBCSS_PUBLIC bool css_computed_style_property_equal(
|
|
const css_computed_style_t *a, const css_computed_style_t *b,
|
|
css_prop_key_t key);
|
|
|
|
LIBCSS_PUBLIC int css_cascade_style(const css_style_decl_t *style,
|
|
css_computed_style_t *computed);
|
|
|
|
LIBCSS_PUBLIC int css_cascade_style_with_parent(
|
|
const css_style_decl_t *style, const css_computed_style_t *parent,
|
|
css_computed_style_t *computed);
|
|
|
|
LIBCSS_PUBLIC void css_compute_absolute_values(
|
|
const css_computed_style_t *parent, css_computed_style_t *s,
|
|
css_metrics_t *m);
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_content_box_width_to_width(
|
|
css_computed_style_t *s, css_numeric_value_t content_width)
|
|
{
|
|
if (css_computed_box_sizing(s) == CSS_BOX_SIZING_BORDER_BOX) {
|
|
return content_width + css_border_x(s) + css_padding_x(s);
|
|
}
|
|
return content_width;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_content_box_height_to_height(
|
|
css_computed_style_t *s, css_numeric_value_t content_height)
|
|
{
|
|
if (css_computed_box_sizing(s) == CSS_BOX_SIZING_BORDER_BOX) {
|
|
return content_height + css_border_y(s) + css_padding_y(s);
|
|
}
|
|
return content_height;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_border_box_width_to_width(
|
|
css_computed_style_t *s, css_numeric_value_t border_width)
|
|
{
|
|
if (css_computed_box_sizing(s) == CSS_BOX_SIZING_CONTENT_BOX) {
|
|
return border_width - css_border_x(s) - css_padding_x(s);
|
|
}
|
|
return border_width;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_border_box_height_to_height(
|
|
css_computed_style_t *s, css_numeric_value_t border_height)
|
|
{
|
|
if (css_computed_box_sizing(s) == CSS_BOX_SIZING_CONTENT_BOX) {
|
|
return border_height - css_border_y(s) - css_padding_y(s);
|
|
}
|
|
return border_height;
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_width_to_content_box_width(
|
|
const css_computed_style_t *s, css_numeric_value_t width)
|
|
{
|
|
return css_computed_box_sizing(s) == CSS_BOX_SIZING_CONTENT_BOX
|
|
? width
|
|
: width - css_padding_x(s) - css_border_x(s);
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_height_to_content_box_height(
|
|
const css_computed_style_t *s, css_numeric_value_t height)
|
|
{
|
|
return css_computed_box_sizing(s) == CSS_BOX_SIZING_CONTENT_BOX
|
|
? height
|
|
: height - css_padding_y(s) - css_border_y(s);
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_width_to_border_box_width(
|
|
const css_computed_style_t *s, css_numeric_value_t width)
|
|
{
|
|
return css_computed_box_sizing(s) == CSS_BOX_SIZING_BORDER_BOX
|
|
? width
|
|
: width + css_padding_x(s) + css_border_x(s);
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_height_to_border_box_height(
|
|
const css_computed_style_t *s, css_numeric_value_t height)
|
|
{
|
|
return css_computed_box_sizing(s) == CSS_BOX_SIZING_BORDER_BOX
|
|
? height
|
|
: height + css_padding_y(s) + css_border_y(s);
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_width_to_outer_box_width(
|
|
const css_computed_style_t *s, css_numeric_value_t width)
|
|
{
|
|
return css_width_to_border_box_width(s, width) + css_margin_x(s);
|
|
}
|
|
|
|
LIBCSS_INLINE css_numeric_value_t css_height_to_outer_box_height(
|
|
const css_computed_style_t *s, css_numeric_value_t height)
|
|
{
|
|
return css_height_to_border_box_height(s, height) + css_margin_y(s);
|
|
}
|
|
|
|
LIBCSS_END_DECLS
|
|
|
|
#define css_width_from_cbox css_content_box_width_to_width
|
|
#define css_height_from_cbox css_content_box_height_to_height
|
|
#define css_width_from_bbox css_border_box_width_to_width
|
|
#define css_height_from_bbox css_border_box_height_to_height
|
|
#define css_cbox_width css_width_to_content_box_width
|
|
#define css_cbox_height css_height_to_content_box_height
|
|
#define css_bbox_width css_width_to_border_box_width
|
|
#define css_bbox_height css_height_to_border_box_height
|
|
#define css_obox_width css_width_to_outer_box_width
|
|
#define css_obox_height css_height_to_outer_box_height
|
|
|
|
#endif
|