Complete rewrite of sf::Font to make it more flexible (no more fixed charset and size)

FS#125 - Fix tab character not working in sf::String

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1309 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-12-13 15:49:30 +00:00
parent 839c80556d
commit 3a34f81561
32 changed files with 1016 additions and 3016 deletions

View file

@ -36,109 +36,328 @@
#include <SFML/Graphics/Rect.hpp>
#include <map>
#include <string>
#include <vector>
namespace sf
{
namespace priv
{
class FontLoader;
}
////////////////////////////////////////////////////////////
/// Font is the low-level class for loading and
/// manipulating character fonts. This class is meant to
/// be used by sf::Text
/// \brief Class for loading and manipulating character fonts
///
////////////////////////////////////////////////////////////
class SFML_API Font : public Resource<Font>
{
public :
////////////////////////////////////////////////////////////
/// Default constructor
/// \brief Default constructor
///
/// This constructor defines an empty font
///
////////////////////////////////////////////////////////////
Font();
////////////////////////////////////////////////////////////
/// Load the font from a file
/// \brief Copy constructor
///
/// \param filename : Font file to load
/// \param charSize : Size of characters in bitmap - the bigger, the higher quality
/// \param charset : Characters set to generate (by default, contains the ISO-8859-1 printable characters)
///
/// \return True if loading was successful
/// \param copy Instance to copy
///
////////////////////////////////////////////////////////////
bool LoadFromFile(const std::string& filename, unsigned int charSize = 30, String charset = ourDefaultCharset);
Font(const Font& copy);
////////////////////////////////////////////////////////////
/// Load the font from a file in memory
/// \brief Destructor
///
/// \param data : Pointer to the data to load
/// \param sizeInBytes : Size of the data, in bytes
/// \param charSize : Size of characters in bitmap - the bigger, the higher quality
/// \param charset : Characters set to generate (by default, contains the ISO-8859-1 printable characters)
///
/// \return True if loading was successful
/// Cleans up all the internal resources used by the font
///
////////////////////////////////////////////////////////////
bool LoadFromMemory(const char* data, std::size_t sizeInBytes, unsigned int charSize = 30, String charset = ourDefaultCharset);
~Font();
////////////////////////////////////////////////////////////
/// Get the base size of characters in the font;
/// All glyphs dimensions are based on this value
/// \brief Load the font from a file
///
/// \return Base size of characters
/// The supported font formats are: TrueType, Type 1, CFF,
/// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
/// Note that this function know nothing about the standard
/// fonts installed on the user's system, thus you can't
/// load them directly.
///
/// \param filename Path of the font file to load
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromMemory
///
////////////////////////////////////////////////////////////
unsigned int GetCharacterSize() const;
bool LoadFromFile(const std::string& filename);
////////////////////////////////////////////////////////////
/// Get the description of a glyph (character)
/// given by its unicode value
/// \brief Load the font from a file
///
/// \param codePoint : Unicode value of the character to get
/// The supported font formats are: TrueType, Type 1, CFF,
/// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
/// Note that this function know nothing about the standard
/// fonts installed on the user's system, thus you can't
/// load them directly.
///
/// \return Glyph's visual settings, or an invalid glyph if character not found
/// \param data Pointer to the file data in memory
/// \param sizeInBytes Size of the data to load, in bytes
///
/// \return True if loading succeeded, false if it failed
///
/// \see LoadFromFile
///
////////////////////////////////////////////////////////////
const Glyph& GetGlyph(Uint32 codePoint) const;
bool LoadFromMemory(const char* data, std::size_t sizeInBytes);
////////////////////////////////////////////////////////////
/// Get the image containing the rendered characters (glyphs)
/// \brief Retrieve a glyph of the font
///
/// \return Image containing glyphs
/// \param codePoint Unicode code point of the character to get
/// \param characterSize Reference character size
///
/// \return The glyph corresponding to \a codePoint and \a characterSize
///
////////////////////////////////////////////////////////////
const Image& GetImage() const;
const Glyph& GetGlyph(Uint32 codePoint, unsigned int characterSize) const;
////////////////////////////////////////////////////////////
/// Get the SFML default built-in font (Arial)
/// \brief Get the kerning offset of two glyphs
///
/// \return Instance of the default font
/// The kerning is an extra offset (negative) to apply between two
/// glyphs when rendering them, to make the pair look more "natural".
/// For example, the pair "AV" have a special kerning to make them
/// closer than other characters. Most of the glyphs pairs have a
/// kerning offset of zero, though.
///
/// \param first Unicode code point of the first character
/// \param second Unicode code point of the second character
/// \param characterSize Reference character size
///
/// \return Kerning value for \a first and \a second, in pixels
///
////////////////////////////////////////////////////////////
int GetKerning(Uint32 first, Uint32 second, unsigned int characterSize) const;
////////////////////////////////////////////////////////////
/// \brief Get the line spacing
///
/// Line spacing is the vertical offset to apply between two
/// consecutive lines of text.
///
/// \param characterSize Reference character size
///
/// \return Line spacing, in pixels
///
////////////////////////////////////////////////////////////
int GetLineSpacing(unsigned int characterSize) const;
////////////////////////////////////////////////////////////
/// \brief Retrieve the image containing the loaded glyphs of a certain size
///
/// The contents of the returned image changes as more glyphs
/// are requested, thus it is not very relevant. It is mainly
/// used internally by sf::Text.
///
/// \param characterSize Reference character size
///
/// \return Image containing the glyphs of the requested size
///
////////////////////////////////////////////////////////////
const Image& GetImage(unsigned int characterSize) const;
////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator
///
/// \param right Instance to assign
///
/// \return Reference to self
///
////////////////////////////////////////////////////////////
Font& operator =(const Font& right);
////////////////////////////////////////////////////////////
/// \brief Return the default built-in font
///
/// This font is provided for convenience, it is used by
/// sf::Text instances by default. It is provided so that
/// users don't have to provide and load a font file in order
/// to display text on screen.
/// The font used is Arial.
///
/// \return Reference to the built-in default font
///
////////////////////////////////////////////////////////////
static const Font& GetDefaultFont();
private :
friend class priv::FontLoader;
////////////////////////////////////////////////////////////
/// \brief Structure storing a glyph together with its
/// rectangle in the texture
///
////////////////////////////////////////////////////////////
struct GlyphInfo
{
Glyph GlyphDesc;
IntRect TextureRect;
};
////////////////////////////////////////////////////////////
// Static member data
/// \brief Structure defining a row of glyphs
///
////////////////////////////////////////////////////////////
static Uint32 ourDefaultCharset[]; ///< The default charset (all printable ISO-8859-1 characters)
struct Row
{
Row(unsigned int top, unsigned int height) : Width(0), Top(top), Height(height) {}
unsigned int Width; ///< Current width of the row
unsigned int Top; ///< Y position of the row into the texture
unsigned int Height; ///< Height of the row
};
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
typedef std::map<Uint32, GlyphInfo> GlyphTable; ///< Table mapping a codepoint to its glyph
////////////////////////////////////////////////////////////
/// \brief Structure defining a page if glyphs
///
////////////////////////////////////////////////////////////
struct Page
{
Page();
GlyphTable Glyphs; ///< Table mapping code points to their corresponding glyph
Image Texture; ///< Image containing the pixels of the glyphs
unsigned int NextRow; ///< Y position of the next new row in the image
std::vector<Row> Rows; ///< List containing the position of all the existing rows
};
////////////////////////////////////////////////////////////
/// \brief Free all the internal resources
///
////////////////////////////////////////////////////////////
void Cleanup();
////////////////////////////////////////////////////////////
/// \brief Load a new glyph and store it in the cache
///
/// \param codePoint Unicode code point of the character to load
/// \param characterSize Reference character size
///
/// \return The glyph corresponding to \a codePoint and \a characterSize
///
////////////////////////////////////////////////////////////
GlyphInfo LoadGlyph(Uint32 codePoint, unsigned int characterSize) const;
////////////////////////////////////////////////////////////
/// \brief Find a suitable rectangle within the texture for a glyph
///
/// \param page Page of glyphs to search in
/// \param width Width of the rectangle
/// \param height Height of the rectangle
///
/// \return Found rectangle within the texture
///
////////////////////////////////////////////////////////////
IntRect FindGlyphRect(Page& page, unsigned int width, unsigned int height) const;
////////////////////////////////////////////////////////////
/// \brief Make sure that the given size is the current one
///
/// \param characterSize Reference character size
///
/// \return True on success, false if any error happened
///
////////////////////////////////////////////////////////////
bool SetCurrentSize(unsigned int characterSize) const;
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
typedef std::map<unsigned int, Page> PageTable; ///< Table mapping a character size to its page (image)
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Image myTexture; ///< Texture holding the bitmap font
unsigned int myCharSize; ///< Size of characters in the bitmap font
std::map<Uint32, Glyph> myGlyphs; ///< Rendering settings of each character (glyph)
void* myLibrary; ///< Pointer to the internal library interface (it is typeless to avoid exposing implementation details)
void* myFace; ///< Pointer to the internal font face (it is typeless to avoid exposing implementation details)
int* myRefCount; ///< Reference counter used by implicit sharing
mutable PageTable myPages; ///< Table containing the glyphs pages by character size
mutable std::vector<Uint8> myPixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture
mutable unsigned int myCurrentSize; ///< Current character size in use
};
} // namespace sf
#endif // SFML_FONT_HPP
////////////////////////////////////////////////////////////
/// \class sf::Font
///
/// Fonts can be loaded from a file or from memory, from
/// the most common types of fonts. See the LoadFromFile()
/// function for the complete list of supported formats.
///
/// Once it is loaded, a sf::Font instance provides three
/// types of informations about the font:
/// \li Global metrics, such as the line spacing
/// \li Per-glyph metrics, such as bounding box or kerning
/// \li Pixel representation of glyphs
///
/// Fonts alone are not very useful: they hold the font data
/// but cannot make anything useful of it. To do so you need to
/// use the sf::Text class, which is able to properly output text
/// with several options such as character size, style, color,
/// position, rotation, etc.
/// This separation allows more flexibility and better performances:
/// indeed a sf::Font is a heavy resource, and any operation on it
/// is slow (often too slow for real-time applications). On the other
/// side, a sf::Text is a lightweight object which can combine the
/// glyphs data and metrics of a sf::Font to display any text on a
/// render target.
/// Note that it is also possible to bind several sf::Text instances
/// to the same sf::Font.
///
/// It is important to note that the sf::Text instance doesn't
/// copy the font that it uses, it only keeps a reference to it.
/// Thus, a sf::Font must not be destructed while it is
/// used by a sf::Text (i.e. never write a function that
/// uses a local sf::Font instance for creating a text).
///
/// Usage example:
/// \code
/// // Declare a new font
/// sf::Font font;
///
/// // Load it from a file
/// if (!font.LoadFromFile("arial.ttf"))
/// {
/// // error...
/// }
///
/// // Create a text which uses our font
/// sf::Text text1;
/// text1.SetFont(font);
/// text1.SetCharacterSize(30);
/// text1.SetStyle(sf::Text::Regular);
///
/// // Create another text using the same font, but with different parameters
/// sf::Text text2;
/// text2.SetFont(font);
/// text2.SetCharacterSize(50);
/// text1.SetStyle(sf::Text::Italic);
/// \endcode
///
/// Apart from loading font files, and passing them to instances
/// of sf::Text, you should normally not have to deal directly
/// with this class. However, it may be useful to access the
/// font metrics or rasterized glyphs for advanced usage.
///
/// \see sf::Text
///
////////////////////////////////////////////////////////////

View file

@ -51,8 +51,8 @@ public :
// Member data
////////////////////////////////////////////////////////////
int Advance; ///< Offset to move horizontically to the next character
IntRect Rectangle; ///< Bounding rectangle of the glyph, in relative coordinates
FloatRect TexCoords; ///< Texture coordinates of the glyph inside the bitmap font
IntRect Rectangle; ///< Bounding rectangle of the glyph, in coordinates relative to the baseline
FloatRect TexCoords; ///< Texture coordinates of the glyph inside the font's image
};
} // namespace sf

View file

@ -203,7 +203,7 @@ public :
/// Warning: for performances reasons, this function doesn't
/// perform any check; thus you're responsible of ensuring that
/// \a rectangle does not exceed the image size, and that
/// \a pixels contain enough elements.
/// \a pixels contains enough elements.
///
/// \param rectangle : Sub-rectangle of the image to update
/// \param pixels : Array of pixels to write to the image

View file

@ -65,12 +65,12 @@ public :
////////////////////////////////////////////////////////////
/// Construct the string from any kind of text
///
/// \param string : Text assigned to the string
/// \param font : Font used to draw the string
/// \param size : Characters size
/// \param string : Text assigned to the string
/// \param font : Font used to draw the string
/// \param characterSize : Base size of characters, in pixels
///
////////////////////////////////////////////////////////////
explicit Text(const String& string, const Font& font = Font::GetDefaultFont(), float size = 30.f);
explicit Text(const String& string, const Font& font = Font::GetDefaultFont(), unsigned int characterSize = 30);
////////////////////////////////////////////////////////////
/// Set the text (from any kind of string)
@ -89,13 +89,13 @@ public :
void SetFont(const Font& font);
////////////////////////////////////////////////////////////
/// Set the size of the string
/// Set the base size for the characters.
/// The default size is 30
///
/// \param size : New size, in pixels
///
////////////////////////////////////////////////////////////
void SetSize(float size);
void SetCharacterSize(unsigned int size);
////////////////////////////////////////////////////////////
/// Set the style of the text
@ -123,12 +123,12 @@ public :
const Font& GetFont() const;
////////////////////////////////////////////////////////////
/// Get the size of the characters
/// Get the base size of characters
///
/// \return Size of the characters
/// \return Size of the characters, in pixels
///
////////////////////////////////////////////////////////////
float GetSize() const;
unsigned int GetCharacterSize() const;
////////////////////////////////////////////////////////////
/// Get the style of the text
@ -148,7 +148,7 @@ public :
/// \return Position of the index-th character (end of string if Index is out of range)
///
////////////////////////////////////////////////////////////
sf::Vector2f GetCharacterPos(std::size_t index) const;
Vector2f GetCharacterPos(std::size_t index) const;
////////////////////////////////////////////////////////////
/// Get the string rectangle on screen
@ -179,7 +179,7 @@ private :
////////////////////////////////////////////////////////////
String myString; ///< String to display
ResourcePtr<Font> myFont; ///< Font used to display the string
float mySize; ///< Size of the characters
unsigned int myCharacterSize; ///< Base size of characters, in pixels
unsigned long myStyle; ///< Text style (see Style enum)
FloatRect myBaseRect; ///< Bounding rectangle of the text in object coordinates
bool myNeedRectUpdate; ///< Does the bounding rect need an update ?