|
|
|||
File indexing completed on 2026-06-02 08:58:14
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_TEXT_HPP 0026 #define SFML_TEXT_HPP 0027 0028 //////////////////////////////////////////////////////////// 0029 // Headers 0030 //////////////////////////////////////////////////////////// 0031 #include <SFML/Graphics/Export.hpp> 0032 #include <SFML/Graphics/Drawable.hpp> 0033 #include <SFML/Graphics/Transformable.hpp> 0034 #include <SFML/Graphics/Font.hpp> 0035 #include <SFML/Graphics/Rect.hpp> 0036 #include <SFML/Graphics/VertexArray.hpp> 0037 #include <SFML/System/String.hpp> 0038 #include <string> 0039 #include <vector> 0040 0041 0042 namespace sf 0043 { 0044 //////////////////////////////////////////////////////////// 0045 /// \brief Graphical text that can be drawn to a render target 0046 /// 0047 //////////////////////////////////////////////////////////// 0048 class SFML_GRAPHICS_API Text : public Drawable, public Transformable 0049 { 0050 public: 0051 0052 //////////////////////////////////////////////////////////// 0053 /// \brief Enumeration of the string drawing styles 0054 /// 0055 //////////////////////////////////////////////////////////// 0056 enum Style 0057 { 0058 Regular = 0, //!< Regular characters, no style 0059 Bold = 1 << 0, //!< Bold characters 0060 Italic = 1 << 1, //!< Italic characters 0061 Underlined = 1 << 2, //!< Underlined characters 0062 StrikeThrough = 1 << 3 //!< Strike through characters 0063 }; 0064 0065 //////////////////////////////////////////////////////////// 0066 /// \brief Default constructor 0067 /// 0068 /// Creates an empty text. 0069 /// 0070 //////////////////////////////////////////////////////////// 0071 Text(); 0072 0073 //////////////////////////////////////////////////////////// 0074 /// \brief Construct the text from a string, font and size 0075 /// 0076 /// Note that if the used font is a bitmap font, it is not 0077 /// scalable, thus not all requested sizes will be available 0078 /// to use. This needs to be taken into consideration when 0079 /// setting the character size. If you need to display text 0080 /// of a certain size, make sure the corresponding bitmap 0081 /// font that supports that size is used. 0082 /// 0083 /// \param string Text assigned to the string 0084 /// \param font Font used to draw the string 0085 /// \param characterSize Base size of characters, in pixels 0086 /// 0087 //////////////////////////////////////////////////////////// 0088 Text(const String& string, const Font& font, unsigned int characterSize = 30); 0089 0090 //////////////////////////////////////////////////////////// 0091 /// \brief Set the text's string 0092 /// 0093 /// The \a string argument is a sf::String, which can 0094 /// automatically be constructed from standard string types. 0095 /// So, the following calls are all valid: 0096 /// \code 0097 /// text.setString("hello"); 0098 /// text.setString(L"hello"); 0099 /// text.setString(std::string("hello")); 0100 /// text.setString(std::wstring(L"hello")); 0101 /// \endcode 0102 /// A text's string is empty by default. 0103 /// 0104 /// \param string New string 0105 /// 0106 /// \see getString 0107 /// 0108 //////////////////////////////////////////////////////////// 0109 void setString(const String& string); 0110 0111 //////////////////////////////////////////////////////////// 0112 /// \brief Set the text's font 0113 /// 0114 /// The \a font argument refers to a font that must 0115 /// exist as long as the text uses it. Indeed, the text 0116 /// doesn't store its own copy of the font, but rather keeps 0117 /// a pointer to the one that you passed to this function. 0118 /// If the font is destroyed and the text tries to 0119 /// use it, the behavior is undefined. 0120 /// 0121 /// \param font New font 0122 /// 0123 /// \see getFont 0124 /// 0125 //////////////////////////////////////////////////////////// 0126 void setFont(const Font& font); 0127 0128 //////////////////////////////////////////////////////////// 0129 /// \brief Set the character size 0130 /// 0131 /// The default size is 30. 0132 /// 0133 /// Note that if the used font is a bitmap font, it is not 0134 /// scalable, thus not all requested sizes will be available 0135 /// to use. This needs to be taken into consideration when 0136 /// setting the character size. If you need to display text 0137 /// of a certain size, make sure the corresponding bitmap 0138 /// font that supports that size is used. 0139 /// 0140 /// \param size New character size, in pixels 0141 /// 0142 /// \see getCharacterSize 0143 /// 0144 //////////////////////////////////////////////////////////// 0145 void setCharacterSize(unsigned int size); 0146 0147 //////////////////////////////////////////////////////////// 0148 /// \brief Set the line spacing factor 0149 /// 0150 /// The default spacing between lines is defined by the font. 0151 /// This method enables you to set a factor for the spacing 0152 /// between lines. By default the line spacing factor is 1. 0153 /// 0154 /// \param spacingFactor New line spacing factor 0155 /// 0156 /// \see getLineSpacing 0157 /// 0158 //////////////////////////////////////////////////////////// 0159 void setLineSpacing(float spacingFactor); 0160 0161 //////////////////////////////////////////////////////////// 0162 /// \brief Set the letter spacing factor 0163 /// 0164 /// The default spacing between letters is defined by the font. 0165 /// This factor doesn't directly apply to the existing 0166 /// spacing between each character, it rather adds a fixed 0167 /// space between them which is calculated from the font 0168 /// metrics and the character size. 0169 /// Note that factors below 1 (including negative numbers) bring 0170 /// characters closer to each other. 0171 /// By default the letter spacing factor is 1. 0172 /// 0173 /// \param spacingFactor New letter spacing factor 0174 /// 0175 /// \see getLetterSpacing 0176 /// 0177 //////////////////////////////////////////////////////////// 0178 void setLetterSpacing(float spacingFactor); 0179 0180 //////////////////////////////////////////////////////////// 0181 /// \brief Set the text's style 0182 /// 0183 /// You can pass a combination of one or more styles, for 0184 /// example sf::Text::Bold | sf::Text::Italic. 0185 /// The default style is sf::Text::Regular. 0186 /// 0187 /// \param style New style 0188 /// 0189 /// \see getStyle 0190 /// 0191 //////////////////////////////////////////////////////////// 0192 void setStyle(Uint32 style); 0193 0194 //////////////////////////////////////////////////////////// 0195 /// \brief Set the fill color of the text 0196 /// 0197 /// By default, the text's fill color is opaque white. 0198 /// Setting the fill color to a transparent color with an outline 0199 /// will cause the outline to be displayed in the fill area of the text. 0200 /// 0201 /// \param color New fill color of the text 0202 /// 0203 /// \see getFillColor 0204 /// 0205 /// \deprecated There is now fill and outline colors instead 0206 /// of a single global color. 0207 /// Use setFillColor() or setOutlineColor() instead. 0208 /// 0209 //////////////////////////////////////////////////////////// 0210 SFML_DEPRECATED void setColor(const Color& color); 0211 0212 //////////////////////////////////////////////////////////// 0213 /// \brief Set the fill color of the text 0214 /// 0215 /// By default, the text's fill color is opaque white. 0216 /// Setting the fill color to a transparent color with an outline 0217 /// will cause the outline to be displayed in the fill area of the text. 0218 /// 0219 /// \param color New fill color of the text 0220 /// 0221 /// \see getFillColor 0222 /// 0223 //////////////////////////////////////////////////////////// 0224 void setFillColor(const Color& color); 0225 0226 //////////////////////////////////////////////////////////// 0227 /// \brief Set the outline color of the text 0228 /// 0229 /// By default, the text's outline color is opaque black. 0230 /// 0231 /// \param color New outline color of the text 0232 /// 0233 /// \see getOutlineColor 0234 /// 0235 //////////////////////////////////////////////////////////// 0236 void setOutlineColor(const Color& color); 0237 0238 //////////////////////////////////////////////////////////// 0239 /// \brief Set the thickness of the text's outline 0240 /// 0241 /// By default, the outline thickness is 0. 0242 /// 0243 /// Be aware that using a negative value for the outline 0244 /// thickness will cause distorted rendering. 0245 /// 0246 /// \param thickness New outline thickness, in pixels 0247 /// 0248 /// \see getOutlineThickness 0249 /// 0250 //////////////////////////////////////////////////////////// 0251 void setOutlineThickness(float thickness); 0252 0253 //////////////////////////////////////////////////////////// 0254 /// \brief Get the text's string 0255 /// 0256 /// The returned string is a sf::String, which can automatically 0257 /// be converted to standard string types. So, the following 0258 /// lines of code are all valid: 0259 /// \code 0260 /// sf::String s1 = text.getString(); 0261 /// std::string s2 = text.getString(); 0262 /// std::wstring s3 = text.getString(); 0263 /// \endcode 0264 /// 0265 /// \return Text's string 0266 /// 0267 /// \see setString 0268 /// 0269 //////////////////////////////////////////////////////////// 0270 const String& getString() const; 0271 0272 //////////////////////////////////////////////////////////// 0273 /// \brief Get the text's font 0274 /// 0275 /// If the text has no font attached, a NULL pointer is returned. 0276 /// The returned pointer is const, which means that you 0277 /// cannot modify the font when you get it from this function. 0278 /// 0279 /// \return Pointer to the text's font 0280 /// 0281 /// \see setFont 0282 /// 0283 //////////////////////////////////////////////////////////// 0284 const Font* getFont() const; 0285 0286 //////////////////////////////////////////////////////////// 0287 /// \brief Get the character size 0288 /// 0289 /// \return Size of the characters, in pixels 0290 /// 0291 /// \see setCharacterSize 0292 /// 0293 //////////////////////////////////////////////////////////// 0294 unsigned int getCharacterSize() const; 0295 0296 //////////////////////////////////////////////////////////// 0297 /// \brief Get the size of the letter spacing factor 0298 /// 0299 /// \return Size of the letter spacing factor 0300 /// 0301 /// \see setLetterSpacing 0302 /// 0303 //////////////////////////////////////////////////////////// 0304 float getLetterSpacing() const; 0305 0306 //////////////////////////////////////////////////////////// 0307 /// \brief Get the size of the line spacing factor 0308 /// 0309 /// \return Size of the line spacing factor 0310 /// 0311 /// \see setLineSpacing 0312 /// 0313 //////////////////////////////////////////////////////////// 0314 float getLineSpacing() const; 0315 0316 //////////////////////////////////////////////////////////// 0317 /// \brief Get the text's style 0318 /// 0319 /// \return Text's style 0320 /// 0321 /// \see setStyle 0322 /// 0323 //////////////////////////////////////////////////////////// 0324 Uint32 getStyle() const; 0325 0326 //////////////////////////////////////////////////////////// 0327 /// \brief Get the fill color of the text 0328 /// 0329 /// \return Fill color of the text 0330 /// 0331 /// \see setFillColor 0332 /// 0333 /// \deprecated There is now fill and outline colors instead 0334 /// of a single global color. 0335 /// Use getFillColor() or getOutlineColor() instead. 0336 /// 0337 //////////////////////////////////////////////////////////// 0338 SFML_DEPRECATED const Color& getColor() const; 0339 0340 //////////////////////////////////////////////////////////// 0341 /// \brief Get the fill color of the text 0342 /// 0343 /// \return Fill color of the text 0344 /// 0345 /// \see setFillColor 0346 /// 0347 //////////////////////////////////////////////////////////// 0348 const Color& getFillColor() const; 0349 0350 //////////////////////////////////////////////////////////// 0351 /// \brief Get the outline color of the text 0352 /// 0353 /// \return Outline color of the text 0354 /// 0355 /// \see setOutlineColor 0356 /// 0357 //////////////////////////////////////////////////////////// 0358 const Color& getOutlineColor() const; 0359 0360 //////////////////////////////////////////////////////////// 0361 /// \brief Get the outline thickness of the text 0362 /// 0363 /// \return Outline thickness of the text, in pixels 0364 /// 0365 /// \see setOutlineThickness 0366 /// 0367 //////////////////////////////////////////////////////////// 0368 float getOutlineThickness() const; 0369 0370 //////////////////////////////////////////////////////////// 0371 /// \brief Return the position of the \a index-th character 0372 /// 0373 /// This function computes the visual position of a character 0374 /// from its index in the string. The returned position is 0375 /// in global coordinates (translation, rotation, scale and 0376 /// origin are applied). 0377 /// If \a index is out of range, the position of the end of 0378 /// the string is returned. 0379 /// 0380 /// \param index Index of the character 0381 /// 0382 /// \return Position of the character 0383 /// 0384 //////////////////////////////////////////////////////////// 0385 Vector2f findCharacterPos(std::size_t index) const; 0386 0387 //////////////////////////////////////////////////////////// 0388 /// \brief Get the local bounding rectangle of the entity 0389 /// 0390 /// The returned rectangle is in local coordinates, which means 0391 /// that it ignores the transformations (translation, rotation, 0392 /// scale, ...) that are applied to the entity. 0393 /// In other words, this function returns the bounds of the 0394 /// entity in the entity's coordinate system. 0395 /// 0396 /// \return Local bounding rectangle of the entity 0397 /// 0398 //////////////////////////////////////////////////////////// 0399 FloatRect getLocalBounds() const; 0400 0401 //////////////////////////////////////////////////////////// 0402 /// \brief Get the global bounding rectangle of the entity 0403 /// 0404 /// The returned rectangle is in global coordinates, which means 0405 /// that it takes into account the transformations (translation, 0406 /// rotation, scale, ...) that are applied to the entity. 0407 /// In other words, this function returns the bounds of the 0408 /// text in the global 2D world's coordinate system. 0409 /// 0410 /// \return Global bounding rectangle of the entity 0411 /// 0412 //////////////////////////////////////////////////////////// 0413 FloatRect getGlobalBounds() const; 0414 0415 private: 0416 0417 //////////////////////////////////////////////////////////// 0418 /// \brief Draw the text to a render target 0419 /// 0420 /// \param target Render target to draw to 0421 /// \param states Current render states 0422 /// 0423 //////////////////////////////////////////////////////////// 0424 virtual void draw(RenderTarget& target, RenderStates states) const; 0425 0426 //////////////////////////////////////////////////////////// 0427 /// \brief Make sure the text's geometry is updated 0428 /// 0429 /// All the attributes related to rendering are cached, such 0430 /// that the geometry is only updated when necessary. 0431 /// 0432 //////////////////////////////////////////////////////////// 0433 void ensureGeometryUpdate() const; 0434 0435 //////////////////////////////////////////////////////////// 0436 // Member data 0437 //////////////////////////////////////////////////////////// 0438 String m_string; //!< String to display 0439 const Font* m_font; //!< Font used to display the string 0440 unsigned int m_characterSize; //!< Base size of characters, in pixels 0441 float m_letterSpacingFactor; //!< Spacing factor between letters 0442 float m_lineSpacingFactor; //!< Spacing factor between lines 0443 Uint32 m_style; //!< Text style (see Style enum) 0444 Color m_fillColor; //!< Text fill color 0445 Color m_outlineColor; //!< Text outline color 0446 float m_outlineThickness; //!< Thickness of the text's outline 0447 mutable VertexArray m_vertices; //!< Vertex array containing the fill geometry 0448 mutable VertexArray m_outlineVertices; //!< Vertex array containing the outline geometry 0449 mutable FloatRect m_bounds; //!< Bounding rectangle of the text (in local coordinates) 0450 mutable bool m_geometryNeedUpdate; //!< Does the geometry need to be recomputed? 0451 mutable Uint64 m_fontTextureId; //!< The font texture id 0452 }; 0453 0454 } // namespace sf 0455 0456 0457 #endif // SFML_TEXT_HPP 0458 0459 0460 //////////////////////////////////////////////////////////// 0461 /// \class sf::Text 0462 /// \ingroup graphics 0463 /// 0464 /// sf::Text is a drawable class that allows to easily display 0465 /// some text with custom style and color on a render target. 0466 /// 0467 /// It inherits all the functions from sf::Transformable: 0468 /// position, rotation, scale, origin. It also adds text-specific 0469 /// properties such as the font to use, the character size, 0470 /// the font style (bold, italic, underlined and strike through), the 0471 /// text color, the outline thickness, the outline color, the character 0472 /// spacing, the line spacing and the text to display of course. 0473 /// It also provides convenience functions to calculate the 0474 /// graphical size of the text, or to get the global position 0475 /// of a given character. 0476 /// 0477 /// sf::Text works in combination with the sf::Font class, which 0478 /// loads and provides the glyphs (visual characters) of a given font. 0479 /// 0480 /// The separation of sf::Font and sf::Text allows more flexibility 0481 /// and better performances: indeed a sf::Font is a heavy resource, 0482 /// and any operation on it is slow (often too slow for real-time 0483 /// applications). On the other side, a sf::Text is a lightweight 0484 /// object which can combine the glyphs data and metrics of a sf::Font 0485 /// to display any text on a render target. 0486 /// 0487 /// It is important to note that the sf::Text instance doesn't 0488 /// copy the font that it uses, it only keeps a reference to it. 0489 /// Thus, a sf::Font must not be destructed while it is 0490 /// used by a sf::Text (i.e. never write a function that 0491 /// uses a local sf::Font instance for creating a text). 0492 /// 0493 /// See also the note on coordinates and undistorted rendering in sf::Transformable. 0494 /// 0495 /// Usage example: 0496 /// \code 0497 /// // Declare and load a font 0498 /// sf::Font font; 0499 /// font.loadFromFile("arial.ttf"); 0500 /// 0501 /// // Create a text 0502 /// sf::Text text("hello", font); 0503 /// text.setCharacterSize(30); 0504 /// text.setStyle(sf::Text::Bold); 0505 /// text.setFillColor(sf::Color::Red); 0506 /// 0507 /// // Draw it 0508 /// window.draw(text); 0509 /// \endcode 0510 /// 0511 /// \see sf::Font, sf::Transformable 0512 /// 0513 ////////////////////////////////////////////////////////////
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|