use crate::layout::builder_data::{FontSettingKey, EMPTY_FONT_SETTINGS};
use crate::sugarloaf::primitives::SugarCursor;
use crate::Sugar;
use crate::SugarDecoration;
use crate::SugarStyle;
use swash::{Stretch, Style, Weight};
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct FragmentStyle {
pub font: usize,
pub width: f32,
pub font_attrs: (Stretch, Weight, Style),
pub font_size: f32,
pub color: [f32; 4],
pub background_color: Option<[f32; 4]>,
pub font_vars: FontSettingKey,
pub letter_spacing: f32,
pub word_spacing: f32,
pub line_spacing: f32,
pub underline: bool,
pub underline_offset: Option<f32>,
pub underline_color: Option<[f32; 4]>,
pub underline_size: Option<f32>,
pub cursor: SugarCursor,
}
impl Default for FragmentStyle {
fn default() -> Self {
Self {
font: 0,
width: 1.0,
font_attrs: (Stretch::NORMAL, Weight::NORMAL, Style::Normal),
font_size: 16.,
font_vars: EMPTY_FONT_SETTINGS,
letter_spacing: 0.,
word_spacing: 0.,
line_spacing: 1.,
color: [1.0, 1.0, 1.0, 1.0],
background_color: None,
cursor: SugarCursor::Disabled,
underline: false,
underline_offset: None,
underline_color: None,
underline_size: None,
}
}
}
impl FragmentStyle {
pub fn scaled_default(scale: f32) -> Self {
Self {
font: 0,
width: 1.0,
font_attrs: (Stretch::NORMAL, Weight::NORMAL, Style::Normal),
font_size: 16. * scale,
font_vars: EMPTY_FONT_SETTINGS,
letter_spacing: 0.,
word_spacing: 0.,
line_spacing: 1.,
color: [1.0, 1.0, 1.0, 1.0],
background_color: None,
cursor: SugarCursor::Disabled,
underline: false,
underline_offset: None,
underline_color: None,
underline_size: None,
}
}
}
impl From<&Sugar> for FragmentStyle {
fn from(sugar: &Sugar) -> Self {
let mut style = FragmentStyle::default();
match sugar.style {
SugarStyle::BoldItalic => {
style.font_attrs.1 = Weight::BOLD;
style.font_attrs.2 = Style::Italic;
}
SugarStyle::Bold => {
style.font_attrs.1 = Weight::BOLD;
}
SugarStyle::Italic => {
style.font_attrs.2 = Style::Italic;
}
_ => {}
}
let mut has_underline_cursor = false;
match sugar.cursor {
SugarCursor::Underline(cursor_color) => {
style.underline = true;
style.underline_offset = Some(-1.);
style.underline_color = Some(cursor_color);
style.underline_size = Some(-1.);
has_underline_cursor = true;
}
SugarCursor::Block(cursor_color) => {
style.cursor = SugarCursor::Block(cursor_color);
}
SugarCursor::Caret(cursor_color) => {
style.cursor = SugarCursor::Caret(cursor_color);
}
_ => {}
}
match &sugar.decoration {
SugarDecoration::Underline => {
if !has_underline_cursor {
style.underline = true;
style.underline_offset = Some(-2.);
style.underline_size = Some(1.);
}
}
SugarDecoration::Strikethrough => {
style.underline = true;
style.underline_offset = Some(style.font_size / 2.);
style.underline_size = Some(2.);
}
_ => {}
}
style.color = sugar.foreground_color;
style.background_color = sugar.background_color;
style
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Direction {
Auto,
LeftToRight,
RightToLeft,
}
impl Default for Direction {
fn default() -> Self {
Self::LeftToRight
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum TextTransform {
None,
Uppercase,
Lowercase,
Capitalize,
}