+float GetLineLeft(const LineRun& lineRun)
+{
+ return lineRun.alignmentOffset;
+}
+
+float GetLineTop(const Vector<LineRun>& lines, const LineRun& lineRun)
+{
+ float lineTop = 0;
+ const int numberOfLines = (int)lines.Count();
+
+ int currentLineIndex = 0;
+ Vector<LineRun>::ConstIterator endIt = (&lineRun);
+ for(Vector<LineRun>::Iterator it = lines.Begin();
+ it != endIt;
+ ++it, ++currentLineIndex)
+ {
+ LineRun& line = *it;
+ bool isLastLine = (currentLineIndex + 1) == numberOfLines;
+ lineTop += GetLineHeight(line, isLastLine);
+ }
+
+ return lineTop;
+}
+
+float GetLineWidth(const LineRun& lineRun)
+{
+ return lineRun.width;
+}
+
+Rect<float> GetLineBoundingRect(ModelPtr textModel, const uint32_t lineIndex)
+{
+
+ if(textModel->mVisualModel == nullptr)
+ {
+ return {0, 0, 0, 0};
+ }
+
+ Length numberOfLines = textModel->mVisualModel->GetTotalNumberOfLines();
+
+ if(lineIndex >= numberOfLines)
+ {
+ return {0, 0, 0, 0};
+ }
+
+ const Vector<LineRun>& lines = textModel->mVisualModel->mLines;
+ const LineRun& lineRun = lines[lineIndex];
+ bool isFirstLine = lineIndex == 0;
+ bool isLastLine = (lineIndex + 1) == numberOfLines;
+
+ // Calculate the Left(lineX) = X position.
+ float lineX = GetLineLeft(lineRun) + textModel->mScrollPosition.x;
+
+ // Calculate the Top(lineY) = PreviousHeights.
+ // If the line is the first line of the text; its top = 0.
+ float lineY = (isFirstLine ? 0 : GetLineTop(lines, lineRun)) + textModel->mScrollPosition.y;
+
+ // The rectangle contains the width and height:
+ float lineWidth = GetLineWidth(lineRun);
+ float lineHeight = GetLineHeight(lineRun, isLastLine);
+
+ return {lineX, lineY, lineWidth, lineHeight};
+}
+
+float GetCharacterLeft(const GlyphInfo& glyph, const Vector2& characterPosition)
+{
+ return characterPosition.x - glyph.xBearing;
+}
+
+float GetCharacterTop(const float& yPosition)
+{
+ return (-1 * yPosition);
+}
+
+float GetCharacterHeight(const GlyphInfo& glyph)
+{
+ return glyph.height;
+}
+
+float GetCharacterWidth(const GlyphInfo& glyph)
+{
+ return glyph.advance;
+}
+
+Rect<> GetCharacterBoundingRect(ModelPtr textModel, const uint32_t charIndex)
+{
+ if(textModel->mVisualModel == nullptr)
+ {
+ return {0, 0, 0, 0};
+ }
+
+ VisualModelPtr& visualModel = textModel->mVisualModel;
+ LogicalModelPtr& logicalModel = textModel->mLogicalModel;
+
+ if(charIndex >= logicalModel->mText.Count() || visualModel->mLines.Empty())
+ {
+ return {0, 0, 0, 0};
+ }
+
+ const Vector<Vector2>& glyphPositions = visualModel->mGlyphPositions;
+ const Vector<GlyphInfo>& glyphs = visualModel->mGlyphs;
+ const Vector<LineRun>& lines = visualModel->mLines;
+
+ //For each character, the index of the first glyph.
+ const GlyphIndex glyphIndex = visualModel->mCharactersToGlyph[charIndex]; //took its glyphs
+
+ const Vector2& characterPosition = glyphPositions[glyphIndex];
+ const GlyphInfo& glyphInfo = glyphs[glyphIndex];
+
+ // GetLineOfCharacter function returns 0 if the lines are empty
+ const int lineIndex = visualModel->GetLineOfCharacter(charIndex);
+ const LineRun& lineRun = lines[lineIndex];
+
+ //Calculate the left: x position of the glyph + alignmentOffset of the line + mScrollPosition.x.
+ float characterX = lineRun.alignmentOffset + GetCharacterLeft(glyphInfo, characterPosition) + textModel->mScrollPosition.x;
+
+ //Calculate the Top(characterY): position.Y + previouse lines height + mScrollPosition.y.
+ bool isFirstLine = lineIndex == 0;
+
+ //If the line is the first line of the text; its top = 0.
+ float lineY = (isFirstLine ? 0 : GetLineTop(lines, lineRun));
+
+ float characterY = lineY + GetCharacterTop(characterPosition.y) + textModel->mScrollPosition.y;
+
+ //The rectangle contains the width and height:
+ float characterWidth = GetCharacterWidth(glyphInfo);
+ float characterHeight = GetCharacterHeight(glyphInfo);
+
+ return {characterX, characterY, characterWidth, characterHeight};
+}
+
+int GetCharIndexAtPosition(ModelPtr textModel, float visualX, float visualY)
+{
+ if(textModel == nullptr)
+ {
+ return -1;
+ }
+
+ VisualModelPtr& visualModel = textModel->mVisualModel;
+
+ const int totalNumberOfGlyphs = visualModel->mGlyphs.Count();
+ const int totalNumberOfLines = visualModel->mLines.Count();
+
+ if((0 == totalNumberOfGlyphs) ||
+ (0 == totalNumberOfLines))
+ {
+ return -1;
+ }
+
+ //The top point of the view = 0.
+ if(visualY < 0)
+ {
+ return -1;
+ }
+
+ const Vector<LineRun>& lines = visualModel->mLines;
+
+ float lineTop = 0.f;
+ int lineIndex = -1;
+ int high = totalNumberOfLines;
+ int low = -1;
+ int guess;
+
+ // Searching for the correct line.
+ while(high - low > 1)
+ {
+ guess = (high + low) / 2;
+ Vector<LineRun>::ConstIterator it = lines.Begin() + guess;
+
+ lineTop = GetLineTop(lines, *it);
+
+ if(lineTop > visualY)
+ {
+ high = guess;
+ }
+ else
+ {
+ low = guess;
+ }
+ }
+
+ if(low < 0)
+ {
+ lineIndex = 0;
+ }
+ else
+ {
+ lineIndex = low;
+ }
+
+ bool isLastLine = lineIndex + 1 == totalNumberOfLines;
+
+ if(isLastLine)
+ {
+ const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
+ float lineHeight = GetLineHeight(line, isLastLine);
+
+ // If the visualY is placed after the last line.
+ if(visualY > lineTop + lineHeight)
+ {
+ return -1;
+ }
+ }
+
+ // Check if a line is not found; return -1.
+ if(lineIndex == -1)
+ {
+ return -1;
+ }
+
+ // Start searching for the visualX
+ const LineRun& line = *(visualModel->mLines.Begin() + lineIndex);
+
+ visualX -= line.alignmentOffset;
+
+ // Positions of the glyphs
+ const Vector2* const positionsBuffer = visualModel->mGlyphPositions.Begin();
+
+ const CharacterIndex startCharacter = line.characterRun.characterIndex;
+ const CharacterIndex endCharacter = line.characterRun.characterIndex + line.characterRun.numberOfCharacters - 1;
+
+ CharacterIndex characterIndexAtPosition = -1;
+ CharacterIndex characterIndex = startCharacter;
+ float characterPosition;
+ float rightMostCharacterPosition;
+
+ for(; characterIndex != endCharacter; characterIndex++)
+ {
+ characterPosition = positionsBuffer[characterIndex].x;
+ rightMostCharacterPosition = positionsBuffer[characterIndex+1].x;
+
+ if(visualX < rightMostCharacterPosition && visualX >= characterPosition)
+ {
+ characterIndexAtPosition = characterIndex;
+ break;
+ }
+ }
+
+ if(characterIndex == endCharacter)
+ {
+ // If the visualX is within the last character position or it comes after the last character; we return the last character.
+ rightMostCharacterPosition = positionsBuffer[endCharacter].x + GetCharacterWidth(visualModel->mGlyphs[endCharacter]);
+
+ if(visualX >= positionsBuffer[endCharacter].x && visualX < rightMostCharacterPosition)
+ {
+ characterIndexAtPosition = endCharacter;
+ }
+ }
+
+ return characterIndexAtPosition;
+}