Pioneer
DistanceFieldFont.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 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 
26 #include "RefCounted.h"
27 #include "vector2.h"
28 
29 #include <string>
30 #include <map>
31 
32 namespace Graphics {
33  class Texture;
34  class VertexArray;
35 } // namespace Graphics
36 
37 namespace Text {
38 
39  class DistanceFieldFont : public RefCounted {
40  public:
41  DistanceFieldFont(const std::string &definitionFileName, Graphics::Texture *);
42  void GetGeometry(Graphics::VertexArray &, const std::string &, const vector2f &offset);
43  Graphics::Texture *GetTexture() const { return m_texture; }
45 
46  private:
47  struct Glyph {
48  vector2f uv;
49  vector2f uvSize;
50  vector2f size; //geometry size
51  vector2f offset; //offset applied to the cursor position
52  float xAdvance; //how much the cursor should be moved after a character
53  };
54  Graphics::Texture *m_texture;
55  std::map<Uint32, Glyph> m_glyphs;
56  vector2f m_sheetSize;
57  float m_lineHeight;
58  float m_fontSize; //32 etc. Glyph size/advance will be scaled to 1/fontSize.
59 
60  void AddGlyph(Graphics::VertexArray &va, const vector2f &pos, const Glyph &, vector2f &bounds);
61  void ParseChar(std::string_view line);
62  void ParseCommon(std::string_view line);
63  void ParseInfo(std::string_view line);
64  };
65 
66 } // namespace Text
67 
68 #endif
Definition: Texture.h:106
Definition: VertexArray.h:19
Definition: RefCounted.h:11
Definition: DistanceFieldFont.h:39
Graphics::Texture * GetTexture() const
Definition: DistanceFieldFont.h:43
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:14
Definition: Background.h:14
Definition: DistanceFieldFont.cpp:12