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 
55 public:
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 
171 protected:
172  //---------------------------------------------------------------------------
174  //---------------------------------------------------------------------------
175  int getFontIndex(float fontSize) const;
176 
177  //---------------------------------------------------------------------------
179  //---------------------------------------------------------------------------
180  void cleanup();
181 
182  //---------------------------------------------------------------------------
184  //---------------------------------------------------------------------------
185  bool reloadFonts();
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 
226 private:
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 
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.
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.
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 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:133
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:141
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.
Definition: mlGLResources.h:27
#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