use std::io::Cursor;
use quick_xml::{
Reader,
Writer,
events::{
BytesStart,
Event,
},
};
use super::{
BooleanValue,
ObjectAnchor,
StringValue,
UInt32Value,
};
use crate::{
reader::driver::{
get_attribute,
set_string_from_xml,
xml_read_loop,
},
structs::{
MediaObject,
raw::RawRelationships,
},
writer::driver::{
write_end_tag,
write_start_tag,
},
};
#[derive(Clone, Default, Debug)]
pub struct EmbeddedObjectProperties {
prog_id: StringValue,
shape_id: UInt32Value,
image: MediaObject,
default_size: BooleanValue,
auto_pict: BooleanValue,
object_anchor: ObjectAnchor,
}
impl EmbeddedObjectProperties {
#[inline]
#[must_use]
pub fn prog_id(&self) -> &str {
self.prog_id.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use prog_id()")]
pub fn get_prog_id(&self) -> &str {
self.prog_id()
}
#[inline]
pub fn set_prog_id<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.prog_id.set_value(value);
self
}
#[inline]
#[must_use]
pub fn shape_id(&self) -> u32 {
self.shape_id.value()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use shape_id()")]
pub fn get_shape_id(&self) -> u32 {
self.shape_id()
}
#[inline]
pub fn set_shape_id(&mut self, value: u32) -> &mut Self {
self.shape_id.set_value(value);
self
}
#[inline]
#[must_use]
pub fn image(&self) -> &MediaObject {
&self.image
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use image()")]
pub fn get_image(&self) -> &MediaObject {
self.image()
}
#[inline]
pub fn image_mut(&mut self) -> &mut MediaObject {
&mut self.image
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use image_mut()")]
pub fn get_image_mut(&mut self) -> &mut MediaObject {
self.image_mut()
}
#[inline]
pub fn set_image(&mut self, value: MediaObject) {
self.image = value;
}
#[inline]
#[must_use]
pub fn default_size(&self) -> bool {
self.default_size.value()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use default_size()")]
pub fn get_default_size(&self) -> bool {
self.default_size()
}
#[inline]
pub fn set_default_size(&mut self, value: bool) -> &mut Self {
self.default_size.set_value(value);
self
}
#[inline]
#[must_use]
pub fn auto_pict(&self) -> bool {
self.auto_pict.value()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use auto_pict()")]
pub fn get_auto_pict(&self) -> bool {
self.auto_pict()
}
#[inline]
pub fn set_auto_pict(&mut self, value: bool) -> &mut Self {
self.auto_pict.set_value(value);
self
}
#[inline]
#[must_use]
pub fn object_anchor(&self) -> &ObjectAnchor {
&self.object_anchor
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use object_anchor()")]
pub fn get_object_anchor(&self) -> &ObjectAnchor {
self.object_anchor()
}
#[inline]
pub fn object_anchor_mut(&mut self) -> &mut ObjectAnchor {
&mut self.object_anchor
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use object_anchor_mut()")]
pub fn get_object_anchor_mut(&mut self) -> &mut ObjectAnchor {
self.object_anchor_mut()
}
#[inline]
pub fn set_object_anchor(&mut self, value: ObjectAnchor) -> &mut Self {
self.object_anchor = value;
self
}
pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
e: &BytesStart,
relationships: &RawRelationships,
) {
let r_id = get_attribute(e, b"r:id").unwrap();
let attached_file = relationships.relationship_by_rid(&r_id).raw_file();
self.image_mut()
.set_image_name(attached_file.file_name());
self.image_mut()
.set_image_data(attached_file.file_data());
set_string_from_xml!(self, e, default_size, "defaultSize");
set_string_from_xml!(self, e, auto_pict, "autoPict");
xml_read_loop!(
reader,
Event::Start(ref e) => {
if e.name().into_inner() == b"anchor" {
self.object_anchor.set_attributes(reader, e);
}
},
Event::End(ref e) => {
if e.name().into_inner() == b"objectPr" {
return
}
},
Event::Eof => panic!("Error: Could not find {} end element", "objectPr")
);
}
pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>, r_id: usize) {
let mut attributes: crate::structs::AttrCollection = Vec::new();
if self.default_size.has_value() {
attributes.push(("defaultSize", self.default_size.value_string()).into());
}
if self.auto_pict.has_value() {
attributes.push(("autoPict", self.auto_pict.value_string()).into());
}
let r_id_str = format!("rId{r_id}");
attributes.push(("r:id", &r_id_str).into());
write_start_tag(writer, "objectPr", attributes, false);
self.object_anchor.write_to(writer);
write_end_tag(writer, "objectPr");
}
}