Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sort_fonts function that internally calls FCFontSort. #39

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

///
/// 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