Skip to content

Commit

Permalink
Fix problems with tabs in text (#3355)
Browse files Browse the repository at this point in the history
* Move ascent out of `GlyphInfo`

* Give a function a better name

* Make tab and thin space act more like a normal space
  • Loading branch information
emilk authored Sep 18, 2023
1 parent fea9047 commit 8073ca6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
22 changes: 11 additions & 11 deletions crates/epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ pub struct GlyphInfo {
/// Unit: points.
pub advance_width: f32,

/// `ascent` value from the font metrics.
/// this is the distance from the top to the baseline.
///
/// Unit: points.
pub ascent: f32,

/// Texture coordinates.
pub uv_rect: UvRect,
}
Expand All @@ -59,7 +53,6 @@ impl Default for GlyphInfo {
Self {
id: ab_glyph::GlyphId(0),
advance_width: 0.0,
ascent: 0.0,
uv_rect: Default::default(),
}
}
Expand Down Expand Up @@ -188,7 +181,7 @@ impl FontImpl {
if let Some(space) = self.glyph_info(' ') {
let glyph_info = GlyphInfo {
advance_width: crate::text::TAB_SIZE as f32 * space.advance_width,
..GlyphInfo::default()
..space
};
self.glyph_info_cache.write().insert(c, glyph_info);
return Some(glyph_info);
Expand All @@ -205,7 +198,7 @@ impl FontImpl {
let advance_width = f32::min(em / 6.0, space.advance_width * 0.5);
let glyph_info = GlyphInfo {
advance_width,
..GlyphInfo::default()
..space
};
self.glyph_info_cache.write().insert(c, glyph_info);
return Some(glyph_info);
Expand Down Expand Up @@ -255,6 +248,14 @@ impl FontImpl {
self.pixels_per_point
}

/// This is the distance from the top to the baseline.
///
/// Unit: points.
#[inline(always)]
pub fn ascent(&self) -> f32 {
self.ascent
}

fn allocate_glyph(&self, glyph_id: ab_glyph::GlyphId) -> GlyphInfo {
assert!(glyph_id.0 != 0);
use ab_glyph::{Font as _, ScaleFont};
Expand Down Expand Up @@ -305,7 +306,6 @@ impl FontImpl {
GlyphInfo {
id: glyph_id,
advance_width: advance_width_in_points,
ascent: self.ascent,
uv_rect,
}
}
Expand Down Expand Up @@ -442,7 +442,7 @@ impl Font {
}

#[inline]
pub(crate) fn glyph_info_and_font_impl(&mut self, c: char) -> (Option<&FontImpl>, GlyphInfo) {
pub(crate) fn font_impl_and_glyph_info(&mut self, c: char) -> (Option<&FontImpl>, GlyphInfo) {
if self.fonts.is_empty() {
return (None, self.replacement_glyph.1);
}
Expand Down
11 changes: 5 additions & 6 deletions crates/epaint/src/text/text_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn layout_section(
paragraph = out_paragraphs.last_mut().unwrap();
paragraph.empty_paragraph_height = line_height; // TODO(emilk): replace this hack with actually including `\n` in the glyphs?
} else {
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(chr);
let (font_impl, glyph_info) = font.font_impl_and_glyph_info(chr);
if let Some(font_impl) = font_impl {
if let Some(last_glyph_id) = last_glyph_id {
paragraph.cursor_x += font_impl.pair_kerning(last_glyph_id, glyph_info.id);
Expand All @@ -146,7 +146,7 @@ fn layout_section(
chr,
pos: pos2(paragraph.cursor_x, f32::NAN),
size: vec2(glyph_info.advance_width, line_height),
ascent: glyph_info.ascent,
ascent: font_impl.map_or(0.0, |font| font.ascent()), // Failure to find the font here would be weird
uv_rect: glyph_info.uv_rect,
section_index,
});
Expand Down Expand Up @@ -340,12 +340,12 @@ fn replace_last_glyph_with_overflow_character(
.unwrap_or_else(|| font.row_height());

let prev_glyph_id = prev_glyph.map(|prev_glyph| {
let (_, prev_glyph_info) = font.glyph_info_and_font_impl(prev_glyph.chr);
let (_, prev_glyph_info) = font.font_impl_and_glyph_info(prev_glyph.chr);
prev_glyph_info.id
});

// undo kerning with previous glyph
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(last_glyph.chr);
let (font_impl, glyph_info) = font.font_impl_and_glyph_info(last_glyph.chr);
last_glyph.pos.x -= extra_letter_spacing
+ font_impl
.zip(prev_glyph_id)
Expand All @@ -356,10 +356,9 @@ fn replace_last_glyph_with_overflow_character(

// replace the glyph
last_glyph.chr = overflow_character;
let (font_impl, glyph_info) = font.glyph_info_and_font_impl(last_glyph.chr);
let (font_impl, glyph_info) = font.font_impl_and_glyph_info(last_glyph.chr);
last_glyph.size = vec2(glyph_info.advance_width, line_height);
last_glyph.uv_rect = glyph_info.uv_rect;
last_glyph.ascent = glyph_info.ascent;

// reapply kerning
last_glyph.pos.x += extra_letter_spacing
Expand Down

0 comments on commit 8073ca6

Please sign in to comment.