use alloc::vec::Vec;
use core::ops::Range;
use parley_engine::shape::Whitespace;
use parley_engine::{Atom, ShapedSlice};
pub(crate) fn is_word_separator(whitespace: Whitespace) -> bool {
whitespace.is_space_or_nbsp()
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) struct Gaps {
pub(crate) before: f32,
pub(crate) after: f32,
}
impl Gaps {
pub(crate) const ZERO: Self = Self {
before: 0.,
after: 0.,
};
#[inline(always)]
pub(crate) const fn total(self) -> f32 {
self.before + self.after
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) struct Spacing {
pub(crate) word: f32,
pub(crate) letter: f32,
}
impl Spacing {
#[inline(always)]
pub(crate) const fn new(word: f32, letter: f32, script: parlance::Script) -> Self {
if script.is_cursive() {
Self {
word: word + letter,
letter: 0.,
}
} else {
Self { word, letter }
}
}
#[inline(always)]
pub(crate) const fn is_zero(self) -> bool {
self.word == 0. && self.letter == 0.
}
#[inline(always)]
pub(crate) fn gaps(self, whitespace: Whitespace, is_default_ignorable: bool) -> Gaps {
if whitespace == Whitespace::Newline {
return Gaps::ZERO;
}
let mut gaps = Gaps {
before: 0.,
after: if is_default_ignorable {
0.
} else {
self.letter
},
};
if is_word_separator(whitespace) {
gaps.after += self.word;
}
gaps
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub(crate) struct Justification {
pub(crate) amount_per_opportunity: f32,
pub(crate) justification_end_char: u32,
}
impl Justification {
pub(crate) const NONE: Self = Self {
amount_per_opportunity: 0.,
justification_end_char: u32::MAX,
};
}
impl Default for Justification {
fn default() -> Self {
Self::NONE
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct EffectiveSpacing {
spacing: Spacing,
justification: Justification,
}
impl EffectiveSpacing {
#[inline(always)]
pub(crate) const fn new(spacing: Spacing, justification: Justification) -> Self {
Self {
spacing,
justification,
}
}
#[inline(always)]
pub(crate) const fn is_zero(self) -> bool {
self.spacing.is_zero() && self.justification.amount_per_opportunity == 0.
}
#[inline(always)]
pub(crate) fn gaps(self, atom: &Atom<'_>) -> Gaps {
let first_cluster = atom.shaped_clusters()[0];
let whitespace = first_cluster.whitespace();
let mut gaps = self
.spacing
.gaps(whitespace, first_cluster.is_default_ignorable());
if is_word_separator(whitespace)
&& atom.char_range().end <= self.justification.justification_end_char
{
gaps.after += self.justification.amount_per_opportunity;
}
gaps
}
}
pub(crate) fn push_cluster_gaps(
cluster_gaps: &mut Vec<Gaps>,
slice: ShapedSlice<'_>,
spacing: EffectiveSpacing,
is_rtl: bool,
split_off_by_l1: bool,
) {
let start = cluster_gaps.len();
cluster_gaps.resize(start + slice.shaped_clusters().len(), Gaps::ZERO);
let entries = &mut cluster_gaps[start..];
let base = slice.shaped_clusters_range().start;
for atom in slice.atoms_start().skip(usize::from(split_off_by_l1)) {
let gaps = spacing.gaps(&atom);
if gaps.before == 0. && gaps.after == 0. {
continue;
}
let clusters = atom.shaped_clusters();
let (first, last) = if is_rtl {
(clusters.len() - 1, 0)
} else {
(0, clusters.len() - 1)
};
let offset = (atom.shaped_clusters_range().start - base) as usize;
entries[offset + first].before += gaps.before;
entries[offset + last].after += gaps.after;
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct RunGaps<'a> {
entries: &'a [Gaps],
run_cluster_start: u32,
}
impl<'a> RunGaps<'a> {
#[inline]
pub(crate) fn new(entries: &'a [Gaps], run_cluster_start: u32) -> Self {
Self {
entries,
run_cluster_start,
}
}
#[inline]
pub(crate) fn for_cluster_range(self, range: Range<u32>) -> &'a [Gaps] {
&self.entries[(range.start - self.run_cluster_start) as usize
..(range.end - self.run_cluster_start) as usize]
}
pub(crate) fn for_atom(self, atom: &Atom<'_>) -> Gaps {
let mut gaps = Gaps::ZERO;
for entry in self.for_cluster_range(atom.shaped_clusters_range()) {
gaps.before += entry.before;
gaps.after += entry.after;
}
gaps
}
}