diff --git a/fontconfig/src/lib.rs b/fontconfig/src/lib.rs index 9ab1a24..e78f927 100644 --- a/fontconfig/src/lib.rs +++ b/fontconfig/src/lib.rs @@ -54,7 +54,7 @@ use sys::statics::{LIB, LIB_RESULT}; #[cfg(not(feature = "dlopen"))] use sys::*; -use std::ffi::{CStr, CString}; +use std::ffi::{c_int, CStr, CString}; use std::marker::PhantomData; use std::mem; use std::os::raw::c_char; @@ -199,7 +199,7 @@ impl<'fc> Pattern<'fc> { Pattern { pat, fc } } - /// Add a key-value pair to this pattern. + /// Add a key-value pair of type `String` to this pattern. /// /// See useful keys in the [fontconfig reference][1]. /// @@ -216,6 +216,17 @@ impl<'fc> Pattern<'fc> { } } + /// Add a key-value pair of type `Int` to this pattern + /// + /// See useful keys in the [fontconfig reference][1]. + /// + /// [1]: http://www.freedesktop.org/software/fontconfig/fontconfig-devel/x19.html + pub fn add_integer(&mut self, name: &CStr, val: c_int) { + unsafe { + ffi_dispatch!(LIB, FcPatternAddInteger, self.pat, name.as_ptr(), val); + } + } + /// Get string the value for a key from this pattern. pub fn get_string<'a>(&'a self, name: &'a CStr) -> Option<&'a str> { unsafe { @@ -511,6 +522,31 @@ pub fn list_fonts<'fc>(pattern: &Pattern<'fc>, objects: Option<&ObjectSet>) -> F } } +/// Returns a [`FontSet`] containing fonts sorted by closeness to the supplied `pattern`. If `trim` is true, elements in +/// the list which don't include Unicode coverage not provided by earlier elements in the list are elided. +/// +/// See the [fontconfig reference][1] for more details. +/// +/// [1]: https://www.freedesktop.org/software/fontconfig/fontconfig-devel/fcfontsort.html +pub fn sort_fonts<'fc>(pattern: &Pattern<'fc>, trim: bool) -> FontSet<'fc> { + // FcFontSort always returns a (possibly empty) set so we don't need to check this. + let mut res = sys::FcResultNoMatch; + let unicode_coverage = ptr::null_mut(); + let config = ptr::null_mut(); + unsafe { + let raw_set = ffi_dispatch!( + LIB, + FcFontSort, + config, + pattern.pat, + trim as FcBool, + unicode_coverage, + &mut res + ); + FontSet::from_raw(pattern.fc, raw_set) + } +} + /// Wrapper around `FcObjectSet`. pub struct ObjectSet { fcset: *mut sys::FcObjectSet,