Bundle and use Inconsolata v2.012

Antonio Scandurra created

There's a newer version of the font available but ligatures seem
broken googlefonts/Inconsolata#58 and googlefonts/Inconsolata#52.

As part of this commit I also upgraded rust-embed to use the new
exclusion feature, which allows us to skip embedding OS files like
`.DS_Store`.

Change summary

Cargo.lock                                           | 14 +++--
gpui/src/app.rs                                      |  4 +
gpui/src/platform.rs                                 |  1 
gpui/src/platform/mac/fonts.rs                       | 30 +++++++++++--
server/Cargo.toml                                    |  2 
server/src/assets.rs                                 |  2 
server/src/main.rs                                   |  4 
zed/Cargo.toml                                       |  2 
zed/assets/fonts/inconsolata/Inconsolata-Bold.ttf    |  0 
zed/assets/fonts/inconsolata/Inconsolata-Regular.ttf |  0 
zed/assets/themes/dark.toml                          |  2 
zed/assets/themes/light.toml                         |  2 
zed/src/assets.rs                                    |  5 +
zed/src/language.rs                                  |  5 +
zed/src/main.rs                                      | 14 +++++-
zed/src/settings.rs                                  |  4 
16 files changed, 65 insertions(+), 26 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -4040,9 +4040,9 @@ dependencies = [
 
 [[package]]
 name = "rust-embed"
-version = "5.9.0"
+version = "6.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2fe1fe6aac5d6bb9e1ffd81002340363272a7648234ec7bdfac5ee202cb65523"
+checksum = "1be44a6694859b7cfc955699935944a6844aa9fe416aeda5d40829e3e38dfee6"
 dependencies = [
  "rust-embed-impl",
  "rust-embed-utils",
@@ -4051,9 +4051,9 @@ dependencies = [
 
 [[package]]
 name = "rust-embed-impl"
-version = "5.9.0"
+version = "6.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3ed91c41c42ef7bf687384439c312e75e0da9c149b0390889b94de3c7d9d9e66"
+checksum = "f567ca01565c50c67b29e535f5f67b8ea8aeadaeed16a88f10792ab57438b957"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -4064,10 +4064,12 @@ dependencies = [
 
 [[package]]
 name = "rust-embed-utils"
-version = "5.1.0"
+version = "7.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2a512219132473ab0a77b52077059f1c47ce4af7fbdc94503e9862a34422876d"
+checksum = "6116e7ab9ea963f60f2f20291d8fcf6c7273192cdd7273b3c80729a9605c97b2"
 dependencies = [
+ "glob 0.3.0",
+ "sha2",
  "walkdir",
 ]
 

gpui/src/app.rs 🔗

@@ -300,6 +300,10 @@ impl App {
         }))
     }
 
+    pub fn platform(&self) -> Arc<dyn Platform> {
+        self.0.borrow().platform()
+    }
+
     pub fn font_cache(&self) -> Arc<FontCache> {
         self.0.borrow().cx.font_cache.clone()
     }

gpui/src/platform.rs 🔗

@@ -127,6 +127,7 @@ pub enum CursorStyle {
 }
 
 pub trait FontSystem: Send + Sync {
+    fn add_fonts(&self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()>;
     fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
     fn select_font(
         &self,

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

@@ -21,9 +21,12 @@ use core_graphics::{
     base::CGGlyph, color_space::CGColorSpace, context::CGContext, geometry::CGAffineTransform,
 };
 use core_text::{line::CTLine, string_attributes::kCTFontAttributeName};
-use font_kit::{canvas::RasterizationOptions, hinting::HintingOptions, source::SystemSource};
+use font_kit::{
+    canvas::RasterizationOptions, handle::Handle, hinting::HintingOptions, source::SystemSource,
+    sources::mem::MemSource,
+};
 use parking_lot::RwLock;
-use std::{cell::RefCell, char, cmp, convert::TryFrom, ffi::c_void};
+use std::{cell::RefCell, char, cmp, convert::TryFrom, ffi::c_void, sync::Arc};
 
 #[allow(non_upper_case_globals)]
 const kCGImageAlphaOnly: u32 = 7;
@@ -31,20 +34,26 @@ const kCGImageAlphaOnly: u32 = 7;
 pub struct FontSystem(RwLock<FontSystemState>);
 
 struct FontSystemState {
-    source: SystemSource,
+    memory_source: MemSource,
+    system_source: SystemSource,
     fonts: Vec<font_kit::font::Font>,
 }
 
 impl FontSystem {
     pub fn new() -> Self {
         Self(RwLock::new(FontSystemState {
-            source: SystemSource::new(),
+            memory_source: MemSource::empty(),
+            system_source: SystemSource::new(),
             fonts: Vec::new(),
         }))
     }
 }
 
 impl platform::FontSystem for FontSystem {
+    fn add_fonts(&self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()> {
+        self.0.write().add_fonts(fonts)
+    }
+
     fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>> {
         self.0.write().load_family(name)
     }
@@ -93,9 +102,20 @@ impl platform::FontSystem for FontSystem {
 }
 
 impl FontSystemState {
+    fn add_fonts(&mut self, fonts: Vec<Arc<Vec<u8>>>) -> anyhow::Result<()> {
+        self.memory_source
+            .add_fonts(fonts.into_iter().map(|bytes| Handle::from_memory(bytes, 0)))?;
+        Ok(())
+    }
+
     fn load_family(&mut self, name: &str) -> anyhow::Result<Vec<FontId>> {
         let mut font_ids = Vec::new();
-        for font in self.source.select_family_by_name(name)?.fonts() {
+
+        let family = self
+            .memory_source
+            .select_family_by_name(name)
+            .or_else(|_| self.system_source.select_family_by_name(name))?;
+        for font in family.fonts() {
             let font = font.load()?;
             font_ids.push(FontId(self.fonts.len()));
             self.fonts.push(font);

server/Cargo.toml 🔗

@@ -29,7 +29,7 @@ oauth2-surf = "0.1.1"
 parking_lot = "0.11.1"
 postage = { version = "0.4.1", features = ["futures-traits"] }
 rand = "0.8"
-rust-embed = "5.9.0"
+rust-embed = { version = "6.2", features = ["include-exclude"] }
 scrypt = "0.7"
 serde = { version = "1.0", features = ["derive"] }
 sha-1 = "0.9"

server/src/assets.rs 🔗

@@ -26,6 +26,6 @@ async fn get_static_asset(request: Request) -> tide::Result {
 
     Ok(tide::Response::builder(200)
         .content_type(content_type)
-        .body(content.as_ref())
+        .body(content.data.as_ref())
         .build())
 }

server/src/main.rs 🔗

@@ -83,7 +83,7 @@ impl AppState {
                 let partial = Templates::get(path.as_ref()).unwrap();
                 self.handlebars
                     .write()
-                    .register_partial(partial_name, std::str::from_utf8(partial.as_ref()).unwrap())
+                    .register_partial(partial_name, std::str::from_utf8(&partial.data).unwrap())
                     .unwrap()
             }
         }
@@ -98,7 +98,7 @@ impl AppState {
         self.register_partials();
 
         self.handlebars.read().render_template(
-            std::str::from_utf8(Templates::get(path).unwrap().as_ref()).unwrap(),
+            std::str::from_utf8(&Templates::get(path).unwrap().data).unwrap(),
             data,
         )
     }

zed/Cargo.toml 🔗

@@ -38,7 +38,7 @@ parking_lot = "0.11.1"
 postage = { version = "0.4.1", features = ["futures-traits"] }
 rand = "0.8.3"
 rsa = "0.4"
-rust-embed = "5.9.0"
+rust-embed = { version = "6.2", features = ["include-exclude"] }
 seahash = "4.1"
 serde = { version = "1", features = ["derive"] }
 serde_json = { version = "1.0.64", features = ["preserve_order"] }

zed/assets/themes/dark.toml 🔗

@@ -6,7 +6,7 @@ extends = "_base"
 2 = "#131415"
 
 [text]
-base = { family = "Helvetica", size = 14.0 }
+base = { family = "Inconsolata", size = 14 }
 0 = { extends = "$text.base", color = "#ffffff" }
 1 = { extends = "$text.base", color = "#b3b3b3" }
 2 = { extends = "$text.base", color = "#7b7d80" }

zed/assets/themes/light.toml 🔗

@@ -7,7 +7,7 @@ extends = "_base"
 3 = "#3a3b3c"
 
 [text]
-base = { family = "Helvetica", size = 14.0 }
+base = { family = "Inconsolata", size = 14 }
 0 = { extends = "$text.base", color = "#acacac" }
 1 = { extends = "$text.base", color = "#111111" }
 2 = { extends = "$text.base", color = "#333333" }

zed/src/assets.rs 🔗

@@ -4,11 +4,14 @@ use rust_embed::RustEmbed;
 
 #[derive(RustEmbed)]
 #[folder = "assets"]
+#[exclude = "*.DS_Store"]
 pub struct Assets;
 
 impl AssetSource for Assets {
     fn load(&self, path: &str) -> Result<std::borrow::Cow<[u8]>> {
-        Self::get(path).ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
+        Self::get(path)
+            .map(|f| f.data)
+            .ok_or_else(|| anyhow!("could not find asset at path \"{}\"", path))
     }
 
     fn list(&self, path: &str) -> Vec<std::borrow::Cow<'static, str>> {

zed/src/language.rs 🔗

@@ -47,7 +47,8 @@ impl Language {
 impl LanguageRegistry {
     pub fn new() -> Self {
         let grammar = tree_sitter_rust::language();
-        let rust_config = toml::from_slice(&LanguageDir::get("rust/config.toml").unwrap()).unwrap();
+        let rust_config =
+            toml::from_slice(&LanguageDir::get("rust/config.toml").unwrap().data).unwrap();
         let rust_language = Language {
             config: rust_config,
             grammar,
@@ -84,7 +85,7 @@ impl LanguageRegistry {
     fn load_query(grammar: tree_sitter::Language, path: &str) -> Query {
         Query::new(
             grammar,
-            str::from_utf8(LanguageDir::get(path).unwrap().as_ref()).unwrap(),
+            str::from_utf8(&LanguageDir::get(path).unwrap().data).unwrap(),
         )
         .unwrap()
     }

zed/src/main.rs 🔗

@@ -2,12 +2,14 @@
 #![allow(non_snake_case)]
 
 use fs::OpenOptions;
+use gpui::AssetSource;
 use log::LevelFilter;
 use parking_lot::Mutex;
 use simplelog::SimpleLogger;
 use std::{fs, path::PathBuf, sync::Arc};
 use zed::{
-    self, assets,
+    self,
+    assets::Assets,
     channel::ChannelList,
     chat_panel, editor, file_finder,
     fs::RealFs,
@@ -20,9 +22,15 @@ use zed::{
 fn main() {
     init_logger();
 
-    let app = gpui::App::new(assets::Assets).unwrap();
+    let app = gpui::App::new(Assets).unwrap();
+    let embedded_fonts = Assets
+        .list("fonts")
+        .into_iter()
+        .map(|f| Arc::new(Assets.load(&f).unwrap().to_vec()))
+        .collect();
+    app.platform().fonts().add_fonts(embedded_fonts).unwrap();
 
-    let themes = settings::ThemeRegistry::new(assets::Assets, app.font_cache());
+    let themes = settings::ThemeRegistry::new(Assets, app.font_cache());
     let (settings_tx, settings) = settings::channel(&app.font_cache(), &themes).unwrap();
     let languages = Arc::new(language::LanguageRegistry::new());
     languages.set_theme(&settings.borrow().theme.syntax);

zed/src/settings.rs 🔗

@@ -37,8 +37,8 @@ impl Settings {
 
     pub fn new(font_cache: &FontCache, theme: Arc<Theme>) -> Result<Self> {
         Ok(Self {
-            buffer_font_family: font_cache.load_family(&["Fira Code", "Monaco"])?,
-            buffer_font_size: 14.0,
+            buffer_font_family: font_cache.load_family(&["Inconsolata"])?,
+            buffer_font_size: 16.,
             tab_size: 4,
             theme,
         })