diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2021-03-18 09:16:30 +0000 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2021-05-19 14:40:54 +0100 |
commit | 65d4fd6e83d421e7fa7a8c7df44d01797e3c69ae (patch) | |
tree | 5f821510e54b70099f8f37db2b1176721e296a3f /src/select | |
parent | 6cd205329373efe5a1629518c2875724cc79dce3 (diff) | |
download | libcss-65d4fd6e83d421e7fa7a8c7df44d01797e3c69ae.tar.gz libcss-65d4fd6e83d421e7fa7a8c7df44d01797e3c69ae.tar.bz2 |
Selection: Remove client callback for unit conversion.
Now clients provide a unit conversion context and libcss provides
code to perform unit conversion.
This reduces the amount of common code that clients have to write.
Diffstat (limited to 'src/select')
-rw-r--r-- | src/select/computed.c | 34 | ||||
-rw-r--r-- | src/select/computed.h | 6 | ||||
-rw-r--r-- | src/select/select.c | 5 | ||||
-rw-r--r-- | src/select/unit.c | 67 | ||||
-rw-r--r-- | src/select/unit.h | 143 |
5 files changed, 76 insertions, 179 deletions
diff --git a/src/select/computed.c b/src/select/computed.c index a1b345b..d075af9 100644 --- a/src/select/computed.c +++ b/src/select/computed.c @@ -12,6 +12,7 @@ #include "select/dispatch.h" #include "select/propget.h" #include "select/propset.h" +#include "select/unit.h" #include "utils/utils.h" static css_error compute_absolute_color(css_computed_style *style, @@ -247,9 +248,7 @@ css_error css__computed_style_initialise(css_computed_style *style, css_error css_computed_style_compose( const css_computed_style *restrict parent, const css_computed_style *restrict child, - css_error (*compute_font_size)(void *pw, - const css_hint *parent, css_hint *size), - void *pw, + const css_unit_len_ctx *unit_len_ctx, css_computed_style **restrict result) { css_computed_style *composed; @@ -275,8 +274,7 @@ css_error css_computed_style_compose( } /* Finally, compute absolute values for everything */ - error = css__compute_absolute_values(parent, composed, - compute_font_size, pw); + error = css__compute_absolute_values(parent, composed, unit_len_ctx); if (error != CSS_OK) { return error; } @@ -1087,31 +1085,36 @@ uint8_t css_computed_order(const css_computed_style *style, * * \param parent Parent style, or NULL for tree root * \param style Computed style to process - * \param compute_font_size Callback to calculate an absolute font-size - * \param pw Private word for callback + * \param unit_len_ctx Client length conversion context. * \return CSS_OK on success. */ css_error css__compute_absolute_values(const css_computed_style *parent, css_computed_style *style, - css_error (*compute_font_size)(void *pw, - const css_hint *parent, css_hint *size), - void *pw) + const css_unit_len_ctx *unit_len_ctx) { + css_hint_length *ref_length = NULL; css_hint psize, size, ex_size; css_error error; - /* Ensure font-size is absolute */ + /* Get reference font-size for relative sizes. */ if (parent != NULL) { psize.status = get_font_size(parent, &psize.data.length.value, &psize.data.length.unit); + if (psize.status != CSS_FONT_SIZE_DIMENSION) { + return CSS_BADPARM; + } + ref_length = &psize.data.length; } size.status = get_font_size(style, &size.data.length.value, &size.data.length.unit); - error = compute_font_size(pw, parent != NULL ? &psize : NULL, &size); + error = css_unit_compute_absolute_font_size(ref_length, + unit_len_ctx->root_style, + unit_len_ctx->font_size_default, + &size); if (error != CSS_OK) return error; @@ -1125,7 +1128,12 @@ css_error css__compute_absolute_values(const css_computed_style *parent, ex_size.status = CSS_FONT_SIZE_DIMENSION; ex_size.data.length.value = INTTOFIX(1); ex_size.data.length.unit = CSS_UNIT_EX; - error = compute_font_size(pw, &size, &ex_size); + + error = css_unit_compute_absolute_font_size( + &size.data.length, + unit_len_ctx->root_style, + unit_len_ctx->font_size_default, + &ex_size); if (error != CSS_OK) return error; diff --git a/src/select/computed.h b/src/select/computed.h index c926cec..8b33405 100644 --- a/src/select/computed.h +++ b/src/select/computed.h @@ -10,6 +10,8 @@ #include <libcss/computed.h> #include <libcss/hint.h> +#include <libcss/unit.h> + #include "autogenerated_computed.h" /** @@ -35,8 +37,6 @@ css_error css__computed_style_initialise(css_computed_style *style, css_error css__compute_absolute_values(const css_computed_style *parent, css_computed_style *style, - css_error (*compute_font_size)(void *pw, - const css_hint *parent, css_hint *size), - void *pw); + const css_unit_len_ctx *unit_len_ctx); #endif diff --git a/src/select/select.c b/src/select/select.c index f6efbfe..03a45fe 100644 --- a/src/select/select.c +++ b/src/select/select.c @@ -23,6 +23,7 @@ #include "select/propset.h" #include "select/font_face.h" #include "select/select.h" +#include "select/unit.h" #include "utils/parserutilserror.h" #include "utils/utils.h" @@ -1164,6 +1165,7 @@ failed: * * \param ctx Selection context to use * \param node Node to select style for + * \param unit_len_ctx Context for length unit conversions. * \param media Currently active media specification * \param inline_style Corresponding inline style for node, or NULL * \param handler Dispatch table of handler functions @@ -1181,6 +1183,7 @@ failed: * update the fully computed style for a node when layout changes. */ css_error css_select_style(css_select_ctx *ctx, void *node, + const css_unit_len_ctx *unit_len_ctx, const css_media *media, const css_stylesheet *inline_style, css_select_handler *handler, void *pw, css_select_results **result) @@ -1353,7 +1356,7 @@ css_error css_select_style(css_select_ctx *ctx, void *node, /* Only compute absolute values for the base element */ error = css__compute_absolute_values(NULL, state.results->styles[CSS_PSEUDO_ELEMENT_NONE], - handler->compute_font_size, pw); + unit_len_ctx); if (error != CSS_OK) goto cleanup; } diff --git a/src/select/unit.c b/src/select/unit.c index f9ecf83..a279ec7 100644 --- a/src/select/unit.c +++ b/src/select/unit.c @@ -115,14 +115,15 @@ static inline css_fixed css_unit__absolute_len2pt( } } -/* Exported function, documented in unit.h. */ +/* Exported function, documented in libcss/unit.h. */ css_fixed css_unit_font_size_len2pt( + const css_computed_style *style, const css_unit_len_ctx *ctx, const css_fixed length, const css_unit unit) { return css_unit__absolute_len2pt( - ctx->ref_style, + style, ctx->viewport_height, ctx->viewport_width, length, @@ -303,15 +304,16 @@ css_fixed css_unit_len2px_mq( return FMUL(length, TRUNCATEFIX(px_per_unit)); } -/* Exported function, documented in unit.h. */ +/* Exported function, documented in libcss/unit.h. */ css_fixed css_unit_len2css_px( + const css_computed_style *style, const css_unit_len_ctx *ctx, const css_fixed length, const css_unit unit) { css_fixed px_per_unit = css_unit__px_per_unit( ctx->measure, - ctx->ref_style, + style, ctx->root_style, ctx->font_size_default, ctx->font_size_minimum, @@ -328,15 +330,16 @@ css_fixed css_unit_len2css_px( return FMUL(length, TRUNCATEFIX(px_per_unit)); } -/* Exported function, documented in unit.h. */ +/* Exported function, documented in libcss/unit.h. */ css_fixed css_unit_len2device_px( + const css_computed_style *style, const css_unit_len_ctx *ctx, const css_fixed length, const css_unit unit) { css_fixed px_per_unit = css_unit__px_per_unit( ctx->measure, - ctx->ref_style, + style, ctx->root_style, ctx->font_size_default, ctx->font_size_minimum, @@ -361,18 +364,18 @@ css_fixed css_unit_len2device_px( * The computed style will have font-size with an absolute unit. * If no computed style is given, the client default font-size will be returned. * - * \param[in] ctx Length unit conversion context. - * \param[in] style The style to get the font-size for, or NULL. + * \param[in] style Reference style. (Element or parent, or NULL). + * \param[in] font_size_default Client default font size in CSS pixels. * \return The font size in absolute units. */ static inline css_hint_length css_unit__get_font_size( - const css_unit_len_ctx *ctx, - const css_computed_style *style) + const css_computed_style *style, + css_fixed font_size_default) { css_hint_length size; if (style == NULL) { - size.value = ctx->font_size_default; + size.value = font_size_default; size.unit = CSS_UNIT_PX; } else { enum css_font_size_e status = get_font_size( @@ -391,10 +394,24 @@ static inline css_hint_length css_unit__get_font_size( /* Exported function, documented in unit.h. */ css_error css_unit_compute_absolute_font_size( - const css_unit_len_ctx *ctx, + const css_hint_length *ref_length, + const css_computed_style *root_style, + css_fixed font_size_default, css_hint *size) { - css_hint_length ref_len; + css_hint_length ref_len = { + .value = font_size_default, + .unit = CSS_UNIT_PX, + }; + + if (ref_length != NULL) { + /* Must be absolute. */ + assert(ref_length->unit != CSS_UNIT_EM); + assert(ref_length->unit != CSS_UNIT_EX); + assert(ref_length->unit != CSS_UNIT_PCT); + + ref_len = *ref_length; + } assert(size->status != CSS_FONT_SIZE_INHERIT); @@ -420,20 +437,18 @@ css_error css_unit_compute_absolute_font_size( assert(CSS_FONT_SIZE_XX_SMALL == 1); size->data.length.value = FMUL(factors[size->status - 1], - ctx->font_size_default); + font_size_default); size->data.length.unit = CSS_UNIT_PX; size->status = CSS_FONT_SIZE_DIMENSION; break; } case CSS_FONT_SIZE_LARGER: - ref_len = css_unit__get_font_size(ctx, ctx->ref_style); size->data.length.value = FMUL(ref_len.value, FLTTOFIX(1.2)); size->data.length.unit = ref_len.unit; size->status = CSS_FONT_SIZE_DIMENSION; break; case CSS_FONT_SIZE_SMALLER: - ref_len = css_unit__get_font_size(ctx, ctx->ref_style); size->data.length.value = FDIV(ref_len.value, FLTTOFIX(1.2)); size->data.length.unit = ref_len.unit; size->status = CSS_FONT_SIZE_DIMENSION; @@ -443,9 +458,8 @@ css_error css_unit_compute_absolute_font_size( /* Convert any relative units to absolute. */ switch (size->data.length.unit) { case CSS_UNIT_PCT: - ref_len = css_unit__get_font_size(ctx, ctx->ref_style); - size->data.length.value = FDIV( - FMUL(size->data.length.value, + size->data.length.value = FDIV(FMUL( + size->data.length.value, ref_len.value), INTTOFIX(100)); size->data.length.unit = ref_len.unit; break; @@ -454,11 +468,8 @@ css_error css_unit_compute_absolute_font_size( case CSS_UNIT_EX: /* Fall-through */ case CSS_UNIT_CH: /* Parent relative units. */ - ref_len = css_unit__get_font_size(ctx, ctx->ref_style); - - size->data.length.unit = ref_len.unit; - size->data.length.value = FMUL(size->data.length.value, - ref_len.value); + size->data.length.value = FMUL( + size->data.length.value, ref_len.value); switch (size->data.length.unit) { case CSS_UNIT_EX: @@ -476,15 +487,17 @@ css_error css_unit_compute_absolute_font_size( default: break; } + size->data.length.unit = ref_len.unit; break; case CSS_UNIT_REM: /* Root element relative units. */ - ref_len = css_unit__get_font_size(ctx, ctx->root_style); + ref_len = css_unit__get_font_size(root_style, + font_size_default); size->data.length.unit = ref_len.unit; - size->data.length.value = FMUL(size->data.length.value, - ref_len.value); + size->data.length.value = FMUL( + size->data.length.value, ref_len.value); break; default: diff --git a/src/select/unit.h b/src/select/unit.h index 738c181..e4d21da 100644 --- a/src/select/unit.h +++ b/src/select/unit.h @@ -9,138 +9,7 @@ #ifndef css_select_unit_h_ #define css_select_unit_h_ -/** - * Client callback for font measuring. - * - * \param[in] pw Client data. - * \param[in] style Style to measure font for, or NULL. - * \param[in] unit Either CSS_UNIT_EX, or CSS_UNIT_CH. - * \return length in CSS pixels. - */ -typedef css_fixed (*css_unit_len_measure)( - void *pw, - const css_computed_style *style, - const css_unit unit); - -/** - * LibCSS unit conversion context. - * - * The client callback is optional. It is used for measuring "ch" - * (glyph '0' advance) and "ex" (height of the letter 'x') units. - * If a NULL pointer is given, LibCSS will use a fixed scaling of - * the "em" unit. - */ -typedef struct css_unit_len_ctx { - /** - * Viewport width in CSS pixels. - * Used if unit is vh, vw, vi, vb, vmin, or vmax. - */ - css_fixed viewport_width; - /** - * Viewport height in CSS pixels. - * Used if unit is vh, vw, vi, vb, vmin, or vmax. - */ - css_fixed viewport_height; - /** - * Client default font size in CSS pixels. - */ - css_fixed font_size_default; - /** - * Client minimum font size in CSS pixels. May be zero. - */ - css_fixed font_size_minimum; - /** - * DPI of the device the style is selected for. - */ - css_fixed device_dpi; - /** - * Reference style. Most of the time, this is the element's style. - * When converting a length for a typographical property, such as - * font-size, then this should be the parent node. If the node has - * no parent then this may be NULL. - */ - const css_computed_style *ref_style; - /** - * Computed style for the document root element. - * May be NULL if unit is not rem. - */ - const css_computed_style *root_style; - /** - * Client private word for measure callback. - */ - void *pw; - /** - * Client callback for font measuring. - */ - const css_unit_len_measure measure; -} css_unit_len_ctx; - -/** - * Convert css pixels to physical pixels. - * - * \param[in] css_pixels Length in css pixels. - * \param[in] device_dpi Device dots per inch. - * \return Length in device pixels. - */ -static inline css_fixed css_unit_css2device_px( - const css_fixed css_pixels, - const css_fixed device_dpi) -{ - return FDIV(FMUL(css_pixels, device_dpi), F_96); -} - -/** - * Convert device pixels to css pixels. - * - * \param[in] device_pixels Length in physical pixels. - * \param[in] device_dpi Device dots per inch. - * \return Length in css pixels. - */ -static inline css_fixed css_unit_device2css_px( - const css_fixed device_pixels, - const css_fixed device_dpi) -{ - return FDIV(FMUL(device_pixels, F_96), device_dpi); -} - -/** - * Convert a length to points (pt). - * - * \param[in] ctx Length unit conversion context. - * \param[in] length Length to convert. - * \param[in] unit Current unit of length. - * \return A length in points. - */ -css_fixed css_unit_font_size_len2pt( - const css_unit_len_ctx *ctx, - const css_fixed length, - const css_unit unit); - -/** - * Convert a length to CSS pixels. - * - * \param[in] ctx Length unit conversion context. - * \param[in] length Length to convert. - * \param[in] unit Current unit of length. - * \return A length in CSS pixels. - */ -css_fixed css_unit_len2css_px( - const css_unit_len_ctx *ctx, - const css_fixed length, - const css_unit unit); - -/** - * Convert a length to device pixels. - * - * \param[in] ctx Length unit conversion context. - * \param[in] length Length to convert. - * \param[in] unit Current unit of length. - * \return A length in device pixels. - */ -css_fixed css_unit_len2device_px( - const css_unit_len_ctx *ctx, - const css_fixed length, - const css_unit unit); +#include <libcss/unit.h> /** * Convert a length to CSS pixels for a media query context. @@ -158,12 +27,16 @@ css_fixed css_unit_len2px_mq( /** * Convert relative font size units to absolute units. * - * \param[in] ctx Length unit conversion context. - * \param[in,out] size The length to convert. + * \param[in] ref_length Reference font-size length or NULL. + * \param[in] root_style Root element style or NULL. + * \param[in] font_size_default Client default font size in CSS pixels. + * \param[in,out] size The length to convert. * \return CSS_OK on success, or appropriate error otherwise. */ css_error css_unit_compute_absolute_font_size( - const css_unit_len_ctx *ctx, + const css_hint_length *ref_length, + const css_computed_style *root_style, + css_fixed font_size_default, css_hint *size); #endif |