Pioneer
DistanceFieldFont.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _TEXT_DISTANCEFIELDFONT_H
5 #define _TEXT_DISTANCEFIELDFONT_H
6 /*
7  * Font rendering using a pregenerated signed distance field texture
8  * to produce highly zoomable text.
9  * Reads texture+definition generated by:
10  * http://www.angelcode.com/products/bmfont/ combined with
11  * http://bitsquid.blogspot.ca/2010/04/distance-field-based-rendering-of.html
12  *
13  * Note, this is meant for the model system Label3D: short text on a single line
14  *
15  * The font definitions are text files looking like this:
16  *
17  * info face="Ubuntu Mono" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=8,8,8,8 spacing=1,1 outline=0
18  * common lineHeight=32 base=26.5 scaleW=256 scaleH=256 pages=1 packed=0 alphaChnl=0 redChnl=4 greenChnl=4 blueChnl=4
19  * page id=0 file="label3d.png"
20  * chars count=96
21  * char id=0 x=251.75 y=0 width=2 height=2.125 xoffset=-1 yoffset=30.875 xadvance=16 page=0 chnl=15
22  * char id=32 x=253.875 y=0 width=2 height=2.125 xoffset=-1 yoffset=30.875 xadvance=16 page=0 chnl=15
23  * (etc)
24  */
25 #include "StringRange.h"
26 #include "libs.h"
27 
28 namespace Graphics {
29  class Texture;
30  class VertexArray;
31 } // namespace Graphics
32 
33 namespace Text {
34 
35  class DistanceFieldFont : public RefCounted {
36  public:
37  DistanceFieldFont(const std::string &definitionFileName, Graphics::Texture *);
38  void GetGeometry(Graphics::VertexArray &, const std::string &, const vector2f &offset);
39  Graphics::Texture *GetTexture() const { return m_texture; }
41 
42  private:
43  struct Glyph {
44  vector2f uv;
45  vector2f uvSize;
46  vector2f size; //geometry size
47  vector2f offset; //offset applied to the cursor position
48  float xAdvance; //how much the cursor should be moved after a character
49  };
50  Graphics::Texture *m_texture;
51  std::map<Uint32, Glyph> m_glyphs;
52  vector2f m_sheetSize;
53  float m_lineHeight;
54  float m_fontSize; //32 etc. Glyph size/advance will be scaled to 1/fontSize.
55 
56  void AddGlyph(Graphics::VertexArray &va, const vector2f &pos, const Glyph &, vector2f &bounds);
57  void ParseChar(const StringRange &line);
58  void ParseCommon(const StringRange &line);
59  void ParseInfo(const StringRange &line);
60  };
61 
62 } // namespace Text
63 
64 #endif
Definition: Texture.h:106
Definition: VertexArray.h:19
Definition: RefCounted.h:11
Definition: DistanceFieldFont.h:35
Graphics::Texture * GetTexture() const
Definition: DistanceFieldFont.h:39
Graphics::VertexArray * CreateVertexArray() const
Definition: DistanceFieldFont.cpp:76
void GetGeometry(Graphics::VertexArray &, const std::string &, const vector2f &offset)
Definition: DistanceFieldFont.cpp:43
DistanceFieldFont(const std::string &definitionFileName, Graphics::Texture *)
Definition: DistanceFieldFont.cpp:15
Definition: Background.h:13
Definition: GuiLabel.h:12
Definition: StringRange.h:11