SFML 3.0.1
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-2025 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
49 namespace sf
50{
51 class InputStream;
52
57 class SFML_GRAPHICS_API Font
58{
59 public:
64 struct Info
65 {
66 std::string family;
67 };
68
75 Font() = default;
76
97 explicit Font(const std::filesystem::path& filename);
98
118 Font(const void* data, std::size_t sizeInBytes);
119
140 explicit Font(InputStream& stream);
141
162 [[nodiscard]] bool openFromFile(const std::filesystem::path& filename);
163
183 [[nodiscard]] bool openFromMemory(const void* data, std::size_t sizeInBytes);
184
202 [[nodiscard]] bool openFromStream(InputStream& stream);
203
210 [[nodiscard]] const Info& getInfo() const;
211
234 [[nodiscard]] const Glyph& getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness = 0) const;
235
252 [[nodiscard]] bool hasGlyph(char32_t codePoint) const;
253
271 [[nodiscard]] float getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold = false) const;
272
284 [[nodiscard]] float getLineSpacing(unsigned int characterSize) const;
285
299 [[nodiscard]] float getUnderlinePosition(unsigned int characterSize) const;
300
313 [[nodiscard]] float getUnderlineThickness(unsigned int characterSize) const;
314
327 [[nodiscard]] const Texture& getTexture(unsigned int characterSize) const;
328
343 void setSmooth(bool smooth);
344
353 [[nodiscard]] bool isSmooth() const;
354
355 private:
360 struct Row
361 {
362 Row(unsigned int rowTop, unsigned int rowHeight) : top(rowTop), height(rowHeight)
363 {
364 }
365
366 unsigned int width{};
367 unsigned int top;
368 unsigned int height;
369 };
370
372 // Types
374 using GlyphTable = std::unordered_map<std::uint64_t, Glyph>;
375
380 struct Page
381 {
382 explicit Page(bool smooth);
383
384 GlyphTable glyphs;
385 Texture texture;
386 unsigned int nextRow{3};
387 std::vector<Row> rows;
388 };
389
394 void cleanup();
395
400 [[nodiscard]] bool openFromStreamImpl(InputStream& stream, std::string_view type);
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 std::shared_ptr<InputStream> m_stream;
461};
462
463} // namespace sf
464
465
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:65
sf::Font::Info::family
std::string family
The font family.
Definition Font.hpp:66
sf::Glyph
Structure describing a glyph.
Definition Glyph.hpp:42