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