MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
27
28//-----------------------------------------------------------------------------
51//-----------------------------------------------------------------------------
53{
54
55public:
57 ~GL2DFont() override;
58
59 // Currently, the maximum internal font number is hardwired.
60 enum {
61 MAX_FONTS = 32
62 };
63
64 //---------------------------------------------------------------------------
67 //---------------------------------------------------------------------------
68 bool init();
69
70 //---------------------------------------------------------------------------
74 //---------------------------------------------------------------------------
75 bool init(const char* fontname);
76
77 //---------------------------------------------------------------------------
82 //---------------------------------------------------------------------------
83 bool init(const char* fontname, int numFonts, const int* sizes);
84
85 //---------------------------------------------------------------------------
103 //---------------------------------------------------------------------------
104 void drawString(float dx, float dy, float fontSize, const float color[4], const char* string, size_t length = 0,
105 bool overlay = false, bool shadow = true, const float* shadowColor = nullptr);
106
107 //---------------------------------------------------------------------------
110 //---------------------------------------------------------------------------
111 int getStringWidth(float fontSize, const char* string, int length = -1) const;
112
113 //---------------------------------------------------------------------------
116 //---------------------------------------------------------------------------
117 int getFontHeight(float fontSize) const;
118
119 //---------------------------------------------------------------------------
122 //---------------------------------------------------------------------------
123 int getFontAscender(float fontSize) const;
124
125 //---------------------------------------------------------------------------
128 //---------------------------------------------------------------------------
129 int getFontDescender(float fontSize) const;
130
131 //---------------------------------------------------------------------------
133 //---------------------------------------------------------------------------
134 bool isInitialized() const
135 {
136 return _init;
137 };
138
139 //---------------------------------------------------------------------------
141 //---------------------------------------------------------------------------
143 {
144 return _unicodeRendering;
145 };
146
147 //---------------------------------------------------------------------------
151 //---------------------------------------------------------------------------
153
154 //---------------------------------------------------------------------------
156 //---------------------------------------------------------------------------
157 void contextDestroyed() override;
158
159 //---------------------------------------------------------------------------
161 //---------------------------------------------------------------------------
163
164 //---------------------------------------------------------------------------
169 //---------------------------------------------------------------------------
170 static std::string getDefaultSystemFontName();
171
172protected:
173 //---------------------------------------------------------------------------
175 //---------------------------------------------------------------------------
176 int getFontIndex(float fontSize) const;
177
178 //---------------------------------------------------------------------------
180 //---------------------------------------------------------------------------
181 void cleanup();
182
183 //---------------------------------------------------------------------------
185 //---------------------------------------------------------------------------
187
188 //---------------------------------------------------------------------------
190 //---------------------------------------------------------------------------
192
193 //---------------------------------------------------------------------------
195 //---------------------------------------------------------------------------
196 void doFontDraw(float dx, float dy, float fontSize, const float color[4], bool shadow, const char* asciiString,
197 const wchar_t* optionalUnicodeString, const float* shadowColor);
198
199 //---------------------------------------------------------------------------
201 //---------------------------------------------------------------------------
202 void postFontDraw(const bool overlay);
203
204 //---------------------------------------------------------------------------
206 //---------------------------------------------------------------------------
207 bool checkUTF8StringForUnicodeChars(const char* string, size_t length = 0) const;
208
209 //---------------------------------------------------------------------------
213 //---------------------------------------------------------------------------
214 const char* copyAndConvertString(const char* string, size_t length, const wchar_t** optionalUnicodeString) const;
215
216 //---------------------------------------------------------------------------
219 //---------------------------------------------------------------------------
220 std::string findFontFile(const char* fontName);
221
222 //---------------------------------------------------------------------------
224 //---------------------------------------------------------------------------
225 std::string findFontFileWithFontconfig(const char* fontName);
226
227private:
229 std::string _fontfile;
230
232 std::string _fallback;
233
235 int _numFonts;
236
238 FTFont* _font[MAX_FONTS];
239
241 float _fontSize[MAX_FONTS];
242
244 bool _init;
245
247 bool _unicodeRendering;
248
250 static GL2DFont* _defaultSystemFont;
251};
252
254#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.
static GL2DFont * getDefaultSystemFont()
Returns the default system font.
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.
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 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,...
bool reloadFonts()
Frees old fonts and reloads them.
void contextDestroyed() override
Forgets the resource, the context was destroyed (called by the GLResourceManager).
bool isInitialized() const
Returns true if font loaded; otherwise, it returns false.
Definition mlGL2DFont.h:134
void postFontDraw(const bool overlay)
Finalizes the font rendering.
int getFontAscender(float fontSize) const
Returns the ascender of the font in pixels.
int getFontDescender(float fontSize) const
Returns the descender of the font in pixels (i.e., the positive distance from the baseline).
bool init()
Initializes with the default system font.
void cleanup()
Cleans up the fonts.
std::string findFontFileWithFontconfig(const char *fontName)
Searches the font by using fontconfig (Linux only)
bool debugFontRenderingEnabled()
Checks whether debug font rendering is enabled.
int getFontIndex(float fontSize) const
Returns the font index that should be used for the given size of the font.
int getFontHeight(float fontSize) const
Returns the height of a font (ascender+descender) in pixels.
static std::string getDefaultSystemFontName()
Returns the default font name for proportional fonts.
void preFontDraw(bool overlay)
Initializes font rendering.
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.
bool isUnicodeRenderingPossible() const
Returns true if Unicode rendering is possible; otherwise, it returns false.
Definition mlGL2DFont.h:142
std::string findFontFile(const char *fontName)
Searches for the file name of the given font.
int getStringWidth(float fontSize, const char *string, int length=-1) const
Returns the width of the given simple string.
bool init(const char *fontname)
Initializes the given font, which is either just a font name without .ttf or an absolute path.
~GL2DFont() override
Collection of OpenGL related resource classes, such as textures, buffers, fragment programs etc.
#define ML_OPENGL_END_NAMESPACE
Closes the namespace for all ML code after the usage of ML_UTILS_START_NAMESPACE.
#define ML_OPENGL_START_NAMESPACE
Macro to put all following code into the namespace ML_NAMESPACE to avoid collisions with symbols of o...
#define MLOPENGL_EXPORT
Target mlrange_cast(Source arg)
Generic version of checked ML casts.