hx3d  1
2D/3D Simple Game Framework
text.cpp
1 /*
2  GUI text.
3  Render text.
4 
5  Copyright (C) 2015 Denis BOURGE
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  USA
21 */
22 
23 #include "hx3d/gui/text.hpp"
24 
25 #include "hx3d/core/core.hpp"
26 #include "hx3d/graphics/font.hpp"
27 
28 #include "hx3d/utils/log.hpp"
29 #include "hx3d/utils/assets.hpp"
30 
31 namespace hx3d {
32 namespace gui {
33 
35  Text(Core::Assets()->get<Font>("default")) {}
36 
38  Text(nullptr, font) {}
39 
40 Text::Text(Widget* parent, Ptr<Font> font):
41  Widget(parent), _font(font), _content("") {
42  init();
43 }
44 
45 void Text::init() {
46  setTint(Color::White);
47  if (_font) {
48  setFont(_font);
49  }
50 
51  _centerAligned = true;
52 }
53 
55  _font = font;
56  _characterSize = font->getDefaultSize();
58 }
59 
60 void Text::setCharacterSize(int size) {
61  _characterSize = size;
63 }
64 
65 void Text::setContent(std::string content) {
66  _content = content;
68 }
69 
71  return _font;
72 }
73 
75  return _characterSize;
76 }
77 
78 float Text::getLength() {
79  return _length;
80 }
81 
82 void Text::setCenterAlignment(bool value) {
83  _centerAligned = value;
84 }
85 
87  return _centerAligned;
88 }
89 
90 void Text::draw(Ptr<Shader> shader) {
91 
92  std::wstring wtext(_content.begin(), _content.end());
93  glm::vec2 pen(0, 0);
94 
95  Font::Data& fontData = _font->getFontData(_characterSize);
96 
97  for (unsigned int i = 0; i < _content.size(); ++i) {
98  texture_glyph_t *glyph = texture_font_get_glyph(fontData.font, &_content[i]);
99  if (glyph != nullptr) {
100  float kerning = 0.f;
101  if (i > 0) {
102  kerning = texture_glyph_get_kerning(glyph, &_content[i-1]);
103  }
104 
105  pen.x += kerning;
106  float x0 = pen.x + glyph->offset_x;
107  float y0 = pen.y + glyph->offset_y;
108  float x1 = x0 + glyph->width;
109  float y1 = y0 - glyph->height;
110  float s0 = glyph->s0;
111  float t0 = glyph->t0;
112  float s1 = glyph->s1;
113  float t1 = glyph->t1;
114 
115  _geometry->setAttribute("Position", std::vector<float> {
116  x0, y0, 0,
117  x0, y1, 0,
118  x1, y1, 0,
119  x1, y0, 0
120  });
121 
122  _geometry->setAttribute("Texture", std::vector<float> {
123  s0, t0,
124  s0, t1,
125  s1, t1,
126  s1, t0
127  });
128 
129  _geometry->uploadAll();
130 
131  Mesh::draw(shader);
132 
133  pen.x += glyph->advance_x;
134  }
135  }
136 }
137 
139  if (_font == nullptr)
140  return 0;
141 
142  glm::vec2 pen(0, 0);
143  std::wstring wtext(_content.begin(), _content.end());
144 
145  Font::Data& fontData = _font->getFontData(_characterSize);
146  for (unsigned int i = 0; i < _content.size(); ++i) {
147  texture_glyph_t *glyph = texture_font_get_glyph(fontData.font, &_content[i]);
148  if (glyph != nullptr) {
149  float kerning = 0.f;
150  if (i > 0) {
151  kerning = texture_glyph_get_kerning(glyph, &_content[i-1]);
152  }
153 
154  pen.x += kerning;
155  pen.x += glyph->advance_x;
156  }
157  }
158 
159  return pen.x;
160 }
161 
163  std::wstring wtext(_content.begin(), _content.end());
164  glm::vec2 pen(0, 0);
165 
166  Font::Data& fontData = _font->getFontData(_characterSize);
167 
168  function.reset();
169 
170  for (unsigned int i = 0; i < _content.size(); ++i) {
171  texture_glyph_t *glyph = texture_font_get_glyph(fontData.font, &_content[i]);
172  if (glyph != nullptr) {
173  float kerning = 0.f;
174  if (i > 0) {
175  kerning = texture_glyph_get_kerning(glyph, &_content[i-1]);
176  }
177 
178  pen += function.sample();
179  pen.x += kerning;
180 
181  float x0 = pen.x + glyph->offset_x;
182  float y0 = pen.y + glyph->offset_y;
183  float x1 = x0 + glyph->width;
184  float y1 = y0 - glyph->height;
185  float s0 = glyph->s0;
186  float t0 = glyph->t0;
187  float s1 = glyph->s1;
188  float t1 = glyph->t1;
189 
190  _geometry->setAttribute("Position", std::vector<float> {
191  x0, y0, 0,
192  x0, y1, 0,
193  x1, y1, 0,
194  x1, y0, 0
195  });
196 
197  _geometry->setAttribute("Texture", std::vector<float> {
198  s0, t0,
199  s0, t1,
200  s1, t1,
201  s1, t0
202  });
203 
204  _geometry->uploadAll();
205 
206  Mesh::draw(shader);
207 
208  pen.x += glyph->advance_x;
209  function.step();
210  }
211  }
212 }
213 
214 } /* gui */
215 } /* hx3d */
bool isCenterAligned()
Is the text center aligned ?
Definition: text.cpp:86
void init()
Initialize the text.
Definition: text.cpp:45
Base GUI element.
Definition: widget.hpp:39
void setTint(Color tint)
Set the mesh tint.
Definition: mesh.cpp:43
void setCharacterSize(int size)
Set the character size.
Definition: text.cpp:60
void setCenterAlignment(bool value)
Set the center alignment.
Definition: text.cpp:82
bool _centerAligned
Is the text center aligned ?
Definition: text.hpp:149
Text()
Create a text without font. See setFont.
Definition: text.cpp:34
void setFont(Ptr< Font > font)
Set the font.
Definition: text.cpp:54
Text GUI element.
Definition: text.hpp:37
float _length
Text length.
Definition: text.hpp:145
hx3d framework namespace
Definition: audio.hpp:26
float calculateLength()
Calculate the text length.
Definition: text.cpp:138
int _characterSize
Character size.
Definition: text.hpp:147
std::string _content
Text content.
Definition: text.hpp:143
Ptr< Font > _font
Text font.
Definition: text.hpp:141
void setContent(std::string content)
Set the text content.
Definition: text.cpp:65
Ptr< geom::BaseGeometry > _geometry
Current geometry.
Definition: mesh.hpp:96
Centralized framework management.
Definition: core.hpp:50
int getCharacterSize()
Get the character size.
Definition: text.cpp:74
Math function definition.
Definition: function.hpp:33
float getLength()
Get the text length.
Definition: text.cpp:78
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
Ptr< Font > getFont()
Get the text font.
Definition: text.cpp:70
void functionDraw(Ptr< Shader > shader, math::Function function)
Draw the text following a function.
Definition: text.cpp:162