MeVisLab Resolution Independence API
mlGL2DFont.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_GL_2D_FONT_H
14#define ML_GL_2D_FONT_H
15
16
18
19#include "mlOpenGLSystem.h"
20
21#include "mlGLResources.h"
22#include <string>
23
24class FTFont;
25
26ML_OPENGL_START_NAMESPACE
27
28//-----------------------------------------------------------------------------
51//-----------------------------------------------------------------------------
53{
54
55public:
57 ~GL2DFont() override;
58
59 // Currently, the maximum internal font number is hardwired.
60 enum { MAX_FONTS = 32 };
61
62 //---------------------------------------------------------------------------
65 //---------------------------------------------------------------------------
66 bool init();
67
68 //---------------------------------------------------------------------------
72 //---------------------------------------------------------------------------
73 bool init(const char* fontname);
74
75 //---------------------------------------------------------------------------
80 //---------------------------------------------------------------------------
81 bool init(const char* fontname, int numFonts, const int* sizes);
82
83 //---------------------------------------------------------------------------
101 //---------------------------------------------------------------------------
102 void drawString(float dx, float dy, float fontSize, const float color[4], const char* string, size_t length = 0,
103 bool overlay = false, bool shadow = true,
104 const float* shadowColor = nullptr);
105
106 //---------------------------------------------------------------------------
109 //---------------------------------------------------------------------------
110 int getStringWidth(float fontSize, const char* string, int length = -1) const;
111
112 //---------------------------------------------------------------------------
115 //---------------------------------------------------------------------------
116 int getFontHeight(float fontSize) const;
117
118 //---------------------------------------------------------------------------
121 //---------------------------------------------------------------------------
122 int getFontAscender(float fontSize) const;
123
124 //---------------------------------------------------------------------------
127 //---------------------------------------------------------------------------
128 int getFontDescender(float fontSize) const;
129
130 //---------------------------------------------------------------------------
132 //---------------------------------------------------------------------------
133 bool isInitialized() const
134 {
135 return _init;
136 };
137
138 //---------------------------------------------------------------------------
140 //---------------------------------------------------------------------------
142 {
143 return _unicodeRendering;
144 };
145
146 //---------------------------------------------------------------------------
150 //---------------------------------------------------------------------------
152
153 //---------------------------------------------------------------------------
155 //---------------------------------------------------------------------------
156 void contextDestroyed() override;
157
158 //---------------------------------------------------------------------------
160 //---------------------------------------------------------------------------
162
163 //---------------------------------------------------------------------------
168 //---------------------------------------------------------------------------
169 static std::string getDefaultSystemFontName();
170
171protected:
172 //---------------------------------------------------------------------------
174 //---------------------------------------------------------------------------
175 int getFontIndex(float fontSize) const;
176
177 //---------------------------------------------------------------------------
179 //---------------------------------------------------------------------------
180 void cleanup();
181
182 //---------------------------------------------------------------------------
184 //---------------------------------------------------------------------------
186
187 //---------------------------------------------------------------------------
189 //---------------------------------------------------------------------------
190 void preFontDraw(bool overlay);
191
192 //---------------------------------------------------------------------------
194 //---------------------------------------------------------------------------
195 void doFontDraw(float dx, float dy, float fontSize, const float color[4], bool shadow,
196 const char* asciiString, const wchar_t* optionalUnicodeString, const float* shadowColor);
197
198 //---------------------------------------------------------------------------
200 //---------------------------------------------------------------------------
201 void postFontDraw(const bool overlay);
202
203 //---------------------------------------------------------------------------
205 //---------------------------------------------------------------------------
206 bool checkUTF8StringForUnicodeChars(const char* string, size_t length = 0) const;
207
208 //---------------------------------------------------------------------------
212 //---------------------------------------------------------------------------
213 const char* copyAndConvertString(const char* string, size_t length, const wchar_t** optionalUnicodeString) const;
214
215 //---------------------------------------------------------------------------
218 //---------------------------------------------------------------------------
219 std::string findFontFile(const char* fontName);
220
221 //---------------------------------------------------------------------------
223 //---------------------------------------------------------------------------
224 std::string findFontFileWithFontconfig(const char* fontName);
225
226private:
228 std::string _fontfile;
229
231 std::string _fallback;
232
234 int _numFonts;
235
237 FTFont* _font[MAX_FONTS];
238
240 float _fontSize[MAX_FONTS];
241
243 bool _init;
244
246 bool _unicodeRendering;
247
249 static GL2DFont* _defaultSystemFont;
250};
251
252ML_OPENGL_END_NAMESPACE
253#endif
A bridge to the FTGL library that manages font rendering with texture fonts.
Definition mlGL2DFont.h:53
bool checkUTF8StringForUnicodeChars(const char *string, size_t length=0) const
Checks whether the string contains unicode chars.
bool isUnicodeRenderingPossible() const
Returns true if Unicode rendering is possible; otherwise, it returns false.
Definition mlGL2DFont.h:141
void drawString(float dx, float dy, float fontSize, const float color[4], const char *string, size_t length=0, bool overlay=false, bool shadow=true, const float *shadowColor=nullptr)
This function draws a simple string with no line feeds.
void doFontDraw(float dx, float dy, float fontSize, const float color[4], bool shadow, const char *asciiString, const wchar_t *optionalUnicodeString, const float *shadowColor)
Renders the fonts.
int getFontAscender(float fontSize) const
Returns the ascender of the font in pixels.
void postFontDraw(const bool overlay)
Finalizes the font rendering.
static std::string getDefaultSystemFontName()
Returns the default font name for proportional fonts.
bool reloadFonts()
Frees old fonts and reloads them.
std::string findFontFile(const char *fontName)
Searches for the file name of the given font.
bool init(const char *fontname)
Initializes the given font, which is either just a font name without .ttf or an absolute path.
void contextDestroyed() override
Forgets the resource, the context was destroyed (called by the GLResourceManager).
static GL2DFont * getDefaultSystemFont()
Returns the default system font.
const char * copyAndConvertString(const char *string, size_t length, const wchar_t **optionalUnicodeString) const
Internal method that copies the string to the internal buffer and generates a Unicode string if it is...
bool debugFontRenderingEnabled()
Checks whether debug font rendering is enabled.
std::string findFontFileWithFontconfig(const char *fontName)
Searches the font by using fontconfig (Linux only)
bool init(const char *fontname, int numFonts, const int *sizes)
Initializes the given font, which is either just a font name without .ttf or an absolute path,...
int getStringWidth(float fontSize, const char *string, int length=-1) const
Returns the width of the given simple string.
int getFontDescender(float fontSize) const
Returns the descender of the font in pixels (i.e., the positive distance from the baseline).
int getFontHeight(float fontSize) const
Returns the height of a font (ascender+descender) in pixels.
bool init()
Initializes with the default system font.
void preFontDraw(bool overlay)
Initializes font rendering.
bool isInitialized() const
Returns true if font loaded; otherwise, it returns false.
Definition mlGL2DFont.h:133
void cleanup()
Cleans up the fonts.
~GL2DFont() override
int getFontIndex(float fontSize) const
Returns the font index that should be used for the given size of the font.
Abstract base class for GLResources.
#define MLOPENGL_EXPORT
Macro to put all following code into the namespace ML_NAMESPACE to avoid collisions with symbols of o...