use super::{ComposedLine, ComposedTextRun, ResolvedStyleSet};
use crate::renderer::style_resolver::ResolvedCharStyle;
fn space_origin(ch: char, inherited: usize, style: Option<&ResolvedCharStyle>) -> Option<usize> {
let style = style?;
(matches!(ch, ' ' | '\u{00a0}')
&& inherited != 1
&& style.font_space_em.is_none()
&& (style.font_size_for_lang(inherited) != style.font_size_for_lang(1)
|| style.ratio_for_lang(inherited) != style.ratio_for_lang(1)))
.then_some(inherited)
}
pub(super) fn apply_space_origin(
output: &mut super::TextStyle,
styles: &ResolvedStyleSet,
style_id: u32,
origin: usize,
) {
let Some(source) = styles.char_styles.get(style_id as usize) else {
return;
};
let em = output.font_size * output.ratio;
if output.font_space_em.is_none() && em > 0.0 {
output.font_space_em =
Some(0.5 * source.font_size_for_lang(origin) * source.ratio_for_lang(origin) / em);
output.inherited_half_space_em = true;
}
}
pub(super) fn character_style(
ch: char,
inherited: usize,
styles: &ResolvedStyleSet,
id: u32,
) -> super::TextStyle {
match try_character_style(
ch,
inherited,
styles,
id,
&crate::renderer::style_resolver::storage::Unbounded,
) {
Ok(style) => style,
Err(never) => match never {},
}
}
pub(super) fn try_character_style<S: crate::renderer::style_resolver::storage::Storage>(
ch: char,
inherited: usize,
styles: &ResolvedStyleSet,
style_id: u32,
storage: &S,
) -> Result<super::TextStyle, S::Error> {
let origin = space_origin(ch, inherited, styles.char_styles.get(style_id as usize));
let lang = metric_slot(ch, inherited, styles, style_id);
let mut output =
crate::renderer::layout::try_resolved_to_text_style(styles, style_id, lang, storage)?;
if let Some(origin) = origin {
apply_space_origin(&mut output, styles, style_id, origin);
}
Ok(output)
}
fn slot(
ch: char,
inherited: usize,
regenerated: bool,
style: Option<&ResolvedCharStyle>,
styles: &ResolvedStyleSet,
) -> usize {
let Some(style) = style else {
return inherited;
};
let explicit_metrics_differ = style.font_size_for_lang(inherited)
!= style.font_size_for_lang(1)
|| style.letter_spacing_for_lang(inherited) != style.letter_spacing_for_lang(1)
|| style.ratio_for_lang(inherited) != style.ratio_for_lang(1);
let supplied_latin_punctuation = inherited != 1
&& ch.is_ascii_punctuation()
&& styles
.native_pua_fonts
.get_otf(
style.font_family_for_lang(inherited),
style.bold,
style.italic,
)
.is_some_and(|font| {
if styles.source_hwp3_native {
!font.has_glyph(ch)
} else {
style.source_hft_for_lang(inherited)
&& style.source_hft_for_lang(1)
&& font.primary_cff_has_glyph(ch) == Some(false)
}
})
&& styles
.native_pua_fonts
.get_otf(style.font_family_for_lang(1), style.bold, style.italic)
.is_some_and(|font| {
if styles.source_hwp3_native {
font.has_glyph(ch)
} else {
font.primary_cff_has_glyph(ch) == Some(true)
}
});
let hanja_latin_punctuation =
!styles.source_hwp3_native && inherited == 2 && ch.is_ascii_punctuation();
if supplied_latin_punctuation || hanja_latin_punctuation {
1
} else if style.explicit_latin_slot && super::is_latin_slot_punct(ch) {
1
} else if explicit_metrics_differ
&& (matches!(ch, ' ' | '\u{00a0}') || ch.is_ascii_punctuation())
{
1
} else if !style.ascii_punct_latin_slot {
inherited
} else if ch.is_ascii_punctuation() {
1
} else if regenerated {
super::reflow_punctuation_slot(ch, inherited, true)
} else {
inherited
}
}
pub(super) fn metric_slot(
ch: char,
inherited: usize,
styles: &ResolvedStyleSet,
style_id: u32,
) -> usize {
slot(
ch,
inherited,
false,
styles.char_styles.get(style_id as usize),
styles,
)
}
pub(super) fn try_resolve_run_slots<S: crate::storage::Storage>(
lines: &mut [ComposedLine],
styles: &ResolvedStyleSet,
regenerated: bool,
storage: &S,
) -> Result<(), S::Error> {
for line in lines {
let mut resolved = storage.vec(0)?;
for run in std::mem::take(&mut line.runs) {
let style = styles.char_styles.get(run.char_style_id as usize);
if run.char_overlap.is_some()
|| !run.text.chars().any(|ch| {
slot(ch, run.lang_index, regenerated, style, styles) != run.lang_index
})
{
storage.push(&mut resolved, run)?;
continue;
}
let mut start = 0;
let mut model_start = 0;
let mut lang = run.lang_index;
let mut origin = run.neutral_space_origin;
for (scalar, (byte, ch)) in run.text.char_indices().enumerate() {
let next = slot(ch, run.lang_index, regenerated, style, styles);
let next_origin = space_origin(ch, run.lang_index, style);
if (next, next_origin) != (lang, origin) {
if byte > start {
storage.push(
&mut resolved,
ComposedTextRun {
text: storage.string(&run.text[start..byte])?,
display_text: run.try_display_for_model_range(
model_start,
scalar,
storage,
)?,
lang_index: lang,
neutral_space_origin: origin,
..run.try_clone(storage)?
},
)?;
}
start = byte;
model_start = scalar;
lang = next;
origin = next_origin;
}
}
if start < run.text.len() {
storage.push(
&mut resolved,
ComposedTextRun {
display_text: run.try_display_for_model_range(
model_start,
run.text.chars().count(),
storage,
)?,
text: storage.string(&run.text[start..])?,
lang_index: lang,
neutral_space_origin: origin,
..run
},
)?;
}
}
line.runs = resolved;
}
Ok(())
}