Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/graphics/fonts/Font#nestedclasses) \| [Methods](https://developer.android.com/reference/android/graphics/fonts/Font#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/graphics/fonts/Font#inhmethods)

# Font

*** ** * ** ***

` public final class Font `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.graphics.fonts.Font |

<br />

*** ** * ** ***

A font class can be used for creating FontFamily.

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/graphics/fonts/Font.Builder` A builder class for creating new Font. |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/android/graphics/fonts/Font#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object o) ` Indicates whether some other object is "equal to" this one. |
| ` https://developer.android.com/reference/android/graphics/fonts/FontVariationAxis` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getAxes()() ` Get a font variation settings associated with this font |
| ` https://developer.android.com/reference/java/nio/ByteBuffer` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getBuffer()() ` Returns a font file buffer. |
| ` https://developer.android.com/reference/java/io/File` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getFile()() ` Returns a file path of this font. |
| ` float` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getGlyphBounds(int,%20android.graphics.Paint,%20android.graphics.RectF)(int glyphId, https://developer.android.com/reference/android/graphics/Paint paint, https://developer.android.com/reference/android/graphics/RectF outBoundingBox) ` Retrieve the glyph horizontal advance and bounding box. |
| ` https://developer.android.com/reference/android/os/LocaleList` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getLocaleList()() ` Get a locale list of this font. |
| ` void` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getMetrics(android.graphics.Paint,%20android.graphics.Paint.FontMetrics)(https://developer.android.com/reference/android/graphics/Paint paint, https://developer.android.com/reference/android/graphics/Paint.FontMetrics outMetrics) ` Retrieve the font metrics information. |
| ` int` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getSourceIdentifier()() ` Returns the unique ID of the source font data. |
| ` https://developer.android.com/reference/android/graphics/fonts/FontStyle` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getStyle()() ` Get a style associated with this font. |
| ` int` | ` https://developer.android.com/reference/android/graphics/fonts/Font#getTtcIndex()() ` Get a TTC index value associated with this font. |
| ` int` | ` https://developer.android.com/reference/android/graphics/fonts/Font#hashCode()() ` Returns a hash code value for the object. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/graphics/fonts/Font#toString()() ` Returns a string representation of the object. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public methods

### equals

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean equals (Object o)
```

Indicates whether some other object is "equal to" this one.

The `equals` method implements an equivalence relation on non-null object references:

- It is *reflexive*: for any non-null reference value `x`, `x.equals(x)` should return `true`.
- It is *symmetric*: for any non-null reference values `x` and `y`, `x.equals(y)` should return `true` if and only if `y.equals(x)` returns `true`.
- It is *transitive*: for any non-null reference values `x`, `y`, and `z`, if `x.equals(y)` returns `true` and `y.equals(z)` returns `true`, then `x.equals(z)` should return `true`.
- It is *consistent*: for any non-null reference values `x` and `y`, multiple invocations of `x.equals(y)` consistently return `true` or consistently return `false`, provided no information used in `equals` comparisons on the objects is modified.
- For any non-null reference value `x`, `x.equals(null)` should return `false`.

An equivalence relation partitions the elements it operates on into *equivalence classes*; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

<br />

| Parameters ||
|---|---|
| `o` | `Object`: This value may be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if this object is the same as the obj argument; `false` otherwise. <br /> |

### getAxes

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FontVariationAxis[] getAxes ()
```

Get a font variation settings associated with this font

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/fonts/FontVariationAxis` | font variation settings. This value may be `null`. <br /> |

**See also:**

- `https://developer.android.com/reference/android/graphics/fonts/Font.Builder#setFontVariationSettings(java.lang.String)`
- `https://developer.android.com/reference/android/graphics/fonts/Font.Builder#setFontVariationSettings(android.graphics.fonts.FontVariationAxis[])`

### getBuffer

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ByteBuffer getBuffer ()
```

Returns a font file buffer. Duplicate before reading values by `https://developer.android.com/reference/java/nio/ByteBuffer#duplicate()` for avoiding unexpected reading position sharing.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/ByteBuffer` | a font buffer. This value cannot be `null`. <br /> |

### getFile

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public File getFile ()
```

Returns a file path of this font. This returns null if this font is not created from regular file.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/File` | a file path of the font <br /> |

### getGlyphBounds

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public float getGlyphBounds (int glyphId, 
                Paint paint, 
                RectF outBoundingBox)
```

Retrieve the glyph horizontal advance and bounding box. Note that `https://developer.android.com/reference/android/graphics/Typeface` in `https://developer.android.com/reference/android/graphics/Paint` is ignored.

<br />

| Parameters ||
|---|---|
| `glyphId` | `int`: a glyph ID. Value is 0 or greater <br /> |
| `paint` | `Paint`: a paint object used for resolving glyph style. This value cannot be `null`. <br /> |
| `outBoundingBox` | `RectF`: a nullable destination object. If null is passed, this function just return the horizontal advance. If non-null is passed, this function fills bounding box information to this object. <br /> |

| Returns ||
|---|---|
| `float` | the amount of horizontal advance in pixels <br /> |

### getLocaleList

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public LocaleList getLocaleList ()
```

Get a locale list of this font. This is always empty if this font is not a system font.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/LocaleList` | a locale list. This value cannot be `null`. <br /> |

### getMetrics

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void getMetrics (Paint paint, 
                Paint.FontMetrics outMetrics)
```

Retrieve the font metrics information. Note that `https://developer.android.com/reference/android/graphics/Typeface` in `https://developer.android.com/reference/android/graphics/Paint` is ignored.

<br />

| Parameters ||
|---|---|
| `paint` | `Paint`: a paint object used for retrieving font metrics. This value cannot be `null`. <br /> |
| `outMetrics` | `Paint.FontMetrics`: a nullable destination object. If null is passed, this function only retrieve recommended interline spacing. If non-null is passed, this function fills to font metrics to it. <br /> |

**See also:**

- `https://developer.android.com/reference/android/graphics/Paint#getFontMetrics()`
- `https://developer.android.com/reference/android/graphics/Paint#getFontMetricsInt()`

### getSourceIdentifier

Added in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getSourceIdentifier ()
```

Returns the unique ID of the source font data. You can use this identifier as a key of the cache or checking if two fonts can be interpolated with font variation settings.


      // Following three Fonts, fontA, fontB, fontC have the same identifier.
      Font fontA = new Font.Builder("/path/to/font").build();
      Font fontB = new Font.Builder(fontA).setTtcIndex(1).build();
      Font fontC = new Font.Builder(fontB).setFontVariationSettings("'wght' 700).build();

      // Following fontD has the different identifier from above three.
      Font fontD = new Font.Builder("/path/to/another/font").build();

      // Following fontE has different identifier from above four even the font path is the same.
      // To get the same identifier, please create new Font instance from existing fonts.
      Font fontE = new Font.Builder("/path/to/font").build();

Here is an example of caching font object that has


      private LongSparseArray
           
            > mCache = new LongSparseArray<>();

      private Font getFontWeightVariation(Font font, int weight) {
          // Different collection index is treated as different font.
          long key = ((long) font.getSourceIdentifier()) << 32 | (long) font.getTtcIndex();

          SparseArray weightCache = mCache.get(key);
          if (weightCache == null) {
             weightCache = new SparseArray<>();
             mCache.put(key, weightCache);
          }

          Font cachedFont = weightCache.get(weight);
          if (cachedFont != null) {
            return cachedFont;
          }

          Font newFont = new Font.Builder(cachedFont)
              .setFontVariationSettings("'wght' " + weight);
              .build();

          weightCache.put(weight, newFont);
          return newFont;
      }

           
<br />

<br />

| Returns ||
|---|---|
| `int` | an unique identifier for the font source data. <br /> |

### getStyle

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public FontStyle getStyle ()
```

Get a style associated with this font.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/fonts/FontStyle` | a font style. This value cannot be `null`. <br /> |

**See also:**

- `https://developer.android.com/reference/android/graphics/fonts/Font.Builder#setWeight(int)`
- `https://developer.android.com/reference/android/graphics/fonts/Font.Builder#setSlant(int)`

### getTtcIndex

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getTtcIndex ()
```

Get a TTC index value associated with this font. If TTF/OTF file is provided, this value is always 0.

<br />

| Returns ||
|---|---|
| `int` | a TTC index value. Value is 0 or greater <br /> |

**See also:**

- `https://developer.android.com/reference/android/graphics/fonts/Font.Builder#setTtcIndex(int)`

### hashCode

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int hashCode ()
```

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by `https://developer.android.com/reference/java/util/HashMap`.

The general contract of `hashCode` is:

- Whenever it is invoked on the same object more than once during an execution of a Java application, the `hashCode` method must consistently return the same integer, provided no information used in `equals` comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` method, then calling the `hashCode` method on each of the two objects must produce the same integer result.
- It is *not* required that if two objects are unequal according to the `https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)` method, then calling the `hashCode` method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

<br />

<br />

| Returns ||
|---|---|
| `int` | a hash code value for this object. <br /> |

### toString

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String toString ()
```

Returns a string representation of the object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representation of the object. <br /> |