FifeGUI 0.2.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 (ImageFont &&)=delete
 ImageFont (ImageFont const &)=delete
 ImageFont (std::string const &filename, std::string const &glyphs)
 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
Font & operator= (Font &&)=default
Font & operator= (Font const &)=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.
Definition imagefont.hpp:51

Noteworthy is that the first glyph actually gives the width of space. Glyphs can, as seen in the second image example above, be separated with horizontal lines making it possible to draw glyphs on more then one line in the image. However, these horizontal lines must have a height of one pixel!

Definition at line 50 of file imagefont.hpp.

Constructor & Destructor Documentation

◆ ImageFont() [1/3]

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 19 of file imagefont.cpp.

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

◆ ImageFont() [2/3]

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 67 of file imagefont.cpp.

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

◆ ImageFont() [3/3]

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 111 of file imagefont.cpp.

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

◆ ~ImageFont()

fcn::ImageFont::~ImageFont ( )
override

Definition at line 148 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
Graphics & RenderingA 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 167 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
Graphics & RenderingA 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 189 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 212 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 162 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 202 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 272 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 it's glyphs.

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

Implements fcn::Font.

Definition at line 260 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 153 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 217 of file imagefont.cpp.

References mFilename, mHeight, and mImage.

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 207 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 197 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 214 of file imagefont.hpp.

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

◆ mGlyph

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

Holds the glyphs areas in the image.

Definition at line 189 of file imagefont.hpp.

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

◆ mGlyphSpacing

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

Holds the glyph spacing of the image font.

Definition at line 199 of file imagefont.hpp.

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

◆ mHeight

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

Holds the height of the image font.

Definition at line 194 of file imagefont.hpp.

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

◆ mImage

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

Holds the image with the font data.

Definition at line 209 of file imagefont.hpp.

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

◆ mRowSpacing

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

Holds the row spacing of the image font.

Definition at line 204 of file imagefont.hpp.

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


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