fonts.rs (3925B)
1 use egui::{FontData, FontDefinitions, FontTweak}; 2 use std::collections::BTreeMap; 3 use tracing::debug; 4 5 pub enum NamedFontFamily { 6 Medium, 7 } 8 9 impl NamedFontFamily { 10 pub fn as_str(&mut self) -> &'static str { 11 match self { 12 //Self::Bold => "bold", 13 Self::Medium => "medium", 14 } 15 } 16 17 pub fn as_family(&mut self) -> egui::FontFamily { 18 egui::FontFamily::Name(self.as_str().into()) 19 } 20 } 21 22 // Use gossip's approach to font loading. This includes japanese fonts 23 // for rending stuff from japanese users. 24 pub fn setup_fonts(ctx: &egui::Context) { 25 let mut font_data: BTreeMap<String, FontData> = BTreeMap::new(); 26 let mut families = BTreeMap::new(); 27 28 font_data.insert( 29 "Onest".to_owned(), 30 FontData::from_static(include_bytes!( 31 "../assets/fonts/onest/OnestRegular1602-hint.ttf" 32 )), 33 ); 34 35 font_data.insert( 36 "OnestMedium".to_owned(), 37 FontData::from_static(include_bytes!( 38 "../assets/fonts/onest/OnestMedium1602-hint.ttf" 39 )), 40 ); 41 42 font_data.insert( 43 "DejaVuSans".to_owned(), 44 FontData::from_static(include_bytes!("../assets/fonts/DejaVuSansSansEmoji.ttf")), 45 ); 46 /* 47 font_data.insert( 48 "OnestBold".to_owned(), 49 FontData::from_static(include_bytes!( 50 "../assets/fonts/onest/OnestBold1602-hint.ttf" 51 )), 52 ); 53 54 font_data.insert( 55 "DejaVuSansBold".to_owned(), 56 FontData::from_static(include_bytes!( 57 "../assets/fonts/DejaVuSans-Bold-SansEmoji.ttf" 58 )), 59 ); 60 61 font_data.insert( 62 "DejaVuSans".to_owned(), 63 FontData::from_static(include_bytes!("../assets/fonts/DejaVuSansSansEmoji.ttf")), 64 ); 65 font_data.insert( 66 "DejaVuSansBold".to_owned(), 67 FontData::from_static(include_bytes!( 68 "../assets/fonts/DejaVuSans-Bold-SansEmoji.ttf" 69 )), 70 ); 71 */ 72 73 font_data.insert( 74 "Inconsolata".to_owned(), 75 FontData::from_static(include_bytes!("../assets/fonts/Inconsolata-Regular.ttf")).tweak( 76 FontTweak { 77 scale: 1.22, // This font is smaller than DejaVuSans 78 y_offset_factor: -0.18, // and too low 79 y_offset: 0.0, 80 baseline_offset_factor: 0.0, 81 }, 82 ), 83 ); 84 85 font_data.insert( 86 "NotoSansCJK".to_owned(), 87 FontData::from_static(include_bytes!("../assets/fonts/NotoSansCJK-Regular.ttc")), 88 ); 89 90 font_data.insert( 91 "NotoSansThai".to_owned(), 92 FontData::from_static(include_bytes!("../assets/fonts/NotoSansThai-Regular.ttf")), 93 ); 94 95 // Some good looking emojis. Use as first priority: 96 font_data.insert( 97 "NotoEmoji".to_owned(), 98 FontData::from_static(include_bytes!("../assets/fonts/NotoEmoji-Regular.ttf")).tweak( 99 FontTweak { 100 scale: 1.1, // make them a touch larger 101 y_offset_factor: 0.0, 102 y_offset: 0.0, 103 baseline_offset_factor: 0.0, 104 }, 105 ), 106 ); 107 108 let base_fonts = vec![ 109 "DejaVuSans".to_owned(), 110 "NotoEmoji".to_owned(), 111 "NotoSansCJK".to_owned(), 112 "NotoSansThai".to_owned(), 113 ]; 114 115 let mut proportional = vec!["Onest".to_owned()]; 116 proportional.extend(base_fonts.clone()); 117 118 let mut medium = vec!["OnestMedium".to_owned()]; 119 medium.extend(base_fonts.clone()); 120 121 let mut mono = vec!["Inconsolata".to_owned()]; 122 mono.extend(base_fonts); 123 124 families.insert(egui::FontFamily::Proportional, proportional); 125 families.insert(egui::FontFamily::Monospace, mono); 126 families.insert( 127 egui::FontFamily::Name(NamedFontFamily::Medium.as_str().into()), 128 medium, 129 ); 130 131 debug!("fonts: {:?}", families); 132 133 let defs = FontDefinitions { 134 font_data, 135 families, 136 }; 137 138 ctx.set_fonts(defs); 139 }