Skip to content

TextMeshProUGUI: fix underline and strikethrough for RTL #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions com.unity.ugui/Runtime/TMP/TextMeshProUGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5055,7 +5055,7 @@ protected virtual void GenerateTextMesh()
underlineMaxScale = underlineStartScale;
xScaleMax = xScale;
}
underline_start = new Vector3(m_textInfo.characterInfo[i].bottomLeft.x, underlineBaseLine, 0);
underline_start = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].bottomRight.x : m_textInfo.characterInfo[i].bottomLeft.x, underlineBaseLine, 0);
underlineColor = m_textInfo.characterInfo[i].underlineColor;
}
}
Expand All @@ -5064,7 +5064,7 @@ protected virtual void GenerateTextMesh()
if (beginUnderline && m_characterCount == 1)
{
beginUnderline = false;
underline_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
underline_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
underlineEndScale = m_textInfo.characterInfo[i].scale;

DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
Expand All @@ -5078,12 +5078,12 @@ protected virtual void GenerateTextMesh()
if (isWhiteSpace || unicode == 0x200B)
{
int lastVisibleCharacterIndex = lineInfo.lastVisibleCharacterIndex;
underline_end = new Vector3(m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, underlineBaseLine, 0);
underline_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[lastVisibleCharacterIndex].topLeft.x : m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, underlineBaseLine, 0);
underlineEndScale = m_textInfo.characterInfo[lastVisibleCharacterIndex].scale;
}
else
{ // End underline if last character of the line.
underline_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
underline_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
underlineEndScale = m_textInfo.characterInfo[i].scale;
}

Expand All @@ -5096,7 +5096,7 @@ protected virtual void GenerateTextMesh()
else if (beginUnderline && !isUnderlineVisible)
{
beginUnderline = false;
underline_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, underlineBaseLine, 0);
underline_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i - 1].topLeft.x : m_textInfo.characterInfo[i - 1].topRight.x, underlineBaseLine, 0);
underlineEndScale = m_textInfo.characterInfo[i - 1].scale;

DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
Expand All @@ -5108,7 +5108,7 @@ protected virtual void GenerateTextMesh()
{
// End underline if underline color has changed.
beginUnderline = false;
underline_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
underline_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, underlineBaseLine, 0);
underlineEndScale = m_textInfo.characterInfo[i].scale;

DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
Expand All @@ -5123,7 +5123,7 @@ protected virtual void GenerateTextMesh()
if (beginUnderline == true)
{
beginUnderline = false;
underline_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, underlineBaseLine, 0);
underline_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i - 1].topLeft.x : m_textInfo.characterInfo[i - 1].topRight.x, underlineBaseLine, 0);
underlineEndScale = m_textInfo.characterInfo[i - 1].scale;

DrawUnderlineMesh(underline_start, underline_end, ref last_vert_index, underlineStartScale, underlineEndScale, underlineMaxScale, xScaleMax, underlineColor);
Expand Down Expand Up @@ -5158,7 +5158,7 @@ protected virtual void GenerateTextMesh()
beginStrikethrough = true;
strikethroughPointSize = m_textInfo.characterInfo[i].pointSize;
strikethroughScale = m_textInfo.characterInfo[i].scale;
strikethrough_start = new Vector3(m_textInfo.characterInfo[i].bottomLeft.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_start = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].bottomRight.x : m_textInfo.characterInfo[i].bottomLeft.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethroughColor = m_textInfo.characterInfo[i].strikethroughColor;
strikethroughBaseline = m_textInfo.characterInfo[i].baseLine;
//Debug.Log("Char [" + currentCharacter + "] Start Strikethrough POS: " + strikethrough_start);
Expand All @@ -5169,7 +5169,7 @@ protected virtual void GenerateTextMesh()
if (beginStrikethrough && m_characterCount == 1)
{
beginStrikethrough = false;
strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);

DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
}
Expand All @@ -5179,16 +5179,17 @@ protected virtual void GenerateTextMesh()
if (isWhiteSpace || unicode == 0x200B)
{
int lastVisibleCharacterIndex = lineInfo.lastVisibleCharacterIndex;
strikethrough_end = new Vector3(m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, m_textInfo.characterInfo[lastVisibleCharacterIndex].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[lastVisibleCharacterIndex].topLeft.x : m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, m_textInfo.characterInfo[lastVisibleCharacterIndex].baseLine + strikethroughOffset * strikethroughScale, 0);
}
else
{
// Terminate Strikethrough at last character of line.
strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
}

beginStrikethrough = false;
DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);

}
else if (beginStrikethrough && i < m_characterCount && (m_textInfo.characterInfo[i + 1].pointSize != strikethroughPointSize || !TMP_Math.Approximately(m_textInfo.characterInfo[i + 1].baseLine + offset.y, strikethroughBaseline)))
{
Expand All @@ -5197,9 +5198,9 @@ protected virtual void GenerateTextMesh()

int lastVisibleCharacterIndex = lineInfo.lastVisibleCharacterIndex;
if (i > lastVisibleCharacterIndex)
strikethrough_end = new Vector3(m_textInfo.characterInfo[lastVisibleCharacterIndex].topRight.x, m_textInfo.characterInfo[lastVisibleCharacterIndex].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[lastVisibleCharacterIndex].topLeft.x : m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[lastVisibleCharacterIndex].baseLine + strikethroughOffset * strikethroughScale, 0);
else
strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);

DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
//Debug.Log("Char [" + currentCharacter + "] at Index: " + i + " End Strikethrough POS: " + strikethrough_end + " Baseline: " + m_textInfo.characterInfo[i].baseLine.ToString("f3"));
Expand All @@ -5208,26 +5209,27 @@ protected virtual void GenerateTextMesh()
{
// Terminate Strikethrough if font asset changes.
beginStrikethrough = false;
strikethrough_end = new Vector3(m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i].topLeft.x : m_textInfo.characterInfo[i].topRight.x, m_textInfo.characterInfo[i].baseLine + strikethroughOffset * strikethroughScale, 0);

DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
}
else if (beginStrikethrough && !isStrikeThroughVisible)
{
// Terminate Strikethrough if character is not visible.
beginStrikethrough = false;
strikethrough_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, m_textInfo.characterInfo[i - 1].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i - 1].topLeft.x : m_textInfo.characterInfo[i - 1].topRight.x, m_textInfo.characterInfo[i - 1].baseLine + strikethroughOffset * strikethroughScale, 0);

DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
}

}
else
{
// End Strikethrough
if (beginStrikethrough == true)
{
beginStrikethrough = false;
strikethrough_end = new Vector3(m_textInfo.characterInfo[i - 1].topRight.x, m_textInfo.characterInfo[i - 1].baseLine + strikethroughOffset * strikethroughScale, 0);
strikethrough_end = new Vector3(isRightToLeftText ? m_textInfo.characterInfo[i - 1].topLeft.x : m_textInfo.characterInfo[i - 1].topRight.x, m_textInfo.characterInfo[i - 1].baseLine + strikethroughOffset * strikethroughScale, 0);

DrawUnderlineMesh(strikethrough_start, strikethrough_end, ref last_vert_index, strikethroughScale, strikethroughScale, strikethroughScale, xScale, strikethroughColor);
}
Expand Down