FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_ftFont.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_GRAPHIC_C_FONT_HPP_INCLUDED
18#define _FGE_GRAPHIC_C_FONT_HPP_INCLUDED
19
20/*
21 * Original from : https://github.com/SFML/SFML
22 * Copyright (C) 2007-2022 Laurent Gomila
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "FastEngine/fge_extern.hpp"
28#include "FastEngine/C_rect.hpp"
29#include "FastEngine/graphic/C_glyph.hpp"
30#include "FastEngine/graphic/C_surface.hpp"
31#include "FastEngine/vulkan/C_textureImage.hpp"
32#include <filesystem>
33#include <string>
34#include <unordered_map>
35#include <vector>
36
37namespace fge
38{
39
40using CharacterSize = uint16_t;
41
42class FGE_API FreeTypeFont
43{
44public:
45 struct Info
46 {
47 std::string family;
48 };
49
51 FreeTypeFont(FreeTypeFont const& r) = delete;
53
54 FreeTypeFont& operator=(FreeTypeFont const& r) = delete;
55
56 bool loadFromFile(std::filesystem::path const& filePath);
57 bool loadFromMemory(void const* data, std::size_t size);
58
59 Info const& getInfo() const;
60
61 Glyph const&
62 getGlyph(uint32_t codePoint, fge::CharacterSize characterSize, bool bold, float outlineThickness = 0) const;
63 bool hasGlyph(uint32_t codePoint) const;
64
65 float getKerning(uint32_t first, uint32_t second, fge::CharacterSize characterSize, bool bold = false) const;
66 float getLineSpacing(fge::CharacterSize characterSize) const;
67 float getUnderlinePosition(fge::CharacterSize characterSize) const;
68 float getUnderlineThickness(fge::CharacterSize characterSize) const;
69
70 fge::vulkan::TextureImage const& getTexture(fge::CharacterSize characterSize) const;
71
72 void setSmooth(bool smooth);
73 bool isSmooth() const;
74
75 [[nodiscard]] std::vector<long> getAvailableSize() const;
76
77private:
78 struct Row
79 {
80 Row(unsigned int rowTop, unsigned int rowHeight) :
81 _width(0),
82 _top(rowTop),
83 _height(rowHeight)
84 {}
85
86 unsigned int _width;
87 unsigned int _top;
88 unsigned int _height;
89 };
90
91 using GlyphTable = std::unordered_map<uint64_t, Glyph>;
92
93 struct Page
94 {
95 explicit Page(bool smooth);
96
97 GlyphTable _glyphs;
99 unsigned int _nextRow;
100 std::vector<Row> _rows;
101 };
102
103 using PageTable =
104 std::unordered_map<fge::CharacterSize, Page>;
105
106 void cleanup();
107
108 Page& loadPage(fge::CharacterSize characterSize) const;
109 Glyph loadGlyph(uint32_t codePoint, fge::CharacterSize characterSize, bool bold, float outlineThickness) const;
110
111 fge::RectInt findGlyphRect(Page& page, unsigned int width, unsigned int height) const;
112
113 bool setCurrentSize(fge::CharacterSize characterSize) const;
114
115 void* g_face;
116 void* g_streamRec;
117 void* g_stroker;
118 bool g_isSmooth;
119 Info g_info;
120 mutable PageTable g_pages;
121 mutable fge::Surface g_surfaceBuffer;
122};
123
124} // namespace fge
125
126#endif // _FGE_GRAPHIC_C_FONT_HPP_INCLUDED
Definition C_ftFont.hpp:43
Definition C_glyph.hpp:33
Abstraction of SDL_Surface.
Definition graphic/C_surface.hpp:55
Definition C_textureImage.hpp:34
Definition C_ftFont.hpp:46