# FontCollectionBase

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-base/media/fonts/struct-fontcollectionbase/

**struct** · [`ferroui-base`](/docs/0.5.0/api/ferroui-base/) · v0.5.0

```rust
struct FontCollectionBase
```

The state and the shared matching, fallback and caching logic of font
collections.

Upstream this is an abstract class. Here a collection type holds a
`FontCollectionBase` and implements [`FontCollectionBaseImpl`](/docs/0.5.0/api/ferroui-base/media/fonts/trait-fontcollectionbaseimpl/) (the
abstract and virtual members); [`IFontCollection`](/docs/0.5.0/api/ferroui-base/media/fonts/trait-ifontcollection/) is then implemented for
it automatically. Members of the base class that reach overridable members
are associated functions taking the collection as `this`
(`FontCollectionBase::try_add_glyph_typeface(this, ..)`); the others are
methods of the state (`collection.base().count()`).

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L160)

## Members

- [`new`](#function-new)
- [`glyph_typeface_cache`](#function-glyph_typeface_cache)
- [`count`](#function-count)
- [`get`](#function-get)
- [`font_families`](#function-font_families)
- [`try_match_character`](#function-try_match_character)
- [`try_create_synthetic_glyph_typeface`](#function-try_create_synthetic_glyph_typeface)
- [`try_get_glyph_typeface`](#function-try_get_glyph_typeface)
- [`try_get_family_typefaces`](#function-try_get_family_typefaces)
- [`try_get_nearest_match`](#function-try_get_nearest_match)
- [`try_add_glyph_typeface`](#function-try_add_glyph_typeface)
- [`try_add_glyph_typeface_with_key`](#function-try_add_glyph_typeface_with_key)
- [`try_add_glyph_typeface_from_stream`](#function-try_add_glyph_typeface_from_stream)
- [`try_add_font_source`](#function-try_add_font_source)
- [`add_font_family`](#function-add_font_family)
- [`try_get_glyph_typeface_by_key`](#function-try_get_glyph_typeface_by_key)
- [`try_get_nearest_match_in`](#function-try_get_nearest_match_in)
- [`try_add_glyph_typeface_by_name`](#function-try_add_glyph_typeface_by_name)
- [`dispose`](#function-dispose)

<a id="function-new"></a>

### new

```rust
fn new() -> Self
```

Creates the state of a font collection.

Panics when no font manager backend (`IFontManagerImpl`) is registered
with the locator.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L365)

<a id="function-glyph_typeface_cache"></a>

### glyph_typeface_cache

```rust
fn glyph_typeface_cache(self: &Self) -> &GlyphTypefaceCache
```

The cached glyph typefaces by family name.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L376)

<a id="function-count"></a>

### count

```rust
fn count(self: &Self) -> usize
```

The number of font families in the collection.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L381)

<a id="function-get"></a>

### get

```rust
fn get(self: &Self, index: usize) -> FontFamily
```

The font family at `index`.

Panics when `index` is out of range.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L388)

<a id="function-font_families"></a>

### font_families

```rust
fn font_families(self: &Self) -> Vec<FontFamily>
```

A snapshot of the font families of the collection, sorted by name.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L393)

<a id="function-try_match_character"></a>

### try_match_character

```rust
fn try_match_character(this: &dyn IFontCollectionBase, codepoint: i32, style: FontStyle, weight: FontWeight, stretch: FontStretch, family_name: Option<&str>, culture: Option<&Locale>) -> Option<Typeface>
```

The base implementation of
`FontCollectionBaseImpl::try_match_character`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L399)

<a id="function-try_create_synthetic_glyph_typeface"></a>

### try_create_synthetic_glyph_typeface

```rust
fn try_create_synthetic_glyph_typeface(this: &dyn IFontCollectionBase, glyph_typeface: &Rc<GlyphTypeface>, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option<Rc<GlyphTypeface>>
```

The base implementation of
`FontCollectionBaseImpl::try_create_synthetic_glyph_typeface`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L843)

<a id="function-try_get_glyph_typeface"></a>

### try_get_glyph_typeface

```rust
fn try_get_glyph_typeface(this: &dyn IFontCollectionBase, family_name: &str, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option<Rc<GlyphTypeface>>
```

The base implementation of
`FontCollectionBaseImpl::try_get_glyph_typeface`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L913)

<a id="function-try_get_family_typefaces"></a>

### try_get_family_typefaces

```rust
fn try_get_family_typefaces(this: &dyn IFontCollectionBase, family_name: &str) -> Option<Vec<Typeface>>
```

The base implementation of
`FontCollectionBaseImpl::try_get_family_typefaces`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L930)

<a id="function-try_get_nearest_match"></a>

### try_get_nearest_match

```rust
fn try_get_nearest_match(self: &Self, family_name: &str, style: FontStyle, weight: FontWeight, stretch: FontStretch) -> Option<Rc<GlyphTypeface>>
```

Attempts to retrieve the glyph typeface that most closely matches the
specified font family name, style, weight, and stretch.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L954)

<a id="function-try_add_glyph_typeface"></a>

### try_add_glyph_typeface

```rust
fn try_add_glyph_typeface(this: &dyn IFontCollectionBase, glyph_typeface: &Rc<GlyphTypeface>) -> bool
```

Attempts to add the specified `GlyphTypeface` to the font collection.

This method checks the family name and, if applicable, the typographic
family name and other family names provided by the glyph typeface. If
any of these names can be associated with the glyph typeface, the
typeface is added to the collection. The method ensures that duplicate
entries are not added.

Returns `true` if the glyph typeface was successfully added to the
collection; otherwise, `false`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L980)

<a id="function-try_add_glyph_typeface_with_key"></a>

### try_add_glyph_typeface_with_key

```rust
fn try_add_glyph_typeface_with_key(this: &dyn IFontCollectionBase, glyph_typeface: &Rc<GlyphTypeface>, key: FontCollectionKey) -> bool
```

Attempts to add the specified glyph typeface to the collection using
the provided key.

The method adds the glyph typeface using both its typographic family
name and all available family names. If the glyph typeface's family
name is empty, the method returns `false` and does not add the
typeface.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L993)

<a id="function-try_add_glyph_typeface_from_stream"></a>

### try_add_glyph_typeface_from_stream

```rust
fn try_add_glyph_typeface_from_stream(this: &dyn IFontCollectionBase, stream: &mut dyn Read) -> Option<Rc<GlyphTypeface>>
```

Attempts to add a glyph typeface from the specified font stream.

The method first attempts to create a glyph typeface from the provided
font stream. If successful, it adds the created glyph typeface to the
collection.

Returns the created glyph typeface if it was created and added;
otherwise, `None`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1033)

<a id="function-try_add_font_source"></a>

### try_add_font_source

```rust
fn try_add_font_source(this: &dyn IFontCollectionBase, source: &url::Url) -> bool
```

Attempts to add a font source to the font collection.

This method processes the specified font source and attempts to load
all available fonts from it. Fonts are added to the collection based
on their family name and typographic family name (if available).

`source` can be a `file:` path (a font file or a directory of font
files) or a resource URI (`ferres:`, `resm:`).

Returns `true` if at least one font from the specified source was
successfully added to the font collection; otherwise, `false`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1056)

<a id="function-add_font_family"></a>

### add_font_family

```rust
fn add_font_family(self: &Self, font_family: FontFamily)
```

Inserts the specified font family into the collection, maintaining the
collection in sorted order by font family name.

If a font family with the same name (ignoring case) already exists in
the collection, nothing is added.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1187)

<a id="function-try_get_glyph_typeface_by_key"></a>

### try_get_glyph_typeface_by_key

```rust
fn try_get_glyph_typeface_by_key(this: &dyn IFontCollectionBase, family_name: &str, key: FontCollectionKey, allow_nearest_match: bool) -> Option<Rc<GlyphTypeface>>
```

Attempts to retrieve a glyph typeface that matches the specified font
family name and font collection key.

This method performs a binary search to locate font families with
names that match the specified `family_name` (ignoring case). If
multiple matches are found, the method iterates over them to find the
best match based on the provided `key`.

`allow_nearest_match` tells whether to allow a nearest match (as
opposed to only an exact match).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1209)

<a id="function-try_get_nearest_match_in"></a>

### try_get_nearest_match_in

```rust
fn try_get_nearest_match_in(glyph_typefaces: &GlyphTypefaceMap, key: FontCollectionKey) -> Option<Rc<GlyphTypeface>>
```

Attempts to retrieve the nearest matching `GlyphTypeface` for the
specified font key from the provided collection of glyph typefaces.

This method attempts to find the best match for the specified font key
by considering various fallback strategies, such as normalizing the
font style, stretch, and weight. If no suitable match is found, the
method will return the first available glyph typeface from the
collection, if any.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1328)

<a id="function-try_add_glyph_typeface_by_name"></a>

### try_add_glyph_typeface_by_name

```rust
fn try_add_glyph_typeface_by_name(this: &dyn IFontCollectionBase, family_name: &str, key: FontCollectionKey, glyph_typeface: Option<Rc<GlyphTypeface>>) -> bool
```

Attempts to add a glyph typeface to the cache for the specified font
family and key.

The font family is added to the collection, in sorted order, with the
first glyph typeface cached for it.

`glyph_typeface` can be `None` (the platform has no typeface for the
key): the answer is cached so that the platform is not asked again,
and the family is not added to the collection, which lists only
families it has a typeface of.

Returns `true` if the glyph typeface was successfully added to the
cache or the same typeface is already cached for the key; otherwise,
`false`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1399)

<a id="function-dispose"></a>

### dispose

```rust
fn dispose(self: &Self)
```

Disposes every cached glyph typeface.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/fonts/font_collection_base.rs#L1576)

## Trait implementations

- `Send`
- `Sync`
- `Freeze`
- `UnsafeUnpin`
- `Unpin`
- `UnwindSafe`
- `RefUnwindSafe`
- [`ClassDefaults`](/docs/0.5.0/api/ferroui-base/trait-classdefaults/)
- `Borrow<T>`
- `BorrowMut<T>`
- `Into<U>`
- `From<T>`
- `TryInto<U>`
- `TryFrom<U>`
- `Any`
- `ErasedDestructor`
- `IntoEither`
- `Default`

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
