SFML - Simple and Fast Multimedia Library

SFML 3.1.0
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-2026 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 <string_view>
42#include <unordered_map>
43#include <vector>
44
45#include <cstddef>
46#include <cstdint>
47
48
49namespace sf
50{
51class InputStream;
52
57 class SFML_GRAPHICS_API Font
58{
59public:
64 struct Info
65 {
66 std::uint64_t id{};
67 std::string family;
68 bool hasKerning{};
69 bool hasVerticalMetrics{};
70 };
71
78 Font() = default;
79
100 explicit Font(const std::filesystem::path& filename);
101
121 Font(const void* data, std::size_t sizeInBytes);
122
143 explicit Font(InputStream& stream);
144
165 [[nodiscard]] bool openFromFile(const std::filesystem::path& filename);
166
186 [[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
187
205 [[nodiscard]] bool openFromStream(InputStream& stream);
206
213 [[nodiscard]] const Info& getInfo() const;
214
236 [[nodiscard]] const Glyph& getGlyphById(std::uint32_t id,
237 unsigned int characterSize,
238 bool bold,
239 float outlineThickness = 0) const;
240
263 [[nodiscard]] const Glyph& getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) const;
264
281 [[nodiscard]] bool hasGlyph(char32_t codePoint) const;
282
302 [[deprecated("Use the getKerning(char32_t, char32_t, unsigned int, bool) overload")]] [[nodiscard]] float getKerning(
303 std::uint32_t first,
304 std::uint32_t second,
305 unsigned int characterSize,
306 bool bold = false) const;
307
325 [[nodiscard]] float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold = false) const;
326
341 [[nodiscard]] float getAscent(unsigned int characterSize) const;
342
361 [[nodiscard]] float getDescent(unsigned int characterSize) const;
362
374 [[nodiscard]] float getLineSpacing(unsigned int characterSize) const;
375
389 [[nodiscard]] float getUnderlinePosition(unsigned int characterSize) const;
390
403 [[nodiscard]] float getUnderlineThickness(unsigned int characterSize) const;
404
417 [[nodiscard]] const Texture& getTexture(unsigned int characterSize) const;
418
433 void setSmooth(bool smooth);
434
443 [[nodiscard]] bool isSmooth() const;
444
445private:
446 friend class Text;
447
452 struct Row
453 {
454 Row(unsigned int rowTop, unsigned int rowHeight) : top(rowTop), height(rowHeight)
455 {
456 }
457
458 unsigned int width{};
459 unsigned int top;
460 unsigned int height;
461 };
462
464 // Types
466 using GlyphTable = std::unordered_map<std::uint64_t, Glyph>;
467
472 struct Page
473 {
474 explicit Page(bool smooth);
475
476 GlyphTable glyphs;
477 Texture texture;
478 unsigned int nextRow{3};
479 std::vector<Row> rows;
480 };
481
486 void cleanup();
487
492 [[nodiscard]] bool openFromStreamImpl(InputStream& stream, std::string_view type);
493
502 Page& loadPage(unsigned int characterSize) const;
503
515 Glyph loadGlyph(std::uint32_t id, unsigned int characterSize, bool bold, float outlineThickness) const;
516
526 IntRect findGlyphRect(Page& page, Vector2u size) const;
527
536 [[nodiscard]] bool setCurrentSize(unsigned int characterSize) const;
537
541 using FontHandle = void*;
542
554 [[nodiscard]] FontHandle getFontHandle() const;
555
557 // Types
559 struct FontHandles;
560 using PageTable = std::unordered_map<unsigned int, Page>;
561
563 // Member data
565 std::shared_ptr<FontHandles> m_fontHandles;
566 bool m_isSmooth{true};
567 Info m_info;
568 mutable PageTable m_pages;
569 mutable std::vector<std::uint8_t> m_pixelBuffer;
570 std::shared_ptr<InputStream> m_stream;
571};
572
573} // namespace sf
574
575
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::getAscent
float getAscent(unsigned int characterSize) const
Get the ascent.
sf::Font::getGlyphById
const Glyph & getGlyphById(std::uint32_t id, unsigned int characterSize, bool bold, float outlineThickness=0) const
Retrieve a glyph of the font by glyph ID.
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::getKerning
float getKerning(char32_t first, char32_t second, unsigned int characterSize, bool bold=false) const
Get the kerning offset of two glyphs.
sf::Font::Font
Font()=default
Default constructor.
sf::Font::Text
friend class Text
Definition Font.hpp:446
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::Font::getDescent
float getDescent(unsigned int characterSize) const
Get the descent.
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:65
sf::Font::Info::family
std::string family
The font family.
Definition Font.hpp:67
sf::Font::Info::hasKerning
bool hasKerning
Has kerning information.
Definition Font.hpp:68
sf::Font::Info::hasVerticalMetrics
bool hasVerticalMetrics
Has native vertical metrics.
Definition Font.hpp:69
sf::Glyph
Structure describing a glyph.
Definition Glyph.hpp:42