{{DISPLAYTITLE:_FREEFONT}}
The [[_FREEFONT]] statement frees a font handle that was created by [[_LOADFONT]].



{{PageSyntax}}
:[[_FREEFONT]] ({{Parameter|fontHandle&amp;}})


{{PageDescription}}
* Unloads fonts that are no longer in use or needed in order to free program memory and resources.
* You cannot free a font which is in use. Change the font to a QB64 default font size before freeing the handle (see example below).
* Predefined '''QB64''' font handle numbers can be used before freeing a font:
**'''_FONT 8 ''' - default font for [[SCREEN (statement)|SCREEN]] 1, 2, 7, 8 or 13
**'''_FONT 14''' - default font for [[SCREEN (statement)|SCREEN]] 9 or 10
**'''_FONT 16''' - default font for [[SCREEN (statement)|SCREEN]] 0 ({{KW|WIDTH}} 80, 25 text only), 11 or 12
**'''_FONT 9, 15''' and '''17''' are the double width versions of 8, 14 and 16 respectively in text '''SCREEN 0'''.
* If the font handle is invalid (equals -1 or 0), an [[ERROR Codes|error]] will occur. '''Check handle values before using or freeing them.'''
* You cannot free inbuilt/default QB64 fonts nor do they ever need freed. 


{{PageExamples}}
''Example 1:'' Previews and creates a file list of valid MONOSPACE TTF fonts by checking the [[_LOADFONT]] handle values.
{{CodeStart}} '' ''
{{Cl|SCREEN (statement)|SCREEN}} 12
path$ = "C:\WINDOWS\Fonts\"                  'path to the font folder
{{Cl|SHELL}} {{Cl|_HIDE}} "DIR /b " + path$ + "\*.ttf > TTFonts.INF"  
style$ = "monospace"                         'set style to MONOSPACE
{{Cl|OPEN}} "TTFonts.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1           'list of TTF fonts only
{{Cl|OPEN}} "TTFMono.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #2          'will hold list of valid MONOSPACE fonts

{{Cl|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1): found = found + 1
  {{Cl|LINE INPUT (file statement)|LINE INPUT}} #1, font$          
  f&amp; ={{Cl|_LOADFONT}}(path$ + font$, 30, style$)   
  {{Cl|IF}} f&amp; > 0 {{Cl|THEN}}                  'check for valid handle values > 0
    OK = OK + 1
    {{Cl|PRINT (file statement)|PRINT}} #2, font$
    {{Cl|_FONT}} f&amp;                      'will create error if handle is invalid!
    {{Cl|PRINT}} "Hello World!"    
    {{Cl|PRINT}}: {{Cl|PRINT}}: {{Cl|PRINT}} font$; f&amp; 
    {{Cl|PRINT}} "Press any key."   
    K$ = {{Cl|INPUT$}}(1)
    {{Cl|_FONT}} 16                      'use QB64 default font to free tested font
    {{Cl|_FREEFONT}} f&amp;                  'returns an error if handle &lt;= 0! 
    {{Cl|CLS}}  
  {{Cl|END IF}}     
  {{Cl|PRINT}}
{{Cl|IF}} K$ = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|EXIT DO}}
{{Cl|LOOP}}
{{Cl|CLOSE}}
{{Cl|PRINT}}: {{Cl|PRINT}}: {{Cl|PRINT}} "Found"; found; "TTF files,"; OK; "can use Monospace,"
{{Cl|END}} '' ''
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{OutputStart}}
Found 106 TTF files, 13 can use Monospace.
{{OutputEnd}}


''Example 2:'' Using a _FREEFONT sub-procedure. 
{{CodeStart}}
fontpath$ = {{Cl|ENVIRON$}}("SYSTEMROOT") + "\fonts\lucon.ttf" 
style$ = "MONOSPACE, ITALIC, BOLD"
fontsize% = 20

{{Cl|_FONT|_FONT }}16
{{Cl|PRINT}}
{{Cl|PRINT}} "This is the QB64 default {{Cl|_FONT|_FONT }}16! To change, press any key!"
{{Cl|DO}}: {{Cl|SLEEP}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} &lt;> ""

{{Cl|GOSUB}} ClearFont  'call will not free anything if font&amp; = 0

font&amp; = {{Cl|_LOADFONT}}(fontpath$, fontsize%, style$)
{{Cl|IF}} font > 0 THEN {{Cl|_FONT|_FONT}} font&amp;  'NEVER try to load a font value less than 1!
{{Cl|PRINT}}
{{Cl|PRINT}} "A NEW {{Cl|_FONT|_FONT}} style. To change to default, press any key!"
{{Cl|DO}}: {{Cl|SLEEP}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} &lt;> ""

{{Cl|GOSUB}} ClearFont  'call will free a valid font handle from memory

{{Cl|END}}

ClearFont: 
{{Cl|IF}} font&amp; > 0 {{Cl|THEN}}
    {{Cl|_FONT|_FONT }}16   'change used font to the QB64 8x16 default font
    {{Cl|_FREEFONT}} font&amp;
    {{Cl|PRINT}}: {{Cl|PRINT}} "The previous font was freed with _FREEFONT!"
{{Cl|ELSE}} : {{Cl|PRINT}}: {{Cl|PRINT}} "_FREEFONT was not used!"
{{Cl|END IF}}
{{Cl|RETURN}} '' ''
{{CodeEnd}}


{{PageSeeAlso}}
* [[_FONT]]
* [[_LOADFONT]]


{{PageNavigation}}
