2022-10-03 11:45:46 -06:00
|
|
|
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
|
2022-10-03 13:13:35 -06:00
|
|
|
use std::{
|
|
|
|
|
cmp,
|
|
|
|
|
time::Instant,
|
|
|
|
|
};
|
2022-10-03 11:45:46 -06:00
|
|
|
|
|
|
|
|
struct Font<'a> {
|
|
|
|
|
data: &'a [u8],
|
|
|
|
|
pub rustybuzz: rustybuzz::Face<'a>,
|
|
|
|
|
pub rusttype: rusttype::Font<'a>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> Font<'a> {
|
|
|
|
|
pub fn new(data: &'a [u8], index: u32) -> Option<Self> {
|
|
|
|
|
Some(Self {
|
|
|
|
|
data,
|
|
|
|
|
rustybuzz: rustybuzz::Face::from_slice(data, index)?,
|
|
|
|
|
rusttype: rusttype::Font::try_from_bytes_and_index(data, index)?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let mut window = Window::new_flags(
|
|
|
|
|
-1, -1,
|
|
|
|
|
1024, 768,
|
|
|
|
|
"COSMIC TEXT",
|
|
|
|
|
&[WindowFlag::Resizable]
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
2022-10-03 12:00:17 -06:00
|
|
|
#[cfg(feature = "mono")]
|
|
|
|
|
let text = include_str!("../res/mono.txt");
|
|
|
|
|
#[cfg(not(feature = "mono"))]
|
|
|
|
|
let text = include_str!("../res/proportional.txt");
|
2022-10-03 11:45:46 -06:00
|
|
|
|
|
|
|
|
let bg_color = Color::rgb(0x34, 0x34, 0x34);
|
2022-10-03 13:04:55 -06:00
|
|
|
let hover_color = Color::rgb(0x80, 0x80, 0x80);
|
2022-10-03 11:45:46 -06:00
|
|
|
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
|
|
|
|
|
let font_sizes = [
|
2022-10-03 13:04:55 -06:00
|
|
|
(10, 14), // Caption
|
|
|
|
|
(14, 20), // Body
|
|
|
|
|
(20, 28), // Title 4
|
|
|
|
|
(24, 32), // Title 3
|
|
|
|
|
(28, 36), // Title 2
|
|
|
|
|
(32, 44), // Title 1
|
2022-10-03 11:45:46 -06:00
|
|
|
];
|
|
|
|
|
let font_size_default = 2; // Title 4
|
|
|
|
|
let mut font_size_i = font_size_default;
|
|
|
|
|
|
2022-10-03 12:00:17 -06:00
|
|
|
#[cfg(feature = "mono")]
|
2022-10-03 11:45:46 -06:00
|
|
|
let font = Font::new(include_bytes!("../../../res/FreeFont/FreeMono.ttf"), 0).unwrap();
|
2022-10-03 12:00:17 -06:00
|
|
|
#[cfg(not(feature = "mono"))]
|
|
|
|
|
let font = Font::new(include_bytes!("../../../res/FreeFont/FreeSerif.ttf"), 0).unwrap();
|
2022-10-03 13:04:55 -06:00
|
|
|
let font_scale = font.rustybuzz.units_per_em();
|
2022-10-03 11:45:46 -06:00
|
|
|
|
2022-10-03 13:13:35 -06:00
|
|
|
let mut shaped_lines = Vec::new();
|
|
|
|
|
for line in text.lines() {
|
|
|
|
|
let mut buffer = rustybuzz::UnicodeBuffer::new();
|
|
|
|
|
buffer.push_str(line);
|
|
|
|
|
buffer.guess_segment_properties();
|
|
|
|
|
println!("{:?}: {}", buffer.script(), line);
|
|
|
|
|
|
|
|
|
|
let glyph_buffer = rustybuzz::shape(&font.rustybuzz, &[], buffer);
|
|
|
|
|
let glyph_infos = glyph_buffer.glyph_infos();
|
|
|
|
|
let glyph_positions = glyph_buffer.glyph_positions();
|
|
|
|
|
|
|
|
|
|
let mut shaped = Vec::new();
|
|
|
|
|
for (info, pos) in glyph_infos.iter().zip(glyph_positions.iter()) {
|
|
|
|
|
println!(" {:?} {:?}", info, pos);
|
|
|
|
|
|
|
|
|
|
shaped.push((*info, *pos));
|
|
|
|
|
}
|
|
|
|
|
shaped_lines.push(shaped);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 13:04:55 -06:00
|
|
|
let mut glyph_lines = Vec::new();
|
|
|
|
|
let mut mouse_x = -1;
|
|
|
|
|
let mut mouse_y = -1;
|
2022-10-03 11:45:46 -06:00
|
|
|
let mut redraw = true;
|
2022-10-03 13:04:55 -06:00
|
|
|
let mut reglyph = true;
|
2022-10-03 11:45:46 -06:00
|
|
|
let mut scroll = 0;
|
|
|
|
|
loop {
|
2022-10-03 13:04:55 -06:00
|
|
|
let (font_size, line_height) = font_sizes[font_size_i];
|
2022-10-03 11:45:46 -06:00
|
|
|
|
2022-10-03 13:04:55 -06:00
|
|
|
if reglyph {
|
2022-10-03 13:13:35 -06:00
|
|
|
let instant = Instant::now();
|
2022-10-03 11:45:46 -06:00
|
|
|
|
2022-10-03 13:04:55 -06:00
|
|
|
glyph_lines.clear();
|
2022-10-03 13:13:35 -06:00
|
|
|
for shaped in shaped_lines.iter() {
|
2022-10-03 13:04:55 -06:00
|
|
|
let mut glyphs = Vec::new();
|
2022-10-03 13:13:35 -06:00
|
|
|
for (info, pos) in shaped.iter() {
|
2022-10-03 13:04:55 -06:00
|
|
|
let glyph = font.rusttype.glyph(rusttype::GlyphId(info.glyph_id as u16))
|
|
|
|
|
.scaled(rusttype::Scale::uniform(font_size as f32))
|
|
|
|
|
.positioned(rusttype::point(
|
|
|
|
|
(font_size * pos.x_offset) as f32 / font_scale as f32,
|
|
|
|
|
(font_size * pos.y_offset) as f32 / font_scale as f32
|
|
|
|
|
));
|
2022-10-03 13:23:27 -06:00
|
|
|
glyphs.push((
|
|
|
|
|
(font_size * pos.x_advance) / font_scale,
|
|
|
|
|
(font_size * pos.y_advance) / font_scale,
|
|
|
|
|
glyph
|
|
|
|
|
));
|
2022-10-03 13:04:55 -06:00
|
|
|
}
|
|
|
|
|
glyph_lines.push(glyphs);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 13:13:35 -06:00
|
|
|
redraw = true;
|
2022-10-03 13:04:55 -06:00
|
|
|
reglyph = false;
|
2022-10-03 13:13:35 -06:00
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
println!("reglyph: {:?}", duration);
|
2022-10-03 13:04:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if redraw {
|
2022-10-03 13:13:35 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
2022-10-03 13:04:55 -06:00
|
|
|
window.set(bg_color);
|
|
|
|
|
|
|
|
|
|
let window_lines = (window.height() as i32 + line_height - 1) / line_height;
|
|
|
|
|
scroll = cmp::max(0, cmp::min(
|
|
|
|
|
glyph_lines.len() as i32 - (window_lines - 1),
|
|
|
|
|
scroll
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
let mut line_y = line_height;
|
|
|
|
|
for glyph_line in glyph_lines.iter().skip(scroll as usize) {
|
|
|
|
|
if line_y >= window.height() as i32 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 11:45:46 -06:00
|
|
|
let mut glyph_x = 0i32;
|
|
|
|
|
let mut glyph_y = line_y;
|
2022-10-03 13:23:27 -06:00
|
|
|
for (advance_x, advance_y, glyph) in glyph_line.iter() {
|
2022-10-03 13:04:55 -06:00
|
|
|
if let Some(bb) = glyph.pixel_bounding_box() {
|
2022-10-03 12:10:23 -06:00
|
|
|
//TODO: make wrapping optional
|
|
|
|
|
if glyph_x + bb.max.x >= window.width() as i32 {
|
|
|
|
|
line_y += line_height;
|
|
|
|
|
|
|
|
|
|
glyph_x = 0;
|
|
|
|
|
glyph_y = line_y;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 13:23:27 -06:00
|
|
|
if mouse_x >= glyph_x
|
|
|
|
|
&& mouse_x < glyph_x + advance_x
|
|
|
|
|
&& mouse_y >= line_y - font_size
|
2022-10-03 14:18:23 -06:00
|
|
|
&& mouse_y < line_y - font_size + line_height
|
2022-10-03 13:23:27 -06:00
|
|
|
{
|
2022-10-03 14:18:23 -06:00
|
|
|
//TODO: this highlights only one character of combinations
|
2022-10-03 13:23:27 -06:00
|
|
|
window.rect(glyph_x, line_y - font_size, *advance_x as u32, line_height as u32, hover_color);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 13:04:55 -06:00
|
|
|
let x = glyph_x + bb.min.x;
|
|
|
|
|
let y = glyph_y + bb.min.y;
|
|
|
|
|
glyph.draw(|off_x, off_y, v| {
|
2022-10-03 11:45:46 -06:00
|
|
|
let c = (v * 255.0) as u32;
|
2022-10-03 13:04:55 -06:00
|
|
|
window.pixel(x + off_x as i32, y + off_y as i32, Color{
|
2022-10-03 11:45:46 -06:00
|
|
|
data: c << 24 | (font_color.data & 0x00FF_FFFF)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 13:23:27 -06:00
|
|
|
glyph_x += advance_x;
|
|
|
|
|
glyph_y += advance_y;
|
2022-10-03 11:45:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line_y += line_height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.sync();
|
|
|
|
|
|
|
|
|
|
redraw = false;
|
2022-10-03 13:13:35 -06:00
|
|
|
|
|
|
|
|
let duration = instant.elapsed();
|
|
|
|
|
println!("redraw: {:?}", duration);
|
2022-10-03 11:45:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for event in window.events() {
|
|
|
|
|
match event.to_option() {
|
|
|
|
|
EventOption::Key(event) => if event.pressed {
|
|
|
|
|
match event.scancode {
|
|
|
|
|
orbclient::K_0 => {
|
|
|
|
|
font_size_i = font_size_default;
|
2022-10-03 13:04:55 -06:00
|
|
|
reglyph = true;
|
2022-10-03 11:45:46 -06:00
|
|
|
},
|
|
|
|
|
orbclient::K_MINUS => if font_size_i > 0 {
|
|
|
|
|
font_size_i -= 1;
|
2022-10-03 13:04:55 -06:00
|
|
|
reglyph = true;
|
2022-10-03 11:45:46 -06:00
|
|
|
},
|
|
|
|
|
orbclient::K_EQUALS => if font_size_i + 1 < font_sizes.len() {
|
|
|
|
|
font_size_i += 1;
|
2022-10-03 13:04:55 -06:00
|
|
|
reglyph = true;
|
2022-10-03 11:45:46 -06:00
|
|
|
},
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
},
|
2022-10-03 13:04:55 -06:00
|
|
|
EventOption::Mouse(event) => {
|
|
|
|
|
mouse_x = event.x;
|
|
|
|
|
mouse_y = event.y;
|
|
|
|
|
redraw = true;
|
|
|
|
|
},
|
2022-10-03 11:45:46 -06:00
|
|
|
EventOption::Resize(_) => {
|
|
|
|
|
redraw = true;
|
|
|
|
|
},
|
|
|
|
|
EventOption::Scroll(event) => {
|
|
|
|
|
scroll -= event.y * 3;
|
|
|
|
|
redraw = true;
|
|
|
|
|
},
|
|
|
|
|
EventOption::Quit(_) => return,
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|