____                _       __
    / __ )____  _____   | |     / /___ ___________
   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  )
 /_____/\____/____/     |__/|__/\__,_/_/  /____/

       A futuristic real-time strategy game.
          This file is part of Bos Wars.
(C) Copyright 2001-2007 by the Bos Wars and Stratagus Project. Distributed under the "GNU General Public License"

gcn::ImageFont Class Reference

#include <imagefont.h>

Inheritance diagram for gcn::ImageFont:

gcn::Font

List of all members.

Public Member Functions

 ImageFont (const std::string &filename, const std::string &glyphs)
 ImageFont (const std::string &filename, unsigned char glyphsFrom=32, unsigned char glyphsTo=126)
virtual ~ImageFont ()
virtual int drawGlyph (Graphics *graphics, unsigned char glyph, int x, int y)
virtual void setRowSpacing (int spacing)
virtual int getRowSpacing ()
virtual void setGlyphSpacing (int spacing)
virtual int getGlyphSpacing ()
virtual int getWidth (unsigned char glyph) const
virtual int getWidth (const std::string &text) const
virtual void drawString (Graphics *graphics, const std::string &text, int x, int y)
virtual int getHeight () const
virtual int getStringIndexAt (const std::string &text, int x)

Protected Member Functions

void addGlyph (unsigned char c, int &x, int &y, const Color &separator)

Protected Attributes

Rectangle mGlyph [256]
int mHeight
int mGlyphSpacing
int mRowSpacing
ImagemImage
std::string mFilename


Detailed Description

A font using an image containing the font data. It implements the font class. You can use any filetype for the font data as long as it can be loaded with your ImageLoader.

This are two examples of an image containing a font.

imagefontexample.bmp
imagefontexample2.bmp

The Image font format works like this: The first pixel, the pixal at coordinate (0,0), tells which color to look for when seperating glyphs. You create an image with your glyphs and simple separates them with the seperation color. When you create your ImageFont you supply the constructor with the glyphs present in your image. When creating an ImageFont for the image data in the first example above, the following constructor call would be used.

 gcn::ImageFont imageFont("fixedfont_big.bmp"," abcdefghijklmno\
pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); 
Noteworthy is that the first glyph actually gives the width of space. Glyphs can, as seen in the second example above, be seperated with horizontal lines making it possible to draw glyphs on more then one line in the image. However, these vertical lines must be of one pixel size!

Definition at line 92 of file imagefont.h.


Constructor & Destructor Documentation

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

Constructor which 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:
filename the filename of the image.
glyphs the glyphs found in the image.
Exceptions:
Exception when glyph list is incorrect or the font file is corrupt or if no ImageLoader exists.

Definition at line 67 of file imagefont.cpp.

References gcn::Image::_getImageLoader(), addGlyph(), data, gcn::ImageLoader::finalize(), GCN_EXCEPTION, gcn::ImageLoader::getHeight(), gcn::ImageLoader::getPixel(), gcn::ImageLoader::getWidth(), int(), mFilename, mGlyphSpacing, mHeight, mImage, mRowSpacing, gcn::ImageLoader::prepare(), and y.

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

Constructor which 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:
filename the filename of the image.
glyphsFrom the ASCII value of the first glyph found in the image.
glyphsTo the ASCII value of the last glyph found in the image.
Exceptions:
Exception when glyph bondaries are incorrect or the font file is corrupt or if no ImageLoader exists.

Definition at line 118 of file imagefont.cpp.

References gcn::Image::_getImageLoader(), addGlyph(), data, gcn::ImageLoader::finalize(), GCN_EXCEPTION, gcn::ImageLoader::getHeight(), gcn::ImageLoader::getPixel(), gcn::ImageLoader::getWidth(), mFilename, mGlyphSpacing, mHeight, mImage, mRowSpacing, gcn::ImageLoader::prepare(), and y.

gcn::ImageFont::~ImageFont (  )  [virtual]

Destructor.

Definition at line 167 of file imagefont.cpp.

References gcn::Image::_getImageLoader(), gcn::ImageLoader::free(), and mImage.


Member Function Documentation

int gcn::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 a graphics object to be used for drawing.
glyph a glyph to draw.
x the x coordinate where to draw the glyph.
y the y coordinate where to draw the glyph.
Returns:
the width of the glyph in pixels.
See also:
Graphics

Definition at line 188 of file imagefont.cpp.

References gcn::Graphics::drawImage(), gcn::Graphics::drawRectangle(), getRowSpacing(), int(), mGlyph, mGlyphSpacing, mImage, and gcn::Rectangle::width.

Referenced by drawString().

void gcn::ImageFont::setRowSpacing ( int  spacing  )  [virtual]

Sets the spacing between rows in pixels. Default is 0 pixels. The spacing can be negative.

Parameters:
spacing the spacing in pixels.

Definition at line 218 of file imagefont.cpp.

References mRowSpacing.

int gcn::ImageFont::getRowSpacing (  )  [virtual]

Gets the spacing between rows in pixels.

Returns:
the spacing.

Definition at line 223 of file imagefont.cpp.

References mRowSpacing.

Referenced by drawGlyph().

void gcn::ImageFont::setGlyphSpacing ( int  spacing  )  [virtual]

Sets the spacing between letters in pixels. Default is 0 pixels. The spacing can be negative.

Parameters:
spacing the spacing in pixels

Definition at line 228 of file imagefont.cpp.

References mGlyphSpacing.

int gcn::ImageFont::getGlyphSpacing (  )  [virtual]

Gets the spacing between letters in pixels.

Returns:
the spacing.

Definition at line 233 of file imagefont.cpp.

References mGlyphSpacing.

int gcn::ImageFont::getWidth ( unsigned char  glyph  )  const [virtual]

Gets a width of a glyph.

Parameters:
glyph the glyph which width will be returned
Returns:
the width of a glyph

Definition at line 173 of file imagefont.cpp.

References int(), mGlyph, mGlyphSpacing, and gcn::Rectangle::width.

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

int gcn::ImageFont::getWidth ( const std::string &  text  )  const [virtual]

Gets the width of a string. The width of a string is not necesserily the sum of all the widths of it's glyphs.

Parameters:
text the string to return the width of.
Returns:
the width of a string.

Implements gcn::Font.

Definition at line 297 of file imagefont.cpp.

References getWidth().

void gcn::ImageFont::drawString ( Graphics graphics,
const std::string &  text,
int  x,
int  y 
) [virtual]

Draws a string.

NOTE: You normally won't use this function to draw text since Graphics contains better functions for drawing text.

Parameters:
graphics a Graphics object to use for drawing.
text the string to draw.
x the x coordinate where to draw the string.
y the y coordinate where to draw the string.

Implements gcn::Font.

Definition at line 207 of file imagefont.cpp.

References drawGlyph(), and getWidth().

int gcn::ImageFont::getHeight (  )  const [virtual]

Gets the height of the glyphs in the font.

Returns:
the height of the glyphs int the font.

Implements gcn::Font.

Definition at line 183 of file imagefont.cpp.

References mHeight, and mRowSpacing.

int gcn::ImageFont::getStringIndexAt ( const std::string &  text,
int  x 
) [virtual]

Gets a string index in a string providing an x coordinate. Used to retrive 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 gcn::Font.

Definition at line 310 of file imagefont.cpp.

References getWidth().

void gcn::ImageFont::addGlyph ( unsigned char  c,
int &  x,
int &  y,
const Color separator 
) [protected]


Member Data Documentation

Definition at line 197 of file imagefont.h.

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

int gcn::ImageFont::mHeight [protected]

Definition at line 198 of file imagefont.h.

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

Definition at line 199 of file imagefont.h.

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

int gcn::ImageFont::mRowSpacing [protected]

Definition at line 200 of file imagefont.h.

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

Definition at line 201 of file imagefont.h.

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

std::string gcn::ImageFont::mFilename [protected]

Definition at line 202 of file imagefont.h.

Referenced by addGlyph(), and ImageFont().


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

Generated on Sat Feb 21 00:28:26 2009 for Bos Wars by  doxygen 1.5.6