Skip to content

Commit 55e529e

Browse files
committed
Align icons and text exactly with the pixel grid
Icon textures are (or can be) optimized to be displayed in a size of 48 pixels, which is a very common size to design for among icon designers. Let's make sure we display the icons exactly as they were inteded to be shown and use a fixed size of 48 pixels for them, aligning them perfectly with the pixel grid by rounding the origin coordinates to physical pixels. When used in combination with properly aligned icons, this eliminates all the sub-pixel bluriness that can be seen on the edges of icons right now. The text rendered for the function keys also becomes a lot less blurry around the edges with this.
1 parent 210ea08 commit 55e529e

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const DFR_HEIGHT: i32 = 64;
4545
const BUTTON_SPACING_PX: i32 = 16;
4646
const BUTTON_COLOR_INACTIVE: f64 = 0.200;
4747
const BUTTON_COLOR_ACTIVE: f64 = 0.400;
48+
const ICON_SIZE: i32 = 48;
4849
const TIMEOUT_MS: i32 = 10 * 1000;
4950

5051
#[derive(Deserialize)]
@@ -83,24 +84,23 @@ impl Button {
8384
action, image: ButtonImage::Svg(svg)
8485
}
8586
}
86-
fn render(&self, c: &Context, button_left_edge: f64, button_width: f64, y_shift: f64) {
87+
fn render(&self, c: &Context, button_left_edge: f64, button_width: u64, y_shift: f64) {
8788
match &self.image {
8889
ButtonImage::Text(text) => {
8990
let extents = c.text_extents(text).unwrap();
9091
c.move_to(
91-
button_left_edge + button_width / 2.0 - extents.width() / 2.0,
92-
DFR_HEIGHT as f64 / 2.0 + extents.height() / 2.0
92+
button_left_edge + (button_width as f64 / 2.0 - extents.width() / 2.0).round(),
93+
y_shift + (DFR_HEIGHT as f64 / 2.0 + extents.height() / 2.0).round()
9394
);
9495
c.show_text(text).unwrap();
9596
},
9697
ButtonImage::Svg(svg) => {
9798
let renderer = CairoRenderer::new(&svg);
98-
let y = 0.12 * DFR_HEIGHT as f64;
99-
let size = DFR_HEIGHT as f64 - y * 2.0;
100-
let x = button_left_edge + button_width / 2.0 - size / 2.0;
99+
let x = button_left_edge + (button_width as f64 / 2.0 - (ICON_SIZE / 2) as f64).round();
100+
let y = y_shift + ((DFR_HEIGHT as f64 - ICON_SIZE as f64) / 2.0).round();
101101

102102
renderer.render_document(c,
103-
&Rectangle::new(x, y + y_shift, size, size)
103+
&Rectangle::new(x, y, ICON_SIZE as f64, ICON_SIZE as f64)
104104
).unwrap();
105105
}
106106
}
@@ -127,7 +127,7 @@ impl FunctionLayer {
127127
c.select_font_face("sans-serif", FontSlant::Normal, FontWeight::Bold);
128128
c.set_font_size(32.0);
129129
for (i, button) in self.buttons.iter().enumerate() {
130-
let left_edge = i as f64 * (button_width + BUTTON_SPACING_PX as f64) + pixel_shift_x + (PIXEL_SHIFT_WIDTH_PX / 2) as f64;
130+
let left_edge = (i as f64 * (button_width + BUTTON_SPACING_PX as f64)).floor() + pixel_shift_x + (PIXEL_SHIFT_WIDTH_PX / 2) as f64;
131131
let color = if active_buttons[i] {
132132
BUTTON_COLOR_ACTIVE
133133
} else if config.show_button_outlines {
@@ -139,7 +139,7 @@ impl FunctionLayer {
139139
// draw box with rounded corners
140140
c.new_sub_path();
141141
let left = left_edge + radius;
142-
let right = left_edge + button_width - radius;
142+
let right = (left_edge + button_width.ceil()) - radius;
143143
c.arc(
144144
right,
145145
bot,
@@ -172,7 +172,7 @@ impl FunctionLayer {
172172

173173
c.fill().unwrap();
174174
c.set_source_rgb(1.0, 1.0, 1.0);
175-
button.render(&c, left_edge, button_width, pixel_shift_y);
175+
button.render(&c, left_edge, button_width.ceil() as u64, pixel_shift_y);
176176
}
177177
}
178178
}

0 commit comments

Comments
 (0)