MeVisLab Toolbox Reference
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 
24 class FTFont;
25 
27 
28 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 
54 public:
56  ~GL2DFont() override;
57 
58  // currently the maximum internal font number is hardwired
59  enum {
60  MAX_FONTS = 32
61  };
62 
63  //---------------------------------------------------------------------------
66  //---------------------------------------------------------------------------
67  bool init();
68 
69  //---------------------------------------------------------------------------
73  //---------------------------------------------------------------------------
74  bool init(const char* fontname);
75 
76  //---------------------------------------------------------------------------
81  //---------------------------------------------------------------------------
82  bool init(const char* fontname,
83  const int numFonts, const int* sizes);
84 
85  //---------------------------------------------------------------------------
99  //---------------------------------------------------------------------------
100  void drawString(const float dx, const float dy,
101  const float fontSize, const float color[4],
102  const char* string, const size_t length = 0,
103  const bool overlay=false, const bool shadow=true,
104  const float* shadowColor=nullptr);
105 
106  //---------------------------------------------------------------------------
109  //---------------------------------------------------------------------------
110  int getStringWidth(const float fontSize,
111  const char* string, const size_t length) const;
112 
113  //---------------------------------------------------------------------------
116  //---------------------------------------------------------------------------
117  int getFontHeight(const float fontSize) const;
118 
119  //---------------------------------------------------------------------------
122  //---------------------------------------------------------------------------
123  int getFontAscender(const float fontSize) const;
124 
125  //---------------------------------------------------------------------------
128  //---------------------------------------------------------------------------
129  int getFontDescender(const float fontSize) const;
130 
131  //---------------------------------------------------------------------------
133  //---------------------------------------------------------------------------
134  bool isInitialized() const { return _init; };
135 
136  //---------------------------------------------------------------------------
138  //---------------------------------------------------------------------------
139  bool isUnicodeRenderingPossible() const { return _unicodeRendering; };
140 
141  //---------------------------------------------------------------------------
145  //---------------------------------------------------------------------------
147 
148  //---------------------------------------------------------------------------
150  //---------------------------------------------------------------------------
151  void contextDestroyed() override;
152 
153  //---------------------------------------------------------------------------
155  //---------------------------------------------------------------------------
157 
158  //---------------------------------------------------------------------------
163  //---------------------------------------------------------------------------
164  static std::string getDefaultSystemFontName();
165 
166 protected:
167  //---------------------------------------------------------------------------
169  //---------------------------------------------------------------------------
170  int getFontIndex(const float fontSize) const;
171 
172  //---------------------------------------------------------------------------
174  //---------------------------------------------------------------------------
175  void cleanup();
176 
177  //---------------------------------------------------------------------------
179  //---------------------------------------------------------------------------
180  bool reloadFonts();
181 
182  //---------------------------------------------------------------------------
184  //---------------------------------------------------------------------------
185  void preFontDraw(const bool overlay);
186 
187  //---------------------------------------------------------------------------
189  //---------------------------------------------------------------------------
190  void doFontDraw(const float dx, const float dy,
191  const float fontSize, const float color[4],
192  const bool shadow, const char* asciiString, const wchar_t* optionalUnicodeString,
193  const float* shadowColor);
194 
195  //---------------------------------------------------------------------------
197  //---------------------------------------------------------------------------
198  void postFontDraw(const bool overlay);
199 
200  //---------------------------------------------------------------------------
202  //---------------------------------------------------------------------------
203  bool checkUTF8StringForUnicodeChars(const char* string, size_t length = 0) const;
204 
205  //---------------------------------------------------------------------------
209  //---------------------------------------------------------------------------
210  const char* copyAndConvertString(const char* string, size_t length, const wchar_t** optionalUnicodeString) const;
211 
212  //---------------------------------------------------------------------------
215  //---------------------------------------------------------------------------
216  std::string findFontFile(const char* fontName);
217 
218  //---------------------------------------------------------------------------
220  //---------------------------------------------------------------------------
221  std::string findFontFileWithFontconfig(const char* fontName);
222 
223 private:
225  std::string _fontfile;
226 
228  std::string _fallback;
229 
231  int _numFonts;
232 
234  FTFont* _font[MAX_FONTS];
235 
237  float _fontSize[MAX_FONTS];
238 
240  bool _init;
241 
243  bool _unicodeRendering;
244 
246  static GL2DFont* _defaultSystemFont;
247 };
248 
250 #endif
A bridge to the ftgl library that manages font rendering with texture fonts.
Definition: mlGL2DFont.h:52
void doFontDraw(const float dx, const float dy, const float fontSize, const float color[4], const bool shadow, const char *asciiString, const wchar_t *optionalUnicodeString, const float *shadowColor)
Render the fonts.
bool checkUTF8StringForUnicodeChars(const char *string, size_t length=0) const
Check if the string contains unicode chars.
static GL2DFont * getDefaultSystemFont()
get the default system font (already initialized, a valid GL context is only required for the draw me...
int getFontDescender(const float fontSize) const
Get the descender of the font in pixels (positive distance from the baseline).
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...
int getFontAscender(const float fontSize) const
Get the ascender of the font in pixels.
bool reloadFonts()
Free old fonts and reload them.
void contextDestroyed() override
forget the resource, the context was destroyed (called by the GLResourceManager)
bool isInitialized() const
Returns true if font loaded, otherwise false.
Definition: mlGL2DFont.h:134
void postFontDraw(const bool overlay)
Finalize the font rendering.
bool init()
Init with the default system font.
void cleanup()
Cleanup the fonts.
std::string findFontFileWithFontconfig(const char *fontName)
Searches the font by using fontconfig (Linux only)
bool debugFontRenderingEnabled()
check if debug font rendering is enabled
void drawString(const float dx, const float dy, const float fontSize, const float color[4], const char *string, const size_t length=0, const bool overlay=false, const bool shadow=true, const float *shadowColor=nullptr)
This function draws a simple string with no linefeeds.
int getFontIndex(const float fontSize) const
Get the font idx that should be used for the given size of the font.
static std::string getDefaultSystemFontName()
returns the default font name for proportional fonts (this can be overwritten via the "GL2DFont_Defau...
bool init(const char *fontname, const int numFonts, const int *sizes)
Init the given font (either just a font name without .ttf or an absolute path) with the given number ...
void preFontDraw(const bool overlay)
Initialize font rendering.
bool isUnicodeRenderingPossible() const
Returns true if unicode rendering is possible, otherwise false.
Definition: mlGL2DFont.h:139
int getStringWidth(const float fontSize, const char *string, const size_t length) const
Get width of the given simple string.
std::string findFontFile(const char *fontName)
Searches for the file name of the given font.
bool init(const char *fontname)
Init the given font (either just a font name without .ttf or an absolute path).
int getFontHeight(const float fontSize) const
Get the height of a font (ascender+descender) in pixels.
~GL2DFont() override
Collection of OpenGL related resource classes, such as textures, buffers, fragment programs etc.
Definition: mlGLResources.h:27
#define ML_OPENGL_END_NAMESPACE
Closes the namespace for all ML stuff after the usage of ML_UTILS_START_NAMESPACE.
#define ML_OPENGL_START_NAMESPACE
Macro to put all following stuff into the namespace ML_NAMESPACE to avoid collisions with symbols of ...
#define MLOPENGL_EXPORT