1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-18 13:55:23 -05:00

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!).
This commit is contained in:
Joe Wilm 2016-02-21 15:20:03 -08:00
parent 621776cd94
commit 2a7dc1deb8
5 changed files with 144 additions and 0 deletions

10
.travis.yml Normal file
View file

@ -0,0 +1,10 @@
language: rust
sudo: false
rust:
# There's currently a cargo bug on stable which prevents libfontconfig_sys
# from building properly.
- nightly
script:
- cargo test

68
Cargo.lock generated Normal file
View file

@ -0,0 +1,68 @@
[root]
name = "alacritty"
version = "0.1.0"
dependencies = [
"freetype 0.1.0 (git+https://github.com/servo/rust-freetype.git)",
"libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"servo-fontconfig 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "expat-sys"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"make-cmd 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "freetype"
version = "0.1.0"
source = "git+https://github.com/servo/rust-freetype.git#d564ff90a3c69d987f5c015d7ec034cfaee21aff"
dependencies = [
"libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "make-cmd"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "pkg-config"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "servo-fontconfig"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"servo-fontconfig-sys 2.11.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "servo-fontconfig-sys"
version = "2.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"expat-sys 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"servo-freetype-sys 2.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "servo-freetype-sys"
version = "2.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"pkg-config 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]

View file

@ -4,3 +4,6 @@ version = "0.1.0"
authors = ["Joe Wilm <joe@jwilm.com>"]
[dependencies]
servo-fontconfig = "0.2"
freetype = { git = "https://github.com/servo/rust-freetype.git" }
libc = "*"

57
src/list_fonts.rs Normal file
View file

@ -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);
}
}

View file

@ -1,3 +1,9 @@
extern crate fontconfig;
extern crate freetype;
extern crate libc;
mod list_fonts;
fn main() {
println!("Hello, world!");
}