use super::{BackendColor, BackendCoord};
use std::error::Error;
#[derive(Clone, Copy)]
pub enum FontFamily<'a> {
Serif,
SansSerif,
Monospace,
Name(&'a str),
}
impl<'a> FontFamily<'a> {
pub fn as_str(&self) -> &str {
match self {
FontFamily::Serif => "serif",
FontFamily::SansSerif => "sans-serif",
FontFamily::Monospace => "monospace",
FontFamily::Name(face) => face,
}
}
}
impl<'a> From<&'a str> for FontFamily<'a> {
fn from(from: &'a str) -> FontFamily<'a> {
match from.to_lowercase().as_str() {
"serif" => FontFamily::Serif,
"sans-serif" => FontFamily::SansSerif,
"monospace" => FontFamily::Monospace,
_ => FontFamily::Name(from),
}
}
}
pub mod text_anchor {
#[derive(Clone, Copy)]
pub enum HPos {
Left,
Right,
Center,
}
#[derive(Clone, Copy)]
pub enum VPos {
Top,
Center,
Bottom,
}
#[derive(Clone, Copy)]
pub struct Pos {
pub h_pos: HPos,
pub v_pos: VPos,
}
impl Pos {
pub fn new(h_pos: HPos, v_pos: VPos) -> Self {
Pos { h_pos, v_pos }
}
pub fn default() -> Self {
Pos {
h_pos: HPos::Left,
v_pos: VPos::Top,
}
}
}
}
#[derive(Clone)]
pub enum FontTransform {
None,
Rotate90,
Rotate180,
Rotate270,
}
impl FontTransform {
pub fn transform(&self, x: i32, y: i32) -> (i32, i32) {
match self {
FontTransform::None => (x, y),
FontTransform::Rotate90 => (-y, x),
FontTransform::Rotate180 => (-x, -y),
FontTransform::Rotate270 => (y, -x),
}
}
}
#[derive(Clone, Copy)]
pub enum FontStyle {
Normal,
Oblique,
Italic,
Bold,
}
impl FontStyle {
pub fn as_str(&self) -> &str {
match self {
FontStyle::Normal => "normal",
FontStyle::Italic => "italic",
FontStyle::Oblique => "oblique",
FontStyle::Bold => "bold",
}
}
}
impl<'a> From<&'a str> for FontStyle {
fn from(from: &'a str) -> FontStyle {
match from.to_lowercase().as_str() {
"normal" => FontStyle::Normal,
"italic" => FontStyle::Italic,
"oblique" => FontStyle::Oblique,
"bold" => FontStyle::Bold,
_ => FontStyle::Normal,
}
}
}
pub trait BackendTextStyle {
type FontError: Error + Sync + Send + 'static;
fn color(&self) -> BackendColor {
BackendColor {
alpha: 1.0,
rgb: (0, 0, 0),
}
}
fn size(&self) -> f64 {
1.0
}
fn transform(&self) -> FontTransform {
FontTransform::None
}
fn style(&self) -> FontStyle {
FontStyle::Normal
}
fn anchor(&self) -> text_anchor::Pos {
text_anchor::Pos::default()
}
fn family(&self) -> FontFamily;
#[allow(clippy::type_complexity)]
fn layout_box(&self, text: &str) -> Result<((i32, i32), (i32, i32)), Self::FontError>;
fn draw<E, DrawFunc: FnMut(i32, i32, BackendColor) -> Result<(), E>>(
&self,
text: &str,
pos: BackendCoord,
draw: DrawFunc,
) -> Result<Result<(), E>, Self::FontError>;
}