From 2916f1337a4fc07337547aa0958fee615d554233 Mon Sep 17 00:00:00 2001 From: keith Date: Sun, 26 Jan 2020 19:09:43 -0800 Subject: [PATCH] don't require emoji font --- src/bridge/mod.rs | 3 +-- src/error_handling.rs | 18 +++++++++++++++++- src/renderer/caching_shaper.rs | 29 ++++++++++++++++++----------- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index 73b72f0..eefc93b 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -3,7 +3,7 @@ mod handler; mod keybindings; mod ui_commands; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::process::Stdio; use rmpv::Value; @@ -15,7 +15,6 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; pub use events::*; pub use keybindings::*; pub use ui_commands::UiCommand; -use crate::editor::{EDITOR, Editor}; use crate::error_handling::ResultPanicExplanation; use crate::INITIAL_DIMENSIONS; use handler::NeovimHandler; diff --git a/src/error_handling.rs b/src/error_handling.rs index c17d331..d5ee1a0 100644 --- a/src/error_handling.rs +++ b/src/error_handling.rs @@ -4,7 +4,7 @@ pub trait ResultPanicExplanation { fn unwrap_or_explained_panic(self, title: &str, explanation: &str) -> T; } -impl ResultPanicExplanation for Result { +impl ResultPanicExplanation for Result { fn unwrap_or_explained_panic(self, title: &str, explanation: &str) -> T { match self { Err(error) => { @@ -16,3 +16,19 @@ impl ResultPanicExplanation for Result { } } } + +pub trait OptionPanicExplanation { + fn unwrap_or_explained_panic(self, title: &str, explanation: &str) -> T; +} + +impl OptionPanicExplanation for Option { + fn unwrap_or_explained_panic(self, title: &str, explanation: &str) -> T { + match self { + None => { + msgbox::create(title, &explanation, IconType::Error); + panic!(explanation.to_string()); + }, + Some(content) => content + } + } +} diff --git a/src/renderer/caching_shaper.rs b/src/renderer/caching_shaper.rs index ba366df..8707379 100644 --- a/src/renderer/caching_shaper.rs +++ b/src/renderer/caching_shaper.rs @@ -6,6 +6,8 @@ use skulpin::skia_safe::{TextBlob, Font as SkiaFont, FontStyle, Typeface, TextBl use font_kit::source::SystemSource; use skribo::{layout_run, LayoutSession, FontRef as SkriboFont, FontFamily, FontCollection, TextStyle}; +use crate::error_handling::OptionPanicExplanation; + const STANDARD_CHARACTER_STRING: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; #[cfg(target_os = "windows")] @@ -41,7 +43,7 @@ struct ShapeKey { struct FontPair { normal: (SkiaFont, SkriboFont), - emoji: (SkiaFont, SkriboFont) + emoji: Option<(SkiaFont, SkriboFont)> } #[derive(Debug)] @@ -52,14 +54,14 @@ pub struct CachingShaper { blob_cache: LruCache> } -fn build_fonts(font_key: &FontKey, font_name: &str, base_size: f32) -> (SkiaFont, SkriboFont) { +fn build_fonts(font_key: &FontKey, font_name: &str, base_size: f32) -> Option<(SkiaFont, SkriboFont)> { let source = SystemSource::new(); let skribo_font = SkriboFont::new( source.select_family_by_name(font_name) - .expect("Failed to load by postscript name") + .ok()? .fonts()[0] .load() - .unwrap()); + .ok()?); let font_style = match (font_key.bold, font_key.italic) { (false, false) => FontStyle::normal(), @@ -68,10 +70,10 @@ fn build_fonts(font_key: &FontKey, font_name: &str, base_size: f32) -> (SkiaFont (true, true) => FontStyle::bold_italic() }; let skia_font = SkiaFont::from_typeface( - Typeface::new(font_name.clone(), font_style).expect("Could not load skia font file"), + Typeface::new(font_name.clone(), font_style)?, base_size * font_key.scale as f32); - (skia_font, skribo_font) + Some((skia_font, skribo_font)) } impl CachingShaper { @@ -87,7 +89,10 @@ impl CachingShaper { fn get_font_pair(&mut self, font_key: &FontKey) -> &FontPair { if !self.font_cache.contains(font_key) { let font_pair = FontPair { - normal: build_fonts(font_key, &self.font_name, self.base_size), + normal: build_fonts(font_key, &self.font_name, self.base_size) + .unwrap_or_explained_panic( + "Could not load configured font", + &format!("Could not load {}. This font was either the font configured in init scripts via guifont, or the default font is not available on your machine. Defaults are defined here: https://github.com/Kethku/neovide/blob/master/src/renderer/caching_shaper.rs", &self.font_name)), emoji: build_fonts(font_key, EMOJI_FONT, self.base_size) }; self.font_cache.put(font_key.clone(), font_pair); @@ -109,9 +114,11 @@ impl CachingShaper { normal_family.add_font(font_pair.normal.1.clone()); collection.add_family(normal_family); - let mut emoji_family = FontFamily::new(); - emoji_family.add_font(font_pair.emoji.1.clone()); - collection.add_family(emoji_family); + if let Some(emoji_font_pair) = &font_pair.emoji { + let mut emoji_family = FontFamily::new(); + emoji_family.add_font(emoji_font_pair.1.clone()); + collection.add_family(emoji_family); + } let session = LayoutSession::create(text, &style, &collection); @@ -125,7 +132,7 @@ impl CachingShaper { let skia_font = if Arc::ptr_eq(&skribo_font.font, &font_pair.normal.1.font) { &font_pair.normal.0 } else { - &font_pair.emoji.0 + &font_pair.emoji.as_ref().unwrap().0 }; let mut blob_builder = TextBlobBuilder::new();