use core::fmt;
use piet::FontWeight;
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Metadata(usize);
impl fmt::Debug for Metadata {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Metadata")
.field("underline", &self.underline())
.field("strikethrough", &self.strikethrough())
.field("boldness", &self.boldness())
.finish()
}
}
const FONT_WEIGHT_SIZE: usize = 10;
const FONT_WEIGHT_MASK: usize = 0b1111111111;
const UNDERLINE: usize = 1 << FONT_WEIGHT_SIZE;
const STRIKETHROUGH: usize = 1 << (FONT_WEIGHT_SIZE + 1);
impl Metadata {
pub fn new() -> Self {
Self(FontWeight::NORMAL.to_raw().into())
}
pub fn from_raw(raw: usize) -> Self {
Self(raw)
}
pub fn into_raw(self) -> usize {
self.0
}
pub fn set_underline(&mut self, underline: bool) {
if underline {
self.0 |= UNDERLINE;
} else {
self.0 &= !UNDERLINE;
}
}
pub fn set_strikethrough(&mut self, strikethrough: bool) {
if strikethrough {
self.0 |= STRIKETHROUGH;
} else {
self.0 &= !STRIKETHROUGH;
}
}
pub fn set_boldness(&mut self, boldness: FontWeight) {
self.0 &= !FONT_WEIGHT_MASK;
self.0 |= usize::from(boldness.to_raw());
}
pub fn underline(&self) -> bool {
self.0 & UNDERLINE != 0
}
pub fn strikethrough(&self) -> bool {
self.0 & STRIKETHROUGH != 0
}
pub fn boldness(&self) -> FontWeight {
FontWeight::new((self.0 & FONT_WEIGHT_MASK) as u16)
}
}