use std::collections::HashMap; use gouache::{Allocator, Color, Vec2, Mat4x4, MeshBuilder, Path, gl::Buffer, gl::Mesh, gl::Renderer}; const TEXT: &'static str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac habitasse platea dictumst."; fn main() { let mut events_loop = glutin::EventsLoop::new(); let window_builder = glutin::WindowBuilder::new() .with_dimensions(glutin::dpi::LogicalSize::new(800.0, 600.0)) .with_title("gouache"); let context = glutin::ContextBuilder::new() .with_vsync(true) .build_windowed(window_builder, &events_loop) .unwrap(); let context = unsafe { context.make_current() }.unwrap(); gl::load_with(|symbol| context.get_proc_address(symbol) as *const _); let mut alloc = Allocator::new(1024, 16); let buffer = Buffer::new(1024, 16); let mut renderer = Renderer::new(); let font = ttf_parser::Font::from_data(include_bytes!("../res/SourceSansPro-Regular.otf"), 0).unwrap(); let mut glyph_cache = HashMap::new(); let mut mesh_builder = MeshBuilder::new(); let scale = 14.0 / font.units_per_em().unwrap() as f32; let mut pos = Vec2::new(0.0, scale * font.ascender() as f32); let line_height = scale * (font.height() + font.line_gap()) as f32; let mut width: f32 = 0.0; let mut height: f32 = line_height; for c in TEXT.chars() { if c == '\n' { pos.x = 0.0; pos.y -= line_height; height += line_height; } else if let Ok(glyph_id) = font.glyph_index(c) { let path_descr = if let Some(&path_descr) = glyph_cache.get(&glyph_id.0) { path_descr } else { use ttf_parser::OutlineBuilder; struct Builder { path: Path } impl OutlineBuilder for Builder { fn move_to(&mut self, x: f32, y: f32) { self.path.move_to(Vec2::new(x, y)); } fn line_to(&mut self, x: f32, y: f32) { self.path.line_to(Vec2::new(x, y)); } fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { self.path.quadratic_to(Vec2::new(x1, y1), Vec2::new(x, y)); } fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { self.path.cubic_to(Vec2::new(x1, y1), Vec2::new(x2, y2), Vec2::new(x, y)); } fn close(&mut self) { self.path.close(); } } let mut builder = Builder { path: Path::new() }; let _ = font.outline_glyph(glyph_id, &mut builder); let path_descr = alloc.add_path(&builder.path).unwrap(); glyph_cache.insert(glyph_id.0, path_descr); path_descr }; mesh_builder.add_path(path_descr, Mat4x4::translate(pos.x, pos.y, 0.0) * Mat4x4::scale(scale), Color::rgba(0.0, 0.0, 0.0, 1.0)); pos.x += scale * font.glyph_hor_metrics(glyph_id).unwrap().advance as f32; width = width.max(pos.x); } } for upload in alloc.uploads() { buffer.upload(upload.x as u32, upload.y as u32, upload.data.len() as u32, 1, &upload.data[..]); } let mesh = Mesh::new(mesh_builder.vertices(), mesh_builder.indices()); let mut z = 70.0; let mut cursor = Vec2::new(-1.0, -1.0); let mut dragging = false; let mut rotate = Mat4x4::id(); let mut running = true; while running { renderer.clear(Color::rgba(0.784, 0.804, 0.824, 1.0)); let model = Mat4x4::scale(0.1) * Mat4x4::translate(-0.5 * width, 0.5 * height, 0.0); let view = Mat4x4::translate(0.0, 0.0, -1.0 - z) * rotate; let proj = Mat4x4::perspective(std::f32::consts::PI / 4.0, 800.0 / 600.0, 0.1, 10000.0); let transform = proj * view * model; renderer.draw(&mesh, &buffer, &transform, 800.0, 600.0); context.swap_buffers().unwrap(); events_loop.poll_events(|event| match event { glutin::Event::WindowEvent { event, .. } => { use glutin::WindowEvent::*; match event { CloseRequested => { running = false; } MouseWheel { delta, .. } => match delta { glutin::MouseScrollDelta::PixelDelta(position) => { z *= 0.995f32.powf(-position.y as f32); } glutin::MouseScrollDelta::LineDelta(_dx, dy) => { z *= 0.995f32.powf(dy as f32 * 12.0); } }, MouseInput { button: glutin::MouseButton::Left, state, .. } => { match state { glutin::ElementState::Pressed => { dragging = true; } glutin::ElementState::Released => { dragging = false; } } } CursorMoved { position, .. } => { let new_cursor = Vec2::new(position.x as f32, position.y as f32); if dragging { let delta = new_cursor - cursor; rotate *= Mat4x4::rotate_zx(delta.x * std::f32::consts::PI / 512.0); rotate *= Mat4x4::rotate_yz(delta.y * std::f32::consts::PI / 512.0); } cursor = new_cursor; } _ => {} } } _ => {} }); } }