Font Struct Reference

Inheritance diagram for Font

Inheritance graph

Collaboration diagram for Font:

Collaboration graph


Public Methods

struct Font* Font_ctor (struct Font *ptr_font)
struct Font* Font_ctor_Ex (struct Font *ptr_font, char *file_name, bool fixed, int spacing)
void Font_dtor (struct Font *ptr_font, int memory_flag)
char* Font_get_name (struct Font *ptr_font)
void Font_set_name (struct Font *ptr_font, char *filename)
int Font_get_char_width (struct Font *ptr_font, int chr)
int Font_string_width (struct Font *ptr_font, char *str)
int Font_string_width_Ex (struct Font *ptr_font, char *str, int len)
int Font_get_char_height (struct Font *ptr_font)
bool Font_is_fixed (struct Font *ptr_font)
void Font_set_fixed (struct Font *ptr_font, bool fixed)
int Font_get_spacing (struct Font *ptr_font)
void Font_set_spacing (struct Font *ptr_font, int spacing)
bool Font_load (struct Font *ptr_font, struct Input *input)
struct BitmapFont_bmp_by_char (struct Font *ptr_font, int chr)
char* Font_split_string (struct Font *ptr_font, char **str, int width, int *len)
int Font_get_size (struct Font *ptr_font)
int Font_get_x (struct Font *ptr_font, int bitmap_index)
int Font_get_y (struct Font *ptr_font, int bitmap_index)
struct BitmapFont_get_bitmap (struct Font *ptr_font, int bitmap_index)
bool Font_store (struct Font *ptr_font, struct Output *output)

Public Attributes

struct Font* cool_bold_font
struct Font* cool_normal_font
struct Font* mini_bold_font
struct Font* mini_normal_font


Detailed Description

To use a Font object, construct it with the Font_ctor or Font_ctor_Ex functions. Use Font_dtor to deconstruct a Font object after use. Font object operations include receiving information about the character width and height, string length or length of a predefined number of characters in a string; as well as receiving the Bitmap representation of any character in a string.

You can fix the current font by calling the Font_set_fixed function. You can set the font spacing value by calling the Font_set_spacing function. You can also use the extended version of the Font_ctor function, which calls Font_ctor_Ex and enables it to create a font object using a resource.

Since all characters in a text string are elements of a BitmapSequence object, you can manipulate a text string as if it were a BitmapSequence object.

See also:
Fonts and Text


Member Function Documentation

struct Bitmap * Font_bmp_by_char ( struct Font * ptr_font,
int chr )
 

Returns the bitmap object of the 'chr' character.
(Bitmap representation of the 'chr' character).

Parameters:
ptr_font   A pointer to the initialized Font object
chr   A character
Returns:
A pointer to a Bitmap object that represents the 'chr' character
   #include <cybiko.h>
     ...
     struct Font   font;
     struct Bitmap * bmp;
     ...
     Font_ctor_Ex( &font, "MyFont.fnt", TRUE, 5 );
     ...
     bmp = Font_bmp_by_char( &font, 'c' );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
     Bitmap_dtor( bmp, FREE_MEMORY );

struct Font * Font_ctor ( struct Font * ptr_font )
 

A simple Font constructor.
Creates an empty Font object.

Parameters:
ptr_font   A pointer to the Font structure
Returns:
A pointer to the initialized Font object
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor( &font );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_ctor_Ex.

struct Font * Font_ctor_Ex ( struct Font * ptr_font,
char * file_name,
bool fixed,
int spacing )
 

The extended version of the Font_ctor function.
It creates a Font object from a file.

Parameters:
ptr_font   A pointer to the Font structure
file_name   The name of a file that contains a font
fixed   sets TRUE for fonts with a fixed width
spacing   The font spacing value in pixels
Returns:
A pointer to the initialized Font object
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor_Ex( &font, "MyFont.fnt", TRUE, 5 );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_ctor.

void Font_dtor ( struct Font * ptr_font,
int memory_flag )
 

Destructor.
Deletes a Font object.

Parameters:
ptr_font   A pointer to the initialized Font object
memory_flag   Can be FREE_MEMORY or LEAVE_MEMORY. If the memory was allocated for the object by malloc(), use FREE_MEMORY to free it. Use LEAVE_MEMORY If the object was static or allocated in a stackcan
Returns:
None
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor( &font );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
FREE_MEMORY, LEAVE_MEMORY.

struct Bitmap * Font_get_bitmap ( struct Font * ptr_font,
int bitmap_index )
 

Returns a pointer to the bitmap with a specified index.

Parameters:
ptr_font   A pointer to the initialized Font object
bitmap_index   The bitmap's index
Returns:
A pointer to the bitmap with the specified index

int Font_get_char_height ( struct Font * ptr_font )
 

Returns the character's height in pixels.

Parameters:
ptr_font   A pointer to the initialized Font object
Returns:
The character's height in pixels
   #include <cybiko.h>
     ...
     struct Font font;
     int char_height;
     ...
     Font_ctor( &font );
     ...
     char_height = Font_get_char_height( &font );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_get_char_width.

int Font_get_char_width ( struct Font * ptr_font,
int chr )
 

Returns the character's width in pixels.

Parameters:
ptr_font   A pointer to the initialized Font object
chr   A character
Returns:
The character's width in pixels
   #include <cybiko.h>
     ...
     struct Font font;
     int char_width;
     Font_ctor( &font );
     ...
     char_width = Font_get_char_width( &font, 'c' );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_get_char_height, Font_string_width.

char * Font_get_name ( struct Font * ptr_font )
 

Returns the font name.
This is equal to the full font file name.

Parameters:
ptr_font   A pointer to the initialized Font object
Returns:
The font name
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor( &font );
     ...
     char*  ptr_font_name;
     ptr_font_name = Font_get_name( &font );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_set_name.

int Font_get_size ( struct Font * ptr_font )
 

Returns the number of bitmaps in a font object.

Parameters:
ptr_font   A pointer to the initialized Font object
Returns:
The number of bitmaps in a font object

int Font_get_spacing ( struct Font * ptr_font )
 

Returns the font spacing value.

Parameters:
ptr_font   A pointer to the initialized Font object
Returns:
The font spacing value
   #include <cybiko.h>
     ...
     struct Font font;
     int    font_space;
     Font_ctor_Ex( &font, "MyFont.fnt", TRUE, 5 );
     ...
     font_space = Font_get_spacing( &font );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_set_spacing.

int Font_get_x ( struct Font * ptr_font,
int bitmap_index )
 

Returns the X origin of the Nth bitmap in a font object.

Parameters:
ptr_font   A pointer to the initialized Font object
bitmap_index   The bitmap's index
Returns:
The X origin of the Nth bitmap in a font object
See also:
Font_get_y

int Font_get_y ( struct Font * ptr_font,
int bitmap_index )
 

Returns the Y origin of the Nth bitmap in a font object.

Parameters:
ptr_font   A pointer to the initialized Font object
bitmap_index   The bitmap's index
Returns:
The Y origin of the Nth bitmap in a font object
See also:
Font_get_x

bool Font_is_fixed ( struct Font * ptr_font )
 

Determines if the font is fixed.

Parameters:
ptr_font   A pointer to the initialized Font object
Returns:
TRUE if the font is fixed
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor_Ex( &font, "MyFont.fnt", TRUE, 5 );
     ...
     if ( !Font_is_fixed( &font ) )
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_set_fixed.

bool Font_load ( struct Font * ptr_font,
struct Input * input )
 

Loads the font from an opened stream.

Parameters:
ptr_font   A pointer to the initialized Font object
input   A pointer to an opened stream
Returns:
TRUE if the font was loaded successfully
   #include <cybiko.h>
     ...
     struct Input * ptr_input;
     struct Font  font;
     ...
     // Your make file must have the archive resource "bmp.pic" linked by filer.exe.
     // For example, see the Cybiko Compiler's Introduction Section.
     ptr_input = open_resource_Ex( "font_1.pic" );
     ...
     if( !Font_load( &font, ptr_input ) )
     ...
     Font_dtor( &font, LEAVE_MEMORY );
     Input_dtor( ptr_input, FREE_MEMORY );

void Font_set_fixed ( struct Font * ptr_font,
bool fixed )
 

Sets or doesn't set a fixed font.

Parameters:
ptr_font   A pointer to the initialized Font object
fixed   TRUE if it's need to set a fixed font
Returns:
None
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor_Ex( &font, "MyFont.fnt", TRUE, 5 );
     ...
     Font_set_fixed( &font, FALSE );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_is_fixed.

void Font_set_name ( struct Font * ptr_font,
char * filename )
 

Sets the font name manually.

Parameters:
ptr_font   A pointer to the initialized Font object
filename   The file name
Returns:
None
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor( &font );
     ...
     Font_set_name( &font, "MyFont.fnt" );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_get_name.

void Font_set_spacing ( struct Font * ptr_font,
int spacing )
 

Sets the font spacing value.

Parameters:
ptr_font   A pointer to the initialized Font object
spacing   A font's spacing value
Returns:
None
   #include <cybiko.h>
     ...
     struct Font font;
     Font_ctor_Ex( &font, "MyFont.fnt", TRUE, 5 );
     ...
     if ( Font_get_spacing( &font ) < 3 )
       Font_set_spacing( &font, 5 );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_get_spacing.

char * Font_split_string ( struct Font * ptr_font,
char ** str,
int width,
int * len )
 

Splits string str to substrings that fit specified width.
This function doesn't modify the source string, but calculates start and length of each substring instead.

The first call to this function returns an address of the source string and calculates first substring's length. Subsequent calls return remaining substrings' addresses and lengths, in order.

The string may contain explicit line breaks ('\n').

Parameters:
ptr_font   A pointer to a font which metrics will be used to measure the string width
str   A string to be splitted; this parameter is modified by the function to handle subsequent calls
width   Width, in pixels, to fit the source text in
len   Returns length of the current substring, in characters
Returns:
A pointer to the current substring, or NULL if no text is left
   #include <cybiko.h>
     ...
     char *str, *ptr;                                                       
     int  y = 0;                                                            
     int  len;                                                              
     struct module_t main_module;                                           
     ...                                                                    
     init_module( &main_module );                                           
     ...                                                                    
     str = "Any    string";                                                 
     while( ptr = Font_split_string( cool_normal_font, &str, 6, &len ) )    
     {                                                                      
       Graphics_draw_text_Ex( main_module.m_gfx, ptr, 0, y*12, len );       
       y++;                                                                 
     }                                                                      
     ...                                                                    

bool Font_store ( struct Font * ptr_font,
struct Output * output )
 

Stores a font object to an opened stream.

Parameters:
ptr_font   A pointer to the initialized Font object
output   A pointer to the Output stream
Returns:
TRUE if the font sequence was stored successfully

int Font_string_width ( struct Font * ptr_font,
char * str )
 

Returns the string's width in pixels.

Parameters:
ptr_font   A pointer to the initialized Font object
str   A string
Returns:
The string's width in pixels
   #include <cybiko.h>
     ...
     struct Font font;
     int    str_width;
     char*  str = "any string";
     ...
     Font_ctor( &font );
     ...
     str_width = Font_string_width( &font, str );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_get_char_width, Font_string_width_Ex.

int Font_string_width_Ex ( struct Font * ptr_font,
char * str,
int len )
 

The extended version of the Font_string_width function.
This function returns the width in pixels of the first 'flen' characters of a string.

Parameters:
ptr_font   A pointer to the initialized Font object
str   A string
len   The number of characters needed
Returns:
The string's width in pixels
   #include <cybiko.h>
     ...
     struct Font font;
     int    str_width;
     char*  str = "any string";
     ...
     Font_ctor( &font );
     ...
     str_width = Font_string_width_Ex( &font, str, 3 );
     ...
     Font_dtor( &font, LEAVE_MEMORY );
See also:
Font_string_width, Font_get_char_width.


Member Data Documentation

struct Font * cool_bold_font
 

Handle for the 'cool bold' font.

struct Font * cool_normal_font
 

Handle for the 'cool normal' font.

struct Font * mini_bold_font
 

Handle for the 'mini bold' font.

struct Font * mini_normal_font
 

Handle for the 'mini normal' font.