浏览代码

Implement computeTruncatedCount.

/main
Yuncong Zhang 5 年前
当前提交
6464abc0
共有 2 个文件被更改,包括 38 次插入9 次删除
  1. 36
      Runtime/ui/txt/layout.cs
  2. 11
      Runtime/ui/txt/paragraph.cs

36
Runtime/ui/txt/layout.cs


float[] advances, int advanceOffset, TabStops tabStops) {
return _doLayout(offset, text, start, count, style, advances, null, advanceOffset, tabStops);
}
public static int computeTruncateCount(string text, int start, int count, TextStyle style, float advanceLimit) {
char startingChar = text[start];
float currentAdvance = 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)) {
currentAdvance += advance;
if (currentAdvance > advanceLimit) {
return count - i;
}
}
}
}
else {
Font font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
for (int i = 0; i < count; i++) {
char ch = text[start + i];
if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle)) {
currentAdvance += glyphInfo.advance + style.letterSpacing;
}
else {
currentAdvance = style.letterSpacing;
}
if (LayoutUtils.isWordSpace(ch)) currentAdvance += style.wordSpacing;
if (currentAdvance > advanceLimit) {
return count - i;
}
}
}
return 0;
}
public void doLayout(float offset, string text, int start, int count, TextStyle style, TabStops tabStops) {
this._count = count;

11
Runtime/ui/txt/paragraph.cs


float ellipsisWidth = Layout.measureText(runXOffset, this._paragraphStyle.ellipsis, 0,
this._paragraphStyle.ellipsis.Length, style, null, 0, this._tabStops);
var textAdvances = new float[textCount];
float textWidth = Layout.measureText(runXOffset, text, textStart, textCount, style,
textAdvances, 0, this._tabStops);
int truncateCount = 0;
while (truncateCount < textCount && runXOffset + textWidth + ellipsisWidth > this._width) {
textWidth -= textAdvances[textCount - truncateCount - 1];
truncateCount++;
}
int truncateCount =
Layout.computeTruncateCount(text, textStart, textCount, style, this._width - ellipsisWidth);
text = text.Substring(0, textStart + textCount - truncateCount) + this._paragraphStyle.ellipsis;
textCount = text.Length - textStart;

正在加载...
取消
保存