浏览代码

Modify measureText not to use _doLayout.

/main
Yuncong Zhang 6 年前
当前提交
b894bcc6
共有 2 个文件被更改,包括 41 次插入9 次删除
  1. 42
      Runtime/ui/txt/layout.cs
  2. 8
      Runtime/ui/txt/paragraph.cs

42
Runtime/ui/txt/layout.cs


namespace Unity.UIWidgets.ui {
static class Layout {
public static float measureText(float offset, string text, int start, int count, TextStyle style,
float[] advances, int advanceOffset, TabStops tabStops) {
return _doLayout(offset, text, start, count, style, advances, null, advanceOffset, tabStops, out var bounds);
public static float measureText(string text, int start, int count, TextStyle style) {
char startingChar = text[start];
float totalWidth = 0;
if (char.IsHighSurrogate(startingChar) || EmojiUtils.isSingleCharEmoji(startingChar)) {
float advance = style.fontSize + style.letterSpacing;
for (int i = 0; i < count; i++) {
char ch = text[start + i];
if (char.IsHighSurrogate(ch) || EmojiUtils.isSingleCharNonEmptyEmoji(ch)) {
totalWidth += advance;
}
}
}
else {
Font font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
font.RequestCharactersInTextureSafe(text.Substring(start, count), style.UnityFontSize, style.UnityFontStyle);
for (int i = 0; i < count; i++) {
char ch = text[start + i];
if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle)) {
totalWidth += glyphInfo.advance + style.letterSpacing;
}
else {
totalWidth += style.letterSpacing;
}
if (LayoutUtils.isWordSpace(ch)) {
totalWidth += style.wordSpacing;
}
}
}
return totalWidth;
}
public static int computeTruncateCount(string text, int start, int count, TextStyle style, float advanceLimit) {

currentAdvance = style.letterSpacing;
}
if (LayoutUtils.isWordSpace(ch)) currentAdvance += style.wordSpacing;
if (LayoutUtils.isWordSpace(ch)) {
currentAdvance += style.wordSpacing;
}
if (currentAdvance > advanceLimit) {
return count - i;
}

advances[i + advanceOffset] = style.letterSpacing;
}
if (LayoutUtils.isWordSpace(ch)) advances[i + advanceOffset] += style.wordSpacing;
if (LayoutUtils.isWordSpace(ch)) {
advances[i + advanceOffset] += style.wordSpacing;
}
}
}
}

8
Runtime/ui/txt/paragraph.cs


// By now, all characters have been "RequestCharactersInTexture"d by computeLineBreaks
// except the ellipsis, so Layout.doLayout skips calling RequestCharactersInTexture for
// performance, and the ellipsis is handled here
Layout.requireEllipsisInTexture(this._paragraphStyle.ellipsis, style);
string ellipsis = this._paragraphStyle.ellipsis;
Layout.requireEllipsisInTexture(ellipsis, style);
float ellipsisWidth = Layout.measureText(runXOffset, this._paragraphStyle.ellipsis, 0,
this._paragraphStyle.ellipsis.Length, style, null, 0, this._tabStops);
float ellipsisWidth = Layout.measureText(ellipsis, 0, ellipsis.Length, style);
// Find the minimum number of characters to truncate, so that the truncated text appended with ellipsis
// is within the constraints of line width

text = text.Substring(0, textStart + textCount - truncateCount) + this._paragraphStyle.ellipsis;
text = text.Substring(0, textStart + textCount - truncateCount) + ellipsis;
textCount = text.Length - textStart;
}

正在加载...
取消
保存