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;
use alloc::vec::Vec;

/// An MTEXT (multi-line text) entity.
///
/// MTEXT supports rich formatting via inline codes in the text string
/// (e.g. `\P` for newline, `\fArial;` for font changes).
#[derive(Debug, Clone, Default)]
pub struct MText<'a> {
    /// Text string.
    pub text: &'a [u8],
    /// Extended text chunks.
    ///
    /// Concatenated with `text` to form the full string.
    pub extended_text: Vec<&'a [u8]>,
    /// Insertion point.
    pub insertion_point: Point3,
    /// Initial text height.
    pub height: f64,
    /// Reference rectangle width.
    pub reference_rect_width: f64,
    /// Defined annotation height.
    pub defined_height: f64,
    /// Attachment point.
    ///
    /// | Value | Meaning       |
    /// |------:|---------------|
    /// |     1 | Top-left      |
    /// |     2 | Top-center    |
    /// |     3 | Top-right     |
    /// |     4 | Middle-left   |
    /// |     5 | Middle-center |
    /// |     6 | Middle-right  |
    /// |     7 | Bottom-left   |
    /// |     8 | Bottom-center |
    /// |     9 | Bottom-right  |
    pub attachment_point: i16,
    /// Drawing direction.
    ///
    /// | Value | Meaning        |
    /// |------:|----------------|
    /// |     1 | Left to right  |
    /// |     3 | Top to bottom  |
    /// |     5 | By style       |
    pub drawing_direction: i16,
    /// Rotation angle in radians.
    ///
    /// When the X-axis direction vector is also present, the direction
    /// vector takes precedence.
    pub rotation_angle: f64,
    /// X-axis direction vector.
    ///
    /// Defines the horizontal direction for the text. When present,
    /// this takes precedence over `rotation_angle`.
    pub x_axis_direction: Point3,
    /// Text style name.
    pub style_name: &'a [u8],
    /// Line spacing style.
    ///
    /// | Value | Meaning  |
    /// |------:|----------|
    /// |     1 | At least |
    /// |     2 | Exact    |
    pub line_spacing_style: i16,
    /// Line spacing factor.
    ///
    /// Multiplier applied to the default line spacing. Defaults to 1.0.
    pub line_spacing_factor: f64,
    /// Background fill box scale factor.
    pub fill_box_scale: f64,
    /// Column type.
    ///
    /// | Value | Meaning |
    /// |------:|---------|
    /// |     0 | None    |
    /// |     1 | Static  |
    /// |     2 | Dynamic |
    pub column_type: i16,
    /// Number of columns.
    pub column_count: i16,
    /// Column width.
    pub column_width: f64,
    /// Column gutter width.
    ///
    /// The spacing between adjacent columns.
    pub column_gutter_width: f64,
    /// Column flow reversed flag.
    pub column_flow_reversed: i16,
    /// Column autoheight flag.
    pub column_autoheight: i16,
}

impl<'a> MText<'a> {
    pub(crate) fn feed(&mut self, code: u16, val: &GroupValue<'a>) {
        match code {
            1 => {
                if let Some(s) = val.as_str_bytes() {
                    self.text = s;
                }
            }
            3 => {
                if let Some(s) = val.as_str_bytes() {
                    self.extended_text.push(s);
                }
            }
            7 => {
                if let Some(s) = val.as_str_bytes() {
                    self.style_name = s;
                }
            }
            71 => {
                if let Some(v) = val.as_i16() {
                    self.attachment_point = v;
                }
            }
            72 => {
                if let Some(v) = val.as_i16() {
                    self.drawing_direction = v;
                }
            }
            73 => {
                if let Some(v) = val.as_i16() {
                    self.line_spacing_style = v;
                }
            }
            75 => {
                if let Some(v) = val.as_i16() {
                    self.column_type = v;
                }
            }
            76 => {
                if let Some(v) = val.as_i16() {
                    self.column_count = v;
                }
            }
            78 => {
                if let Some(v) = val.as_i16() {
                    self.column_flow_reversed = v;
                }
            }
            79 => {
                if let Some(v) = val.as_i16() {
                    self.column_autoheight = 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.x_axis_direction.x = v,
                        21 => self.x_axis_direction.y = v,
                        31 => self.x_axis_direction.z = v,
                        40 => self.height = v,
                        41 => self.reference_rect_width = v,
                        44 => self.line_spacing_factor = v,
                        45 => self.fill_box_scale = v,
                        46 => self.defined_height = v,
                        48 => self.column_width = v,
                        49 => self.column_gutter_width = v,
                        50 => self.rotation_angle = v,
                        _ => {}
                    }
                }
            }
        }
    }
}