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