use crate::model::document::Document;
use crate::model::paragraph::{LineSeg, Paragraph};
use crate::renderer::hwpunit_to_px;
use crate::renderer::layout::{estimate_text_width_unrounded, resolved_to_text_style};
use crate::renderer::style_resolver::{detect_lang_category, ResolvedStyleSet};
const FIT_TOLERANCE_RATIO: f64 = 0.02;
const MIN_HALFWIDTH_WITNESSES: usize = 3;
const DOMINANCE: usize = 4;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub(crate) struct HftAsciiWitnesses {
pub halfwidth: usize,
pub proportional: usize,
}
impl HftAsciiWitnesses {
pub(crate) fn proves_halfwidth(self) -> bool {
self.halfwidth >= MIN_HALFWIDTH_WITNESSES
&& self.halfwidth >= DOMINANCE * self.proportional.max(1)
}
}
pub(crate) fn count_hft_ascii_witnesses(
document: &Document,
styles: &ResolvedStyleSet,
dpi: f64,
) -> HftAsciiWitnesses {
let mut witnesses = HftAsciiWitnesses::default();
if !styles
.char_styles
.iter()
.any(|cs| cs.font_families_hft_hangul.iter().any(|hft| *hft))
{
return witnesses;
}
for section in &document.sections {
for para in §ion.paragraphs {
count_paragraph(para, styles, dpi, &mut witnesses);
}
}
witnesses
}
fn stored_lines(para: &Paragraph) -> Vec<&LineSeg> {
para.line_segs
.iter()
.filter(|seg| seg.tag & LineSeg::TAG_IMPLEMENTATION_PROPERTY == 0)
.collect()
}
fn char_widths(styles: &ResolvedStyleSet, shape_id: u32, ch: char) -> (f64, f64, bool) {
let lang = detect_lang_category(ch);
let mut style = resolved_to_text_style(styles, shape_id, lang);
let hft_ascii = ch.is_ascii_graphic()
&& styles
.char_styles
.get(shape_id as usize)
.is_some_and(|cs| cs.hft_hangul_face_for_lang(lang));
let text = ch.to_string();
style.hft_hangul_face = false;
let proportional = estimate_text_width_unrounded(&text, &style);
if !hft_ascii {
return (proportional, proportional, false);
}
style.hft_hangul_face = true;
(
proportional,
estimate_text_width_unrounded(&text, &style),
true,
)
}
fn count_paragraph(
para: &Paragraph,
styles: &ResolvedStyleSet,
dpi: f64,
witnesses: &mut HftAsciiWitnesses,
) {
let lines = stored_lines(para);
if lines.len() < 2 || para.char_offsets.len() != para.text.chars().count() {
return;
}
let chars: Vec<char> = para.text.chars().collect();
if chars.iter().any(|ch| (*ch as u32) < 0x20) {
return;
}
let widths: Vec<(f64, f64, bool)> = chars
.iter()
.enumerate()
.map(|(index, ch)| {
let shape_id = para.char_shape_id_at(index).unwrap_or(0);
char_widths(styles, shape_id, *ch)
})
.collect();
let line_range = |line_index: usize| -> (usize, usize) {
let start_pos = lines[line_index].text_start;
let end_pos = lines
.get(line_index + 1)
.map(|next| next.text_start)
.unwrap_or(u32::MAX);
let start = para.char_offsets.partition_point(|pos| *pos < start_pos);
let end = para.char_offsets.partition_point(|pos| *pos < end_pos);
(start, end)
};
for line_index in 0..lines.len() - 1 {
let line = lines[line_index];
if line.segment_width <= 0 {
continue;
}
let box_width = hwpunit_to_px(line.segment_width, dpi);
let tolerance = box_width * FIT_TOLERANCE_RATIO;
let (start, end) = line_range(line_index);
if start >= end {
continue;
}
let mut ink_end = end;
while ink_end > start && chars[ink_end - 1] == ' ' {
ink_end -= 1;
}
let line_widths = &widths[start..ink_end];
if !line_widths.iter().any(|(_, _, hft)| *hft) {
continue;
}
let proportional: f64 = line_widths.iter().map(|w| w.0).sum();
let halfwidth: f64 = line_widths.iter().map(|w| w.1).sum();
if proportional > box_width + tolerance && halfwidth <= box_width - tolerance {
witnesses.halfwidth += 1;
continue;
}
let (next_start, next_end) = line_range(line_index + 1);
let word_end = (next_start..next_end)
.find(|index| chars[*index] == ' ')
.unwrap_or(next_end);
if word_end <= next_start {
continue;
}
let gap: f64 = widths[ink_end..end].iter().map(|w| w.1).sum();
let next_word = &widths[next_start..word_end];
let half_with_word = halfwidth + gap + next_word.iter().map(|w| w.1).sum::<f64>();
let prop_with_word = proportional + gap + next_word.iter().map(|w| w.0).sum::<f64>();
if half_with_word <= box_width - tolerance && prop_with_word > box_width + tolerance {
witnesses.proportional += 1;
}
}
}