FREEFONT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 22: Line 22:
''Example 1:'' Previews and creates a file list of valid MONOSPACE TTF fonts by checking the [[_LOADFONT]] handle values.
''Example 1:'' Previews and creates a file list of valid MONOSPACE TTF fonts by checking the [[_LOADFONT]] handle values.
{{CodeStart}}
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} {{Text|12|#F580B1}}
path$ = "C:\WINDOWS\Fonts\"                 'path to the font folder
path$ = {{Text|<nowiki>"C:\WINDOWS\Fonts\"</nowiki>|#FFB100}} {{Text|<nowiki>'path to the font folder</nowiki>|#919191}}
{{Cl|SHELL}} {{Cl|_HIDE}} "DIR /b " + path$ + "\*.ttf > TTFonts.INF"
{{Cl|SHELL}} {{Cl|_HIDE}} {{Text|<nowiki>"DIR /b "</nowiki>|#FFB100}} + path$ + {{Text|<nowiki>"\*.ttf > TTFonts.INF"</nowiki>|#FFB100}}
style$ = "monospace"                         'set style to MONOSPACE
style$ = {{Text|<nowiki>"monospace"</nowiki>|#FFB100}} {{Text|<nowiki>'set style to MONOSPACE</nowiki>|#919191}}
{{Cl|OPEN}} "TTFonts.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1           'list of TTF fonts only
{{Cl|OPEN}} {{Text|<nowiki>"TTFonts.INF"</nowiki>|#FFB100}} {{Cl|OPEN#File_Access_Modes|FOR}} {{Cl|OPEN#File_Access_Modes|INPUT}} {{Cl|OPEN|AS}} #1 {{Text|<nowiki>'list of TTF fonts only</nowiki>|#919191}}
{{Cl|OPEN}} "TTFMono.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #2         'will hold list of valid MONOSPACE fonts
{{Cl|OPEN}} {{Text|<nowiki>"TTFMono.INF"</nowiki>|#FFB100}} {{Cl|OPEN#File_Access_Modes|FOR}} {{Cl|OPEN#File_Access_Modes|OUTPUT}} {{Cl|OPEN|AS}} #2 {{Text|<nowiki>'will hold list of valid MONOSPACE fonts</nowiki>|#919191}}


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


----


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


{{Cl|_FONT|_FONT }}16
{{Cl|_FONT}} {{Text|16|#F580B1}}
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|PRINT}} "This is the QB64 default {{Cl|_FONT|_FONT }}16! To change, press any key!"
{{Cl|PRINT}} {{Text|<nowiki>"This is the QB64 default _FONT 16! To change, press any key!"</nowiki>|#FFB100}}
{{Cl|DO}}: {{Cl|SLEEP}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
{{Cl|DO}}: {{Cl|SLEEP}}: {{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|INKEY$}} <> {{Text|<nowiki>""</nowiki>|#FFB100}}


{{Cl|GOSUB}} ClearFont 'call will not free anything if font& = 0
{{Cl|GOSUB}} ClearFont {{Text|<nowiki>'call will not free anything if font& = 0</nowiki>|#919191}}


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


{{Cl|GOSUB}} ClearFont 'call will free a valid font handle from memory
{{Cl|GOSUB}} ClearFont {{Text|<nowiki>'call will free a valid font handle from memory</nowiki>|#919191}}


{{Cl|END}}
{{Cl|END}}


ClearFont:
ClearFont:
{{Cl|IF}} font& > 0 {{Cl|THEN}}
{{Cl|IF}} font& > {{Text|0|#F580B1}} {{Cl|THEN}}
     {{Cl|_FONT|_FONT }}16  'change used font to the QB64 8x16 default font
     {{Cl|_FONT}} {{Text|16|#F580B1}} {{Text|<nowiki>'change used font to the QB64 8x16 default font</nowiki>|#919191}}
     {{Cl|_FREEFONT}} font&
     {{Cl|_FREEFONT}} font&
     {{Cl|PRINT}}: {{Cl|PRINT}} "The previous font was freed with _FREEFONT!"
     {{Cl|PRINT}}: {{Cl|PRINT}} {{Text|<nowiki>"The previous font was freed with _FREEFONT!"</nowiki>|#FFB100}}
{{Cl|ELSE}} : {{Cl|PRINT}}: {{Cl|PRINT}} "_FREEFONT was not used!"
{{Cl|ELSE}}: {{Cl|PRINT}}: {{Cl|PRINT}} {{Text|<nowiki>"_FREEFONT was not used!"</nowiki>|#FFB100}}
{{Cl|END IF}}
{{Cl|END IF}}
{{Cl|RETURN}}
{{Cl|RETURN}}

Latest revision as of 10:24, 29 March 2023

The _FREEFONT statement frees a font handle that was created by _LOADFONT.


Syntax

_FREEFONT (fontHandle&)


Description

  • 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 1, 2, 7, 8 or 13
    • _FONT 14 - default font for SCREEN 9 or 10
    • _FONT 16 - default font for SCREEN 0 (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 will occur. Check handle values before using or freeing them.
  • You cannot free inbuilt/default QB64 fonts nor do they ever need freed.


Examples

Example 1: Previews and creates a file list of valid MONOSPACE TTF fonts by checking the _LOADFONT handle values.

SCREEN 12
path$ = "C:\WINDOWS\Fonts\" 'path to the font folder
SHELL _HIDE "DIR /b " + path$ + "\*.ttf > TTFonts.INF"
style$ = "monospace" 'set style to MONOSPACE
OPEN "TTFonts.INF" FOR INPUT AS #1 'list of TTF fonts only
OPEN "TTFMono.INF" FOR OUTPUT AS #2 'will hold list of valid MONOSPACE fonts

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

Example 2: Using a _FREEFONT sub-procedure.

fontpath$ = ENVIRON$("SYSTEMROOT") + "\fonts\lucon.ttf"
style$ = "MONOSPACE, ITALIC, BOLD"
fontsize% = 20

_FONT 16
PRINT
PRINT "This is the QB64 default _FONT 16! To change, press any key!"
DO: SLEEP: LOOP UNTIL INKEY$ <> ""

GOSUB ClearFont 'call will not free anything if font& = 0

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

GOSUB ClearFont 'call will free a valid font handle from memory

END

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


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link