GlistEngine
Loading...
Searching...
No Matches
gFont.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Nitra Games Ltd.
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 ENGINE_GRAPHICS_GFONT_H_
18#define ENGINE_GRAPHICS_GFONT_H_
19
20#include "gNode.h"
21#include "gEventHook.h"
22#include <map>
23#include <vector>
24#ifdef EMSCRIPTEN
25#include "ft2build.h"
26#include FT_FREETYPE_H
27#else
28#include "freetype2/ft2build.h"
29#endif
30#ifdef ANDROID
31#include "freetype2/freetype/freetype.h"
32#include <android/asset_manager.h>
33#endif
34#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
35#include "freetype2/freetype/freetype.h"
36#endif
37#if defined(WIN32) || defined(LINUX) || TARGET_OS_OSX
38#include FT_FREETYPE_H
39#endif
40
41
42#include "gMesh.h"
43#include "gTexture.h"
44#include <unordered_map>
45
46
47class gFont : public gNode, public gEventHook {
48public:
50 enum class TextRenderMode {GLYPH, ATLAS};
51
53 virtual ~gFont();
54
73 bool load(const std::string& fullPath, int size, bool isAntialiased = true, int dpi = 96);
74
93 bool loadFont(const std::string& fontPath, int size, bool isAntialiased = true, int dpi = 96);
94
102 void drawText(const std::string& text, float x, float y);
103
110
115 void setAtlasPageLimit(int maxPageCount);
116 int getAtlasPageLimit() const;
117 int getAtlasPageCount() const;
118
119
125 void drawTextVerticallyFlipped(const std::string& text, float x, float y);
126
132 void drawTextHorizontallyFlipped(const std::string& text, float x, float y);
133
143 float getStringWidth(const std::string& text);
144
145
155 float getStringHeight(const std::string& text);
156
162 float getLineHeight() const;
163
168 float getAscender() const;
169 float getDescender() const;
170
176 const std::string& getPath() const;
177
183 int getSize() const;
184
190 bool isLoaded() const;
191
197 bool isAntialised() const;
198
206 int getDpi() const;
207 /*
208 * Wraps text into multiple lines based on font width.
209 *
210 * @param text The input text to be wrapped
211 * @param maxWidth The maximum visual width of a line
212 *
213 * @return A list of lines that fit within the given width
214 */
215 std::vector<std::string> wrapSentenceByWidth(const std::string& text, float maxWidth, TextAlign align = TextAlign::LEFT);
216
217 void onEvent(gEvent& event) override;
218
219 void getVisualBoundsX(const std::string& text, float& xmin, float& xmax);
220
221
222private:
223 void reloadFont();
224 bool buildAtlas();
225
226 bool isloaded = false;
227 std::string fullpath;
228 bool isantialiased = false;
229 int dpi = 0;
230 float fontsize = 0.0f;
231 int characternumlimit = 0;
232 int border = 3;
233 bool iskerning = false;
234
235 FT_Library ftlib = nullptr;
236 FT_Face fontface = nullptr;
237
238 float lineheight = 0.0f;
239 float ascender = 0.0f;
240 float descender = 0.0f;
241 float letterspacing = 1.0f;
242 float spacesize = 1.0f;
243 float scale = 1.0f;
244
245 void loadChar(int charCode);
246
247 struct CharProperties {
248 float height = 0.0f, width = 0.0f;
249 float topmargin = 0.0f, leftmargin = 0.0f;
250 float advance = 0.0f;
251 float texturewidth = 0.0f, textureheight = 0.0f;
252 float dxleft = 0.0f, dxright = 0.0f, dytop = 0.0f, dybottom = 0.0f;
253 float atlasu0 = 0.0f, atlasv0 = 0.0f, atlasu1 = 0.0f, atlasv1 = 0.0f;
254 int atlaspage = -1;
255 bool atlasattempted = false;
256 bool inatlas = false;
257 };
258
259 struct AtlasPage {
260 gTexture* texture = nullptr;
261 int cursorx = 0;
262 int cursory = 0;
263 int rowheight = 0;
264 bool dirty = false;
265 std::vector<unsigned char> pixels;
266 };
267
268 std::unordered_map<int, CharProperties> charproperties;
269 std::unordered_map<int, gTexture*> chartextures;
270 std::vector<AtlasPage> atlaspages;
271 static constexpr int atlaswidth = 1024;
272 static constexpr int atlasheight = 1024;
273 int atlasmaxpages = 2;
274 bool atlasbuilding = false;
275 std::vector<std::vector<float>> batchvertices;
276 TextRenderMode textrendermode = TextRenderMode::GLYPH;
277
278 float getKerning(int c, int prevC) const;
279 void resizeVectors(int num);
280 bool insertData(const unsigned char* srcData, int srcWidth, int srcHeight, int componentNum,
281 unsigned char* dstData, int dstWidth, int dstHeight, int dstComponentNum,
282 size_t dstFirstX, size_t dstFirstY) const;
283 std::wstring s2ws(const std::string& s) const;
284 float roundIfRequired(float val);
285};
286
287#endif /* ENGINE_GRAPHICS_GFONT_H_ */
Definition gEvent.h:32
void getVisualBoundsX(const std::string &text, float &xmin, float &xmax)
void onEvent(gEvent &event) override
bool loadFont(const std::string &fontPath, int size, bool isAntialiased=true, int dpi=96)
virtual ~gFont()
float getLineHeight() const
bool isLoaded() const
bool isAntialised() const
float getStringWidth(const std::string &text)
float getAscender() const
int getAtlasPageLimit() const
std::vector< std::string > wrapSentenceByWidth(const std::string &text, float maxWidth, TextAlign align=TextAlign::LEFT)
TextRenderMode getTextRenderMode() const
void setAtlasPageLimit(int maxPageCount)
bool load(const std::string &fullPath, int size, bool isAntialiased=true, int dpi=96)
TextAlign
Definition gFont.h:49
@ JUSTIFY
Definition gFont.h:49
@ RIGHT
Definition gFont.h:49
@ LEFT
Definition gFont.h:49
@ CENTER
Definition gFont.h:49
int getSize() const
void drawTextVerticallyFlipped(const std::string &text, float x, float y)
Draws the given text vertically flipped at the specified position.
int getAtlasPageCount() const
TextRenderMode
Definition gFont.h:50
@ ATLAS
Definition gFont.h:50
@ GLYPH
Definition gFont.h:50
float getStringHeight(const std::string &text)
int getDpi() const
const std::string & getPath() const
float getDescender() const
void setTextRenderMode(TextRenderMode renderMode)
void drawText(const std::string &text, float x, float y)
void drawTextHorizontallyFlipped(const std::string &text, float x, float y)
Draws the given text horizontally flipped at the specified location.
Definition gTexture.h:17