Difference between revisions of "LOADFONT"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
Line 3: Line 3:
  
 
{{PageSyntax}}
 
{{PageSyntax}}
: handle& = {{KW|_LOADFONT}} (ttf_filename$, height[, "BOLD|, ITALIC|, UNDERLINE|, DONTBLEND|, MONOSPACE"])
+
: handle& = {{KW|_LOADFONT}} (ttf_filename$, size[, "MONOSPACE|, BOLD|, ITALIC|, UNDERLINE|, DONTBLEND"])
  
  
Line 9: Line 9:
  
 
{{PageDescription}}
 
{{PageDescription}}
* The assigned ''font_handle'' variable return value designates the font to use. A return of -1 indicates a font loading failure.
+
* The assigned [[LONG]] font ''handle'' variable return value designates a font style to be used somewhere in a program.
* Multiple fonts will require multiple font variables unless used and freed consecutively.
+
* '''Always check that font handle values are greater than 0 before using them or [[ERROR Codes|illegal function errors]] may occur!'''
* ''TTF_filename$'' is the filename of '''truetype''' fonts only. Can include the path to the font file. Best to include with program.
+
* ''TTF_filename$'' is the filename of '''truetype''' fonts only. Can include the path to the font file. Best to include font files with a program.
 +
* If a program is on a different drive than Windows, [[ENVIRON$]]("SYSTEMROOT") will return the path to the "WINDOWS" folder. Normally "C:\WINDOWS". Then add the "\FONTS\" folder and the font '''.TTF''' filename to the path [[STRING]].
 +
* ''Size'' is the height of the font. Font sizes can be found using the [[_FONTHEIGHT]] and [[_FONTWIDTH]] functions.
 +
* Font ''size''s will also affect the [[SCREEN (statement)|SCREEN]] sizes!
 +
* ''Style'' parameter(s) used, if any, are literal(in quotes) or variable text parameters. "Monospace" has limited font file selections.
 +
* You can pass different font styles using different predefined [[STRING]] variable lists. You '''can''' include an empty style string!
 +
* Multiple fonts will require multiple font variable handles unless used and freed consecutively.
 
* Windows users should find '''TTF''' font files in the C:\WINDOWS\FONTS folder. Use the font properties to see the '''real''' filename.
 
* Windows users should find '''TTF''' font files in the C:\WINDOWS\FONTS folder. Use the font properties to see the '''real''' filename.
* ''Height'' is the height of the font. Font sizes can be found using the [[_FONTHEIGHT]] and [[_FONTWIDTH]] functions.
+
* Font handles with values greater than 0 that are '''no longer used''' should be freed using [[_FREEFONT]].  
* ''Style'' parameter(s) used, if any, are literal(in quotes) or variable text parameters. Monospace has limited font selections.
+
* Font handle values of -1 (load failure) '''do not''' need to be freed! '''An [[ERROR Codes|error]] will occur if you try to free invalid handles!'''
* You can pass different font styles using different predefined [[STRING]] variable lists. You '''can''' include an empty style string!
+
 
* Font handles with values greater than 0 and are no longer needed should be freed using [[_FREEFONT]].  
 
* Font handle values of -1 (load failure) '''do not''' need to be freed! An [[ERROR Codes|error]] will occur if you try to free invalid handles!
 
* Check that the font values are greater than 0 before using them or [[ERROR Codes|illegal function errors]] may occur!
 
  
  
Line 27: Line 30:
 
{{CodeStart}}
 
{{CodeStart}}
  
font$ = "C:\WINDOWS\Fonts\cour.ttf"         'TTF file in Windows  
+
rootpath$ = {{Cl|ENVIRON$}}("SYSTEMROOT")          'normally "C:\WINDOWS"
style$ = "monospace, italic, bold"          'font style
+
fontfile$ = rootpath$ + "\Fonts\cour.ttf"   'TTF file in Windows  
f& ={{Cl|_LOADFONT}}(font$, 30, style$)
+
style$ = "monospace, italic, bold"          'font style is not case sensitive
 +
f& ={{Cl|_LOADFONT}}(fontfile$, 30, style$)
 
{{Cl|_FONT}} f&
 
{{Cl|_FONT}} f&
 
{{Cl|PRINT}} "Hello!"
 
{{Cl|PRINT}} "Hello!"

Revision as of 21:33, 22 August 2010

The _LOADFONT function returns a font handle for a TrueType font (.TTF).


Syntax

handle& = _LOADFONT (ttf_filename$, size[, "MONOSPACE|, BOLD|, ITALIC|, UNDERLINE|, DONTBLEND"])



Description

  • The assigned LONG font handle variable return value designates a font style to be used somewhere in a program.
  • Always check that font handle values are greater than 0 before using them or illegal function errors may occur!
  • TTF_filename$ is the filename of truetype fonts only. Can include the path to the font file. Best to include font files with a program.
  • If a program is on a different drive than Windows, ENVIRON$("SYSTEMROOT") will return the path to the "WINDOWS" folder. Normally "C:\WINDOWS". Then add the "\FONTS\" folder and the font .TTF filename to the path STRING.
  • Size is the height of the font. Font sizes can be found using the _FONTHEIGHT and _FONTWIDTH functions.
  • Font sizes will also affect the SCREEN sizes!
  • Style parameter(s) used, if any, are literal(in quotes) or variable text parameters. "Monospace" has limited font file selections.
  • You can pass different font styles using different predefined STRING variable lists. You can include an empty style string!
  • Multiple fonts will require multiple font variable handles unless used and freed consecutively.
  • Windows users should find TTF font files in the C:\WINDOWS\FONTS folder. Use the font properties to see the real filename.
  • Font handles with values greater than 0 that are no longer used should be freed using _FREEFONT.
  • Font handle values of -1 (load failure) do not need to be freed! An error will occur if you try to free invalid handles!


Examples

Example 1: You need to know that if you are in a text mode (such as SCREEN 0 - the default) then you will only be able to use mono-spaced (fixed width) fonts.

rootpath$ = ENVIRON$("SYSTEMROOT") 'normally "C:\WINDOWS" fontfile$ = rootpath$ + "\Fonts\cour.ttf" 'TTF file in Windows style$ = "monospace, italic, bold" 'font style is not case sensitive f& =_LOADFONT(fontfile$, 30, style$) _FONT f& PRINT "Hello!"

Hello!

Note: 30 means each row of text (including vertical spacing) will be exactly 30 pixels high. This may make some program screens larger. If you don't want a style listed just use style$ = "" if using a STRING variable for different calls.


Example 2: In a 32-bit graphics mode you can alpha blend onto the background:

i& =_NEWIMAGE(800,600,32) SCREEN i& COLOR &HC0FFFF00,&H200000FF f& =_LOADFONT("C:\Windows\Fonts\times.ttf", 25) 'normal style PRINT "Hello!"

Hello!


Note: You can load a fixed width font file without using the "monospace" option and it will be treated as variable width. This can be useful because LOCATE treats the horizontal position as an offset in pixels for variable width fonts.


QB64 Font SUBs and Functions:

SUB _FREEFONT font_handle
SUB _FONT font_handle[,image_handle]
FUNC _FONT (function) font_handle = _FONT[(image_handle)]
SUB _PRINTSTRING (x,y), text_to_print$[,image_handle]
FUNC _PRINTWIDTH width_in_pixels = _PRINTWIDTH(text_to_print$[,image_handle])
SUB _PRINTMODE _FILLBACKGROUND/_KEEPBACKGROUND/_ONLYBACKGROUND[,image_handle]
FUNC _PRINTMODE (function) 1 keepbackground/2 onlybackground/3 fillbackground =_PRINTMODE[(image_handle)]
FUNC _FONTHEIGHT character_height_in_pixels = _FONTHEIGHT[(font_handle)]
FUNC _FONTWIDTH character_width_in_pixels = _FONTWIDTH[(font_handle)]




Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page