cosmic-text/examples/editor-test/src/main.rs

180 lines
5.2 KiB
Rust
Raw Normal View History

2022-10-24 08:56:48 -06:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2022-10-25 11:40:10 -06:00
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, thread, time::{Duration, Instant}};
2022-10-25 10:17:09 -06:00
use unicode_segmentation::UnicodeSegmentation;
2022-10-25 11:40:10 -06:00
fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) {
let bg_color = Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
2022-10-25 20:49:15 -06:00
if buffer.cursor_moved {
buffer.shape_until_cursor();
buffer.cursor_moved = false;
} else {
buffer.shape_until_scroll();
}
2022-10-21 12:31:02 -06:00
if buffer.redraw {
let instant = Instant::now();
window.set(bg_color);
2022-10-25 11:40:10 -06:00
buffer.draw(swash_cache, font_color.data, |x, y, w, h, color| {
window.rect(x, y, w, h, Color { data: color });
});
window.sync();
buffer.redraw = false;
let duration = instant.elapsed();
log::debug!("redraw: {:?}", duration);
}
}
fn main() {
env_logger::init();
2022-10-21 14:58:15 -06:00
let display_scale = 1;
let font_system = FontSystem::new();
let mut window = Window::new_flags(
-1,
-1,
2022-10-25 22:10:05 -06:00
1024,
768,
&format!("COSMIC TEXT - {}", font_system.locale),
&[WindowFlag::Async],
)
.unwrap();
let font_sizes = [
TextMetrics::new(10, 14).scale(display_scale), // Caption
TextMetrics::new(14, 20).scale(display_scale), // Body
TextMetrics::new(20, 28).scale(display_scale), // Title 4
TextMetrics::new(24, 32).scale(display_scale), // Title 3
TextMetrics::new(28, 36).scale(display_scale), // Title 2
TextMetrics::new(32, 44).scale(display_scale), // Title 1
];
let font_size_default = 1; // Body
2022-10-25 16:13:07 -06:00
let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono"));
let mut buffer = TextBuffer::new(
2022-10-25 16:13:07 -06:00
&font_system,
attrs,
2022-10-21 14:58:15 -06:00
font_sizes[font_size_default]
);
buffer.set_size(
window.width() as i32,
window.height() as i32
);
2022-10-25 11:40:10 -06:00
let mut swash_cache = SwashCache::new();
let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file")
} else {
#[cfg(feature = "mono")]
let default_text = include_str!("../../../sample/mono.txt");
#[cfg(not(feature = "mono"))]
let default_text = include_str!("../../../sample/proportional.txt");
default_text.to_string()
};
2022-10-20 09:16:39 -06:00
let test_start = Instant::now();
2022-10-19 15:31:09 -06:00
//TODO: support bidi
for line in text.lines() {
2022-10-19 16:09:22 -06:00
log::debug!("Line {:?}", line);
2022-10-25 10:17:09 -06:00
for grapheme in line.graphemes(true) {
for c in grapheme.chars() {
log::trace!("Insert {:?}", c);
2022-10-19 15:31:09 -06:00
2022-10-25 10:17:09 -06:00
// Test backspace of character
{
let cursor = buffer.cursor();
buffer.action(TextAction::Insert(c));
buffer.action(TextAction::Backspace);
assert_eq!(cursor, buffer.cursor());
}
// Finally, normal insert of character
2022-10-19 15:31:09 -06:00
buffer.action(TextAction::Insert(c));
}
2022-10-25 10:17:09 -06:00
// Test delete of EGC
2022-10-19 15:31:09 -06:00
{
let cursor = buffer.cursor();
2022-10-21 12:11:28 -06:00
buffer.action(TextAction::Previous);
2022-10-19 15:31:09 -06:00
buffer.action(TextAction::Delete);
2022-10-25 10:17:09 -06:00
for c in grapheme.chars() {
buffer.action(TextAction::Insert(c));
}
2022-10-19 15:31:09 -06:00
assert_eq!(cursor, buffer.cursor());
}
2022-10-19 15:31:09 -06:00
}
// Test backspace of newline
{
let cursor = buffer.cursor();
buffer.action(TextAction::Enter);
buffer.action(TextAction::Backspace);
assert_eq!(cursor, buffer.cursor());
}
2022-10-19 15:31:09 -06:00
// Test delete of newline
{
let cursor = buffer.cursor();
buffer.action(TextAction::Enter);
2022-10-21 12:11:28 -06:00
buffer.action(TextAction::Previous);
2022-10-19 15:31:09 -06:00
buffer.action(TextAction::Delete);
assert_eq!(cursor, buffer.cursor());
}
// Finally, normal enter
buffer.action(TextAction::Enter);
2022-10-25 11:40:10 -06:00
redraw(&mut window, &mut buffer, &mut swash_cache);
for event in window.events() {
match event.to_option() {
EventOption::Quit(_) => process::exit(1),
_ => (),
}
}
}
2022-10-20 09:16:39 -06:00
let test_elapsed = test_start.elapsed();
log::info!("Test completed in {:?}", test_elapsed);
let mut wrong = 0;
let buffer_lines = buffer.text_lines();
for (line_i, line) in text.lines().enumerate() {
let buffer_line = &buffer_lines[line_i];
if buffer_line.text() != line {
log::error!("line {}: {:?} != {:?}", line_i, buffer_line.text(), line);
wrong += 1;
}
}
if wrong == 0 {
log::info!("All lines matched!");
} else {
log::error!("{} lines did not match!", wrong);
}
//TODO: make window not async?
loop {
for event in window.events() {
match event.to_option() {
EventOption::Quit(_) => process::exit(0),
_ => (),
}
}
thread::sleep(Duration::from_millis(1));
}
}