use std::sync::Arc;
use crate::{
layout::Alignment,
node::{TextColor, color::Color},
};
mod font_features;
pub use font_features::{FontFeature, FontFeatures};
const DEFAULT_FONT_SIZE: f32 = 16.0;
const DEFAULT_LINE_HEIGHT: f32 = 1.2;
const DEFAULT_TEXT_COLOR: Color = Color::new(0, 0, 0, 255);
#[cfg(target_os = "windows")]
const DEFAULT_FONT_FAMILY_WINDOWS: &str = "Segoe UI";
#[cfg(not(target_os = "windows"))]
const DEFAULT_FONT_FAMILY_FALLBACK: &str = "";
#[derive(Clone, PartialEq)]
pub struct TextStyle {
pub font_family: Arc<str>,
pub font_size: f32,
pub line_height: f32,
pub weight: FontWeight,
pub style: FontStyle,
pub text_align: TextAlign,
pub vertical_align: VerticalAlign,
pub trim_line_box: bool,
pub letter_spacing: f32,
pub font_features: FontFeatures,
pub color: Color,
pub caret_color: Option<TextColor>,
pub shadow: Option<TextShadow>,
}
impl Default for TextStyle {
fn default() -> Self {
Self {
font_family: default_font_family(),
font_size: DEFAULT_FONT_SIZE,
line_height: DEFAULT_LINE_HEIGHT,
weight: FontWeight::Normal,
style: FontStyle::Normal,
text_align: TextAlign::Left,
vertical_align: VerticalAlign::default(),
trim_line_box: false,
letter_spacing: 0.0,
font_features: FontFeatures::default(),
color: DEFAULT_TEXT_COLOR,
caret_color: None,
shadow: None,
}
}
}
impl TextStyle {
pub(crate) fn scale_pixels(&mut self, factor: f32) {
self.font_size *= factor;
self.letter_spacing *= factor;
}
}
#[derive(Clone, Copy, PartialEq)]
pub struct TextShadow {
pub offset_x: f32,
pub offset_y: f32,
pub blur_radius: f32,
pub color: Color,
}
impl TextShadow {
pub fn new(offset_x: f32, offset_y: f32, blur_radius: f32, color: Color) -> Self {
Self {
offset_x,
offset_y,
blur_radius: blur_radius.max(0.0),
color,
}
}
pub(crate) fn is_visible(&self) -> bool {
self.color.a() > 0 && (self.offset_x != 0.0 || self.offset_y != 0.0 || self.blur_radius > 0.0)
}
}
fn default_font_family() -> Arc<str> {
#[cfg(target_os = "windows")]
{
Arc::from(DEFAULT_FONT_FAMILY_WINDOWS)
}
#[cfg(not(target_os = "windows"))]
{
Arc::from(DEFAULT_FONT_FAMILY_FALLBACK)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub enum FontWeight {
Thin,
ExtraLight,
Light,
#[default]
Normal,
Medium,
SemiBold,
Bold,
ExtraBold,
Black,
Numeric(u16),
}
impl FontWeight {
pub const MIN_VALUE: u16 = 1;
pub const MAX_VALUE: u16 = 1000;
pub const fn value(self) -> u16 {
match self {
Self::Thin => 100,
Self::ExtraLight => 200,
Self::Light => 300,
Self::Normal => 400,
Self::Medium => 500,
Self::SemiBold => 600,
Self::Bold => 700,
Self::ExtraBold => 800,
Self::Black => 900,
Self::Numeric(value) => {
if value < Self::MIN_VALUE {
Self::MIN_VALUE
} else if value > Self::MAX_VALUE {
Self::MAX_VALUE
} else {
value
}
}
}
}
pub fn to_cosmic(&self) -> cosmic_text::Weight {
cosmic_text::Weight(self.value())
}
}
impl PartialEq for FontWeight {
fn eq(&self, other: &Self) -> bool {
self.value() == other.value()
}
}
impl Eq for FontWeight {}
impl std::hash::Hash for FontWeight {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.value().hash(state);
}
}
impl From<u16> for FontWeight {
fn from(value: u16) -> Self {
Self::Numeric(value)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FontStyle {
#[default]
Normal,
Italic,
}
impl FontStyle {
pub fn to_cosmic(&self) -> cosmic_text::Style {
match self {
Self::Normal => cosmic_text::Style::Normal,
Self::Italic => cosmic_text::Style::Italic,
}
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum TextAlign {
#[default]
Left,
Center,
Right,
Justified,
End,
}
impl TextAlign {
pub(crate) fn to_cosmic(self) -> cosmic_text::Align {
match self {
Self::Left => cosmic_text::Align::Left,
Self::Center => cosmic_text::Align::Center,
Self::Right => cosmic_text::Align::Right,
Self::Justified => cosmic_text::Align::Justified,
Self::End => cosmic_text::Align::End,
}
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum VerticalAlign {
Top,
#[default]
Center,
Bottom,
LineBox,
}
impl From<Alignment> for TextAlign {
fn from(alignment: Alignment) -> Self {
match alignment {
Alignment::Start | Alignment::Stretch => Self::Left,
Alignment::Center => Self::Center,
Alignment::End => Self::Right,
}
}
}