Added SDL2 and Freetype as dependencies, in preparation for a GUI.
Also wrapped Freetype functionality in a Font struct.
This commit is contained in:
parent
e726b2d0f8
commit
30ad0c2ea4
|
@ -8,8 +8,14 @@ authors = ["Nathan Vegdahl <cessen@cessen.com>"]
|
||||||
name = "led"
|
name = "led"
|
||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
sdl2 = "0.0.17"
|
||||||
|
|
||||||
[dependencies.docopt]
|
[dependencies.docopt]
|
||||||
git = "https://github.com/docopt/docopt.rs.git"
|
git = "https://github.com/docopt/docopt.rs.git"
|
||||||
|
|
||||||
[dependencies.rustbox]
|
[dependencies.rustbox]
|
||||||
git = "https://github.com/gchp/rustbox.git"
|
git = "https://github.com/gchp/rustbox.git"
|
||||||
|
|
||||||
|
[dependencies.freetype-rs]
|
||||||
|
git = "https://github.com/PistonDevelopers/freetype-rs.git"
|
74
src/font.rs
Normal file
74
src/font.rs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,8 @@
|
||||||
extern crate rustbox;
|
extern crate rustbox;
|
||||||
extern crate docopt;
|
extern crate docopt;
|
||||||
extern crate "rustc-serialize" as rustc_serialize;
|
extern crate "rustc-serialize" as rustc_serialize;
|
||||||
|
extern crate freetype;
|
||||||
|
extern crate sdl2;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use docopt::Docopt;
|
use docopt::Docopt;
|
||||||
|
@ -14,6 +16,7 @@ mod buffer;
|
||||||
mod files;
|
mod files;
|
||||||
mod editor;
|
mod editor;
|
||||||
mod term_ui;
|
mod term_ui;
|
||||||
|
mod font;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user