Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force disable kerning for monospace text #783

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/before_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ main() {
local src=$(pwd) \
stage=

if [ "$OS_NAME" = "macOS-latest" ]; then
if [[ $OS_NAME =~ ^macos\-.*$ ]]; then
stage=$(mktemp -d -t tmp)
else
stage=$(mktemp -d)
Expand Down
145 changes: 76 additions & 69 deletions src/rendering/default_text_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use super::{FontKind, PathBuilder, Rgba, SharedOwnership, TEXT_FONT, TIMER_FONT}

mod color_font;

type CachedGlyph<P> = (f32, Vec<(Option<Rgba>, P)>);
struct CachedGlyph<P> {
scale: f32,
paths: Vec<(Option<Rgba>, P)>,
unkerned_x_advance: f32,
}

/// The text engine allows you to create fonts and manage text labels. That way
/// the underlying renderer doesn't by itself need to be able to render text.
Expand Down Expand Up @@ -239,49 +243,44 @@ impl<P: SharedOwnership> TextEngine<P> {
} else {
glyphs.next()
} {
let (scale, layer_glyphs) = self
.glyph_cache
.entry((glyph.font_id, glyph.glyph_id))
.or_insert_with(|| {
let font = self.font_system.get_font(glyph.font_id).unwrap();
let mut glyphs = Vec::new();
let color_tables = ColorTables::new(font.rustybuzz());
let glyph = GlyphId(glyph.glyph_id);
color_font::iter_colored_glyphs(
&color_tables,
0,
glyph,
|glyph, color| {
let mut builder = GlyphBuilder(path_builder());
font.rustybuzz().outline_glyph(glyph, &mut builder);
let path = builder.0.finish();
glyphs.push((color.map(|c| c.to_array()), path));
},
);
(f32::recip(font.rustybuzz().units_per_em() as _), glyphs)
});
let cached_glyph = cache_glyph(
&mut self.glyph_cache,
&mut self.font_system,
glyph.font_id,
glyph.glyph_id,
&mut path_builder,
);

// FIXME: We use the x advance of the individual
// glyph to remove any kerning that happened during
// shaping. This is a workaround for the fact that
// cosmic-text doesn't provide a way to turn off
// kerning (and / or enable tabular nums) at the
// moment.
// https://github.com/pop-os/cosmic-text/issues/229
let unkerned_x_advance = cached_glyph.unkerned_x_advance;

let (x_advance, x_offset) = if monotonic
.digit_glyphs
.contains(&(glyph.font_id, glyph.glyph_id))
{
(
monotonic.digit_width,
0.5 * (monotonic.digit_width - glyph.x_advance)
0.5 * (monotonic.digit_width - unkerned_x_advance)
+ glyph.x_offset,
)
} else {
(glyph.x_advance, glyph.x_offset)
(unkerned_x_advance, glyph.x_offset)
};

label
.glyphs
.extend(layer_glyphs.iter().map(|(color, path)| Glyph {
.extend(cached_glyph.paths.iter().map(|(color, path)| Glyph {
color: *color,
x: x + x_offset,
y: y - glyph.y_offset,
path: path.share(),
scale: *scale,
scale: cached_glyph.scale,
}));

x += x_advance;
Expand All @@ -304,36 +303,22 @@ impl<P: SharedOwnership> TextEngine<P> {
} else {
glyphs.next()
} {
let (scale, layer_glyphs) = self
.glyph_cache
.entry((glyph.font_id, glyph.glyph_id))
.or_insert_with(|| {
let font = self.font_system.get_font(glyph.font_id).unwrap();
let mut glyphs = Vec::new();
let color_tables = ColorTables::new(font.rustybuzz());
let glyph = GlyphId(glyph.glyph_id);
color_font::iter_colored_glyphs(
&color_tables,
0,
glyph,
|glyph, color| {
let mut builder = GlyphBuilder(path_builder());
font.rustybuzz().outline_glyph(glyph, &mut builder);
let path = builder.0.finish();
glyphs.push((color.map(|c| c.to_array()), path));
},
);
(f32::recip(font.rustybuzz().units_per_em() as _), glyphs)
});
let cached_glyph = cache_glyph(
&mut self.glyph_cache,
&mut self.font_system,
glyph.font_id,
glyph.glyph_id,
&mut path_builder,
);

label
.glyphs
.extend(layer_glyphs.iter().map(|(color, path)| Glyph {
.extend(cached_glyph.paths.iter().map(|(color, path)| Glyph {
color: *color,
x: glyph_x + glyph.x_offset,
y: glyph_y - glyph.y_offset,
path: path.share(),
scale: *scale,
scale: cached_glyph.scale,
}));

glyph_x += glyph.x_advance;
Expand Down Expand Up @@ -365,33 +350,24 @@ impl<P: SharedOwnership> TextEngine<P> {
.unwrap_or_default();
label.glyphs.drain(last_index..);

let font_id = font.ellipsis_font_id;
let glyph_id = font.ellipsis_glyph_id;
let (scale, layer_glyphs) = self
.glyph_cache
.entry((font_id, glyph_id))
.or_insert_with(|| {
let font = self.font_system.get_font(font_id).unwrap();
let mut glyphs = Vec::new();
let color_tables = ColorTables::new(font.rustybuzz());
let glyph = GlyphId(glyph_id);
color_font::iter_colored_glyphs(&color_tables, 0, glyph, |glyph, color| {
let mut builder = GlyphBuilder(path_builder());
font.rustybuzz().outline_glyph(glyph, &mut builder);
let path = builder.0.finish();
glyphs.push((color.map(|c| c.to_array()), path));
});
(f32::recip(font.rustybuzz().units_per_em() as _), glyphs)
});
// FIXME: Test RTL text.

let cached_glyph = cache_glyph(
&mut self.glyph_cache,
&mut self.font_system,
font.ellipsis_font_id,
font.ellipsis_glyph_id,
&mut path_builder,
);

label
.glyphs
.extend(layer_glyphs.iter().map(|(color, path)| Glyph {
.extend(cached_glyph.paths.iter().map(|(color, path)| Glyph {
color: *color,
x,
y,
path: path.share(),
scale: *scale,
scale: cached_glyph.scale,
}));
x += font.ellipsis_width;
}
Expand All @@ -401,6 +377,37 @@ impl<P: SharedOwnership> TextEngine<P> {
}
}

fn cache_glyph<'gc, P, PB: PathBuilder<Path = P>>(
glyph_cache: &'gc mut HashMap<(ID, u16), CachedGlyph<P>>,
font_system: &mut FontSystem,
font_id: ID,
glyph_id: u16,
path_builder: &mut impl FnMut() -> PB,
) -> &'gc mut CachedGlyph<P> {
glyph_cache.entry((font_id, glyph_id)).or_insert_with(|| {
let font = font_system.get_font(font_id).unwrap();
let font = font.rustybuzz();
let mut paths = Vec::new();
let color_tables = ColorTables::new(font);
let glyph = GlyphId(glyph_id);
color_font::iter_colored_glyphs(&color_tables, 0, glyph, |glyph, color| {
let mut builder = GlyphBuilder(path_builder());
font.outline_glyph(glyph, &mut builder);
let path = builder.0.finish();
paths.push((color.map(|c| c.to_array()), path));
});
let scale = f32::recip(font.units_per_em() as _);
CachedGlyph {
scale,
paths,
unkerned_x_advance: font
.glyph_hor_advance(glyph)
.map(|v| v as f32 * scale)
.unwrap_or_default(),
}
})
}

struct MonotonicInfo {
digit_glyphs: [(ID, u16); 10],
digit_width: f32,
Expand Down
42 changes: 21 additions & 21 deletions tests/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn default() {
check(
&state,
&image_cache,
"670e0e09bf3dbfed",
"ff7a86855648dd1b",
"a98ef51c25f115fe",
"08cbb11aa1719035",
"default",
);
}
Expand Down Expand Up @@ -122,8 +122,8 @@ fn font_fallback() {
check(
&state,
&image_cache,
"5a0e8df5e424a5cf",
"23e71509050b368f",
"d908fda633352ba5",
"299f188d2a8ccf5d",
"font_fallback",
);
}
Expand All @@ -138,8 +138,8 @@ fn actual_split_file() {
check(
&layout.state(&mut image_cache, &timer.snapshot()),
&image_cache,
"cd9735cf9575f503",
"442e5df389ce2add",
"86ccc8595787d41b",
"ec4e4283ff1aaf7c",
"actual_split_file",
);
}
Expand All @@ -155,8 +155,8 @@ fn wsplit() {
&layout.state(&mut image_cache, &timer.snapshot()),
&image_cache,
[250, 300],
"9c69454a9258e768",
"d1eebea6860d57c3",
"be70116db88c23c1",
"cf399969d8ac9baf",
"wsplit",
);
}
Expand Down Expand Up @@ -211,17 +211,17 @@ fn all_components() {
&state,
&image_cache,
[300, 800],
"a4a9f27478717418",
"2294184b8afcea27",
"9c3a68e4c9c6b73c",
"f94f2b05c06f8d16",
"all_components",
);

check_dims(
&state,
&image_cache,
[150, 800],
"0ecd0bad25453ff6",
"b4186e90d4a93b4a",
"37cc2602ef0402a8",
"1ceb8d7ff13d1741",
"all_components_thin",
);
}
Expand All @@ -248,8 +248,8 @@ fn score_split() {
&state,
&image_cache,
[300, 400],
"6ec6913f5ace6ab6",
"1acd4eb5a81f4665",
"f5dadfe58e621e7b",
"c7fec6cb25b993bc",
"score_split",
);
}
Expand Down Expand Up @@ -289,8 +289,8 @@ fn subsplits_layout() {
&layout.state(&mut image_cache, &timer.snapshot()),
&image_cache,
[300, 800],
"57165de23ce37b9c",
"0984cf3a14c0edef",
"8694d76628ff63f8",
"f2bcd20608fb35df",
"subsplits_layout",
);
}
Expand All @@ -317,7 +317,7 @@ fn display_two_rows() {
&image_cache,
[200, 100],
"d174c2f9a0c54d66",
"1cf9537c6fe5ed76",
"12b6ab31502b1baa",
"display_two_rows",
);
}
Expand All @@ -343,8 +343,8 @@ fn single_line_title() {
&layout.state(&mut image_cache, &timer.snapshot()),
&image_cache,
[300, 60],
"5f0a41091c33ecad",
"229c2e381e03328a",
"db305e29ec814f33",
"1848cdb01676cfec",
"single_line_title",
);
}
Expand Down Expand Up @@ -381,8 +381,8 @@ fn horizontal() {
&layout.state(&mut image_cache, &timer.snapshot()),
&image_cache,
[1500, 40],
"987157e649936cbb",
"ca63a8972570fac6",
"944a515a48627b31",
"65b9819a3e903307",
"horizontal",
);
}
Expand Down
Loading