PrivateFontCollection class

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

A PrivateFontCollection starts empty. AddFontFile and AddMemoryFont put fonts into it, and FamilyCount and Families, inherited from FontCollection, list the families that result. To draw with one of them, pass the collection as the last argument of FontFamily or of Font, with the name of the family. The class mirrors the GDI+ 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

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: Ok for success, FileNotFound when the file does not exist. The families of the file are in Families afterwards.

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. GDI+ does not copy the data: the block must stay valid, and unchanged, until the collection is released.

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