Skip to main content

Fontconfig

Struct Fontconfig 

Source
pub struct Fontconfig { /* private fields */ }
Expand description

Handle obtained after Fontconfig has been initialised.

Implementations§

Source§

impl Fontconfig

Source

pub fn new() -> Option<Self>

Initialise Fontconfig and return a handle allowing further interaction with the API.

If Fontconfig fails to initialise, returns None.

§Example
use fontconfig::{Fontconfig};

let fc = Fontconfig::new().expect("unable to initialise Fontconfig");
Examples found in repository?
examples/fc-match.rs (line 6)
5fn main() -> Result<(), FontconfigError> {
6    let fc = Fontconfig::new().expect("unable to init Fontconfig");
7
8    let Some(family) = env::args().nth(1) else {
9        eprintln!("Error: font family not specified");
10        eprintln!(
11            "Usage: {}: family",
12            env::args().next().as_deref().unwrap_or("fc-list")
13        );
14        return Ok(());
15    };
16    let family = CString::new(family)?;
17
18    let mut pattern = fontconfig::Pattern::new(&fc)?;
19    pattern.add_string(fontconfig::FC_FAMILY, &family)?;
20
21    let matched = pattern.font_match()?;
22    let mut fontset = FontSet::new(&fc)?;
23    fontset.add_pattern(matched)?;
24
25    for pattern in fontset.iter() {
26        println!(
27            "{}[{}] ({}): {}, weight = {}, width = {}, slant = {}",
28            pattern.filename()?,
29            pattern.face_index()?,
30            pattern
31                .format()
32                .map(|f| f.to_string())
33                .unwrap_or_else(|_| String::from("Unknown")),
34            pattern.name()?,
35            pattern.weight()?,
36            pattern.width()?,
37            pattern.slant()?
38        )
39    }
40    Ok(())
41}
More examples
Hide additional examples
examples/fc-list.rs (line 6)
5fn main() -> Result<(), FontconfigError> {
6    let fc = Fontconfig::new().expect("unable to init Fontconfig");
7
8    let Some(family) = env::args().nth(1) else {
9        eprintln!("Error: font family not specified");
10        eprintln!(
11            "Usage: {}: family",
12            env::args().next().as_deref().unwrap_or("fc-list")
13        );
14        return Ok(());
15    };
16    let family = CString::new(family)?;
17
18    let mut pattern = fontconfig::Pattern::new(&fc)?;
19    pattern.add_string(fontconfig::FC_FAMILY, &family)?;
20
21    let mut objects = fontconfig::ObjectSet::new(&fc)?;
22    objects.add(fontconfig::FC_FAMILY)?;
23    objects.add(fontconfig::FC_FILE)?;
24    objects.add(fontconfig::FC_FONTFORMAT)?;
25    objects.add(fontconfig::FC_INDEX)?;
26    objects.add(fontconfig::FC_STYLE)?;
27
28    let fontset = fontconfig::list_fonts(&pattern, Some(&objects))?;
29
30    for pattern in fontset.iter() {
31        let family = pattern.get_string(fontconfig::FC_FAMILY)?;
32        let style = pattern.get_string(fontconfig::FC_STYLE)?;
33
34        println!(
35            "{}[{}] ({}): {}:style={}",
36            pattern.filename()?,
37            pattern.face_index()?,
38            pattern
39                .format()
40                .map(|f| f.to_string())
41                .unwrap_or_else(|_| String::from("Unknown")),
42            family,
43            style
44        )
45    }
46    Ok(())
47}
Source

pub fn find( &self, family: &str, style: Option<&str>, ) -> Result<Font, FontconfigError>

Find a font of the given family and style.

Results can optionally be filtered by style. Both fields are case-insensitive.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.