From 4e8341d35c20a8447ac948d6c84cae5ab098bac2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 14 Dec 2022 15:15:29 +0100 Subject: [PATCH] Don't render the \r (Carriage Return) character, because it sucks (#2452) * Don't render the \r (Carriage Return) character, because it sucks * Update PR links * Fix doclink --- CHANGELOG.md | 1 + crates/epaint/CHANGELOG.md | 3 ++- crates/epaint/src/text/font.rs | 31 +++++++++++++++++++++---------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e5c8c1e..d65ce7a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG ### Fixed 🐛 * Expose `TextEdit`'s multiline flag to AccessKit ([#2448](https://github.com/emilk/egui/pull/2448)). +* Don't render `\r` (Carriage Return) ([#2452](https://github.com/emilk/egui/pull/2452)). ## 0.20.1 - 2022-12-11 - Fix key-repeat diff --git a/crates/epaint/CHANGELOG.md b/crates/epaint/CHANGELOG.md index 52ac1405..69e2b034 100644 --- a/crates/epaint/CHANGELOG.md +++ b/crates/epaint/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to the epaint crate will be documented in this file. ## Unreleased * Improve the look of thin white lines ([#2437](https://github.com/emilk/egui/pull/2437)). +* Don't render `\r` (Carriage Return) ([#2452](https://github.com/emilk/egui/pull/2452)). ## 0.20.0 - 2022-12-08 @@ -20,7 +21,7 @@ All notable changes to the epaint crate will be documented in this file. * Added `epaint::hex_color!` to create `Color32`'s from hex strings under the `color-hex` feature ([#1596](https://github.com/emilk/egui/pull/1596)). * Optimize tessellation of filled circles by 10x or more ([#1616](https://github.com/emilk/egui/pull/1616)). * Added opt-in feature `deadlock_detection` to detect double-lock of mutexes on the same thread ([#1619](https://github.com/emilk/egui/pull/1619)). -* Texture loading now takes a `TexureOptions` with minification and magnification filters ([#2224](https://github.com/emilk/egui/pull/2224)). +* Texture loading now takes a `TextureOptions` with minification and magnification filters ([#2224](https://github.com/emilk/egui/pull/2224)). ## 0.18.1 - 2022-05-01 diff --git a/crates/epaint/src/text/font.rs b/crates/epaint/src/text/font.rs index 4fb0079c..67dfd43e 100644 --- a/crates/epaint/src/text/font.rs +++ b/crates/epaint/src/text/font.rs @@ -38,11 +38,12 @@ pub struct GlyphInfo { /// Unit: points. pub advance_width: f32, - /// Texture coordinates. None for space. + /// Texture coordinates. pub uv_rect: UvRect, } impl Default for GlyphInfo { + /// Basically a zero-width space. fn default() -> Self { Self { id: ab_glyph::GlyphId(0), @@ -105,6 +106,9 @@ impl FontImpl { } } + /// Code points that will always be replaced by the replacement character. + /// + /// See also [`invisible_char`]. fn ignore_character(&self, chr: char) -> bool { if self.name == "emoji-icon-font" { // HACK: https://github.com/emilk/egui/issues/1284 https://github.com/jslegers/emoji-icon-font/issues/18 @@ -142,7 +146,7 @@ impl FontImpl { } if self.ignore_character(c) { - return None; + return None; // these will result in the replacement character when rendering } if c == '\t' { @@ -173,19 +177,18 @@ impl FontImpl { } } + if invisible_char(c) { + let glyph_info = GlyphInfo::default(); + self.glyph_info_cache.write().insert(c, glyph_info); + return Some(glyph_info); + } + // Add new character: use ab_glyph::Font as _; let glyph_id = self.ab_glyph_font.glyph_id(c); if glyph_id.0 == 0 { - if invisible_char(c) { - // hack - let glyph_info = GlyphInfo::default(); - self.glyph_info_cache.write().insert(c, glyph_info); - Some(glyph_info) - } else { - None // unsupported character - } + None // unsupported character } else { let glyph_info = allocate_glyph( &mut self.atlas.lock(), @@ -376,8 +379,16 @@ impl Font { } } +/// Code points that will always be invisible (zero width). +/// +/// See also [`FontImpl::ignore_character`]. #[inline] fn invisible_char(c: char) -> bool { + if c == '\r' { + // A character most vile and pernicious. Don't display it. + return true; + } + // See https://github.com/emilk/egui/issues/336 // From https://www.fileformat.info/info/unicode/category/Cf/list.htm