From 65d4fd6e83d421e7fa7a8c7df44d01797e3c69ae Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Thu, 18 Mar 2021 09:16:30 +0000 Subject: 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. --- include/libcss/computed.h | 6 +- include/libcss/select.h | 5 +- include/libcss/unit.h | 156 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 include/libcss/unit.h (limited to 'include') diff --git a/include/libcss/computed.h b/include/libcss/computed.h index f4b3e21..1587d78 100644 --- a/include/libcss/computed.h +++ b/include/libcss/computed.h @@ -19,6 +19,7 @@ extern "C" #include #include #include +#include struct css_hint; struct css_select_handler; @@ -81,10 +82,7 @@ css_error css_computed_style_destroy(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 struct css_hint *parent, - struct css_hint *size), - void *pw, + const css_unit_len_ctx *unit_len_ctx, css_computed_style **restrict result); /****************************************************************************** diff --git a/include/libcss/select.h b/include/libcss/select.h index ca57456..bfaf531 100644 --- a/include/libcss/select.h +++ b/include/libcss/select.h @@ -18,6 +18,7 @@ extern "C" #include #include #include +#include typedef enum css_pseudo_element { CSS_PSEUDO_ELEMENT_NONE = 0, @@ -123,9 +124,6 @@ typedef struct css_select_handler { css_error (*ua_default_for_property)(void *pw, uint32_t property, css_hint *hint); - css_error (*compute_font_size)(void *pw, const css_hint *parent, - css_hint *size); - /** * Set libcss_node_data on a DOM node. * @@ -221,6 +219,7 @@ css_error css_select_default_style(css_select_ctx *ctx, css_select_handler *handler, void *pw, css_computed_style **style); 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); diff --git a/include/libcss/unit.h b/include/libcss/unit.h new file mode 100644 index 0000000..a847077 --- /dev/null +++ b/include/libcss/unit.h @@ -0,0 +1,156 @@ +/* + * This file is part of LibCSS. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2008 John-Mark Bell + */ + +#ifndef libcss_unit_h_ +#define libcss_unit_h_ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +/** + * 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; + /** + * Computed style for the document root element, needed for rem units. + * May be NULL, in which case font_size_default is used instead, as + * would be the case if rem unit is used on the root element. + */ + const css_computed_style *root_style; + /** + * Optional client private word for measure callback. + */ + void *pw; + /** + * Optional 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] style Style to perform conversion for or NULL. + * \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_computed_style *style, + const css_unit_len_ctx *ctx, + const css_fixed length, + const css_unit unit); + +/** + * Convert a length to CSS pixels. + * + * \param[in] style Style to perform conversion for or NULL. + * \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_computed_style *style, + const css_unit_len_ctx *ctx, + const css_fixed length, + const css_unit unit); + +/** + * Convert a length to device pixels. + * + * \param[in] style Style to perform conversion for or NULL. + * \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_computed_style *style, + const css_unit_len_ctx *ctx, + const css_fixed length, + const css_unit unit); + +#ifdef __cplusplus +} +#endif + +#endif + -- cgit v1.2.1