cosmic-text/examples/terminal/src/main.rs

109 lines
3.1 KiB
Rust
Raw Normal View History

2022-11-08 08:43:27 -07:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2023-01-04 20:03:03 -07:00
use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, SwashCache};
2022-10-27 11:15:08 -06:00
use std::cmp;
2023-01-04 20:03:03 -07:00
use termion::{color, cursor};
2022-10-27 11:15:08 -06:00
fn main() {
// A FontSystem provides access to detected system fonts, create one per application
let font_system = FontSystem::new();
// A SwashCache stores rasterized glyphs, create one per application
let mut swash_cache = SwashCache::new(&font_system);
// Text metrics indicate the font size and line height of a buffer
2022-10-31 11:24:36 -06:00
let metrics = Metrics::new(14, 20);
2022-10-27 11:15:08 -06:00
2022-10-31 11:24:36 -06:00
// A Buffer provides shaping and layout for a UTF-8 string, create one per text widget
let mut buffer = Buffer::new(&font_system, metrics);
2022-10-27 11:15:08 -06:00
// Set a size for the text buffer, in pixels
let width = 80u16;
let height = 25u16;
2022-10-31 11:24:36 -06:00
buffer.set_size(width as i32, height as i32);
2022-10-27 11:15:08 -06:00
// Attributes indicate what font to choose
let attrs = Attrs::new();
// Add some text!
2022-10-31 11:24:36 -06:00
buffer.set_text(" Hi, Rust! 🦀", attrs);
2022-10-27 11:15:08 -06:00
// Perform shaping as desired
2022-10-31 11:26:05 -06:00
buffer.shape_until_scroll();
2022-10-27 11:15:08 -06:00
// Default text color (0xFF, 0xFF, 0xFF is white)
let text_color = Color::rgb(0xFF, 0xFF, 0xFF);
// Start on a new line
println!();
// Clear buffer with black background
for _y in 0..height {
2022-10-31 11:24:36 -06:00
for _x in 0..buffer.size().0 {
2022-10-27 11:15:08 -06:00
print!(
"{} {}",
color::Bg(color::Rgb(0, 0, 0)),
color::Bg(color::Reset),
);
}
println!();
}
// Go back to start
print!("{}", cursor::Up(height));
// Print the buffer
let mut last_x = 0;
let mut last_y = 0;
2022-10-31 11:24:36 -06:00
buffer.draw(&mut swash_cache, text_color, |x, y, w, h, color| {
2022-10-27 11:15:08 -06:00
let a = color.a();
if a == 0 || x < 0 || y < 0 || w != 1 || h != 1 {
// Ignore alphas of 0, or invalid x, y coordinates, or unimplemented sizes
return;
}
// Scale by alpha (mimics blending with black)
2023-01-04 20:03:03 -07:00
let scale = |c: u8| cmp::max(0, cmp::min(255, ((c as i32) * (a as i32)) / 255)) as u8;
2022-10-27 11:15:08 -06:00
// Navigate to x coordinate
if x > last_x {
print!("{}", cursor::Right((x - last_x) as u16));
last_x = x;
} else if x < last_x {
print!("{}", cursor::Left((last_x - x) as u16));
last_x = x;
}
// Navigate to y coordinate
if y > last_y {
print!("{}", cursor::Down((y - last_y) as u16));
last_y = y;
} else if y < last_y {
print!("{}", cursor::Up((last_y - y) as u16));
last_y = y;
}
// Print a space with the expected color as the background
print!(
"{} {}",
color::Bg(color::Rgb(
scale(color.r()),
2022-11-08 08:43:27 -07:00
scale(color.g()),
scale(color.b()),
2022-10-27 11:15:08 -06:00
)),
color::Bg(color::Reset),
);
// Printing a space increases x coordinate
last_x += 1;
});
// Skip over output
if last_x > 0 {
print!("{}", cursor::Left(last_x as u16));
}
if (last_y as u16) < height {
print!("{}", cursor::Down(height - last_y as u16));
}
}