gpui/text_system/composed_family.rs
1//! vue-native fork: font families made of several faces, each of which draws
2//! only the characters of its own `unicode-range`.
3//!
4//! A web font is often shipped as one CSS family cut into subsets (Fontsource
5//! and Google Fonts: `latin`, `latin-ext`, `cyrillic`, …), one file and one
6//! `@font-face` rule each. A browser draws every character with the face of
7//! the family whose `unicode-range` covers it (CSS Fonts 4 §4.5 and §5.3), and
8//! a character no face covers with the next family in the list. The platform
9//! text system matches faces by family name, weight and style only, so each
10//! face is registered under a family of its own and the family the page names
11//! is a [`FamilyComposer`]: which faces a run of that family is drawn with, for
12//! the run's weight and style, and which characters each one draws. The
13//! platform splits the run by character before shaping.
14
15use std::sync::Arc;
16
17use crate::{FontStyle, FontWeight, SharedString};
18
19/// One face of a composed family: the family it is registered under (a family
20/// of its own, holding that one font file) and the characters it draws.
21#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22pub struct ComposedFace {
23 /// The family the face's file was registered under.
24 pub family: SharedString,
25 /// Inclusive code point ranges; empty means every code point.
26 pub ranges: Arc<[(u32, u32)]>,
27}
28
29impl ComposedFace {
30 /// Does the face draw `c`?
31 pub fn covers(&self, c: char) -> bool {
32 let c = c as u32;
33 self.ranges.is_empty() || self.ranges.iter().any(|&(start, end)| start <= c && c <= end)
34 }
35}
36
37/// The faces a composed family draws text of a weight and style with, in the
38/// order they are tried for a character: the first that covers it draws it.
39/// Empty when the family has no face for that text (yet).
40pub type FamilyComposer = Arc<dyn Fn(FontWeight, FontStyle) -> Vec<ComposedFace> + Send + Sync>;
41
42/// Is `c` a character that belongs to the cluster before it, so that it is
43/// drawn with the same face as that character rather than chosen on its own?
44/// Combining marks, joiners and variation selectors: a browser keeps a
45/// grapheme in one font when it can.
46pub fn continues_cluster(c: char) -> bool {
47 matches!(
48 c as u32,
49 0x0300..=0x036F
50 | 0x0483..=0x0489
51 | 0x1AB0..=0x1AFF
52 | 0x1DC0..=0x1DFF
53 | 0x200C..=0x200D
54 | 0x20D0..=0x20FF
55 | 0xFE00..=0xFE0F
56 | 0xFE20..=0xFE2F
57 | 0xE0100..=0xE01EF
58 )
59}
60
61/// Splits `text` into the pieces each face of `faces` draws: `(byte length,
62/// index into faces)`, `None` for a piece no face covers. Adjacent characters
63/// with the same answer are one piece; a [`continues_cluster`] character
64/// stays with the piece before it.
65pub fn split_by_face(text: &str, faces: &[ComposedFace]) -> Vec<(usize, Option<usize>)> {
66 let mut out: Vec<(usize, Option<usize>)> = Vec::new();
67 for c in text.chars() {
68 let len = c.len_utf8();
69 let face = match out.last() {
70 Some(&(_, prev)) if continues_cluster(c) => prev,
71 _ => faces.iter().position(|f| f.covers(c)),
72 };
73 match out.last_mut() {
74 Some(last) if last.1 == face => last.0 += len,
75 _ => out.push((len, face)),
76 }
77 }
78 out
79}
80
81// Tested in gpui_windows (`direct_write::composed_tests`), whose tests run in
82// vue-native's workspace; this crate's need dev-dependencies it does not have.