Skip to content

Commit 3d7c634

Browse files
emilkhacknus
authored andcommitted
Add improved pixel alignment test with alternating white/black lines (emilk#4537)
This is a good test, because you will have obvious darkening/lightening when this fails, and/or Moiré patterns. <img width="490" alt="image" src="https://github.com/emilk/egui/assets/1148717/bee74b9f-2529-4544-9102-2ee273eade2d">
1 parent 6095196 commit 3d7c634

File tree

1 file changed

+65
-8
lines changed

1 file changed

+65
-8
lines changed

crates/egui_demo_lib/src/rendering_test.rs

+65-8
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,27 @@ impl TextureManager {
398398
/// Requires eyes and a magnifying glass to verify.
399399
pub fn pixel_test(ui: &mut Ui) {
400400
ui.heading("Pixel alignment test");
401+
ui.label("If anything is blurry, then everything will be blurry, including text.");
402+
ui.label("You might need a magnifying glass to check this test.");
403+
404+
if cfg!(target_arch = "wasm32") {
405+
ui.label("Make sure these test pass even when you zoom in/out and resize the browser.");
406+
}
407+
408+
ui.add_space(4.0);
409+
410+
pixel_test_lines(ui);
411+
412+
ui.add_space(4.0);
413+
414+
pixel_test_squares(ui);
415+
}
416+
417+
fn pixel_test_squares(ui: &mut Ui) {
401418
ui.label("The first square should be exactly one physical pixel big.");
402419
ui.label("They should be exactly one physical pixel apart.");
403420
ui.label("Each subsequent square should be one physical pixel larger than the previous.");
404421
ui.label("They should be perfectly aligned to the physical pixel grid.");
405-
ui.label("If these squares are blurry, everything will be blurry, including text.");
406-
ui.label("You might need a magnifying glass to check this test.");
407422

408423
let color = if ui.style().visuals.dark_mode {
409424
egui::Color32::WHITE
@@ -412,6 +427,7 @@ pub fn pixel_test(ui: &mut Ui) {
412427
};
413428

414429
let pixels_per_point = ui.ctx().pixels_per_point();
430+
415431
let num_squares = (pixels_per_point * 10.0).round().max(10.0) as u32;
416432
let size_pixels = vec2(
417433
((num_squares + 1) * (num_squares + 2) / 2) as f32,
@@ -427,17 +443,58 @@ pub fn pixel_test(ui: &mut Ui) {
427443
.ceil();
428444
for size in 1..=num_squares {
429445
let rect_points = Rect::from_min_size(
430-
Pos2::new(
431-
cursor_pixel.x / pixels_per_point,
432-
cursor_pixel.y / pixels_per_point,
433-
),
434-
Vec2::splat(size as f32) / pixels_per_point,
446+
Pos2::new(cursor_pixel.x, cursor_pixel.y),
447+
Vec2::splat(size as f32),
435448
);
436-
painter.rect_filled(rect_points, 0.0, color);
449+
painter.rect_filled(rect_points / pixels_per_point, 0.0, color);
437450
cursor_pixel.x += (1 + size) as f32;
438451
}
439452
}
440453

454+
fn pixel_test_lines(ui: &mut Ui) {
455+
let pixels_per_point = ui.ctx().pixels_per_point();
456+
let n = (96.0 * pixels_per_point) as usize;
457+
458+
ui.label("The lines should be exactly one physical pixel wide, one physical pixel apart.");
459+
ui.label("They should be perfectly white and black.");
460+
461+
let hspace_px = pixels_per_point * 4.0;
462+
463+
let size_px = Vec2::new(2.0 * n as f32 + hspace_px, n as f32);
464+
let size_points = size_px / pixels_per_point + Vec2::splat(2.0);
465+
let (response, painter) = ui.allocate_painter(size_points, Sense::hover());
466+
467+
let mut cursor_px = Pos2::new(
468+
response.rect.min.x * pixels_per_point,
469+
response.rect.min.y * pixels_per_point,
470+
)
471+
.ceil();
472+
473+
// Vertical stripes:
474+
for x in 0..n / 2 {
475+
let rect_px = Rect::from_min_size(
476+
Pos2::new(cursor_px.x + 2.0 * x as f32, cursor_px.y),
477+
Vec2::new(1.0, n as f32),
478+
);
479+
painter.rect_filled(rect_px / pixels_per_point, 0.0, egui::Color32::WHITE);
480+
let rect_px = rect_px.translate(vec2(1.0, 0.0));
481+
painter.rect_filled(rect_px / pixels_per_point, 0.0, egui::Color32::BLACK);
482+
}
483+
484+
cursor_px.x += n as f32 + hspace_px;
485+
486+
// Horizontal stripes:
487+
for y in 0..n / 2 {
488+
let rect_px = Rect::from_min_size(
489+
Pos2::new(cursor_px.x, cursor_px.y + 2.0 * y as f32),
490+
Vec2::new(n as f32, 1.0),
491+
);
492+
painter.rect_filled(rect_px / pixels_per_point, 0.0, egui::Color32::WHITE);
493+
let rect_px = rect_px.translate(vec2(0.0, 1.0));
494+
painter.rect_filled(rect_px / pixels_per_point, 0.0, egui::Color32::BLACK);
495+
}
496+
}
497+
441498
fn blending_and_feathering_test(ui: &mut Ui) {
442499
ui.label("The left side shows how lines of different widths look.");
443500
ui.label("The right side tests text rendering at different opacities and sizes.");

0 commit comments

Comments
 (0)