aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-02-21 15:20:03 -0800
committerJoe Wilm <joe@jwilm.com>2016-02-21 15:20:03 -0800
commit2a7dc1deb825f9f1db6de8b50656ef420ff41a44 (patch)
treee7206c9a4187232bccee88ee7cfa5e1d14ecbad4 /src
parent621776cd94890936b24f3abb8b7ec1f36dad9150 (diff)
downloadr-alacritty-2a7dc1deb825f9f1db6de8b50656ef420ff41a44.tar.gz
r-alacritty-2a7dc1deb825f9f1db6de8b50656ef420ff41a44.tar.bz2
r-alacritty-2a7dc1deb825f9f1db6de8b50656ef420ff41a44.zip
Add function for listing font names on linux
This function isn't exactly useful, but it's working ffi with the fontconfig library. Woo! Next step will be returning some objects with more information (like font path so we can start rendering glyphs!).
Diffstat (limited to 'src')
-rw-r--r--src/list_fonts.rs57
-rw-r--r--src/main.rs6
2 files changed, 63 insertions, 0 deletions
diff --git a/src/list_fonts.rs b/src/list_fonts.rs
new file mode 100644
index 00000000..9b15ea3d
--- /dev/null
+++ b/src/list_fonts.rs
@@ -0,0 +1,57 @@
+use std::ffi::CStr;
+use std::ptr;
+use std::str;
+
+use libc::{c_char, c_int};
+
+use fontconfig::fontconfig::{FcConfigGetCurrent, FcConfigGetFonts, FcSetSystem};
+use fontconfig::fontconfig::{FcPatternGetString};
+use fontconfig::fontconfig::{FcResultMatch};
+use fontconfig::fontconfig::{FcChar8};
+
+pub fn list_font_names() -> Vec<String> {
+ let mut fonts = Vec::new();
+ unsafe {
+ // https://www.freedesktop.org/software/fontconfig/fontconfig-devel/fcconfiggetcurrent.html
+ let config = FcConfigGetCurrent(); // *mut FcConfig
+
+ // https://www.freedesktop.org/software/fontconfig/fontconfig-devel/fcconfiggetfonts.html
+ let font_set = FcConfigGetFonts(config, FcSetSystem); // *mut FcFontSet
+
+ let nfont = (*font_set).nfont as isize;
+ for i in 0..nfont {
+ let font = (*font_set).fonts.offset(i); // *mut FcPattern
+ let id = 0 as c_int;
+ let mut fullname: *mut FcChar8 = ptr::null_mut();
+
+ // The second parameter here (fullname) is from the "FONT PROPERTIES" table:
+ // https://www.freedesktop.org/software/fontconfig/fontconfig-devel/x19.html
+ let result = FcPatternGetString(*font,
+ b"fullname\0".as_ptr() as *mut c_char,
+ id,
+ &mut fullname);
+ if result != FcResultMatch {
+ continue;
+ }
+
+ let s = str::from_utf8(CStr::from_ptr(fullname as *const c_char).to_bytes())
+ .unwrap().to_owned();
+ fonts.push(s);
+ }
+ }
+
+ fonts
+}
+
+#[cfg(test)]
+mod tests {
+ use super::list_font_names;
+
+ #[test]
+ fn list_fonts() {
+ let fonts = list_font_names();
+ assert!(!fonts.is_empty());
+
+ println!("fonts: {:?}", fonts);
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index e7a11a96..66be9b34 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,9 @@
+extern crate fontconfig;
+extern crate freetype;
+extern crate libc;
+
+mod list_fonts;
+
fn main() {
println!("Hello, world!");
}