Styles
resolve
resolve() -> style
You can use this to combine the style in ctx
, the style given by a user for a single element and an element's default style.
base
is first merged onto dict
without overwriting existing values, and if root
is given it is merged onto that key of dict
. merge
is then merged onto dict
but does overwrite existing entries, if root
is given it is merged onto that key of dict
. Then entries in dict
that are auto inherit values from their nearest ancestor and entries of type dictionary are merged with their closest ancestor.
#let dict = (
stroke: "black",
fill: none,
mark: (stroke: auto, fill: "blue"),
line: (stroke: auto, mark: auto, fill: "red")
)
#cetz.styles.resolve(dict, merge: (mark: (stroke: "yellow")), root: "line")
The following is a more detailed explanation of how the algorithm works to use as a reference if needed. It should be updated whenever changes are made.
Remember that dictionaries are recursively merged, if an entry is any other type it is simply updated. (dict + dict = merged dict, value + dict = dict, dict + value = value)
First if base
is given, it will be merged without overwriting values onto dict
. If root
is given it will be merged onto that key of dict
.
Each level of dict
is then processed with these steps. If root
is given the level with that key will be the first, otherwise the whole of dict
is processed.
- Values on the corresponding level of
merge
are inserted into the level if the key does not exist on the level or if they are not both dictionaries. If they are both dictionaries their values will be inserted in the same stage at a lower level. - If an entry is
auto
or a dictionary, the tree is travelled back up until an entry with the same key is found. If the current entry isauto
the value of the ancestor's entry is copied. Or if the current entry and ancestor entry is a dictionary, they are merged with the current entry overwriting any values in it's ancestors. - Each entry that is a dictionary is then resolved from step 1.
get-ctx(ctx => {
// Get the current "mark" style
content((0,0), [#cetz.styles.resolve(ctx.style, root: "mark")])
})
Style values overwriting the current style. I.e. inline styles passed with an element: line(.., stroke: red)
.