From da3ee989fede7b3177165ef35445d3e004b371f2 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 4 Jan 2024 07:59:44 -0800 Subject: [PATCH] fonts: use gossip's font loading code for testing --- src/app.rs | 4 +-- src/fonts.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3791270f..69a24407 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,6 @@ use crate::abbrev; use crate::contacts::Contacts; -use crate::fonts::setup_fonts; +use crate::fonts::{setup_fonts, setup_gossip_fonts}; use crate::frame_history::FrameHistory; use crate::images::fetch_img; use crate::ui::padding; @@ -146,7 +146,7 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) { #[cfg(feature = "profiling")] setup_profiling(); - setup_fonts(ctx); + setup_gossip_fonts(ctx); damus.pool = RelayPool::new(); relay_setup(&mut damus.pool, ctx); damus.state = DamusState::Initialized; diff --git a/src/fonts.rs b/src/fonts.rs index 31be27f0..3a6fd8c2 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -1,7 +1,9 @@ -use egui::{FontData, FontDefinitions, FontFamily}; +use egui::{FontData, FontDefinitions, FontFamily, FontTweak}; +use std::collections::BTreeMap; pub fn setup_fonts(ctx: &egui::Context) { let mut fonts = FontDefinitions::default(); + let mut families = BTreeMap::::new(); let our_font: String = "onest".to_owned(); @@ -26,3 +28,77 @@ pub fn setup_fonts(ctx: &egui::Context) { ctx.set_fonts(fonts); } + +// Use gossip's approach to font loading. This includes japanese fonts +// for rending stuff from japanese users. +pub fn setup_gossip_fonts(ctx: &egui::Context) { + let mut font_data: BTreeMap = BTreeMap::new(); + let mut families = BTreeMap::new(); + + font_data.insert( + "DejaVuSans".to_owned(), + FontData::from_static(include_bytes!("../assets/fonts/DejaVuSansSansEmoji.ttf")), + ); + font_data.insert( + "DejaVuSansBold".to_owned(), + FontData::from_static(include_bytes!( + "../assets/fonts/DejaVuSans-Bold-SansEmoji.ttf" + )), + ); + + if cfg!(feature = "lang-cjk") { + font_data.insert( + "NotoSansCJK".to_owned(), + FontData::from_static(include_bytes!("../assets/fonts/NotoSansCJK-Regular.ttc")), + ); + } + + font_data.insert( + "Inconsolata".to_owned(), + FontData::from_static(include_bytes!("../assets/fonts/Inconsolata-Regular.ttf")).tweak( + FontTweak { + scale: 1.22, // This font is smaller than DejaVuSans + y_offset_factor: -0.18, // and too low + y_offset: 0.0, + baseline_offset_factor: 0.0, + }, + ), + ); + + // Some good looking emojis. Use as first priority: + font_data.insert( + "NotoEmoji-Regular".to_owned(), + FontData::from_static(include_bytes!("../assets/fonts/NotoEmoji-Regular.ttf")).tweak( + FontTweak { + scale: 1.1, // make them a touch larger + y_offset_factor: 0.0, + y_offset: 0.0, + baseline_offset_factor: 0.0, + }, + ), + ); + + let mut proportional = vec!["DejaVuSans".to_owned(), "NotoEmoji-Regular".to_owned()]; + if cfg!(feature = "lang-cjk") { + proportional.push("NotoSansCJK".to_owned()); + } + + families.insert(FontFamily::Proportional, proportional); + + families.insert( + FontFamily::Monospace, + vec!["Inconsolata".to_owned(), "NotoEmoji-Regular".to_owned()], + ); + + families.insert( + FontFamily::Name("Bold".into()), + vec!["DejaVuSansBold".to_owned()], + ); + + let defs = FontDefinitions { + font_data, + families, + }; + + ctx.set_fonts(defs); +}