| 371 | } |
| 372 | |
| 373 | void TextLayer::resolveTextAlignment(const std::vector<std::shared_ptr<GlyphLine>>& glyphLines, |
| 374 | float emptyAdvance, |
| 375 | std::vector<std::shared_ptr<GlyphInfo>>& finalGlyphInfos, |
| 376 | std::vector<Point>& positions) const { |
| 377 | if (glyphLines.empty()) { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | float xOffset = 0.0f; |
| 382 | float yOffset = 0.0f; |
| 383 | for (size_t i = 0; i < glyphLines.size(); ++i) { |
| 384 | const auto& glyphLine = glyphLines[i]; |
| 385 | const auto lineGlyphCount = glyphLine->getGlyphCount(); |
| 386 | |
| 387 | const auto lineWidth = glyphLine->getLineWidth(); |
| 388 | const auto lineHeight = getLineHeight(glyphLine); |
| 389 | |
| 390 | xOffset = 0.0f; |
| 391 | yOffset += lineHeight; |
| 392 | float spaceWidth = 0.0f; |
| 393 | |
| 394 | if (_width != 0.0f) { |
| 395 | switch (_textAlign) { |
| 396 | case TextAlign::Left: |
| 397 | // do nothing |
| 398 | break; |
| 399 | case TextAlign::Center: |
| 400 | xOffset = (_width - lineWidth) / 2.0f; |
| 401 | break; |
| 402 | case TextAlign::Right: |
| 403 | xOffset = _width - lineWidth; |
| 404 | break; |
| 405 | case TextAlign::Justify: { |
| 406 | // 1. Each line must have more than one character |
| 407 | if (lineGlyphCount <= 1) { |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | // 2. If auto-wrap is disabled and the width is set very small, spaceWidth might be negative, causing characters to overlap. This needs to be avoided. |
| 412 | if (_width < lineWidth) { |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | // 3. The last line should not be justified (align it to the left), or if auto-wrap is disabled and there is only one line of text, it should be justified. |
| 417 | if (i < glyphLines.size() - 1 || (!_autoWrap && 1 == glyphLines.size())) { |
| 418 | spaceWidth = (_width - lineWidth) / static_cast<float>(lineGlyphCount - 1); |
| 419 | } |
| 420 | break; |
| 421 | } |
| 422 | default: |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | for (size_t index = 0; index < lineGlyphCount; ++index) { |
| 428 | const auto& glyphInfo = glyphLine->getGlyphInfo(index); |
| 429 | if (glyphInfo == nullptr) { |
| 430 | continue; |
nothing calls this directly
no test coverage detected