diff --git a/rangeset/src/lib.rs b/rangeset/src/lib.rs index cac1ed987..69cf326c9 100644 --- a/rangeset/src/lib.rs +++ b/rangeset/src/lib.rs @@ -6,7 +6,7 @@ use std::ops::Range; /// Track a set of integers, collapsing adjacent integers into ranges. /// Internally stores the set in an array of ranges. /// Allows adding and subtracting ranges. -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct RangeSet { ranges: Vec>, } @@ -241,6 +241,10 @@ impl RangeSet { } } + pub fn add_range_unchecked(&mut self, range: Range) { + self.ranges.push(range); + } + /// Add a set of ranges to this set pub fn add_set(&mut self, set: &Self) { for r in set.iter() { diff --git a/wezterm-font/src/fcwrap.rs b/wezterm-font/src/fcwrap.rs index c79e86b12..dfffa93d7 100644 --- a/wezterm-font/src/fcwrap.rs +++ b/wezterm-font/src/fcwrap.rs @@ -104,6 +104,49 @@ pub struct CharSet { cset: *mut FcCharSet, } +pub struct CharSetRef<'a> { + cset: *mut FcCharSet, + phantom: std::marker::PhantomData<&'a FcCharSet>, +} + +impl<'a> CharSetRef<'a> { + pub fn to_range_set(&self) -> rangeset::RangeSet { + let mut coverage = rangeset::RangeSet::new(); + let mut next_base_code_point = FcChar32::default(); + const FC_CHARSET_MAP_SIZE: usize = 256 / 32; + const FC_CHARSET_DONE: FcChar32 = FcChar32::MAX; + let mut map = [FcChar32::default(); FC_CHARSET_MAP_SIZE]; + let mut base_code_point = + unsafe { FcCharSetFirstPage(self.cset, map.as_mut_ptr(), &mut next_base_code_point) }; + let mut range_start = FcChar32::MAX; + let mut code_point = FcChar32::MAX; + while base_code_point != FC_CHARSET_DONE { + for (i, mask) in map.iter().enumerate() { + for j in 0..32 { + if mask & (1 << j) != 0 { + let new_code_point = base_code_point + (j + i * 32) as u32; + if new_code_point - 1 > code_point { + coverage.add_range_unchecked(range_start..code_point + 1); + range_start = new_code_point; + } + if range_start == FcChar32::MAX { + range_start = new_code_point; + } + code_point = new_code_point; + } + } + } + base_code_point = unsafe { + FcCharSetNextPage(self.cset, map.as_mut_ptr(), &mut next_base_code_point) + }; + } + if range_start != FcChar32::MAX { + coverage.add_range_unchecked(range_start..code_point + 1); + } + coverage + } +} + impl Drop for CharSet { fn drop(&mut self) { unsafe { @@ -112,6 +155,15 @@ impl Drop for CharSet { } } +impl<'a> From<&'a CharSet> for CharSetRef<'a> { + fn from(c: &'a CharSet) -> Self { + Self { + cset: c.cset, + phantom: std::marker::PhantomData, + } + } +} + impl CharSet { pub fn new() -> anyhow::Result { unsafe { @@ -145,6 +197,18 @@ impl Pattern { } } + pub fn get_charset<'a>(&'a self) -> anyhow::Result> { + let mut c = ptr::null_mut(); + unsafe { + FcPatternGetCharSet(self.pat, b"charset\0".as_ptr() as *const c_char, 0, &mut c); + } + ensure!(!c.is_null(), "pattern has no charset"); + Ok(CharSetRef { + cset: c, + phantom: std::marker::PhantomData, + }) + } + pub fn add_charset(&mut self, charset: &CharSet) -> anyhow::Result<()> { unsafe { ensure!( diff --git a/wezterm-font/src/ftwrap.rs b/wezterm-font/src/ftwrap.rs index dee2639cc..5b9acd864 100644 --- a/wezterm-font/src/ftwrap.rs +++ b/wezterm-font/src/ftwrap.rs @@ -157,6 +157,7 @@ impl Face { index: self.source.index, variation: i, origin: self.source.origin, + coverage: self.source.coverage.clone(), }; res.push(ParsedFont::from_face(&self, source)?); } @@ -258,6 +259,9 @@ impl Face { } pub fn compute_coverage(&self) -> RangeSet { + if let Some(coverage) = self.source.coverage.as_ref() { + return coverage.clone(); + } let mut coverage = RangeSet::new(); for encoding in &[ @@ -270,10 +274,22 @@ impl Face { let mut glyph = 0; let mut ucs4 = unsafe { FT_Get_First_Char(self.face, &mut glyph) }; - while glyph != 0 { - coverage.add(ucs4 as u32); - ucs4 = unsafe { FT_Get_Next_Char(self.face, ucs4, &mut glyph) }; + if glyph == 0 { + break; } + let mut ucs4_range_start = ucs4; + loop { + let ucs4_new = unsafe { FT_Get_Next_Char(self.face, ucs4, &mut glyph) }; + if glyph == 0 { + break; + } + if ucs4_new - ucs4 != 1 { + coverage.add_range_unchecked(ucs4_range_start as u32..(ucs4 + 1) as u32); + ucs4_range_start = ucs4_new; + } + ucs4 = ucs4_new; + } + coverage.add_range_unchecked(ucs4_range_start as u32..(ucs4 + 1) as u32); if *encoding == FT_Encoding::FT_ENCODING_MS_SYMBOL { // Fontconfig duplicates F000..F0FF to 0000..00FF @@ -284,7 +300,6 @@ impl Face { } } } - coverage } diff --git a/wezterm-font/src/locator/font_config.rs b/wezterm-font/src/locator/font_config.rs index 9ef0f67c3..ed7ef6a6e 100644 --- a/wezterm-font/src/locator/font_config.rs +++ b/wezterm-font/src/locator/font_config.rs @@ -81,6 +81,7 @@ impl FontLocator for FontConfigFontLocator { index, variation, origin: FontOrigin::FontConfig, + coverage: Some(pat.get_charset()?.to_range_set()) }; // fontconfig will give us a boatload of random fallbacks. @@ -157,6 +158,7 @@ impl FontLocator for FontConfigFontLocator { index: pat.get_integer("index")?.try_into()?, variation: 0, origin: FontOrigin::FontConfig, + coverage: Some(pat.get_charset()?.to_range_set()) }; if let Ok(parsed) = crate::parser::ParsedFont::from_locator(&handle) { fonts.push(parsed); diff --git a/wezterm-font/src/locator/mod.rs b/wezterm-font/src/locator/mod.rs index 3fb80bf09..52d2c6838 100644 --- a/wezterm-font/src/locator/mod.rs +++ b/wezterm-font/src/locator/mod.rs @@ -108,12 +108,35 @@ impl std::fmt::Debug for FontDataSource { /// The `index` parameter is the index into a font /// collection if the data represents a collection of /// fonts. -#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct FontDataHandle { pub source: FontDataSource, pub index: u32, pub variation: u32, pub origin: FontOrigin, + pub coverage: Option> +} + +impl PartialOrd for FontDataHandle { + fn partial_cmp(&self, other: &Self) -> Option { + (&self.source, self.index, self.variation, &self.origin).partial_cmp(&( + &other.source, + other.index, + other.variation, + &other.origin, + )) + } +} + +impl Ord for FontDataHandle { + fn cmp(&self, other: &Self) -> Ordering { + (&self.source, self.index, self.variation, &self.origin).cmp(&( + &other.source, + other.index, + other.variation, + &other.origin, + )) + } } impl FontDataHandle { diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index d233fa3d1..340f31b6c 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -448,6 +448,8 @@ pub(crate) fn load_built_in_fonts(font_info: &mut Vec) -> anyhow::Re index: 0, variation: 0, origin: FontOrigin::BuiltIn, + // TODO: precompute this! + coverage: None, }; let face = lib.face_from_locator(&locator)?; let parsed = ParsedFont::from_face(&face, locator)?; @@ -487,6 +489,8 @@ pub(crate) fn parse_and_collect_font_info( index, variation: 0, origin, + // TODO: ??? + coverage: None, }; let face = lib.face_from_locator(&locator)?;