434 lines
17 KiB
C++
434 lines
17 KiB
C++
////////////////////////////////////////////////////////////
|
|
//
|
|
// SFML - Simple and Fast Multimedia Library
|
|
// Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
|
|
//
|
|
// This software is provided 'as-is', without any express or implied warranty.
|
|
// In no event will the authors be held liable for any damages arising from the use of this software.
|
|
//
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
// including commercial applications, and to alter it and redistribute it freely,
|
|
// subject to the following restrictions:
|
|
//
|
|
// 1. The origin of this software must not be misrepresented;
|
|
// you must not claim that you wrote the original software.
|
|
// If you use this software in a product, an acknowledgment
|
|
// in the product documentation would be appreciated but is not required.
|
|
//
|
|
// 2. Altered source versions must be plainly marked as such,
|
|
// and must not be misrepresented as being the original software.
|
|
//
|
|
// 3. This notice may not be removed or altered from any source distribution.
|
|
//
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#ifndef SFML_FONT_HPP
|
|
#define SFML_FONT_HPP
|
|
|
|
////////////////////////////////////////////////////////////
|
|
// Headers
|
|
////////////////////////////////////////////////////////////
|
|
#include <SFML/Graphics/Export.hpp>
|
|
#include <SFML/Graphics/Glyph.hpp>
|
|
#include <SFML/Graphics/Texture.hpp>
|
|
#include <SFML/Graphics/Rect.hpp>
|
|
#include <SFML/System/Vector2.hpp>
|
|
#include <SFML/System/String.hpp>
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
|
|
namespace sf
|
|
{
|
|
class InputStream;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Class for loading and manipulating character fonts
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
class SFML_GRAPHICS_API Font
|
|
{
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Holds various information about a font
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
struct Info
|
|
{
|
|
std::string family; ///< The font family
|
|
};
|
|
|
|
public:
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Default constructor
|
|
///
|
|
/// This constructor defines an empty font
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
Font();
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Copy constructor
|
|
///
|
|
/// \param copy Instance to copy
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
Font(const Font& copy);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Destructor
|
|
///
|
|
/// Cleans up all the internal resources used by the font
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
~Font();
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Load the font from a file
|
|
///
|
|
/// 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.
|
|
///
|
|
/// \warning SFML cannot preload all the font data in this
|
|
/// function, so the file has to remain accessible until
|
|
/// the sf::Font object loads a new font or is destroyed.
|
|
///
|
|
/// \param filename Path of the font file to load
|
|
///
|
|
/// \return True if loading succeeded, false if it failed
|
|
///
|
|
/// \see loadFromMemory, loadFromStream
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
bool loadFromFile(const std::string& filename);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Load the font from a file in memory
|
|
///
|
|
/// The supported font formats are: TrueType, Type 1, CFF,
|
|
/// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
|
|
///
|
|
/// \warning SFML cannot preload all the font data in this
|
|
/// function, so the buffer pointed by \a data has to remain
|
|
/// valid until the sf::Font object loads a new font or
|
|
/// is destroyed.
|
|
///
|
|
/// \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, loadFromStream
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
bool loadFromMemory(const void* data, std::size_t sizeInBytes);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Load the font from a custom stream
|
|
///
|
|
/// The supported font formats are: TrueType, Type 1, CFF,
|
|
/// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
|
|
/// Warning: SFML cannot preload all the font data in this
|
|
/// function, so the contents of \a stream have to remain
|
|
/// valid as long as the font is used.
|
|
///
|
|
/// \warning SFML cannot preload all the font data in this
|
|
/// function, so the stream has to remain accessible until
|
|
/// the sf::Font object loads a new font or is destroyed.
|
|
///
|
|
/// \param stream Source stream to read from
|
|
///
|
|
/// \return True if loading succeeded, false if it failed
|
|
///
|
|
/// \see loadFromFile, loadFromMemory
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
bool loadFromStream(InputStream& stream);
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Get the font information
|
|
///
|
|
/// \return A structure that holds the font information
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
const Info& getInfo() const;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Retrieve a glyph of the font
|
|
///
|
|
/// If the font is a bitmap font, not all character sizes
|
|
/// might be available. If the glyph is not available at the
|
|
/// requested size, an empty glyph is returned.
|
|
///
|
|
/// \param codePoint Unicode code point of the character to get
|
|
/// \param characterSize Reference character size
|
|
/// \param bold Retrieve the bold version or the regular one?
|
|
///
|
|
/// \return The glyph corresponding to \a codePoint and \a characterSize
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
const Glyph& getGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) const;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Get the kerning offset of two glyphs
|
|
///
|
|
/// 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
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
float 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
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
float getLineSpacing(unsigned int characterSize) const;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Get the position of the underline
|
|
///
|
|
/// Underline position is the vertical offset to apply between the
|
|
/// baseline and the underline.
|
|
///
|
|
/// \param characterSize Reference character size
|
|
///
|
|
/// \return Underline position, in pixels
|
|
///
|
|
/// \see getUnderlineThickness
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
float getUnderlinePosition(unsigned int characterSize) const;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Get the thickness of the underline
|
|
///
|
|
/// Underline thickness is the vertical size of the underline.
|
|
///
|
|
/// \param characterSize Reference character size
|
|
///
|
|
/// \return Underline thickness, in pixels
|
|
///
|
|
/// \see getUnderlinePosition
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
float getUnderlineThickness(unsigned int characterSize) const;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Retrieve the texture containing the loaded glyphs of a certain size
|
|
///
|
|
/// The contents of the returned texture 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 Texture containing the glyphs of the requested size
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
const Texture& getTexture(unsigned int characterSize) const;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Overload of assignment operator
|
|
///
|
|
/// \param right Instance to assign
|
|
///
|
|
/// \return Reference to self
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
Font& operator =(const Font& right);
|
|
|
|
private:
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Structure defining a row of glyphs
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
struct Row
|
|
{
|
|
Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
|
|
|
|
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, Glyph> GlyphTable; ///< Table mapping a codepoint to its glyph
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \brief Structure defining a page of glyphs
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
struct Page
|
|
{
|
|
Page();
|
|
|
|
GlyphTable glyphs; ///< Table mapping code points to their corresponding glyph
|
|
Texture texture; ///< Texture containing the pixels of the glyphs
|
|
unsigned int nextRow; ///< Y position of the next new row in the texture
|
|
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
|
|
/// \param bold Retrieve the bold version or the regular one?
|
|
///
|
|
/// \return The glyph corresponding to \a codePoint and \a characterSize
|
|
///
|
|
////////////////////////////////////////////////////////////
|
|
Glyph loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) 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 (texture)
|
|
|
|
////////////////////////////////////////////////////////////
|
|
// Member data
|
|
////////////////////////////////////////////////////////////
|
|
void* m_library; ///< Pointer to the internal library interface (it is typeless to avoid exposing implementation details)
|
|
void* m_face; ///< Pointer to the internal font face (it is typeless to avoid exposing implementation details)
|
|
void* m_streamRec; ///< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details)
|
|
int* m_refCount; ///< Reference counter used by implicit sharing
|
|
Info m_info; ///< Information about the font
|
|
mutable PageTable m_pages; ///< Table containing the glyphs pages by character size
|
|
mutable std::vector<Uint8> m_pixelBuffer; ///< Pixel buffer holding a glyph's pixels before being written to the texture
|
|
#ifdef SFML_SYSTEM_ANDROID
|
|
void* m_stream; ///< Asset file streamer (if loaded from file)
|
|
#endif
|
|
};
|
|
|
|
} // namespace sf
|
|
|
|
|
|
#endif // SFML_FONT_HPP
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
/// \class sf::Font
|
|
/// \ingroup graphics
|
|
///
|
|
/// Fonts can be loaded from a file, from memory or from a custom
|
|
/// stream, and supports 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 information 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);
|
|
/// text2.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.
|
|
///
|
|
/// Note that if the font is a bitmap font, it is not scalable,
|
|
/// thus not all requested sizes will be available to use. This
|
|
/// needs to be taken into consideration when using sf::Text.
|
|
/// If you need to display text of a certain size, make sure the
|
|
/// corresponding bitmap font that supports that size is used.
|
|
///
|
|
/// \see sf::Text
|
|
///
|
|
////////////////////////////////////////////////////////////
|