mod brush;
mod font;
mod styleset;
use alloc::borrow::Cow;
pub use brush::*;
pub use font::{
FontFamily, FontFamilyName, FontFeature, FontFeatures, FontStyle, FontVariation,
FontVariations, FontWeight, FontWidth, GenericFamily,
};
pub use fontique::Language;
pub use parlance::{OverflowWrap, TextWrapMode, WordBreak};
pub use styleset::StyleSet;
use crate::util::nearly_eq;
#[derive(Debug, Clone, Copy)]
pub enum WhiteSpaceCollapse {
Collapse,
Preserve,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LineHeight {
MetricsRelative(f32),
FontSizeRelative(f32),
Absolute(f32),
}
impl Default for LineHeight {
fn default() -> Self {
Self::MetricsRelative(1.0)
}
}
impl LineHeight {
pub(crate) fn nearly_eq(self, other: Self) -> bool {
match (self, other) {
(Self::MetricsRelative(a), Self::MetricsRelative(b))
| (Self::FontSizeRelative(a), Self::FontSizeRelative(b))
| (Self::Absolute(a), Self::Absolute(b)) => nearly_eq(a, b),
_ => false,
}
}
pub(crate) fn scale(self, scale: f32) -> Self {
match self {
Self::Absolute(value) => Self::Absolute(value * scale),
value => value,
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum StyleProperty<'a, B: Brush> {
FontFamily(FontFamily<'a>),
FontSize(f32),
FontWidth(FontWidth),
FontStyle(FontStyle),
FontWeight(FontWeight),
FontVariations(FontVariations<'a>),
FontFeatures(FontFeatures<'a>),
Locale(Option<Language>),
Brush(B),
Underline(bool),
UnderlineOffset(Option<f32>),
UnderlineSize(Option<f32>),
UnderlineBrush(Option<B>),
Strikethrough(bool),
StrikethroughOffset(Option<f32>),
StrikethroughSize(Option<f32>),
StrikethroughBrush(Option<B>),
LineHeight(LineHeight),
WordSpacing(f32),
LetterSpacing(f32),
WordBreak(WordBreak),
OverflowWrap(OverflowWrap),
TextWrapMode(TextWrapMode),
}
#[derive(Clone, PartialEq, Debug)]
pub struct TextStyle<'family, 'settings, B: Brush> {
pub font_family: FontFamily<'family>,
pub font_size: f32,
pub font_width: FontWidth,
pub font_style: FontStyle,
pub font_weight: FontWeight,
pub font_variations: FontVariations<'settings>,
pub font_features: FontFeatures<'settings>,
pub locale: Option<Language>,
pub brush: B,
pub has_underline: bool,
pub underline_offset: Option<f32>,
pub underline_size: Option<f32>,
pub underline_brush: Option<B>,
pub has_strikethrough: bool,
pub strikethrough_offset: Option<f32>,
pub strikethrough_size: Option<f32>,
pub strikethrough_brush: Option<B>,
pub line_height: LineHeight,
pub word_spacing: f32,
pub letter_spacing: f32,
pub word_break: WordBreak,
pub overflow_wrap: OverflowWrap,
pub text_wrap_mode: TextWrapMode,
}
impl<B: Brush> Default for TextStyle<'static, 'static, B> {
fn default() -> Self {
TextStyle {
font_family: FontFamily::Source(Cow::Borrowed("sans-serif")),
font_size: 16.0,
font_width: FontWidth::default(),
font_style: FontStyle::default(),
font_weight: FontWeight::default(),
font_variations: FontVariations::empty(),
font_features: FontFeatures::empty(),
locale: None,
brush: B::default(),
has_underline: false,
underline_offset: None,
underline_size: None,
underline_brush: None,
has_strikethrough: false,
strikethrough_offset: None,
strikethrough_size: None,
strikethrough_brush: None,
line_height: LineHeight::default(),
word_spacing: 0.0,
letter_spacing: 0.0,
word_break: WordBreak::default(),
overflow_wrap: OverflowWrap::default(),
text_wrap_mode: TextWrapMode::default(),
}
}
}
impl<'a, B: Brush> From<FontFamily<'a>> for StyleProperty<'a, B> {
fn from(value: FontFamily<'a>) -> Self {
StyleProperty::FontFamily(value)
}
}
impl<'a, B: Brush> From<&'a [FontFamilyName<'a>]> for StyleProperty<'a, B> {
fn from(value: &'a [FontFamilyName<'a>]) -> Self {
StyleProperty::FontFamily(value.into())
}
}
impl<'a, B: Brush> From<FontFamilyName<'a>> for StyleProperty<'a, B> {
fn from(value: FontFamilyName<'a>) -> Self {
StyleProperty::FontFamily(value.into())
}
}
impl<'a, B: Brush> From<FontVariations<'a>> for StyleProperty<'a, B> {
fn from(value: FontVariations<'a>) -> Self {
StyleProperty::FontVariations(value)
}
}
impl<'a, B: Brush> From<FontFeatures<'a>> for StyleProperty<'a, B> {
fn from(value: FontFeatures<'a>) -> Self {
StyleProperty::FontFeatures(value)
}
}
impl<B: Brush> From<GenericFamily> for StyleProperty<'_, B> {
fn from(f: GenericFamily) -> Self {
StyleProperty::FontFamily(f.into())
}
}
impl<B: Brush> From<LineHeight> for StyleProperty<'_, B> {
fn from(value: LineHeight) -> Self {
StyleProperty::LineHeight(value)
}
}