MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / processWrapText

Method processWrapText

source/rendering/StarTextPainter.cpp:129–228  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

127}
128
129bool TextPainter::processWrapText(StringView text, unsigned* wrapWidth, WrapTextCallback textFunc) {
130 String font = m_renderSettings.font, setFont = font;
131 m_fontTextureGroup.switchFont(font);
132 auto iterator = text.begin(), end = text.end();
133
134 unsigned lines = 0;
135 auto lineStartIterator = iterator, splitIterator = end;
136 unsigned linePixelWidth = 0, splitPixelWidth = 0;
137 size_t commandStart = NPos, commandEnd = NPos;
138 bool finished = true;
139
140 auto slice = [](StringView::const_iterator a, StringView::const_iterator b) -> StringView {
141 return StringView(&*a.base(), b.base() - a.base());
142 };
143
144 while (iterator != end) {
145 auto character = *iterator;
146 finished = false; // assume at least one character if we get here
147 bool noMoreCommands = commandStart != NPos && commandEnd == NPos;
148 if (!noMoreCommands && Text::isEscapeCode(character)) {
149 size_t index = &*iterator.base() - text.utf8Ptr();
150 if (commandStart == NPos) {
151 for (size_t escOrEnd = commandStart = index;
152 (escOrEnd = text.utf8().find_first_of(Text::AllEscEnd, escOrEnd + 1)) != NPos;) {
153 if (text.utf8().at(escOrEnd) != Text::EndEsc)
154 commandStart = escOrEnd;
155 else {
156 commandEnd = escOrEnd;
157 break;
158 }
159 }
160 }
161 if (commandStart == index && commandEnd != NPos) {
162 const char* commandStr = text.utf8Ptr() + ++commandStart;
163 StringView inner(commandStr, commandEnd - commandStart);
164 inner.forEachSplitView(",", [&](StringView command, size_t, size_t) {
165 if (command == "reset") {
166 m_fontTextureGroup.switchFont(font = setFont);
167 } else if (command == "set") {
168 setFont = font;
169 } else if (command.beginsWith("font=")) {
170 m_fontTextureGroup.switchFont(font = command.substr(5));
171 }
172 });
173 // jump the iterator to the character after the command
174 iterator = text.utf8().begin() + commandEnd + 1;
175 commandStart = commandEnd = NPos;
176 continue;
177 }
178 }
179 // is this a linefeed / cr / whatever that forces a line split ?
180 if (character == '\n' || character == '\v') {
181 // knock one off the end because we don't render the CR
182 if (!textFunc(slice(lineStartIterator, iterator), lines++))
183 return false;
184
185 lineStartIterator = iterator;
186 ++lineStartIterator;

Callers

nothing calls this directly

Calls 11

StringViewClass · 0.85
isEscapeCodeFunction · 0.85
sliceFunction · 0.85
switchFontMethod · 0.80
forEachSplitViewMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
utf8PtrMethod · 0.45
atMethod · 0.45
beginsWithMethod · 0.45
substrMethod · 0.45

Tested by

no test coverage detected