#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Alignment {
pub horizontal: String,
pub vertical: String,
pub text_rotation: i64,
pub wrap_text: bool,
pub shrink_to_fit: bool,
pub indent: i64,
pub relative_indent: i32,
pub justify_last_line: Option<bool>,
pub reading_order: u32,
}
impl Default for Alignment {
fn default() -> Self {
Alignment {
horizontal: Alignment::HORIZONTAL_GENERAL.to_string(),
vertical: Alignment::VERTICAL_BOTTOM.to_string(),
text_rotation: 0,
wrap_text: false,
shrink_to_fit: false,
indent: 0,
relative_indent: 0,
justify_last_line: None,
reading_order: 0,
}
}
}
impl Alignment {
pub const HORIZONTAL_GENERAL: &'static str = "general";
pub const HORIZONTAL_LEFT: &'static str = "left";
pub const HORIZONTAL_RIGHT: &'static str = "right";
pub const HORIZONTAL_CENTER: &'static str = "center";
pub const HORIZONTAL_CENTER_CONTINUOUS: &'static str = "centerContinuous";
pub const HORIZONTAL_JUSTIFY: &'static str = "justify";
pub const VERTICAL_BOTTOM: &'static str = "bottom";
pub const VERTICAL_TOP: &'static str = "top";
pub const VERTICAL_CENTER: &'static str = "center";
pub const VERTICAL_JUSTIFY: &'static str = "justify";
pub fn new() -> Self {
Alignment::default()
}
pub fn with_horizontal(mut self, value: &str) -> Self {
self.horizontal = value.to_string();
self
}
pub fn with_vertical(mut self, value: &str) -> Self {
self.vertical = value.to_string();
self
}
pub fn with_text_rotation(mut self, value: i64) -> Self {
self.text_rotation = value;
self
}
pub fn with_wrap_text(mut self, value: bool) -> Self {
self.wrap_text = value;
self
}
pub fn with_shrink_to_fit(mut self, value: bool) -> Self {
self.shrink_to_fit = value;
self
}
pub fn with_relative_indent(mut self, value: i32) -> Self {
self.relative_indent = value.clamp(-255, 255);
self
}
pub fn with_justify_last_line(mut self, justify: bool) -> Self {
self.justify_last_line = Some(justify);
self
}
pub fn with_reading_order(mut self, order: u32) -> Self {
self.reading_order = order;
self
}
pub fn with_indent(mut self, value: i64) -> Self {
self.indent = value;
self
}
pub fn is_default(&self) -> bool {
self == &Alignment::default()
}
pub fn attributes(&self) -> Vec<(String, String)> {
let mut attrs = Vec::new();
if self.horizontal != Alignment::HORIZONTAL_GENERAL {
attrs.push(("horizontal".to_string(), self.horizontal.clone()));
}
if self.vertical != Alignment::VERTICAL_BOTTOM {
attrs.push(("vertical".to_string(), self.vertical.clone()));
}
if self.wrap_text {
attrs.push(("wrapText".to_string(), "1".to_string()));
}
if self.shrink_to_fit {
attrs.push(("shrinkToFit".to_string(), "1".to_string()));
}
if self.indent > 0 {
attrs.push(("indent".to_string(), self.indent.to_string()));
}
if self.relative_indent != 0 {
attrs.push((
"relativeIndent".to_string(),
self.relative_indent.to_string(),
));
}
if let Some(justify) = self.justify_last_line {
attrs.push(("justifyLastLine".to_string(), "1".to_string()));
if !justify {
let last = attrs.len() - 1;
attrs[last].1 = "0".to_string();
}
}
if self.reading_order != 0 {
attrs.push(("readingOrder".to_string(), self.reading_order.to_string()));
}
if self.text_rotation > 0 {
attrs.push(("textRotation".to_string(), self.text_rotation.to_string()));
} else if self.text_rotation < 0 {
attrs.push((
"textRotation".to_string(),
(90 - self.text_rotation).to_string(),
));
}
attrs
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_match_python() {
let a = Alignment::new();
assert_eq!(a.horizontal, "general");
assert_eq!(a.vertical, "bottom");
assert_eq!(a.text_rotation, 0);
assert!(a.is_default());
assert!(a.attributes().is_empty());
}
#[test]
fn negative_rotation_is_encoded() {
let a = Alignment::new().with_text_rotation(-45);
let attrs = a.attributes();
assert!(attrs.contains(&("textRotation".to_string(), "135".to_string())));
}
#[test]
fn deviations_only() {
let a = Alignment::new()
.with_horizontal(Alignment::HORIZONTAL_CENTER)
.with_wrap_text(true)
.with_indent(2)
.with_text_rotation(90);
let attrs = a.attributes();
assert!(attrs.contains(&("horizontal".to_string(), "center".to_string())));
assert!(attrs.contains(&("wrapText".to_string(), "1".to_string())));
assert!(attrs.contains(&("indent".to_string(), "2".to_string())));
assert!(attrs.contains(&("textRotation".to_string(), "90".to_string())));
assert!(!attrs.iter().any(|(k, _)| k == "vertical"));
assert!(!a.is_default());
}
}