omt/font/
fontpreviewer.rs

1use crate::util::OmError;
2
3use crate::font::Font;
4use crate::gfx::DrawBuffer;
5
6use image::GenericImageView;
7use minifb::{Key, Window, WindowOptions};
8use std::time::SystemTime;
9
10pub struct FontPreviewer {
11
12}
13
14const SIZE: usize = 1024;
15const WIDTH: usize = SIZE;
16const HEIGHT: usize = SIZE;
17//const GRID_SIZE: usize = 64;
18impl FontPreviewer {
19
20	pub fn preview(
21		input: &str
22	) -> Result<u32,OmError>{
23
24		let start_time = SystemTime::now();
25		let mut scale; // = 1.0;
26//		let mut frame_col: u32 = 0xa020a0ff;
27
28		let font = match Font::load( &input ) {
29			Err( e ) => { return Err( e ); },
30			Ok( f ) => f,
31		};
32
33//		println!("{:#?}", font );
34
35		{
36			// display
37
38			let mut grid_draw_buffer = DrawBuffer::new( WIDTH as u32, HEIGHT as u32);
39			let mut img_draw_buffer = DrawBuffer::new( WIDTH as u32, HEIGHT as u32);
40			let mut draw_buffer = DrawBuffer::new( WIDTH as u32, HEIGHT as u32);
41
42			let mut window = Window::new(
43				"omt-font - preview - ESC to exit",
44				WIDTH,
45				HEIGHT,
46				WindowOptions::default(),
47			)
48			.unwrap_or_else(|e| {
49				panic!("{}", e);
50			});
51
52    		window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));
53
54    		grid_draw_buffer.fill_with_grid( 64, 0xaaaaaaaa, 0x00000000 );
55
56			while window.is_open() && !window.is_key_down(Key::Escape) {
57				let now = SystemTime::now();
58				let time = now.duration_since(start_time).unwrap().as_millis();
59				let time = time as u128 as f32 / 1000.0;
60
61				let is = font.image.dimensions().0 as f32;
62				scale = is / WIDTH as f32;
63//				println!("New atlas scale: {:?}", scale );
64				img_draw_buffer.set_scale( scale );
65				draw_buffer.set_scale( scale );
66				img_draw_buffer.copy_from_draw_buffer( &grid_draw_buffer );
67				img_draw_buffer.blit_image( &font.image );
68
69/*
70				if prev_active_atlas != active_atlas {
71					match atlases.get( active_atlas ) {
72						None => {},
73						Some( a ) => {
74//							println!("New active atlas {:?}", a );
75							prev_active_atlas = active_atlas;
76							match &a.image {
77								None => {
78									// :TODO: clear buffer
79								},
80								Some( i ) => {
81									// :TODO: calculate best scale
82									scale = 0.5;
83
84									// assumption texture is square
85									let is = i.dimensions().0 as f32;
86									scale = is / WIDTH as f32;
87									println!("New atlas scale: {:?}", scale );
88									img_draw_buffer.set_scale( scale );
89									draw_buffer.set_scale( scale );
90									img_draw_buffer.copy_from_draw_buffer( &grid_draw_buffer );
91									img_draw_buffer.blit_image( &i );
92								},
93							}							
94						}						
95					}
96				}
97*/
98				let m = 0.5 + 0.5 * ( time * 1.5 ).sin();
99				let frame_col = DrawBuffer::mix_rgba( 0x808080ff, 0x802080ff, m );
100				let baseline_col = DrawBuffer::mix_rgba( 0xffffffff, 0xe0e020ff, m );
101				draw_buffer.copy_from_draw_buffer( &img_draw_buffer );
102				for g in &font.glyphs {
103					let bs = ( 5.0 * draw_buffer.get_scale() ).trunc() as u32; 
104					draw_buffer.draw_frame( g.x as i32, g.y as i32, g.width, g.height, frame_col, bs );
105				}
106				for g in &font.glyphs {
107					let bs = ( 2.0 * draw_buffer.get_scale() ).trunc() as u32; 
108					let y_offset = g.y_offset * font.image.dimensions().1 as f32;
109					println!("{:?} - {:?}", g.height, y_offset);
110					let h = g.height as f32;
111					let h = h - y_offset;
112					let h = h as u32;
113					draw_buffer.draw_frame( g.x as i32, g.y as i32, g.width, h, baseline_col, bs );
114//					draw_buffer.draw_hline( g.x, g.x + g.width, g.y + g.height - y_offset as u32, baseline_col );
115				}
116//			draw_buffer.copy_from_draw_buffer( &img_draw_buffer );
117
118			// We unwrap here as we want this code to exit if it fails. Real applications may want to handle this in a different way
119			window
120				.update_with_buffer(draw_buffer.get_data(), draw_buffer.get_width() as usize, draw_buffer.get_height() as usize )
121				.unwrap();
122			}
123
124			Ok( 0 )
125		}
126	}
127}