MCPcopy Create free account
hub / github.com/TankOs/SFGUI / GetTextStringMetrics

Method GetTextStringMetrics

src/SFGUI/Engine.cpp:126–170  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

124}
125
126sf::Vector2f Engine::GetTextStringMetrics( const std::u32string& string, const sf::Font& font, unsigned int font_size ) const {
127 // SFML is incapable of giving us the metrics we need so we have to do it ourselves.
128 auto horizontal_spacing = static_cast<float>( font.getGlyph( L' ', font_size, false ).advance );
129 auto vertical_spacing = static_cast<float>( font.getLineSpacing( font_size ) );
130
131 sf::Vector2f metrics( 0.f, 0.f );
132
133 const static auto tab_spaces = 2.f;
134
135 std::uint32_t previous_character = 0;
136
137 auto longest_line = 0.f;
138
139 for( const auto& current_character : string ) {
140 metrics.x += static_cast<float>( font.getKerning( previous_character, current_character, font_size ) );
141
142 switch( current_character ) {
143 case L' ':
144 metrics.x += horizontal_spacing;
145 continue;
146 case L'\t':
147 metrics.x += horizontal_spacing * tab_spaces;
148 continue;
149 case L'\n':
150 metrics.y += vertical_spacing;
151 longest_line = std::max( metrics.x, longest_line );
152 metrics.x = 0.f;
153 continue;
154 case L'\v':
155 metrics.y += vertical_spacing * tab_spaces;
156 continue;
157 default:
158 break;
159 }
160
161 const auto& glyph = font.getGlyph( current_character, font_size, false );
162
163 metrics.x += static_cast<float>( glyph.advance );
164 metrics.y = std::max( metrics.y, static_cast<float>( glyph.bounds.size.y ) );
165 }
166
167 metrics.x = std::max( longest_line, metrics.x );
168
169 return metrics;
170}
171
172sf::Vector2f Engine::GetTextStringMetrics( const sf::String& string, const sf::Font& font, unsigned int font_size ) const {
173 // SFML is incapable of giving us the metrics we need so we have to do it ourselves.

Callers 10

WrapTextMethod · 0.80
CalculateRequisitionMethod · 0.80
CalculateRequisitionMethod · 0.80
CalculateRequisitionMethod · 0.80
CalculateRequisitionMethod · 0.80
GetPositionFromMouseXMethod · 0.80
CalculateRequisitionMethod · 0.80
InvalidateImplMethod · 0.80
CalculateRequisitionMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected