Skip to content

Commit 42e049c

Browse files
alokedesaiwezm
authored andcommitted
Add support for sort_fonts
1 parent e99f217 commit 42e049c

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

Diff for: fontconfig/src/lib.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use sys::statics::{LIB, LIB_RESULT};
5454
#[cfg(not(feature = "dlopen"))]
5555
use sys::*;
5656

57-
use std::ffi::{CStr, CString};
57+
use std::ffi::{c_int, CStr, CString};
5858
use std::marker::PhantomData;
5959
use std::mem;
6060
use std::os::raw::c_char;
@@ -199,7 +199,7 @@ impl<'fc> Pattern<'fc> {
199199
Pattern { pat, fc }
200200
}
201201

202-
/// Add a key-value pair to this pattern.
202+
/// Add a key-value pair of type `String` to this pattern.
203203
///
204204
/// See useful keys in the [fontconfig reference][1].
205205
///
@@ -216,6 +216,17 @@ impl<'fc> Pattern<'fc> {
216216
}
217217
}
218218

219+
/// Add a key-value pair of type `Int` to this pattern
220+
///
221+
/// See useful keys in the [fontconfig reference][1].
222+
///
223+
/// [1]: http://www.freedesktop.org/software/fontconfig/fontconfig-devel/x19.html
224+
pub fn add_integer(&mut self, name: &CStr, val: c_int) {
225+
unsafe {
226+
ffi_dispatch!(LIB, FcPatternAddInteger, self.pat, name.as_ptr(), val);
227+
}
228+
}
229+
219230
/// Get string the value for a key from this pattern.
220231
pub fn get_string<'a>(&'a self, name: &'a CStr) -> Option<&'a str> {
221232
unsafe {
@@ -511,6 +522,31 @@ pub fn list_fonts<'fc>(pattern: &Pattern<'fc>, objects: Option<&ObjectSet>) -> F
511522
}
512523
}
513524

525+
/// Returns a [`FontSet`] containing fonts sorted by closeness to the supplied `pattern`. If `trim` is true, elements in
526+
/// the list which don't include Unicode coverage not provided by earlier elements in the list are elided.
527+
///
528+
/// See the [fontconfig reference][1] for more details.
529+
///
530+
/// [1]: https://www.freedesktop.org/software/fontconfig/fontconfig-devel/fcfontsort.html
531+
pub fn sort_fonts<'fc>(pattern: &Pattern<'fc>, trim: bool) -> FontSet<'fc> {
532+
// FcFontSort always returns a (possibly empty) set so we don't need to check this.
533+
let mut res = sys::FcResultNoMatch;
534+
let unicode_coverage = ptr::null_mut();
535+
let config = ptr::null_mut();
536+
unsafe {
537+
let raw_set = ffi_dispatch!(
538+
LIB,
539+
FcFontSort,
540+
config,
541+
pattern.pat,
542+
trim as FcBool,
543+
unicode_coverage,
544+
&mut res
545+
);
546+
FontSet::from_raw(pattern.fc, raw_set)
547+
}
548+
}
549+
514550
/// Wrapper around `FcObjectSet`.
515551
pub struct ObjectSet {
516552
fcset: *mut sys::FcObjectSet,

0 commit comments

Comments
 (0)