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
31
35
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
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
#define SFML_GRAPHICS_API
Class for loading and manipulating character fonts.
Definition Font.hpp:64
bool openFromMemory(const void *data, std::size_t sizeInBytes)
Open the font from a file in memory.
float getLineSpacing(unsigned int characterSize) const
Get the line spacing.
const Texture & getTexture(unsigned int characterSize) const
Retrieve the texture containing the loaded glyphs of a certain size.
Font(InputStream &stream)
Construct the font from a custom stream.
float getUnderlinePosition(unsigned int characterSize) const
Get the position of the underline.
Font(const std::filesystem::path &filename)
Construct the font from a file.
void setSmooth(bool smooth)
Enable or disable the smooth filter.
Font(const void *data, std::size_t sizeInBytes)
Construct the font from a file in memory.
const Info & getInfo() const
Get the font information.
const Glyph & getGlyph(char32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) const
Retrieve a glyph of the font.
float getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold=false) const
Get the kerning offset of two glyphs.
bool openFromStream(InputStream &stream)
Open the font from a custom stream.
float getUnderlineThickness(unsigned int characterSize) const
Get the thickness of the underline.
bool isSmooth() const
Tell whether the smooth filter is enabled or not.
Font()=default
Default constructor.
bool openFromFile(const std::filesystem::path &filename)
Open the font from a file.
bool hasGlyph(char32_t codePoint) const
Determine if this font has a glyph representing the requested code point.
Abstract class for custom file input streams.
Image living on the graphics card that can be used for drawing.
Definition Texture.hpp:56
Rect< int > IntRect
Definition Rect.hpp:146
Holds various information about a font.
Definition Font.hpp:71
std::string family
The font family.
Definition Font.hpp:72
Structure describing a glyph.
Definition Glyph.hpp:42