| 406 | } |
| 407 | |
| 408 | Float2 Font::GetCharPosition(const StringView& text, int32 index, const TextLayoutOptions& layout) |
| 409 | { |
| 410 | // Check if there is no need to do anything |
| 411 | if (text.IsEmpty()) |
| 412 | { |
| 413 | Float2 location = layout.Bounds.Location; |
| 414 | if (layout.VerticalAlignment == TextAlignment::Center) |
| 415 | location.Y += layout.Bounds.Size.Y * 0.5f - static_cast<float>(_height) * 0.5f; |
| 416 | else if (layout.VerticalAlignment == TextAlignment::Far) |
| 417 | location.Y += layout.Bounds.Size.Y - static_cast<float>(_height) * 0.5f; |
| 418 | |
| 419 | if (layout.HorizontalAlignment == TextAlignment::Center) |
| 420 | location.X += layout.Bounds.Size.X * 0.5f; |
| 421 | else if (layout.HorizontalAlignment == TextAlignment::Far) |
| 422 | location.X += layout.Bounds.Size.X; |
| 423 | return location; |
| 424 | } |
| 425 | |
| 426 | // Process text |
| 427 | Array<FontLineCache, InlinedAllocation<8>> lines; |
| 428 | ProcessText(text, lines, layout); |
| 429 | ASSERT(lines.HasItems()); |
| 430 | float scale = layout.Scale / FontManager::FontScale; |
| 431 | float baseLinesDistance = static_cast<float>(_height) * layout.BaseLinesGapScale * scale; |
| 432 | |
| 433 | // Find line with that position |
| 434 | FontCharacterEntry previous; |
| 435 | FontCharacterEntry entry; |
| 436 | for (int32 lineIndex = 0; lineIndex < lines.Count(); lineIndex++) |
| 437 | { |
| 438 | const FontLineCache& line = lines[lineIndex]; |
| 439 | |
| 440 | // Check if desire position is somewhere inside characters in line range |
| 441 | if (Math::IsInRange(index, line.FirstCharIndex, line.LastCharIndex)) |
| 442 | { |
| 443 | Float2 charPos = line.Location; |
| 444 | |
| 445 | // Check all characters in the line |
| 446 | for (int32 currentIndex = line.FirstCharIndex; currentIndex < index; currentIndex++) |
| 447 | { |
| 448 | // Cache current character |
| 449 | const Char currentChar = text[currentIndex]; |
| 450 | GetCharacter(currentChar, entry); |
| 451 | const bool isWhitespace = StringUtils::IsWhitespace(currentChar); |
| 452 | |
| 453 | // Apply kerning |
| 454 | if (!isWhitespace && previous.IsValid) |
| 455 | { |
| 456 | charPos.X += entry.Font->GetKerning(previous.Character, entry.Character); |
| 457 | } |
| 458 | previous = entry; |
| 459 | |
| 460 | // Move |
| 461 | charPos.X += entry.AdvanceX * scale; |
| 462 | } |
| 463 | |
| 464 | // Upper left corner of the character |
| 465 | return layout.Bounds.Location + charPos; |
no test coverage detected