| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::one::property::object_reference::ObjectReference; |
| 3 |
use crate::one::property::object_space_reference::ObjectSpaceReference; |
| 4 |
use crate::one::property::{simple, PropertyType}; |
| 5 |
use crate::onestore::object::Object; |
| 6 |
use crate::onestore::types::compact_id::CompactId; |
| 7 |
use crate::onestore::types::jcid::JcId; |
| 8 |
use crate::onestore::types::object_prop_set::ObjectPropSet; |
| 9 |
use crate::onestore::types::prop_set::PropertySet; |
| 10 |
use crate::onestore::types::property::PropertyId; |
| 11 |
|
| 12 |
/// An embedded ink handwriting container. |
| 13 |
#[derive(Debug)] |
| 14 |
pub(crate) struct Data { |
| 15 |
pub(crate) space_width: Option<f32>, |
| 16 |
pub(crate) space_height: Option<f32>, |
| 17 |
|
| 18 |
pub(crate) start_x: Option<f32>, |
| 19 |
pub(crate) start_y: Option<f32>, |
| 20 |
pub(crate) height: Option<f32>, |
| 21 |
pub(crate) width: Option<f32>, |
| 22 |
pub(crate) offset_horiz: Option<f32>, |
| 23 |
pub(crate) offset_vert: Option<f32>, |
| 24 |
} |
| 25 |
|
| 26 |
impl Data { |
| 27 |
pub(crate) fn parse(object: &Object) -> Result<Option<Vec<Data>>> { |
| 28 |
let (prop_id, prop_sets) = match object.props().get(PropertyType::TextRunData) { |
| 29 |
Some(value) => value.to_property_values().ok_or_else(|| { |
| 30 |
ErrorKind::MalformedOneNoteFileData( |
| 31 |
"embedded ink container is not a property values list".into(), |
| 32 |
) |
| 33 |
})?, |
| 34 |
None => return Ok(None), |
| 35 |
}; |
| 36 |
|
| 37 |
let data = prop_sets |
| 38 |
.iter() |
| 39 |
.map(|props| { |
| 40 |
let object = Self::parse_object(object, prop_id, props)?; |
| 41 |
let data = Self::parse_data(object)?; |
| 42 |
|
| 43 |
Ok(data) |
| 44 |
}) |
| 45 |
.collect::<Result<Vec<_>>>()?; |
| 46 |
|
| 47 |
Ok(Some(data)) |
| 48 |
} |
| 49 |
|
| 50 |
fn parse_object<'a>( |
| 51 |
object: &'a Object, |
| 52 |
prop_id: PropertyId, |
| 53 |
props: &PropertySet, |
| 54 |
) -> Result<Object<'a>> { |
| 55 |
Ok(Object { |
| 56 |
context_id: object.context_id, |
| 57 |
jc_id: JcId(prop_id.value()), |
| 58 |
props: ObjectPropSet { |
| 59 |
object_ids: Self::get_object_ids(props, object)?, |
| 60 |
object_space_ids: Self::get_object_space_ids(props, object)?, |
| 61 |
context_ids: vec![], |
| 62 |
properties: props.clone(), |
| 63 |
}, |
| 64 |
file_data: None, |
| 65 |
mapping: object.mapping.clone(), |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
fn parse_data(object: Object) -> Result<Data> { |
| 70 |
let space_width = simple::parse_f32(PropertyType::EmbeddedInkSpaceWidth, &object)?; |
| 71 |
let space_height = simple::parse_f32(PropertyType::EmbeddedInkSpaceHeight, &object)?; |
| 72 |
|
| 73 |
let start_x = simple::parse_f32(PropertyType::EmbeddedInkStartX, &object)?; |
| 74 |
let start_y = simple::parse_f32(PropertyType::EmbeddedInkStartY, &object)?; |
| 75 |
let height = simple::parse_f32(PropertyType::EmbeddedInkHeight, &object)?; |
| 76 |
let width = simple::parse_f32(PropertyType::EmbeddedInkWidth, &object)?; |
| 77 |
let offset_horiz = simple::parse_f32(PropertyType::EmbeddedInkOffsetHoriz, &object)?; |
| 78 |
let offset_vert = simple::parse_f32(PropertyType::EmbeddedInkOffsetVert, &object)?; |
| 79 |
|
| 80 |
let data = Data { |
| 81 |
space_width, |
| 82 |
space_height, |
| 83 |
start_x, |
| 84 |
start_y, |
| 85 |
height, |
| 86 |
width, |
| 87 |
offset_horiz, |
| 88 |
offset_vert, |
| 89 |
}; |
| 90 |
|
| 91 |
Ok(data) |
| 92 |
} |
| 93 |
|
| 94 |
fn get_object_ids(props: &PropertySet, object: &Object) -> Result<Vec<CompactId>> { |
| 95 |
Ok(object |
| 96 |
.props |
| 97 |
.object_ids |
| 98 |
.iter() |
| 99 |
.skip(ObjectReference::get_offset( |
| 100 |
PropertyType::TextRunData, |
| 101 |
object, |
| 102 |
)?) |
| 103 |
.take(ObjectReference::count_references(props.values())) |
| 104 |
.copied() |
| 105 |
.collect()) |
| 106 |
} |
| 107 |
|
| 108 |
fn get_object_space_ids(props: &PropertySet, object: &Object) -> Result<Vec<CompactId>> { |
| 109 |
Ok(object |
| 110 |
.props |
| 111 |
.object_ids |
| 112 |
.iter() |
| 113 |
.skip(ObjectSpaceReference::get_offset( |
| 114 |
PropertyType::TextRunData, |
| 115 |
object, |
| 116 |
)?) |
| 117 |
.take(ObjectSpaceReference::count_references(props.values())) |
| 118 |
.copied() |
| 119 |
.collect()) |
| 120 |
} |
| 121 |
} |