From e4ea6a6c4b685b67ffd9864ca836d889ccf2eec6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 22 Aug 2023 19:03:02 -0700 Subject: [PATCH] fonts: capture palette info when parsing fonts --- wezterm-font/src/ftwrap.rs | 2 +- wezterm-font/src/parser.rs | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/wezterm-font/src/ftwrap.rs b/wezterm-font/src/ftwrap.rs index a4773903c..2a60730c1 100644 --- a/wezterm-font/src/ftwrap.rs +++ b/wezterm-font/src/ftwrap.rs @@ -709,7 +709,7 @@ impl Face { .ok_or_else(|| anyhow::anyhow!("invalid palette entry {index}")) } - pub fn get_palette_data(&mut self) -> anyhow::Result { + pub fn get_palette_data(&self) -> anyhow::Result { unsafe { let mut result = MaybeUninit::::zeroed(); ft_result(FT_Palette_Data_Get(self.face, result.as_mut_ptr()), ()) diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index 49c0f7de2..18a667187 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -12,6 +12,14 @@ pub enum MaybeShaped { Unresolved { raw: String, slice_start: usize }, } +#[derive(Debug, Clone)] +pub struct FontPaletteInfo { + pub name: String, + pub palette_index: usize, + pub usable_with_light_bg: bool, + pub usable_with_dark_bg: bool, +} + /// Represents a parsed font pub struct ParsedFont { names: Names, @@ -27,6 +35,7 @@ pub struct ParsedFont { pub assume_emoji_presentation: bool, pub pixel_sizes: Vec, pub is_built_in_fallback: bool, + pub palettes: Vec, pub harfbuzz_features: Option>, pub freetype_load_target: Option, @@ -79,6 +88,7 @@ impl Clone for ParsedFont { freetype_load_flags: self.freetype_load_flags, is_built_in_fallback: self.is_built_in_fallback, scale: self.scale, + palettes: self.palettes.clone(), } } } @@ -298,6 +308,23 @@ impl ParsedFont { if !p.pixel_sizes.is_empty() { code.push_str(&format!(" -- Pixel sizes: {:?}\n", p.pixel_sizes)); } + if !p.palettes.is_empty() { + for pal in &p.palettes { + let mut info = format!( + " -- Palette: {} {}", + pal.palette_index, + pal.name.to_string() + ); + if pal.usable_with_light_bg { + info.push_str(" (with light bg)"); + } + if pal.usable_with_dark_bg { + info.push_str(" (with dark bg)"); + } + info.push('\n'); + code.push_str(&info); + } + } for aka in &p.names.aliases { code.push_str(&format!(" -- AKA: \"{}\"\n", aka)); } @@ -367,6 +394,24 @@ impl ParsedFont { let cap_height = face.cap_height(); let pixel_sizes = face.pixel_sizes(); + let palettes = match face.get_palette_data() { + Ok(info) => info + .palettes + .iter() + .map(|p| FontPaletteInfo { + name: p.name.to_string(), + palette_index: p.palette_index, + usable_with_light_bg: (p.flags + & crate::ftwrap::FT_PALETTE_FOR_LIGHT_BACKGROUND as u16) + != 0, + usable_with_dark_bg: (p.flags + & crate::ftwrap::FT_PALETTE_FOR_DARK_BACKGROUND as u16) + != 0, + }) + .collect(), + Err(_) => vec![], + }; + let has_svg = unsafe { (((*face.face).face_flags as u32) & (crate::ftwrap::FT_FACE_FLAG_SVG as u32)) != 0 }; @@ -427,6 +472,7 @@ impl ParsedFont { freetype_load_target: None, freetype_load_flags: None, scale: None, + palettes, }) }