FifeGUI 0.3.0
A C++ GUI library designed for games.
fcn::ImageFont Class Reference

#include <imagefont.hpp>

Inheritance diagram for fcn::ImageFont:
fcn::Font

Public Member Functions

virtual int drawGlyph (Graphics *graphics, unsigned char glyph, int x, int y)
void drawString (Graphics *graphics, std::string const &text, int x, int y) override
virtual int getGlyphSpacing ()
int getHeight () const override
virtual int getRowSpacing ()
int getStringIndexAt (std::string const &text, int x) const override
int getWidth (std::string const &text) const override
virtual int getWidth (unsigned char glyph) const
 ImageFont (Image *image, std::string const &glyphs)
 ImageFont (Image *image, std::string const &glyphs, ImageFontConfig const &config)
 ImageFont (ImageFont &&)=delete
 ImageFont (ImageFont const &)=delete
 ImageFont (std::string const &filename, std::string const &glyphs)
 ImageFont (std::string const &filename, std::string const &glyphs, ImageFontConfig const &config)
 ImageFont (std::string const &filename, unsigned char glyphsFrom, unsigned char glyphsTo, ImageFontConfig const &config)
 ImageFont (std::string const &filename, unsigned char glyphsFrom=32, unsigned char glyphsTo=126)
ImageFontoperator= (ImageFont &&)=delete
ImageFontoperator= (ImageFont const &)=delete
virtual void setGlyphSpacing (int spacing)
virtual void setRowSpacing (int spacing)
Public Member Functions inherited from fcn::Font
 Font (Font &&)=default
 Font (Font const &)=default
Fontoperator= (Font &&)=default
Fontoperator= (Font const &)=default
virtual ~Font ()=default

Protected Member Functions

Rectangle scanForGlyph (unsigned char glyph, int x, int y, Color const &separator)

Protected Attributes

std::string mFilename
std::array< Rectangle, 256 > mGlyph
int mGlyphSpacing {0}
int mHeight {0}
ImagemImage {nullptr}
int mRowSpacing {0}

Detailed Description

A font implementation using an image atlas containing glyph data.

ImageFont can be used with any image supported by the currently used ImageLoader.

These are two examples of an image containing a font.

The first pixel at coordinate (0,0) tells which color the image font looks for when separating glyphs. The glyphs in the image is provided to the image font's constructor in the order they appear in the image.

To create an ImageFont from the first image example above the following constructor call should be made:

fcn::ImageFont imageFont("fixedfont_big.bmp",
" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
A font implementation using an image atlas containing glyph data.

The first glyph defines the width of a space.

Glyphs can span multiple lines using horizontal separators, but these must be exactly 1 pixel high.

Space glyph: If included (e.g. as the first glyph), its width defines the logical space width. Otherwise, ImageFont uses a default.

Vertical separators: Glyphs are divided by columns of a single color. These may span multiple adjacent columns; any column where all pixels match the separator color is treated as part of a separator. This allows flexible spacing (e.g. wider gaps before punctuation).

Horizontal separators: Used to split glyph rows and must be exactly 1 pixel high. Multi-pixel rows are not recognized and may break row detection. If your atlas uses thicker dividers, convert them to 1-pixel rows or use an explicit separator strategy. Support for multi-pixel separators can be added if needed.

Definition at line 126 of file imagefont.hpp.

Constructor & Destructor Documentation

◆ ImageFont() [1/6]

fcn::ImageFont::ImageFont ( std::string const & filename,
std::string const & glyphs )

Constructor.

Takes an image file containing the font and a string containing the glyphs. The glyphs in the string should be in the same order as they appear in the font image.

Parameters
filenameThe filename of the image.
glyphsThe glyphs found in the image.
Exceptions
Exceptionwhen glyph list is incorrect or the font file is corrupt or if no ImageLoader exists.

Definition at line 232 of file imagefont.cpp.

References mFilename, mGlyph, mGlyphSpacing, mHeight, mImage, mRowSpacing, and scanForGlyph().

◆ ImageFont() [2/6]

fcn::ImageFont::ImageFont ( std::string const & filename,
std::string const & glyphs,
ImageFontConfig const & config )

Constructor with configuration.

Parameters
filenameThe filename of the image.
glyphsThe glyphs found in the image.
configConfiguration for parsing the image font.

Definition at line 180 of file imagefont.cpp.

References fcn::Color::b, fcn::Color::g, fcn::ImageFontConfig::glyphPadding, fcn::Rectangle::height, mFilename, mGlyph, mGlyphSpacing, mHeight, mImage, mRowSpacing, fcn::Color::r, fcn::ImageFontConfig::strategy, fcn::throwException(), fcn::ImageFontConfig::verbose, fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

◆ ImageFont() [3/6]

fcn::ImageFont::ImageFont ( Image * image,
std::string const & glyphs )

Constructor.

Takes an image containing the font and a string containing the glyphs. The glyphs in the string should be in the same order as they appear in the font image. The image will be deleted in the destructor.

Parameters
imageThe image with font glyphs.
glyphsThe glyphs found in the image.
Exceptions
Exceptionwhen glyph list is incorrect or the font image is is missing.

Definition at line 280 of file imagefont.cpp.

References mFilename, mGlyph, mGlyphSpacing, mHeight, mImage, mRowSpacing, scanForGlyph(), and fcn::throwException().

◆ ImageFont() [4/6]

fcn::ImageFont::ImageFont ( Image * image,
std::string const & glyphs,
ImageFontConfig const & config )

Constructor from an image with configuration.

The image will be owned by the ImageFont instance.

Parameters
imageThe image with font glyphs.
glyphsThe glyphs found in the image.
configConfiguration for parsing the image font.

Definition at line 324 of file imagefont.cpp.

References fcn::Color::b, fcn::Color::g, fcn::ImageFontConfig::glyphPadding, mFilename, mGlyph, mGlyphSpacing, mHeight, mImage, mRowSpacing, fcn::Color::r, fcn::ImageFontConfig::strategy, fcn::throwException(), and fcn::ImageFontConfig::verbose.

◆ ImageFont() [5/6]

fcn::ImageFont::ImageFont ( std::string const & filename,
unsigned char glyphsFrom = 32,
unsigned char glyphsTo = 126 )
explicit

Constructor.

Takes an image file containing the font and two boundaries of ASCII values. The font image should include all glyphs specified with the boundaries in increasing ASCII order. The boundaries are inclusive.

Parameters
filenameThe filename of the image.
glyphsFromThe ASCII value of the first glyph found in the image.
glyphsToThe ASCII value of the last glyph found in the image.
Exceptions
Exceptionwhen glyph bondaries are incorrect or the font file is corrupt or if no ImageLoader exists.

Definition at line 364 of file imagefont.cpp.

References getWidth(), mFilename, mGlyph, mGlyphSpacing, mHeight, mImage, mRowSpacing, and scanForGlyph().

◆ ImageFont() [6/6]

fcn::ImageFont::ImageFont ( std::string const & filename,
unsigned char glyphsFrom,
unsigned char glyphsTo,
ImageFontConfig const & config )

Constructor defining glyph range with configuration.

Parameters
filenameThe filename of the image.
glyphsFromASCII of the first glyph (inclusive).
glyphsToASCII of the last glyph (inclusive).
configConfiguration for parsing the image font.

Definition at line 402 of file imagefont.cpp.

References fcn::Color::b, fcn::Color::g, fcn::ImageFontConfig::glyphPadding, mFilename, mGlyph, mGlyphSpacing, mHeight, mImage, mRowSpacing, fcn::Color::r, fcn::ImageFontConfig::strategy, fcn::throwException(), and fcn::ImageFontConfig::verbose.

◆ ~ImageFont()

fcn::ImageFont::~ImageFont ( )
override

Definition at line 447 of file imagefont.cpp.

Member Function Documentation

◆ drawGlyph()

int fcn::ImageFont::drawGlyph ( Graphics * graphics,
unsigned char glyph,
int x,
int y )
virtual

Draws a glyph.

Note
You normally won't use this function to draw text since the Graphics class contains better functions for drawing text.
Parameters
graphicsA graphics object used for drawing.
glyphA glyph to draw.
xThe x coordinate where to draw the glyph.
yThe y coordinate where to draw the glyph.
Returns
The width of the glyph in pixels.

Definition at line 466 of file imagefont.cpp.

References fcn::Graphics::drawImage(), fcn::Graphics::drawRectangle(), getRowSpacing(), mGlyph, mGlyphSpacing, and mImage.

Referenced by drawString().

◆ drawString()

void fcn::ImageFont::drawString ( Graphics * graphics,
std::string const & text,
int x,
int y )
overridevirtual

Draws a string.

Note
You normally won't use this function to draw text since Graphics contains better functions for drawing text.
Parameters
graphicsA Graphics object to use for drawing.
textThe string to draw.
xThe x coordinate where to draw the string.
yThe y coordinate where to draw the string.

Implements fcn::Font.

Definition at line 494 of file imagefont.cpp.

References drawGlyph(), and getWidth().

◆ getGlyphSpacing()

int fcn::ImageFont::getGlyphSpacing ( )
virtual

Gets the spacing between letters in pixels.

Returns
the spacing.
See also
setGlyphSpacing

Definition at line 517 of file imagefont.cpp.

References mGlyphSpacing.

◆ getHeight()

int fcn::ImageFont::getHeight ( ) const
overridevirtual

Gets the height of the glyphs in the font.

Returns
The height of the glyphs int the font.

Implements fcn::Font.

Definition at line 461 of file imagefont.cpp.

References mHeight, and mRowSpacing.

◆ getRowSpacing()

int fcn::ImageFont::getRowSpacing ( )
virtual

Gets the space between rows in pixels.

Returns
The space between rows in pixels.
See also
setRowSpacing

Definition at line 507 of file imagefont.cpp.

References mRowSpacing.

Referenced by drawGlyph().

◆ getStringIndexAt()

int fcn::ImageFont::getStringIndexAt ( std::string const & text,
int x ) const
overridevirtual

Gets a string index in a string providing an x coordinate.

Used to retrieve a string index (for a character in a string) at a certain x position. It is especially useful when a mouse clicks in a TextField and you want to know which character was clicked.

Returns
A string index in a string providing an x coordinate.

Reimplemented from fcn::Font.

Definition at line 586 of file imagefont.cpp.

References getWidth().

◆ getWidth() [1/2]

int fcn::ImageFont::getWidth ( std::string const & text) const
overridevirtual

Gets the width of a string.

The width of a string is not necessarily the sum of all the widths of its glyphs.

Parameters
textThe string to return the width of.
Returns
The width of a string.

Implements fcn::Font.

Definition at line 574 of file imagefont.cpp.

References getWidth(), and mGlyphSpacing.

◆ getWidth() [2/2]

int fcn::ImageFont::getWidth ( unsigned char glyph) const
virtual

Gets a width of a glyph in pixels.

Parameters
glyphThe glyph which width will be returned.
Returns
The width of a glyph in pixels.

Definition at line 452 of file imagefont.cpp.

References mGlyph, and mGlyphSpacing.

Referenced by drawString(), getStringIndexAt(), getWidth(), and ImageFont().

◆ scanForGlyph()

Rectangle fcn::ImageFont::scanForGlyph ( unsigned char glyph,
int x,
int y,
Color const & separator )
protected

Scans for a certain glyph.

Parameters
glyphThe glyph to scan for. Used for exception messages.
xThe x coordinate where to begin the scan. The coordinate will be updated with the end x coordinate of the glyph when the scan is complete.
yThe y coordinate where to begin the scan. The coordinate will be updated with the end y coordinate of the glyph when the scan is complete.
separatorThe color separator to look for where the glyph ends.
Returns
A rectangle with the found glyph dimension in the image with the font.
Exceptions
Exceptionwhen no glyph is found.

Definition at line 522 of file imagefont.cpp.

References mFilename, mHeight, mImage, and fcn::throwException().

Referenced by ImageFont(), ImageFont(), and ImageFont().

◆ setGlyphSpacing()

void fcn::ImageFont::setGlyphSpacing ( int spacing)
virtual

Sets the spacing between glyphs in pixels.

Default is 0 pixels. The space can be negative.

Parameters
spacingThe glyph space in pixels.
See also
getGlyphSpacing

Definition at line 512 of file imagefont.cpp.

References mGlyphSpacing.

◆ setRowSpacing()

void fcn::ImageFont::setRowSpacing ( int spacing)
virtual

Sets the space between rows in pixels.

Default is 0 pixels. The space can be negative.

Parameters
spacingThe space between rows in pixels.
See also
getRowSpacing

Definition at line 502 of file imagefont.cpp.

References mRowSpacing.

Member Data Documentation

◆ mFilename

std::string fcn::ImageFont::mFilename
protected

Holds the filename of the image with the font data.

Definition at line 324 of file imagefont.hpp.

Referenced by ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), and scanForGlyph().

◆ mGlyph

std::array<Rectangle, 256> fcn::ImageFont::mGlyph
protected

Holds the glyphs areas in the image.

Definition at line 299 of file imagefont.hpp.

Referenced by drawGlyph(), getWidth(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), and ImageFont().

◆ mGlyphSpacing

int fcn::ImageFont::mGlyphSpacing {0}
protected

Holds the glyph spacing of the image font.

Definition at line 309 of file imagefont.hpp.

Referenced by drawGlyph(), getGlyphSpacing(), getWidth(), getWidth(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), and setGlyphSpacing().

◆ mHeight

int fcn::ImageFont::mHeight {0}
protected

Holds the height of the image font.

Definition at line 304 of file imagefont.hpp.

Referenced by getHeight(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), and scanForGlyph().

◆ mImage

Image* fcn::ImageFont::mImage {nullptr}
protected

Holds the image with the font data.

Definition at line 319 of file imagefont.hpp.

Referenced by drawGlyph(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), and scanForGlyph().

◆ mRowSpacing

int fcn::ImageFont::mRowSpacing {0}
protected

Holds the row spacing of the image font.

Definition at line 314 of file imagefont.hpp.

Referenced by getHeight(), getRowSpacing(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), ImageFont(), and setRowSpacing().


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