From b98004330bf3ef372a0f0ef5c7daf1f96fd7873a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 19 Sep 2022 10:28:06 +0200 Subject: Handle non-breaking spaces during justification --- src/library/text/shaping.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/library/text/shaping.rs b/src/library/text/shaping.rs index 9465fbd9..1bac626d 100644 --- a/src/library/text/shaping.rs +++ b/src/library/text/shaping.rs @@ -58,12 +58,12 @@ pub struct ShapedGlyph { impl ShapedGlyph { /// Whether the glyph is a space. pub fn is_space(&self) -> bool { - self.c == ' ' + matches!(self.c, ' ' | '\u{00A0}' | ' ') } /// Whether the glyph is justifiable. pub fn is_justifiable(&self) -> bool { - matches!(self.c, ' ' | ',' | ' ' | '。' | '、') + self.is_space() || matches!(self.c, ',' | '。' | '、') } } @@ -508,12 +508,14 @@ fn track_and_space(ctx: &mut ShapingContext) { .get(TextNode::SPACING) .map(|abs| Em::from_length(abs, ctx.size)); - if tracking.is_zero() && spacing.is_one() { - return; - } - let mut glyphs = ctx.glyphs.iter_mut().peekable(); while let Some(glyph) = glyphs.next() { + // Make non-breaking space same width as normal space. + if glyph.c == '\u{00A0}' { + let face = ctx.fonts.get(glyph.face_id); + glyph.x_advance -= nbsp_delta(face).unwrap_or_default(); + } + if glyph.is_space() { glyph.x_advance = spacing.relative_to(glyph.x_advance); } @@ -524,6 +526,13 @@ fn track_and_space(ctx: &mut ShapingContext) { } } +/// Difference between non-breaking and normal space. +fn nbsp_delta(face: &Face) -> Option { + let space = face.ttf().glyph_index(' ')?.0; + let nbsp = face.ttf().glyph_index('\u{00A0}')?.0; + Some(face.advance(nbsp)? - face.advance(space)?) +} + /// Resolve the font variant with `BOLD` and `ITALIC` factored in. pub fn variant(styles: StyleChain) -> FontVariant { let mut variant = FontVariant::new( -- cgit v1.2.3