diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2021-03-09 21:32:18 +0100 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2021-03-09 21:32:18 +0100 |
commit | bcdfeb3d4e2569e073a34be67f38f57da90be723 (patch) | |
tree | 918fe2f00bf4eefb373afbbe9d10e522c80086d2 | |
parent | 4d2edd797d5f8054284bc990371f28e918ea044b (diff) | |
download | wasm-card-main.tar.gz wasm-card-main.tar.bz2 |
Use LinearLayout for vcardmain
-rw-r--r-- | examples/cli.rs | 28 | ||||
-rw-r--r-- | src/model/property_groups/address.rs | 2 | ||||
-rw-r--r-- | src/model/property_groups/name.rs | 2 | ||||
-rw-r--r-- | src/model/property_groups/telephone.rs | 2 | ||||
-rw-r--r-- | src/model/vcard.rs | 87 |
5 files changed, 66 insertions, 55 deletions
diff --git a/examples/cli.rs b/examples/cli.rs new file mode 100644 index 0000000..7e5e6db --- /dev/null +++ b/examples/cli.rs @@ -0,0 +1,28 @@ +use wasm_card::model::property_groups::address::Address; +use wasm_card::model::property_groups::name::Name; +use wasm_card::model::property_groups::telephone::Telephone; +use wasm_card::model::vcard; + +fn main() { + let mut data = vcard::VCardData::new(); + data.add_name(Name { + first_name: "John".to_owned(), + last_name: "Doe".to_owned(), + ..Default::default() + }); + data.add_telephone(Telephone { + number: "+49 123 45678".to_owned(), + cell: true, + ..Default::default() + }); + data.add_address(Address { + street: "Musterweg 1".to_owned(), + code: "12345".to_owned(), + locality: "Musterhausen".to_owned(), + country: "Germany".to_owned(), + ..Default::default() + }); + + let pdf = data.generate_pdf().expect("Failed to generate PDF"); + std::fs::write("output.pdf", &pdf).expect("Failed to write PDF file"); +} diff --git a/src/model/property_groups/address.rs b/src/model/property_groups/address.rs index 00bca0b..e50a59a 100644 --- a/src/model/property_groups/address.rs +++ b/src/model/property_groups/address.rs @@ -1,7 +1,7 @@ use crate::model::input_fields::VCardPropertyInputField; use crate::model::*; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct Address { pub post_office_box: String, pub extension: String, diff --git a/src/model/property_groups/name.rs b/src/model/property_groups/name.rs index eb0d9da..4a4d72c 100644 --- a/src/model/property_groups/name.rs +++ b/src/model/property_groups/name.rs @@ -16,7 +16,7 @@ use crate::model::*; /// /// assert_eq!(name.generate_fn(), String::from("Sir Arthur Charles Clarke, CBE FRAS")); /// ``` -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct Name { pub prefix: String, pub first_name: String, diff --git a/src/model/property_groups/telephone.rs b/src/model/property_groups/telephone.rs index 03de4ee..dee28e1 100644 --- a/src/model/property_groups/telephone.rs +++ b/src/model/property_groups/telephone.rs @@ -1,7 +1,7 @@ use crate::model::input_fields::VCardPropertyInputField; use crate::model::*; -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq)] pub struct Telephone { pub number: String, pub work: bool, diff --git a/src/model/vcard.rs b/src/model/vcard.rs index ea66224..118d543 100644 --- a/src/model/vcard.rs +++ b/src/model/vcard.rs @@ -242,21 +242,21 @@ impl VCardData { pub fn generate_pdf(&self) -> Result<String, ()> { let regular_bytes = - include_bytes!("/usr/share/fonts/liberation/LiberationSans-Regular.ttf"); + include_bytes!("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"); let regular_font_data = fonts::FontData::new( regular_bytes.to_vec(), Some(printpdf::BuiltinFont::Helvetica), ) .expect("font data should be correct"); - let bold_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Bold.ttf"); + let bold_bytes = include_bytes!("/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"); let bold_font_data = fonts::FontData::new( bold_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaBold), ) .expect("font data should be correct"); - let italic_bytes = include_bytes!("/usr/share/fonts/liberation/LiberationSans-Italic.ttf"); + let italic_bytes = include_bytes!("/usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf"); let italic_font_data = fonts::FontData::new( italic_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaOblique), @@ -264,7 +264,7 @@ impl VCardData { .expect("font data should be correct"); let bold_italic_bytes = - include_bytes!("/usr/share/fonts/liberation/LiberationSans-BoldItalic.ttf"); + include_bytes!("/usr/share/fonts/truetype/liberation/LiberationSans-BoldItalic.ttf"); let bold_italic_font_data = fonts::FontData::new( bold_italic_bytes.to_vec(), Some(printpdf::BuiltinFont::HelveticaBoldOblique), @@ -294,52 +294,7 @@ impl VCardData { .aligned(elements::Alignment::Center) .styled(style::Style::new().bold().with_font_size(20)), ); - - let mut card_data = elements::Paragraph::new(""); - - let vcard_data = self.clone(); - - if let Some(name) = vcard_data.names.get(0) { - card_data.push_styled( - name.generate_fn(), - style::Effect::Bold - ); - } - card_data.push( - "" - ); - if let Some(communication) = vcard_data.communications.get(0) { - card_data.push( - communication.email_address.clone() - ); - } - card_data.push( - "" - ); - if let Some(address) = vcard_data.addresses.get(0) { - card_data.push( - address.street.clone() - ); - card_data.push( - format!("{} {} {}", address.code, address.locality, address.extension) - ); - card_data.push( - format!("{} {}", address.region, address.country) - ); - } - card_data.push( - "" - ); - for telephone in vcard_data.telephones { - card_data.push( - telephone.number - ); - } - - let framed_card_data = elements::FramedElement::new( - card_data - ); - + let mut table = elements::TableLayout::new(vec![1,1]); table.set_cell_decorator( elements::FrameCellDecorator::new(false, false, false) @@ -347,8 +302,8 @@ impl VCardData { for _i in 0..4 { table .row() - .element(framed_card_data.clone()) - .element(framed_card_data.clone()) + .element(self.create_card_element()) + .element(self.create_card_element()) .push() .expect("invalid table row"); } @@ -366,4 +321,32 @@ impl VCardData { Err(_) => Err(()), } } + + fn create_card_element(&self) -> impl genpdf::Element { + let mut layout = elements::LinearLayout::vertical(); + + if let Some(name) = self.names.get(0) { + layout.push(elements::Text::new(style::StyledString::new( + name.generate_fn(), + style::Effect::Bold + ))); + } + if let Some(communication) = self.communications.get(0) { + layout.push(elements::Text::new(&communication.email_address)); + } + if let Some(address) = self.addresses.get(0) { + layout.push(elements::Text::new(&address.street)); + layout.push(elements::Text::new( + format!("{} {} {}", address.code, address.locality, address.extension) + )); + layout.push(elements::Text::new( + format!("{} {}", address.region, address.country) + )); + } + for telephone in &self.telephones { + layout.push(elements::Text::new(&telephone.number)); + } + + elements::FramedElement::new(layout) + } } |