diff --git a/Cargo.toml b/Cargo.toml index 0f411e2..8bff2c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,14 @@ authors = ["Nathan Vegdahl "] name = "led" path = "src/main.rs" +[dependencies] +sdl2 = "0.0.17" + [dependencies.docopt] git = "https://github.com/docopt/docopt.rs.git" [dependencies.rustbox] -git = "https://github.com/gchp/rustbox.git" \ No newline at end of file +git = "https://github.com/gchp/rustbox.git" + +[dependencies.freetype-rs] +git = "https://github.com/PistonDevelopers/freetype-rs.git" \ No newline at end of file diff --git a/src/font.rs b/src/font.rs new file mode 100644 index 0000000..b276729 --- /dev/null +++ b/src/font.rs @@ -0,0 +1,74 @@ +#![allow(dead_code)] + +use std::path::Path; + +use freetype; +use sdl2; + +use sdl2::surface::Surface; +use sdl2::rect::Rect; + + +pub struct Font { + ftl: freetype::Library, + face: freetype::Face, +} + +impl Font { + pub fn new_from_file(path: &Path, size: u32) -> Font { + let lib = freetype::Library::init().unwrap(); + let mut face = lib.new_face(path.as_str().unwrap(), 0).unwrap(); + let _ = face.set_pixel_sizes(0, size); + + Font { + ftl: lib, + face: face, + } + } + + + pub fn draw_text(&mut self, text: &str, color: (u8, u8, u8), cx: i32, cy: i32, renderer: &sdl2::render::Renderer) { + let mut x = cx; + let mut y = cy; + + for grapheme in text.graphemes(true) { + for ch in grapheme.chars() { + let _ = self.face.load_char(ch as u64, freetype::face::RENDER); + let g = self.face.glyph(); + + match (g.bitmap().width(), g.bitmap().rows()) { + (0, _) | (_, 0) => { + }, + + _ => { + // Get the char's glyph bitmap as an sdl surface + + let bitmap = g.bitmap(); + let width = g.bitmap().width() as isize; + let height = g.bitmap().rows() as isize; + let mut buf = Vec::with_capacity(bitmap.buffer().len() * 4); + for b in bitmap.buffer().iter() { + buf.push(*b); + buf.push(color.2); + buf.push(color.1); + buf.push(color.0); + } + let gs = Surface::from_data(buf.as_mut_slice(), width, height, 32, width*4, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF).unwrap(); + + // Get glyph surface as a texture + let gt = renderer.create_texture_from_surface(&gs).unwrap(); + + // Draw the glyph + let _ = renderer.copy(>, Some(Rect{x:0, y:0, h:height as i32, w:width as i32}), Some(Rect{x:x+g.bitmap_left(), y:y-g.bitmap_top(), h:height as i32, w:width as i32})); + + + } + } + + x += (g.advance().x >> 6) as i32; + y += (g.advance().y >> 6) as i32; + break; + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 6ace1b0..1dc1b87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate rustbox; extern crate docopt; extern crate "rustc-serialize" as rustc_serialize; +extern crate freetype; +extern crate sdl2; use std::path::Path; use docopt::Docopt; @@ -14,6 +16,7 @@ mod buffer; mod files; mod editor; mod term_ui; +mod font;