Skip to content

Commit

Permalink
egui_web: make text thicker and less pixelated (emilk#640)
Browse files Browse the repository at this point in the history
Closes emilk#516
  • Loading branch information
emilk authored and mankinskin committed Sep 29, 2021
1 parent cbfc09f commit c722234
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion egui_demo_lib/src/apps/demo/painting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Default for Painting {
fn default() -> Self {
Self {
lines: Default::default(),
stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
stroke: Stroke::new(2.0, Color32::LIGHT_BLUE), // Thin strokes looks bad on web
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions egui_web/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ All notable changes to the `egui_web` integration will be noted in this file.
### Added ⭐
* Added support for dragging and dropping files into the browser window.

### Fixed 🐛
* Made text thicker and less pixelated.


## 0.13.0 - 2021-06-24

Expand Down
3 changes: 2 additions & 1 deletion egui_web/src/webgl1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ impl crate::Painter for WebGlPainter {
}

let mut pixels: Vec<u8> = Vec::with_capacity(texture.pixels.len() * 4);
for srgba in texture.srgba_pixels() {
let font_gamma = 1.0 / 2.2; // HACK due to non-linear framebuffer blending.
for srgba in texture.srgba_pixels(font_gamma) {
pixels.push(srgba.r());
pixels.push(srgba.g());
pixels.push(srgba.b());
Expand Down
3 changes: 2 additions & 1 deletion egui_web/src/webgl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ impl crate::Painter for WebGl2Painter {
}

let mut pixels: Vec<u8> = Vec::with_capacity(texture.pixels.len() * 4);
for srgba in texture.srgba_pixels() {
let font_gamma = 1.0 / 2.2; // HACK due to non-linear framebuffer blending.
for srgba in texture.srgba_pixels(font_gamma) {
pixels.push(srgba.r());
pixels.push(srgba.g());
pixels.push(srgba.b());
Expand Down
15 changes: 12 additions & 3 deletions epaint/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ impl Texture {
}

/// Returns the textures as `sRGBA` premultiplied pixels, row by row, top to bottom.
pub fn srgba_pixels(&'_ self) -> impl Iterator<Item = super::Color32> + '_ {
///
/// `gamma` should normally be set to 1.0.
/// If you are having problems with egui text looking skinny and pixelated, try
/// setting a lower gamma, e.g. `0.5`.
pub fn srgba_pixels(&'_ self, gamma: f32) -> impl Iterator<Item = super::Color32> + '_ {
use super::Color32;
let srgba_from_luminance_lut: Vec<Color32> =
(0..=255).map(Color32::from_white_alpha).collect();

let srgba_from_luminance_lut: Vec<Color32> = (0..=255)
.map(|a| {
let a = super::color::linear_f32_from_linear_u8(a).powf(gamma);
super::Rgba::from_white_alpha(a).into()
})
.collect();
self.pixels
.iter()
.map(move |&l| srgba_from_luminance_lut[l as usize])
Expand Down

0 comments on commit c722234

Please sign in to comment.