use font_provider::FontProvider;
use galileo_types::cartesian::Vector2;
use serde::{Deserialize, Serialize};
use crate::Color;
pub(crate) mod font_provider;
pub mod text_service;
pub(crate) use text_service::TextService;
use crate::render::text::text_service::FontServiceError;
#[cfg(feature = "rustybuzz")]
mod rustybuzz;
#[cfg(feature = "rustybuzz")]
pub use rustybuzz::RustybuzzRasterizer;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TextStyle {
pub font_family: Vec<String>,
pub font_size: f32,
#[serde(default = "default_font_color")]
pub font_color: Color,
#[serde(default)]
pub horizontal_alignment: HorizontalAlignment,
#[serde(default)]
pub vertical_alignment: VerticalAlignment,
#[serde(default)]
pub weight: FontWeight,
#[serde(default)]
pub style: FontStyle,
#[serde(default)]
pub outline_width: f32,
#[serde(default = "default_outline_color")]
pub outline_color: Color,
}
fn default_font_color() -> Color {
Color::BLACK
}
fn default_outline_color() -> Color {
Color::TRANSPARENT
}
#[derive(Default, Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
pub enum HorizontalAlignment {
Left,
#[default]
Center,
Right,
}
#[derive(Default, Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
pub enum VerticalAlignment {
Top,
Middle,
#[default]
Bottom,
}
pub enum TextShaping {
Tessellation {
glyphs: Vec<TessellatedGlyph>,
},
Raster,
}
#[derive(Debug, Copy, Clone)]
pub struct GlyphVertex {
pub position: [f32; 2],
pub color: Color,
}
#[derive(Debug, Clone)]
pub struct TessellatedGlyph {
pub vertices: Vec<GlyphVertex>,
pub indices: Vec<u32>,
}
pub trait TextRasterizer {
fn shape(
&self,
text: &str,
style: &TextStyle,
offset: Vector2<f32>,
font_provider: &dyn FontProvider,
) -> Result<TextShaping, FontServiceError>;
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct FontWeight(pub(crate) u16);
impl FontWeight {
pub const NORMAL: Self = FontWeight(500);
pub const BOLD: Self = FontWeight(700);
pub const THIN: Self = FontWeight(300);
}
impl Default for FontWeight {
fn default() -> Self {
Self::NORMAL
}
}
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub enum FontStyle {
Normal,
Italic,
Oblique,
}
impl Default for FontStyle {
fn default() -> Self {
Self::Normal
}
}
pub struct FontProperties {
pub weight: FontWeight,
pub style: FontStyle,
}