feat: shape and antialias Tessera text with system fonts
Verify Tessera GUI / verify (push) Successful in 13m28s
Verify Tessera GUI / verify (push) Successful in 13m28s
This commit is contained in:
+37
-35
@@ -1,8 +1,7 @@
|
||||
use font8x8::{BASIC_FONTS, UnicodeFonts};
|
||||
|
||||
use crate::{
|
||||
display_list::{Color, DisplayList, DrawCommand},
|
||||
geometry::{Point, Rect},
|
||||
text::{self, TextStyle},
|
||||
};
|
||||
|
||||
pub struct SoftwareRenderer;
|
||||
@@ -32,9 +31,9 @@ impl SoftwareRenderer {
|
||||
DrawCommand::Text {
|
||||
origin,
|
||||
value,
|
||||
scale,
|
||||
style,
|
||||
color,
|
||||
} => canvas.draw_text(*origin, value, *scale, *color),
|
||||
} => canvas.draw_text(*origin, value, *style, *color),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,29 +87,12 @@ impl Canvas<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_text(&mut self, origin: Point, value: &str, scale: u32, color: Color) {
|
||||
let scale = scale.max(1) as i32;
|
||||
let mut cursor_x = origin.x.round() as i32;
|
||||
let baseline_y = origin.y.round() as i32;
|
||||
|
||||
for character in value.chars() {
|
||||
if let Some(glyph) = BASIC_FONTS.get(character) {
|
||||
for (row, byte) in glyph.iter().enumerate() {
|
||||
for column in 0..8 {
|
||||
if byte & (1 << column) != 0 {
|
||||
self.fill_pixels(
|
||||
cursor_x + column * scale,
|
||||
baseline_y + row as i32 * scale,
|
||||
scale,
|
||||
scale,
|
||||
color,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor_x += 8 * scale;
|
||||
}
|
||||
fn draw_text(&mut self, origin: Point, value: &str, style: TextStyle, color: Color) {
|
||||
let x = origin.x.round() as i32;
|
||||
let y = origin.y.round() as i32;
|
||||
text::draw(value, style, color, |dx, dy, pixel| {
|
||||
self.blend_pixel(x + dx, y + dy, pixel)
|
||||
});
|
||||
}
|
||||
|
||||
fn clamped_bounds(&self, rect: Rect) -> (i32, i32, i32, i32) {
|
||||
@@ -122,14 +104,6 @@ impl Canvas<'_> {
|
||||
)
|
||||
}
|
||||
|
||||
fn fill_pixels(&mut self, x: i32, y: i32, width: i32, height: i32, color: Color) {
|
||||
for py in y.max(0)..(y + height).min(self.height as i32) {
|
||||
for px in x.max(0)..(x + width).min(self.width as i32) {
|
||||
self.blend_pixel(px, py, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn blend_pixel(&mut self, x: i32, y: i32, source: Color) {
|
||||
if x < 0 || y < 0 || x >= self.width as i32 || y >= self.height as i32 || source.alpha == 0
|
||||
{
|
||||
@@ -192,4 +166,32 @@ mod tests {
|
||||
assert_eq!(pixels[5], 0x00ff0000);
|
||||
assert_eq!(pixels[0], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_edges_are_antialiased_and_respect_opacity() {
|
||||
let mut list = DisplayList::default();
|
||||
list.push(DrawCommand::Clear(Color::rgb(0, 0, 0)));
|
||||
list.push(DrawCommand::Text {
|
||||
origin: Point::new(2.0, 2.0),
|
||||
value: "Hello 中文".into(),
|
||||
style: TextStyle::ui(1).scaled(1.25),
|
||||
color: Color::rgba(255, 255, 255, 128),
|
||||
});
|
||||
let mut pixels = vec![0; 256 * 48];
|
||||
SoftwareRenderer::render(&list, &mut pixels, 256, 48);
|
||||
assert!(pixels.iter().any(|pixel| *pixel > 0));
|
||||
assert!(pixels.iter().all(|pixel| (pixel & 255) <= 128));
|
||||
assert!(pixels.iter().any(|pixel| (1..128).contains(&(pixel & 255))));
|
||||
|
||||
let mut invisible = DisplayList::default();
|
||||
invisible.push(DrawCommand::Text {
|
||||
origin: Point::new(2.0, 2.0),
|
||||
value: "Hello 中文".into(),
|
||||
style: TextStyle::ui(1),
|
||||
color: Color::rgba(255, 255, 255, 0),
|
||||
});
|
||||
let mut pixels = vec![0; 256 * 48];
|
||||
SoftwareRenderer::render(&invisible, &mut pixels, 256, 48);
|
||||
assert!(pixels.iter().all(|pixel| *pixel == 0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user