use std::io::Cursor;
use quick_xml::{
Reader,
Writer,
events::{
BytesStart,
Event,
},
};
use super::{
super::{
EnumValue,
Int32Value,
},
EffectList,
GradientFill,
NoFill,
Outline,
SolidFill,
TextCapsValues,
TextFontType,
};
use crate::{
reader::driver::{
get_attribute,
xml_read_loop,
},
structs::StringValue,
writer::driver::{
write_end_tag,
write_start_tag,
},
};
#[derive(Clone, Default, Debug)]
pub struct RunProperties {
text: Box<str>,
kumimoji: StringValue,
language: StringValue,
alternative_language: StringValue,
bold: StringValue,
sz: StringValue,
italic: StringValue,
capital: EnumValue<TextCapsValues>,
spacing: Int32Value,
strike: StringValue,
outline: Option<Box<Outline>>,
solid_fill: Option<Box<SolidFill>>,
latin_font: Option<Box<TextFontType>>,
east_asian_font: Option<Box<TextFontType>>,
complex_script_font: Option<Box<TextFontType>>,
gradient_fill: Option<Box<GradientFill>>,
no_fill: Option<NoFill>,
effect_list: Option<Box<EffectList>>,
}
impl RunProperties {
#[inline]
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use text()")]
pub fn get_text(&self) -> &str {
self.text()
}
#[inline]
pub fn set_text<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.text = value.into().into_boxed_str();
self
}
#[inline]
#[must_use]
pub fn kumimoji(&self) -> &str {
self.kumimoji.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use kumimoji()")]
pub fn get_kumimoji(&self) -> &str {
self.kumimoji()
}
#[inline]
pub fn set_kumimoji<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.kumimoji.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn language(&self) -> &str {
self.language.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use language()")]
pub fn get_language(&self) -> &str {
self.language()
}
#[inline]
pub fn set_language<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.language.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn alternative_language(&self) -> &str {
self.alternative_language.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use alternative_language()")]
pub fn get_alternative_language(&self) -> &str {
self.alternative_language()
}
#[inline]
pub fn set_alternative_language<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.alternative_language.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn bold(&self) -> &str {
self.bold.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use bold()")]
pub fn get_bold(&self) -> &str {
self.bold()
}
#[inline]
pub fn set_bold<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.bold.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn sz(&self) -> &str {
self.sz.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use sz()")]
pub fn get_sz(&self) -> &str {
self.sz()
}
#[inline]
pub fn set_sz<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.sz.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn italic(&self) -> &str {
self.italic.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use italic()")]
pub fn get_italic(&self) -> &str {
self.italic()
}
#[inline]
pub fn set_italic<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.italic.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn capital(&self) -> &TextCapsValues {
self.capital.value()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use capital()")]
pub fn get_capital(&self) -> &TextCapsValues {
self.capital()
}
#[inline]
pub fn set_capital(&mut self, value: TextCapsValues) -> &mut Self {
self.capital.set_value(value);
self
}
#[inline]
#[must_use]
pub fn spacing(&self) -> i32 {
self.spacing.value()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use spacing()")]
pub fn get_spacing(&self) -> i32 {
self.spacing()
}
#[inline]
pub fn set_spacing(&mut self, value: i32) -> &mut Self {
self.spacing.set_value(value);
self
}
#[inline]
#[must_use]
pub fn strike(&self) -> &str {
self.strike.value_str()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use strike()")]
pub fn get_strike(&self) -> &str {
self.strike()
}
#[inline]
pub fn set_strike<S: Into<String>>(&mut self, value: S) -> &mut Self {
self.strike.set_value_string(value.into());
self
}
#[inline]
#[must_use]
pub fn solid_fill(&self) -> Option<&SolidFill> {
self.solid_fill.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use solid_fill()")]
pub fn get_solid_fill(&self) -> Option<&SolidFill> {
self.solid_fill()
}
#[inline]
pub fn solid_fill_mut(&mut self) -> Option<&mut SolidFill> {
self.solid_fill.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use solid_fill_mut()")]
pub fn get_solid_fill_mut(&mut self) -> Option<&mut SolidFill> {
self.solid_fill_mut()
}
#[inline]
pub fn set_solid_fill(&mut self, value: SolidFill) -> &mut Self {
self.solid_fill = Some(Box::new(value));
self
}
#[inline]
#[must_use]
pub fn outline(&self) -> Option<&Outline> {
self.outline.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use outline()")]
pub fn get_outline(&self) -> Option<&Outline> {
self.outline()
}
#[inline]
pub fn outline_mut(&mut self) -> Option<&mut Outline> {
self.outline.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use outline_mut()")]
pub fn get_outline_mut(&mut self) -> Option<&mut Outline> {
self.outline_mut()
}
#[inline]
pub fn set_outline(&mut self, value: Outline) -> &mut Self {
self.outline = Some(Box::new(value));
self
}
#[inline]
#[must_use]
pub fn latin_font(&self) -> Option<&TextFontType> {
self.latin_font.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use latin_font()")]
pub fn get_latin_font(&self) -> Option<&TextFontType> {
self.latin_font()
}
#[inline]
pub fn latin_font_mut(&mut self) -> Option<&mut TextFontType> {
self.latin_font.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use latin_font_mut()")]
pub fn get_latin_font_mut(&mut self) -> Option<&mut TextFontType> {
self.latin_font_mut()
}
#[inline]
pub fn set_latin_font(&mut self, value: TextFontType) -> &mut Self {
self.latin_font = Some(Box::new(value));
self
}
#[inline]
#[must_use]
pub fn east_asian_font(&self) -> Option<&TextFontType> {
self.east_asian_font.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use east_asian_font()")]
pub fn get_east_asian_font(&self) -> Option<&TextFontType> {
self.east_asian_font()
}
#[inline]
pub fn east_asian_font_mut(&mut self) -> Option<&mut TextFontType> {
self.east_asian_font.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use east_asian_font_mut()")]
pub fn get_east_asian_font_mut(&mut self) -> Option<&mut TextFontType> {
self.east_asian_font_mut()
}
#[inline]
pub fn set_east_asian_font(&mut self, value: TextFontType) -> &mut Self {
self.east_asian_font = Some(Box::new(value));
self
}
#[inline]
#[must_use]
pub fn complex_script_font(&self) -> Option<&TextFontType> {
self.complex_script_font.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use complex_script_font()")]
pub fn get_complex_script_font(&self) -> Option<&TextFontType> {
self.complex_script_font()
}
#[inline]
pub fn complex_script_font_mut(&mut self) -> Option<&mut TextFontType> {
self.complex_script_font.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use complex_script_font_mut()")]
pub fn get_complex_script_font_mut(&mut self) -> Option<&mut TextFontType> {
self.complex_script_font_mut()
}
#[inline]
pub fn set_complex_script_font(&mut self, value: TextFontType) -> &mut Self {
self.complex_script_font = Some(Box::new(value));
self
}
#[inline]
#[must_use]
pub fn gradient_fill(&self) -> Option<&GradientFill> {
self.gradient_fill.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use gradient_fill()")]
pub fn get_gradient_fill(&self) -> Option<&GradientFill> {
self.gradient_fill()
}
#[inline]
pub fn gradient_fill_mut(&mut self) -> Option<&mut GradientFill> {
self.gradient_fill.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use gradient_fill_mut()")]
pub fn get_gradient_fill_mut(&mut self) -> Option<&mut GradientFill> {
self.gradient_fill_mut()
}
#[inline]
pub fn set_gradient_fill(&mut self, value: GradientFill) -> &mut Self {
self.gradient_fill = Some(Box::new(value));
self
}
#[inline]
#[must_use]
pub fn no_fill(&self) -> Option<&NoFill> {
self.no_fill.as_ref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use no_fill()")]
pub fn get_no_fill(&self) -> Option<&NoFill> {
self.no_fill()
}
#[inline]
pub fn no_fill_mut(&mut self) -> Option<&mut NoFill> {
self.no_fill.as_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use no_fill_mut()")]
pub fn get_no_fill_mut(&mut self) -> Option<&mut NoFill> {
self.no_fill_mut()
}
#[inline]
pub fn set_no_fill(&mut self, value: NoFill) -> &mut Self {
self.no_fill = Some(value);
self
}
#[inline]
#[must_use]
pub fn effect_list(&self) -> Option<&EffectList> {
self.effect_list.as_deref()
}
#[inline]
#[must_use]
#[deprecated(since = "3.0.0", note = "Use effect_list()")]
pub fn get_effect_list(&self) -> Option<&EffectList> {
self.effect_list()
}
#[inline]
pub fn effect_list_mut(&mut self) -> Option<&mut EffectList> {
self.effect_list.as_deref_mut()
}
#[inline]
#[deprecated(since = "3.0.0", note = "Use effect_list_mut()")]
pub fn get_effect_list_mut(&mut self) -> Option<&mut EffectList> {
self.effect_list_mut()
}
#[inline]
pub fn set_effect_list(&mut self, value: EffectList) -> &mut Self {
self.effect_list = Some(Box::new(value));
self
}
pub(crate) fn set_attributes<R: std::io::BufRead>(
&mut self,
reader: &mut Reader<R>,
e: &BytesStart,
empty_flag: bool,
) {
if let Some(v) = get_attribute(e, b"kumimoji") {
self.set_kumimoji(v);
}
if let Some(v) = get_attribute(e, b"lang") {
self.set_language(v);
}
if let Some(v) = get_attribute(e, b"altLang") {
self.set_alternative_language(v);
}
if let Some(v) = get_attribute(e, b"b") {
self.set_bold(v);
}
if let Some(v) = get_attribute(e, b"sz") {
self.set_sz(v);
}
if let Some(v) = get_attribute(e, b"strike") {
self.set_strike(v);
}
if let Some(v) = get_attribute(e, b"i") {
self.set_italic(v);
}
if let Some(v) = get_attribute(e, b"cap") {
self.capital.set_value_string(v);
}
if let Some(v) = get_attribute(e, b"spc") {
self.spacing.set_value_string(v);
}
if empty_flag {
return;
}
xml_read_loop!(
reader,
Event::Start(ref e) => {
match e.name().into_inner() {
b"a:solidFill" => {
let mut obj = SolidFill::default();
obj.set_attributes(reader, e);
self.set_solid_fill(obj);
}
b"a:ln" => {
let mut obj = Outline::default();
obj.set_attributes(reader, e);
self.set_outline(obj);
}
b"a:gradFill" => {
let mut obj = GradientFill::default();
obj.set_attributes(reader, e);
self.set_gradient_fill(obj);
}
b"a:effectLst" => {
let mut effect_list = EffectList::default();
effect_list.set_attributes(reader, e, false);
self.set_effect_list(effect_list);
}
_ => (),
}
},
Event::Empty(ref e) => {
match e.name().into_inner() {
b"a:latin" => {
let mut obj = TextFontType::default();
obj.set_attributes(reader, e, true);
self.set_latin_font(obj);
}
b"a:ea" => {
let mut obj = TextFontType::default();
obj.set_attributes(reader, e, true);
self.set_east_asian_font(obj);
}
b"a:cs" => {
let mut obj = TextFontType::default();
obj.set_attributes(reader, e, true);
self.set_complex_script_font(obj);
}
b"a:noFill" => {
let obj = NoFill::default();
NoFill::set_attributes(reader, e, true);
self.set_no_fill(obj);
}
b"a:effectLst" => {
let mut obj = EffectList::default();
obj.set_attributes(reader, e, true);
self.set_effect_list(obj);
}
_ => (),
}
},
Event::End(ref e) => {
match e.name().into_inner() {
b"a:rPr" | b"a:endParaRPr" | b"a:defRPr" => return,
_ => (),
}
},
Event::Eof => panic!(
"Error: Could not find {} end element",
"a:rPr, a:endParaRPr, a:defRPr"
)
);
}
#[inline]
pub(crate) fn write_to_rpr(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
self.write_to(writer, "a:rPr");
}
#[inline]
pub(crate) fn write_to_end_para_rpr(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
self.write_to(writer, "a:endParaRPr");
}
#[inline]
pub(crate) fn write_to_def_rpr(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
self.write_to(writer, "a:defRPr");
}
fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>, tag_name: &str) {
let mut attributes: crate::structs::AttrCollection = Vec::new();
if self.kumimoji.has_value() {
attributes.push(("kumimoji", self.kumimoji.value_str()).into());
}
if self.language.has_value() {
attributes.push(("lang", self.language.value_str()).into());
}
if self.alternative_language.has_value() {
attributes.push(("altLang", self.alternative_language.value_str()).into());
}
if self.sz.has_value() {
attributes.push(("sz", self.sz.value_str()).into());
}
if self.bold.has_value() {
attributes.push(("b", self.bold.value_str()).into());
}
if self.italic.has_value() {
attributes.push(("i", self.italic.value_str()).into());
}
if self.capital.has_value() {
attributes.push(("cap", self.capital.value_string()).into());
}
let spc = self.spacing.value_string();
if self.spacing.has_value() {
attributes.push(("spc", &spc).into());
}
if self.strike.has_value() {
attributes.push(("strike", self.strike.value_str()).into());
}
if self.solid_fill.is_some()
|| self.outline.is_some()
|| self.latin_font.is_some()
|| self.east_asian_font.is_some()
|| self.gradient_fill.is_some()
|| self.effect_list.is_some()
{
write_start_tag(writer, tag_name, attributes, false);
if let Some(v) = &self.solid_fill {
v.write_to(writer);
}
if let Some(v) = &self.outline {
v.write_to(writer);
}
if let Some(v) = &self.latin_font {
v.write_to_latin(writer);
}
if let Some(v) = &self.east_asian_font {
v.write_to_ea(writer);
}
if let Some(v) = &self.complex_script_font {
v.write_to_cs(writer);
}
if let Some(v) = &self.gradient_fill {
v.write_to(writer);
}
if self.no_fill.is_some() {
NoFill::write_to(writer);
}
if let Some(v) = &self.effect_list {
v.write_to(writer);
}
write_end_tag(writer, tag_name);
} else {
write_start_tag(writer, tag_name, attributes, true);
}
}
}