| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 3 |
use crate::one::property::color::Color; |
| 4 |
use crate::one::property::layout_alignment::LayoutAlignment; |
| 5 |
use crate::one::property::outline_indent_distance::OutlineIndentDistance; |
| 6 |
use crate::one::property_set::{table_cell_node, table_node, table_row_node}; |
| 7 |
use crate::onenote::note_tag::{parse_note_tags, NoteTag}; |
| 8 |
use crate::onenote::outline::{parse_outline_element, OutlineElement}; |
| 9 |
use crate::onestore::object_space::ObjectSpace; |
| 10 |
|
| 11 |
/// A table. |
| 12 |
/// |
| 13 |
/// See [\[MS-ONE\] 2.2.26]. |
| 14 |
/// |
| 15 |
/// [\[MS-ONE\] 2.2.26]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/9046980a-2410-4b2d-8a35-ec06e55648e0 |
| 16 |
#[derive(Clone, Debug)] |
| 17 |
pub struct Table { |
| 18 |
pub(crate) rows: u32, |
| 19 |
pub(crate) cols: u32, |
| 20 |
pub(crate) contents: Vec<TableRow>, |
| 21 |
|
| 22 |
pub(crate) cols_locked: Vec<u8>, |
| 23 |
pub(crate) col_widths: Vec<f32>, |
| 24 |
|
| 25 |
pub(crate) borders_visible: bool, |
| 26 |
|
| 27 |
pub(crate) layout_alignment_in_parent: Option<LayoutAlignment>, |
| 28 |
pub(crate) layout_alignment_self: Option<LayoutAlignment>, |
| 29 |
|
| 30 |
pub(crate) note_tags: Vec<NoteTag>, |
| 31 |
} |
| 32 |
|
| 33 |
impl Table { |
| 34 |
/// The number of rows in this table. |
| 35 |
pub fn rows(&self) -> u32 { |
| 36 |
self.rows |
| 37 |
} |
| 38 |
|
| 39 |
/// The number of columns in this table. |
| 40 |
pub fn cols(&self) -> u32 { |
| 41 |
self.cols |
| 42 |
} |
| 43 |
|
| 44 |
/// The table rows. |
| 45 |
pub fn contents(&self) -> &[TableRow] { |
| 46 |
&self.contents |
| 47 |
} |
| 48 |
|
| 49 |
/// Which columns have a locked width. |
| 50 |
/// |
| 51 |
/// To determine if column `c` has a locked with, calculate: |
| 52 |
/// |
| 53 |
/// ```ignore |
| 54 |
/// table.cols_locked()[c / 8] & (1 << (c % 8)) == 1; |
| 55 |
/// ``` |
| 56 |
/// |
| 57 |
/// See [\[MS-ONE\] 2.3.70]. |
| 58 |
/// |
| 59 |
/// [\[MS-ONE\] 2.3.70]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/465c1f7c-63be-4d8b-a8de-76924afe92c2 |
| 60 |
pub fn cols_locked(&self) -> &[u8] { |
| 61 |
&self.cols_locked |
| 62 |
} |
| 63 |
|
| 64 |
/// The column widths in half-inch increments. |
| 65 |
/// |
| 66 |
/// See [\[MS-ONE\] 2.3.66]. |
| 67 |
/// |
| 68 |
/// [\[MS-ONE\] 2.3.66]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/aea7e232-e7f3-444b-8d2c-e8f46fa8cc59 |
| 69 |
pub fn col_widths(&self) -> &[f32] { |
| 70 |
&self.col_widths |
| 71 |
} |
| 72 |
|
| 73 |
/// Whether the table borders are visible. |
| 74 |
/// |
| 75 |
/// See [\[MS-ONE\] 2.3.65]. |
| 76 |
/// |
| 77 |
/// [\[MS-ONE\] 2.3.65]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/37be8f3c-e015-4c45-be99-615a669439b8 |
| 78 |
pub fn borders_visible(&self) -> bool { |
| 79 |
self.borders_visible |
| 80 |
} |
| 81 |
|
| 82 |
/// The table's alignment relative to the containing outline element (if present). |
| 83 |
/// |
| 84 |
/// See [\[MS-ONE\] 2.3.27]. |
| 85 |
/// |
| 86 |
/// [\[MS-ONE\] 2.3.27]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/61fa50be-c355-4b8d-ac01-761a2f7f66c0 |
| 87 |
pub fn layout_alignment_in_parent(&self) -> Option<LayoutAlignment> { |
| 88 |
self.layout_alignment_in_parent |
| 89 |
} |
| 90 |
|
| 91 |
/// The table's alignment. |
| 92 |
/// |
| 93 |
/// See [\[MS-ONE\] 2.3.33]. |
| 94 |
/// |
| 95 |
/// [\[MS-ONE\] 2.3.33]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4e7fe9db-2fdb-4239-b291-dc4b909c94ad |
| 96 |
pub fn layout_alignment_self(&self) -> Option<LayoutAlignment> { |
| 97 |
self.layout_alignment_self |
| 98 |
} |
| 99 |
|
| 100 |
/// Note tags for this table. |
| 101 |
pub fn note_tags(&self) -> &[NoteTag] { |
| 102 |
&self.note_tags |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/// A table row. |
| 107 |
/// |
| 108 |
/// See [\[MS-ONE\] 2.2.27]. |
| 109 |
/// |
| 110 |
/// [\[MS-ONE\] 2.2.27]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/d22af1aa-5e0b-40ed-b914-f6397979d6b0 |
| 111 |
#[derive(Clone, Debug)] |
| 112 |
pub struct TableRow { |
| 113 |
pub(crate) contents: Vec<TableCell>, |
| 114 |
} |
| 115 |
|
| 116 |
impl TableRow { |
| 117 |
/// The cells in the table row. |
| 118 |
pub fn contents(&self) -> &[TableCell] { |
| 119 |
&self.contents |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/// A table cell. |
| 124 |
/// |
| 125 |
/// See [\[MS-ONE\] 2.2.28]. |
| 126 |
/// |
| 127 |
/// [\[MS-ONE\] 2.2.28]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/e5660d6b-72c3-4d9f-bad0-435c00f42183 |
| 128 |
#[derive(Clone, Debug)] |
| 129 |
pub struct TableCell { |
| 130 |
pub(crate) contents: Vec<OutlineElement>, |
| 131 |
|
| 132 |
pub(crate) background_color: Option<Color>, |
| 133 |
pub(crate) layout_max_width: Option<f32>, |
| 134 |
pub(crate) outline_indent_distance: OutlineIndentDistance, |
| 135 |
} |
| 136 |
|
| 137 |
impl TableCell { |
| 138 |
/// The contents of the table cell. |
| 139 |
pub fn contents(&self) -> &[OutlineElement] { |
| 140 |
&self.contents |
| 141 |
} |
| 142 |
|
| 143 |
/// The content's max width in half-inch increments. |
| 144 |
/// |
| 145 |
/// See [\[MS-ONE\] 2.3.22]. |
| 146 |
/// |
| 147 |
/// [\[MS-ONE\] 2.3.22]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/a770ac4b-2225-4aa6-ba92-d3a51f97c405 |
| 148 |
pub fn layout_max_width(&self) -> Option<f32> { |
| 149 |
self.layout_max_width |
| 150 |
} |
| 151 |
|
| 152 |
/// The indentation size for the table cell contents. |
| 153 |
/// |
| 154 |
/// See [\[MS-ONE\] 2.2.2]. |
| 155 |
/// |
| 156 |
/// [\[MS-ONE\] 2.2.2]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/269a3e7b-d85a-4ba8-8e1d-d85e1c840772 |
| 157 |
pub fn outline_indent_distance(&self) -> &OutlineIndentDistance { |
| 158 |
&self.outline_indent_distance |
| 159 |
} |
| 160 |
|
| 161 |
/// The cell's background color. |
| 162 |
pub fn background_color(&self) -> Option<Color> { |
| 163 |
self.background_color |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
pub(crate) fn parse_table(table_id: ExGuid, space: &ObjectSpace) -> Result<Table> { |
| 168 |
let table_object = space |
| 169 |
.get_object(table_id) |
| 170 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("table object is missing".into()))?; |
| 171 |
let data = table_node::parse(table_object)?; |
| 172 |
|
| 173 |
let contents = data |
| 174 |
.rows |
| 175 |
.into_iter() |
| 176 |
.map(|row_id| parse_row(row_id, space)) |
| 177 |
.collect::<Result<_>>()?; |
| 178 |
|
| 179 |
let table = Table { |
| 180 |
rows: data.row_count, |
| 181 |
cols: data.col_count, |
| 182 |
contents, |
| 183 |
cols_locked: data.cols_locked, |
| 184 |
col_widths: data.col_widths, |
| 185 |
borders_visible: data.borders_visible, |
| 186 |
layout_alignment_in_parent: data.layout_alignment_in_parent, |
| 187 |
layout_alignment_self: data.layout_alignment_self, |
| 188 |
note_tags: parse_note_tags(data.note_tags, space)?, |
| 189 |
}; |
| 190 |
|
| 191 |
Ok(table) |
| 192 |
} |
| 193 |
|
| 194 |
fn parse_row(row_id: ExGuid, space: &ObjectSpace) -> Result<TableRow> { |
| 195 |
let row_object = space |
| 196 |
.get_object(row_id) |
| 197 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("row object is missing".into()))?; |
| 198 |
let data = table_row_node::parse(row_object)?; |
| 199 |
|
| 200 |
let contents = data |
| 201 |
.cells |
| 202 |
.into_iter() |
| 203 |
.map(|cell_id| parse_cell(cell_id, space)) |
| 204 |
.collect::<Result<_>>()?; |
| 205 |
|
| 206 |
let row = TableRow { contents }; |
| 207 |
|
| 208 |
Ok(row) |
| 209 |
} |
| 210 |
|
| 211 |
fn parse_cell(cell_id: ExGuid, space: &ObjectSpace) -> Result<TableCell> { |
| 212 |
let cell_object = space |
| 213 |
.get_object(cell_id) |
| 214 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("cell object is missing".into()))?; |
| 215 |
let data = table_cell_node::parse(cell_object)?; |
| 216 |
|
| 217 |
let contents = data |
| 218 |
.contents |
| 219 |
.into_iter() |
| 220 |
.map(|element_id| parse_outline_element(element_id, space)) |
| 221 |
.collect::<Result<_>>()?; |
| 222 |
|
| 223 |
let cell = TableCell { |
| 224 |
contents, |
| 225 |
background_color: data.background_color, |
| 226 |
layout_max_width: data.layout_max_width, |
| 227 |
outline_indent_distance: data.outline_indent_distance, |
| 228 |
}; |
| 229 |
|
| 230 |
Ok(cell) |
| 231 |
} |