use super::colors::Color;
#[derive(Debug, Clone)]
pub struct Font {
pub name: String,
pub size: f64,
pub bold: bool,
pub italic: bool,
pub superscript: bool,
pub subscript: bool,
pub underline: String,
pub strikethrough: bool,
pub color: Color,
pub charset: i64,
pub family: i64,
pub scheme: String,
pub outline: Option<bool>,
pub shadow: Option<bool>,
pub condense: Option<bool>,
pub extend: Option<bool>,
}
impl PartialEq for Font {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.size.to_bits() == other.size.to_bits()
&& self.bold == other.bold
&& self.italic == other.italic
&& self.superscript == other.superscript
&& self.subscript == other.subscript
&& self.underline == other.underline
&& self.strikethrough == other.strikethrough
&& self.color == other.color
}
}
impl Eq for Font {}
impl std::hash::Hash for Font {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.size.to_bits().hash(state);
self.bold.hash(state);
self.italic.hash(state);
self.superscript.hash(state);
self.subscript.hash(state);
self.underline.hash(state);
self.strikethrough.hash(state);
self.color.hash(state);
}
}
impl Default for Font {
fn default() -> Self {
Font {
name: "Calibri".to_string(),
size: 11.0,
bold: false,
italic: false,
superscript: false,
subscript: false,
underline: Font::UNDERLINE_NONE.to_string(),
strikethrough: false,
color: Color::new(Color::BLACK),
charset: -1,
family: -1,
scheme: String::new(),
outline: None,
shadow: None,
condense: None,
extend: None,
}
}
}
impl Font {
pub const UNDERLINE_NONE: &'static str = "none";
pub const UNDERLINE_DOUBLE: &'static str = "double";
pub const UNDERLINE_DOUBLE_ACCOUNTING: &'static str = "doubleAccounting";
pub const UNDERLINE_SINGLE: &'static str = "single";
pub const UNDERLINE_SINGLE_ACCOUNTING: &'static str = "singleAccounting";
pub fn new() -> Self {
Font::default()
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
pub fn with_size(mut self, size: f64) -> Self {
self.size = size;
self
}
pub fn with_bold(mut self, bold: bool) -> Self {
self.bold = bold;
self
}
pub fn with_italic(mut self, italic: bool) -> Self {
self.italic = italic;
self
}
pub fn with_underline(mut self, style: &str) -> Self {
self.underline = style.to_string();
self
}
pub fn with_charset(mut self, charset: i64) -> Self {
self.charset = charset;
self
}
pub fn with_family(mut self, family: i64) -> Self {
self.family = family;
self
}
pub fn with_scheme(mut self, scheme: impl Into<String>) -> Self {
self.scheme = scheme.into();
self
}
pub fn with_outline(mut self, outline: bool) -> Self {
self.outline = Some(outline);
self
}
pub fn with_shadow(mut self, shadow: bool) -> Self {
self.shadow = Some(shadow);
self
}
pub fn with_condense(mut self, condense: bool) -> Self {
self.condense = Some(condense);
self
}
pub fn with_extend(mut self, extend: bool) -> Self {
self.extend = Some(extend);
self
}
pub fn with_color(mut self, color: Color) -> Self {
self.color = color;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_python() {
let font = Font::new();
assert_eq!(font.name, "Calibri");
assert_eq!(font.size, 11.0);
assert!(!font.bold);
assert_eq!(font.underline, "none");
assert_eq!(font.color.index, Color::BLACK);
}
#[test]
fn equality_uses_all_fields() {
let mut a = Font::new();
let b = Font::new();
assert_eq!(a, b);
a.bold = true;
assert_ne!(a, b);
}
#[test]
fn builders_compose() {
let font = Font::new()
.with_name("Arial")
.with_size(14.0)
.with_bold(true)
.with_underline(Font::UNDERLINE_DOUBLE);
assert_eq!(font.name, "Arial");
assert_eq!(font.size, 14.0);
assert_eq!(font.underline, "double");
}
}