-
Notifications
You must be signed in to change notification settings - Fork 30
Fonts and Texts
The relationship between fonts and texts is a lot like the relationship between textures and sprites: one font can be used to print many texts. Due to this similarity, this chapter will be a little shorter.
Fonts can be loaded just like textures using the [loadFromFile](http://jsfml.org/javadoc/org/jsfml/graphics/Font.html#loadFromFile(java.nio.file.Path\)) method. The supported font file formats are ttf, cff, pcf, fnt, bdf, pfr, sfnt, type 1, type 42
.
A Text can be used to print a text string using a font. Texts are drawable and transformable, much like sprites.
The following example code will load a font and display a few texts on the screen. In this example, the FreeSans TTF font is used, which can be obtained as part of the GNU FreeFont fonts.
//Create and try to load font
Font freeSans = new Font();
try {
freeSans.loadFromFile(Paths.get("FreeSans.ttf"));
} catch(IOException ex) {
//Failed to load font
ex.printStackTrace();
}
//Create some texts with different colors, sizes and styles
Text text1 = new Text("This is an example text.", freeSans, 24);
text1.setPosition(100, 100);
Text text2 = new Text("This text is red, bold and underlined!", freeSans, 32);
text2.setColor(Color.RED);
text2.setStyle(Text.BOLD | Text.UNDERLINED);
Text text3 = new Text("Centered, italic and unicode: Привет!", freeSans, 32);
text3.setStyle(Text.ITALIC);
//Center and rotate text3
FloatRect text3bounds = text3.getLocalBounds();
text3.setOrigin(text3bounds.width / 2, text3bounds.height / 2);
text3.setPosition(320, 240);
text3.setRotation(20);
//Main loop
while(window.isOpen()) {
window.clear();
window.draw(text1);
window.draw(text2);
window.draw(text3);
window.display();
// ... (event handling)
}
To align text, you can use its local bounds and set its origin. Remember doing this everytime the text string changes (which can be done using setString, because that also changes its local bounds.
In the code above, the origin of text3
gets set to the center of its local bounds. This will center the text both horizontally and vertically at its position.
Texts can be styled by making them bold, italic or by underlining them. The style is a bitfield, that means that the various style flags need to be combined using the bitwise OR operator |
.
For example, text2
becomes bold and underlined by combining the respective flags: Text.BOLD | Text.UNDERLINED
.
#Font properties and Glyphs Internally, SFML pre-renders a font's gylphs into a texture. This allows for much faster drawing than rendering them live.
The texture in which a font's gylphs are pre-rendered can be obtained using the [getTexture](http://jsfml.org/javadoc/org/jsfml/graphics/Font.html#getTexture(int\)) method. The data (source rectangles and boundaries) for the single gylphs are represented by the Glyph class. You can obtain a Glyph using the font's [getGlyph](http://jsfml.org/javadoc/org/jsfml/graphics/Font.html#getGlyph(int, int, boolean)) method.
Fonts also provide methods to get their [kerning](http://jsfml.org/javadoc/org/jsfml/graphics/Font.html#getKerning(int, int, int)) and [line spacing](http://jsfml.org/javadoc/org/jsfml/graphics/Font.html#getLineSpacing(int\)).
Conclusively, you can retrieve all the relevant information to draw texts entirely on your own. This may be useful when you need texts where single characters have a different color, as seen in Quake.