Add test for font selection

Antonio Scandurra created

Change summary

gpui/src/font_cache.rs         | 26 +++++++++++++++
gpui/src/platform/mac/fonts.rs | 60 +++++++++++++++++++----------------
2 files changed, 58 insertions(+), 28 deletions(-)

Detailed changes

gpui/src/font_cache.rs 🔗

@@ -161,3 +161,29 @@ impl FontCache {
         metric * font_size / self.metric(font_id, |m| m.units_per_em as f32)
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::{
+        fonts::{Style, Weight},
+        platform::{test, Platform as _},
+    };
+
+    #[test]
+    fn test_select_font() {
+        let platform = test::platform();
+        let fonts = FontCache::new(platform.fonts());
+        let arial = fonts.load_family(&["Arial"]).unwrap();
+        let arial_regular = fonts.select_font(arial, &Properties::new()).unwrap();
+        let arial_italic = fonts
+            .select_font(arial, &Properties::new().style(Style::Italic))
+            .unwrap();
+        let arial_bold = fonts
+            .select_font(arial, &Properties::new().weight(Weight::BOLD))
+            .unwrap();
+        assert_ne!(arial_regular, arial_italic);
+        assert_ne!(arial_regular, arial_bold);
+        assert_ne!(arial_italic, arial_bold);
+    }
+}

gpui/src/platform/mac/fonts.rs 🔗

@@ -322,6 +322,9 @@ mod tests {
         let menlo_regular = fonts.select_font(&menlo, &Properties::new())?;
         let menlo_italic = fonts.select_font(&menlo, &Properties::new().style(Style::Italic))?;
         let menlo_bold = fonts.select_font(&menlo, &Properties::new().weight(Weight::BOLD))?;
+        assert_ne!(menlo_regular, menlo_italic);
+        assert_ne!(menlo_regular, menlo_bold);
+        assert_ne!(menlo_italic, menlo_bold);
 
         let line = fonts.layout_str(
             "hello world",
@@ -371,32 +374,33 @@ mod tests {
         Ok(())
     }
 
-    // #[test]
-    // fn test_rasterize_glyph() {
-    //     use std::{fs::File, io::BufWriter, path::Path};
-
-    //     let fonts = FontSystem::new();
-    //     let font_ids = fonts.load_family("Fira Code").unwrap();
-    //     let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
-    //     let glyph_id = fonts.glyph_for_char(font_id, 'G').unwrap();
-
-    //     const VARIANTS: usize = 1;
-    //     for i in 0..VARIANTS {
-    //         let variant = i as f32 / VARIANTS as f32;
-    //         let (bounds, bytes) = fonts
-    //             .rasterize_glyph(font_id, 16.0, glyph_id, vec2f(variant, variant), 2.)
-    //             .unwrap();
-
-    //         let name = format!("/Users/as-cii/Desktop/twog-{}.png", i);
-    //         let path = Path::new(&name);
-    //         let file = File::create(path).unwrap();
-    //         let ref mut w = BufWriter::new(file);
-
-    //         let mut encoder = png::Encoder::new(w, bounds.width() as u32, bounds.height() as u32);
-    //         encoder.set_color(png::ColorType::Grayscale);
-    //         encoder.set_depth(png::BitDepth::Eight);
-    //         let mut writer = encoder.write_header().unwrap();
-    //         writer.write_image_data(&bytes).unwrap();
-    //     }
-    // }
+    #[test]
+    #[ignore]
+    fn test_rasterize_glyph() {
+        use std::{fs::File, io::BufWriter, path::Path};
+
+        let fonts = FontSystem::new();
+        let font_ids = fonts.load_family("Fira Code").unwrap();
+        let font_id = fonts.select_font(&font_ids, &Default::default()).unwrap();
+        let glyph_id = fonts.glyph_for_char(font_id, 'G').unwrap();
+
+        const VARIANTS: usize = 1;
+        for i in 0..VARIANTS {
+            let variant = i as f32 / VARIANTS as f32;
+            let (bounds, bytes) = fonts
+                .rasterize_glyph(font_id, 16.0, glyph_id, vec2f(variant, variant), 2.)
+                .unwrap();
+
+            let name = format!("/Users/as-cii/Desktop/twog-{}.png", i);
+            let path = Path::new(&name);
+            let file = File::create(path).unwrap();
+            let ref mut w = BufWriter::new(file);
+
+            let mut encoder = png::Encoder::new(w, bounds.width() as u32, bounds.height() as u32);
+            encoder.set_color(png::ColorType::Grayscale);
+            encoder.set_depth(png::BitDepth::Eight);
+            let mut writer = encoder.write_header().unwrap();
+            writer.write_image_data(&bytes).unwrap();
+        }
+    }
 }