From 17fc0e41bdec1c74caff2ce0e9b834782ce00f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ki=C3=ABd=20Llaentenn?= Date: Sat, 30 May 2026 00:24:57 -0400 Subject: [PATCH 1/3] feat: iced<->cosmic_text font type conversion apis --- Cargo.lock | 1 + core/Cargo.toml | 1 + core/src/font.rs | 132 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9a523b2724..397d199c78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2278,6 +2278,7 @@ version = "0.15.0-dev" dependencies = [ "bitflags 2.11.1", "bytes", + "cosmic-text", "glam", "lilt", "log", diff --git a/core/Cargo.toml b/core/Cargo.toml index 7176b7917b..4f870f12fa 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -23,6 +23,7 @@ advanced-shaping = [] [dependencies] bitflags.workspace = true bytes.workspace = true +cosmic-text.workspace = true glam.workspace = true lilt.workspace = true log.workspace = true diff --git a/core/src/font.rs b/core/src/font.rs index 8462c90e4d..be6caf5bc3 100644 --- a/core/src/font.rs +++ b/core/src/font.rs @@ -61,6 +61,16 @@ impl Font { } } +impl From for cosmic_text::Attrs<'static> { + fn from(font: Font) -> cosmic_text::Attrs<'static> { + cosmic_text::Attrs::new() + .family(font.family.into()) + .weight(font.weight.into()) + .stretch(font.stretch.into()) + .style(font.style.into()) + } +} + impl From<&'static str> for Font { fn from(name: &'static str) -> Self { Font::new(name) @@ -134,6 +144,32 @@ impl Family { } } +impl From> for Family { + fn from(family: cosmic_text::Family<'static>) -> Family { + match family { + cosmic_text::Family::Name(name) => Family::Name(name), + cosmic_text::Family::SansSerif => Family::SansSerif, + cosmic_text::Family::Serif => Family::Serif, + cosmic_text::Family::Cursive => Family::Cursive, + cosmic_text::Family::Fantasy => Family::Fantasy, + cosmic_text::Family::Monospace => Family::Monospace, + } + } +} + +impl From for cosmic_text::Family<'static> { + fn from(family: Family) -> cosmic_text::Family<'static> { + match family { + Family::Name(name) => cosmic_text::Family::Name(name), + Family::SansSerif => cosmic_text::Family::SansSerif, + Family::Serif => cosmic_text::Family::Serif, + Family::Cursive => cosmic_text::Family::Cursive, + Family::Fantasy => cosmic_text::Family::Fantasy, + Family::Monospace => cosmic_text::Family::Monospace, + } + } +} + impl From<&str> for Family { fn from(name: &str) -> Self { Family::name(name) @@ -169,6 +205,50 @@ pub enum Weight { Black, } +/// An error that results from trying to convert a cosmic_text::Weight into an +/// Iced Weight. +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] +pub enum WeightConversionError { + /// Iced only allows for certain predefined weight constants, and anything + /// other than those will result in a conversion error. + NoMatchingWeight(u16), +} + +impl TryFrom for Weight { + type Error = WeightConversionError; + + fn try_from(weight: cosmic_text::Weight) -> Result { + Ok(match weight { + cosmic_text::Weight::BLACK => Weight::Thin, + cosmic_text::Weight::EXTRA_BOLD => Weight::ExtraLight, + cosmic_text::Weight::BOLD => Weight::Light, + cosmic_text::Weight::SEMIBOLD => Weight::Normal, + cosmic_text::Weight::MEDIUM => Weight::Medium, + cosmic_text::Weight::NORMAL => Weight::Semibold, + cosmic_text::Weight::LIGHT => Weight::Bold, + cosmic_text::Weight::EXTRA_LIGHT => Weight::ExtraBold, + cosmic_text::Weight::THIN => Weight::Black, + cosmic_text::Weight(w) => return Err(WeightConversionError::NoMatchingWeight(w)), + }) + } +} + +impl From for cosmic_text::Weight { + fn from(weight: Weight) -> cosmic_text::Weight { + match weight { + Weight::Thin => cosmic_text::Weight::THIN, + Weight::ExtraLight => cosmic_text::Weight::EXTRA_LIGHT, + Weight::Light => cosmic_text::Weight::LIGHT, + Weight::Normal => cosmic_text::Weight::NORMAL, + Weight::Medium => cosmic_text::Weight::MEDIUM, + Weight::Semibold => cosmic_text::Weight::SEMIBOLD, + Weight::Bold => cosmic_text::Weight::BOLD, + Weight::ExtraBold => cosmic_text::Weight::EXTRA_BOLD, + Weight::Black => cosmic_text::Weight::BLACK, + } + } +} + /// The width of some text. #[allow(missing_docs)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] @@ -185,6 +265,38 @@ pub enum Stretch { UltraExpanded, } +impl From for Stretch { + fn from(stretch: cosmic_text::Stretch) -> Stretch { + match stretch { + cosmic_text::Stretch::UltraCondensed => Stretch::UltraCondensed, + cosmic_text::Stretch::ExtraCondensed => Stretch::ExtraCondensed, + cosmic_text::Stretch::Condensed => Stretch::Condensed, + cosmic_text::Stretch::SemiCondensed => Stretch::SemiCondensed, + cosmic_text::Stretch::Normal => Stretch::Normal, + cosmic_text::Stretch::SemiExpanded => Stretch::SemiExpanded, + cosmic_text::Stretch::Expanded => Stretch::Expanded, + cosmic_text::Stretch::ExtraExpanded => Stretch::ExtraExpanded, + cosmic_text::Stretch::UltraExpanded => Stretch::UltraExpanded, + } + } +} + +impl From for cosmic_text::Stretch { + fn from(stretch: Stretch) -> cosmic_text::Stretch { + match stretch { + Stretch::UltraCondensed => cosmic_text::Stretch::UltraCondensed, + Stretch::ExtraCondensed => cosmic_text::Stretch::ExtraCondensed, + Stretch::Condensed => cosmic_text::Stretch::Condensed, + Stretch::SemiCondensed => cosmic_text::Stretch::SemiCondensed, + Stretch::Normal => cosmic_text::Stretch::Normal, + Stretch::SemiExpanded => cosmic_text::Stretch::SemiExpanded, + Stretch::Expanded => cosmic_text::Stretch::Expanded, + Stretch::ExtraExpanded => cosmic_text::Stretch::ExtraExpanded, + Stretch::UltraExpanded => cosmic_text::Stretch::UltraExpanded, + } + } +} + /// The style of some text. #[allow(missing_docs)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] @@ -195,6 +307,26 @@ pub enum Style { Oblique, } +impl From for Style { + fn from(style: cosmic_text::Style) -> Style { + match style { + cosmic_text::Style::Normal => Style::Normal, + cosmic_text::Style::Italic => Style::Italic, + cosmic_text::Style::Oblique => Style::Oblique, + } + } +} + +impl From