dxfscan 0.1.0

Binary DXF parser with typed entity data and lookup indices
Documentation
// SPDX-License-Identifier: ISC
use crate::point::Point3;
use crate::value::GroupValue;

/// A TEXT (single-line text) entity.
#[derive(Debug, Clone, Copy)]
pub struct Text<'a> {
    /// Text string.
    pub value: &'a [u8],
    /// Insertion point.
    pub insertion_point: Point3,
    /// Text height.
    pub height: f64,
    /// Rotation angle in degrees.
    pub rotation: f64,
    /// Relative X scale factor.
    ///
    /// Defaults to 1.0.
    pub width_factor: f64,
    /// Text style name.
    pub style_name: &'a [u8],
    /// Second alignment point.
    ///
    /// Used for all non-default justifications. When `h_justify` or
    /// `v_justify` is nonzero, this point (not `insertion_point`)
    /// determines the text's actual position.
    pub second_alignment_point: Point3,
    /// Horizontal text justification.
    ///
    /// | Value | Meaning |
    /// |------:|---------|
    /// |     0 | Left    |
    /// |     1 | Center  |
    /// |     2 | Right   |
    /// |     3 | Aligned |
    /// |     4 | Middle  |
    /// |     5 | Fit     |
    pub h_justify: i16,
    /// Vertical text justification.
    ///
    /// | Value | Meaning  |
    /// |------:|----------|
    /// |     0 | Baseline |
    /// |     1 | Bottom   |
    /// |     2 | Middle   |
    /// |     3 | Top      |
    pub v_justify: i16,
    /// Oblique angle in degrees.
    pub oblique_angle: f64,
    /// Extrusion thickness.
    pub thickness: f64,
    /// Text generation flags.
    ///
    /// `0b10` = backward (mirrored in X),
    /// `0b100` = upside down (mirrored in Y).
    pub text_generation_flags: i16,
}

impl<'a> Default for Text<'a> {
    fn default() -> Self {
        Self {
            value: b"",
            insertion_point: Point3::default(),
            height: 0.0,
            rotation: 0.0,
            width_factor: 1.0,
            style_name: b"",
            second_alignment_point: Point3::default(),
            h_justify: 0,
            v_justify: 0,
            oblique_angle: 0.0,
            thickness: 0.0,
            text_generation_flags: 0,
        }
    }
}

impl<'a> Text<'a> {
    pub(crate) fn feed(&mut self, code: u16, val: &GroupValue<'a>) {
        match code {
            1 => {
                if let Some(s) = val.as_str_bytes() {
                    self.value = s;
                }
            }
            7 => {
                if let Some(s) = val.as_str_bytes() {
                    self.style_name = s;
                }
            }
            71 => {
                if let Some(v) = val.as_i16() {
                    self.text_generation_flags = v;
                }
            }
            72 => {
                if let Some(v) = val.as_i16() {
                    self.h_justify = v;
                }
            }
            73 => {
                if let Some(v) = val.as_i16() {
                    self.v_justify = v;
                }
            }
            _ => {
                if let Some(v) = val.as_f64() {
                    match code {
                        10 => self.insertion_point.x = v,
                        20 => self.insertion_point.y = v,
                        30 => self.insertion_point.z = v,
                        11 => self.second_alignment_point.x = v,
                        21 => self.second_alignment_point.y = v,
                        31 => self.second_alignment_point.z = v,
                        40 => self.height = v,
                        41 => self.width_factor = v,
                        39 => self.thickness = v,
                        50 => self.rotation = v,
                        51 => self.oblique_angle = v,
                        _ => {}
                    }
                }
            }
        }
    }
}