| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 3 |
use crate::one::property::layout_alignment::LayoutAlignment; |
| 4 |
use crate::one::property_set::{image_node, picture_container}; |
| 5 |
use crate::onenote::iframe::{parse_iframe, IFrame}; |
| 6 |
use crate::onenote::note_tag::{parse_note_tags, NoteTag}; |
| 7 |
use crate::onestore::object_space::ObjectSpace; |
| 8 |
|
| 9 |
/// An embedded image. |
| 10 |
/// |
| 11 |
/// See [\[MS-ONE\] 2.2.24]. |
| 12 |
/// |
| 13 |
/// [\[MS-ONE\] 2.2.24]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/b7bb4d1a-2a57-4819-9eb4-5a2ce8cf210f |
| 14 |
#[derive(Clone, PartialEq, PartialOrd, Debug)] |
| 15 |
pub struct Image { |
| 16 |
pub(crate) data: Option<Vec<u8>>, |
| 17 |
pub(crate) extension: Option<String>, |
| 18 |
|
| 19 |
pub(crate) layout_max_width: Option<f32>, |
| 20 |
pub(crate) layout_max_height: Option<f32>, |
| 21 |
|
| 22 |
// pub(crate) language_code: Option<u32>, |
| 23 |
pub(crate) alt_text: Option<String>, |
| 24 |
|
| 25 |
pub(crate) layout_alignment_in_parent: Option<LayoutAlignment>, |
| 26 |
pub(crate) layout_alignment_self: Option<LayoutAlignment>, |
| 27 |
|
| 28 |
pub(crate) image_filename: Option<String>, |
| 29 |
|
| 30 |
pub(crate) displayed_page_number: Option<u32>, |
| 31 |
|
| 32 |
pub(crate) text: Option<String>, |
| 33 |
pub(crate) text_language_code: Option<u32>, |
| 34 |
|
| 35 |
pub(crate) picture_width: Option<f32>, |
| 36 |
pub(crate) picture_height: Option<f32>, |
| 37 |
|
| 38 |
pub(crate) hyperlink_url: Option<String>, |
| 39 |
|
| 40 |
pub(crate) offset_horizontal: Option<f32>, |
| 41 |
pub(crate) offset_vertical: Option<f32>, |
| 42 |
|
| 43 |
pub(crate) is_background: bool, |
| 44 |
|
| 45 |
pub(crate) note_tags: Vec<NoteTag>, |
| 46 |
|
| 47 |
pub(crate) embeds: Vec<IFrame>, |
| 48 |
} |
| 49 |
|
| 50 |
impl Image { |
| 51 |
/// The image's binary data. |
| 52 |
/// |
| 53 |
/// If `None` this means that the image data hasn't been uploaded yet. |
| 54 |
pub fn data(&self) -> Option<&[u8]> { |
| 55 |
self.data.as_deref() |
| 56 |
} |
| 57 |
|
| 58 |
/// The image's file extension. |
| 59 |
pub fn extension(&self) -> Option<&str> { |
| 60 |
self.extension.as_deref() |
| 61 |
} |
| 62 |
|
| 63 |
/// The maximum width to display the image in half-inch increments. |
| 64 |
/// |
| 65 |
/// See [\[MS-ONE\] 2.3.21]. |
| 66 |
/// |
| 67 |
/// [\[MS-ONE\] 2.3.21]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/2561c763-93b8-4b64-b6c7-1b86335d5b85 |
| 68 |
pub fn layout_max_width(&self) -> Option<f32> { |
| 69 |
self.layout_max_width |
| 70 |
} |
| 71 |
|
| 72 |
/// The maximum height to display the image in half-inch increments. |
| 73 |
/// |
| 74 |
/// See [\[MS-ONE\] 2.3.23]. |
| 75 |
/// |
| 76 |
/// [\[MS-ONE\] 2.3.23]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/ce514d53-1229-4e77-9908-ef8de1761ceb |
| 77 |
pub fn layout_max_height(&self) -> Option<f32> { |
| 78 |
self.layout_max_height |
| 79 |
} |
| 80 |
|
| 81 |
/// Alternative text for the image. |
| 82 |
/// |
| 83 |
/// Usually this seems to be the result of OneNote's OCR processing. |
| 84 |
/// |
| 85 |
/// See [\[MS-ONE\] 2.2.79]. |
| 86 |
/// |
| 87 |
/// [\[MS-ONE\] 2.2.79]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/f39569a6-84fa-4d5b-8a65-2b3e0ee36117 |
| 88 |
pub fn alt_text(&self) -> Option<&str> { |
| 89 |
self.alt_text.as_deref() |
| 90 |
} |
| 91 |
|
| 92 |
/// The image's alignment relative to the containing outline element (if present). |
| 93 |
/// |
| 94 |
/// See [\[MS-ONE\] 2.3.27]. |
| 95 |
/// |
| 96 |
/// [\[MS-ONE\] 2.3.27]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/61fa50be-c355-4b8d-ac01-761a2f7f66c0 |
| 97 |
pub fn layout_alignment_in_parent(&self) -> Option<LayoutAlignment> { |
| 98 |
self.layout_alignment_in_parent |
| 99 |
} |
| 100 |
|
| 101 |
/// The image's alignment. |
| 102 |
/// |
| 103 |
/// See [\[MS-ONE\] 2.3.33]. |
| 104 |
/// |
| 105 |
/// [\[MS-ONE\] 2.3.33]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4e7fe9db-2fdb-4239-b291-dc4b909c94ad |
| 106 |
pub fn layout_alignment_self(&self) -> Option<LayoutAlignment> { |
| 107 |
self.layout_alignment_self |
| 108 |
} |
| 109 |
|
| 110 |
/// The image's original file name. |
| 111 |
/// |
| 112 |
/// See [\[MS-ONE\] 2.2.75]. |
| 113 |
/// |
| 114 |
/// [\[MS-ONE\] 2.2.75]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/91f543ab-dfe5-47ce-9c61-a49680c726bb |
| 115 |
pub fn image_filename(&self) -> Option<&str> { |
| 116 |
self.image_filename.as_deref() |
| 117 |
} |
| 118 |
|
| 119 |
/// The page number to display if the image file contains multiple pages (e.g. XPS, PDF). |
| 120 |
/// |
| 121 |
/// See [\[MS-ONE\] 2.3.95]. |
| 122 |
/// |
| 123 |
/// [\[MS-ONE\] 2.3.95]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/acf52570-aa45-45ef-a62e-38f4a5844d5e |
| 124 |
pub fn displayed_page_number(&self) -> Option<u32> { |
| 125 |
self.displayed_page_number |
| 126 |
} |
| 127 |
|
| 128 |
/// The text for the image. |
| 129 |
pub fn text(&self) -> Option<&str> { |
| 130 |
self.text.as_deref() |
| 131 |
} |
| 132 |
|
| 133 |
/// The text's language code (MS-LCID) |
| 134 |
pub fn text_language_code(&self) -> Option<u32> { |
| 135 |
self.text_language_code |
| 136 |
} |
| 137 |
|
| 138 |
/// The image's width in half-inch increments. |
| 139 |
/// |
| 140 |
/// See [\[MS-ONE\] 2.3.100]. |
| 141 |
/// |
| 142 |
/// [\[MS-ONE\] 2.3.100]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/b4feae22-69cb-4623-8d40-faf97a026465 |
| 143 |
pub fn picture_width(&self) -> Option<f32> { |
| 144 |
self.picture_width |
| 145 |
} |
| 146 |
|
| 147 |
/// The image's height in half-inch increments. |
| 148 |
/// |
| 149 |
/// See [\[MS-ONE\] 2.3.101]. |
| 150 |
/// |
| 151 |
/// [\[MS-ONE\] 2.3.101]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/65454c84-1a39-4e81-ba50-053666d92dd0 |
| 152 |
pub fn picture_height(&self) -> Option<f32> { |
| 153 |
self.picture_height |
| 154 |
} |
| 155 |
|
| 156 |
/// A hyperlink associated with this image. |
| 157 |
pub fn hyperlink_url(&self) -> Option<&str> { |
| 158 |
self.hyperlink_url.as_deref() |
| 159 |
} |
| 160 |
|
| 161 |
/// The horizontal offset from the page origin in half-inch increments. |
| 162 |
/// |
| 163 |
/// See [\[MS-ONE\] 2.3.18]. |
| 164 |
/// |
| 165 |
/// [\[MS-ONE\] 2.3.18]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/5fb9e84a-c9e9-4537-ab14-e5512f24669a |
| 166 |
pub fn offset_horizontal(&self) -> Option<f32> { |
| 167 |
self.offset_horizontal |
| 168 |
} |
| 169 |
|
| 170 |
/// The vertical offset from the page origin in half-inch increments. |
| 171 |
/// |
| 172 |
/// See [\[MS-ONE\] 2.3.19]. |
| 173 |
/// |
| 174 |
/// [\[MS-ONE\] 2.3.19]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/5c4992ba-1db5-43e9-83dd-7299c562104d |
| 175 |
pub fn offset_vertical(&self) -> Option<f32> { |
| 176 |
self.offset_vertical |
| 177 |
} |
| 178 |
|
| 179 |
/// Whether the image is a background image. |
| 180 |
/// |
| 181 |
/// See [\[MS-ONE\] 2.3.61]. |
| 182 |
/// |
| 183 |
/// [\[MS-ONE\] 2.3.61]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/d8623a4b-7496-48fd-af00-8c8f9507c93b |
| 184 |
pub fn is_background(&self) -> bool { |
| 185 |
self.is_background |
| 186 |
} |
| 187 |
|
| 188 |
/// Note tags for this image. |
| 189 |
pub fn note_tags(&self) -> &[NoteTag] { |
| 190 |
&self.note_tags |
| 191 |
} |
| 192 |
|
| 193 |
/// Embedded iframes for this image. |
| 194 |
pub fn embeds(&self) -> &[IFrame] { |
| 195 |
&self.embeds |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
pub(crate) fn parse_image(image_id: ExGuid, space: &ObjectSpace) -> Result<Image> { |
| 200 |
let node_object = space |
| 201 |
.get_object(image_id) |
| 202 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("image is missing".into()))?; |
| 203 |
let node = image_node::parse(node_object)?; |
| 204 |
|
| 205 |
let container_data = node |
| 206 |
.picture_container |
| 207 |
.map(|container_object_id| { |
| 208 |
space |
| 209 |
.get_object(container_object_id) |
| 210 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("image container is missing".into())) |
| 211 |
}) |
| 212 |
.transpose()? |
| 213 |
.map(|container_object| picture_container::parse(container_object)) |
| 214 |
.transpose()?; |
| 215 |
|
| 216 |
let (data, extension) = if let Some(data) = container_data { |
| 217 |
(Some(data.data), data.extension) |
| 218 |
} else { |
| 219 |
(None, None) |
| 220 |
}; |
| 221 |
|
| 222 |
let embed = node |
| 223 |
.iframe |
| 224 |
.into_iter() |
| 225 |
.map(|iframe_id| parse_iframe(iframe_id, space)) |
| 226 |
.collect::<Result<_>>()?; |
| 227 |
|
| 228 |
// TODO: Parse language code |
| 229 |
|
| 230 |
let image = Image { |
| 231 |
data, |
| 232 |
extension, |
| 233 |
layout_max_width: node.layout_max_width, |
| 234 |
layout_max_height: node.layout_max_height, |
| 235 |
alt_text: node.alt_text.map(String::from), |
| 236 |
layout_alignment_in_parent: node.layout_alignment_in_parent, |
| 237 |
layout_alignment_self: node.layout_alignment_self, |
| 238 |
image_filename: node.image_filename, |
| 239 |
displayed_page_number: node.displayed_page_number, |
| 240 |
text: node.text.map(String::from), |
| 241 |
text_language_code: node.text_language_code, |
| 242 |
picture_width: node.picture_width, |
| 243 |
picture_height: node.picture_height, |
| 244 |
hyperlink_url: node.hyperlink_url.map(String::from), |
| 245 |
offset_horizontal: node.offset_from_parent_horiz, |
| 246 |
offset_vertical: node.offset_from_parent_vert, |
| 247 |
is_background: node.is_background, |
| 248 |
note_tags: parse_note_tags(node.note_tags, space)?, |
| 249 |
embeds: embed, |
| 250 |
}; |
| 251 |
|
| 252 |
Ok(image) |
| 253 |
} |