use super::style_resolver::ResolvedStyleSet;
use super::TextStyle;
use crate::model::document::Document;
use crate::model::style::NativeFontProgram;
pub(crate) const HUMAN_HFT_METRIC_FACE: &str = "HumanMyeongJo-HFT";
pub(crate) const HANYANG_HFT_METRIC_FACE: &str = "한양신명조 (HFT)";
const MALGUN_BOLD_DECORATION_METRIC_FACE: &str = "MalgunGothic-HancomBoldDecoration";
pub(crate) fn has_verified_hft_bold_tracking_source(face: &crate::model::style::Font) -> bool {
!face.is_embedded
&& face.alt_type == 2
&& matches!(face.name.trim(), "신명 중고딕" | "Sinmyeong Junggothic")
}
pub(crate) fn select_native_programs(document: &Document, styles: &mut ResolvedStyleSet) {
use crate::model::provenance::SourceFormat;
let source_format = document.provenance.format;
if !matches!(
source_format,
SourceFormat::Hwp3 | SourceFormat::Hwp5 | SourceFormat::Hwpx
) {
return;
}
for (shape, style) in document
.doc_info
.char_shapes
.iter()
.zip(&mut styles.char_styles)
{
for (lang, &font_id) in shape.font_ids.iter().enumerate() {
let face = document
.doc_info
.font_faces
.get(lang)
.and_then(|faces| faces.get(font_id as usize));
if source_format == SourceFormat::Hwp3
&& face.is_some_and(|face| {
face.alt_type == 0
&& !face.is_embedded
&& super::font_rule_layout_name_projection::find_font_rule_layout_name(
"rust-style-resolution.hft",
&face.name,
lang,
)
.is_some()
})
{
style.font_families_source_hft[lang] = true;
}
if let Some(face) = face.filter(|face| !face.is_embedded) {
let metric_face = match face.native_program {
Some(NativeFontProgram::HumanMyeongJo) => Some(HUMAN_HFT_METRIC_FACE),
Some(NativeFontProgram::HanyangSinMyeongJo) => Some(HANYANG_HFT_METRIC_FACE),
None if source_format == SourceFormat::Hwp3 && face.name == "휴먼명조" => {
Some(HUMAN_HFT_METRIC_FACE)
}
None => None,
};
if let Some(metric_face) = metric_face {
style.font_families_metric_face[lang] = Some(metric_face.to_string());
}
}
if matches!(source_format, SourceFormat::Hwp5 | SourceFormat::Hwpx)
&& shape.bold
&& !shape.italic
&& face.is_some_and(|face| {
!face.is_embedded
&& matches!(
face.name.as_str(),
"맑은 고딕" | "Malgun Gothic" | "MalgunGothic"
)
})
{
style.font_families_metric_face[lang] =
Some(MALGUN_BOLD_DECORATION_METRIC_FACE.to_string());
}
}
}
}
pub(crate) fn measurement_face(declared: &str) -> &str {
match declared {
HUMAN_HFT_METRIC_FACE => "HumanMyeongJo",
MALGUN_BOLD_DECORATION_METRIC_FACE => "Malgun Gothic",
_ => declared,
}
}
pub(crate) fn measurement_bold(
declared: &str,
requested_bold: bool,
regenerated_font_metrics: bool,
) -> bool {
requested_bold && !(regenerated_font_metrics && declared == MALGUN_BOLD_DECORATION_METRIC_FACE)
}
pub(crate) fn has_bold_tracking(style: &TextStyle) -> bool {
style.bold && style.metric_font_family.as_deref() == Some(HANYANG_HFT_METRIC_FACE)
}
fn fixed_ascii_face(face: Option<&str>) -> bool {
matches!(face, Some("고딕" | "명조" | "산세리프"))
}
pub(crate) fn has_fixed_ascii_bold_tracking(style: &TextStyle) -> bool {
style.bold
&& !style.italic
&& (fixed_ascii_face(
style
.original_glyph_metric_family
.as_deref()
.map(String::as_str),
) || style.ascii_punct_latin.as_deref().is_some_and(|slot| {
fixed_ascii_face(
slot.original_glyph_metric_family
.as_deref()
.map(String::as_str),
)
}))
}
pub(crate) fn bold_advance(
style: &TextStyle,
ch: char,
ordinary_advance: f64,
em_advance: f64,
original_glyph_face: Option<&str>,
) -> f64 {
if style.bold && !style.italic && fixed_ascii_face(original_glyph_face) && ch.is_ascii_graphic()
{
return em_advance * 0.05;
}
if has_bold_tracking(style) && !ch.is_whitespace() && !ch.is_control() {
return em_advance * 0.05;
}
if style.bold
&& style.metric_font_family.as_deref() == Some(HUMAN_HFT_METRIC_FACE)
&& ('\u{AC00}'..='\u{D7A3}').contains(&ch)
{
ordinary_advance * 0.05
} else {
0.0
}
}