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