FREEFONT: Difference between revisions
Jump to navigation
Jump to search
Code by Ted Weissgerber
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
**'''_FONT 9, 15''' and '''17''' are the double width versions of 8, 14 and 16 respectively in text '''SCREEN 0'''. | **'''_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.''' | * 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. | * You cannot free inbuilt/default QB64 fonts nor do they ever need freed. | ||
Line 25: | Line 25: | ||
{{Cl|SCREEN (statement)|SCREEN}} 12 | {{Cl|SCREEN (statement)|SCREEN}} 12 | ||
path$ = "C:\WINDOWS\Fonts\" 'path to the font folder | path$ = "C:\WINDOWS\Fonts\" 'path to the font folder | ||
{{Cl|SHELL}} {{Cl|_HIDE}} "DIR /b " + path$ + "\*.ttf > TTFonts.INF" | {{Cl|SHELL}} {{Cl|_HIDE}} "DIR /b " + path$ + "\*.ttf > TTFonts.INF" | ||
style$ = "monospace" 'set style to MONOSPACE | 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}} "TTFonts.INF" {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #1 'list of TTF fonts only | ||
Line 31: | Line 31: | ||
{{Cl|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1): found = found + 1 | {{Cl|DO}} {{Cl|UNTIL}} {{Cl|EOF}}(1): found = found + 1 | ||
{{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$, 30, style$) | ||
{{Cl|IF}} f& > 0 {{Cl|THEN}} 'check for valid handle values > 0 | {{Cl|IF}} f& > 0 {{Cl|THEN}} 'check for valid handle values > 0 | ||
OK = OK + 1 | OK = OK + 1 | ||
{{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& 'will create error if handle is invalid! | ||
{{Cl|PRINT}} "Hello World!" | {{Cl|PRINT}} "Hello World!" | ||
{{Cl|PRINT}}: {{Cl|PRINT}}: {{Cl|PRINT}} font$; f& | {{Cl|PRINT}}: {{Cl|PRINT}}: {{Cl|PRINT}} font$; f& | ||
{{Cl|PRINT}} "Press any key." | {{Cl|PRINT}} "Press any key." | ||
K$ = {{Cl|INPUT$}}(1) | K$ = {{Cl|INPUT$}}(1) | ||
{{Cl|_FONT}} 16 'use QB64 default font to free tested font | {{Cl|_FONT}} 16 'use QB64 default font to free tested font | ||
{{Cl|_FREEFONT}} f& 'returns an error if handle <= 0! | {{Cl|_FREEFONT}} f& 'returns an error if handle <= 0! | ||
{{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$}}(27) {{Cl|THEN}} {{Cl|EXIT DO}} | ||
Line 50: | Line 50: | ||
{{Cl|CLOSE}} | {{Cl|CLOSE}} | ||
{{Cl|PRINT}}: {{Cl|PRINT}}: {{Cl|PRINT}} "Found"; found; "TTF files,"; OK; "can use Monospace," | {{Cl|PRINT}}: {{Cl|PRINT}}: {{Cl|PRINT}} "Found"; found; "TTF files,"; OK; "can use Monospace," | ||
{{Cl|END}} | {{Cl|END}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{small|Code by Ted Weissgerber}} | {{small|Code by Ted Weissgerber}} | ||
Line 58: | Line 58: | ||
''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$}}("SYSTEMROOT") + "\fonts\lucon.ttf" | ||
style$ = "MONOSPACE, ITALIC, BOLD" | style$ = "MONOSPACE, ITALIC, BOLD" | ||
fontsize% = 20 | fontsize% = 20 | ||
Line 81: | Line 81: | ||
{{Cl|END}} | {{Cl|END}} | ||
ClearFont: | ClearFont: | ||
{{Cl|IF}} font& > 0 {{Cl|THEN}} | {{Cl|IF}} font& > 0 {{Cl|THEN}} | ||
{{Cl|_FONT|_FONT }}16 'change used font to the QB64 8x16 default font | {{Cl|_FONT|_FONT }}16 'change used font to the QB64 8x16 default font | ||
Line 88: | Line 88: | ||
{{Cl|ELSE}} : {{Cl|PRINT}}: {{Cl|PRINT}} "_FREEFONT was not used!" | {{Cl|ELSE}} : {{Cl|PRINT}}: {{Cl|PRINT}} "_FREEFONT was not used!" | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|RETURN}} | {{Cl|RETURN}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
Revision as of 01:40, 23 January 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 (Template:KW 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 |
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