mod font12x16;
mod font6x12;
mod font6x8;
mod font8x16;
pub mod font_builder;
pub use self::font12x16::Font12x16;
pub use self::font6x12::Font6x12;
pub use self::font6x8::Font6x8;
pub use self::font8x16::Font8x16;
use crate::geometry::Dimensions;
use crate::pixelcolor::PixelColor;
use crate::style::WithStyle;
pub trait Font<'a, C>: WithStyle<C> + Dimensions
where
C: PixelColor,
{
fn render_str(chars: &'a str) -> Self;
}
#[doc(hidden)]
#[macro_export]
macro_rules! impl_text {
($Font:ident, $text:expr $(, $style_key:ident = $style_value:expr )* $(,)?) => {{
#[allow(unused_imports)]
use $crate::style::WithStyle;
$crate::fonts::$Font::render_str($text)
$( .$style_key($style_value) )*
}};
}
#[macro_export]
macro_rules! text_6x8 {
($text:expr $(, $style_key:ident = $style_value:expr )* $(,)?) => {
$crate::impl_text!(Font6x8, $text $(, $style_key = $style_value )*)
};
}
#[macro_export]
macro_rules! text_6x12 {
($text:expr $(, $style_key:ident = $style_value:expr )* $(,)?) => {
$crate::impl_text!(Font6x12, $text $(, $style_key = $style_value )*)
};
}
#[macro_export]
macro_rules! text_8x16 {
($text:expr $(, $style_key:ident = $style_value:expr )* $(,)?) => {
$crate::impl_text!(Font8x16, $text $(, $style_key = $style_value )*)
};
}
#[macro_export]
macro_rules! text_12x16 {
($text:expr $(, $style_key:ident = $style_value:expr )* $(,)?) => {
$crate::impl_text!(Font12x16, $text $(, $style_key = $style_value )*)
};
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pixelcolor::{BinaryColor, Rgb565, RgbColor};
#[test]
fn font_macros() {
let _text: Font6x8<BinaryColor> = text_6x8!("Hello!");
let _text: Font6x12<BinaryColor> = text_6x12!("Hello!");
let _text: Font8x16<BinaryColor> = text_8x16!("Hello!");
let _text: Font12x16<BinaryColor> = text_12x16!("Hello!");
}
#[test]
fn styled_text() {
let _text: Font6x8<Rgb565> = text_6x8!("Hello!", stroke = Some(Rgb565::RED));
let _text: Font6x12<Rgb565> = text_6x12!("Hello!", stroke = Some(Rgb565::GREEN));
let _text: Font8x16<Rgb565> = text_8x16!("Hello!", stroke = Some(Rgb565::BLUE));
let _text: Font12x16<Rgb565> = text_12x16!("Hello!", stroke = Some(Rgb565::YELLOW));
}
}