CUGL
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::FontLoader Class Reference

#include <CUFontLoader.h>

Inheritance diagram for cugl::FontLoader:
cugl::Loader< Font > cugl::BaseLoader

Public Member Functions

 FontLoader ()
 
void dispose () override
 
bool load (const std::string &key, const std::string &source, int size)
 
bool load (const char *key, const std::string &source, int size)
 
bool load (const std::string &key, const char *source, int size)
 
bool load (const char *key, const char *source, int size)
 
void loadAsync (const std::string &key, const std::string &source, int size, LoaderCallback callback)
 
void loadAsync (const char *key, const std::string &source, int size, LoaderCallback callback)
 
void loadAsync (const std::string &key, const char *source, int size, LoaderCallback callback)
 
void loadAsync (const char *key, const char *source, int size, LoaderCallback callback)
 
float getFontSize () const
 
void setDefaultSize (float size)
 
const std::string & getCharacterSet () const
 
void setCharacterSet (const std::string &charset)
 
- Public Member Functions inherited from cugl::Loader< Font >
 Loader ()
 
std::shared_ptr< Fontget (const std::string &key) const
 
std::shared_ptr< Fontget (const char *key) const
 
std::shared_ptr< Fontoperator[] (const std::string &key) const
 
std::shared_ptr< Fontoperator[] (const char *key) const
 
size_t loadCount () const override
 
size_t waitCount () const override
 
void unloadAll () override
 
- Public Member Functions inherited from cugl::BaseLoader
 BaseLoader ()
 
 ~BaseLoader ()
 
virtual bool init ()
 
virtual bool init (const std::shared_ptr< ThreadPool > &threads)
 
std::shared_ptr< BaseLoadergetHook ()
 
std::shared_ptr< ThreadPoolgetThreadPool () const
 
void setThreadPool (const std::shared_ptr< ThreadPool > &threads)
 
bool load (const std::string &key, const std::string &source)
 
bool load (const char *key, const std::string &source)
 
bool load (const std::string &key, const char *source)
 
bool load (const char *key, const char *source)
 
bool load (const std::shared_ptr< JsonValue > &json)
 
void loadAsync (const std::string &key, const std::string &source, LoaderCallback callback)
 
void loadAsync (const char *key, const std::string &source, LoaderCallback callback)
 
void loadAsync (const std::string &key, const char *source, LoaderCallback callback)
 
void loadAsync (const char *key, const char *source, LoaderCallback callback)
 
void loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
 
bool unload (const std::string &key)
 
bool unload (const char *key)
 
bool contains (const std::string &key) const
 
bool contains (const char *key) const
 
bool complete () const
 
float progress () const
 

Static Public Member Functions

static std::shared_ptr< FontLoaderalloc ()
 
static std::shared_ptr< FontLoaderalloc (const std::shared_ptr< ThreadPool > &threads)
 

Protected Member Functions

std::shared_ptr< Fontpreload (const std::string &source, const std::string &charset, int size)
 
void materialize (const std::string &key, const std::shared_ptr< Font > &font, LoaderCallback callback)
 
bool read (const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
 
bool read (const std::string &key, const std::string &source, int size, LoaderCallback callback, bool async)
 
bool read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override
 
- Protected Member Functions inherited from cugl::Loader< Font >
bool purge (const std::string &key) override
 
bool verify (const std::string &key) const override
 

Protected Attributes

int _fontsize
 
std::string _charset
 
- Protected Attributes inherited from cugl::Loader< Font >
std::unordered_map< std::string, std::shared_ptr< Font > > _assets
 
std::unordered_set< std::string > _queue
 
- Protected Attributes inherited from cugl::BaseLoader
std::shared_ptr< ThreadPool_loader
 

Detailed Description

This class is a implementation of Loader<Font>

This asset loader allows us to allocate fonts from the associated TrueType files. At load time you must specify both the TrueType file and the font size. Hence you may wish to load a font asset multiple times. If you do this, you may wish to adjust the character set. The size of the font atlas texture is determined by both the font size and the character set.

Note that this implementation uses a two phase loading system. First, it loads as much of the asset as possible without using OpenGL. This allows us to load the texture in a separate thread. It then finishes off the remainder of asset loading (particularly the OpenGL atlas generation) using Application#schedule. This is a good template for asset loaders in general.

As with all of our loaders, this loader is designed to be attached to an asset manager. Use the method getHook() to get the appropriate pointer for attaching the loader.

Constructor & Destructor Documentation

cugl::FontLoader::FontLoader ( )

Creates a new, uninitialized font loader

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a loader on the heap, use one of the static constructors instead.

Member Function Documentation

static std::shared_ptr<FontLoader> cugl::FontLoader::alloc ( )
inlinestatic

Returns a newly allocated font loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.

This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.

Returns
a newly allocated font loader.
static std::shared_ptr<FontLoader> cugl::FontLoader::alloc ( const std::shared_ptr< ThreadPool > &  threads)
inlinestatic

Returns a newly allocated font loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the OpenGL context must be active. Attempts to load an asset before this method is called will fail.

Parameters
threadsThe thread pool for asynchronous loading
Returns
a newly allocated font loader.
void cugl::FontLoader::dispose ( )
inlineoverridevirtual

Disposes all resources and assets of this loader

Any assets loaded by this object will be immediately released by the loader. However, a font may still be available if it is referenced by another smart pointer. OpenGL will only release a font atlas once all smart pointer attached to the asset are null.

Once the loader is disposed, any attempts to load a new asset will fail. You must reinitialize the loader to begin loading assets again.

Reimplemented from cugl::BaseLoader.

const std::string& cugl::FontLoader::getCharacterSet ( ) const
inline

Returns the default atlas character set

The character set determines exactly those characters in the font atlas. You should keep this value at a minimum, as it reduces the size of the atlas texture. Once set, any font processed by this loader will use this character set for its atlas.

If character set is the empty string, the atlas will contain all of the ASCII characters. This is the default value.

Returns
the default atlas character set
float cugl::FontLoader::getFontSize ( ) const
inline

Returns the default font size

Once set, any future font processed by this loader will have this size unless otherwise specified. The default is 12 point.

Returns
the default font size
bool cugl::FontLoader::load ( const std::string &  key,
const std::string &  source,
int  size 
)
inline

Synchronously loads the given asset with the specified key.

The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.

This version of load allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
Returns
true if the asset was successfully loaded
bool cugl::FontLoader::load ( const char *  key,
const std::string &  source,
int  size 
)
inline

Synchronously loads the given asset with the specified key.

The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.

This version of load allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
Returns
true if the asset was successfully loaded
bool cugl::FontLoader::load ( const std::string &  key,
const char *  source,
int  size 
)
inline

Synchronously loads the given asset with the specified key.

The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.

This version of load allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
Returns
true if the asset was successfully loaded
bool cugl::FontLoader::load ( const char *  key,
const char *  source,
int  size 
)
inline

Synchronously loads the given asset with the specified key.

The asset will be loaded synchronously, which means the main CUGL thread will block until loading is complete. When it is finished loading, the asset will be added to the contents loader, and accessible under the given key.

This version of load allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
Returns
true if the asset was successfully loaded
void cugl::FontLoader::loadAsync ( const std::string &  key,
const std::string &  source,
int  size,
LoaderCallback  callback 
)
inline

Asynchronously loads the given asset with the specified key.

The asset will be loaded asynchronously. When it is finished loading, the asset will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded asset until it is complete again.

The optional callback function will be called with the asset status when it either finishes loading or fails to load.

This version of loadAsync allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
callbackAn optional callback for asynchronous loading
void cugl::FontLoader::loadAsync ( const char *  key,
const std::string &  source,
int  size,
LoaderCallback  callback 
)
inline

Asynchronously loads the given asset with the specified key.

The asset will be loaded asynchronously. When it is finished loading, the asset will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded asset until it is complete again.

The optional callback function will be called with the asset status when it either finishes loading or fails to load.

This version of loadAsync allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
callbackAn optional callback for asynchronous loading
void cugl::FontLoader::loadAsync ( const std::string &  key,
const char *  source,
int  size,
LoaderCallback  callback 
)
inline

Asynchronously loads the given asset with the specified key.

The asset will be loaded asynchronously. When it is finished loading, the asset will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded asset until it is complete again.

The optional callback function will be called with the asset status when it either finishes loading or fails to load.

This version of loadAsync allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
callbackAn optional callback for asynchronous loading
void cugl::FontLoader::loadAsync ( const char *  key,
const char *  source,
int  size,
LoaderCallback  callback 
)
inline

Asynchronously loads the given asset with the specified key.

The asset will be loaded asynchronously. When it is finished loading, the asset will be added to this loader, and accessible under the given key. This method will mark the loading process as not complete, even if it was completed previously. It is not safe to access the loaded asset until it is complete again.

The optional callback function will be called with the asset status when it either finishes loading or fails to load.

This version of loadAsync allows you to specify the font size, overriding the default value.

Parameters
keyThe key to access the font after loading
sourceThe pathname to the font
sizeThe font size
callbackAn optional callback for asynchronous loading
void cugl::FontLoader::materialize ( const std::string &  key,
const std::shared_ptr< Font > &  font,
LoaderCallback  callback 
)
protected

Creates an atlas for the font asset, and assigns it the given key.

This method finishes the asset loading started in preload. As atlas generation requires OpenGL, this step is not safe to be done in a separate thread. Instead, it takes place in the main CUGL thread via ( Application#schedule.

The font atlas will use the character set specified in the asset.

This method supports an optional callback function which reports whether the asset was successfully materialized.

Parameters
keyThe key to access the asset after loading
fontThe font for atlas generation
callbackAn optional callback for asynchronous loading
std::shared_ptr<Font> cugl::FontLoader::preload ( const std::string &  source,
const std::string &  charset,
int  size 
)
protected

Loads the portion of this asset that is safe to load outside the main thread.

It is not safe to create an font atlas (which requires OpenGL) in a separate thread. However, it is safe to load the TTF data from the file. Hence this method does the maximum amount of work that can be done in asynchronous font loading.

Parameters
sourceThe pathname to the asset
charsetThe atlas character set
sizeThe font size
Returns
the font asset with no generated atlas
bool cugl::FontLoader::read ( const std::string &  key,
const std::string &  source,
LoaderCallback  callback,
bool  async 
)
inlineoverrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

bool cugl::FontLoader::read ( const std::string &  key,
const std::string &  source,
int  size,
LoaderCallback  callback,
bool  async 
)
protected

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
sizeThe font size (overriding the default)
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded
bool cugl::FontLoader::read ( const std::shared_ptr< JsonValue > &  json,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the preload and materialize methods. This ensures that asynchronous loading is safe.

This version of read provides support for JSON directories. A font directory entry has the following values

 "file":         The path to the asset
 "size":         This font size (int)
 "charset":      The set of characters for the font atlas (string)
Parameters
jsonThe directory entry for the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

void cugl::FontLoader::setCharacterSet ( const std::string &  charset)
inline

Sets the default atlas character set

The character set determines exactly those characters in the font atlas. You should keep this value at a minimum, as it reduces the size of the atlas texture. Once set, any font processed by this loader will use this character set for its atlas.

If character set is the empty string, the atlas will contain all of the ASCII characters. This is the default value.

Parameters
charsetThe default atlas character set
void cugl::FontLoader::setDefaultSize ( float  size)
inline

Sets the default font size

Once set, any future font processed by this loader will have this size unless otherwise specified. The default is 12 point.

Parameters
sizeThe default font size

Member Data Documentation

std::string cugl::FontLoader::_charset
protected

The default atlas character set ("" for ASCII)

int cugl::FontLoader::_fontsize
protected

The default font size


The documentation for this class was generated from the following file: