fonts: change font to onest

This commit is contained in:
William Casarin
2023-07-05 14:52:48 -07:00
parent f7c5fd0cfc
commit a99056edf2
4 changed files with 86 additions and 2 deletions

View File

@@ -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<RetainedImage> {
fn parse_img_response(response: ehttp::Response) -> Result<RetainedImage> {
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<Result<Retained
let request = ehttp::Request::get(url);
let ctx = ctx.clone();
ehttp::fetch(request, move |response| {
let image = response.map_err(Error::Generic).and_then(parse_response);
let image = response
.map_err(Error::Generic)
.and_then(parse_img_response);
sender.send(image); // send the results back to the UI thread.
ctx.request_repaint();
});
@@ -318,6 +322,7 @@ fn render_pfp(ui: &mut egui::Ui, img_cache: &mut ImageCache, url: &str) {
fn pfp_image(ui: &mut egui::Ui, img: &RetainedImage, size: f32) -> 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..]);
*/
});
}

28
src/fonts.rs Normal file
View File

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

45
src/images.rs Normal file
View File

@@ -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;
}
}
}

View File

@@ -2,6 +2,10 @@ mod app;
//mod camera;
mod contacts;
mod error;
//mod note;
//mod block;
mod fonts;
mod images;
mod parser;
mod result;