- Main Page
- Classes

- Class List
- Class Hierarchy
- Class Members

# JTTFont Class Reference

`#include <JTTFont.h>`

Collaboration diagram for JTTFont:

List of all members.

## Public Types
enum | FONT_LOADING_MODE { MODE_NORMAL, MODE_PRECACHE_ASCII, MODE_PRECACHE_ASCII_EX }
enum | CACHE_IMAGE_SIZE { CACHE_IMAGE_64x64, CACHE_IMAGE_128x128, CACHE_IMAGE_256x256, CACHE_IMAGE_512x512 }
## Public Member Functions
| JTTFont (int cacheImageSize=CACHE_IMAGE_256x256)
void | SetColor (PIXEL_TYPE color)
void | SetAngle (float angle)
bool | SetSize (int size)
bool | Load (const char *filename, int size=12, int mode=MODE_NORMAL)
bool | Load (JTTFont *fontSource, int size=12, int mode=MODE_NORMAL)
void | Unload (void)
void | RenderString (const u16 *text, float x, float y, int alignment=JGETEXT_LEFT)
void | RenderString (const char *text, float x, float y, int alignment=JGETEXT_LEFT)
void | RenderString (const u8 *text, float x, float y, int alignment=JGETEXT_LEFT)
void | PreCacheString (const u16 *text)
void | PreCacheString (const char *text)
void | PreCacheString (const u8 *text)
int | GetWidth (const u16 *text)
int | GetWidth (const char *text)
int | GetWidth (const u8 *text)
bool | PreCacheASCII ()
bool | PreCacheExtendedASCII ()

## Detailed Description

True Type font support with the help of Freetype library. JTTFont has a simple caching system so that a character which has been rendered before can be retrieved from the cache instead of drawing it again by the Freetype library. This can give you a much faster rendering speed. Also, if you only need to use a limited number of characters in your game, you can actually cache all your characters in the cache beforehand and unload the font to save memory.

For example, if you only want to use the standard ASCII characters in
  your game:

     // in Create()
     mTTFont = new JTTFont();
     mTTFont->Load("arial.ttf", 32);                // size 32

     if (mTTFont->PreCacheASCII())
                    mTTFont->Unload();
     ...

     // in Render()
     mTTFont->RenderString("Hello World!", 240, 80, JGETEXT_CENTER);

## Member Enumeration Documentation

enum JTTFont::FONT_LOADING_MODE

Font loading options.

Enumerator:

  MODE_NORMAL | Load only.
  MODE_PRECACHE_ASCII | Load the font and cache all ASCII characters.
  MODE_PRECACHE_ASCII_EX | Load the font and cache all Extended ASCII characters.

enum JTTFont::CACHE_IMAGE_SIZE

Size of the texture used for caching.

Enumerator:

  CACHE_IMAGE_64x64 | 64x64
  CACHE_IMAGE_128x128 | 128x128
  CACHE_IMAGE_256x256 | 256x256
  CACHE_IMAGE_512x512 | 512x512

## Constructor & Destructor Documentation

JTTFont::JTTFont | ( | int | cacheImageSize = `CACHE_IMAGE_256x256` | ) |

Constructor.

Parameters:

  | cacheImageSize | - Size of the texture used for caching. This can be 64x64, 128x128(default), 256x256 or 512x512.

## Member Function Documentation

void JTTFont::SetColor | ( | PIXEL_TYPE | color | ) |

Set color of font.

Parameters:

  | color | - Font color.

void JTTFont::SetAngle | ( | float | angle | ) |

Set angle of the font for rendering.

Parameters:

  | angle | - Angle in radians.

bool JTTFont::SetSize | ( | int | size | ) |

Set font size.

Parameters:

  | size | - Font size.

Note:
  Setting font size will clear the cache.

bool JTTFont::Load | ( | const char * | filename,
|  | int | size = `12`,
|  | int | mode = `MODE_NORMAL` |
| ) |  |  |

Load font file.

Parameters:

  | filename | - Name of True Type font.
  | size | - Initial font size. Default is 12.
  | mode | - Loading mode.

Returns:
  - True if no error.

bool JTTFont::Load | ( | JTTFont * | fontSource,
|  | int | size = `12`,
|  | int | mode = `MODE_NORMAL` |
| ) |  |  |

Create font using font data from another JTTFont instance.

Parameters:

  | fontSource | - Source of font data.
  | size | - Initial font size. Default is 12.
  | mode | - Loading mode.

Returns:
  - True if no error.

void JTTFont::Unload | ( | void |  | ) |

Unload font file and related Freetype objects from memory.

void JTTFont::RenderString | ( | const u16 * | text,
|  | float | x,
|  | float | y,
|  | int | alignment = `JGETEXT_LEFT` |
| ) |  |  |

Render Unicode string to screen.

Parameters:

  | text | - NULL terminated Unicode-16 string.
  | x | - X position.
  | y | - Y position.
  | alignment | - Text alignment: JGETEXT_LEFT, JGETEXT_RIGHT, JGETEXT_CENTER

void JTTFont::RenderString | ( | const char * | text,
|  | float | x,
|  | float | y,
|  | int | alignment = `JGETEXT_LEFT` |
| ) |  |  |

Render ASCII string to screen.

Parameters:

  | text | - NULL terminated ASCII string.
  | x | - X position.
  | y | - Y position.
  | alignment | - Text alignment: JGETEXT_LEFT, JGETEXT_RIGHT, JGETEXT_CENTER

void JTTFont::RenderString | ( | const u8 * | text,
|  | float | x,
|  | float | y,
|  | int | alignment = `JGETEXT_LEFT` |
| ) |  |  |

Render Chinese (GBK) string to screen.

Parameters:

  | text | - NULL terminated GBK encoded string.
  | x | - X position.
  | y | - Y position.
  | alignment | - Text alignment: JGETEXT_LEFT, JGETEXT_RIGHT, JGETEXT_CENTER

void JTTFont::PreCacheString | ( | const u16 * | text | ) |

Put characters of an Unicode string into cache

Parameters:

  | text | - NULL terminated Unicode-16 string.

void JTTFont::PreCacheString | ( | const char * | text | ) |

Put characters of an ASCII string into cache.

Parameters:

  | text | - NULL terminated ASCII string.

void JTTFont::PreCacheString | ( | const u8 * | text | ) |

Put characters of a Chinese (GBK) string into cache.

Parameters:

  | text | - NULL terminated GBK encoded string.

int JTTFont::GetWidth | ( | const u16 * | text | ) |

Get width of Unicode string on screen.

Parameters:

  | text | - NULL terminated Unicode-16 string.

Returns:
  - Width in pixels

int JTTFont::GetWidth | ( | const char * | text | ) |

Get width of ASCII string on screen.

Parameters:

  | text | - NULL terminated ASCII string.

Returns:
  - Width in pixels

int JTTFont::GetWidth | ( | const u8 * | text | ) |

Get width of Chinese (GBK) string on screen.

Parameters:

  | text | - NULL terminated GBK encoded string.

Returns:
  - Width in pixels

bool JTTFont::PreCacheASCII | ( |  | ) |

Put all standard ASCII characters (0x20-0x7F) into cache.

Returns:
  - True if success.

bool JTTFont::PreCacheExtendedASCII | ( |  | ) |

Put all ASCII characters (0x20-0xFF) into cache.

Returns:
  - True if success.