28#include <SFML/Graphics/String.hpp>
29#include <SFML/Graphics/Image.hpp>
30#include <SFML/Graphics/GraphicsContext.hpp>
40myFont (&
Font::GetDefaultFont()),
67 myNeedRectUpdate =
true;
77 if (myFont != &CharFont)
79 myNeedRectUpdate =
true;
92 myNeedRectUpdate =
true;
104 if (myStyle != TextStyle)
106 myNeedRectUpdate =
true;
156 const Unicode::UTF32String& Text = myText;
159 if (Index > Text.length())
160 Index = Text.length();
163 float FactorX = mySize / myFont->GetCharacterSize();
164 float AdvanceY = mySize;
167 sf::Vector2f Position;
168 for (std::size_t i = 0; i < Index; ++i)
171 Uint32 CurChar = Text[i];
172 const Glyph& CurGlyph = myFont->GetGlyph(CurChar);
173 float AdvanceX = CurGlyph.
Advance * FactorX;
178 case L
' ' : Position.
x += AdvanceX;
break;
179 case L
'\t' : Position.
x += AdvanceX * 4;
break;
180 case L
'\v' : Position.
y += AdvanceY * 4;
break;
181 case L
'\n' : Position.
y += AdvanceY; Position.
x = 0;
break;
184 default : Position.
x += AdvanceX;
break;
197 if (myNeedRectUpdate)
198 const_cast<String*
>(
this)->RecomputeRect();
216 const Unicode::UTF32String& Text = myText;
223 float CharSize =
static_cast<float>(myFont->GetCharacterSize());
224 float Factor = mySize / CharSize;
225 GLCheck(glScalef(Factor, Factor, 1.f));
228 myFont->GetImage().Bind();
235 std::vector<float> UnderlineCoords;
236 UnderlineCoords.reserve(16);
239 float ItalicCoeff = (myStyle &
Italic) ? 0.208f : 0.f;
243 for (std::size_t i = 0; i < Text.size(); ++i)
246 Uint32 CurChar = Text[i];
247 const Glyph& CurGlyph = myFont->GetGlyph(CurChar);
248 int Advance = CurGlyph.
Advance;
250 const FloatRect& Coord = CurGlyph.
TexCoords;
254 if ((CurChar == L
'\n') && (myStyle &
Underlined))
256 UnderlineCoords.push_back(X);
257 UnderlineCoords.push_back(Y + 2);
263 case L
' ' : X += Advance;
continue;
264 case L
'\n' : Y += CharSize; X = 0;
continue;
265 case L
'\t' : X += Advance * 4;
continue;
266 case L
'\v' : Y += CharSize * 4;
continue;
279 static const float OffsetsX[] = {-0.5f, 0.5f, 0.f, 0.f};
280 static const float OffsetsY[] = {0.f, 0.f, -0.5f, 0.5f};
282 for (
int j = 0; j < 4; ++j)
300 float Thickness = (myStyle &
Bold) ? 3.f : 2.f;
303 UnderlineCoords.push_back(X);
304 UnderlineCoords.push_back(Y + 2);
307 GLCheck(glDisable(GL_TEXTURE_2D));
309 for (std::size_t i = 0; i < UnderlineCoords.size(); i += 2)
311 glVertex2f(0, UnderlineCoords[i + 1]);
312 glVertex2f(0, UnderlineCoords[i + 1] + Thickness);
313 glVertex2f(UnderlineCoords[i], UnderlineCoords[i + 1] + Thickness);
314 glVertex2f(UnderlineCoords[i], UnderlineCoords[i + 1]);
324void String::RecomputeRect()
327 const Unicode::UTF32String& Text = myText;
330 myNeedRectUpdate =
false;
335 myBaseRect = FloatRect(0, 0, 0, 0);
344 float Factor = mySize / myFont->GetCharacterSize();
347 for (std::size_t i = 0; i < Text.size(); ++i)
350 Uint32 CurChar = Text[i];
351 const Glyph& CurGlyph = myFont->GetGlyph(CurChar);
352 float Advance = CurGlyph.Advance * Factor;
353 const IntRect& Rect = CurGlyph.Rectangle;
358 case L
' ' : CurWidth += Advance;
continue;
359 case L
'\t' : CurWidth += Advance * 4;
continue;
360 case L
'\v' : Height += mySize * 4; CurHeight = 0;
continue;
365 if (CurWidth > Width)
375 float CharHeight = (myFont->GetCharacterSize() + Rect.Bottom) * Factor;
376 if (CharHeight > CurHeight)
377 CurHeight = CharHeight;
381 if (CurWidth > Width)
389 Height += 1 * Factor;
395 Width += 0.208f * mySize;
401 if (CurHeight < mySize + 4 * Factor)
402 Height += 4 * Factor;
408 myBaseRect.Right = Width;
409 myBaseRect.Bottom = Height;
const Vector2f & GetPosition() const
Get the position of the object.
const Vector2f & GetScale() const
Get the current scale of the object.
const Vector2f & GetCenter() const
Get the center of the object.
Font is the low-level class for loading and manipulating character fonts.
Structure describing a glyph (a visual character).
IntRect Rectangle
Bounding rectangle of the glyph, in relative coordinates.
int Advance
Offset to move horizontically to the next character.
FloatRect TexCoords
Texture coordinates of the glyph inside the bitmap font.
Rect is an utility class for manipulating rectangles.
T Top
Top coordinate of the rectangle.
T Right
Right coordinate of the rectangle.
T Left
Left coordinate of the rectangle.
T Bottom
Bottom coordinate of the rectangle.
const Unicode::Text & GetText() const
Get the text (the returned text can be converted implicitely to any kind of string).
const Font & GetFont() const
Get the font used by the string.
void SetFont(const Font &CharFont)
Set the font of the string.
void SetSize(float Size)
Set the size of the string The default size is 30.
FloatRect GetRect() const
Get the string rectangle on screen.
void SetStyle(unsigned long TextStyle)
Set the style of the text The default style is Regular.
String()
Default constructor.
float GetSize() const
Get the size of the characters.
void SetText(const Unicode::Text &Text)
Set the text (from any kind of string).
virtual void Render(RenderTarget &Target) const
/see Drawable::Render
sf::Vector2f GetCharacterPos(std::size_t Index) const
Return the visual position of the Index-th character of the string, in coordinates relative to the st...
unsigned long GetStyle() const
Get the style of the text.
@ Regular
Regular characters, no style.
@ Bold
Characters are bold.
@ Underlined
Characters are underlined.
@ Italic
Characters are in italic.
This class is an abstract definition of a unicode text, it can be converted from and to any kind of s...
T x
X coordinate of the vector.
T y
Y coordinate of the vector.