//! A line of text to glyphs: bidi levels, a font for each character, runs //! shaped by their fonts' rules, and each glyph's place, left to right as //! it is drawn. //! //! The fonts are a list: the first is the one asked for (the system's sans //! or mono), the others fall back in order for what it lacks. A font is //! asked for only when a character needs one it has not tried, so a //! program that never shows Chinese never reads the Chinese font. //! //! Latin, Greek, Cyrillic and CJK runs are set glyph by glyph from the //! character map and advances, as the system always drew them; the scripts //! whose letters join or stack (Arabic, Hebrew, Syriac, Thaana, N'Ko) and //! emoji sequences go through the font's `GSUB` and `GPOS`. use alloc::vec; use alloc::vec::Vec; use pinnacle_ttf::Font as Face; use pinnacle_ttf::layout::{Buffer, Info, Layout, Table}; use crate::arabic::{Form, forms}; use crate::bidi; use crate::{Script, default_ignorable, emoji_presentation, script}; /// A glyph placed: which font and glyph, where its origin is from the /// line's start on the baseline (pixels, y down), and which character of /// the text it came from. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Glyph { pub face: usize, pub glyph: u16, pub x: f32, pub y: f32, pub cluster: usize, } /// A line laid out. #[derive(Clone, Debug, Default)] pub struct Line { /// In the order they are drawn, left to right. pub glyphs: Vec, /// Its width in pixels. pub width: f32, /// Its paragraph runs right to left (its first strong letter does). pub rtl: bool, } const GLOBAL: u32 = 1; const ISOL: u32 = 2; const INIT: u32 = 4; const MEDI: u32 = 8; const FINA: u32 = 16; /// The variation selector asking for emoji, and the joiners. const VS16: char = '\u{fe0f}'; const ZWJ: char = '\u{200d}'; const ZWNJ: char = '\u{200c}'; /// What a run of the line is: its characters (logical, `start..end`), its /// level, font and script. #[derive(Clone, Copy, Debug)] struct Run { start: usize, end: usize, level: u8, face: usize, script: Script, } /// Fonts asked for by index, each read at most once by whoever answers. struct Fonts<'a, 'f> { get: &'a dyn Fn(usize) -> Option<&'f Face<'f>>, count: usize, tried: Vec>>>, } impl<'f> Fonts<'_, 'f> { fn face(&mut self, i: usize) -> Option<&'f Face<'f>> { if i >= self.count { return None; } if self.tried[i].is_none() { self.tried[i] = Some((self.get)(i)); } self.tried[i].flatten() } fn has(&mut self, i: usize, c: char) -> bool { self.face(i).is_some_and(|f| f.glyph_index(c) != 0) } } /// Lays out `text` (one line) at `px` pixels to the em. `faces(i)` is the /// `i`th of `count` fonts, `None` if it is not there; `emoji` says which /// of them is for emoji, taken first for what is shown as one. pub fn shape<'f>( text: &str, px: f32, faces: &dyn Fn(usize) -> Option<&'f Face<'f>>, count: usize, emoji: Option, ) -> Line { let mut fonts = Fonts { get: faces, count, tried: vec![None; count], }; let mut chars: Vec = text.chars().collect(); let n = chars.len(); if n == 0 { return Line::default(); } let (levels, removed, rtl) = if bidi::any_rtl(text) { let p = bidi::paragraph(&chars, None); (p.levels, p.removed, p.level % 2 == 1) } else { ( vec![0; n], chars.iter().map(|&c| bidi::class(c) == bidi::Class::BN).collect(), false, ) }; // L4: a bracket in right-to-left text is drawn as its mirror. for i in 0..n { if levels[i] % 2 == 1 { chars[i] = bidi::mirror(chars[i]); } } // A font for each character. let mut face_of = vec![0usize; n]; for i in 0..n { let c = chars[i]; let prev = if i > 0 { Some(face_of[i - 1]) } else { None }; let follows = matches!(script(c), Script::Inherited) || matches!(c, VS16 | ZWJ | ZWNJ) || (0x1f3fb..=0x1f3ff).contains(&(c as u32)) || removed[i] || default_ignorable(c); face_of[i] = if let Some(p) = prev.filter(|&p| follows && (fonts.has(p, c) || default_ignorable(c))) { p } else if let Some(e) = emoji.filter(|&e| { (emoji_presentation(c) || chars.get(i + 1) == Some(&VS16) || prev == Some(e) && is_emoji_part(c)) && fonts.has(e, c) }) { e } else if let Some(p) = prev.filter(|&p| script(c) == Script::Common && fonts.has(p, c)) { p } else { (0..count).find(|&k| fonts.has(k, c)).unwrap_or(prev.unwrap_or(0)) }; } // Each character's script: punctuation and marks take their // neighbours'. let mut scripts: Vec