use super::{Font, GlyphId};
pub const TEXT_PRESENTATION: char = '\u{FE0E}';
pub const EMOJI_PRESENTATION: char = '\u{FE0F}';
pub const ZERO_WIDTH_JOINER: char = '\u{200D}';
pub const MAX_FALLBACK_FACES: usize = u8::MAX as usize;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Mapped {
pub face: u8,
pub glyph: GlyphId,
}
#[must_use]
pub fn is_invisible(c: char) -> bool {
matches!(c, '\u{FE00}'..='\u{FE0F}' | ZERO_WIDTH_JOINER)
}
#[must_use]
pub fn map_char(font: &Font, c: char, next: Option<char>) -> Option<Mapped> {
if is_invisible(c) {
return None;
}
let faces = core::iter::once(font).chain(font.fallback());
let mut first = None;
for (index, face) in faces.enumerate() {
let glyph = face.glyph_id(c);
if glyph == GlyphId::NOTDEF {
continue;
}
let mapped = Mapped {
face: index as u8,
glyph,
};
let wanted = match next {
Some(EMOJI_PRESENTATION) => face.has_color_glyph(glyph),
Some(TEXT_PRESENTATION) => !face.has_color_glyph(glyph),
_ => return Some(mapped),
};
if wanted {
return Some(mapped);
}
first.get_or_insert(mapped);
}
Some(first.unwrap_or(Mapped {
face: 0,
glyph: GlyphId::NOTDEF,
}))
}
pub fn map_text<'a>(
font: &'a Font,
text: &'a str,
) -> impl Iterator<Item = (usize, char, Option<Mapped>)> + 'a {
let mut chars = text.char_indices().peekable();
core::iter::from_fn(move || {
let (byte, c) = chars.next()?;
let next = chars.peek().map(|&(_, next)| next);
Some((byte, c, map_char(font, c, next)))
})
}
#[cfg(all(test, feature = "parsed-font"))]
mod tests {
use super::*;
use crate::font::fixtures;
fn latin1() -> Font {
Font::sans().clone()
}
#[test]
fn a_char_is_drawn_by_the_first_face_that_maps_it() {
let (cbdt, colr) = (fixtures::cbdt(), fixtures::colrv0());
let font = latin1().with_fallback(&[cbdt.clone(), colr.clone()]);
assert_eq!(
map_char(&font, 'a', None),
Some(Mapped {
face: 0,
glyph: font.glyph_id('a')
})
);
let smile = map_char(&font, '\u{1F600}', None).expect("drawn");
assert_eq!(smile.face, 1);
assert_eq!(smile.glyph, cbdt.glyph_id('\u{1F600}'));
assert_eq!(font.face(smile.face), &cbdt);
let many = map_char(&font, '\u{1F601}', None).expect("drawn");
assert_eq!((many.face, many.glyph), (2, colr.glyph_id('\u{1F601}')));
assert_eq!(font.face(2), &colr);
assert_eq!(
map_char(&font, '\u{4E2D}', None),
Some(Mapped {
face: 0,
glyph: GlyphId::NOTDEF
})
);
let swapped = latin1().with_fallback(&[colr.clone(), cbdt]);
assert_eq!(
map_char(&swapped, '\u{1F600}', None).map(|m| m.face),
Some(1)
);
assert_eq!(swapped.face(1), &colr);
}
#[test]
fn presentation_selectors_choose_colour_or_text() {
let (text, cbdt) = (fixtures::text(), fixtures::cbdt());
let heart = '\u{2764}';
let font = text.with_fallback(core::slice::from_ref(&cbdt));
assert_eq!(map_char(&font, heart, None).map(|m| m.face), Some(0));
assert_eq!(
map_char(&font, heart, Some(TEXT_PRESENTATION)).map(|m| m.face),
Some(0)
);
let colour = map_char(&font, heart, Some(EMOJI_PRESENTATION)).expect("drawn");
assert_eq!(colour.face, 1);
assert!(font.face(colour.face).has_color_glyph(colour.glyph));
let font = cbdt.with_fallback(&[text]);
assert_eq!(map_char(&font, heart, None).map(|m| m.face), Some(0));
assert_eq!(
map_char(&font, heart, Some(TEXT_PRESENTATION)).map(|m| m.face),
Some(1)
);
assert_eq!(
map_char(&cbdt, heart, Some(TEXT_PRESENTATION)).map(|m| m.face),
Some(0)
);
for invisible in [EMOJI_PRESENTATION, TEXT_PRESENTATION, ZERO_WIDTH_JOINER] {
assert_eq!(map_char(&font, invisible, None), None, "{invisible:?}");
}
assert!(!is_invisible('a'));
}
#[test]
fn mapping_text_reads_the_selector_after_each_char() {
let (text, cbdt) = (fixtures::text(), fixtures::cbdt());
let font = text.with_fallback(&[cbdt]);
let mapped: Vec<_> = map_text(&font, "\u{2764}\u{FE0F}\u{2764}")
.map(|(byte, c, mapped)| (byte, c, mapped.map(|m| m.face)))
.collect();
assert_eq!(
mapped,
[
(0, '\u{2764}', Some(1)),
(3, '\u{FE0F}', None),
(6, '\u{2764}', Some(0)),
]
);
}
}