28 #include <SFML/Graphics/String.hpp>
29 #include <SFML/Graphics/Image.hpp>
30 #include <SFML/Graphics/GraphicsContext.hpp>
40 myFont (&
Font::GetDefaultFont()),
43 myNeedRectUpdate(true)
56 myNeedRectUpdate(true)
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;
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;
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;
270 glTexCoord2f(Coord.
Left, Coord.
Top); glVertex2f(X + Rect.
Left - ItalicCoeff * Rect.
Top, Y + Rect.
Top);
273 glTexCoord2f(Coord.
Right, Coord.
Top); glVertex2f(X + Rect.
Right - ItalicCoeff * Rect.
Top, Y + Rect.
Top);
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)
284 glTexCoord2f(Coord.
Left, Coord.
Top); glVertex2f(X + OffsetsX[j] + Rect.
Left - ItalicCoeff * Rect.
Top, Y + OffsetsY[j] + Rect.
Top);
285 glTexCoord2f(Coord.
Left, Coord.
Bottom); glVertex2f(X + OffsetsX[j] + Rect.
Left - ItalicCoeff * Rect.
Bottom, Y + OffsetsY[j] + Rect.
Bottom);
286 glTexCoord2f(Coord.
Right, Coord.
Bottom); glVertex2f(X + OffsetsX[j] + Rect.
Right - ItalicCoeff * Rect.
Bottom, Y + OffsetsY[j] + Rect.
Bottom);
287 glTexCoord2f(Coord.
Right, Coord.
Top); glVertex2f(X + OffsetsX[j] + Rect.
Right - ItalicCoeff * Rect.
Top, Y + OffsetsY[j] + Rect.
Top);
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]);
324 void String::RecomputeRect()
327 const Unicode::UTF32String& Text = myText;
330 myNeedRectUpdate =
false;
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;
String()
Default constructor.
void SetFont(const Font &CharFont)
Set the font of the string.
T x
X coordinate of the vector.
const Vector2f & GetPosition() const
Get the position of the object.
float GetSize() const
Get the size of the characters.
void SetStyle(unsigned long TextStyle)
Set the style of the text The default style is Regular.
Characters are underlined.
void SetSize(float Size)
Set the size of the string The default size is 30.
Characters are in italic.
FloatRect TexCoords
Texture coordinates of the glyph inside the bitmap font.
T Right
Right coordinate of the rectangle.
FloatRect GetRect() const
Get the string rectangle on screen.
Font is the low-level class for loading and manipulating character fonts.
This class is an abstract definition of a unicode text, it can be converted from and to any kind of s...
const Vector2f & GetScale() const
Get the current scale of the object.
const Vector2f & GetCenter() const
Get the center of the object.
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.
IntRect Rectangle
Bounding rectangle of the glyph, in relative coordinates.
String defines a graphical 2D text, that can be drawn on screen.
int Advance
Offset to move horizontically to the next character.
unsigned long GetStyle() const
Get the style of the text.
Structure describing a glyph (a visual character)
virtual void Render(RenderTarget &Target) const
/see Drawable::Render
T Bottom
Bottom coordinate of the rectangle.
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...
T y
Y coordinate of the vector.
void SetText(const Unicode::Text &Text)
Set the text (from any kind of string)
Base class for all render targets (window, image, ...)
T Top
Top coordinate of the rectangle.
T Left
Left coordinate of the rectangle.