Difference between revisions of "LOADFONT"

From QB64 Wiki
Jump to navigation Jump to search
Line 1: Line 1:
The '''_LOADFONT''' function loads a TrueType font (.TTF) and returns a font handle value.
The '''_LOADFONT''' function loads a TrueType font (.TTF) file and returns a font handle value.

Revision as of 16:45, 1 June 2011

The _LOADFONT function loads a TrueType font (.TTF) file and returns a font handle value.


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


  • The assigned LONG font handle variable return value designates a font style to be used somewhere in a 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.
  • Size is the height of the font. Font sizes can be found using the _FONTHEIGHT function. Font sizes also affect SCREEN sizes!
  • Style parameter(s) used, if any, are literal(in quotes) or variable text parameters. "Monospace" has limited font file selections.
  • "DONTBLEND" turns off _ALPHA blending of fonts. This can also be done with the _DONTBLEND statement.
  • You can pass different font styles using different predefined STRING variable lists. You can include an empty style string!
  • Always check that font handle values are greater than 0 before using them or illegal function errors may occur!
  • NOTE: SCREEN 0 can only use ONE font on a screen page! Thus a style like underline would affect the entire page.
  • _FONTWIDTH can only measure MONOSPACE fonts! "MONOSPACE" cannot be used on a variable width font.
  • _PRINTWIDTH can measure the width of a string of text in graphics modes only. Use one character to get the font's width.

Font Handles
  • Multiple fonts will require multiple font variable handles unless used and freed consecutively.
  • Font handles with values greater than 0 that are no longer used should be freed using _FREEFONT.
  • Predefined QB64 font handle numbers can be substituted before freeing a font handle:
    • _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 only.
  • Once the font is changed to a predefined value, the font handle value can be freed using _FREEFONT for another font type.
  • Font handle values of -1 (load failure) do not need to be freed! An error will occur if you try to free invalid handles!

Font File Specs
  • Windows users should find TTF font files in the C:\WINDOWS\FONTS folder, but don't depend on unusual ones being there.
  • Check the font file name! The name in the "viewer" is NOT necessarily the file's name! Use the name in properties!
  • 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.

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!"


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!"


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.

See also:

Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page