Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:13

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_FONT_HPP
0026 #define SFML_FONT_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Graphics/Export.hpp>
0032 #include <SFML/Graphics/Glyph.hpp>
0033 #include <SFML/Graphics/Texture.hpp>
0034 #include <SFML/Graphics/Rect.hpp>
0035 #include <map>
0036 #include <string>
0037 #include <vector>
0038 
0039 
0040 namespace sf
0041 {
0042 class InputStream;
0043 
0044 ////////////////////////////////////////////////////////////
0045 /// \brief Class for loading and manipulating character fonts
0046 ///
0047 ////////////////////////////////////////////////////////////
0048 class SFML_GRAPHICS_API Font
0049 {
0050 public:
0051 
0052     ////////////////////////////////////////////////////////////
0053     /// \brief Holds various information about a font
0054     ///
0055     ////////////////////////////////////////////////////////////
0056     struct Info
0057     {
0058         std::string family; //!< The font family
0059     };
0060 
0061 public:
0062 
0063     ////////////////////////////////////////////////////////////
0064     /// \brief Default constructor
0065     ///
0066     /// This constructor defines an empty font
0067     ///
0068     ////////////////////////////////////////////////////////////
0069     Font();
0070 
0071     ////////////////////////////////////////////////////////////
0072     /// \brief Copy constructor
0073     ///
0074     /// \param copy Instance to copy
0075     ///
0076     ////////////////////////////////////////////////////////////
0077     Font(const Font& copy);
0078 
0079     ////////////////////////////////////////////////////////////
0080     /// \brief Destructor
0081     ///
0082     /// Cleans up all the internal resources used by the font
0083     ///
0084     ////////////////////////////////////////////////////////////
0085     ~Font();
0086 
0087     ////////////////////////////////////////////////////////////
0088     /// \brief Load the font from a file
0089     ///
0090     /// The supported font formats are: TrueType, Type 1, CFF,
0091     /// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
0092     /// Note that this function knows nothing about the standard
0093     /// fonts installed on the user's system, thus you can't
0094     /// load them directly.
0095     ///
0096     /// \warning SFML cannot preload all the font data in this
0097     /// function, so the file has to remain accessible until
0098     /// the sf::Font object loads a new font or is destroyed.
0099     ///
0100     /// \param filename Path of the font file to load
0101     ///
0102     /// \return True if loading succeeded, false if it failed
0103     ///
0104     /// \see loadFromMemory, loadFromStream
0105     ///
0106     ////////////////////////////////////////////////////////////
0107     bool loadFromFile(const std::string& filename);
0108 
0109     ////////////////////////////////////////////////////////////
0110     /// \brief Load the font from a file in memory
0111     ///
0112     /// The supported font formats are: TrueType, Type 1, CFF,
0113     /// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
0114     ///
0115     /// \warning SFML cannot preload all the font data in this
0116     /// function, so the buffer pointed by \a data has to remain
0117     /// valid until the sf::Font object loads a new font or
0118     /// is destroyed.
0119     ///
0120     /// \param data        Pointer to the file data in memory
0121     /// \param sizeInBytes Size of the data to load, in bytes
0122     ///
0123     /// \return True if loading succeeded, false if it failed
0124     ///
0125     /// \see loadFromFile, loadFromStream
0126     ///
0127     ////////////////////////////////////////////////////////////
0128     bool loadFromMemory(const void* data, std::size_t sizeInBytes);
0129 
0130     ////////////////////////////////////////////////////////////
0131     /// \brief Load the font from a custom stream
0132     ///
0133     /// The supported font formats are: TrueType, Type 1, CFF,
0134     /// OpenType, SFNT, X11 PCF, Windows FNT, BDF, PFR and Type 42.
0135     /// Warning: SFML cannot preload all the font data in this
0136     /// function, so the contents of \a stream have to remain
0137     /// valid as long as the font is used.
0138     ///
0139     /// \warning SFML cannot preload all the font data in this
0140     /// function, so the stream has to remain accessible until
0141     /// the sf::Font object loads a new font or is destroyed.
0142     ///
0143     /// \param stream Source stream to read from
0144     ///
0145     /// \return True if loading succeeded, false if it failed
0146     ///
0147     /// \see loadFromFile, loadFromMemory
0148     ///
0149     ////////////////////////////////////////////////////////////
0150     bool loadFromStream(InputStream& stream);
0151 
0152     ////////////////////////////////////////////////////////////
0153     /// \brief Get the font information
0154     ///
0155     /// \return A structure that holds the font information
0156     ///
0157     ////////////////////////////////////////////////////////////
0158     const Info& getInfo() const;
0159 
0160     ////////////////////////////////////////////////////////////
0161     /// \brief Retrieve a glyph of the font
0162     ///
0163     /// If the font is a bitmap font, not all character sizes
0164     /// might be available. If the glyph is not available at the
0165     /// requested size, an empty glyph is returned.
0166     ///
0167     /// You may want to use \ref hasGlyph to determine if the
0168     /// glyph exists before requesting it. If the glyph does not
0169     /// exist, a font specific default is returned.
0170     ///
0171     /// Be aware that using a negative value for the outline
0172     /// thickness will cause distorted rendering.
0173     ///
0174     /// \param codePoint        Unicode code point of the character to get
0175     /// \param characterSize    Reference character size
0176     /// \param bold             Retrieve the bold version or the regular one?
0177     /// \param outlineThickness Thickness of outline (when != 0 the glyph will not be filled)
0178     ///
0179     /// \return The glyph corresponding to \a codePoint and \a characterSize
0180     ///
0181     ////////////////////////////////////////////////////////////
0182     const Glyph& getGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) const;
0183 
0184     ////////////////////////////////////////////////////////////
0185     /// \brief Determine if this font has a glyph representing the requested code point
0186     ///
0187     /// Most fonts only include a very limited selection of glyphs from
0188     /// specific Unicode subsets, like Latin, Cyrillic, or Asian characters.
0189     ///
0190     /// While code points without representation will return a font specific
0191     /// default character, it might be useful to verify whether specific
0192     /// code points are included to determine whether a font is suited
0193     /// to display text in a specific language.
0194     ///
0195     /// \param codePoint Unicode code point to check
0196     ///
0197     /// \return True if the codepoint has a glyph representation, false otherwise
0198     ///
0199     ////////////////////////////////////////////////////////////
0200     bool hasGlyph(Uint32 codePoint) const;
0201 
0202     ////////////////////////////////////////////////////////////
0203     /// \brief Get the kerning offset of two glyphs
0204     ///
0205     /// The kerning is an extra offset (negative) to apply between two
0206     /// glyphs when rendering them, to make the pair look more "natural".
0207     /// For example, the pair "AV" have a special kerning to make them
0208     /// closer than other characters. Most of the glyphs pairs have a
0209     /// kerning offset of zero, though.
0210     ///
0211     /// \param first         Unicode code point of the first character
0212     /// \param second        Unicode code point of the second character
0213     /// \param characterSize Reference character size
0214     ///
0215     /// \return Kerning value for \a first and \a second, in pixels
0216     ///
0217     ////////////////////////////////////////////////////////////
0218     float getKerning(Uint32 first, Uint32 second, unsigned int characterSize, bool bold = false) const;
0219 
0220     ////////////////////////////////////////////////////////////
0221     /// \brief Get the line spacing
0222     ///
0223     /// Line spacing is the vertical offset to apply between two
0224     /// consecutive lines of text.
0225     ///
0226     /// \param characterSize Reference character size
0227     ///
0228     /// \return Line spacing, in pixels
0229     ///
0230     ////////////////////////////////////////////////////////////
0231     float getLineSpacing(unsigned int characterSize) const;
0232 
0233     ////////////////////////////////////////////////////////////
0234     /// \brief Get the position of the underline
0235     ///
0236     /// Underline position is the vertical offset to apply between the
0237     /// baseline and the underline.
0238     ///
0239     /// \param characterSize Reference character size
0240     ///
0241     /// \return Underline position, in pixels
0242     ///
0243     /// \see getUnderlineThickness
0244     ///
0245     ////////////////////////////////////////////////////////////
0246     float getUnderlinePosition(unsigned int characterSize) const;
0247 
0248     ////////////////////////////////////////////////////////////
0249     /// \brief Get the thickness of the underline
0250     ///
0251     /// Underline thickness is the vertical size of the underline.
0252     ///
0253     /// \param characterSize Reference character size
0254     ///
0255     /// \return Underline thickness, in pixels
0256     ///
0257     /// \see getUnderlinePosition
0258     ///
0259     ////////////////////////////////////////////////////////////
0260     float getUnderlineThickness(unsigned int characterSize) const;
0261 
0262     ////////////////////////////////////////////////////////////
0263     /// \brief Retrieve the texture containing the loaded glyphs of a certain size
0264     ///
0265     /// The contents of the returned texture changes as more glyphs
0266     /// are requested, thus it is not very relevant. It is mainly
0267     /// used internally by sf::Text.
0268     ///
0269     /// \param characterSize Reference character size
0270     ///
0271     /// \return Texture containing the glyphs of the requested size
0272     ///
0273     ////////////////////////////////////////////////////////////
0274     const Texture& getTexture(unsigned int characterSize) const;
0275 
0276     ////////////////////////////////////////////////////////////
0277     /// \brief Enable or disable the smooth filter
0278     ///
0279     /// When the filter is activated, the font appears smoother
0280     /// so that pixels are less noticeable. However if you want
0281     /// the font to look exactly the same as its source file,
0282     /// you should disable it.
0283     /// The smooth filter is enabled by default.
0284     ///
0285     /// \param smooth True to enable smoothing, false to disable it
0286     ///
0287     /// \see isSmooth
0288     ///
0289     ////////////////////////////////////////////////////////////
0290     void setSmooth(bool smooth);
0291 
0292     ////////////////////////////////////////////////////////////
0293     /// \brief Tell whether the smooth filter is enabled or not
0294     ///
0295     /// \return True if smoothing is enabled, false if it is disabled
0296     ///
0297     /// \see setSmooth
0298     ///
0299     ////////////////////////////////////////////////////////////
0300     bool isSmooth() const;
0301 
0302     ////////////////////////////////////////////////////////////
0303     /// \brief Overload of assignment operator
0304     ///
0305     /// \param right Instance to assign
0306     ///
0307     /// \return Reference to self
0308     ///
0309     ////////////////////////////////////////////////////////////
0310     Font& operator =(const Font& right);
0311 
0312 private:
0313 
0314     ////////////////////////////////////////////////////////////
0315     /// \brief Structure defining a row of glyphs
0316     ///
0317     ////////////////////////////////////////////////////////////
0318     struct Row
0319     {
0320         Row(unsigned int rowTop, unsigned int rowHeight) : width(0), top(rowTop), height(rowHeight) {}
0321 
0322         unsigned int width;  //!< Current width of the row
0323         unsigned int top;    //!< Y position of the row into the texture
0324         unsigned int height; //!< Height of the row
0325     };
0326 
0327     ////////////////////////////////////////////////////////////
0328     // Types
0329     ////////////////////////////////////////////////////////////
0330     typedef std::map<Uint64, Glyph> GlyphTable; //!< Table mapping a codepoint to its glyph
0331 
0332     ////////////////////////////////////////////////////////////
0333     /// \brief Structure defining a page of glyphs
0334     ///
0335     ////////////////////////////////////////////////////////////
0336     struct Page
0337     {
0338         explicit Page(bool smooth);
0339 
0340         GlyphTable       glyphs;  //!< Table mapping code points to their corresponding glyph
0341         Texture          texture; //!< Texture containing the pixels of the glyphs
0342         unsigned int     nextRow; //!< Y position of the next new row in the texture
0343         std::vector<Row> rows;    //!< List containing the position of all the existing rows
0344     };
0345 
0346     ////////////////////////////////////////////////////////////
0347     /// \brief Free all the internal resources
0348     ///
0349     ////////////////////////////////////////////////////////////
0350     void cleanup();
0351 
0352     ////////////////////////////////////////////////////////////
0353     /// \brief Find or create the glyphs page corresponding to the given character size
0354     ///
0355     /// \param characterSize Reference character size
0356     ///
0357     /// \return The glyphs page corresponding to \a characterSize
0358     ///
0359     ////////////////////////////////////////////////////////////
0360     Page& loadPage(unsigned int characterSize) const;
0361 
0362     ////////////////////////////////////////////////////////////
0363     /// \brief Load a new glyph and store it in the cache
0364     ///
0365     /// \param codePoint        Unicode code point of the character to load
0366     /// \param characterSize    Reference character size
0367     /// \param bold             Retrieve the bold version or the regular one?
0368     /// \param outlineThickness Thickness of outline (when != 0 the glyph will not be filled)
0369     ///
0370     /// \return The glyph corresponding to \a codePoint and \a characterSize
0371     ///
0372     ////////////////////////////////////////////////////////////
0373     Glyph loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, float outlineThickness) const;
0374 
0375     ////////////////////////////////////////////////////////////
0376     /// \brief Find a suitable rectangle within the texture for a glyph
0377     ///
0378     /// \param page   Page of glyphs to search in
0379     /// \param width  Width of the rectangle
0380     /// \param height Height of the rectangle
0381     ///
0382     /// \return Found rectangle within the texture
0383     ///
0384     ////////////////////////////////////////////////////////////
0385     IntRect findGlyphRect(Page& page, unsigned int width, unsigned int height) const;
0386 
0387     ////////////////////////////////////////////////////////////
0388     /// \brief Make sure that the given size is the current one
0389     ///
0390     /// \param characterSize Reference character size
0391     ///
0392     /// \return True on success, false if any error happened
0393     ///
0394     ////////////////////////////////////////////////////////////
0395     bool setCurrentSize(unsigned int characterSize) const;
0396 
0397     ////////////////////////////////////////////////////////////
0398     // Types
0399     ////////////////////////////////////////////////////////////
0400     typedef std::map<unsigned int, Page> PageTable; //!< Table mapping a character size to its page (texture)
0401 
0402     ////////////////////////////////////////////////////////////
0403     // Member data
0404     ////////////////////////////////////////////////////////////
0405     void*                      m_library;     //!< Pointer to the internal library interface (it is typeless to avoid exposing implementation details)
0406     void*                      m_face;        //!< Pointer to the internal font face (it is typeless to avoid exposing implementation details)
0407     void*                      m_streamRec;   //!< Pointer to the stream rec instance (it is typeless to avoid exposing implementation details)
0408     void*                      m_stroker;     //!< Pointer to the stroker (it is typeless to avoid exposing implementation details)
0409     int*                       m_refCount;    //!< Reference counter used by implicit sharing
0410     bool                       m_isSmooth;    //!< Status of the smooth filter
0411     Info                       m_info;        //!< Information about the font
0412     mutable PageTable          m_pages;       //!< Table containing the glyphs pages by character size
0413     mutable std::vector<Uint8> m_pixelBuffer; //!< Pixel buffer holding a glyph's pixels before being written to the texture
0414     #ifdef SFML_SYSTEM_ANDROID
0415     void*                      m_stream; //!< Asset file streamer (if loaded from file)
0416     #endif
0417 };
0418 
0419 } // namespace sf
0420 
0421 
0422 #endif // SFML_FONT_HPP
0423 
0424 
0425 ////////////////////////////////////////////////////////////
0426 /// \class sf::Font
0427 /// \ingroup graphics
0428 ///
0429 /// Fonts can be loaded from a file, from memory or from a custom
0430 /// stream, and supports the most common types of fonts. See
0431 /// the loadFromFile function for the complete list of supported formats.
0432 ///
0433 /// Once it is loaded, a sf::Font instance provides three
0434 /// types of information about the font:
0435 /// \li Global metrics, such as the line spacing
0436 /// \li Per-glyph metrics, such as bounding box or kerning
0437 /// \li Pixel representation of glyphs
0438 ///
0439 /// Fonts alone are not very useful: they hold the font data
0440 /// but cannot make anything useful of it. To do so you need to
0441 /// use the sf::Text class, which is able to properly output text
0442 /// with several options such as character size, style, color,
0443 /// position, rotation, etc.
0444 /// This separation allows more flexibility and better performances:
0445 /// indeed a sf::Font is a heavy resource, and any operation on it
0446 /// is slow (often too slow for real-time applications). On the other
0447 /// side, a sf::Text is a lightweight object which can combine the
0448 /// glyphs data and metrics of a sf::Font to display any text on a
0449 /// render target.
0450 /// Note that it is also possible to bind several sf::Text instances
0451 /// to the same sf::Font.
0452 ///
0453 /// It is important to note that the sf::Text instance doesn't
0454 /// copy the font that it uses, it only keeps a reference to it.
0455 /// Thus, a sf::Font must not be destructed while it is
0456 /// used by a sf::Text (i.e. never write a function that
0457 /// uses a local sf::Font instance for creating a text).
0458 ///
0459 /// Usage example:
0460 /// \code
0461 /// // Declare a new font
0462 /// sf::Font font;
0463 ///
0464 /// // Load it from a file
0465 /// if (!font.loadFromFile("arial.ttf"))
0466 /// {
0467 ///     // error...
0468 /// }
0469 ///
0470 /// // Create a text which uses our font
0471 /// sf::Text text1;
0472 /// text1.setFont(font);
0473 /// text1.setCharacterSize(30);
0474 /// text1.setStyle(sf::Text::Regular);
0475 ///
0476 /// // Create another text using the same font, but with different parameters
0477 /// sf::Text text2;
0478 /// text2.setFont(font);
0479 /// text2.setCharacterSize(50);
0480 /// text2.setStyle(sf::Text::Italic);
0481 /// \endcode
0482 ///
0483 /// Apart from loading font files, and passing them to instances
0484 /// of sf::Text, you should normally not have to deal directly
0485 /// with this class. However, it may be useful to access the
0486 /// font metrics or rasterized glyphs for advanced usage.
0487 ///
0488 /// Note that if the font is a bitmap font, it is not scalable,
0489 /// thus not all requested sizes will be available to use. This
0490 /// needs to be taken into consideration when using sf::Text.
0491 /// If you need to display text of a certain size, make sure the
0492 /// corresponding bitmap font that supports that size is used.
0493 ///
0494 /// \see sf::Text
0495 ///
0496 ////////////////////////////////////////////////////////////