commit da3ee989fede7b3177165ef35445d3e004b371f2
parent 16323abe9329cde6e7e30fcc38a2e4606a7bc273
Author: William Casarin <jb55@jb55.com>
Date: Thu, 4 Jan 2024 07:59:44 -0800
fonts: use gossip's font loading code for testing
Diffstat:
M | src/app.rs | | | 4 | ++-- |
M | src/fonts.rs | | | 78 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- |
2 files changed, 79 insertions(+), 3 deletions(-)
diff --git 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
@@ -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::<String, FontData>::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<String, FontData> = 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);
+}