Add Emoji support with NotoEmoji-Regular

This commit is contained in:
Emil Ernerfeldt 2020-12-12 00:53:07 +01:00
parent a9df510a01
commit cb310676af
5 changed files with 237 additions and 171 deletions

View file

@ -209,5 +209,6 @@ Egui is under MIT OR Apache-2.0 license.
Fonts: Fonts:
* NotoEmoji-Regular.ttf, [google.com/get/noto](https://google.com/get/noto), [SIL Open Font License](https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL)
* ProggyClean.ttf, Copyright (c) 2004, 2005 Tristan Grimmer. MIT License. <http://www.proggyfonts.net/> * ProggyClean.ttf, Copyright (c) 2004, 2005 Tristan Grimmer. MIT License. <http://www.proggyfonts.net/>
* Ubuntu-Light.ttf by [Dalton Maag](http://www.daltonmaag.com/): [Ubuntu font licence](https://ubuntu.com/legal/font-licence) * Ubuntu-Light.ttf by [Dalton Maag](http://www.daltonmaag.com/): [Ubuntu font licence](https://ubuntu.com/legal/font-licence)

Binary file not shown.

49
OFL.txt → egui/fonts/OFL.txt Executable file → Normal file
View file

@ -1,20 +1,19 @@
Copyright 2011 The Comfortaa Project Authors (https://github.com/alexeiva/comfortaa), with Reserved Font Name "Comfortaa". This Font Software is licensed under the SIL Open Font License,
Version 1.1.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at: This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL http://scripts.sil.org/OFL
----------------------------------------------------------- -----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
----------------------------------------------------------- -----------------------------------------------------------
PREAMBLE PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation development of collaborative font projects, to support the font
efforts of academic and linguistic communities, and to provide a free and creation efforts of academic and linguistic communities, and to
open framework in which fonts may be shared and improved in partnership provide a free and open framework in which fonts may be shared and
with others. improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The redistributed freely as long as they are not sold by themselves. The
@ -22,8 +21,8 @@ fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives, names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply requirement for fonts to remain under this license does not apply to
to any document created using the fonts or their derivatives. any document created using the fonts or their derivatives.
DEFINITIONS DEFINITIONS
"Font Software" refers to the set of files released by the Copyright "Font Software" refers to the set of files released by the Copyright
@ -33,25 +32,25 @@ include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the "Reserved Font Name" refers to any names specified as such after the
copyright statement(s). copyright statement(s).
"Original Version" refers to the collection of Font Software components as "Original Version" refers to the collection of Font Software
distributed by the Copyright Holder(s). components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting, "Modified Version" refers to any derivative made by adding to,
or substituting -- in part or in whole -- any of the components of the deleting, or substituting -- in part or in whole -- any of the
Original Version, by changing formats or by porting the Font Software to a components of the Original Version, by changing formats or by porting
new environment. the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical "Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software. writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify, a copy of the Font Software, to use, study, copy, merge, embed,
redistribute, and sell modified and unmodified copies of the Font modify, redistribute, and sell modified and unmodified copies of the
Software, subject to the following conditions: Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, 1) Neither the Font Software nor any of its individual components, in
in Original or Modified Versions, may be sold by itself. Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled, 2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy redistributed and/or sold with any software, provided that each copy
@ -61,9 +60,9 @@ in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user. binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font 3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding Name(s) unless explicit written permission is granted by the
Copyright Holder. This restriction only applies to the primary font name as corresponding Copyright Holder. This restriction only applies to the
presented to the users. primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any Software shall not be used to promote, endorse or advertise any
@ -74,8 +73,8 @@ permission.
5) The Font Software, modified or unmodified, in part or in whole, 5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created remain under this license does not apply to any document created using
using the Font Software. the Font Software.
TERMINATION TERMINATION
This license becomes null and void if any of the above conditions are This license becomes null and void if any of the above conditions are

View file

@ -50,7 +50,7 @@ pub struct FontImpl {
/// Maximum character height /// Maximum character height
scale_in_pixels: f32, scale_in_pixels: f32,
pixels_per_point: f32, pixels_per_point: f32,
glyph_infos: RwLock<AHashMap<char, GlyphInfo>>, glyph_info_cache: RwLock<AHashMap<char, GlyphInfo>>,
atlas: Arc<Mutex<TextureAtlas>>, atlas: Arc<Mutex<TextureAtlas>>,
} }
@ -71,7 +71,7 @@ impl FontImpl {
font, font,
scale_in_pixels, scale_in_pixels,
pixels_per_point, pixels_per_point,
glyph_infos: Default::default(), glyph_info_cache: Default::default(),
atlas, atlas,
}; };
@ -89,7 +89,7 @@ impl FontImpl {
/// `\n` will result in `None` /// `\n` will result in `None`
fn glyph_info(&self, c: char) -> Option<GlyphInfo> { fn glyph_info(&self, c: char) -> Option<GlyphInfo> {
{ {
if let Some(glyph_info) = self.glyph_infos.read().get(&c) { if let Some(glyph_info) = self.glyph_info_cache.read().get(&c) {
return Some(*glyph_info); return Some(*glyph_info);
} }
} }
@ -103,7 +103,7 @@ impl FontImpl {
self.pixels_per_point, self.pixels_per_point,
); );
let glyph_info = glyph_info?; let glyph_info = glyph_info?;
self.glyph_infos.write().insert(c, glyph_info); self.glyph_info_cache.write().insert(c, glyph_info);
Some(glyph_info) Some(glyph_info)
} }
@ -118,63 +118,135 @@ impl FontImpl {
/ self.pixels_per_point / self.pixels_per_point
} }
/// Height of one row of text. In points
pub fn row_height(&self) -> f32 {
self.scale_in_pixels / self.pixels_per_point
}
pub fn pixels_per_point(&self) -> f32 {
self.pixels_per_point
}
}
type FontIndex = usize;
// TODO: rename Layouter ?
/// Wrapper over multiple `FontImpl` (commonly two: primary + emoji fallback)
pub struct Font {
fonts: Vec<Arc<FontImpl>>,
replacement_font_index_glyph_info: (FontIndex, GlyphInfo),
pixels_per_point: f32,
row_height: f32,
glyph_info_cache: RwLock<AHashMap<char, (FontIndex, GlyphInfo)>>,
}
impl Font {
pub fn new(fonts: Vec<Arc<FontImpl>>) -> Self {
assert!(!fonts.is_empty());
let replacement_glyph_font_index = 0;
let replacement_glyph_info = fonts[replacement_glyph_font_index]
.glyph_info(REPLACEMENT_CHAR)
.unwrap_or_else(|| {
panic!(
"Failed to find replacement character {:?}",
REPLACEMENT_CHAR
)
});
let replacement_font_index_glyph_info =
(replacement_glyph_font_index, replacement_glyph_info);
let pixels_per_point = fonts[0].pixels_per_point();
let row_height = fonts[0].row_height();
let slf = Self {
fonts,
replacement_font_index_glyph_info,
pixels_per_point,
row_height,
glyph_info_cache: Default::default(),
};
slf.glyph_info_cache
.write()
.insert(REPLACEMENT_CHAR, replacement_font_index_glyph_info);
slf
}
pub fn round_to_pixel(&self, point: f32) -> f32 { pub fn round_to_pixel(&self, point: f32) -> f32 {
(point * self.pixels_per_point).round() / self.pixels_per_point (point * self.pixels_per_point).round() / self.pixels_per_point
} }
/// Height of one row of text. In points /// Height of one row of text. In points
pub fn row_height(&self) -> f32 { pub fn row_height(&self) -> f32 {
self.scale_in_pixels / self.pixels_per_point self.row_height
} }
pub fn uv_rect(&self, c: char) -> Option<UvRect> { pub fn uv_rect(&self, c: char) -> Option<UvRect> {
self.glyph_infos.read().get(&c).and_then(|gi| gi.uv_rect) self.glyph_info_cache
} .read()
} .get(&c)
.and_then(|gi| gi.1.uv_rect)
// TODO: rename Layouter ?
/// Wrapper over multiple `FontImpl` (commonly two: primary + emoji fallback)
pub struct Font {
font_impl: Arc<FontImpl>,
replacement_glyph_info: GlyphInfo,
}
impl Font {
pub fn new(font_impl: Arc<FontImpl>) -> Self {
let replacement_glyph_info = font_impl.glyph_info(REPLACEMENT_CHAR).unwrap_or_else(|| {
panic!(
"Failed to find replacement character {:?}",
REPLACEMENT_CHAR
)
});
Self {
font_impl,
replacement_glyph_info,
}
}
pub fn round_to_pixel(&self, point: f32) -> f32 {
self.font_impl.round_to_pixel(point)
}
/// Height of one row of text. In points
pub fn row_height(&self) -> f32 {
self.font_impl.row_height()
}
pub fn uv_rect(&self, c: char) -> Option<UvRect> {
self.font_impl.uv_rect(c)
} }
pub fn glyph_width(&self, c: char) -> f32 { pub fn glyph_width(&self, c: char) -> f32 {
self.glyph_info(c).advance_width self.glyph_info(c).1.advance_width
} }
/// `\n` will (intentionally) show up as '?' (`REPLACEMENT_CHAR`) /// `\n` will (intentionally) show up as '?' (`REPLACEMENT_CHAR`)
fn glyph_info(&self, c: char) -> GlyphInfo { fn glyph_info(&self, c: char) -> (FontIndex, GlyphInfo) {
self.font_impl {
.glyph_info(c) if let Some(glyph_info) = self.glyph_info_cache.read().get(&c) {
.unwrap_or_else(|| self.replacement_glyph_info) return *glyph_info;
}
}
let font_index_glyph_info = self.glyph_info_no_cache(c);
let font_index_glyph_info =
font_index_glyph_info.unwrap_or_else(|| self.replacement_font_index_glyph_info);
self.glyph_info_cache
.write()
.insert(c, font_index_glyph_info);
font_index_glyph_info
}
fn glyph_info_no_cache(&self, c: char) -> Option<(FontIndex, GlyphInfo)> {
for (font_index, font_impl) in self.fonts.iter().enumerate() {
if let Some(glyph_info) = font_impl.glyph_info(c) {
self.glyph_info_cache
.write()
.insert(c, (font_index, glyph_info));
return Some((font_index, glyph_info));
}
}
None
}
/// Typeset the given text onto one row.
/// Assumes there are no `\n` in the text.
/// Return `x_offsets`, one longer than the number of characters in the text.
fn layout_single_row_fragment(&self, text: &str) -> Vec<f32> {
let mut x_offsets = Vec::with_capacity(text.chars().count() + 1);
x_offsets.push(0.0);
let mut cursor_x_in_points = 0.0f32;
let mut last_glyph_id = None;
for c in text.chars() {
let (font_index, glyph_info) = self.glyph_info(c);
let font_impl = &self.fonts[font_index];
if let Some(last_glyph_id) = last_glyph_id {
cursor_x_in_points += font_impl.pair_kerning(last_glyph_id, glyph_info.id)
}
cursor_x_in_points += glyph_info.advance_width;
cursor_x_in_points = self.round_to_pixel(cursor_x_in_points);
last_glyph_id = Some(glyph_info.id);
x_offsets.push(cursor_x_in_points);
}
x_offsets
} }
/// Typeset the given text onto one row. /// Typeset the given text onto one row.
@ -272,32 +344,6 @@ impl Font {
galley galley
} }
/// Typeset the given text onto one row.
/// Assumes there are no `\n` in the text.
/// Return `x_offsets`, one longer than the number of characters in the text.
fn layout_single_row_fragment(&self, text: &str) -> Vec<f32> {
let mut x_offsets = Vec::with_capacity(text.chars().count() + 1);
x_offsets.push(0.0);
let mut cursor_x_in_points = 0.0f32;
let mut last_glyph_id = None;
for c in text.chars() {
let glyph = self.glyph_info(c);
if let Some(last_glyph_id) = last_glyph_id {
cursor_x_in_points += self.font_impl.pair_kerning(last_glyph_id, glyph.id)
}
cursor_x_in_points += glyph.advance_width;
cursor_x_in_points = self.round_to_pixel(cursor_x_in_points);
last_glyph_id = Some(glyph.id);
x_offsets.push(cursor_x_in_points);
}
x_offsets
}
/// A paragraph is text with no line break character in it. /// A paragraph is text with no line break character in it.
/// The text will be wrapped by the given `max_width_in_points`. /// The text will be wrapped by the given `max_width_in_points`.
/// Always returns at least one row. /// Always returns at least one row.

View file

@ -7,7 +7,7 @@ use std::{
use crate::mutex::Mutex; use crate::mutex::Mutex;
use super::{ use super::{
font::Font, font::{Font, FontImpl},
texture_atlas::{Texture, TextureAtlas}, texture_atlas::{Texture, TextureAtlas},
}; };
@ -41,6 +41,9 @@ pub struct FontDefinitions {
/// Egui has built-in-default for these, /// Egui has built-in-default for these,
/// but you can override them if you like. /// but you can override them if you like.
pub ttf_data: BTreeMap<FontFamily, &'static [u8]>, pub ttf_data: BTreeMap<FontFamily, &'static [u8]>,
/// ttf data for emoji font, if any
pub emoji_ttf_data: Option<&'static [u8]>,
} }
impl Default for FontDefinitions { impl Default for FontDefinitions {
@ -66,10 +69,13 @@ impl FontDefinitions {
ttf_data.insert(FontFamily::Monospace, monospace_typeface_data); ttf_data.insert(FontFamily::Monospace, monospace_typeface_data);
ttf_data.insert(FontFamily::VariableWidth, variable_typeface_data); ttf_data.insert(FontFamily::VariableWidth, variable_typeface_data);
let emoji_ttf_data = include_bytes!("../../fonts/NotoEmoji-Regular.ttf");
Self { Self {
pixels_per_point, pixels_per_point,
fonts, fonts,
ttf_data, ttf_data,
emoji_ttf_data: Some(emoji_ttf_data),
} }
} }
} }
@ -101,7 +107,7 @@ impl Fonts {
return; return;
} }
let mut atlas = TextureAtlas::new(512, 16); // TODO: better default? let mut atlas = TextureAtlas::new(1024, 16); // TODO: better default?
{ {
// Make the top left pixel fully white: // Make the top left pixel fully white:
@ -117,22 +123,36 @@ impl Fonts {
pixels_per_point, pixels_per_point,
fonts, fonts,
ttf_data, ttf_data,
emoji_ttf_data,
} = definitions; } = definitions;
self.fonts = fonts self.fonts = fonts
.into_iter() .into_iter()
.map(|(text_style, (family, size))| { .map(|(text_style, (family, size))| {
let typeface_data = ttf_data let typeface_data = ttf_data
.get(&family) .get(&family)
.unwrap_or_else(|| panic!("Missing TTF data for {:?}", family)); .unwrap_or_else(|| panic!("Missing TTF data for {:?}", family));
let font_impl = super::font::FontImpl::new(
let font_impl = Arc::new(FontImpl::new(
atlas.clone(), atlas.clone(),
typeface_data, typeface_data,
size, size,
pixels_per_point, pixels_per_point,
); ));
let font_impl = Arc::new(font_impl);
(text_style, Font::new(font_impl)) let mut fonts = vec![font_impl];
if let Some(emoji_ttf_data) = emoji_ttf_data {
let emoji_font_impl = Arc::new(FontImpl::new(
atlas.clone(),
emoji_ttf_data,
size,
pixels_per_point,
));
fonts.push(emoji_font_impl);
}
(text_style, Font::new(fonts))
}) })
.collect(); .collect();