Skip to content

Commit

Permalink
fonts: use gossip's font loading code for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed Jan 4, 2024
1 parent 16323ab commit da3ee98
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::abbrev;
use crate::contacts::Contacts;
use crate::fonts::setup_fonts;
use crate::fonts::{setup_fonts, setup_gossip_fonts};

Check warning on line 3 in src/app.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `setup_fonts`

Check failure on line 3 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `setup_fonts`

Check warning on line 3 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `setup_fonts`
use crate::frame_history::FrameHistory;
use crate::images::fetch_img;
use crate::ui::padding;
Expand Down Expand Up @@ -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;
Expand Down
78 changes: 77 additions & 1 deletion src/fonts.rs
Original file line number Diff line number Diff line change
@@ -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();

Check warning on line 6 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `families`

Check warning on line 6 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Check

variable does not need to be mutable

Check failure on line 6 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused variable: `families`

Check failure on line 6 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Clippy

variable does not need to be mutable

Check warning on line 6 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `families`

Check warning on line 6 in src/fonts.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable

let our_font: String = "onest".to_owned();

Expand All @@ -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);
}

0 comments on commit da3ee98

Please sign in to comment.