void BuildLines()
{
_textChanged = false;
_requireUpdateMesh = true;
_renderScale = UIContentScaler.scaleFactor;
Cleanup();
bool html = false;
if (_alternativeText != null)
{
if (_alternativeText.Length > 0)
{
html = _alternatvieHtml;
if (_alternatvieHtml)
HtmlParser.inst.Parse(_alternativeText, _textFormat, _elements,
_richTextField != null ? _richTextField.htmlParseOptions : null);
else
{
HtmlElement element = HtmlElement.GetElement(HtmlElementType.Text);
element.text = _alternativeText;
element.format.CopyFrom(_textFormat);
_elements.Add(element);
}
}
}
else
{
if (_text.Length > 0)
{
html = _html;
if (_html)
HtmlParser.inst.Parse(_text, _textFormat, _elements,
_richTextField != null ? _richTextField.htmlParseOptions : null);
else
{
HtmlElement element = HtmlElement.GetElement(HtmlElementType.Text);
element.text = _text;
element.format.CopyFrom(_textFormat);
_elements.Add(element);
}
}
}
if (_elements.Count == 0)
{
LineInfo emptyLine = LineInfo.Borrow();
emptyLine.width = 0;
emptyLine.height = 0;
emptyLine.text = string.Empty;
emptyLine.y = emptyLine.y2 = GUTTER_Y;
_lines.Add(emptyLine);
_textWidth = 0;
_textHeight = 0;
_fontSizeScale = 1;
BuildLinesFinal();
return;
}
if (_richTextField != null && _richTextField.emojies != null)
{
html = true;
HandleEmojies();
}
int letterSpacing = _textFormat.letterSpacing;
int lineSpacing = _textFormat.lineSpacing - 1;
float rectWidth = _contentRect.width - GUTTER_X * 2;
float lineWidth = 0, lineHeight = 0, lineTextHeight = 0;
float glyphWidth = 0, glyphHeight = 0;
int wordChars = 0;
float wordStart = 0;
bool wordPossible = false;
float lastLineHeight = 0;
TextFormat format = _textFormat;
_font.SetFormat(format, _fontSizeScale);
bool wrap;
if (_input)
{
letterSpacing++;
wrap = !_singleLine;
}
else
wrap = _wordWrap && !_singleLine;
float lineY = GUTTER_Y;
_fontSizeScale = 1;
RequestText();
LineInfo line;
StringBuilder lineBuffer = new StringBuilder();
int count = _elements.Count;
for (int i = 0; i < count; i++)
{
HtmlElement element = _elements[i];
if (html)
{
//Special tag, indicates the start of an element
lineBuffer.Append(E_TAG);
lineBuffer.Append((char)(i + 33));
}
if (element.type == HtmlElementType.Text)
{
format = element.format;
_font.SetFormat(format, _fontSizeScale);
int textLength = element.text.Length;
for (int offset = 0; offset < textLength; ++offset)
{
char ch = element.text[offset];
if (ch == E_TAG)
ch = '?';
if (ch == '\r')
{
if (offset != textLength - 1 && element.text[offset + 1] == '\n')
continue;
ch = '\n';
}
if (ch == '\n')
{
lineBuffer.Append(ch);
line = LineInfo.Borrow();
line.width = lineWidth;
if (lineTextHeight == 0)
{
if (lastLineHeight == 0)
lastLineHeight = format.size;
if (lineHeight == 0)
lineHeight = lastLineHeight;
lineTextHeight = lineHeight;
}
line.height = lineHeight;
lastLineHeight = lineHeight;
line.textHeight = lineTextHeight;
line.text = lineBuffer.ToString();
line.y = line.y2 = lineY;
lineY += (line.height + lineSpacing);
if (line.width > _textWidth)
_textWidth = line.width;
_lines.Add(line);
lineBuffer.Length = 0;
lineWidth = 0;
lineHeight = 0;
lineTextHeight = 0;
wordChars = 0;
continue;
}
if (ch == ' ')
{
wordChars = 0;
wordPossible = true;
}
else if (wordPossible && (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'))
{
if (wordChars == 0)
wordStart = lineWidth;
else if (wordChars > 10)
wordChars = int.MinValue;
wordChars++;
}
else
{
wordChars = 0;
wordPossible = false;
}
if (_font.GetGlyphSize(ch, out glyphWidth, out glyphHeight))
{
if (glyphHeight > lineTextHeight)
lineTextHeight = glyphHeight;
if (glyphHeight > lineHeight)
lineHeight = glyphHeight;
if (lineWidth != 0)
lineWidth += letterSpacing;
lineWidth += glyphWidth;
}
if (!wrap || lineWidth <= rectWidth)
{
lineBuffer.Append(ch);
}
else
{
line = LineInfo.Borrow();
line.height = lineHeight;
line.textHeight = lineTextHeight;
if (lineBuffer.Length == 0) //the line cannt fit even a char
{
line.text = ch.ToString();
wordChars = 0;
wordPossible = false;
}
else if (wordChars > 0 && wordStart > 0) //if word had broken, move it to new line
{
lineBuffer.Append(ch);
int len = lineBuffer.Length - wordChars;
line.text = lineBuffer.ToString(0, len);
line.width = wordStart;
lineBuffer.Remove(0, len);
lineWidth -= wordStart;
wordStart = 0;
}
else
{
line.text = lineBuffer.ToString();
line.width = lineWidth - (glyphWidth + letterSpacing);
lineBuffer.Length = 0;
lineBuffer.Append(ch);
lineWidth = glyphWidth;
lineHeight = glyphHeight;
lineTextHeight = glyphHeight;
wordChars = 0;
wordPossible = false;
}
line.y = line.y2 = lineY;
lineY += (line.height + lineSpacing);
if (line.width > _textWidth)
_textWidth = line.width;
_lines.Add(line);
}
}
}
else
{
wordChars = 0;
wordPossible = false;
IHtmlObject htmlObject = null;
if (_richTextField != null)
{
element.space = (int)(rectWidth - lineWidth - 4);
htmlObject = _richTextField.htmlPageContext.CreateObject(_richTextField, element);
element.htmlObject = htmlObject;
}
if (htmlObject != null)
{
glyphWidth = (int)htmlObject.width;
glyphHeight = (int)htmlObject.height;
if (glyphWidth == 0)
continue;
glyphWidth += 2;
}
else
continue;
if (glyphHeight > lineHeight)
lineHeight = glyphHeight;
if (lineWidth != 0)
lineWidth += letterSpacing;
lineWidth += glyphWidth;
if (wrap && lineWidth > rectWidth && glyphWidth < rectWidth)
{
line = LineInfo.Borrow();
line.height = lineHeight;
line.textHeight = lineTextHeight;
int len = lineBuffer.Length;
line.text = lineBuffer.ToString(0, len - 2);
line.width = lineWidth - (glyphWidth + letterSpacing);
lineBuffer.Remove(0, len - 2);
lineWidth = glyphWidth;
line.y = line.y2 = lineY;
lineY += (line.height + lineSpacing);
if (line.width > _textWidth)
_textWidth = line.width;
lineTextHeight = 0;
lineHeight = glyphHeight;
wordChars = 0;
wordPossible = false;
_lines.Add(line);
}
}
}
if (lineWidth > 0 || _lines.Count == 0 || _lines[_lines.Count - 1].text.EndsWith("\n"))
{
line = LineInfo.Borrow();
line.width = lineWidth;
if (lineHeight == 0)
lineHeight = lastLineHeight;
if (lineTextHeight == 0)
lineTextHeight = lineHeight;
line.height = lineHeight;
line.textHeight = lineTextHeight;
line.text = lineBuffer.ToString();
line.y = line.y2 = lineY;
if (line.width > _textWidth)
_textWidth = line.width;
_lines.Add(line);
}
if (_textWidth > 0)
_textWidth += GUTTER_X * 2;
line = _lines[_lines.Count - 1];
_textHeight = line.y + line.height + GUTTER_Y;
_textWidth = Mathf.CeilToInt(_textWidth);
_textHeight = Mathf.CeilToInt(_textHeight);
if (_autoSize == AutoSizeType.Shrink && _textWidth > rectWidth)
{
_fontSizeScale = rectWidth / _textWidth;
_textWidth = rectWidth;
_textHeight = Mathf.CeilToInt(_textHeight * _fontSizeScale);
//调整各行的大小
int lineCount = _lines.Count;
for (int i = 0; i < lineCount; ++i)
{
line = _lines[i];
line.y *= _fontSizeScale;
line.y2 *= _fontSizeScale;
line.height *= _fontSizeScale;
line.width *= _fontSizeScale;
line.textHeight *= _fontSizeScale;
}
}
else
_fontSizeScale = 1;
BuildLinesFinal();
}