QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The _LOADFONT function loads a TrueType font (.TTF) or an OpenType font (.OTF) file in a specific size and style and returns a LONG font handle.

Syntax

handle& = _LOADFONT(fontFileName$, size%[, “{MONOSPACE , BOLD , ITALIC , UNDERLINE , UNICODE , DONTBLEND}”])

Description

Font Handles

Font File Specs

Example(s)

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"          '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.

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.

Loading a Unicode font, cyberbit.ttf, which was downloaded with QB64:


SCREEN 12

DECLARE CUSTOMTYPE LIBRARY 'Directory Information using KERNEL32 provided by Dav
    FUNCTION GetModuleFileNameA& (BYVAL hModule AS LONG, lpFileName AS STRING, BYVAL nSize AS LONG)
    FUNCTION GetModuleFileNameW& (BYVAL hModule AS LONG, lpFileName AS STRING, BYVAL nSize AS LONG)
END DECLARE

'=== SHOW CURRENT PROGRAM
FileName$ = SPACE$(512)

Result = GetModuleFileNameA(0, FileName$, LEN(FileName$))
IF Result THEN PRINT "CURRENT PROGRAM (ASCII): "; LEFT$(FileName$, Result)

'load a unicode font
f = _LOADFONT("cyberbit.ttf", 24, "UNICODE")
_FONT f
Result = GetModuleFileNameW(0, FileName$, LEN(FileName$) \ 2)
LOCATE 2, 1
PRINT QuickCP437toUTF32$("CURRENT PROGRAM (UTF): ") + QuickUTF16toUTF32$(LEFT$(FileName$, Result * 2))
_FONT 16 'restore CP437 font

FUNCTION QuickCP437toUTF32$ (a$)
b$ = STRING$(LEN(a$) * 4, 0)
FOR i = 1 TO LEN(a$)
    ASC(b$, i * 4 - 3) = ASC(a$, i)
NEXT
QuickCP437toUTF32$ = b$
END FUNCTION

FUNCTION QuickUTF16toUTF32$ (a$)
b$ = STRING$(LEN(a$) * 2, 0)
FOR i = 1 TO LEN(a$) \ 2
    ASC(b$, i * 4 - 3) = ASC(a$, i * 2 - 1)
    ASC(b$, i * 4 - 2) = ASC(a$, i * 2)
NEXT
QuickUTF16toUTF32$ = b$
END FUNCTION 

See Also