# PrivateFontCollection class
{: .no_toc }

A set of fonts loaded from files or from memory for use by one program, without installing them.

A **PrivateFontCollection** starts empty. [**AddFontFile**](#addfontfile) and [**AddMemoryFont**](#addmemoryfont) put fonts into it, and [**FamilyCount**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontCollection.md#familycount) and [**Families**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontCollection.md#families), inherited from [**FontCollection**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontCollection.md), list the families that result. To draw with one of them, pass the collection as the last argument of [**FontFamily**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontFamily.md) or of [**Font**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/Font.md), with the name of the family. The class mirrors the GDI+ [PrivateFontCollection](https://learn.microsoft.com/en-us/windows/win32/api/gdiplusheaders/nl-gdiplusheaders-privatefontcollection) class.

Microsoft warns that a program must never load arbitrary fonts from sources it does not trust: the operating system requires elevated rights to install a font, so that every installed font is trusted.

## Creating

Syntax: *collection* **= PrivateFontCollection**( )

Syntax: *collection* **= New PrivateFontCollection**

> [!WARNING] 0.9.45.0, BETA 1045
> Creating a **PrivateFontCollection** raises run-time error 438, *Object doesn't support this property or method*, and the result is **Nothing**. The statement `Debug.Assert TypeOf (Me) Is PrivateFontCollection` in the class raises it. Even with that statement removed, the constructor does not store the native collection it creates, so the object has no handle and **AddFontFile**, **AddMemoryFont** and the members of **FontCollection** fail with **InvalidParameter**. The class cannot be used until a version of the package corrects both.

## AddFontFile

Syntax: *status* **=** *collection*.**AddFontFile**( *filename* )

*filename*
: *required* A **String** with the path of a font file, such as a `.ttf` or an `.otf` file.

Adds the fonts of the file to the collection and returns a [**GpStatus**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/Enumerations/GpStatus.md): **Ok** for success, **FileNotFound** when the file does not exist. The families of the file are in [**Families**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontCollection.md#families) afterwards.

```tb check_build
Dim user As GdiPlusUser = GdiPlusUser()

Dim fonts As PrivateFontCollection = PrivateFontCollection()
Dim status As GpStatus = fonts.AddFontFile(Environ$("TEMP") & "\MyFont.ttf")
If status = Ok Then
    Dim f As GdiPlus.Font = Font(fonts.Families(1).FamilyName, 14!, FontStyleRegular, UnitPoint, fonts)
End If
```

## AddMemoryFont

Syntax: *status* **=** *collection*.**AddMemoryFont**( *memAddress*, *length* )

*memAddress*
: *required* A **LongPtr** with the address of the font data, such as a font file read into a **Byte** array or locked from a resource.

*length*
: *required* A **Long** with the number of bytes at *memAddress*.

Adds the fonts in the block of memory to the collection and returns a [**GpStatus**](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/Enumerations/GpStatus.md). GDI+ does not copy the data: the block must stay valid, and unchanged, until the collection is released.

```tb check_build
Dim user As GdiPlusUser = GdiPlusUser()

Dim fonts As PrivateFontCollection = PrivateFontCollection()
Dim data(0 To 65535) As Byte
' ... read the bytes of a font file into data ...
Debug.Print fonts.AddMemoryFont(VarPtr(data(0)), UBound(data) + 1)
```

## See Also

- [FontCollection](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontCollection.md) class -- the members this class inherits
- [InstalledFontCollection](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/InstalledFontCollection.md) class -- the fonts installed on the system
- [FontFamily](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/FontFamily.md) class
- [Font](/twinBASIC-docs/tB/Packages/Contributed/GdiPlus/Font.md) class
- [PrivateFontCollection class](https://learn.microsoft.com/en-us/windows/win32/api/gdiplusheaders/nl-gdiplusheaders-privatefontcollection) -- Microsoft's GDI+ reference
