//! Text beyond Latin, for every program that shows people's words. //! //! A line of text goes through four steps before it is drawn: the //! bidirectional algorithm finds which parts run right to left ([`bidi`], //! Unicode's UAX #9), each character is given a font that has it, the //! system font first and the fallback fonts after it (Arabic, Hebrew, CJK, //! emoji: [`shape`]), Arabic letters take the form their neighbours ask //! for ([`arabic`]), and each run is shaped by its font's own rules (its //! `GSUB` and `GPOS`, read by `pinnacle_ttf::layout`). What comes out is //! glyphs with their places, left to right as they are drawn. //! //! The character data is Unicode's, made into `tables.rs` by //! `cargo xtask unicode`. #![no_std] extern crate alloc; pub mod arabic; pub mod bidi; pub mod shape; mod tables; pub use shape::{Glyph, Line, shape}; /// The scripts that change how text is shaped, or which font it wants. #[derive(Clone, Copy, PartialEq, Eq, Debug)] #[repr(u8)] pub enum Script { /// Punctuation, digits, spaces: they take their neighbours' script. Common, /// Combining marks: they take the script of what they combine with. Inherited, Latin, Greek, Cyrillic, Arabic, Hebrew, Syriac, Thaana, Nko, Han, Hiragana, Katakana, Hangul, Other, } const SCRIPTS: [Script; 14] = [ Script::Common, Script::Inherited, Script::Latin, Script::Greek, Script::Cyrillic, Script::Arabic, Script::Hebrew, Script::Syriac, Script::Thaana, Script::Nko, Script::Han, Script::Hiragana, Script::Katakana, Script::Hangul, ]; impl Script { /// The OpenType tag a font's layout tables name the script by. pub fn tag(self) -> &'static [u8; 4] { match self { Script::Latin => b"latn", Script::Greek => b"grek", Script::Cyrillic => b"cyrl", Script::Arabic => b"arab", Script::Hebrew => b"hebr", Script::Syriac => b"syrc", Script::Thaana => b"thaa", Script::Nko => b"nko ", Script::Han => b"hani", Script::Hiragana | Script::Katakana => b"kana", Script::Hangul => b"hang", _ => b"DFLT", } } /// Whether its letters join and need the font's rules to be read. pub fn complex(self) -> bool { matches!( self, Script::Arabic | Script::Hebrew | Script::Syriac | Script::Thaana | Script::Nko ) } } /// The value of the range in `table` that holds `c`. fn find(table: &[(u32, u32, u8)], c: u32) -> Option { let i = table.partition_point(|&(_, end, _)| end < c); table .get(i) .filter(|&&(start, _, _)| start <= c) .map(|&(_, _, v)| v) } pub fn script(c: char) -> Script { match find(&tables::SCRIPT, c as u32) { Some(i) => SCRIPTS.get(usize::from(i)).copied().unwrap_or(Script::Other), None => Script::Common, } } /// Shown as an emoji picture rather than a text symbol by itself. pub fn emoji_presentation(c: char) -> bool { find(&tables::EMOJI, c as u32).is_some_and(|v| v & 1 != 0) } /// A pictograph that may be shown as an emoji (with U+FE0F after it). pub fn pictographic(c: char) -> bool { find(&tables::EMOJI, c as u32).is_some_and(|v| v & 2 != 0) } /// Characters that are never drawn by themselves: joiners, direction /// marks and embeddings, variation selectors, the byte order mark, tags. pub fn default_ignorable(c: char) -> bool { matches!(c as u32, 0x00ad | 0x034f | 0x061c | 0x115f..=0x1160 | 0x17b4..=0x17b5 | 0x180b..=0x180f | 0x200b..=0x200f | 0x202a..=0x202e | 0x2060..=0x206f | 0x3164 | 0xfe00..=0xfe0f | 0xfeff | 0xffa0 | 0xfff0..=0xfff8 | 0x1bca0..=0x1bca3 | 0x1d173..=0x1d17a | 0xe0000..=0xe0fff) } /// Whether a line can be drawn as it was before this crate: every /// character left to right in one font, with no shaping. True for most of /// what the system itself says, which then costs nothing more. pub fn simple(text: &str) -> bool { text.chars().all(|c| (c as u32) < 0x0590) } #[cfg(test)] mod tests { use super::*; #[test] fn scripts_and_emoji() { assert_eq!(script('a'), Script::Latin); assert_eq!(script('ب'), Script::Arabic); assert_eq!(script('א'), Script::Hebrew); assert_eq!(script('中'), Script::Han); assert_eq!(script(' '), Script::Common); assert_eq!(script('\u{064e}'), Script::Inherited); assert!(emoji_presentation('😀')); assert!(!emoji_presentation('❤')); assert!(pictographic('❤')); assert!(default_ignorable('\u{200d}')); assert!(!default_ignorable('a')); } }