use std::borrow::Cow;
use crate::model::{
document::{DocInfo, Document},
style::{CharShape, Font},
};
pub(crate) fn missing_char_table(document: &Document) -> bool {
document.layout_profile().hwpx_container() && document.doc_info.char_shapes.is_empty()
}
pub(crate) fn missing_latin_default(document: &Document) -> Option<&'static str> {
(document.layout_profile().hwpx_container()
&& document
.doc_info
.font_faces
.get(1)
.is_some_and(Vec::is_empty)
&& document
.doc_info
.char_shapes
.iter()
.any(|shape| shape.font_ids[1] == 0))
.then_some("한컴바탕")
}
pub(super) fn lookup_context(document: &Document) -> Cow<'_, DocInfo> {
match try_lookup_context(document, &super::storage::Unbounded) {
Ok(lookup) => lookup,
Err(never) => match never {},
}
}
pub(super) fn try_lookup_context<'a, S: super::storage::Storage>(
document: &'a Document,
storage: &S,
) -> Result<Cow<'a, DocInfo>, S::Error> {
if missing_char_table(document) {
let mut info = storage.clone_doc_info(&document.doc_info)?;
storage.push(
&mut info.char_shapes,
CharShape {
base_size: 1000,
..Default::default()
},
)?;
let extra = 7usize.saturating_sub(info.font_faces.len());
storage.reserve(&mut info.font_faces, extra)?;
info.font_faces.resize_with(7, Vec::new);
for fonts in &mut info.font_faces {
if fonts.is_empty() {
storage.push(
fonts,
Font {
name: storage.string("굴림")?,
alt_type: 1,
default_name: Some(storage.string("Gulim")?),
..Default::default()
},
)?;
}
}
return Ok(Cow::Owned(info));
}
let Some(name) = missing_latin_default(document) else {
return Ok(Cow::Borrowed(&document.doc_info));
};
let mut info = storage.clone_doc_info(&document.doc_info)?;
storage.reserve(&mut info.font_faces[1], 1)?;
info.font_faces[1].push(Font {
name: storage.string(name)?,
alt_type: 1,
default_name: Some(storage.string("Haansoft Batang")?),
..Default::default()
});
Ok(Cow::Owned(info))
}