Ignore characters that are wrong in emoji-icon-font.ttf

Closes https://github.com/emilk/egui/issues/1284
This commit is contained in:
Emil Ernerfeldt 2022-02-22 09:47:31 +01:00
parent 2af1dda4c3
commit a90379ac8d
2 changed files with 32 additions and 14 deletions

View file

@ -56,6 +56,7 @@ impl Default for GlyphInfo {
/// A specific font with a size. /// A specific font with a size.
/// The interface uses points as the unit for everything. /// The interface uses points as the unit for everything.
pub struct FontImpl { pub struct FontImpl {
name: String,
ab_glyph_font: ab_glyph::FontArc, ab_glyph_font: ab_glyph::FontArc,
/// Maximum character height /// Maximum character height
scale_in_pixels: u32, scale_in_pixels: u32,
@ -71,6 +72,7 @@ impl FontImpl {
pub fn new( pub fn new(
atlas: Arc<Mutex<TextureAtlas>>, atlas: Arc<Mutex<TextureAtlas>>,
pixels_per_point: f32, pixels_per_point: f32,
name: String,
ab_glyph_font: ab_glyph::FontArc, ab_glyph_font: ab_glyph::FontArc,
scale_in_pixels: u32, scale_in_pixels: u32,
y_offset_points: f32, y_offset_points: f32,
@ -91,6 +93,7 @@ impl FontImpl {
let y_offset = (y_offset_points * pixels_per_point).round() / pixels_per_point; let y_offset = (y_offset_points * pixels_per_point).round() / pixels_per_point;
Self { Self {
name,
ab_glyph_font, ab_glyph_font,
scale_in_pixels, scale_in_pixels,
height_in_points, height_in_points,
@ -101,14 +104,16 @@ impl FontImpl {
} }
} }
/// An un-ordered iterator over all supported characters. fn ignore_character(&self, chr: char) -> bool {
fn characters(&self) -> impl Iterator<Item = char> + '_ { if self.name == "emoji-icon-font" {
use ab_glyph::Font as _; // HACK: https://github.com/emilk/egui/issues/1284 https://github.com/jslegers/emoji-icon-font/issues/18
self.ab_glyph_font // Don't show the wrong fullwidth capital letters:
.codepoint_ids() if '' <= chr && chr <= '' {
.map(|(_, chr)| chr) return true;
.filter(|chr| { }
!matches!( }
matches!(
chr, chr,
// Strip out a religious symbol with secondary nefarious interpretation: // Strip out a religious symbol with secondary nefarious interpretation:
'\u{534d}' | '\u{5350}' | '\u{534d}' | '\u{5350}' |
@ -116,7 +121,15 @@ impl FontImpl {
// Ignore ubuntu-specific stuff in `Ubuntu-Light.ttf`: // Ignore ubuntu-specific stuff in `Ubuntu-Light.ttf`:
'\u{E0FF}' | '\u{EFFD}' | '\u{F0FF}' | '\u{F200}' '\u{E0FF}' | '\u{EFFD}' | '\u{F0FF}' | '\u{F200}'
) )
}) }
/// An un-ordered iterator over all supported characters.
fn characters(&self) -> impl Iterator<Item = char> + '_ {
use ab_glyph::Font as _;
self.ab_glyph_font
.codepoint_ids()
.map(|(_, chr)| chr)
.filter(|&chr| !self.ignore_character(chr))
} }
/// `\n` will result in `None` /// `\n` will result in `None`
@ -127,9 +140,9 @@ impl FontImpl {
} }
} }
// Add new character: if self.ignore_character(c) {
use ab_glyph::Font as _; return None;
let glyph_id = self.ab_glyph_font.glyph_id(c); }
if c == '\t' { if c == '\t' {
if let Some(space) = self.glyph_info(' ') { if let Some(space) = self.glyph_info(' ') {
@ -142,6 +155,10 @@ impl FontImpl {
} }
} }
// Add new character:
use ab_glyph::Font as _;
let glyph_id = self.ab_glyph_font.glyph_id(c);
if glyph_id.0 == 0 { if glyph_id.0 == 0 {
if invisible_char(c) { if invisible_char(c) {
// hack // hack
@ -149,7 +166,7 @@ impl FontImpl {
self.glyph_info_cache.write().insert(c, glyph_info); self.glyph_info_cache.write().insert(c, glyph_info);
Some(glyph_info) Some(glyph_info)
} else { } else {
None None // unsupported character
} }
} else { } else {
let glyph_info = allocate_glyph( let glyph_info = allocate_glyph(

View file

@ -714,6 +714,7 @@ impl FontImplCache {
Arc::new(FontImpl::new( Arc::new(FontImpl::new(
self.atlas.clone(), self.atlas.clone(),
self.pixels_per_point, self.pixels_per_point,
font_name.to_owned(),
ab_glyph_font, ab_glyph_font,
scale_in_pixels, scale_in_pixels,
y_offset_points, y_offset_points,