Debian Codegraph @

utils/fgpanel/FGFontCache.cxx

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2011 Torsten Dreyer
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #ifdef HAVE_CONFIG_H
5 # include <config.h>
6 #endif
7
8 #ifdef HAVE_WINDOWS_H
9 #include <windows.h>
10 #endif
11
12 #include <math.h>
13
14 #include "FGGLApplication.hxx"
15 #include "ApplicationProperties.hxx"
16 #include "FGFontCache.hxx"
17 using std::pair;
18 using std::string;
19 using std::stringstream;
20
21 ////////////////////////////////////////////////////////////////////////
22 // FGFontCache class.
23 ////////////////////////////////////////////////////////////////////////
24
25 const unsigned short FGFontCache::First_Printable_Char = 32;
26 const unsigned short FGFontCache::Last_Printable_Char = 127;
27
28 FGFontCache::FGFontCache () :
29 m_Face_Map (),
30 m_Current_Face_Ptr (NULL),
31 m_Pos_Map (),
32 m_Current_Pos (0) {
33 if (FT_Init_FreeType (&m_Ft)) {
34 SG_LOG (SG_COCKPIT, SG_ALERT, "Could not init freetype library");
35 }
36 for (unsigned int Index = 0; Index < Texture_Size * Texture_Size; ++Index) {
37 m_Texture[Index] = char (0);
38 }
39 glGenTextures (1, &m_Glyph_Texture);
40 }
41
42 FGFontCache::~FGFontCache () {
43 for (Face_Map_Type::iterator It = m_Face_Map.begin (); It != m_Face_Map.end (); ++It) {
44 delete (It->second);
45 }
46 m_Face_Map.clear ();
47 glDeleteTextures (1, &m_Glyph_Texture);
48 }
49
50 bool
51 FGFontCache::Set_Font (const string& Font_Name,
52 const float Size,
53 GLuint &Glyph_Texture) {
54 if (m_Face_Map.find (Font_Name) != m_Face_Map.end ()) {
55 m_Current_Face_Ptr = m_Face_Map[Font_Name];
56 } else {
57 FT_Face * const Face_Ptr (new FT_Face);
58 if (FT_New_Face (m_Ft, Font_Name.c_str (), 0, Face_Ptr)) {
59 SG_LOG (SG_COCKPIT, SG_ALERT, "Could not open font : " + Font_Name);
60 return false;
61 }
62 m_Face_Map.insert (pair <string, FT_Face *> (Font_Name, Face_Ptr));
63 m_Current_Face_Ptr = Face_Ptr;
64 }
65 if (m_Current_Face_Ptr != NULL) {
66 FT_Set_Pixel_Sizes (*m_Current_Face_Ptr, 0, Size);
67 } else {
68 return false;
69 }
70 const string Key_Str (Font_Name + "_" + Get_Size (Size));
71 if (m_Pos_Map.find (Key_Str) != m_Pos_Map.end ()) {
72 m_Current_Pos = m_Pos_Map[Key_Str];
73 } else {
74 m_Current_Pos = m_Pos_Map.size ();
75 for (unsigned short ASCII_Code = First_Printable_Char;
76 ASCII_Code < Last_Printable_Char;
77 ++ASCII_Code) {
78 if (FT_Load_Char (*m_Current_Face_Ptr, char (ASCII_Code), FT_LOAD_RENDER)) {
79 SG_LOG (SG_COCKPIT, SG_ALERT, "Could not load character : " << char (ASCII_Code));
80 } else {
81 unsigned int Line;
82 unsigned int Column;
83 const FT_GlyphSlot Glyph ((*m_Current_Face_Ptr)->glyph);
84 for (unsigned int Row = 0; Row < Glyph->bitmap.rows; ++Row) {
85 for (unsigned int Width = 0; Width < Glyph->bitmap.width; ++Width) {
86 Get_Pos (ASCII_Code, Row, Width, Line, Column);
87 m_Texture[Line * Texture_Size + Column] =
88 Glyph->bitmap.buffer[Row * Glyph->bitmap.width + Width];
89 }
90 }
91 }
92 }
93 glBindTexture (GL_TEXTURE_2D, m_Glyph_Texture);
94 /* We require 1 byte alignment when uploading texture data */
95 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
96 /* Clamping to edges is important to prevent artifacts when scaling */
97 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
98 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
99 /* Linear filtering usually looks best for text */
100 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
101 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
102 glTexImage2D (GL_TEXTURE_2D,
103 0,
104 GL_ALPHA,
105 Texture_Size,
106 Texture_Size,
107 0,
108 GL_ALPHA,
109 GL_UNSIGNED_BYTE,
110 m_Texture);
111 m_Pos_Map.insert (pair <string, unsigned int> (Key_Str, m_Current_Pos));
112 }
113 Glyph_Texture = m_Glyph_Texture;
114 return true;
115 }
116
117 bool
118 FGFontCache::Get_Char (const char Char,
119 int &X, int &Y,
120 int &Left, int &Bottom,
121 int &W, int &H,
122 double &X1, double &Y1,
123 double &X2, double &Y2) const {
124 if (m_Current_Face_Ptr != NULL) {
125 if (FT_Load_Char (*m_Current_Face_Ptr, Char, FT_LOAD_RENDER)) {
126 SG_LOG (SG_COCKPIT, SG_ALERT, "Could not load character : " + Char);
127 return false;
128 } else {
129 const FT_GlyphSlot Glyph ((*m_Current_Face_Ptr)->glyph);
130 Left = Glyph->bitmap_left;
131 W = Glyph->bitmap.width;
132 H = Glyph->bitmap.rows;
133 Bottom = Glyph->bitmap_top - H;
134 X += (Glyph->advance.x >> 6);
135 Y += (Glyph->advance.y >> 6);
136 Get_Relative_Pos (int (Char), 0, 0, X1, Y1);
137 Get_Relative_Pos (int (Char), Glyph->bitmap.rows, Glyph->bitmap.width, X2, Y2);
138 return true;
139 }
140 } else {
141 return false;
142 }
143 }
144
145 void
146 FGFontCache::Get_Pos (const unsigned short ASCII_Code,
147 const unsigned short Row,
148 const unsigned short Width,
149 unsigned int &Line,
150 unsigned int &Column) const {
151 const unsigned short Font_Size (32);
152 Line = (((((ASCII_Code - First_Printable_Char) * Font_Size) / Texture_Size) + 3 * m_Current_Pos) * Font_Size) + Row;
153 Column = (((ASCII_Code - First_Printable_Char) * Font_Size) % Texture_Size) + Width;
154 }
155
156 void
157 FGFontCache::Get_Relative_Pos (const unsigned short ASCII_Code,
158 const unsigned short Row,
159 const unsigned short Width,
160 double &X,
161 double &Y) const {
162 unsigned int Line;
163 unsigned int Column;
164 Get_Pos (ASCII_Code, Row, Width, Line, Column);
165 X = double (Column) / double (Texture_Size);
166 Y = double (Line) / double (Texture_Size);
167 }
168
169 string
170 FGFontCache::Get_Size (const float Size) {
171 const int Half_Size (int (round (2.0 * Size)));
172 const int Int_Part (Half_Size / 2);
173 const int Dec_Part ((Half_Size % 2) ? 5 : 0);
174 stringstream Result_SS;
175 Result_SS << Int_Part << "." << Dec_Part;
176 return Result_SS.str ();
177 }