use super::super::*;
pub(crate) fn span_boundary_needs_join_control(left: &str, right: &str) -> bool {
if left
.chars()
.next_back()
.is_some_and(character_is_join_control)
|| right.chars().next().is_some_and(character_is_join_control)
{
return false;
}
let Some(left) = left
.chars()
.rev()
.find(|character| !character_is_join_control(*character))
else {
return false;
};
let Some(right) = right
.chars()
.find(|character| !character_is_join_control(*character))
else {
return false;
};
cursive_boundary_needs_context(left, right)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::text) enum ShapingContextKind {
AuthoredJoinControl,
VirtualBoundaryContext,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::text) struct ShapingContextRange {
range: Range<usize>,
kind: ShapingContextKind,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(in crate::text) struct ShapingContextMap {
ranges: Vec<ShapingContextRange>,
}
impl ShapingContextMap {
#[cfg(test)]
pub(in crate::text) fn from_synthetic_ranges(ranges: &[Range<usize>]) -> Self {
Self {
ranges: ranges
.iter()
.cloned()
.map(|range| ShapingContextRange {
range,
kind: ShapingContextKind::VirtualBoundaryContext,
})
.collect(),
}
}
fn push(&mut self, range: Range<usize>, kind: ShapingContextKind) {
self.ranges.push(ShapingContextRange { range, kind });
}
pub(in crate::text) fn add_authored_join_controls(&mut self, text: &str) {
for (start, character) in text.char_indices() {
let end = start + character.len_utf8();
if character_is_join_control(character)
&& !self.is_synthetic_at(start)
&& !self
.ranges
.iter()
.any(|context| context.range.start == start && context.range.end == end)
{
self.push(start..end, ShapingContextKind::AuthoredJoinControl);
}
}
}
pub(in crate::text) fn is_synthetic_at(&self, index: usize) -> bool {
self.ranges.iter().any(|context| {
context.range.contains(&index)
&& matches!(context.kind, ShapingContextKind::VirtualBoundaryContext)
})
}
}
pub(in crate::text) fn push_synthetic_join_control(
text: &mut String,
contexts: &mut ShapingContextMap,
) {
let start = text.len();
text.push('\u{200d}');
contexts.push(
start..text.len(),
ShapingContextKind::VirtualBoundaryContext,
);
}
pub(in crate::text) fn text_without_synthetic_join_controls(
text: &str,
range: Range<usize>,
contexts: &ShapingContextMap,
) -> String {
let Some(slice) = text.get(range.clone()) else {
return String::new();
};
slice
.char_indices()
.filter_map(|(offset, character)| {
let index = range.start + offset;
(!contexts.is_synthetic_at(index)).then_some(character)
})
.collect()
}
pub(in crate::text) fn glyphs_without_synthetic_join_controls(
glyphs: Vec<RenderedGlyph>,
raw_text: &str,
run_start: usize,
contexts: &ShapingContextMap,
) -> Vec<RenderedGlyph> {
let mut glyphs = glyphs.into_iter();
let mut output = raw_text
.char_indices()
.filter_map(|(offset, character)| {
let mut glyph = glyphs.next()?;
let index = run_start + offset;
if contexts.is_synthetic_at(index) || character_is_join_control(character) {
None
} else {
glyph.unicode = character.to_string();
Some(glyph)
}
})
.collect::<Vec<_>>();
output.extend(glyphs);
output
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::text) struct ShaperInlineAdvance(f32);
impl ShaperInlineAdvance {
pub(in crate::text) fn from_parley(value: f32) -> Self {
Self(value)
}
pub(in crate::text) fn points(self) -> f32 {
self.0
}
pub(in crate::text) fn is_zero(self) -> bool {
self.0 == 0.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::text) struct ShaperVisualInlinePosition(f32);
impl ShaperVisualInlinePosition {
pub(in crate::text) fn from_parley(value: f32) -> Self {
Self(value)
}
pub(in crate::text) fn points(self) -> f32 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::text) struct LayoutInlineAdvance(f32);
impl LayoutInlineAdvance {
pub(in crate::text) fn zero() -> Self {
Self(0.0)
}
pub(in crate::text) fn points(self) -> f32 {
self.0
}
}
#[derive(Debug, Clone, PartialEq)]
pub(in crate::text) struct SourceOnlyRun {
pub(in crate::text) visual_position: ShaperVisualInlinePosition,
pub(in crate::text) shaper_advance: ShaperInlineAdvance,
pub(in crate::text) text: Rc<str>,
}
impl SourceOnlyRun {
pub(in crate::text) fn layout_inline_advance(&self) -> LayoutInlineAdvance {
LayoutInlineAdvance::zero()
}
}
pub(in crate::text) fn text_is_join_control_only(text: &str) -> bool {
!text.is_empty() && text.chars().all(character_is_join_control)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(in crate::text) enum ControlFallbackCluster {
DropControlOnly,
RehomeSimpleVisibleFragment { character: char },
PreserveParleyGlyphs,
}
#[derive(Debug, Clone)]
pub(in crate::text) struct ControlFallbackRehomeRequest {
pub(in crate::text) character: char,
pub(in crate::text) fallback_font_id: usize,
pub(in crate::text) text: Rc<str>,
pub(in crate::text) font_size: f32,
pub(in crate::text) visual_position: ShaperVisualInlinePosition,
pub(in crate::text) shaper_advance: ShaperInlineAdvance,
pub(in crate::text) source_range: Option<Range<usize>>,
}
pub(in crate::text) fn classify_control_fallback_cluster(
text: &str,
has_positioned_glyph: bool,
) -> ControlFallbackCluster {
if text_is_join_control_only(text) {
return ControlFallbackCluster::DropControlOnly;
}
if has_positioned_glyph || !text.chars().any(character_is_join_control) {
return ControlFallbackCluster::PreserveParleyGlyphs;
}
let mut visible = text
.chars()
.filter(|character| !character_is_default_ignorable_code_point(*character));
let Some(character) = visible.next() else {
return ControlFallbackCluster::PreserveParleyGlyphs;
};
if visible.next().is_none() && character_has_simple_shaping(character) {
ControlFallbackCluster::RehomeSimpleVisibleFragment { character }
} else {
ControlFallbackCluster::PreserveParleyGlyphs
}
}
fn character_has_simple_shaping(character: char) -> bool {
if character_has_cursive_shaping_behavior(character) {
return false;
}
let script = CodePointMapData::<IcuScript>::new().get(character);
let Some(script) = PropertyNamesShort::<IcuScript>::new().get_locale_script(script) else {
return false;
};
let tag = script.into_raw();
tag == *b"Latn" || tag == *b"Grek" || tag == *b"Cyrl" || tag == *b"Armn" || tag == *b"Geor"
}
pub(in crate::text) fn corrected_visual_inline_position(
position: ShaperVisualInlinePosition,
dropped_runs: &[SourceOnlyRun],
) -> ShaperVisualInlinePosition {
debug_assert!(
dropped_runs
.iter()
.all(|run| run.layout_inline_advance().points() == 0.0)
);
ShaperVisualInlinePosition(
position.0
- dropped_runs
.iter()
.filter(|dropped| dropped.visual_position < position)
.map(|dropped| dropped.shaper_advance.points())
.sum::<f32>(),
)
}
pub(in crate::text) fn stitch_rehomed_control_fallback_run(
runs: &mut Vec<ShapedGlyphRun>,
index: usize,
) -> bool {
let Some(next_index) = index.checked_add(1).filter(|index| *index < runs.len()) else {
return false;
};
let previous = &runs[index];
let next = &runs[next_index];
if next.x_offset < previous.x_offset
|| previous.font_id != next.font_id
|| previous.font_size != next.font_size
|| previous.font_palette != next.font_palette
{
return false;
}
let next = runs.remove(next_index);
let previous = &mut runs[index];
previous.text = format!("{}{}", previous.text, next.text).into();
previous.glyphs.extend(next.glyphs);
previous
.glyph_source_ranges
.extend(next.glyph_source_ranges);
true
}
pub(in crate::text) fn stitch_dropped_join_control_runs(
runs: &mut Vec<ShapedGlyphRun>,
dropped_runs: &[SourceOnlyRun],
) {
for dropped in dropped_runs {
if !dropped.text.chars().any(character_is_join_control) {
continue;
}
let Some(previous_index) = runs
.iter()
.enumerate()
.filter(|(_, run)| run.x_offset <= dropped.visual_position.points())
.map(|(index, _)| index)
.next_back()
else {
continue;
};
let next_index = (previous_index + 1..runs.len())
.find(|&index| runs[index].x_offset >= dropped.visual_position.points());
let mut combined_text = String::from(runs[previous_index].text.as_ref());
combined_text.push_str(&dropped.text);
let can_merge_next = next_index.is_some_and(|index| {
let previous = &runs[previous_index];
let next = &runs[index];
previous.font_id == next.font_id
&& previous.font_size == next.font_size
&& previous.font_palette == next.font_palette
&& previous.y_offset == next.y_offset
&& previous.text_matrix == next.text_matrix
});
if can_merge_next {
let next = runs.remove(next_index.expect("checked above"));
combined_text.push_str(&next.text);
let previous = &mut runs[previous_index];
previous.text = combined_text.into();
previous.glyphs.extend(next.glyphs);
previous
.glyph_source_ranges
.extend(next.glyph_source_ranges);
} else {
runs[previous_index].text = combined_text.into();
}
}
}
pub(in crate::text) fn apply_synthetic_position_fallback(
glyphs: &mut [RenderedGlyph],
font_size: &mut f32,
style: &ComputedStyle,
face: &ttf_parser::Face<'_>,
text: &str,
) {
if !style.font_synthesis.position {
return;
}
let (scale, shift) = match style.font_variant_position {
FontVariantPosition::Sub => (0.65, -*font_size * 0.2),
FontVariantPosition::Super => (0.65, *font_size * 0.35),
FontVariantPosition::Normal => return,
};
if opentype_position_feature_substituted(glyphs, face, text) {
return;
}
*font_size *= scale;
for glyph in glyphs {
glyph.x_advance *= scale;
glyph.nominal_x_advance *= scale;
glyph.x_offset *= scale;
glyph.y_offset = glyph.y_offset * scale + shift;
}
}
pub(in crate::text) fn opentype_position_feature_substituted(
glyphs: &[RenderedGlyph],
face: &ttf_parser::Face<'_>,
text: &str,
) -> bool {
let mut visible_glyphs = glyphs
.iter()
.filter(|glyph| !glyph.unicode.is_empty())
.filter(|glyph| {
glyph
.unicode
.chars()
.any(|character| !character_is_default_ignorable_code_point(character))
});
let mut visible_characters = text
.chars()
.filter(|character| !character_is_default_ignorable_code_point(*character));
let mut has_visible_character = false;
loop {
match (visible_characters.next(), visible_glyphs.next()) {
(Some(character), Some(glyph)) => {
has_visible_character = true;
let Some(nominal) = face.glyph_index(character) else {
return false;
};
if Some(nominal.0) == glyph.painted_id() {
return false;
}
}
(None, None) => return has_visible_character,
_ => return false,
}
}
}
pub(in crate::text) fn cluster_is_default_ignorable_only(
raw_text: &str,
emitted_text: &str,
) -> bool {
!raw_text.is_empty()
&& raw_text
.chars()
.all(character_is_default_ignorable_code_point)
&& (emitted_text.is_empty()
|| emitted_text
.chars()
.all(character_is_default_ignorable_code_point))
}
pub(in crate::text) fn glyph_is_non_painting_shaping_artifact(
face: &ttf_parser::Face<'_>,
glyph_id: u16,
used_advance: f32,
unicode: &str,
) -> bool {
unicode.is_empty()
&& used_advance == 0.0
&& face
.glyph_hor_advance(ttf_parser::GlyphId(glyph_id))
.is_some_and(|advance| advance != 0)
}
pub(in crate::text) fn glyph_is_join_control_artifact(
face: &ttf_parser::Face<'_>,
glyph_id: u16,
unicode: &str,
cluster_text: &str,
) -> bool {
unicode.is_empty()
&& cluster_text.chars().any(character_is_join_control)
&& (glyph_id == 0
|| ['\u{200c}', '\u{200d}'].into_iter().any(|character| {
face.glyph_index(character)
.is_some_and(|nominal| nominal.0 == glyph_id)
}))
}
pub(in crate::text) fn default_ignorable_cluster_has_shaping_glyph(
face: &ttf_parser::Face<'_>,
cluster_text: &str,
emitted_cluster_text: &str,
glyphs: impl IntoIterator<Item = (u16, f32)>,
) -> bool {
if !cluster_text.chars().any(character_is_join_control) {
return cluster_text
.chars()
.any(|character| !character_is_default_ignorable_code_point(character))
&& glyphs.into_iter().any(|(glyph_id, advance)| {
advance != 0.0
&& !emitted_cluster_text.chars().any(|character| {
face.glyph_index(character)
.is_some_and(|nominal| nominal.0 == glyph_id)
})
});
}
let nominal_cluster_glyphs = cluster_text
.chars()
.filter_map(|character| face.glyph_index(character).map(|glyph| glyph.0))
.collect::<Vec<_>>();
if cluster_text
.chars()
.any(|character| !character_is_default_ignorable_code_point(character))
{
return glyphs.into_iter().any(|(glyph_id, advance)| {
advance != 0.0
&& !emitted_cluster_text.chars().any(|character| {
face.glyph_index(character)
.is_some_and(|nominal| nominal.0 == glyph_id)
})
});
}
glyphs.into_iter().any(|(glyph_id, advance)| {
advance != 0.0 && glyph_id != 0 && !nominal_cluster_glyphs.contains(&glyph_id)
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::paint::text::RenderedTextMatrix;
#[test]
fn join_control_only_text_is_distinguished_from_other_ignorables() {
assert!(text_is_join_control_only("\u{200c}\u{200d}"));
assert!(!text_is_join_control_only("f\u{200c}i"));
assert!(!text_is_join_control_only("\u{fe0f}"));
assert!(!text_is_join_control_only("\u{00ad}"));
assert!(!text_is_join_control_only(""));
}
#[test]
fn control_fallback_cluster_classification_preserves_ambiguous_shaping() {
assert_eq!(
classify_control_fallback_cluster("\u{200c}\u{200d}", false),
ControlFallbackCluster::DropControlOnly,
);
assert_eq!(
classify_control_fallback_cluster("f\u{200c}", false),
ControlFallbackCluster::RehomeSimpleVisibleFragment { character: 'f' },
);
assert_eq!(
classify_control_fallback_cluster("f\u{200c}", true),
ControlFallbackCluster::PreserveParleyGlyphs,
);
assert_eq!(
classify_control_fallback_cluster("fi\u{200c}", false),
ControlFallbackCluster::PreserveParleyGlyphs,
);
assert_eq!(
classify_control_fallback_cluster("\u{0627}\u{200d}", false),
ControlFallbackCluster::PreserveParleyGlyphs,
);
}
#[test]
fn dropped_default_ignorable_run_shifts_later_visual_origins() {
let dropped_runs = [SourceOnlyRun {
visual_position: ShaperVisualInlinePosition::from_parley(10.0),
shaper_advance: ShaperInlineAdvance::from_parley(3.0),
text: Rc::from(""),
}];
assert_eq!(
corrected_visual_inline_position(
ShaperVisualInlinePosition::from_parley(5.0),
&dropped_runs,
)
.points(),
5.0
);
assert_eq!(
corrected_visual_inline_position(
ShaperVisualInlinePosition::from_parley(10.0),
&dropped_runs,
)
.points(),
10.0
);
assert_eq!(
corrected_visual_inline_position(
ShaperVisualInlinePosition::from_parley(16.0),
&dropped_runs,
)
.points(),
13.0
);
}
#[test]
fn dropped_default_ignorable_correction_is_independent_of_logical_run_order() {
let dropped_runs = [
SourceOnlyRun {
visual_position: ShaperVisualInlinePosition::from_parley(18.0),
shaper_advance: ShaperInlineAdvance::from_parley(2.0),
text: Rc::from(""),
},
SourceOnlyRun {
visual_position: ShaperVisualInlinePosition::from_parley(6.0),
shaper_advance: ShaperInlineAdvance::from_parley(1.5),
text: Rc::from(""),
},
];
assert_eq!(
corrected_visual_inline_position(
ShaperVisualInlinePosition::from_parley(12.0),
&dropped_runs,
)
.points(),
10.5
);
assert_eq!(
corrected_visual_inline_position(
ShaperVisualInlinePosition::from_parley(24.0),
&dropped_runs,
)
.points(),
20.5
);
}
#[test]
fn rehomed_control_fragment_stitches_only_following_compatible_visual_runs() {
let glyph = |unicode: &str| RenderedGlyph {
kind: RenderedGlyphKind::Paint(1),
x_advance: 1.0,
nominal_x_advance: 1.0,
x_offset: 0.0,
y_offset: 0.0,
unicode: unicode.to_string(),
};
let run = |text: &str, x_offset: f32| ShapedGlyphRun {
text: text.into(),
x_offset,
y_offset: 0.0,
text_matrix: RenderedTextMatrix::IDENTITY,
font_size: 12.0,
font_id: Some(1),
font_palette: FontPalette::Normal,
glyphs: vec![glyph(text)],
glyph_source_ranges: vec![None],
};
let mut runs = vec![run("f\u{200c}", 2.0), run("i", 3.0)];
assert!(stitch_rehomed_control_fallback_run(&mut runs, 0));
assert_eq!(runs.len(), 1);
assert_eq!(runs[0].text.as_ref(), "f\u{200c}i");
let mut reversed_runs = vec![run("f\u{200c}", 3.0), run("i", 2.0)];
assert!(!stitch_rehomed_control_fallback_run(&mut reversed_runs, 0));
}
}