CUGL 2.3
Cornell University Game Library
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
cugl::Font::Atlas Class Reference

#include <CUFont.h>

Public Member Functions

 Atlas ()
 
 ~Atlas ()
 
void dispose ()
 
bool init (Font *parent, std::deque< Uint32 > &glyphset)
 
bool hasGlyph (Uint32 a) const
 
bool hasGlyphs (const std::vector< Uint32 > &glyphs) const
 
bool hasGlyphs (const std::string glyphs) const
 
bool getQuad (Uint32 thechar, Vec2 &offset, Mesh< SpriteVertex2 > &mesh, const Rect rect) const
 
void getQuad (Uint32 thechar, Vec2 &offset, Mesh< SpriteVertex2 > &mesh) const
 
bool build ()
 
bool materialize ()
 

Static Public Member Functions

static std::shared_ptr< Atlasalloc (Font *parent, std::deque< Uint32 > &glyphset)
 

Public Attributes

std::shared_ptr< Texturetexture
 
std::unordered_map< Uint32, Rectglyphmap
 

Detailed Description

This class represents a single font atlas.

A font atlas is a collection of pre-rendered glyphs, together with a directory of the bounds for each glyph. This directory information makes it very easy to quickly construct a textured quad mesh for a series of glyphys.

An font may have more than one atlas, particulary if the font size is large and there are a large number of supported glyphs. In that case, the atlases typically support a disjoint set of glyphs. However, we do not enforce this.

Constructor & Destructor Documentation

◆ Atlas()

cugl::Font::Atlas::Atlas ( )

Creates an uninitialized atlas with no parent font.

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

◆ ~Atlas()

cugl::Font::Atlas::~Atlas ( )
inline

Deletes this atlas, diposing of all its resources.

Member Function Documentation

◆ alloc()

static std::shared_ptr< Atlas > cugl::Font::Atlas::alloc ( Font parent,
std::deque< Uint32 > &  glyphset 
)
inlinestatic

Returns a newly allocated atlas for the given font and glyphset

This allocator will perform the layout computation, but it will not create any textures or SDL surfaces. It will consume glyphs from the provided glyphset as it adds them to the atlas. So if it successfully adds all glyphs, the value glyphset will be emptied.

It is possible for the atlas to reject some glyphs. This is typically because the resulting texture size would exceed the maximum allowable texture size. In that case, the remaining elements in glyphset are glyphs that must be processed by another atlas.

If this atlas cannot process any of the elements in glyphset (because they are unsupported), then this method returns nullptr.

Parameters
parentThe parent font of this atlas
glyphsetThe glyphs to add to this atlas
Returns
a newly allocated atlas for the given font and glyphset

◆ build()

bool cugl::Font::Atlas::build ( )

Builds the texture data for this given atlas.

This method does not generate the OpenGL texture, but does all other work in creates the atlas. In particular it creates the image buffer so that texture creation is just one OpenGL call. This creation will happen once materialize() is called. As a result, it is safe to call this method outside of the main thread.

Returns
true if atlas creation was successful

◆ dispose()

void cugl::Font::Atlas::dispose ( )

Deletes the atlas resources and resets all attributes.

This will delete the parent font as well. You must reinitialize the atlas to use it.

◆ getQuad() [1/2]

void cugl::Font::Atlas::getQuad ( Uint32  thechar,
Vec2 offset,
Mesh< SpriteVertex2 > &  mesh 
) const

Creates a single quad to render this character and stores it in mesh

This method will append the vertices to the provided mesh and update the indices to include these new vertices. Once the quad is generated, the offset will be adjusted to contain the next place to render a character. This method will not generate anything if the character is not supported by this atlas.

Parameters
thecharThe character to convert to render data
offsetThe (unkerned) starting position of the quad
meshThe mesh to store the vertices

◆ getQuad() [2/2]

bool cugl::Font::Atlas::getQuad ( Uint32  thechar,
Vec2 offset,
Mesh< SpriteVertex2 > &  mesh,
const Rect  rect 
) const

Creates a single quad to render this character and stores it in mesh

This method will append the vertices to the provided mesh and update the indices to include these new vertices. Once the quad is generated, the offset will be adjusted to contain the next place to render a character. This method will not generate anything if the character is not supported by this atlas.

The quad is adjusted so that all of the vertices fit in the provided rectangle. This may mean that no quad is generated at all.

Parameters
thecharThe character to convert to render data
offsetThe (unkerned) starting position of the quad
rectThe bounding box for the quad
meshThe mesh to store the vertices

◆ hasGlyph()

bool cugl::Font::Atlas::hasGlyph ( Uint32  a) const

Returns true if this font has a glyph for the given (UNICODE) character.

The Unicode representation uses the endianness native to the platform. Therefore, this value should not be serialized. Use UTF8 to represent unicode in a platform-independent manner.

Note that control characters (like newline) never have glyphs. However, spaces do.

Parameters
aThe Unicode character to check.
Returns
true if this font has a glyph for the given (UNICODE) character.

◆ hasGlyphs() [1/2]

bool cugl::Font::Atlas::hasGlyphs ( const std::string  glyphs) const

Returns true if this atlas has all of the given glyphs

We assume that the string represents the glyphs in a UTF8 encoding.

Note that control characters (like newline) never have glyphs. However, spaces do.

Parameters
glyphsThe UTF8 glyphs to check.
Returns
true if this atlas has all of the given glyphs

◆ hasGlyphs() [2/2]

bool cugl::Font::Atlas::hasGlyphs ( const std::vector< Uint32 > &  glyphs) const

Returns true if this atlas has all of the given unicode glyphs

The Unicode representation uses the endianness native to the platform. Therefore, this value should not be serialized. Use UTF8 to represent unicode in a platform-independent manner.

Note that control characters (like newline) never have glyphs. However, spaces do.

Parameters
glyphsThe Unicode characters to check.
Returns
true if this atlas has all of the given unicode glyphs

◆ init()

bool cugl::Font::Atlas::init ( Font parent,
std::deque< Uint32 > &  glyphset 
)

Initializes an atlas for the given font and glyphset

This initializer will perform the layout computation, but it will not create any textures or SDL surfaces. It will consume glyphs from the provided glyphset as it adds them to the atlas. So if it successfully adds all glyphs, the value glyphset will be emptied.

It is possible for the atlas to reject some glyphs. This is typically because the resulting texture size would exceed the maximum allowable texture size. In that case, the remaining elements in glyphset are glyphs that must be processed by another atlas.

If this atlas cannot process any of the elements in glyphset (because they are unsupported), then this method returns false.

Parameters
parentThe parent font of this atlas
glyphsetThe glyphs to add to this atlas
Returns
true if the atlas was successfully initialized

◆ materialize()

bool cugl::Font::Atlas::materialize ( )

Creates the OpenGL texture for this atlas.

This method must be called on the main thread. It is only safe to call this method after a succesful call to build().

Returns
true if texture creation was successful.

Member Data Documentation

◆ glyphmap

std::unordered_map<Uint32,Rect> cugl::Font::Atlas::glyphmap

The location of each glyph in the atlas texture. This includes padding.

◆ texture

std::shared_ptr<Texture> cugl::Font::Atlas::texture

The texture (may be null if not materialized)


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