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
52 virtual ~gFont();
53
72 bool load(const std::string& fullPath, int size, bool isAntialiased = true, int dpi = 96);
73
92 bool loadFont(const std::string& fontPath, int size, bool isAntialiased = true, int dpi = 96);
93
101 void drawText(const std::string& text, float x, float y);
102
103
109 void drawTextVerticallyFlipped(const std::string& text, float x, float y);
110
116 void drawTextHorizontallyFlipped(const std::string& text, float x, float y);
117
127 float getStringWidth(const std::string& text);
128
129
139 float getStringHeight(const std::string& text);
140
146 float getLineHeight() const;
147
153 const std::string& getPath() const;
154
160 int getSize() const;
161
167 bool isLoaded() const;
168
174 bool isAntialised() const;
175
183 int getDpi() const;
184 /*
185 * Wraps text into multiple lines based on font width.
186 *
187 * @param text The input text to be wrapped
188 * @param maxWidth The maximum visual width of a line
189 *
190 * @return A list of lines that fit within the given width
191 */
192 std::vector<std::string> wrapSentenceByWidth(const std::string& text, float maxWidth, TextAlign align = TextAlign::LEFT);
193
194 void onEvent(gEvent& event) override;
195
196private:
197 void reloadFont();
198
199 bool isloaded = false;
200 std::string fullpath;
201 bool isantialiased = false;
202 int dpi = 0;
203 float fontsize = 0.0f;
204 int characternumlimit = 0;
205 int border = 3;
206 bool iskerning = false;
207
208 FT_Library ftlib = nullptr;
209 FT_Face fontface = nullptr;
210
211 float lineheight = 0.0f;
212 float letterspacing = 1.0f;
213 float spacesize = 1.0f;
214 float scale = 1.0f;
215
216 void loadChar(int charCode);
217
218 struct CharProperties {
219 float height = 0.0f, width = 0.0f;
220 float topmargin = 0.0f, leftmargin = 0.0f;
221 float advance = 0.0f;
222 float texturewidth = 0.0f, textureheight = 0.0f;
223 float dxleft = 0.0f, dxright = 0.0f, dytop = 0.0f, dybottom = 0.0f;
224 };
225
226 std::unordered_map<int, CharProperties> charproperties;
227 std::unordered_map<int, gTexture*> chartextures;
228
229 float getKerning(int c, int prevC) const;
230 void resizeVectors(int num);
231 bool insertData(const unsigned char* srcData, int srcWidth, int srcHeight, int componentNum,
232 unsigned char* dstData, int dstWidth, int dstHeight, int dstComponentNum,
233 size_t dstFirstX, size_t dstFirstY) const;
234 std::wstring s2ws(const std::string& s) const;
235 float roundIfRequired(float val);
236};
237
238#endif /* ENGINE_GRAPHICS_GFONT_H_ */
Definition gEventHook.h:14
Definition gEvent.h:32
Definition gFont.h:47
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)
std::vector< std::string > wrapSentenceByWidth(const std::string &text, float maxWidth, TextAlign align=TextAlign::LEFT)
bool load(const std::string &fullPath, int size, bool isAntialiased=true, int dpi=96)
TextAlign
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.
float getStringHeight(const std::string &text)
int getDpi() const
const std::string & getPath() const
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 gNode.h:15