Lines
80.57 %
Functions
70.75 %
Branches
100 %
//! Default icon resolver implementations for Azul
//!
//! This module provides the standard callback implementations for icon resolution.
//! The core types and resolution infrastructure are in `azul_core::icon`.
//! # Usage
//! ```rust,ignore
//! use azul_core::icon::IconProviderHandle;
//! use azul_layout::icon::{default_icon_resolver, ImageIconData, FontIconData};
//! // Create provider with the default resolver
//! let provider = IconProviderHandle::with_resolver(default_icon_resolver);
//! // Register an image icon
//! provider.register_icon("app-images", "logo", RefAny::new(ImageIconData {
//! image: image_ref, width: 32.0, height: 32.0
//! }));
//! // Register a font icon
//! provider.register_icon("material-icons", "home", RefAny::new(FontIconData {
//! font: font_ref, icon_char: "\u{e88a}".to_string()
//! ```
use alloc::{
string::{String, ToString},
vec::Vec,
};
use azul_core::{
dom::{Dom, NodeData},
icon::IconProviderHandle,
refany::{OptionRefAny, RefAny},
resources::ImageRef,
styled_dom::StyledDom,
use azul_css::{
css::{Css, CssPropertyValue},
dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec},
props::{
basic::{length::FloatValue, FontRef, StyleFontFamily, StyleFontFamilyVec},
layout::{LayoutHeight, LayoutWidth},
property::CssProperty,
style::{
filter::{StyleColorMatrix, StyleFilter, StyleFilterVec},
text::StyleTextColor,
},
system::SystemStyle,
// ============================================================================
// Icon Data Marker Structs (for RefAny::downcast)
/// Image-based icon data stored in `RefAny` for the icon resolver.
///
/// Pass to `register_image_icon` or wrap in `RefAny::new(...)` and register
/// directly via `IconProviderHandle::register_icon`.
#[derive(Debug)]
pub struct ImageIconData {
pub image: ImageRef,
/// The icon's NATURAL WIDTH in logical px - the size it is drawn at, not
/// the width of the bitmap behind it.
/// The two differ whenever the bitmap is oversampled: a 16px indicator
/// rasterised from an SVG at 32x32 so it stays crisp on a 2x display is
/// still a 16px indicator, and laying it out at 32 makes it twice the
/// size the desktop draws it (this is exactly how a window-close button
/// once came out as a giant X). [`register_image_icon`] takes the bitmap's
/// own size, which is right for a 1:1 bitmap; an oversampled one must say
/// its natural size with [`register_image_icon_sized`].
pub width: f32,
/// The icon's natural HEIGHT in logical px - see [`Self::width`].
pub height: f32,
}
/// Font-based icon data stored in `RefAny` for the icon resolver.
/// Pass to `register_font_icon` or wrap in `RefAny::new(...)` and register
pub struct FontIconData {
pub font: FontRef,
/// The character/codepoint for this specific icon (e.g., "\u{e88a}" for home)
pub icon_char: String,
/// An icon that IS a `Dom` - the general case, of which image and font icons
/// are special cases.
/// Register any DOM as an icon and refer to it by name everywhere an icon spec
/// is taken (`<icon>my-logo</icon>`, a tray icon, an app icon). Because the DOM
/// carries its own inline styles AND its own stylesheets (`Dom::css`), the icon
/// decides what it looks like - colour included - instead of every call site
/// having to thread a tint parameter down to the renderer.
/// The whole subtree is spliced in before the cascade, so a DOM icon can be any
/// number of nodes with any styling, not just a single glyph.
#[derive(Debug, Clone)]
pub struct DomIconData {
pub dom: Dom,
impl DomIconData {
#[must_use]
pub const fn new(dom: Dom) -> Self {
Self { dom }
// Default Icon Resolver
/// Default icon resolver that handles both image and font icons.
/// Resolution logic:
/// 1. If `icon_data` is None -> return empty div (icon not found)
/// 2. If `icon_data` contains `ImageIconData` -> render as image
/// 3. If `icon_data` contains `FontIconData` -> render as text with font
/// 4. Unknown data type -> return empty div
/// Styles from the original icon DOM are copied to the result,
/// filtered based on `SystemStyle` preferences.
pub extern "C" fn default_icon_resolver(
icon_data: OptionRefAny,
original_icon_node: &NodeData,
system_style: &SystemStyle,
) -> Dom {
// No icon found -> empty div
let Some(mut data) = icon_data.into_option() else {
return Dom::create_div();
// A registered Dom: the icon IS a DOM, spliced in whole. This is what lets
// an icon carry its own styling - colour above all - instead of every call
// site having to pass a tint down. Checked FIRST because it is the most
// specific: a caller who registered a whole DOM meant that DOM.
if let Some(dom_icon) = data.downcast_ref::<DomIconData>() {
return create_dom_icon_from_original(&dom_icon, original_icon_node);
// Try ImageIconData
if let Some(img) = data.downcast_ref::<ImageIconData>() {
return create_image_icon_from_original(&img, original_icon_node, system_style);
// Try FontIconData
if let Some(font_icon) = data.downcast_ref::<FontIconData>() {
return create_font_icon_from_original(&font_icon, original_icon_node, system_style);
// Unknown data type -> empty div
Dom::create_div()
/// An icon that IS a `Dom`: whatever the caller registered, spliced in whole.
/// The call site's styles are applied FIRST and the registered DOM's own styles
/// second, so the icon wins on anything it specifies (colour, background,
/// borders) while still inheriting what it does not (the size the tray or the
/// surrounding text asked for). That ordering is the point: an icon knows what
/// it should look like, a call site knows how big it should be.
fn create_dom_icon_from_original(icon: &DomIconData, original: &NodeData) -> Dom {
let mut dom = icon.dom.clone();
let mut props = copy_appropriate_styles_vec(original);
if props.is_empty() {
return dom;
// The registered DOM's own declarations go last so they override.
props.extend(copy_appropriate_styles_vec(&dom.root));
dom.root
.set_css_props(CssPropertyWithConditionsVec::from_vec(props));
dom
// Icon DOM Creation (from original)
/// Create a `StyledDom` for an image-based icon, copying styles from original.
/// Applies SystemStyle-aware modifications:
/// - Grayscale filter if `prefer_grayscale` is true
/// - Tint color overlay if `tint_color` is set
fn create_image_icon_from_original(
img: &ImageIconData,
original: &NodeData,
let mut dom = Dom::create_image(img.image.clone());
// Copy appropriate styles from original
{
let original_node = original;
let mut props_vec = copy_appropriate_styles_vec(original_node);
// Add default dimensions if not specified in original styles
let has_width = props_vec
.iter()
.any(|p| matches!(&p.property, CssProperty::Width(_)));
let has_height = props_vec
.any(|p| matches!(&p.property, CssProperty::Height(_)));
if !has_width {
props_vec.push(CssPropertyWithConditions::simple(CssProperty::width(
LayoutWidth::px(img.width),
)));
if !has_height {
props_vec.push(CssPropertyWithConditions::simple(CssProperty::height(
LayoutHeight::px(img.height),
// Apply SystemStyle-aware filters
apply_icon_style_filters(&mut props_vec, system_style);
.set_css_props(CssPropertyWithConditionsVec::from_vec(props_vec));
// Copy accessibility info
if let Some(a11y) = original_node.get_accessibility_info() {
dom = dom.with_accessibility_info(a11y.clone());
/// Create a `StyledDom` for a font-based icon, copying styles from original.
/// - Text color override if `inherit_text_color` is true
/// - Tint color if `tint_color` is set
fn create_font_icon_from_original(
font_icon: &FontIconData,
// A SPAN, not a bare text node and not a <p>.
//
// The glyph carries css (font-family, colour, tint) and frequently sits as
// a direct item of a flex or inline-flex container — the engine warned about
// exactly that: "text node ... is one of 2 items in a InlineFlex container".
// A bare text run has no box, so the css is INERT and the glyph competes as
// a flex item with nothing to size. A <p> would give it a box and get the
// flow wrong: an icon belongs INLINE inside its label, not as a block
// paragraph beside it. `span` is the one that is both boxed and inline.
let mut dom = Dom::create_span_with_text(font_icon.icon_char.clone());
// Add font family
let font_prop = CssPropertyWithConditions::simple(CssProperty::font_family(
StyleFontFamilyVec::from_vec(vec![StyleFontFamily::Ref(font_icon.font.clone())]),
));
props_vec.push(font_prop);
// Apply SystemStyle-aware color modifications for font icons
apply_font_icon_color(&mut props_vec, system_style);
/// Copy styles from original node
/// Returns a Vec for easier manipulation
fn copy_appropriate_styles_vec(original_node: &NodeData) -> Vec<CssPropertyWithConditions> {
// Reconstruct the legacy flat list from the unified Css store.
original_node
.get_style()
.iter_inline_properties()
.map(|(prop, conds)| CssPropertyWithConditions {
property: prop.clone(),
apply_if: conds.clone(),
})
.collect()
/// Apply SystemStyle-aware filters to icon properties.
/// This adds CSS filters based on accessibility and theming settings:
fn apply_icon_style_filters(
props_vec: &mut Vec<CssPropertyWithConditions>,
) {
let icon_style = &system_style.icon_style;
// Collect filters to apply
let mut filters = Vec::new();
// Grayscale filter: Uses a color matrix that converts to grayscale
// Standard luminance weights: R*0.2126 + G*0.7152 + B*0.0722
if icon_style.prefer_grayscale {
// Grayscale color matrix (4x5):
// [0.2126, 0.7152, 0.0722, 0, 0] <- R output
// [0.2126, 0.7152, 0.0722, 0, 0] <- G output
// [0.2126, 0.7152, 0.0722, 0, 0] <- B output
// [0, 0, 0, 1, 0] <- A output
let grayscale_matrix = StyleColorMatrix {
m0: FloatValue::new(0.2126),
m1: FloatValue::new(0.7152),
m2: FloatValue::new(0.0722),
m3: FloatValue::new(0.0),
m4: FloatValue::new(0.0),
m5: FloatValue::new(0.2126),
m6: FloatValue::new(0.7152),
m7: FloatValue::new(0.0722),
m8: FloatValue::new(0.0),
m9: FloatValue::new(0.0),
m10: FloatValue::new(0.2126),
m11: FloatValue::new(0.7152),
m12: FloatValue::new(0.0722),
m13: FloatValue::new(0.0),
m14: FloatValue::new(0.0),
m15: FloatValue::new(0.0),
m16: FloatValue::new(0.0),
m17: FloatValue::new(0.0),
m18: FloatValue::new(1.0),
m19: FloatValue::new(0.0),
filters.push(StyleFilter::ColorMatrix(grayscale_matrix));
// Apply tint color as a flood filter if specified
if let azul_css::props::basic::color::OptionColorU::Some(tint) = &icon_style.tint_color {
filters.push(StyleFilter::Flood(*tint));
// Add filters if any were collected
if !filters.is_empty() {
props_vec.push(CssPropertyWithConditions::simple(CssProperty::Filter(
CssPropertyValue::Exact(StyleFilterVec::from_vec(filters)),
/// Apply SystemStyle-aware color modifications for font icons.
/// Font icons can use text color directly, so we can:
/// - Apply tint color as text color
/// - Inherit text color from parent
fn apply_font_icon_color(
// If tint color is specified, use it as the text color
props_vec.push(CssPropertyWithConditions::simple(CssProperty::TextColor(
CssPropertyValue::Exact(StyleTextColor { inner: *tint }),
// Note: inherit_text_color doesn't need explicit handling - text color
// is inherited by default in CSS. We only need to NOT override it.
// IconProviderHandle Helper Functions
/// Register an image icon in a pack
pub fn register_image_icon(
provider: &mut IconProviderHandle,
pack_name: &str,
icon_name: &str,
image: ImageRef,
// The bitmap's own size IS the natural size for a 1:1 bitmap. An
// OVERSAMPLED one (an SVG rasterised at 2x for a HiDPI display) must use
// `register_image_icon_sized` instead, or it lays out at twice the size
// it is drawn at.
let size = image.get_size();
register_image_icon_sized(
provider,
pack_name,
icon_name,
image,
size.width,
size.height,
);
/// Register an image icon whose NATURAL SIZE differs from its bitmap's.
/// `width`/`height` are the logical px the icon occupies in layout; the
/// bitmap may be larger (an SVG rasterised at 2x or 3x so it stays crisp when
/// the display or the zoom scales it up). This is the difference between
/// "how big is this picture" and "how big is this icon", and only the caller
/// that rasterised it knows the second: a freedesktop theme states it in the
/// directory the file came from (`actions/16`, `actions/22`, ...), and a
/// `scalable` icon has whatever size its consumer intends.
/// A non-finite or non-positive size is IGNORED in favour of the bitmap's own
/// - a zero-sized icon is invisible, which is a worse failure than a
/// wrong-sized one, and NaN would poison layout.
pub fn register_image_icon_sized(
width: f32,
height: f32,
let bitmap = image.get_size();
let sane = |asked: f32, fallback: f32| {
if asked.is_finite() && asked > 0.0 {
asked
} else {
fallback
let data = ImageIconData {
width: sane(width, bitmap.width),
height: sane(height, bitmap.height),
provider.register_icon(pack_name, icon_name, RefAny::new(data));
/// Register icons from a ZIP file (file names become icon names)
#[cfg(feature = "zip")]
pub fn register_icons_from_zip(
zip_bytes: &[u8],
for (icon_name, image, width, height) in load_images_from_zip(zip_bytes) {
width,
height,
provider.register_icon(pack_name, &icon_name, RefAny::new(data));
#[cfg(not(feature = "zip"))]
_provider: &mut IconProviderHandle,
_pack_name: &str,
_zip_bytes: &[u8],
// ZIP support not enabled — the caller explicitly handed us an icon pack
// and NOTHING got registered; every later lookup will just miss. Say so
// once (this gate is hit by DEFAULT builds: `zip` is not a default
// feature of azul-layout).
static ANNOUNCE: std::sync::Once = std::sync::Once::new();
ANNOUNCE.call_once(|| {
eprintln!(
"[azul][icons] register_icons_from_zip called, but this build has no `zip` feature — \
NO icons were registered from the pack. Rebuild azul-layout with the `zip` (+ \
`image_decoding`) features"
});
/// Register a font icon in a pack
/// Register an arbitrary `Dom` as an icon.
/// The general case: an icon is just a DOM, and image / font icons are special
/// cases of it. Whatever you register is spliced in wherever that icon name is
/// used - an `<icon>` node, a tray icon, an app icon - and it keeps its own
/// styling, because the DOM carries both inline properties and its own
/// stylesheets (`Dom::css`).
/// That is what makes colour work without a tint parameter: an icon that should
/// be red says so itself, once, instead of every call site having to thread a
/// colour down to the renderer. Sizing still comes from the call site, since the
/// caller is the one who knows how big the icon has to be.
pub fn register_dom_icon(
dom: Dom,
provider.register_icon(pack_name, icon_name, RefAny::new(DomIconData::new(dom)));
pub fn register_font_icon(
font: FontRef,
icon_char: &str,
let data = FontIconData {
font,
icon_char: icon_char.to_string(),
// ZIP Support
/// Load all images from a ZIP file, returning (`icon_name`, `ImageRef`, width, height)
#[cfg(all(feature = "zip", feature = "image_decoding"))]
#[allow(clippy::cast_precision_loss)] // bounded graphics/coord/counter/fixed-point cast
fn load_images_from_zip(zip_bytes: &[u8]) -> Vec<(String, ImageRef, f32, f32)> {
use std::path::Path;
use crate::{
image::decode::{decode_raw_image_from_any_bytes, ResultRawImageDecodeImageError},
zip::{ZipFile, ZipReadConfig},
let mut result = Vec::new();
let config = ZipReadConfig::default();
let Ok(entries) = ZipFile::list(zip_bytes, &config) else {
return result;
for entry in &entries {
if entry.path.ends_with('/') {
continue;
} // Skip directories
let Ok(Some(file_bytes)) = ZipFile::get_single_file(zip_bytes, entry, &config) else {
// Decode as image
if let ResultRawImageDecodeImageError::Ok(raw_image) =
decode_raw_image_from_any_bytes(&file_bytes)
// Icon name = filename without extension
let path = Path::new(&entry.path);
let icon_name = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_string();
let width = raw_image.width as f32;
let height = raw_image.height as f32;
if let Some(image) = ImageRef::new_rawimage(raw_image) {
result.push((icon_name, image, width, height));
result
#[cfg(not(all(feature = "zip", feature = "image_decoding")))]
fn load_images_from_zip(_zip_bytes: &[u8]) -> Vec<(String, ImageRef, f32, f32)> {
// Only reachable when `zip` is on but `image_decoding` is off (the
// zip-off case never calls this) — same silent-empty trap, so say so.
"[azul][icons] icon ZIP was readable but this build has no `image_decoding` feature — \
0 images decoded, NO icons registered"
Vec::new()
// Material Icons Registration
/// Register all Material Icons in the provider.
/// This registers all 2234 Material Icons from the `material-icons` crate.
/// Each icon is registered under the "material-icons" pack with its HTML name
/// (e.g., "home", "settings", "`arrow_back`", etc.).
/// Requires the "icons" feature with material-icons crate.
#[cfg(feature = "icons")]
pub fn register_material_icons(provider: &mut IconProviderHandle, font: &FontRef) {
use material_icons::{icon_to_char, icon_to_html_name, ALL_ICONS};
// Register all Material Icons with their Unicode codepoints
for icon in &ALL_ICONS {
let icon_char = icon_to_char(*icon);
let name = icon_to_html_name(icon);
font: font.clone(),
provider.register_icon("material-icons", name, RefAny::new(data));
#[cfg(not(feature = "icons"))]
pub fn register_material_icons(_provider: &mut IconProviderHandle, _font: FontRef) {
// Icons feature not enabled — the caller asked for 2234 Material Icons
// and got zero, silently. Say so once.
"[azul][icons] register_material_icons called, but this build has no `icons` feature \
— NO Material Icons were registered"
/// Load the embedded Material Icons font and register all standard icons.
/// This uses the `material-icons` crate which embeds the Material Icons TTF font.
/// The font is Apache 2.0 licensed by Google.
/// Returns true if registration was successful.
/// Register all Material Icons from caller-supplied TTF bytes.
/// The font bytes are NOT embedded here. `azul-doc codegen all` generates
/// `target/codegen/material_icons.ttf.br`, and `azul-doc` builds (depends
/// on) `azul-layout` — so `include!`ing that generated artifact in this
/// crate is a build cycle (it bit us on `cargo clean`). The `include!` +
/// brotli-decompression live in `azul-dll` (downstream of codegen), which
/// passes the decompressed TTF in here.
#[cfg(all(feature = "icons", feature = "text_layout"))]
pub fn register_embedded_material_icons(
font_bytes: &[u8],
) -> bool {
use crate::{font::parsed::ParsedFont, parsed_font_to_font_ref};
let mut warnings = Vec::new();
let Some(parsed_font) = ParsedFont::from_bytes(font_bytes, 0, &mut warnings) else {
return false;
let font_ref = parsed_font_to_font_ref(parsed_font);
register_material_icons(provider, &font_ref);
true
#[cfg(not(all(feature = "icons", feature = "text_layout")))]
_font_bytes: &[u8],
// Icons or text_layout feature not enabled. Returning false is a weak
// signal callers routinely ignore — name the gate once.
"[azul][icons] register_embedded_material_icons called, but this build lacks the \
`icons` and/or `text_layout` feature — NO icons registered (returning false)"
false
// Convenience Functions
/// Create an `IconProviderHandle` with the default resolver.
pub fn create_default_icon_provider() -> IconProviderHandle {
IconProviderHandle::with_resolver(default_icon_resolver)
// The embedded Material Icons font bytes (the `include!` of the
// codegen-generated `target/codegen/material_icons.ttf.br` + brotli
// decompression) deliberately live in `azul-dll`, not here — see
// `register_embedded_material_icons` above for why (build-cycle: azul-doc
// builds azul-layout to generate that artifact).
// Tests
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_resolver_no_data() {
let style = SystemStyle::default();
let original = Dom::create_div().root;
let result = default_icon_resolver(OptionRefAny::None, &original, &style);
// Without data, should return empty div StyledDom
assert_eq!(result.children.as_ref().len(), 0);
fn test_create_default_provider() {
let provider = create_default_icon_provider();
assert!(provider.list_packs().is_empty());
#[allow(
clippy::float_cmp,
clippy::items_after_statements,
clippy::redundant_clone,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::cast_lossless,
clippy::unreadable_literal,
clippy::too_many_lines,
clippy::many_single_char_names,
clippy::similar_names,
unused_qualifications,
unreachable_pub,
private_interfaces
)] // pedantic lints are noise in adversarial test code
mod autotest_generated {
use azul_core::{a11y::SmallAriaInfo, dom::NodeType, resources::RawImageFormat};
use azul_css::props::basic::color::{ColorU, OptionColorU};
// ---------------------------------------------------------------------
// helpers
/// A `FontRef` whose `parsed` pointer addresses a `'static` byte and whose
/// destructor is a no-op, so nothing is freed on drop. Sound here because
/// nothing on the icon-resolution path ever dereferences `parsed` (only
/// `cpurender::raster` does, and that is not reached from `StyledDom::create`).
fn dummy_font_ref() -> FontRef {
static DUMMY_FONT_DATA: u8 = 0;
extern "C" fn dummy_destructor(_: *mut core::ffi::c_void) {}
FontRef::new(
core::ptr::addr_of!(DUMMY_FONT_DATA).cast::<core::ffi::c_void>(),
dummy_destructor,
)
/// A null (non-decoded) `ImageRef` of the given pixel size — `get_size()`
/// reports exactly `width` / `height`, with no allocation.
fn null_img(width: usize, height: usize) -> ImageRef {
ImageRef::null_image(width, height, RawImageFormat::RGBA8, Vec::new())
/// `ImageIconData` with explicitly-chosen (possibly hostile) f32 dimensions.
fn image_icon(width: f32, height: f32) -> ImageIconData {
ImageIconData {
image: null_img(1, 1),
fn font_icon(icon_char: &str) -> FontIconData {
FontIconData {
font: dummy_font_ref(),
fn grayscale_style() -> SystemStyle {
let mut s = SystemStyle::default();
s.icon_style.prefer_grayscale = true;
s
fn tint_style(color: ColorU) -> SystemStyle {
s.icon_style.tint_color = OptionColorU::Some(color);
/// A "normal" original icon node: a single div carrying `props` as inline style.
fn original_with(props: Vec<CssPropertyWithConditions>) -> NodeData {
let mut dom = Dom::create_div();
/// An original icon node with NO inline styles. The resolver takes a
/// `NodeData` now, so "no node at all" is no longer representable - and no
/// longer needs to be: the old `else` branch it drove existed only because
/// a `StyledDom` could be empty.
fn original_without_nodes() -> NodeData {
Dom::create_div().root
/// Every inline property on every node of the result, in document order.
/// (Collected across all nodes rather than `node_data[0]` so the assertions
/// survive any future anonymous-node insertion in `StyledDom::create`.)
fn all_props(dom: &Dom) -> Vec<CssPropertyWithConditions> {
all_nodes(dom)
.flat_map(|nd| {
nd.get_style()
.map(|(property, apply_if)| CssPropertyWithConditions {
property: property.clone(),
apply_if: apply_if.clone(),
.collect::<Vec<_>>()
fn width_px(dom: &Dom) -> Option<f32> {
all_props(dom).into_iter().find_map(|p| match p.property {
CssProperty::Width(CssPropertyValue::Exact(LayoutWidth::Px(px))) => {
Some(px.number.get())
_ => None,
fn height_px(dom: &Dom) -> Option<f32> {
CssProperty::Height(CssPropertyValue::Exact(LayoutHeight::Px(px))) => {
fn count_widths(dom: &Dom) -> usize {
all_props(dom)
.filter(|p| matches!(p.property, CssProperty::Width(_)))
.count()
/// Every node of a `Dom` tree, in document order.
fn all_nodes(dom: &Dom) -> Vec<NodeData> {
let mut out = Vec::new();
fn walk(d: &Dom, out: &mut Vec<NodeData>) {
out.push(d.root.clone());
for c in d.children.as_ref() {
walk(c, out);
walk(dom, &mut out);
out
fn text_of(dom: &Dom) -> Option<String> {
.find_map(|nd| match nd.get_node_type() {
NodeType::Text(t) => Some(t.as_str().to_string()),
fn has_image_node(dom: &Dom) -> bool {
.any(|nd| matches!(nd.get_node_type(), NodeType::Image(_)))
/// All `StyleFilter`s across every `filter:` property in the list.
fn filters_of(props: &[CssPropertyWithConditions]) -> Vec<StyleFilter> {
props
.filter_map(|p| match &p.property {
CssProperty::Filter(CssPropertyValue::Exact(v)) => Some(v.as_ref().to_vec()),
.flatten()
fn text_color_of(props: &[CssPropertyWithConditions]) -> Option<ColorU> {
props.iter().find_map(|p| match &p.property {
CssProperty::TextColor(CssPropertyValue::Exact(c)) => Some(c.inner),
fn resolve(data: RefAny, original: &NodeData, style: &SystemStyle) -> Dom {
default_icon_resolver(OptionRefAny::Some(data), original, style)
// default_icon_resolver — dispatch
fn resolver_none_yields_single_unstyled_div() {
let out = default_icon_resolver(
OptionRefAny::None,
&Dom::create_div().root,
&SystemStyle::default(),
assert_eq!(all_nodes(&out).len(), 1);
assert!(matches!(out.root.get_node_type(), NodeType::Div));
// The "not found" placeholder must carry no styling at all — in particular
// it must not inherit the original's width/height.
assert!(all_props(&out).is_empty());
fn resolver_unknown_refany_type_yields_empty_div() {
// A RefAny holding neither ImageIconData nor FontIconData must fall through
// to the placeholder rather than panicking on a bad downcast.
struct NotAnIconAtAll {
_payload: [u64; 4],
let data = RefAny::new(NotAnIconAtAll { _payload: [7; 4] });
let out = resolve(data, &Dom::create_div().root, &SystemStyle::default());
assert!(!has_image_node(&out));
assert!(text_of(&out).is_none());
fn resolver_image_icon_yields_image_node_with_default_dimensions() {
let out = resolve(
RefAny::new(image_icon(32.0, 24.0)),
assert!(has_image_node(&out));
assert_eq!(width_px(&out), Some(32.0));
assert_eq!(height_px(&out), Some(24.0));
fn resolver_font_icon_yields_text_node_with_font_family() {
let font = dummy_font_ref();
let data = RefAny::new(FontIconData {
icon_char: "\u{e88a}".to_string(),
// TWO nodes: the <span> and its text leaf. It was one when the glyph
// was a BARE text node — which is what made its css inert and left it
// competing as a flex item with no box ("text node ... is one of 2
// items in a InlineFlex container"). The span is the box; the glyph
// still reads through it.
assert_eq!(all_nodes(&out).len(), 2);
assert_eq!(text_of(&out).as_deref(), Some("\u{e88a}"));
assert!(
matches!(out.root.get_node_type(), azul_core::dom::NodeType::Span),
"the icon must be wrapped in a span — inline, and boxed"
// The registered font must be the one that ends up in `font-family`.
let has_font = all_props(&out).iter().any(|p| match &p.property {
CssProperty::FontFamily(CssPropertyValue::Exact(families)) => families
.as_ref()
.any(|f| matches!(f, StyleFontFamily::Ref(fr) if *fr == font)),
_ => false,
assert!(has_font, "font-family with the icon's FontRef must be set");
// default_icon_resolver — degenerate originals
fn image_icon_with_node_less_original_still_gets_dimensions() {
// `original.node_data.first()` is None -> the fallback branch must still
// produce a fully-sized image instead of panicking / emitting no style.
let original = original_without_nodes();
RefAny::new(image_icon(16.0, 16.0)),
&original,
assert_eq!(width_px(&out), Some(16.0));
assert_eq!(height_px(&out), Some(16.0));
fn font_icon_with_node_less_original_still_gets_font() {
RefAny::new(font_icon("A")),
assert_eq!(text_of(&out).as_deref(), Some("A"));
assert!(all_props(&out)
.any(|p| matches!(p.property, CssProperty::FontFamily(_))));
// numeric limits: the icon dimensions are attacker-controlled f32s
fn image_icon_nan_dimensions_saturate_to_zero_without_panicking() {
// FloatValue stores `(v * 1000.0) as isize`; `NaN as isize` saturates to 0,
// so a NaN-sized icon degrades to a 0x0 box rather than poisoning layout.
RefAny::new(image_icon(f32::NAN, f32::NAN)),
let (w, h) = (
width_px(&out).expect("width emitted"),
height_px(&out).expect("height emitted"),
w.is_finite() && h.is_finite(),
"NaN must not survive into CSS"
assert_eq!(w, 0.0);
assert_eq!(h, 0.0);
fn image_icon_infinite_dimensions_saturate_to_finite_values() {
RefAny::new(image_icon(f32::INFINITY, f32::NEG_INFINITY)),
let w = width_px(&out).expect("width emitted");
let h = height_px(&out).expect("height emitted");
assert!(w.is_finite(), "+inf must saturate, got {w}");
assert!(h.is_finite(), "-inf must saturate, got {h}");
assert!(w > 0.0 && h < 0.0, "saturation must keep the sign");
fn image_icon_negative_dimensions_are_passed_through_unclamped() {
// Documents current behaviour: the resolver does NOT reject negative sizes,
// it forwards them verbatim into `width` / `height`.
RefAny::new(image_icon(-32.0, -1.5)),
assert_eq!(width_px(&out), Some(-32.0));
assert_eq!(height_px(&out), Some(-1.5));
/// THE bug this exists for: an oversampled bitmap is not a bigger icon.
/// A 16px window control rasterised at 32x32 for a 2x display laid out at
/// 32px and came out as a giant X next to the title.
fn an_oversampled_icon_lays_out_at_its_natural_size_not_its_bitmap_size() {
let mut provider = create_default_icon_provider();
&mut provider,
"system",
"close",
null_img(32, 32),
16.0,
let data = provider.lookup("close").expect("icon registered");
// The negative control: the plain constructor still takes the bitmap's
// size, which is correct for a 1:1 bitmap.
register_image_icon(&mut provider, "system", "close", null_img(32, 32));
/// A nonsense natural size must not make the icon invisible: an unusable
/// answer falls back to the bitmap, which is at least drawable.
fn a_nonsense_natural_size_falls_back_to_the_bitmap() {
for bad in [0.0, -8.0, f32::NAN, f32::INFINITY] {
register_image_icon_sized(&mut provider, "system", "x", null_img(24, 24), bad, bad);
let data = provider.lookup("x").expect("icon registered");
assert_eq!(
width_px(&out),
Some(24.0),
"natural size {bad} must be ignored"
fn register_image_icon_with_usize_max_size_saturates() {
// `ImageRef::get_size()` casts usize -> f32 (1.8e19); FloatValue then scales
// by 1000 and casts to isize, which must saturate rather than wrap/panic.
register_image_icon(
"huge",
"big",
null_img(usize::MAX, usize::MAX),
let data = provider.lookup("big").expect("icon registered");
w.is_finite() && w > 0.0,
"usize::MAX size must saturate finitely, got {w}"
fn image_icon_zero_size_is_preserved() {
RefAny::new(image_icon(0.0, 0.0)),
assert_eq!(width_px(&out), Some(0.0));
assert_eq!(height_px(&out), Some(0.0));
// style copying / precedence
fn original_dimensions_win_over_image_defaults() {
let original = original_with(vec![
CssPropertyWithConditions::simple(CssProperty::width(LayoutWidth::px(999.0))),
CssPropertyWithConditions::simple(CssProperty::height(LayoutHeight::px(888.0))),
]);
RefAny::new(image_icon(32.0, 32.0)),
assert_eq!(width_px(&out), Some(999.0));
assert_eq!(height_px(&out), Some(888.0));
// ...and the 32px default must not be appended as a *second* width.
assert_eq!(count_widths(&out), 1);
fn copy_appropriate_styles_vec_round_trips_exactly() {
// encode (set_css_props -> Css) == decode (copy_appropriate_styles_vec)
let props = vec![
CssPropertyWithConditions::simple(CssProperty::width(LayoutWidth::px(12.5))),
CssPropertyWithConditions::simple(CssProperty::height(LayoutHeight::px(7.0))),
];
let mut nd = NodeData::create_div();
nd.set_css_props(CssPropertyWithConditionsVec::from_vec(props.clone()));
assert_eq!(copy_appropriate_styles_vec(&nd), props);
fn copy_appropriate_styles_vec_of_unstyled_node_is_empty() {
let nd = NodeData::create_div();
assert!(copy_appropriate_styles_vec(&nd).is_empty());
fn copy_appropriate_styles_vec_preserves_order_of_many_props() {
// 512 same-typed declarations: nothing may be deduplicated or reordered,
// otherwise the last-wins cascade of the copied icon style would flip.
let props: Vec<CssPropertyWithConditions> = (0..512u32)
.map(|i| {
CssPropertyWithConditions::simple(CssProperty::width(LayoutWidth::px(i as f32)))
.collect();
let copied = copy_appropriate_styles_vec(&nd);
assert_eq!(copied.len(), 512);
assert_eq!(copied, props);
fn accessibility_info_is_copied_onto_the_resolved_icon() {
let original = Dom::create_div()
.with_accessibility_info(SmallAriaInfo::label("Save").to_full_info())
.root;
RefAny::new(image_icon(8.0, 8.0)),
let nodes = all_nodes(&out);
let a11y = nodes
.find_map(NodeData::get_accessibility_info)
.expect("a11y info must survive icon resolution");
a11y.accessibility_name.as_ref().map(|s| s.as_str()),
Some("Save")
// apply_icon_style_filters
fn icon_filters_default_style_adds_nothing() {
let mut props = Vec::new();
apply_icon_style_filters(&mut props, &SystemStyle::default());
props.is_empty(),
"default SystemStyle must not synthesise a filter"
fn icon_filters_grayscale_uses_quantised_luminance_matrix() {
apply_icon_style_filters(&mut props, &grayscale_style());
let filters = filters_of(&props);
assert_eq!(filters.len(), 1);
let StyleFilter::ColorMatrix(m) = &filters[0] else {
panic!(
"prefer_grayscale must emit a ColorMatrix filter, got {:?}",
filters[0]
// Rec.709 luminance weights, rounded through FloatValue's 1/1000 fixed point.
for r in [m.m0, m.m5, m.m10] {
assert!((r.get() - 0.2126).abs() < 0.001, "R weight {}", r.get());
for g in [m.m1, m.m6, m.m11] {
assert!((g.get() - 0.7152).abs() < 0.001, "G weight {}", g.get());
for b in [m.m2, m.m7, m.m12] {
assert!((b.get() - 0.0722).abs() < 0.001, "B weight {}", b.get());
// Alpha row must be pass-through, or grayscale icons would turn opaque/invisible.
assert_eq!(m.m18.get(), 1.0);
assert_eq!(m.m15.get(), 0.0);
assert_eq!(m.m19.get(), 0.0);
// FloatValue truncates at 3 decimals: the 4th digit of 0.2126 is lost.
assert_ne!(m.m0.get(), 0.2126);
fn icon_filters_tint_emits_flood_even_when_fully_transparent() {
// a == 0 is still forwarded — the resolver does not treat it as "no tint".
let transparent = ColorU {
r: 1,
g: 2,
b: 3,
a: 0,
apply_icon_style_filters(&mut props, &tint_style(transparent));
assert!(matches!(filters[0], StyleFilter::Flood(c) if c == transparent));
fn icon_filters_grayscale_and_tint_are_ordered_matrix_then_flood() {
let tint = ColorU {
r: 255,
g: 0,
b: 128,
a: 255,
let mut style = grayscale_style();
style.icon_style.tint_color = OptionColorU::Some(tint);
apply_icon_style_filters(&mut props, &style);
// Both filters must live in ONE `filter:` declaration (a second declaration
// would overwrite the first in the cascade, silently dropping the grayscale).
let filter_decls = props
.filter(|p| matches!(p.property, CssProperty::Filter(_)))
.count();
assert_eq!(filter_decls, 1);
assert_eq!(filters.len(), 2);
assert!(matches!(filters[0], StyleFilter::ColorMatrix(_)));
assert!(matches!(filters[1], StyleFilter::Flood(c) if c == tint));
fn icon_filters_preserve_pre_existing_properties() {
let mut props = vec![CssPropertyWithConditions::simple(CssProperty::width(
LayoutWidth::px(4.0),
))];
assert_eq!(props.len(), 2);
matches!(props[0].property, CssProperty::Width(_)),
"existing props must not be clobbered"
assert!(matches!(props[1].property, CssProperty::Filter(_)));
fn image_icon_grayscale_reaches_the_resolved_dom() {
RefAny::new(image_icon(10.0, 10.0)),
&grayscale_style(),
let filters = filters_of(&all_props(&out));
// apply_font_icon_color
fn font_icon_color_default_style_adds_nothing() {
apply_font_icon_color(&mut props, &SystemStyle::default());
assert!(props.is_empty());
fn font_icon_color_inherit_text_color_alone_is_a_noop() {
// Documented: inheritance is CSS's default, so `inherit_text_color` must
// *not* synthesise a `color:` declaration (that would break inheritance).
let mut style = SystemStyle::default();
style.icon_style.inherit_text_color = true;
apply_font_icon_color(&mut props, &style);
fn font_icon_color_tint_becomes_text_color() {
r: 9,
g: 8,
b: 7,
a: 6,
apply_font_icon_color(&mut props, &tint_style(tint));
assert_eq!(props.len(), 1);
assert_eq!(text_color_of(&props), Some(tint));
fn font_icon_color_tint_wins_over_inherit_text_color() {
g: 1,
b: 1,
let mut style = tint_style(tint);
fn font_icons_never_get_a_grayscale_filter() {
// Font icons take the color path, not the filter path — a ColorMatrix here
// would double-apply on top of the (inherited) text color.
RefAny::new(font_icon("\u{e88a}")),
assert!(filters_of(&all_props(&out)).is_empty());
// unicode / huge strings in the icon char
fn font_icon_empty_char_yields_empty_text_node() {
RefAny::new(font_icon("")),
assert_eq!(text_of(&out).as_deref(), Some(""));
fn font_icon_hostile_unicode_round_trips_verbatim() {
// ZWJ emoji sequence, RTL override, combining marks, an embedded NUL and a
// lone PUA codepoint: none may be normalised, truncated or panicked on.
for s in [
"\u{1F469}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}",
"\u{202E}gnippilf\u{202C}",
"e\u{0301}\u{0327}\u{0328}",
"a\0b",
"\u{F8FF}",
"\u{FFFD}",
] {
RefAny::new(font_icon(s)),
text_of(&out).as_deref(),
Some(s),
"icon_char {s:?} was altered"
fn font_icon_huge_char_string_does_not_panic() {
let huge = "\u{e88a}".repeat(65_536);
RefAny::new(font_icon(&huge)),
assert_eq!(text_of(&out).map(|s| s.chars().count()), Some(65_536));
// registration helpers
fn register_image_icon_lowercases_the_name_and_reads_size_from_the_imageref() {
register_image_icon(&mut provider, "App-Images", "HOME", null_img(64, 32));
// pack names are case-sensitive, icon names are normalised to lowercase
assert_eq!(provider.list_packs(), vec![String::from("App-Images")]);
provider.list_icons_in_pack("App-Images"),
vec![String::from("home")]
assert!(provider.list_icons_in_pack("app-images").is_empty());
assert!(provider.has_icon("hOmE"));
let data = provider.lookup("HOME").expect("case-insensitive lookup");
assert_eq!(width_px(&out), Some(64.0));
assert_eq!(height_px(&out), Some(32.0));
fn register_font_icon_accepts_empty_pack_and_icon_names() {
register_font_icon(&mut provider, "", "", dummy_font_ref(), "");
assert_eq!(provider.list_packs(), vec![String::new()]);
assert!(provider.has_icon(""));
let data = provider
.lookup("")
.expect("empty-named icon is still addressable");
fn register_icon_handles_oversized_and_unicode_names() {
let long_name = "n".repeat(10_000);
register_font_icon(&mut provider, "p", &long_name, dummy_font_ref(), "x");
assert!(provider.has_icon(&long_name));
// "İ" (U+0130) lowercases to TWO chars (i + U+0307); the key is the
// lowercased form, so the dotless "i" must NOT match.
register_font_icon(&mut provider, "p", "\u{130}", dummy_font_ref(), "y");
let folded = "\u{130}".to_lowercase();
assert!(provider.has_icon("\u{130}"));
assert!(provider.has_icon(&folded));
assert!(!provider.has_icon("i"));
fn duplicate_icon_across_packs_resolves_to_the_alphabetically_first_pack() {
// "First match wins" iterates a BTreeMap => pack *name* order, NOT the
// registration order. Registering into "zzz" first must not shadow "aaa".
register_image_icon(&mut provider, "zzz", "dup", null_img(1, 1));
register_image_icon(&mut provider, "aaa", "dup", null_img(2, 2));
let data = provider.lookup("dup").expect("icon registered");
Some(2.0),
"lookup must return the alphabetically-first pack's icon"
fn re_registering_an_icon_replaces_it_and_unregistering_drops_the_empty_pack() {
register_image_icon(&mut provider, "p", "icon", null_img(1, 1));
register_image_icon(&mut provider, "p", "ICON", null_img(5, 5));
assert_eq!(provider.list_icons_in_pack("p").len(), 1);
let data = provider.lookup("icon").expect("icon registered");
assert_eq!(width_px(&out), Some(5.0));
provider.unregister_icon("p", "IcOn");
assert!(!provider.has_icon("icon"));
provider.list_packs().is_empty(),
"empty pack must be removed"
fn create_default_icon_provider_starts_empty_and_misses_resolve_to_a_placeholder() {
assert!(provider.lookup("nope").is_none());
assert!(!provider.has_icon("nope"));
OptionRefAny::from(provider.lookup("nope")),
// ZIP / font-bytes entry points (both cfg variants share these signatures)
fn load_images_from_zip_rejects_malformed_archives() {
assert!(load_images_from_zip(&[]).is_empty());
assert!(load_images_from_zip(b"definitely not a zip file").is_empty());
// valid local-file-header magic, truncated body
assert!(load_images_from_zip(b"PK\x03\x04\x00\x00\x00\x00").is_empty());
// End-of-central-directory magic claiming 0xFFFF entries that don't exist
assert!(load_images_from_zip(b"PK\x05\x06\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF").is_empty());
assert!(load_images_from_zip(&[0xFFu8; 4096]).is_empty());
fn register_icons_from_zip_registers_nothing_for_garbage_bytes() {
for bytes in [
&b""[..],
&b"not a zip"[..],
&b"PK\x03\x04\x00\x00\x00\x00"[..],
&[0x00u8; 512][..],
register_icons_from_zip(&mut provider, "pack", bytes);
"a malformed ZIP must not create a pack"
fn register_embedded_material_icons_rejects_non_font_bytes() {
&b"this is not a TTF"[..],
// sfnt version tag + nothing else
&b"\x00\x01\x00\x00"[..],
&[0xFFu8; 256][..],
let ok = register_embedded_material_icons(&mut provider, bytes);
assert!(!ok, "corrupt font bytes must not report success");
fn register_material_icons_fills_a_single_lowercase_pack() {
register_material_icons(&mut provider, &font);
assert_eq!(provider.list_packs(), vec![String::from("material-icons")]);
let names = provider.list_icons_in_pack("material-icons");
names.len() > 1000,
"expected the full icon set, got {}",
names.len()
names.iter().all(|n| *n == n.to_lowercase()),
"every registered icon name must be normalised to lowercase"
assert!(provider.has_icon("home"));
assert!(provider.has_icon("HOME"));
let data = provider.lookup("home").expect("material 'home' icon");
text_of(&out).is_some(),
"a material icon must resolve to a text node"