graphics/ font.bi

Summary
graphics/ font.bi
LicenseCopyright © 2009, FreeBASIC Extended Library Development Group
ext. gfx.font
Functions
custom_infoProvides an easy way to load information about a custom draw string font.
GetTextWidthReturns the width of a string in a particular draw string font.
Enumerations
FontErrorError values that can be returned by the loadttf function.
Functions
loadttfLoads a truetype font to a Draw String compatible font buffer, not supported or defined on DOS.

License

Copyright © 2009, FreeBASIC Extended Library Development Group

Distributed under the FreeBASIC Extended Library Group license.  See accompanying file LICENSE.txt or copy at http://code.google.com- /p- /fb-extended-lib- /wiki- /License

ext. gfx.font

Summary
Functions
custom_infoProvides an easy way to load information about a custom draw string font.
GetTextWidthReturns the width of a string in a particular draw string font.
Enumerations
FontErrorError values that can be returned by the loadttf function.
Functions
loadttfLoads a truetype font to a Draw String compatible font buffer, not supported or defined on DOS.

Functions

custom_info

declare function custom_info(byval buf as FB.IMAGE ptr,
byref first_ as const string,
byref last_ as const string,
widths() as const ubyte) as ext.bool

Provides an easy way to load information about a custom draw string font.

Parameters

bufthe FB.IMAGE buffer containing your font.
first_string containing the first character your font supports.
last_string containing the last character your font supports.
widths()ubyte array containing the width values for each character.

Returns

Returns one of the values defined by ext.bool.

GetTextWidth

declare function GetTextWidth(byval fnt as const FB.IMAGE ptr,
byref xstr as const string) as uinteger

Returns the width of a string in a particular draw string font.

Parameters

fntFB.IMAGE ptr to the draw string font.
xstrthe string to return the width of.

Returns

Uinteger containing the pixel width of the specified string in the passed font.

Enumerations

FontError

Error values that can be returned by the loadttf function.  Not supported or defined on DOS.

ftinitunable to initialize the freetype library.
noloadfontunable to locate the font file.
cantsizefreetype was unable to size the font to the value you specified.
noloadcharfreetype can’t find one of the characters you requested in the font file.
cantrendercharfreetype encountered an error attempting to render one of the characters.

Functions

loadttf

declare function loadttf (byref fontname as Const string,  
byref img as FB.IMAGE ptr,  
byval range_lo as integer =  1,
byval range_hi as integer =  255,
byval font_size as integer =  14,
byval colour as uinteger =  &HFFFFFF) as integer

Loads a truetype font to a Draw String compatible font buffer, not supported or defined on DOS.

Parameters

fontnamestring containing the path and filename of the font to load.
imgun-initialized FB.IMAGE ptr to draw the font on.
range_lothe lowest character your font should support.  Defaults to 1.
range_hithe highest character your font should support.  Defaults to 255.
font_sizerequested size of the font in pixels, not exact.  Defaults to 14.
colourthe colour to use when drawing the font, defaults to white or &hFFFFFF.

Returns

Integer value indicating success or failure.

Usage

You may use # as a shortcut for the common font directories on Windows and Linux. e.g.  “#arial.ttf” on windows expands to “c:/Windows/Fonts/arial.ttf” and on Linux will expand to “/usr/share/fonts/truetype/arial.ttf”.  Certain fonts at certain sizes may show some artifacting.  We’ve tried to minimize this, but it seems like a feature of freetype and the TrueType standard.  Due to the way FreeType loads the font, you will have to use the ALPHA drawing method with Draw String for your font to be displayed properly.

#include once "ext/graphics/font.bi"
#include once "fbgfx.bi"

using ext

dim as FB.IMAGE ptr myFont

screenres 320, 240, 32

if gfx.loadttf( "Vera.ttf", myFont ) = 1 then

  draw string (10,10), "Hello, World!", ,myFont, ALPHA

else

  print "Error loading font."

end if

imagedestroy myFont
declare function custom_info(byval buf as FB.IMAGE ptr,
byref first_ as const string,
byref last_ as const string,
widths() as const ubyte) as ext.bool
Provides an easy way to load information about a custom draw string font.
declare function GetTextWidth(byval fnt as const FB.IMAGE ptr,
byref xstr as const string) as uinteger
Returns the width of a string in a particular draw string font.
declare function loadttf (byref fontname as Const string,  
byref img as FB.IMAGE ptr,  
byval range_lo as integer =  1,
byval range_hi as integer =  255,
byval font_size as integer =  14,
byval colour as uinteger =  &HFFFFFF) as integer
Loads a truetype font to a Draw String compatible font buffer, not supported or defined on DOS.