| 1153 | std::vector<TextLayoutBuilderSegment>& out, |
| 1154 | Point* drawPosition, |
| 1155 | TextLayoutBuilderWhitespaceIndexes& whitespaceIndexes) { |
| 1156 | /** |
| 1157 | This algorithm will break down each TextLayoutBuilderSegment into one segment per word. |
| 1158 | When encountering a whitespace that can be used to justify words, the index of segment |
| 1159 | following the whitespace is added into the WhitespaceIndexes. |
| 1160 | */ |
| 1161 | const auto* glyphsIt = glyphs + segment.glyphsStart; |
| 1162 | const auto* glyphsEnd = glyphsIt + segment.glyphsCount; |
| 1163 | const ShapedGlyph* wordStart = nullptr; |
| 1164 | |
| 1165 | while (glyphsIt != glyphsEnd) { |
| 1166 | auto uni = glyphsIt->character(); |
| 1167 | |
| 1168 | if (Unicode::isBreakingWhitespace(uni)) { |
| 1169 | if (wordStart != nullptr) { |
| 1170 | appendSegmentWord(segment, glyphs, wordStart, glyphsIt, drawPosition, out); |
| 1171 | wordStart = nullptr; |
| 1172 | } |
| 1173 | |
| 1174 | appendWhitespaceIndexIfNeeded(segment, out, whitespaceIndexes); |
| 1175 | } else { |
| 1176 | if (wordStart == nullptr) { |
| 1177 | wordStart = glyphsIt; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | glyphsIt++; |
| 1182 | } |
| 1183 | |
| 1184 | if (wordStart != nullptr) { |
| 1185 | appendSegmentWord(segment, glyphs, wordStart, glyphsIt, drawPosition, out); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | static void justifySegments(TextLayoutBuilderSegment* segments, |
| 1190 | size_t segmentsStart, |
| 1191 | size_t segmentsEnd, |
| 1192 | Scalar lineWidth, |
| 1193 | Scalar consumedCharactersWidth, |
| 1194 | const TextLayoutBuilderWhitespaceIndexes& whitespaceIndexes) { |
| 1195 | if (whitespaceIndexes.empty()) { |
| 1196 | return; |
| 1197 | } |
| 1198 | |
| 1199 | auto resolvedWhitespaceIndexesSize = whitespaceIndexes.size(); |
| 1200 | if (whitespaceIndexes[resolvedWhitespaceIndexesSize - 1] == segmentsEnd - 1) { |
| 1201 | // If the last whitespace index matches the last segment, we have trailing |
| 1202 | // spaces and we should ignore it |
| 1203 | resolvedWhitespaceIndexesSize--; |
| 1204 | } |
| 1205 | |
| 1206 | // Calculate how much space should be inserted between each segment that has whitespace before it |
| 1207 | auto widthPerWhitespaceIndex = |
| 1208 | (lineWidth - consumedCharactersWidth) / static_cast<Scalar>(resolvedWhitespaceIndexesSize); |
| 1209 | |
| 1210 | Scalar currentOffset = 0; |
| 1211 | size_t currentWhitespaceIndex = 0; |
| 1212 | // Go over all the segments on the line and update their drawX position to account for the whitespaces. |
no test coverage detected