docx_rs/documents/elements/
text_box.rs

1use serde::Serialize;
2
3use crate::documents::*;
4use crate::types::*;
5
6#[derive(Debug, Clone, Serialize, PartialEq)]
7#[serde(rename_all = "camelCase")]
8pub struct TextBox {
9    // For writer only
10    pub children: Vec<TextBoxContentChild>,
11    // unit is emu
12    pub size: (u32, u32),
13    pub position_type: DrawingPositionType,
14    /// Specifies that this object shall be positioned using the positioning information in the
15    /// simplePos child element (ยง20.4.2.13). This positioning, when specified, positions the
16    /// object on the page by placing its top left point at the x-y coordinates specified by that
17    /// element.
18    pub simple_pos: bool,
19    // unit is emu
20    pub simple_pos_x: i32,
21    pub simple_pos_y: i32,
22    /// Specifies how this DrawingML object behaves when its anchor is located in a table cell;
23    /// and its specified position would cause it to intersect with a table cell displayed in the
24    /// document. That behavior shall be as follows:
25    pub layout_in_cell: bool,
26    /// Specifies the relative Z-ordering of all DrawingML objects in this document. Each floating
27    /// DrawingML object shall have a Z-ordering value, which determines which object is
28    /// displayed when any two objects intersect. Higher values shall indicate higher Z-order;
29    /// lower values shall indicate lower Z-order.
30    pub relative_height: u32,
31    pub allow_overlap: bool,
32    pub position_h: DrawingPosition,
33    pub position_v: DrawingPosition,
34    pub relative_from_h: RelativeFromHType,
35    pub relative_from_v: RelativeFromVType,
36    /// Specifies the minimum distance which shall be maintained between the top edge of this drawing object and any subsequent text within the document when this graphical object is displayed within the document's contents.,
37    /// The distance shall be measured in EMUs (English Metric Units).,
38    pub dist_t: i32,
39    pub dist_b: i32,
40    pub dist_l: i32,
41    pub dist_r: i32,
42}
43
44impl Default for TextBox {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl TextBox {
51    pub fn new() -> Self {
52        Self {
53            children: vec![],
54            size: (from_px(100), from_px(100)),
55            position_type: DrawingPositionType::Inline,
56            simple_pos: false,
57            simple_pos_x: 0,
58            simple_pos_y: 0,
59            layout_in_cell: false,
60            relative_height: 190500,
61            allow_overlap: false,
62            position_v: DrawingPosition::Offset(0),
63            position_h: DrawingPosition::Offset(0),
64            relative_from_h: RelativeFromHType::default(),
65            relative_from_v: RelativeFromVType::default(),
66            dist_t: 0,
67            dist_b: 0,
68            dist_l: 0,
69            dist_r: 0,
70        }
71    }
72
73    // unit is emu
74    pub fn size(mut self, w_emu: u32, h_emu: u32) -> Self {
75        self.size = (w_emu, h_emu);
76        self
77    }
78
79    pub fn floating(mut self) -> Self {
80        self.position_type = DrawingPositionType::Anchor;
81        self
82    }
83
84    pub fn offset_x(mut self, x: i32) -> Self {
85        self.position_h = DrawingPosition::Offset(x);
86        self
87    }
88
89    pub fn offset_y(mut self, y: i32) -> Self {
90        self.position_v = DrawingPosition::Offset(y);
91        self
92    }
93
94    pub fn position_h(mut self, pos: DrawingPosition) -> Self {
95        self.position_h = pos;
96        self
97    }
98
99    pub fn position_v(mut self, pos: DrawingPosition) -> Self {
100        self.position_v = pos;
101        self
102    }
103
104    pub fn relative_from_h(mut self, t: RelativeFromHType) -> Self {
105        self.relative_from_h = t;
106        self
107    }
108
109    pub fn relative_from_v(mut self, t: RelativeFromVType) -> Self {
110        self.relative_from_v = t;
111        self
112    }
113
114    pub fn dist_t(mut self, v: i32) -> Self {
115        self.dist_t = v;
116        self
117    }
118
119    pub fn dist_b(mut self, v: i32) -> Self {
120        self.dist_b = v;
121        self
122    }
123
124    pub fn dist_l(mut self, v: i32) -> Self {
125        self.dist_l = v;
126        self
127    }
128
129    pub fn dist_r(mut self, v: i32) -> Self {
130        self.dist_r = v;
131        self
132    }
133
134    pub fn simple_pos(mut self, v: bool) -> Self {
135        self.simple_pos = v;
136        self
137    }
138
139    pub fn relative_height(mut self, v: u32) -> Self {
140        self.relative_height = v;
141        self
142    }
143}
144
145/*
146impl BuildXML for TextBox {
147    fn build_to<W: Write>(&self, stream: xml::writer::EventWriter<W>) -> xml::writer::Result<xml::writer::EventWriter<W>> {
148        let w = format!("{}", self.size.0);
149        let h = format!("{}", self.size.1);
150        XMLBuilder::from(stream)
151            .open_pic("http://schemas.openxmlformats.org/drawingml/2006/picture")?
152            .open_pic_nv_pic_pr()?
153            .pic_c_nv_pr("0", "")?
154            .open_pic_c_nv_pic_pr()?
155            .a_pic_locks("1", "1")?
156            .close()?
157            .close()?
158            .open_blip_fill()?
159            .a_blip(&self.id)?
160            .a_src_rect()?
161            .open_a_stretch()?
162            .a_fill_rect()?
163            .close()?
164            .close()?
165            .open_pic_sp_pr("auto")?
166            .open_a_xfrm()?
167            .a_off("0", "0")?
168            .a_ext(&w, &h)?
169            .close()?
170            .open_a_prst_geom("rect")?
171            .a_av_lst()?
172            .close()?
173            .close()?
174            .close()?
175            .into_inner()
176    }
177}
178*/