# Function Reference

# _GDIPlus_FontPrivateAddMemoryFont

Adds a font that is contained in system memory to a Microsoft� Windows� GDI+ font collection

#include <GDIPlus.au3>
_GDIPlus_FontPrivateAddMemoryFont ( $hFontCollection, $tFont )

## Parameters

$hFontCollection | A handle to the font collection object
$tFont | A dll struct value filled with binary ttf font data

## Return Value

Success: | True
Failure: | False and sets the @error flag to non-zero, @extended may contain GPSTATUS error code ($GDIP_ERR* see GPIPlusConstants.au3).

## Related

_GDIPlus_FontPrivateCreateCollection, _GDIPlus_FontPrivateCollectionDispose, _GDIPlus_FontCreate

## See Also

Search GdipPrivateAddMemoryFont in MSDN Library.

## Example

    #include <GDIPlus.au3>
    #include <GUIConstantsEx.au3>
    #include <WinAPIGdi.au3>

    Example()

    Func Example()
        Local $bFont = _MemFont() ;load binary data (TTF) to use it later in GDI+
        Local $hGUI, $hGraphic, $hBrush, $hFormat, $hFamily, $hFont, $tLayout, $sFontInternalName
        Local $hCollection, $tFont

        $hGUI = GUICreate("GDI+", 720, 235)
        GUISetState()

        _GDIPlus_Startup()
        $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
        _GDIPlus_GraphicsSetSmoothingMode($hGraphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;Sets the graphics object rendering quality (anti aliasing)
        _GDIPlus_GraphicsSetTextRenderingHint($hGraphic, $GDIP_TEXTRENDERINGHINTANTIALIASGRIDFIT) ;set the anti aliasing for fonts

        $hCollection = _GDIPlus_FontPrivateCreateCollection() ;create a PrivateFontCollection object to add fonts later
        $tFont = DllStructCreate('byte[' & BinaryLen($bFont) & ']') ;create byte struct to add font to collection
        DllStructSetData($tFont, 1, $bFont)
        _GDIPlus_FontPrivateAddMemoryFont($hCollection, $tFont) ;add font to collection

        $hBrush = _GDIPlus_BrushCreateSolid(0xFF00007F) ;create a brush with a defined color for the text
        $hFormat = _GDIPlus_StringFormatCreate() ;create a string format object
        $sFontInternalName = _WinAPI_GetFontMemoryResourceInfo(DllStructGetPtr($tFont)) ;get internal font name
        $hFamily = _GDIPlus_FontFamilyCreate($sFontInternalName, $hCollection) ;create a font family object
        $hFont = _GDIPlus_FontCreate($hFamily, 70) ;create a font object
        $tLayout = _GDIPlus_RectFCreate(0, 0, 720, 235) ;create a $tagGDIPRECTF structure to save x, y position of the text
        _GDIPlus_StringFormatSetAlign($hFormat, 1) ;center text
        _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ;center text
        _GDIPlus_GraphicsDrawStringEx($hGraphic, "AutoIt" & @LF & "forever", $hFont, $tLayout, $hFormat, $hBrush) ;draw the string

        ; Loop until user exits
        Do
        Until GUIGetMsg() = $GUI_EVENT_CLOSE

        ; Clean up resources
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        _GDIPlus_StringFormatDispose($hFormat)
        _GDIPlus_FontPrivateCollectionDispose($hCollection)
        _GDIPlus_BrushDispose($hBrush)
        _GDIPlus_GraphicsDispose($hGraphic)
        _GDIPlus_Shutdown()
    EndFunc   ;==>Example

    Func _MemFont($bSaveBinary = False, $sSavePath = @ScriptDir) ;compressed base64 saved TTL font
        Local $vMemFont
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont &= ' '
        $vMemFont = _WinAPI_Base64Decode($vMemFont)
        Local $tSource = DllStructCreate('byte[' & BinaryLen($vMemFont) & ']')
        DllStructSetData($tSource, 1, $vMemFont)
        Local $tDecompress
        _WinAPI_LZNTDecompress($tSource, $tDecompress, 31304)
        $tSource = 0
        Local $bString = Binary(DllStructGetData($tDecompress, 1))
        If $bSaveBinary Then
            Local $hFile = FileOpen($sSavePath & "\ETHNOCEN.TTF", 18)
            FileWrite($hFile, $bString)
            FileClose($hFile)
        EndIf
        Return $bString
    EndFunc   ;==>_MemFont

    Func _WinAPI_Base64Decode($sB64String)
        Local $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "ptr", 0, "dword*", 0, "ptr", 0, "ptr", 0)
        If @error Or Not $aCrypt[0] Then Return SetError(1, 0, "")
        Local $tBuffer = DllStructCreate("byte[" & $aCrypt[5] & "]")
        $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "struct*", $tBuffer, "dword*", $aCrypt[5], "ptr", 0, "ptr", 0)
        If @error Or Not $aCrypt[0] Then Return SetError(2, 0, "")
        Return DllStructGetData($tBuffer, 1)
    EndFunc   ;==>_WinAPI_Base64Decode

    Func _WinAPI_LZNTDecompress(ByRef $tInput, ByRef $tOutput, $iBufferSize)
        $tOutput = DllStructCreate("byte[" & $iBufferSize & "]")
        If @error Then Return SetError(1, 0, 0)
        Local $aRet = DllCall("ntdll.dll", "uint", "RtlDecompressBuffer", "ushort", 0x0002, "struct*", $tOutput, "ulong", $iBufferSize, "struct*", $tInput, "ulong", DllStructGetSize($tInput), "ulong*", 0)
        If @error Then Return SetError(2, 0, 0)
        If $aRet[0] Then Return SetError(3, $aRet[0], 0)
        Return $aRet[6]
    EndFunc   ;==>_WinAPI_LZNTDecompress