MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / GetCharPosition

Method GetCharPosition

Source/Engine/Render2D/Font.cpp:408–471  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

406}
407
408Float2 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;

Callers 6

UpdateFilterMethod · 0.45
UpdateFilterMethod · 0.45
UpdateFilterMethod · 0.45
UpdateFilterMethod · 0.45
UpdateFilterMethod · 0.45

Calls 8

ProcessTextFunction · 0.85
IsInRangeFunction · 0.85
Float2Class · 0.85
GetKerningMethod · 0.80
IsEmptyMethod · 0.45
HasItemsMethod · 0.45
CountMethod · 0.45
LastMethod · 0.45

Tested by

no test coverage detected