use super::{Entity, EntityCommon};
use crate::types::{BoundingBox3D, Color, Handle, LineWeight, Transparency, Vector3};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TextHorizontalAlignment {
Left,
Center,
Right,
Aligned,
Middle,
Fit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TextVerticalAlignment {
Baseline,
Bottom,
Middle,
Top,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Text {
pub common: EntityCommon,
pub value: String,
pub insertion_point: Vector3,
pub alignment_point: Option<Vector3>,
pub height: f64,
pub rotation: f64,
pub width_factor: f64,
pub oblique_angle: f64,
pub style: String,
pub horizontal_alignment: TextHorizontalAlignment,
pub vertical_alignment: TextVerticalAlignment,
pub normal: Vector3,
}
impl Text {
pub fn new() -> Self {
Text {
common: EntityCommon::new(),
value: String::new(),
insertion_point: Vector3::ZERO,
alignment_point: None,
height: 1.0,
rotation: 0.0,
width_factor: 1.0,
oblique_angle: 0.0,
style: "STANDARD".to_string(),
horizontal_alignment: TextHorizontalAlignment::Left,
vertical_alignment: TextVerticalAlignment::Baseline,
normal: Vector3::UNIT_Z,
}
}
pub fn with_value(value: impl Into<String>, position: Vector3) -> Self {
Text {
value: value.into(),
insertion_point: position,
..Self::new()
}
}
pub fn with_height(mut self, height: f64) -> Self {
self.height = height;
self
}
pub fn with_rotation(mut self, rotation: f64) -> Self {
self.rotation = rotation;
self
}
}
impl Default for Text {
fn default() -> Self {
Self::new()
}
}
impl Entity for Text {
fn handle(&self) -> Handle {
self.common.handle
}
fn set_handle(&mut self, handle: Handle) {
self.common.handle = handle;
}
fn layer(&self) -> &str {
&self.common.layer
}
fn set_layer(&mut self, layer: String) {
self.common.layer = layer;
}
fn color(&self) -> Color {
self.common.color
}
fn set_color(&mut self, color: Color) {
self.common.color = color;
}
fn line_weight(&self) -> LineWeight {
self.common.line_weight
}
fn set_line_weight(&mut self, weight: LineWeight) {
self.common.line_weight = weight;
}
fn transparency(&self) -> Transparency {
self.common.transparency
}
fn set_transparency(&mut self, transparency: Transparency) {
self.common.transparency = transparency;
}
fn is_invisible(&self) -> bool {
self.common.invisible
}
fn set_invisible(&mut self, invisible: bool) {
self.common.invisible = invisible;
}
fn bounding_box(&self) -> BoundingBox3D {
let width = self.value.len() as f64 * self.height * 0.6 * self.width_factor;
BoundingBox3D::new(
self.insertion_point,
Vector3::new(
self.insertion_point.x + width,
self.insertion_point.y + self.height,
self.insertion_point.z,
),
)
}
fn translate(&mut self, offset: Vector3) {
super::translate::translate_text(self, offset);
}
fn entity_type(&self) -> &'static str {
"TEXT"
}
fn apply_transform(&mut self, transform: &crate::types::Transform) {
super::transform::transform_text(self, transform);
}
fn apply_mirror(&mut self, transform: &crate::types::Transform) {
super::mirror::mirror_text(self, transform);
}
}