From a99056edf20a51ec07d9b3257c5201e0bac3a7a6 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Wed, 5 Jul 2023 14:52:48 -0700 Subject: [PATCH] fonts: change font to onest --- src/app.rs | 11 +++++++++-- src/fonts.rs | 28 ++++++++++++++++++++++++++++ src/images.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 4 ++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/fonts.rs create mode 100644 src/images.rs diff --git a/src/app.rs b/src/app.rs index 9d6c9a3f..8e20bda9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use egui_extras::RetainedImage; use crate::contacts::Contacts; +use crate::fonts::setup_fonts; use crate::{Error, Result}; use egui::Context; use enostr::{ClientMessage, EventId, Filter, Profile, Pubkey, RelayEvent, RelayMessage}; @@ -111,6 +112,7 @@ fn try_process_event(damus: &mut Damus) { fn update_damus(damus: &mut Damus, ctx: &egui::Context) { if damus.state == DamusState::Initializing { + setup_fonts(ctx); damus.pool = RelayPool::new(); relay_setup(&mut damus.pool, ctx); damus.state = DamusState::Initialized; @@ -235,7 +237,7 @@ impl Damus { } #[allow(clippy::needless_pass_by_value)] -fn parse_response(response: ehttp::Response) -> Result { +fn parse_img_response(response: ehttp::Response) -> Result { let content_type = response.content_type().unwrap_or_default(); if content_type.starts_with("image/svg") { @@ -263,7 +265,9 @@ fn fetch_img_from_net(ctx: &egui::Context, url: &str) -> Promise egui::Response { img.show_max_size(ui, egui::vec2(size, size)) + //.with_options() } fn render_username(ui: &mut egui::Ui, contacts: &Contacts, pk: &Pubkey) { @@ -329,9 +334,11 @@ fn render_username(ui: &mut egui::Ui, contacts: &Contacts, pk: &Pubkey) { } } + /* ui.label(&pk.as_ref()[0..8]); ui.label(":"); ui.label(&pk.as_ref()[64 - 8..]); + */ }); } diff --git a/src/fonts.rs b/src/fonts.rs new file mode 100644 index 00000000..31be27f0 --- /dev/null +++ b/src/fonts.rs @@ -0,0 +1,28 @@ +use egui::{FontData, FontDefinitions, FontFamily}; + +pub fn setup_fonts(ctx: &egui::Context) { + let mut fonts = FontDefinitions::default(); + + let our_font: String = "onest".to_owned(); + + // Install my own font (maybe supporting non-latin characters): + fonts.font_data.insert( + our_font.clone(), + FontData::from_static(include_bytes!( + "../assets/fonts/onest/OnestRegular1602-hint.ttf" + )), + ); // .ttf and .otf supported + + // Put my font first (highest priority): + fonts + .families + .get_mut(&FontFamily::Proportional) + .unwrap() + .insert(0, our_font); + + // Put my font as last fallback for monospace: + //fonts.families.get_mut(&FontFamily::Monospace).unwrap() + //.push("onest".to_owned()); + + ctx.set_fonts(fonts); +} diff --git a/src/images.rs b/src/images.rs new file mode 100644 index 00000000..9a51d791 --- /dev/null +++ b/src/images.rs @@ -0,0 +1,45 @@ +use egui::{Color32, ColorImage}; + +pub fn round_image(image: &mut ColorImage) { + // The radius to the edge of of the avatar circle + let edge_radius = image.size[0] as f32 / 2.0; + let edge_radius_squared = edge_radius * edge_radius; + + for (pixnum, pixel) in image.pixels.iter_mut().enumerate() { + // y coordinate + let uy = pixnum / image.size[0]; + let y = uy as f32; + let y_offset = edge_radius - y; + + // x coordinate + let ux = pixnum % image.size[0]; + let x = ux as f32; + let x_offset = edge_radius - x; + + // The radius to this pixel (may be inside or outside the circle) + let pixel_radius_squared: f32 = x_offset * x_offset + y_offset * y_offset; + + // If inside of the avatar circle + if pixel_radius_squared <= edge_radius_squared { + // squareroot to find how many pixels we are from the edge + let pixel_radius: f32 = pixel_radius_squared.sqrt(); + let distance = edge_radius - pixel_radius; + + // If we are within 1 pixel of the edge, we should fade, to + // antialias the edge of the circle. 1 pixel from the edge should + // be 100% of the original color, and right on the edge should be + // 0% of the original color. + if distance <= 1.0 { + *pixel = Color32::from_rgba_premultiplied( + (pixel.r() as f32 * distance) as u8, + (pixel.g() as f32 * distance) as u8, + (pixel.b() as f32 * distance) as u8, + (pixel.a() as f32 * distance) as u8, + ); + } + } else { + // Outside of the avatar circle + *pixel = Color32::TRANSPARENT; + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 14d5d9e5..5c3212fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,10 @@ mod app; //mod camera; mod contacts; mod error; +//mod note; +//mod block; +mod fonts; +mod images; mod parser; mod result;