MCPcopy Create free account
hub / github.com/REGoth-project/REGoth / layoutText

Method layoutText

src/ui/zFont.cpp:171–211  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169}
170
171std::vector<std::string> UI::zFont::layoutText(const std::string& text, int maxWidth) const
172{
173 std::vector<std::size_t> newLinePositions;
174 int w = 0;
175 unsigned int lastSpace = 0;
176 bool spaceFound = false;
177 for (unsigned i = 0; i < text.size(); i++)
178 {
179 Glyph g;
180 getGlyphOf((unsigned char)text[i], g);
181
182 if (text[i] == ' ')
183 {
184 spaceFound = true;
185 lastSpace = i;
186 }
187
188 w += g.width + DISTANCE_BETWEEN_GLYPHS;
189 if (w > maxWidth && spaceFound)
190 {
191 spaceFound = false;
192 w = 0;
193 // go back to character after newline
194 i = lastSpace + 1;
195 newLinePositions.push_back(lastSpace);
196 }
197 }
198 std::vector<std::string> lines;
199 auto last_end = text.begin();
200 for (auto nlPos : newLinePositions)
201 {
202 lines.emplace_back(last_end, text.begin() + nlPos);
203 // + 1 because of omitting the space to split at
204 last_end = text.begin() + nlPos + 1;
205 }
206 if (last_end != text.end())
207 {
208 lines.emplace_back(last_end, text.end());
209 }
210 return lines;
211}
212
213UI::zFontCache::zFontCache(Engine::BaseEngine& e)
214 : m_Engine(e)

Callers 2

setTopicEntriesMethod · 0.80
updateMethod · 0.80

Calls 4

push_backMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected