2019-01-12 23:55:56 +00:00
|
|
|
use std::{
|
2020-05-24 09:36:24 +00:00
|
|
|
collections::BTreeMap,
|
2019-01-17 17:03:39 +00:00
|
|
|
hash::{Hash, Hasher},
|
2020-04-17 21:30:01 +00:00
|
|
|
sync::Arc,
|
2019-01-12 23:55:56 +00:00
|
|
|
};
|
|
|
|
|
2021-01-10 14:42:46 +00:00
|
|
|
use crate::{
|
|
|
|
mutex::Mutex,
|
|
|
|
text::font::{Font, FontImpl},
|
|
|
|
Texture, TextureAtlas,
|
|
|
|
};
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2020-07-23 12:35:12 +00:00
|
|
|
// TODO: rename
|
2020-12-27 13:16:37 +00:00
|
|
|
/// One of a few categories of styles of text, e.g. body, button or heading.
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-01-10 10:37:47 +00:00
|
|
|
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "persistence", serde(rename_all = "snake_case"))]
|
2019-01-12 23:55:56 +00:00
|
|
|
pub enum TextStyle {
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Used when small text is needed.
|
2020-10-25 07:56:48 +00:00
|
|
|
Small,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Normal labels. Easily readable, doesn't take up too much space.
|
2019-01-12 23:55:56 +00:00
|
|
|
Body,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Buttons. Maybe slightly bigger than `Body`.
|
2019-01-12 23:55:56 +00:00
|
|
|
Button,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Heading. Probably larger than `Body`.
|
2019-01-12 23:55:56 +00:00
|
|
|
Heading,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Same size as `Body`, but used when monospace is important (for aligning number, code snippets, etc).
|
2019-12-02 19:08:49 +00:00
|
|
|
Monospace,
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 18:30:01 +00:00
|
|
|
impl TextStyle {
|
|
|
|
pub fn all() -> impl Iterator<Item = TextStyle> {
|
|
|
|
[
|
|
|
|
TextStyle::Small,
|
|
|
|
TextStyle::Body,
|
|
|
|
TextStyle::Button,
|
|
|
|
TextStyle::Heading,
|
|
|
|
TextStyle::Monospace,
|
|
|
|
]
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 13:19:20 +00:00
|
|
|
/// Which style of font: [`Monospace`][`FontFamily::Monospace`] or [`Proportional`][`FontFamily::Proportional`].
|
2020-05-30 09:04:40 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2021-01-10 10:37:47 +00:00
|
|
|
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "persistence", serde(rename_all = "snake_case"))]
|
2019-12-02 19:08:49 +00:00
|
|
|
pub enum FontFamily {
|
2020-12-27 13:16:37 +00:00
|
|
|
/// A font where each character is the same width (`w` is the same width as `i`).
|
2019-12-02 19:08:49 +00:00
|
|
|
Monospace,
|
2020-12-27 13:16:37 +00:00
|
|
|
/// A font where some characters are wider than other (e.g. 'w' is wider than 'i').
|
2020-12-27 13:19:20 +00:00
|
|
|
Proportional,
|
2019-12-02 19:08:49 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 20:04:02 +00:00
|
|
|
/// The data of a `.ttf` or `.otf` file.
|
|
|
|
pub type FontData = std::borrow::Cow<'static, [u8]>;
|
|
|
|
|
|
|
|
fn rusttype_font_from_font_data(name: &str, data: &FontData) -> rusttype::Font<'static> {
|
|
|
|
match data {
|
|
|
|
std::borrow::Cow::Borrowed(bytes) => rusttype::Font::try_from_bytes(bytes),
|
|
|
|
std::borrow::Cow::Owned(bytes) => rusttype::Font::try_from_vec(bytes.clone()),
|
|
|
|
}
|
|
|
|
.unwrap_or_else(|| panic!("Error parsing {:?} TTF/OTF font file", name))
|
|
|
|
}
|
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Describes the font data and the sizes to use.
|
|
|
|
///
|
|
|
|
/// This is how you can tell Egui which fonts and font sizes to use.
|
|
|
|
///
|
|
|
|
/// Often you would start with [`FontDefinitions::default()`] and then add/change the contents.
|
|
|
|
///
|
2021-01-10 14:42:46 +00:00
|
|
|
/// ``` ignore
|
2020-12-27 13:16:37 +00:00
|
|
|
/// # let mut ctx = egui::CtxRef::default();
|
|
|
|
/// let mut fonts = egui::FontDefinitions::default();
|
|
|
|
/// // Large button text:
|
|
|
|
/// fonts.family_and_size.insert(
|
|
|
|
/// egui::TextStyle::Button,
|
2020-12-27 13:19:20 +00:00
|
|
|
/// (egui::FontFamily::Proportional, 32.0));
|
2020-12-27 13:16:37 +00:00
|
|
|
/// ctx.set_fonts(fonts);
|
|
|
|
/// ```
|
2020-05-30 12:56:38 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2021-01-10 10:37:47 +00:00
|
|
|
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "persistence", serde(default))]
|
2020-05-30 12:56:38 +00:00
|
|
|
pub struct FontDefinitions {
|
2020-12-13 18:19:57 +00:00
|
|
|
/// List of font names and their definitions.
|
|
|
|
/// The definition must be the contents of either a `.ttf` or `.otf` font file.
|
|
|
|
///
|
2020-10-31 17:03:13 +00:00
|
|
|
/// Egui has built-in-default for these,
|
|
|
|
/// but you can override them if you like.
|
2021-01-10 10:37:47 +00:00
|
|
|
#[cfg_attr(feature = "persistence", serde(skip))]
|
2020-12-13 20:04:02 +00:00
|
|
|
pub font_data: BTreeMap<String, FontData>,
|
2020-12-13 18:19:57 +00:00
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// Which fonts (names) to use for each [`FontFamily`].
|
2020-12-13 18:19:57 +00:00
|
|
|
///
|
2020-12-27 13:16:37 +00:00
|
|
|
/// The list should be a list of keys into [`Self::font_data`].
|
2020-12-13 19:37:44 +00:00
|
|
|
/// When looking for a character glyph Egui will start with
|
|
|
|
/// the first font and then move to the second, and so on.
|
2020-12-13 18:19:57 +00:00
|
|
|
/// So the first font is the primary, and then comes a list of fallbacks in order of priority.
|
|
|
|
pub fonts_for_family: BTreeMap<FontFamily, Vec<String>>,
|
|
|
|
|
2020-12-27 13:16:37 +00:00
|
|
|
/// The [`FontFamily`] and size you want to use for a specific [`TextStyle`].
|
2020-12-13 18:19:57 +00:00
|
|
|
pub family_and_size: BTreeMap<TextStyle, (FontFamily, f32)>,
|
2020-05-30 12:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FontDefinitions {
|
|
|
|
fn default() -> Self {
|
2020-12-26 20:20:55 +00:00
|
|
|
#[allow(unused)]
|
2020-12-13 20:04:02 +00:00
|
|
|
let mut font_data: BTreeMap<String, FontData> = BTreeMap::new();
|
2020-12-13 18:19:57 +00:00
|
|
|
|
|
|
|
let mut fonts_for_family = BTreeMap::new();
|
2020-12-26 20:20:55 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "default_fonts")]
|
|
|
|
{
|
|
|
|
// TODO: figure out a way to make the WASM smaller despite including fonts. Zip them?
|
|
|
|
|
|
|
|
// Use size 13 for this. NOTHING ELSE:
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"ProggyClean".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/ProggyClean.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"Ubuntu-Light".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/Ubuntu-Light.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Some good looking emojis. Use as first priority:
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"NotoEmoji-Regular".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/NotoEmoji-Regular.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
// Bigger emojis, and more. <http://jslegers.github.io/emoji-icon-font/>:
|
|
|
|
font_data.insert(
|
2020-12-13 18:19:57 +00:00
|
|
|
"emoji-icon-font".to_owned(),
|
2021-01-10 14:42:46 +00:00
|
|
|
std::borrow::Cow::Borrowed(include_bytes!("../../fonts/emoji-icon-font.ttf")),
|
2020-12-26 20:20:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
fonts_for_family.insert(
|
|
|
|
FontFamily::Monospace,
|
|
|
|
vec![
|
|
|
|
"ProggyClean".to_owned(),
|
|
|
|
"Ubuntu-Light".to_owned(), // fallback for √ etc
|
|
|
|
"NotoEmoji-Regular".to_owned(),
|
|
|
|
"emoji-icon-font".to_owned(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
fonts_for_family.insert(
|
2020-12-27 13:19:20 +00:00
|
|
|
FontFamily::Proportional,
|
2020-12-26 20:20:55 +00:00
|
|
|
vec![
|
|
|
|
"Ubuntu-Light".to_owned(),
|
|
|
|
"NotoEmoji-Regular".to_owned(),
|
|
|
|
"emoji-icon-font".to_owned(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "default_fonts"))]
|
|
|
|
{
|
|
|
|
fonts_for_family.insert(FontFamily::Monospace, vec![]);
|
2020-12-27 13:19:20 +00:00
|
|
|
fonts_for_family.insert(FontFamily::Proportional, vec![]);
|
2020-12-26 20:20:55 +00:00
|
|
|
}
|
2020-12-13 18:19:57 +00:00
|
|
|
|
|
|
|
let mut family_and_size = BTreeMap::new();
|
2020-12-27 13:19:20 +00:00
|
|
|
family_and_size.insert(TextStyle::Small, (FontFamily::Proportional, 10.0));
|
|
|
|
family_and_size.insert(TextStyle::Body, (FontFamily::Proportional, 14.0));
|
|
|
|
family_and_size.insert(TextStyle::Button, (FontFamily::Proportional, 16.0));
|
|
|
|
family_and_size.insert(TextStyle::Heading, (FontFamily::Proportional, 20.0));
|
2020-12-13 18:19:57 +00:00
|
|
|
family_and_size.insert(TextStyle::Monospace, (FontFamily::Monospace, 13.0)); // 13 for `ProggyClean`
|
2020-05-30 12:56:38 +00:00
|
|
|
|
|
|
|
Self {
|
2020-12-13 18:19:57 +00:00
|
|
|
font_data,
|
|
|
|
fonts_for_family,
|
|
|
|
family_and_size,
|
2020-05-30 12:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-12 10:07:51 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
/// The collection of fonts used by Egui.
|
|
|
|
///
|
|
|
|
/// Note: `Fonts::default()` is invalid (missing `pixels_per_point`).
|
2020-05-30 12:56:38 +00:00
|
|
|
#[derive(Default)]
|
2019-01-12 23:55:56 +00:00
|
|
|
pub struct Fonts {
|
2020-12-27 22:09:51 +00:00
|
|
|
pixels_per_point: f32,
|
2019-12-02 19:08:49 +00:00
|
|
|
definitions: FontDefinitions,
|
2019-01-12 23:55:56 +00:00
|
|
|
fonts: BTreeMap<TextStyle, Font>,
|
2020-09-09 12:24:13 +00:00
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
/// Copy of the texture in the texture atlas.
|
|
|
|
/// This is so we can return a reference to it (the texture atlas is behind a lock).
|
|
|
|
buffered_texture: Mutex<Arc<Texture>>,
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Fonts {
|
2020-12-27 22:09:51 +00:00
|
|
|
pub fn from_definitions(pixels_per_point: f32, definitions: FontDefinitions) -> Self {
|
2020-12-12 18:30:01 +00:00
|
|
|
// We want an atlas big enough to be able to include all the Emojis in the `TextStyle::Heading`,
|
|
|
|
// so we can show the Emoji picker demo window.
|
|
|
|
let mut atlas = TextureAtlas::new(2048, 64);
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
{
|
2020-09-09 13:24:09 +00:00
|
|
|
// Make the top left pixel fully white:
|
|
|
|
let pos = atlas.allocate((1, 1));
|
2020-09-09 12:24:13 +00:00
|
|
|
assert_eq!(pos, (0, 0));
|
2020-09-09 13:24:09 +00:00
|
|
|
atlas.texture_mut()[pos] = 255;
|
2020-09-09 12:24:13 +00:00
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
|
|
|
|
let atlas = Arc::new(Mutex::new(atlas));
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
let mut font_impl_cache = FontImplCache::new(atlas.clone(), pixels_per_point, &definitions);
|
2020-12-12 18:30:01 +00:00
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
let fonts = definitions
|
2020-12-13 18:19:57 +00:00
|
|
|
.family_and_size
|
2020-12-12 18:30:01 +00:00
|
|
|
.iter()
|
2020-12-13 18:19:57 +00:00
|
|
|
.map(|(&text_style, &(family, scale_in_points))| {
|
2020-12-27 22:09:51 +00:00
|
|
|
let fonts = &definitions.fonts_for_family.get(&family);
|
2020-12-26 20:20:55 +00:00
|
|
|
let fonts = fonts.unwrap_or_else(|| {
|
|
|
|
panic!("FontFamily::{:?} is not bound to any fonts", family)
|
|
|
|
});
|
|
|
|
let fonts: Vec<Arc<FontImpl>> = fonts
|
2020-12-13 18:19:57 +00:00
|
|
|
.iter()
|
|
|
|
.map(|font_name| font_impl_cache.font_impl(font_name, scale_in_points))
|
|
|
|
.collect();
|
2020-12-11 22:39:32 +00:00
|
|
|
|
2020-12-11 23:53:07 +00:00
|
|
|
(text_style, Font::new(fonts))
|
2019-01-19 16:10:28 +00:00
|
|
|
})
|
2019-01-17 17:03:39 +00:00
|
|
|
.collect();
|
2019-01-12 23:55:56 +00:00
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
{
|
|
|
|
let mut atlas = atlas.lock();
|
|
|
|
let texture = atlas.texture_mut();
|
|
|
|
// Make sure we seed the texture version with something unique based on the default characters:
|
2020-11-28 11:24:38 +00:00
|
|
|
use std::collections::hash_map::DefaultHasher;
|
|
|
|
let mut hasher = DefaultHasher::default();
|
2020-09-09 12:24:13 +00:00
|
|
|
texture.pixels.hash(&mut hasher);
|
|
|
|
texture.version = hasher.finish();
|
|
|
|
}
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
Self {
|
|
|
|
pixels_per_point,
|
|
|
|
definitions,
|
|
|
|
fonts,
|
|
|
|
atlas,
|
|
|
|
buffered_texture: Default::default(), //atlas.lock().texture().clone();
|
|
|
|
}
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 22:09:51 +00:00
|
|
|
pub fn pixels_per_point(&self) -> f32 {
|
|
|
|
self.pixels_per_point
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn definitions(&self) -> &FontDefinitions {
|
|
|
|
&self.definitions
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Call each frame to get the latest available font texture data.
|
2020-09-09 12:24:13 +00:00
|
|
|
pub fn texture(&self) -> Arc<Texture> {
|
|
|
|
let atlas = self.atlas.lock();
|
|
|
|
let mut buffered_texture = self.buffered_texture.lock();
|
|
|
|
if buffered_texture.version != atlas.texture().version {
|
|
|
|
*buffered_texture = Arc::new(atlas.texture().clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
buffered_texture.clone()
|
2019-01-12 23:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Index<TextStyle> for Fonts {
|
|
|
|
type Output = Font;
|
|
|
|
|
|
|
|
fn index(&self, text_style: TextStyle) -> &Font {
|
|
|
|
&self.fonts[&text_style]
|
|
|
|
}
|
|
|
|
}
|
2020-12-12 18:30:01 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-12-27 11:56:16 +00:00
|
|
|
struct FontImplCache {
|
2020-12-12 18:30:01 +00:00
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
pixels_per_point: f32,
|
2020-12-13 18:19:57 +00:00
|
|
|
rusttype_fonts: std::collections::BTreeMap<String, Arc<rusttype::Font<'static>>>,
|
2020-12-12 18:30:01 +00:00
|
|
|
|
2020-12-13 18:19:57 +00:00
|
|
|
/// Map font names and size to the cached `FontImpl`.
|
|
|
|
/// Can't have f32 in a HashMap or BTreeMap, so let's do a linear search
|
|
|
|
cache: Vec<(String, f32, Arc<FontImpl>)>,
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FontImplCache {
|
2020-12-27 22:09:51 +00:00
|
|
|
pub fn new(
|
|
|
|
atlas: Arc<Mutex<TextureAtlas>>,
|
|
|
|
pixels_per_point: f32,
|
|
|
|
definitions: &super::FontDefinitions,
|
|
|
|
) -> Self {
|
2020-12-13 18:19:57 +00:00
|
|
|
let rusttype_fonts = definitions
|
|
|
|
.font_data
|
2020-12-12 18:30:01 +00:00
|
|
|
.iter()
|
2020-12-13 18:19:57 +00:00
|
|
|
.map(|(name, font_data)| {
|
2020-12-12 18:30:01 +00:00
|
|
|
(
|
2020-12-13 18:19:57 +00:00
|
|
|
name.clone(),
|
2020-12-13 20:04:02 +00:00
|
|
|
Arc::new(rusttype_font_from_font_data(name, font_data)),
|
2020-12-12 18:30:01 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
atlas,
|
2020-12-27 22:09:51 +00:00
|
|
|
pixels_per_point,
|
2020-12-13 18:19:57 +00:00
|
|
|
rusttype_fonts,
|
2020-12-12 18:30:01 +00:00
|
|
|
cache: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:19:57 +00:00
|
|
|
pub fn rusttype_font(&self, font_name: &str) -> Arc<rusttype::Font<'static>> {
|
|
|
|
self.rusttype_fonts
|
|
|
|
.get(font_name)
|
|
|
|
.unwrap_or_else(|| panic!("No font data found for {:?}", font_name))
|
|
|
|
.clone()
|
2020-12-12 18:30:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 18:19:57 +00:00
|
|
|
pub fn font_impl(&mut self, font_name: &str, scale_in_points: f32) -> Arc<FontImpl> {
|
2020-12-12 18:30:01 +00:00
|
|
|
for entry in &self.cache {
|
2020-12-13 18:19:57 +00:00
|
|
|
if (entry.0.as_str(), entry.1) == (font_name, scale_in_points) {
|
2020-12-12 18:30:01 +00:00
|
|
|
return entry.2.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 09:20:50 +00:00
|
|
|
let y_offset = if font_name == "emoji-icon-font" {
|
|
|
|
1.0 // TODO: remove font alignment hack
|
|
|
|
} else {
|
|
|
|
-3.0 // TODO: remove font alignment hack
|
|
|
|
};
|
|
|
|
|
|
|
|
let scale_in_points = if font_name == "emoji-icon-font" {
|
|
|
|
scale_in_points - 2.0 // TODO: remove HACK!
|
|
|
|
} else {
|
|
|
|
scale_in_points
|
|
|
|
};
|
|
|
|
|
2020-12-12 18:30:01 +00:00
|
|
|
let font_impl = Arc::new(FontImpl::new(
|
|
|
|
self.atlas.clone(),
|
|
|
|
self.pixels_per_point,
|
2020-12-13 18:19:57 +00:00
|
|
|
self.rusttype_font(font_name),
|
2020-12-12 18:30:01 +00:00
|
|
|
scale_in_points,
|
2021-01-10 09:20:50 +00:00
|
|
|
y_offset,
|
2020-12-12 18:30:01 +00:00
|
|
|
));
|
|
|
|
self.cache
|
2020-12-13 18:19:57 +00:00
|
|
|
.push((font_name.to_owned(), scale_in_points, font_impl.clone()));
|
2020-12-12 18:30:01 +00:00
|
|
|
font_impl
|
|
|
|
}
|
|
|
|
}
|