This commit is contained in:
2026-11-24 15:51:02 +01:00
parent cca4d73e41
commit f4aa17114a
22092 changed files with 5042406 additions and 0 deletions
@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Unity.CodeEditor.Rendering;
using UnityEngine;
namespace Unity.CodeEditor.Text
{
internal class TextFormatter
{
internal static TextFormatter Current = new TextFormatter();
internal interface ITextSource
{
TextRun GetTextRun(int textOffset);
}
internal Vector2 CalcSize(string text, TextParagraphProperties paragraphProperties)
{
GUIContent guiContent = new GUIContent(text);
return paragraphProperties.GUIStyle.CalcSize(guiContent);
}
internal TextLine FormatLine(
ITextSource textSource,
int textOffset,
float availableWidth,
TextParagraphProperties paragraphProperties)
{
List<TextRun> textRuns = FetchTextRuns(textSource, availableWidth, paragraphProperties, textOffset);
StringBuilder lineText = new StringBuilder();
StringBuilder markup = new StringBuilder();
foreach (TextRun textRun in textRuns)
{
if (textRun is TextEndOfParagraph)
continue;
lineText.Append(textRun.Text);
markup.Append("<color=#");
markup.Append(ColorUtility.ToHtmlStringRGB(textRun.TextParagraphProperties.GUIStyle.normal.textColor));
markup.Append(">");
markup.Append(Escape(textRun.Text));
markup.Append("</color>");
}
GUIContent markupGuiContent = new GUIContent(markup.ToString());
GUIContent plainGuiContent = new GUIContent(lineText.ToString());
float height = paragraphProperties.GUIStyle.CalcHeight(plainGuiContent, availableWidth);
Vector2 lineSize = paragraphProperties.GUIStyle.CalcSize(plainGuiContent);
Rect lineRect = new Rect(0, 0, paragraphProperties.TextWrapping ? Math.Min(lineSize.x, availableWidth) : lineSize.x, height);
TextLine result = new TextLine(
textRuns.ToArray(),
textRuns.Aggregate(0, (acc, run) => acc + run.Length),
markupGuiContent,
plainGuiContent,
height,
lineRect,
paragraphProperties,
textOffset);
return result;
}
List<TextRun> FetchTextRuns(
ITextSource textSource,
float availableWidth,
TextParagraphProperties paragraphProperties,
int textOffset)
{
List<TextRun> result = new List<TextRun>();
float accumulatedWidth = 0;
while (true)
{
TextRun textRun = textSource.GetTextRun(textOffset);
if (paragraphProperties.TextWrapping && textRun.Text != null)
{
float textRunWidth = CalcSize(textRun.Text, textRun.TextParagraphProperties).x;
if (accumulatedWidth + textRunWidth > availableWidth)
{
if (textRun is TabTextRun)
{
result.Add(textRun);
return result;
}
if (!textRun.Text.Contains(' '))
{
if (result.Count == 0)
{
if (PerformCharacterWrap(availableWidth, textRun.Text, textRun.TextParagraphProperties, accumulatedWidth, result, ref result))
return result;
}
return result;
}
else
{
StringBuilder wordBuffer = new StringBuilder();
StringBuilder currentWordBuffer = new StringBuilder();
for (int i = 0; i < textRun.Text.Length; i++)
{
currentWordBuffer.Append(textRun.Text[i]);
if (!IsWordSeparator(textRun.Text[i]) && i < textRun.Text.Length - 1)
continue;
string currentWord = currentWordBuffer.ToString();
float wordWidth = CalcSize(currentWord, textRun.TextParagraphProperties).x;
if (accumulatedWidth + wordWidth > availableWidth)
{
if (wordBuffer.Length == 0)
{
if (PerformCharacterWrap(availableWidth, currentWord, textRun.TextParagraphProperties, accumulatedWidth, result, ref result))
return result;
}
result.Add(new StringTextRun(wordBuffer.ToString(), textRun.TextParagraphProperties));
return result;
}
wordBuffer.Append(currentWordBuffer);
accumulatedWidth += wordWidth;
currentWordBuffer.Clear();
}
}
}
accumulatedWidth += textRunWidth;
}
result.Add(textRun);
textOffset += textRun.Length;
if (textRun is TextEndOfParagraph)
break;
}
return result;
}
bool PerformCharacterWrap(
float availableWidth,
string text,
TextParagraphProperties p,
float accumulatedWidth,
List<TextRun> result,
ref List<TextRun> textRuns)
{
StringBuilder charBuffer = new StringBuilder();
foreach (char c in text)
{
float charWidth = CalcSize(c.ToString(), p).x;
if (accumulatedWidth + charWidth > availableWidth)
{
if (charBuffer.Length == 0)
{
// no content, at least add the current char
result.Add(new StringTextRun(c.ToString(), p));
{
textRuns = result;
return true;
}
}
result.Add(new StringTextRun(charBuffer.ToString(), p));
{
textRuns = result;
return true;
}
}
charBuffer.Append(c);
accumulatedWidth += charWidth;
}
return false;
}
private static bool IsWordSeparator(char c)
{
return char.IsWhiteSpace(c) || char.IsPunctuation(c);
}
private string Escape(string text)
{
if (text == null)
return string.Empty;
return text.Replace("<", "<\u200B");
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6ff43a353ef749139882e055ebd32a3d
timeCreated: 1710781497
@@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.CodeEditor.Rendering;
using Unity.CodeEditor.Text;
using UnityEngine;
namespace Unity.CodeEditor.Text
{
/// <summary>
/// Represents a line of text that is used for text rendering.
/// </summary>
internal class TextLine : IDisposable
{
private readonly TextRun[] _textRuns;
private int _initialTextLineOffset;
internal TextLine(
TextRun[] textRuns,
int length,
GUIContent markupGUIContent,
GUIContent plainGuiContent,
float height,
Rect lineRect,
TextParagraphProperties paragraphProperties,
int initialTextLineOffset)
{
_textRuns = textRuns;
_initialTextLineOffset = initialTextLineOffset;
Length = length;
MarkupGUIContent = markupGUIContent;
PlainGUIContent = plainGuiContent;
TextParagraphProperties = paragraphProperties;
Height = Mathf.Round(height + paragraphProperties.Leading);
LineRect = lineRect;
}
/// <summary>
/// Gets the text runs that are contained within a line.
/// </summary>
/// <value>
/// The contained text runs.
/// </value>
internal IReadOnlyList<TextRun> TextRuns => _textRuns;
/// <summary>
/// Gets the total number of TextSource positions of the current line.
/// </summary>
internal int Length { get; }
internal TextParagraphProperties TextParagraphProperties { get; }
internal GUIContent MarkupGUIContent { get; }
internal GUIContent PlainGUIContent { get; }
internal string MarkupText => MarkupGUIContent?.text ?? string.Empty;
/// <summary>
/// Gets the height of a line of text.
/// </summary>
/// <returns>
/// The text line height.
/// </returns>
internal float Height { get; }
internal Rect LineRect { get; }
/// <summary>
/// Gets the width of a line of text, excluding trailing whitespace characters.
/// </summary>
/// <returns>
/// The text line width, excluding trailing whitespace characters.
/// </returns>
internal float Width => LineRect.width;
/// <summary>
/// Gets the width of a line of text, including trailing whitespace characters.
/// </summary>
/// <returns>
/// The text line width, including trailing whitespace characters.
/// </returns>
internal float WidthIncludingTrailingWhitespace => LineRect.width;
internal int TrailingWhitespaceLength { get; }
/// <summary>
/// Gets the character hit corresponding to the specified distance from the beginning of the line.
/// </summary>
/// <param name="distance">A <see cref="double"/> value that represents the distance from the beginning of the line.</param>
/// <returns>The <see cref="CharacterHit"/> object at the specified distance from the beginning of the line.</returns>
internal CharacterHit GetCharacterHitFromDistance(float distance)
{
int index = TextParagraphProperties.GUIStyle.GetCursorStringIndex(LineRect, PlainGUIContent, new Vector2(distance, 0));
return new CharacterHit(index + _initialTextLineOffset, 0);
}
/// <summary>
/// Gets the distance from the beginning of the line to the specified character hit.
/// <see cref="CharacterHit"/>.
/// </summary>
/// <param name="characterHit">The <see cref="CharacterHit"/> object whose distance you want to query.</param>
/// <returns>A <see cref="double"/> that represents the distance from the beginning of the line.</returns>
internal float GetDistanceFromCharacterHit(CharacterHit characterHit)
{
return TextParagraphProperties.GUIStyle.GetCursorPixelPosition(LineRect, PlainGUIContent, characterHit.FirstCharacterIndex - _initialTextLineOffset).x;
}
/// <summary>
/// Gets the next character hit for caret navigation.
/// </summary>
/// <param name="characterHit">The current <see cref="CharacterHit"/>.</param>
/// <returns>The next <see cref="CharacterHit"/>.</returns>
internal CharacterHit GetNextCaretCharacterHit(CharacterHit characterHit)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the previous character hit for caret navigation.
/// </summary>
/// <param name="characterHit">The current <see cref="CharacterHit"/>.</param>
/// <returns>The previous <see cref="CharacterHit"/>.</returns>
internal CharacterHit GetPreviousCaretCharacterHit(CharacterHit characterHit)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets the previous character hit after backspacing.
/// </summary>
/// <param name="characterHit">The current <see cref="CharacterHit"/>.</param>
/// <returns>The <see cref="CharacterHit"/> after backspacing.</returns>
internal CharacterHit GetBackspaceCaretCharacterHit(CharacterHit characterHit)
{
throw new NotImplementedException();
}
/// <summary>
/// Get an array of bounding rectangles of a range of characters within a text line.
/// </summary>
/// <param name="firstTextSourceCharacterIndex">index of first character of specified range</param>
/// <param name="textLength">number of characters of the specified range</param>
/// <returns>an array of bounding rectangles.</returns>
internal IReadOnlyList<Rect> GetTextBounds(int firstTextSourceCharacterIndex, int textLength)
{
Vector2 iniPoint = TextParagraphProperties.GUIStyle.GetCursorPixelPosition(LineRect, PlainGUIContent, firstTextSourceCharacterIndex - _initialTextLineOffset);
Vector2 endPoint = TextParagraphProperties.GUIStyle.GetCursorPixelPosition(LineRect, PlainGUIContent, firstTextSourceCharacterIndex + textLength - _initialTextLineOffset);
return new List<Rect>() { new Rect(iniPoint.x, iniPoint.y, endPoint.x - iniPoint.x, endPoint.y - iniPoint.y) };
}
public void Dispose()
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 54e481e5c7bd459795071ffd9879c1f6
timeCreated: 1710777783
@@ -0,0 +1,98 @@
using System;
using System.Diagnostics;
using Unity.CodeEditor.Rendering;
using Unity.CodeEditor.Utils;
using UnityEngine;
namespace Unity.CodeEditor.Text
{
internal abstract class TextRun
{
internal const int DefaultTextSourceLength = 1;
/// <summary>
/// Gets the text source length.
/// </summary>
internal virtual int Length => DefaultTextSourceLength;
/// <summary>
/// Gets the text run's text.
/// </summary>
internal virtual string Text => default;
internal TextParagraphProperties TextParagraphProperties { get; }
internal TextRun(TextParagraphProperties properties)
{
TextParagraphProperties = properties;
}
}
internal class StringTextRun : TextRun
{
private string _contents;
internal StringTextRun(string contents, TextParagraphProperties properties) : base(properties)
{
_contents = contents;
}
internal override int Length => _contents.Length;
internal override string Text => _contents;
}
internal class TabTextRun : StringTextRun
{
internal TabTextRun(string tabString, TextParagraphProperties properties) : base(tabString, properties)
{
}
}
internal class StringSegmentTextRun : TextRun
{
private StringSegment _stringSegment;
internal StringSegmentTextRun(StringSegment stringSegment, TextParagraphProperties properties) : base(properties)
{
_stringSegment = stringSegment;
}
internal override int Length => _stringSegment.Count;
internal override string Text => _stringSegment.Text.Substring(_stringSegment.Offset, _stringSegment.Count);
}
internal class TextEndOfLine : TextRun
{
internal TextEndOfLine(int textSourceLength, TextParagraphProperties properties) : base(properties)
{
Length = textSourceLength;
}
internal override int Length { get; }
}
internal class TextEndOfParagraph : TextEndOfLine
{
internal TextEndOfParagraph(TextParagraphProperties properties) : base(DefaultTextSourceLength, properties)
{
}
internal TextEndOfParagraph(int textSourceLength, TextParagraphProperties properties) : base(textSourceLength, properties)
{
}
}
internal class DrawableTextRun : StringTextRun
{
internal DrawableTextRun(string text, TextParagraphProperties properties) : base(text, properties)
{
}
internal virtual void Draw(Vector2 origin)
{
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3caade91a0f84e08b049f6bdff64becc
timeCreated: 1710779063