use std::{ops::Range, sync::Arc};
use crate::{
glyph_id::GlyphId,
index_range::widen,
shaped_glyphs::{BreakBefore, GlyphOffset, ShapedGlyph, ShapedGlyphs},
};
#[derive(Clone, Debug, Default)]
pub(crate) struct LaidGlyphs {
glyphs: Glyphs,
offsets: Option<Arc<[GlyphOffset]>>,
}
impl PartialEq for LaidGlyphs {
fn eq(&self, other: &Self) -> bool {
self.all() == other.all() && self.offsets == other.offsets
}
}
#[derive(Clone, Debug)]
enum Glyphs {
Packed { room: Arc<[PackedGlyph]>, len: u32 },
Full(Arc<[ShapedGlyph]>),
}
impl Default for Glyphs {
fn default() -> Self {
Self::Packed {
room: Arc::default(),
len: 0,
}
}
}
fn takes_room(room: usize, count: usize) -> bool {
count <= room && room <= count.saturating_mul(2)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PackedGlyph {
id: u16,
advance: i16,
cluster: u32,
}
impl PackedGlyph {
pub(crate) fn id(self) -> GlyphId {
GlyphId(u32::from(self.id))
}
fn text_offset(self) -> usize {
usize::try_from(self.cluster).unwrap_or(usize::MAX)
}
fn fits(glyph: &ShapedGlyph) -> bool {
let advance = glyph.advance.cast_unsigned();
(glyph.id().0 >> 16 == 0) & (advance.wrapping_add(1 << 14) >> 15 == 0)
}
fn pack(glyph: &ShapedGlyph) -> Self {
let advance = glyph.advance.cast_unsigned();
Self {
id: (glyph.id().0 & 0xFFFF) as u16,
advance: (((advance & 0x7FFF) as u16) << 1).cast_signed()
| i16::from(glyph.unsafe_to_break()),
cluster: glyph.cluster,
}
}
fn unpack(self) -> ShapedGlyph {
let before = if self.advance & 1 == 0 {
BreakBefore::Safe
} else {
BreakBefore::Unsafe
};
ShapedGlyph::new(
GlyphId(u32::from(self.id)),
self.cluster,
i32::from(self.advance >> 1),
before,
)
}
}
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum RunGlyphs<'a> {
Packed(&'a [PackedGlyph]),
Full(&'a [ShapedGlyph]),
}
impl<'a> RunGlyphs<'a> {
pub(crate) fn len(self) -> usize {
match self {
Self::Packed(glyphs) => glyphs.len(),
Self::Full(glyphs) => glyphs.len(),
}
}
pub(crate) fn get(self, index: usize) -> Option<ShapedGlyph> {
match self {
Self::Packed(glyphs) => glyphs.get(index).map(|glyph| glyph.unpack()),
Self::Full(glyphs) => glyphs.get(index).copied(),
}
}
pub(crate) fn iter(self) -> impl Iterator<Item = ShapedGlyph> + 'a {
(0..self.len()).filter_map(move |index| self.get(index))
}
fn slice(self, range: Range<usize>) -> Self {
match self {
Self::Packed(glyphs) => Self::Packed(&glyphs[range]),
Self::Full(glyphs) => Self::Full(&glyphs[range]),
}
}
}
impl LaidGlyphs {
pub(crate) fn new(shaped: &ShapedGlyphs) -> Self {
let offsets = shaped.offsets();
let mut fits = true;
let room: Arc<[PackedGlyph]> = shaped
.iter()
.map(|glyph| {
fits &= PackedGlyph::fits(glyph);
PackedGlyph::pack(glyph)
})
.collect();
let glyphs = match u32::try_from(shaped.len()) {
Ok(len) if fits => Glyphs::Packed { room, len },
_ => Glyphs::Full(Arc::from(&shaped[..])),
};
Self {
glyphs,
offsets: offsets
.iter()
.any(|offset| *offset != GlyphOffset::default())
.then(|| Arc::from(offsets)),
}
}
pub(crate) fn new_in(shaped: &ShapedGlyphs, spare: Self) -> Self {
if let Glyphs::Packed { mut room, .. } = spare.glyphs
&& takes_room(room.len(), shaped.len())
&& let Ok(len) = u32::try_from(shaped.len())
&& shaped
.offsets()
.iter()
.all(|offset| *offset == GlyphOffset::default())
&& let Some(slots) = Arc::get_mut(&mut room)
{
let mut fits = true;
for (slot, glyph) in slots[..shaped.len()].iter_mut().zip(shaped.iter()) {
fits &= PackedGlyph::fits(glyph);
*slot = PackedGlyph::pack(glyph);
}
if fits {
return Self {
glyphs: Glyphs::Packed { room, len },
offsets: None,
};
}
}
Self::new(shaped)
}
pub(crate) fn len(&self) -> usize {
self.all().len()
}
pub(crate) fn run(&self, range: Range<usize>) -> RunGlyphs<'_> {
self.all().slice(range)
}
fn all(&self) -> RunGlyphs<'_> {
match &self.glyphs {
Glyphs::Packed { room, len } => RunGlyphs::Packed(&room[..widen(*len)]),
Glyphs::Full(glyphs) => RunGlyphs::Full(glyphs),
}
}
pub(crate) fn has_offsets(&self) -> bool {
self.offsets.is_some()
}
pub(crate) fn offsets(&self, range: Range<usize>) -> Option<&[GlyphOffset]> {
self.offsets.as_deref().map(|offsets| {
let len = offsets.len();
&offsets[range.start.min(len)..range.end.min(len)]
})
}
pub(crate) fn take(
&self,
range: Range<usize>,
text: &Range<usize>,
out: &mut ShapedGlyphs,
) -> usize {
let start = range.start;
let taken = match self.run(range) {
RunGlyphs::Packed(glyphs) => glyphs
.iter()
.take_while(|glyph| text.contains(&glyph.text_offset()))
.count(),
RunGlyphs::Full(glyphs) => glyphs
.iter()
.take_while(|glyph| text.contains(&glyph.text_offset()))
.count(),
};
let end = start + taken;
let offsets = self.offsets(start..end).unwrap_or_default();
match self.run(start..end) {
RunGlyphs::Packed(glyphs) => {
out.extend(glyphs.iter().map(|glyph| glyph.unpack()), offsets);
}
RunGlyphs::Full(glyphs) => out.extend_from_slice(glyphs, offsets),
}
end
}
}