SFML - Simple and Fast Multimedia Library

SFML 3.0.0

- Main Page
- Topics
- Namespaces
- Classes
- Files

- File List
- File Members

Loading...
Searching...
No Matches
Font.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Graphics/Export.hpp>
31
32#include <SFML/Graphics/Glyph.hpp>
33#include <SFML/Graphics/Rect.hpp>
34#include <SFML/Graphics/Texture.hpp>
35
36#include <SFML/System/Vector2.hpp>
37
38#include <filesystem>
39#include <memory>
40#include <string>
41#include <unordered_map>
42#include <vector>
43
44#include <cstddef>
45#include <cstdint>
46
47
48#ifdef SFML_SYSTEM_ANDROID
49namespace sf::priv
50{
51class ResourceStream;
52}
53#endif
54
55namespace sf
56{
57class InputStream;
58
63 class SFML_GRAPHICS_API Font
64{
65public:
70 struct Info
71 {
72 std::string family;
73 };
74
81 Font() = default;
82
103 explicit Font(const std::filesystem::path& filename);
104
124 Font(const void* data, std::size_t sizeInBytes);
125
146 explicit Font(InputStream& stream);
147
168 [[nodiscard]] bool openFromFile(const std::filesystem::path& filename);
169
189 [[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
190
208 [[nodiscard]] bool openFromStream(InputStream& stream);
209
216 [[nodiscard]] const Info& getInfo() const;
217
240 [[nodiscard]] const Glyph& getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) const;
241
258 [[nodiscard]] bool hasGlyph(char32_t codePoint) const;
259
277 [[nodiscard]] float getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold = false) const;
278
290 [[nodiscard]] float getLineSpacing(unsigned int characterSize) const;
291
305 [[nodiscard]] float getUnderlinePosition(unsigned int characterSize) const;
306
319 [[nodiscard]] float getUnderlineThickness(unsigned int characterSize) const;
320
333 [[nodiscard]] const Texture& getTexture(unsigned int characterSize) const;
334
349 void setSmooth(bool smooth);
350
359 [[nodiscard]] bool isSmooth() const;
360
361private:
366 struct Row
367 {
368 Row(unsigned int rowTop, unsigned int rowHeight) : top(rowTop), height(rowHeight)
369 {
370 }
371
372 unsigned int width{};
373 unsigned int top;
374 unsigned int height;
375 };
376
378 // Types
380 using GlyphTable = std::unordered_map<std::uint64_t, Glyph>;
381
386 struct Page
387 {
388 explicit Page(bool smooth);
389
390 GlyphTable glyphs;
391 Texture texture;
392 unsigned int nextRow{3};
393 std::vector<Row> rows;
394 };
395
400 void cleanup();
401
410 Page& loadPage(unsigned int characterSize) const;
411
423 Glyph loadGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness) const;
424
434 IntRect findGlyphRect(Page& page, Vector2u size) const;
435
444 [[nodiscard]] bool setCurrentSize(unsigned int characterSize) const;
445
447 // Types
449 struct FontHandles;
450 using PageTable = std::unordered_map<unsigned int, Page>;
451
453 // Member data
455 std::shared_ptr<FontHandles> m_fontHandles;
456 bool m_isSmooth{true};
457 Info m_info;
458 mutable PageTable m_pages;
459 mutable std::vector<std::uint8_t> m_pixelBuffer;
460#ifdef SFML_SYSTEM_ANDROID
461 std::shared_ptr<priv::ResourceStream> m_stream;
462#endif
463};
464
465} // namespace sf
466
467
Glyph.hpp
Export.hpp
SFML_GRAPHICS_API
#define SFML_GRAPHICS_API
Definition Graphics/Export.hpp:42
Rect.hpp
Texture.hpp
Vector2.hpp
sf::Font::openFromMemory
bool openFromMemory(const void *data, std::size_t sizeInBytes)
Open the font from a file in memory.
sf::Font::getLineSpacing
float getLineSpacing(unsigned int characterSize) const
Get the line spacing.
sf::Font::getTexture
const Texture & getTexture(unsigned int characterSize) const
Retrieve the texture containing the loaded glyphs of a certain size.
sf::Font::Font
Font(InputStream &stream)
Construct the font from a custom stream.
sf::Font::getUnderlinePosition
float getUnderlinePosition(unsigned int characterSize) const
Get the position of the underline.
sf::Font::Font
Font(const std::filesystem::path &filename)
Construct the font from a file.
sf::Font::setSmooth
void setSmooth(bool smooth)
Enable or disable the smooth filter.
sf::Font::Font
Font(const void *data, std::size_t sizeInBytes)
Construct the font from a file in memory.
sf::Font::getInfo
const Info & getInfo() const
Get the font information.
sf::Font::getGlyph
const Glyph & getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) const
Retrieve a glyph of the font.
sf::Font::getKerning
float getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold=false) const
Get the kerning offset of two glyphs.
sf::Font::openFromStream
bool openFromStream(InputStream &stream)
Open the font from a custom stream.
sf::Font::getUnderlineThickness
float getUnderlineThickness(unsigned int characterSize) const
Get the thickness of the underline.
sf::Font::isSmooth
bool isSmooth() const
Tell whether the smooth filter is enabled or not.
sf::Font::Font
Font()=default
Default constructor.
sf::Font::openFromFile
bool openFromFile(const std::filesystem::path &filename)
Open the font from a file.
sf::Font::hasGlyph
bool hasGlyph(char32_t codePoint) const
Determine if this font has a glyph representing the requested code point.
sf::InputStream
Abstract class for custom file input streams.
Definition InputStream.hpp:46
sf::Texture
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56
sf
Definition AudioResource.hpp:36
sf::IntRect
Rect< int > IntRect
Definition Rect.hpp:146
sf::Font::Info
Holds various information about a font.
Definition Font.hpp:71
sf::Font::Info::family
std::string family
The font family.
Definition Font.hpp:72
sf::Glyph
Structure describing a glyph.
Definition Glyph.hpp:42