use super::TableEntry;
use crate::types::Handle;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TextGenerationFlags {
pub backward: bool,
pub upside_down: bool,
}
impl TextGenerationFlags {
pub fn new() -> Self {
TextGenerationFlags {
backward: false,
upside_down: false,
}
}
}
impl Default for TextGenerationFlags {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TextStyle {
pub handle: Handle,
pub name: String,
pub flags: TextGenerationFlags,
pub height: f64,
pub width_factor: f64,
pub oblique_angle: f64,
pub last_height: f64,
pub font_file: String,
pub big_font_file: String,
pub true_type_font: String,
pub xref_dependent: bool,
}
impl TextStyle {
pub fn new(name: impl Into<String>) -> Self {
TextStyle {
handle: Handle::NULL,
name: name.into(),
flags: TextGenerationFlags::new(),
height: 0.0,
width_factor: 1.0,
oblique_angle: 0.0,
last_height: 2.5,
font_file: "txt".to_string(),
big_font_file: String::new(),
true_type_font: String::new(),
xref_dependent: false,
}
}
pub fn standard() -> Self {
TextStyle {
handle: Handle::NULL,
name: "Standard".to_string(),
flags: TextGenerationFlags::new(),
height: 0.0,
width_factor: 1.0,
oblique_angle: 0.0,
last_height: 2.5,
font_file: "txt".to_string(),
big_font_file: String::new(),
true_type_font: String::new(),
xref_dependent: false,
}
}
pub fn with_truetype(name: impl Into<String>, font: impl Into<String>) -> Self {
TextStyle {
true_type_font: font.into(),
..Self::new(name)
}
}
pub fn effective_last_height(&self) -> f64 {
if self.last_height > 0.0 {
self.last_height
} else {
2.5
}
}
pub fn set_backward(&mut self, backward: bool) {
self.flags.backward = backward;
}
pub fn set_upside_down(&mut self, upside_down: bool) {
self.flags.upside_down = upside_down;
}
pub fn is_backward(&self) -> bool {
self.flags.backward
}
pub fn is_upside_down(&self) -> bool {
self.flags.upside_down
}
pub fn has_fixed_height(&self) -> bool {
self.height > 0.0
}
}
impl TableEntry for TextStyle {
fn handle(&self) -> Handle {
self.handle
}
fn set_handle(&mut self, handle: Handle) {
self.handle = handle;
}
fn name(&self) -> &str {
&self.name
}
fn set_name(&mut self, name: String) {
self.name = name;
}
fn is_standard(&self) -> bool {
self.name == "Standard"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_textstyle_creation() {
let style = TextStyle::new("MyStyle");
assert_eq!(style.name, "MyStyle");
assert_eq!(style.width_factor, 1.0);
assert!(!style.has_fixed_height());
}
#[test]
fn test_textstyle_standard() {
let style = TextStyle::standard();
assert_eq!(style.name, "Standard");
assert!(style.is_standard());
}
#[test]
fn test_textstyle_flags() {
let mut style = TextStyle::new("Test");
assert!(!style.is_backward());
assert!(!style.is_upside_down());
style.set_backward(true);
assert!(style.is_backward());
style.set_upside_down(true);
assert!(style.is_upside_down());
}
}