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(std::uint32_t codePoint,
241 unsigned int characterSize,
242 bool bold,
243 float outlineThickness = 0) const;
244
261 [[nodiscard]] bool hasGlyph(std::uint32_t codePoint) const;
262
280 [[nodiscard]] float getKerning(std::uint32_t first, std::uint32_t second, unsigned int characterSize, bool bold = false) const;
281
293 [[nodiscard]] float getLineSpacing(unsigned int characterSize) const;
294
308 [[nodiscard]] float getUnderlinePosition(unsigned int characterSize) const;
309
322 [[nodiscard]] float getUnderlineThickness(unsigned int characterSize) const;
323
336 [[nodiscard]] const Texture& getTexture(unsigned int characterSize) const;
337
352 void setSmooth(bool smooth);
353
362 [[nodiscard]] bool isSmooth() const;
363
364private:
369 struct Row
370 {
371 Row(unsigned int rowTop, unsigned int rowHeight) : top(rowTop), height(rowHeight)
372 {
373 }
374
375 unsigned int width{};
376 unsigned int top;
377 unsigned int height;
378 };
379
381 // Types
383 using GlyphTable = std::unordered_map<std::uint64_t, Glyph>;
384
389 struct Page
390 {
391 explicit Page(bool smooth);
392
393 GlyphTable glyphs;
394 Texture texture;
395 unsigned int nextRow{3};
396 std::vector<Row> rows;
397 };
398
403 void cleanup();
404
413 Page& loadPage(unsigned int characterSize) const;
414
426 Glyph loadGlyph(std::uint32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness) const;
427
437 IntRect findGlyphRect(Page& page, Vector2u size) const;
438
447 [[nodiscard]] bool setCurrentSize(unsigned int characterSize) const;
448
450 // Types
452 struct FontHandles;
453 using PageTable = std::unordered_map<unsigned int, Page>;
454
456 // Member data
458 std::shared_ptr<FontHandles> m_fontHandles;
459 bool m_isSmooth{true};
460 Info m_info;
461 mutable PageTable m_pages;
462 mutable std::vector<std::uint8_t> m_pixelBuffer;
463#ifdef SFML_SYSTEM_ANDROID
464 std::shared_ptr<priv::ResourceStream> m_stream;
465#endif
466};
467
468} // namespace sf
469
470
#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.
bool hasGlyph(std::uint32_t codePoint) const
Determine if this font has a glyph representing the requested code point.
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.
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.
const Glyph & getGlyph(std::uint32_t codePoint, unsigned int characterSize, bool bold, float outlineThickness=0) const
Retrieve a glyph of the font.
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.
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