rag
graphic 2d engine
TTFFont.h
1 #ifndef Rag_TTFFont_h
2 #define Rag_TTFFont_h
3 
4 #include "ITextFont.h"
5 #include <rag/Platform.h>
6 
7 #define STBTT_malloc(x,u) malloc(x)
8 #define STBTT_free(x,u) free(x)
9 
10 #include "external/stb_truetype.h"
11 #include <string>
12 #include <map>
13 #include <glm/glm.hpp>
14 
15 namespace rag
16 {
18  class TTFFont: public ITextFont
19  {
20  public:
21 
23  TTFFont(const std::string& path, float pixelHeight = 24);
24  virtual ~TTFFont();
25 
26  static void addFontAlias(const std::string& alias, const std::string& fontPath);
27 
28  virtual int getWidth(const std::string& text);
29  virtual void print(const std::string& text, const glm::mat4& matrix);
30 
31  virtual void setLetterSpacing(float value);
32  virtual void reloadTexture();
33 
34  private:
35 
36  void getBakedQuad(int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q);
37 
39  int bakeFontBitmap(int first_char, bool uploadTexture = true);
40 
41  void allocTexture();
42 
43  void uploadTexture();
44 
45  stbtt_fontinfo info;
46  std::map<int, stbtt_bakedchar> chardata;
47  float pixelHeight, scale;
48  int tex_w, tex_h;
49  GLuint textureName;
50  unsigned char* buffer;
51  int ascent, descent, lineGap;
52  unsigned char* bmp;
53 
55  int x, y, bottom_y; // \todo refactor x and y names
56  float ipw, iph;
57 
58  float letterSpacing;
59 
60  static std::map<std::string, std::string> fontsAlias;
61 
62  };
63 }
64 
65 #endif
virtual int getWidth(const std::string &text)
Returns the width of a text.
Definition: TTFFont.cpp:78
virtual void setLetterSpacing(float value)
Sets the extra space between characters.
Definition: TTFFont.cpp:153
static void addFontAlias(const std::string &alias, const std::string &fontPath)
Allows to use a different name (or an 'alias') to refer to a font.
Definition: TTFFont.cpp:16
Definition: Bitmap.h:8
virtual void print(const std::string &text, const glm::mat4 &matrix)
Renders text. Asumes ortho projection 1:1 screen pixel.
Definition: TTFFont.cpp:96
virtual void reloadTexture()
On context loss, reload textures. ITextFont should inherit Resource. Reload should be part of resour...
Definition: TTFFont.cpp:264
TTFFont(const std::string &path, float pixelHeight=24)
Constructs a TTFFont using a path and a text size.
Definition: TTFFont.cpp:21
Interface for text fonts.
Definition: ITextFont.h:10
Implementation of ITextFint based on TrueType or OpenType Fonts.
Definition: TTFFont.h:18