From fac1ad8641848a3d747b6c495cf443ccc29c077e Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Sat, 7 Mar 2015 21:41:28 +0000 Subject: After composing styles, intern the result in the style sharing arena. Note this changes the API. Selection tests updated. --- include/libcss/computed.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include/libcss/computed.h') diff --git a/include/libcss/computed.h b/include/libcss/computed.h index 3470da8..0009267 100644 --- a/include/libcss/computed.h +++ b/include/libcss/computed.h @@ -83,12 +83,12 @@ css_error css_computed_style_initialise(css_computed_style *style, struct css_select_handler *handler, void *pw); css_error css_computed_style_compose(const css_computed_style *parent, - const css_computed_style *child, + css_computed_style *child, css_error (*compute_font_size)(void *pw, const struct css_hint *parent, struct css_hint *size), void *pw, - css_computed_style *result); + css_computed_style **result); /****************************************************************************** * Property accessors below here * -- cgit v1.2.3 From fb095fb2411d3127df035d93b7b33ff6064ad2e9 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Mon, 2 Nov 2015 16:10:36 +0000 Subject: Intern partial styles. Note this changes the public API. We can't compose directly over child style now, since it may be interned. --- include/libcss/computed.h | 7 ++-- include/libcss/select.h | 3 ++ src/select/computed.c | 30 +++++++++++----- src/select/computed.h | 17 +++++++++ src/select/select.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ test/select-common.c | 5 ++- 6 files changed, 140 insertions(+), 13 deletions(-) (limited to 'include/libcss/computed.h') diff --git a/include/libcss/computed.h b/include/libcss/computed.h index 0009267..db3e3e4 100644 --- a/include/libcss/computed.h +++ b/include/libcss/computed.h @@ -82,13 +82,14 @@ css_error css_computed_style_destroy(css_computed_style *style); css_error css_computed_style_initialise(css_computed_style *style, struct css_select_handler *handler, void *pw); -css_error css_computed_style_compose(const css_computed_style *parent, - css_computed_style *child, +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, - css_computed_style **result); + css_computed_style **restrict result); /****************************************************************************** * Property accessors below here * diff --git a/include/libcss/select.h b/include/libcss/select.h index 74fa5d2..3f1e14a 100644 --- a/include/libcss/select.h +++ b/include/libcss/select.h @@ -217,6 +217,9 @@ css_error css_select_ctx_count_sheets(css_select_ctx *ctx, uint32_t *count); css_error css_select_ctx_get_sheet(css_select_ctx *ctx, uint32_t index, const css_stylesheet **sheet); +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, uint64_t media, const css_stylesheet *inline_style, css_select_handler *handler, void *pw, diff --git a/src/select/computed.c b/src/select/computed.c index 107c884..2d3aea8 100644 --- a/src/select/computed.c +++ b/src/select/computed.c @@ -277,23 +277,34 @@ css_error css_computed_style_initialise(css_computed_style *style, * \param child Child style * \param compute_font_size Function to compute an absolute font size * \param pw Client data for compute_font_size - * \param result Pointer to style to compose into + * \param result Updated to point to new composed style + * Ownership passed to client. * \return CSS_OK on success, appropriate error otherwise. * * \pre \a parent is a fully composed style (thus has no inherited properties) - * - * \note \a child and \a result may point at the same object */ -css_error css_computed_style_compose(const css_computed_style *parent, - css_computed_style *child, +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, - css_computed_style **result) + css_computed_style **restrict result) { - css_error error = CSS_OK; + css_computed_style *composed; + css_error error; size_t i; + /* TODO: + * Make this function take a composition context, to allow us + * to avoid the churn of unnecesaraly allocating and freeing + * the memory to compose styles into. + */ + error = css_computed_style_create(&composed); + if (error != CSS_OK) { + return error; + } + /* Iterate through the properties */ for (i = 0; i < CSS_N_PROPERTIES; i++) { /* Skip any in extension blocks if the block does not exist */ @@ -316,18 +327,19 @@ css_error css_computed_style_compose(const css_computed_style *parent, } /* Compose the property */ - error = prop_dispatch[i].compose(parent, child, *result); + error = prop_dispatch[i].compose(parent, child, composed); if (error != CSS_OK) break; } /* Finally, compute absolute values for everything */ - error = css__compute_absolute_values(parent, *result, + error = css__compute_absolute_values(parent, composed, compute_font_size, pw); if (error != CSS_OK) { return error; } + *result = composed; return css__arena_intern_style(result); } diff --git a/src/select/computed.h b/src/select/computed.h index f965993..230711d 100644 --- a/src/select/computed.h +++ b/src/select/computed.h @@ -325,6 +325,23 @@ struct css_computed_style { uint32_t bin; }; + +/** + * Take a new reference to a computed style + * + * \param style The style to take a new reference to. + * \return The new computed style reference + */ +static inline css_computed_style * css__computed_style_ref( + css_computed_style *style) +{ + if (style == NULL) + return NULL; + + style->count++; + return style; +} + css_error css__computed_uncommon_destroy(css_computed_uncommon *uncommon); css_error css__compute_absolute_values(const css_computed_style *parent, diff --git a/src/select/select.c b/src/select/select.c index 367c26a..a1ec746 100644 --- a/src/select/select.c +++ b/src/select/select.c @@ -15,6 +15,7 @@ #include "bytecode/bytecode.h" #include "bytecode/opcodes.h" #include "stylesheet.h" +#include "select/arena.h" #include "select/computed.h" #include "select/dispatch.h" #include "select/hash.h" @@ -74,6 +75,9 @@ struct css_select_ctx { lwc_string *first_letter; lwc_string *before; lwc_string *after; + + /* Interned default style */ + css_computed_style *default_style; }; /** @@ -258,6 +262,9 @@ css_error css_select_ctx_destroy(css_select_ctx *ctx) destroy_strings(ctx); + if (ctx->default_style != NULL) + css_computed_style_destroy(ctx->default_style); + if (ctx->sheets != NULL) free(ctx->sheets); @@ -406,6 +413,78 @@ css_error css_select_ctx_get_sheet(css_select_ctx *ctx, uint32_t index, return CSS_OK; } + +/** + * Create a default style on the selection context + * + * \param ctx Context to create default style in + * \param handler Dispatch table of handler functions + * \param pw Client-specific private data for handler functions + * \return CSS_OK on success, appropriate error otherwise + */ +static css_error css__select_ctx_create_default_style(css_select_ctx *ctx, + css_select_handler *handler, void *pw) +{ + css_computed_style *style; + css_error error; + + /* Need to construct the default style */ + error = css_computed_style_create(&style); + if (error != CSS_OK) + return error; + + error = css_computed_style_initialise(style, handler, pw); + if (error != CSS_OK) { + css_computed_style_destroy(style); + return error; + } + + /* Neither create nor initialise intern the style, so intern it now */ + error = css__arena_intern_style(&style); + if (error != CSS_OK) + return error; + + /* Store it on the ctx */ + ctx->default_style = style; + + return CSS_OK; +} + + +/** + * Get a default style, e.g. for an implied element's anonamous box + * + * \param ctx Selection context (used to avoid recreating default) + * \param handler Dispatch table of handler functions + * \param pw Client-specific private data for handler functions + * \param style Pointer to location to receive default style + * \return CSS_OK on success, appropriate error otherwise. + */ +css_error css_select_default_style(css_select_ctx *ctx, + css_select_handler *handler, void *pw, + css_computed_style **style) +{ + css_error error; + + if (ctx == NULL || style == NULL || handler == NULL || + handler->handler_version != + CSS_SELECT_HANDLER_VERSION_1) + return CSS_BADPARM; + + /* Ensure the ctx has a default style */ + if (ctx->default_style == NULL) { + error = css__select_ctx_create_default_style(ctx, handler, pw); + if (error != CSS_OK) { + return error; + } + } + + /* Pass a ref back to the client */ + *style = css__computed_style_ref(ctx->default_style); + return CSS_OK; +} + + /** * Select a style for the given node * @@ -659,6 +738,18 @@ css_error css_select_style(css_select_ctx *ctx, void *node, goto cleanup; } + /* Intern the partial computed styles */ + for (j = CSS_PSEUDO_ELEMENT_NONE; j < CSS_PSEUDO_ELEMENT_COUNT; j++) { + /* Skip non-existent pseudo elements */ + if (state.results->styles[j] == NULL) + continue; + + error = css__arena_intern_style(&state.results->styles[j]); + if (error != CSS_OK) { + goto cleanup; + } + } + /* Add node name to bloom */ if (lwc_string_caseless_hash_value(state.element.name, diff --git a/test/select-common.c b/test/select-common.c index c6d33c0..b4f588a 100644 --- a/test/select-common.c +++ b/test/select-common.c @@ -769,11 +769,14 @@ static void run_test_select_tree(css_select_ctx *select, &select_handler, ctx, &sr) == CSS_OK); if (node->parent != NULL) { + css_computed_style *composed; assert(css_computed_style_compose( node->parent->sr->styles[ctx->pseudo_element], sr->styles[ctx->pseudo_element], compute_font_size, NULL, - &(sr->styles[ctx->pseudo_element])) == CSS_OK); + &composed) == CSS_OK); + css_computed_style_destroy(sr->styles[ctx->pseudo_element]); + sr->styles[ctx->pseudo_element] = composed; } node->sr = sr; -- cgit v1.2.3 From 03824fd772e44b4cd281dd893df07d991cac9e41 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Mon, 2 Nov 2015 22:21:53 +0000 Subject: Remove redundant API surface. --- include/libcss/computed.h | 4 ---- src/select/computed.c | 6 +++--- src/select/computed.h | 6 ++++++ src/select/select.c | 10 +++++----- 4 files changed, 14 insertions(+), 12 deletions(-) (limited to 'include/libcss/computed.h') diff --git a/include/libcss/computed.h b/include/libcss/computed.h index db3e3e4..c7e77e0 100644 --- a/include/libcss/computed.h +++ b/include/libcss/computed.h @@ -76,12 +76,8 @@ typedef struct css_computed_content_item { } data; } css_computed_content_item; -css_error css_computed_style_create(css_computed_style **result); css_error css_computed_style_destroy(css_computed_style *style); -css_error css_computed_style_initialise(css_computed_style *style, - struct css_select_handler *handler, void *pw); - css_error css_computed_style_compose( const css_computed_style *restrict parent, const css_computed_style *restrict child, diff --git a/src/select/computed.c b/src/select/computed.c index 1fe6a81..2efc8a7 100644 --- a/src/select/computed.c +++ b/src/select/computed.c @@ -65,7 +65,7 @@ static css_error compute_absolute_length_pair(css_computed_style *style, * CSS_NOMEM on memory exhaustion, * CSS_BADPARM on bad parameters. */ -css_error css_computed_style_create(css_computed_style **result) +css_error css__computed_style_create(css_computed_style **result) { css_computed_style *s; @@ -239,7 +239,7 @@ css_error css_computed_style_destroy(css_computed_style *style) * \param pw Client-specific private data for handler functions * \return CSS_OK on success. */ -css_error css_computed_style_initialise(css_computed_style *style, +css_error css__computed_style_initialise(css_computed_style *style, css_select_handler *handler, void *pw) { css_select_state state; @@ -300,7 +300,7 @@ css_error css_computed_style_compose( * to avoid the churn of unnecesaraly allocating and freeing * the memory to compose styles into. */ - error = css_computed_style_create(&composed); + error = css__computed_style_create(&composed); if (error != CSS_OK) { return error; } diff --git a/src/select/computed.h b/src/select/computed.h index 230711d..57981b2 100644 --- a/src/select/computed.h +++ b/src/select/computed.h @@ -342,6 +342,12 @@ static inline css_computed_style * css__computed_style_ref( return style; } +css_error css__computed_style_create(css_computed_style **result); + +css_error css__computed_style_initialise(css_computed_style *style, + struct css_select_handler *handler, void *pw); + + css_error css__computed_uncommon_destroy(css_computed_uncommon *uncommon); css_error css__compute_absolute_values(const css_computed_style *parent, diff --git a/src/select/select.c b/src/select/select.c index a1ec746..f2cb6d5 100644 --- a/src/select/select.c +++ b/src/select/select.c @@ -429,11 +429,11 @@ static css_error css__select_ctx_create_default_style(css_select_ctx *ctx, css_error error; /* Need to construct the default style */ - error = css_computed_style_create(&style); + error = css__computed_style_create(&style); if (error != CSS_OK) return error; - error = css_computed_style_initialise(style, handler, pw); + error = css__computed_style_initialise(style, handler, pw); if (error != CSS_OK) { css_computed_style_destroy(style); return error; @@ -543,7 +543,7 @@ css_error css_select_style(css_select_ctx *ctx, void *node, state.results->styles[i] = NULL; /* Base element style is guaranteed to exist */ - error = css_computed_style_create( + error = css__computed_style_create( &state.results->styles[CSS_PSEUDO_ELEMENT_NONE]); if (error != CSS_OK) { free(state.results); @@ -626,7 +626,7 @@ css_error css_select_style(css_select_ctx *ctx, void *node, struct css_computed_style *computed_style = state.results->styles[CSS_PSEUDO_ELEMENT_NONE]; if (computed_style == NULL) { - error = css_computed_style_create(&computed_style); + error = css__computed_style_create(&computed_style); if (error != CSS_OK) goto cleanup; } @@ -1799,7 +1799,7 @@ css_error match_selector_chain(css_select_ctx *ctx, /* Ensure that the appropriate computed style exists */ if (state->results->styles[pseudo] == NULL) { - error = css_computed_style_create( + error = css__computed_style_create( &state->results->styles[pseudo]); if (error != CSS_OK) return error; -- cgit v1.2.3