Debian Codegraph @

.cargo/vendor/onenote_parser/src/onenote/rich_text.rs

raw
1 use crate::errors::{ErrorKind, Result};
2 use crate::fsshttpb::data::exguid::ExGuid;
3 use crate::one::property::charset::Charset;
4 use crate::one::property::color_ref::ColorRef;
5 use crate::one::property::layout_alignment::LayoutAlignment;
6 use crate::one::property::paragraph_alignment::ParagraphAlignment;
7 use crate::one::property_set::{embedded_ink_container, paragraph_style_object, rich_text_node};
8 use crate::onenote::ink::{parse_ink_data, Ink, InkBoundingBox};
9 use crate::onenote::note_tag::{parse_note_tags, NoteTag};
10 use crate::onestore::object_space::ObjectSpace;
11 use itertools::Itertools;
12
13 /// A rich text paragraph.
14 ///
15 /// # Formatting
16 ///
17 /// Rich-text formatting is represented by storing the paragraph text along
18 /// with a list of text runs. Each text run specified formatting that is only
19 /// applied to a substring of the paragraph text.
20 ///
21 /// The text run indices represent where each text run ends. The last text run
22 /// always ends at the end of the paragraph text. If there are no text run indices,
23 /// the text run formatting applies to the whole paragraph.
24 ///
25 /// Text runs can be rendered by splitting the paragraph text at the text run
26 /// indices and then applying each text run formatting to its respective
27 /// substring.
28 #[derive(Clone, Debug)]
29 pub struct RichText {
30 pub(crate) text: String,
31
32 pub(crate) text_run_formatting: Vec<ParagraphStyling>,
33 pub(crate) text_run_indices: Vec<u32>,
34
35 pub(crate) paragraph_style: ParagraphStyling,
36 pub(crate) paragraph_space_before: f32,
37 pub(crate) paragraph_space_after: f32,
38 pub(crate) paragraph_line_spacing_exact: Option<f32>,
39 pub(crate) paragraph_alignment: ParagraphAlignment,
40
41 pub(crate) layout_alignment_in_parent: Option<LayoutAlignment>,
42 pub(crate) layout_alignment_self: Option<LayoutAlignment>,
43
44 pub(crate) note_tags: Vec<NoteTag>,
45 pub(crate) embedded_objects: Vec<EmbeddedObject>,
46 }
47
48 impl RichText {
49 /// The paragraph text content.
50 pub fn text(&self) -> &str {
51 &self.text
52 }
53
54 /// The formatting of each text run.
55 ///
56 /// See [\[MS-ONE\] 2.3.77].
57 ///
58 /// [\[MS-ONE\] 2.3.77]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/7b560477-14d0-4f2c-a65e-4159f69f299f
59 pub fn text_run_formatting(&self) -> &[ParagraphStyling] {
60 &self.text_run_formatting
61 }
62
63 /// The character positions where the text runs end.
64 ///
65 /// See [\[MS-ONE\] 2.3.76].
66 ///
67 /// [\[MS-ONE\] 2.3.76]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/f5ae3d7a-09dd-4904-a8bd-7a529d8067c3
68 pub fn text_run_indices(&self) -> &[u32] {
69 &self.text_run_indices
70 }
71
72 /// The base paragraph style.
73 pub fn paragraph_style(&self) -> &ParagraphStyling {
74 &self.paragraph_style
75 }
76
77 /// The paragraph's top margin in half-inch increments.
78 ///
79 /// See [\[MS-ONE\] 2.3.81].
80 ///
81 /// [\[MS-ONE\] 2.3.81]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/1a2958bd-7512-419b-a8a5-eda200edb7cd
82 pub fn paragraph_space_before(&self) -> f32 {
83 self.paragraph_space_before
84 }
85
86 /// The paragraph's bottom margin in half-inch increments.
87 ///
88 /// See [\[MS-ONE\] 2.3.82].
89 ///
90 /// [\[MS-ONE\] 2.3.82]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/505393e2-c641-416f-be83-050da44d581d
91 pub fn paragraph_space_after(&self) -> f32 {
92 self.paragraph_space_after
93 }
94
95 /// The paragraph's line spacing in half-inch increments.
96 ///
97 /// See [\[MS-ONE\] 2.3.83].
98 ///
99 /// [\[MS-ONE\] 2.3.83]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4474bd74-5407-4675-a9bb-a32f81eb799c
100 pub fn paragraph_line_spacing_exact(&self) -> Option<f32> {
101 self.paragraph_line_spacing_exact
102 }
103
104 /// The paragraph's text alignment.
105 ///
106 /// See [\[MS-ONE\] 2.3.94].
107 ///
108 /// [\[MS-ONE\] 2.3.94]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/36edb135-5e8e-400f-9394-82853d662d90
109 pub fn paragraph_alignment(&self) -> ParagraphAlignment {
110 self.paragraph_alignment
111 }
112
113 /// The paragraph's alignment relative to the containing outline element (if present).
114 ///
115 /// See [\[MS-ONE\] 2.3.27].
116 ///
117 /// [\[MS-ONE\] 2.3.27]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/61fa50be-c355-4b8d-ac01-761a2f7f66c0
118 pub fn layout_alignment_in_parent(&self) -> Option<LayoutAlignment> {
119 self.layout_alignment_in_parent
120 }
121
122 /// The paragraph's alignment.
123 ///
124 /// See [\[MS-ONE\] 2.3.33].
125 ///
126 /// [\[MS-ONE\] 2.3.33]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4e7fe9db-2fdb-4239-b291-dc4b909c94ad
127 pub fn layout_alignment_self(&self) -> Option<LayoutAlignment> {
128 self.layout_alignment_self
129 }
130
131 /// Note tags for this paragraph.
132 pub fn note_tags(&self) -> &[NoteTag] {
133 &self.note_tags
134 }
135
136 /// Objects embedded in this paragraph.
137 pub fn embedded_objects(&self) -> &[EmbeddedObject] {
138 &self.embedded_objects
139 }
140 }
141
142 /// An object embedded in a rich text paragraph.
143 #[derive(Clone, Debug)]
144 pub enum EmbeddedObject {
145 /// An ink handwriting object container.
146 Ink(EmbeddedInkContainer),
147
148 /// A space in the ink handwriting.
149 InkSpace(EmbeddedInkSpace),
150
151 /// A line break in the ink handwriting.
152 InkLineBreak,
153 }
154
155 /// An ink handwriting object container.
156 #[derive(Clone, Debug)]
157 pub struct EmbeddedInkContainer {
158 pub(crate) ink: Ink,
159 pub(crate) bounding_box: Option<InkBoundingBox>,
160 }
161
162 impl EmbeddedInkContainer {
163 /// The ink data embedded in a paragraph.
164 pub fn ink(&self) -> &Ink {
165 &self.ink
166 }
167
168 /// The ink object's bounding box.
169 pub fn bounding_box(&self) -> Option<&InkBoundingBox> {
170 self.bounding_box.as_ref()
171 }
172 }
173
174 /// A space in an embedded ink handwriting object.
175 #[derive(Clone, Debug)]
176 pub struct EmbeddedInkSpace {
177 height: f32,
178 width: f32,
179 }
180
181 impl EmbeddedInkSpace {
182 /// The space's height.
183 pub fn height(&self) -> f32 {
184 self.height
185 }
186
187 /// The space's width.
188 pub fn width(&self) -> f32 {
189 self.width
190 }
191 }
192
193 /// A paragraph's style.
194 ///
195 /// See [\[MS-ONE\] 2.2.43] and [\[MS-ONE\] 2.2.44].
196 ///
197 /// [\[MS-ONE\] 2.2.43]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/38eb9b74-cfaf-4df7-b061-a83968c7ff5b
198 /// [\[MS-ONE\] 2.2.44]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/f0baabae-f42a-42e0-8cb2-869d420e865f
199 #[derive(Clone, Debug)]
200 pub struct ParagraphStyling {
201 pub(crate) charset: Option<Charset>,
202 pub(crate) bold: bool,
203 pub(crate) italic: bool,
204 pub(crate) underline: bool,
205 pub(crate) strikethrough: bool,
206 pub(crate) superscript: bool,
207 pub(crate) subscript: bool,
208 pub(crate) font: Option<String>,
209 pub(crate) font_size: Option<u16>,
210 pub(crate) font_color: Option<ColorRef>,
211 pub(crate) highlight: Option<ColorRef>,
212 pub(crate) next_style: Option<String>,
213 pub(crate) style_id: Option<String>,
214 pub(crate) paragraph_alignment: Option<ParagraphAlignment>,
215 pub(crate) paragraph_space_before: Option<f32>,
216 pub(crate) paragraph_space_after: Option<f32>,
217 pub(crate) paragraph_line_spacing_exact: Option<f32>,
218 pub(crate) language_code: Option<u32>,
219 pub(crate) math_formatting: bool,
220 pub(crate) hyperlink: bool,
221 }
222
223 impl ParagraphStyling {
224 /// The text's charset.
225 pub fn charset(&self) -> Option<Charset> {
226 self.charset
227 }
228
229 /// Whether the text is bold.
230 pub fn bold(&self) -> bool {
231 self.bold
232 }
233
234 /// Whether the text is italic.
235 pub fn italic(&self) -> bool {
236 self.italic
237 }
238
239 /// Whether the text is underlined.
240 pub fn underline(&self) -> bool {
241 self.underline
242 }
243
244 /// Whether the text has strike-through formatting.
245 pub fn strikethrough(&self) -> bool {
246 self.strikethrough
247 }
248
249 /// Whether the text is formatted as superscript.
250 pub fn superscript(&self) -> bool {
251 self.superscript
252 }
253
254 /// Whether the text is formatted as subscript.
255 pub fn subscript(&self) -> bool {
256 self.subscript
257 }
258
259 /// The font for this text.
260 pub fn font(&self) -> Option<&str> {
261 self.font.as_deref()
262 }
263
264 /// The font size for this text in half-point increments.
265 ///
266 /// See [\[MS-ONE\] 2.3.16].
267 ///
268 /// [\[MS-ONE\] 2.3.16]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/f209fd9c-9042-4df2-b90c-1be20ac9c2d3
269 pub fn font_size(&self) -> Option<u16> {
270 self.font_size
271 }
272
273 /// The font color for this text.
274 ///
275 /// See [\[MS-ONE\] 2.3.45].
276 ///
277 /// [\[MS-ONE\] 2.3.45]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/17a7e6a7-7fa9-456f-a3fe-b2d8fef31be3
278 pub fn font_color(&self) -> Option<ColorRef> {
279 self.font_color
280 }
281
282 /// The background color for this text.
283 ///
284 /// See [\[MS-ONE\] 2.3.6].
285 ///
286 /// [\[MS-ONE\] 2.3.6]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/9932eafb-8200-4fc6-aa86-3a4d6a60bb62
287 pub fn highlight(&self) -> Option<ColorRef> {
288 self.highlight
289 }
290
291 /// The name of the default style for the next paragraph.
292 ///
293 /// See [\[MS-ONE\] 2.2.92].
294 ///
295 /// [\[MS-ONE\] 2.2.92]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/fa70a53b-2661-4d33-aec7-948488e49fc3
296 pub fn next_style(&self) -> Option<&str> {
297 self.next_style.as_deref()
298 }
299
300 /// The paragraph style's name.
301 ///
302 /// See [\[MS-ONE\] 2.2.83].
303 ///
304 /// [\[MS-ONE\] 2.2.83]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4c9ad3ed-d804-44df-9c49-55b2a867db66
305 pub fn style_id(&self) -> Option<&str> {
306 self.style_id.as_deref()
307 }
308
309 /// The paragraph alignment.
310 pub fn paragraph_alignment(&self) -> Option<ParagraphAlignment> {
311 self.paragraph_alignment
312 }
313
314 /// The paragraph's top margin in half-inch increments.
315 ///
316 /// See [\[MS-ONE\] 2.3.81].
317 ///
318 /// [\[MS-ONE\] 2.3.81]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/1a2958bd-7512-419b-a8a5-eda200edb7cd
319 pub fn paragraph_space_before(&self) -> Option<f32> {
320 self.paragraph_space_before
321 }
322
323 /// The paragraph's bottom margin in half-inch increments.
324 ///
325 /// See [\[MS-ONE\] 2.3.82].
326 ///
327 /// [\[MS-ONE\] 2.3.82]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/505393e2-c641-416f-be83-050da44d581d
328 pub fn paragraph_space_after(&self) -> Option<f32> {
329 self.paragraph_space_after
330 }
331
332 /// The paragraph's line spacing in half-inch increments.
333 ///
334 /// See [\[MS-ONE\] 2.3.83].
335 ///
336 /// [\[MS-ONE\] 2.3.83]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/4474bd74-5407-4675-a9bb-a32f81eb799c
337 pub fn paragraph_line_spacing_exact(&self) -> Option<f32> {
338 self.paragraph_line_spacing_exact
339 }
340
341 /// The LCID language code for the text.
342 ///
343 /// See [\[MS-ONE\] 2.3.26].
344 ///
345 /// [\[MS-ONE\] 2.3.26]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/f82cdbc0-d4e9-4cd0-bd0b-8c7734853d7f
346 pub fn language_code(&self) -> Option<u32> {
347 self.language_code
348 }
349
350 /// Whether the text is formatted as a math expression
351 pub fn math_formatting(&self) -> bool {
352 self.math_formatting
353 }
354
355 /// Whether the text is the display text for a hyperlink
356 pub fn hyperlink(&self) -> bool {
357 self.hyperlink
358 }
359 }
360
361 // Embedded object types
362 const INK_SPACE_BLOB: u32 = 0x00020026;
363 const INK_END_OF_LINE_BLOB: u32 = 0x00020027;
364
365 pub(crate) fn parse_rich_text(content_id: ExGuid, space: &ObjectSpace) -> Result<RichText> {
366 let object = space
367 .get_object(content_id)
368 .ok_or_else(|| ErrorKind::MalformedOneNoteData("rich text content is missing".into()))?;
369 let data = rich_text_node::parse(object)?;
370
371 // Parse the base paragraph style
372 let paragraph_style_object = space
373 .get_object(data.paragraph_style)
374 .ok_or_else(|| ErrorKind::MalformedOneNoteData("paragraph styling is missing".into()))?;
375 let paragraph_style_data = paragraph_style_object::parse(paragraph_style_object)?;
376 let paragraph_style = parse_style(paragraph_style_data);
377
378 // Parse the styles text runs (part 1)
379 let text_run_data = embedded_ink_container::Data::parse(object)?.unwrap_or_default();
380 let styles_data: Vec<paragraph_style_object::Data> = data
381 .text_run_formatting
382 .iter()
383 .map(|style_id| {
384 space
385 .get_object(*style_id)
386 .ok_or_else(|| ErrorKind::MalformedOneNoteData("styling is missing".into()).into())
387 })
388 .map(|style_object| style_object.and_then(|object| paragraph_style_object::parse(object)))
389 .collect::<Result<Vec<_>>>()?;
390
391 // Parse the embedded objects
392 let objects = text_run_data
393 .into_iter()
394 .zip(&styles_data)
395 .flat_map(|(object_data, style_data)| {
396 style_data
397 .text_run_is_embedded_object
398 .then(|| (style_data.text_run_object_type, object_data))
399 })
400 .collect_vec();
401
402 let mut objects_without_ref = 0;
403
404 let embedded_objects: Vec<_> = objects
405 .into_iter()
406 .enumerate()
407 .map(|(i, (object_type, embedded_data))| match object_type {
408 Some(INK_END_OF_LINE_BLOB) => {
409 objects_without_ref += 1;
410 Ok(Some(EmbeddedObject::InkLineBreak))
411 }
412 Some(INK_SPACE_BLOB) => {
413 objects_without_ref += 1;
414 parse_embedded_ink_space(embedded_data)
415 .map(|space| Some(EmbeddedObject::InkSpace(space)))
416 }
417 None => {
418 if !data.text_run_data_object.is_empty() {
419 return parse_embedded_ink_data(
420 data.text_run_data_object[i - objects_without_ref],
421 space,
422 embedded_data,
423 )
424 .map(|container| Some(EmbeddedObject::Ink(container)));
425 }
426
427 Ok(None)
428 }
429 Some(v) => Err(ErrorKind::MalformedOneNoteFileData(
430 format!("unknown embedded object type: {:x}", v).into(),
431 )
432 .into()),
433 })
434 .collect::<Result<Vec<_>>>()?
435 .into_iter()
436 .flatten()
437 .collect_vec();
438
439 // Parse the styles text runs (part 2)
440 let styles = styles_data.into_iter().map(parse_style).collect_vec();
441
442 // TODO: Parse lang code into iso code
443 // dia-i18n = "0.8.0"
444
445 let text = if !embedded_objects.is_empty() {
446 "".to_string()
447 } else {
448 data.text.unwrap_or_default()
449 };
450
451 let text = RichText {
452 text,
453 embedded_objects,
454 text_run_formatting: styles,
455 text_run_indices: data.text_run_indices,
456 paragraph_style,
457 paragraph_space_before: data.paragraph_space_before,
458 paragraph_space_after: data.paragraph_space_after,
459 paragraph_line_spacing_exact: data.paragraph_line_spacing_exact,
460 paragraph_alignment: data.paragraph_alignment,
461 layout_alignment_in_parent: data.layout_alignment_in_parent,
462 layout_alignment_self: data.layout_alignment_self,
463 note_tags: parse_note_tags(data.note_tags, space)?,
464 };
465
466 Ok(text)
467 }
468
469 fn parse_embedded_ink_data(
470 embedded_id: ExGuid,
471 space: &ObjectSpace,
472 data: embedded_ink_container::Data,
473 ) -> Result<EmbeddedInkContainer> {
474 let (strokes, bb) = parse_ink_data(embedded_id, space, None, None)?;
475
476 let display_bb = data
477 .start_x
478 .zip(data.start_y)
479 .zip(data.height)
480 .zip(data.width)
481 .map(|(((x, y), height), width)| InkBoundingBox {
482 x,
483 y,
484 height,
485 width,
486 });
487
488 let data = EmbeddedInkContainer {
489 ink: Ink {
490 ink_strokes: strokes,
491 bounding_box: bb,
492 offset_horizontal: data.offset_horiz,
493 offset_vertical: data.offset_vert,
494 },
495 bounding_box: display_bb,
496 };
497
498 Ok(data)
499 }
500
501 fn parse_embedded_ink_space(data: embedded_ink_container::Data) -> Result<EmbeddedInkSpace> {
502 let width = data.space_width.ok_or_else(|| {
503 ErrorKind::MalformedOneNoteFileData("embedded ink space has no width".into())
504 })?;
505
506 let height = data.space_height.ok_or_else(|| {
507 ErrorKind::MalformedOneNoteFileData("embedded ink space has no height".into())
508 })?;
509
510 Ok(EmbeddedInkSpace { height, width })
511 }
512
513 fn parse_style(data: paragraph_style_object::Data) -> ParagraphStyling {
514 ParagraphStyling {
515 charset: data.charset,
516 bold: data.bold,
517 italic: data.italic,
518 underline: data.underline,
519 strikethrough: data.strikethrough,
520 superscript: data.superscript,
521 subscript: data.subscript,
522 font: data.font,
523 font_size: data.font_size,
524 font_color: data.font_color,
525 highlight: data.highlight,
526 next_style: data.next_style,
527 style_id: data.style_id,
528 paragraph_alignment: data.paragraph_alignment,
529 paragraph_space_before: data.paragraph_space_before,
530 paragraph_space_after: data.paragraph_space_after,
531 paragraph_line_spacing_exact: data.paragraph_line_spacing_exact,
532 language_code: data.language_code,
533 math_formatting: data.math_formatting,
534 hyperlink: data.hyperlink,
535 }
536 }