1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Replace ab_glyph with Skrifa + vello_cpu; enable font hinting (#7694)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes N/A
* [x] I have followed the instructions in the PR template

I'll probably come back to this and clean it up a bit. This PR
reimplements ab_glyph's functionality on top of Skrifa, a somewhat
lower-level font API that's being used in Chrome now.

Skrifa doesn't perform rasterization itself, so I'm using
[vello_cpu](https://github.com/linebender/vello) from the Linebender
project for rasterization. It's still in its early days, but I believe
it's already quite fast. It also supports color and gradient fills, so
color emoji support will be easier.

Skrifa also supports font hinting, which should make text look a bit
nicer / less blurry.

Here's the current ab_glyph rendering:

<img width="1592" height="1068" alt="image"
src="https://github.com/user-attachments/assets/2385b66e-23f8-4c6e-b8c2-ea90e0eea4e4"
/>

Here's Skrifa *without* hinting--it looks almost identical, but there
are some subpixel differences, probably due to rasterizer behavior:

<img width="1592" height="1068" alt="image"
src="https://github.com/user-attachments/assets/a815f3e9-65ac-4940-bc00-571177bef53d"
/>

Here's Skrifa  *with* hinting:

<img width="1592" height="1068" alt="image"
src="https://github.com/user-attachments/assets/d6cc0669-3537-4377-bba9-ed5ef09664db"
/>

Hinting does make the horizontal strokes look a bit bolder, which makes
me wonder once again about increasing the font weight from "light" to
"regular".

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
valadaptive
2025-12-06 10:11:33 -05:00
committed by GitHub
parent 2174b309bd
commit 609dd2d28e
172 changed files with 928 additions and 643 deletions

View File

@@ -1,7 +1,7 @@
use ecolor::Color32;
use emath::{Rect, remap_clamp};
use crate::{AlphaFromCoverage, ColorImage, ImageDelta};
use crate::{ColorImage, ImageDelta, TextOptions};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct Rectu {
@@ -75,11 +75,11 @@ pub struct TextureAtlas {
discs: Vec<PrerasterizedDisc>,
/// Controls how to convert glyph coverage to alpha.
pub(crate) text_alpha_from_coverage: AlphaFromCoverage,
options: TextOptions,
}
impl TextureAtlas {
pub fn new(size: [usize; 2], text_alpha_from_coverage: AlphaFromCoverage) -> Self {
pub fn new(size: [usize; 2], options: TextOptions) -> Self {
assert!(size[0] >= 1024, "Tiny texture atlas");
let mut atlas = Self {
image: ColorImage::filled(size, Color32::TRANSPARENT),
@@ -88,7 +88,7 @@ impl TextureAtlas {
row_height: 0,
overflowed: false,
discs: vec![], // will be filled in below
text_alpha_from_coverage,
options,
};
// Make the top left pixel fully white for `WHITE_UV`, i.e. painting something with solid color:
@@ -121,7 +121,7 @@ impl TextureAtlas {
let coverage =
remap_clamp(distance_to_center, (r - 0.5)..=(r + 0.5), 1.0..=0.0);
image[((x as i32 + hw + dx) as usize, (y as i32 + hw + dy) as usize)] =
text_alpha_from_coverage.color_from_coverage(coverage);
options.alpha_from_coverage.color_from_coverage(coverage);
}
}
atlas.discs.push(PrerasterizedDisc {
@@ -138,6 +138,10 @@ impl TextureAtlas {
atlas
}
pub fn options(&self) -> &TextOptions {
&self.options
}
pub fn size(&self) -> [usize; 2] {
self.image.size
}