Skip to content

Commit

Permalink
Add support for sort_fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
alokedesai authored and wezm committed Jan 18, 2024
1 parent e99f217 commit a967823
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions fontconfig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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].
///
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a967823

Please sign in to comment.