use crate::model::document::{DocInfo, Document};
use crate::model::style::{CharShape, Font, ParaShape};
pub fn char_shape(doc_info: &DocInfo, id: u32) -> Option<&CharShape> {
doc_info.char_shapes.get(id as usize)
}
pub fn para_shape(doc_info: &DocInfo, id: u16) -> Option<&ParaShape> {
doc_info.para_shapes.get(id as usize)
}
pub fn bin_data_bytes(document: &Document, bin_data_id: u16) -> Option<(Vec<u8>, &str)> {
if bin_data_id == 0 {
return None;
}
document
.bin_data_content
.iter()
.find(|c| c.id == bin_data_id)
.and_then(|c| {
c.data
.load_limited(crate::model::bin_data::MAX_BIN_DATA_BYTES)
.filter(|bytes| !bytes.is_empty())
.map(|bytes| (bytes, c.extension.as_str()))
})
}
pub fn font_name(doc_info: &DocInfo, lang_idx: usize, font_id: u16) -> Option<&str> {
doc_info
.font_faces
.get(lang_idx)
.and_then(|faces: &Vec<Font>| faces.get(font_id as usize))
.map(|f| f.name.as_str())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::bin_data::{BinDataBytes, BinDataContent};
use crate::model::document::DocInfo;
use crate::model::style::{CharShape, Font, ParaShape};
fn make_doc_info_with_char_shapes(shapes: Vec<CharShape>) -> DocInfo {
DocInfo {
char_shapes: shapes,
..Default::default()
}
}
#[test]
fn char_shape_found() {
let cs = CharShape {
bold: true,
..Default::default()
};
let doc_info = make_doc_info_with_char_shapes(vec![CharShape::default(), cs]);
assert_eq!(char_shape(&doc_info, 1).map(|s| s.bold), Some(true));
}
#[test]
fn char_shape_out_of_range() {
let doc_info = make_doc_info_with_char_shapes(vec![]);
assert!(char_shape(&doc_info, 0).is_none());
}
#[test]
fn para_shape_found() {
let doc_info = DocInfo {
para_shapes: vec![ParaShape::default(), ParaShape::default()],
..Default::default()
};
assert!(para_shape(&doc_info, 1).is_some());
assert!(para_shape(&doc_info, 2).is_none());
}
#[test]
fn bin_data_bytes_found() {
let doc = Document {
bin_data_content: vec![
BinDataContent {
id: 1,
data: BinDataBytes::Loaded(vec![0xFF, 0xD8]),
extension: "jpg".to_string(),
},
BinDataContent {
id: 2,
data: BinDataBytes::Loaded(vec![0x89, 0x50]),
extension: "png".to_string(),
},
],
..Default::default()
};
let (bytes, ext) = bin_data_bytes(&doc, 2).unwrap();
assert_eq!(ext, "png");
assert_eq!(bytes, vec![0x89u8, 0x50]);
}
#[test]
fn bin_data_bytes_zero_id() {
let doc = Document::default();
assert!(bin_data_bytes(&doc, 0).is_none());
}
#[test]
fn bin_data_bytes_empty_data_skipped() {
let doc = Document {
bin_data_content: vec![BinDataContent {
id: 3,
data: BinDataBytes::Loaded(Vec::new()),
extension: "png".to_string(),
}],
..Default::default()
};
assert!(bin_data_bytes(&doc, 3).is_none());
}
#[test]
fn font_name_found() {
let font = Font {
name: "Nanum Gothic".to_string(),
..Default::default()
};
let doc_info = DocInfo {
font_faces: vec![vec![font]],
..Default::default()
};
assert_eq!(font_name(&doc_info, 0, 0), Some("Nanum Gothic"));
}
#[test]
fn font_name_out_of_range() {
let doc_info = DocInfo::default();
assert!(font_name(&doc_info, 0, 0).is_none());
}
}