use std::sync::Arc;
use crate::{
layout::Alignment,
node::{TextColor, color::Color},
};
mod font_features;
mod generic_family;
pub use font_features::{FontFeature, FontFeatures};
pub use generic_family::GenericFamily;
pub(crate) use generic_family::{alias_name, cosmic_family};
const DEFAULT_FONT_SIZE: f32 = 16.0;
const DEFAULT_LINE_HEIGHT: f32 = 1.2;
pub(crate) 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 baseline_shift: f32,
pub font_features: FontFeatures,
pub color: Color,
pub caret_color: Option<TextColor>,
pub shadow: Option<TextShadow>,
pub underline: Option<TextUnderline>,
pub strikethrough: Option<TextStrikethrough>,
pub case: TextCase,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextCase {
#[default]
AsWritten,
Upper,
Lower,
}
impl TextCase {
pub fn apply(self, text: &str) -> std::borrow::Cow<'_, str> {
let change = |c: char| -> char {
let mut other = match self {
Self::AsWritten => return c,
Self::Upper => CaseIter::Upper(c.to_uppercase()),
Self::Lower => CaseIter::Lower(c.to_lowercase()),
};
match (other.next(), other.next()) {
(Some(one), None) if one.len_utf8() == c.len_utf8() => one,
_ => c,
}
};
if self == Self::AsWritten || text.chars().all(|c| change(c) == c) {
return std::borrow::Cow::Borrowed(text);
}
std::borrow::Cow::Owned(text.chars().map(change).collect())
}
}
enum CaseIter {
Upper(std::char::ToUppercase),
Lower(std::char::ToLowercase),
}
impl Iterator for CaseIter {
type Item = char;
fn next(&mut self) -> Option<char> {
match self {
Self::Upper(chars) => chars.next(),
Self::Lower(chars) => chars.next(),
}
}
}
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,
baseline_shift: 0.0,
font_features: FontFeatures::default(),
color: DEFAULT_TEXT_COLOR,
caret_color: None,
shadow: None,
underline: None,
strikethrough: None,
case: TextCase::AsWritten,
}
}
}
impl TextStyle {
pub(crate) fn scale_pixels(&mut self, factor: f32) {
self.font_size *= factor;
self.letter_spacing *= factor;
self.baseline_shift *= factor;
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TextUnderline {
pub color: Option<Color>,
pub thickness: Option<f32>,
pub offset: Option<f32>,
}
impl TextUnderline {
pub fn new() -> Self {
Self::default()
}
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn thickness(mut self, thickness: f32) -> Self {
self.thickness = Some(thickness);
self
}
pub fn offset(mut self, offset: f32) -> Self {
self.offset = Some(offset);
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TextStrikethrough {
pub color: Option<Color>,
pub thickness: Option<f32>,
}
impl TextStrikethrough {
pub fn new() -> Self {
Self::default()
}
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn thickness(mut self, thickness: f32) -> Self {
self.thickness = Some(thickness);
self
}
}
#[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,
}
}
}