Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit d2443a2

Browse files
author
Yuncong Zhang
committed
Fix editable trailing space problem.
1 parent 62b9d3f commit d2443a2

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

Runtime/ui/txt/layout.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,17 @@ public static int computeTruncateCount(float offset, string text, int start, int
8080
return 0;
8181
}
8282

83-
public static void computeCharWidths(string text, int start, int count, TextStyle style, float[] advances,
84-
int advanceOffset) {
83+
public static float computeCharWidths(float offset, string text, int start, int count, TextStyle style,
84+
float[] advances, int advanceOffset, TabStops tabStops) {
8585
char startingChar = text[start];
86+
float totalWidths = 0;
8687
if (char.IsHighSurrogate(startingChar) || EmojiUtils.isSingleCharEmoji(startingChar)) {
8788
float advance = style.fontSize + style.letterSpacing;
8889
for (int i = 0; i < count; i++) {
8990
char ch = text[start + i];
9091
if (char.IsHighSurrogate(ch) || EmojiUtils.isSingleCharNonEmptyEmoji(ch)) {
9192
advances[i + advanceOffset] = advance;
93+
totalWidths += advance;
9294
}
9395
else {
9496
advances[i + advanceOffset] = 0;
@@ -101,7 +103,10 @@ public static void computeCharWidths(string text, int start, int count, TextStyl
101103
font.RequestCharactersInTextureSafe(text, style.UnityFontSize, style.UnityFontStyle);
102104
for (int i = 0; i < count; i++) {
103105
char ch = text[start + i];
104-
if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle)) {
106+
if (ch == '\t') {
107+
advances[i + advanceOffset] = tabStops.nextTab(offset + totalWidths) - (offset + totalWidths);
108+
}
109+
else if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle)) {
105110
advances[i + advanceOffset] = glyphInfo.advance + style.letterSpacing;
106111
}
107112
else {
@@ -111,8 +116,12 @@ public static void computeCharWidths(string text, int start, int count, TextStyl
111116
if (LayoutUtils.isWordSpace(ch)) {
112117
advances[i + advanceOffset] += style.wordSpacing;
113118
}
119+
120+
totalWidths += advances[i + advanceOffset];
114121
}
115122
}
123+
124+
return totalWidths;
116125
}
117126

118127
public static float doLayout(float offset, string text, int start, int count, TextStyle style,

Runtime/ui/txt/linebreaker.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,15 @@ public void setLineWidth(float lineWidth) {
174174
this._lineWidth = lineWidth;
175175
}
176176

177-
public void addStyleRun(TextStyle style, int start, int end) {
177+
public float addStyleRun(TextStyle style, int start, int end) {
178+
float width = 0;
178179
if (style != null) {
179180
// Layout.measureText(this._width - this._preBreak, this._textBuf,
180181
// start, end - start, style,
181182
// this._charWidths, start, this._tabStops);
182-
Layout.computeCharWidths(this._textBuf.text, this._textBuf.offset + start, end - start, style,
183-
this._charWidths, start);
183+
width = Layout.computeCharWidths(this._width - this._preBreak, this._textBuf.text,
184+
this._textBuf.offset + start, end - start, style,
185+
this._charWidths, start, this._tabStops);
184186
}
185187

186188
int current = this._wordBreaker.current();
@@ -215,6 +217,8 @@ public void addStyleRun(TextStyle style, int start, int end) {
215217
current = this._wordBreaker.next();
216218
}
217219
}
220+
221+
return width;
218222
}
219223

220224
public void finish() {

Runtime/ui/txt/paragraph.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -580,22 +580,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
580580

581581
this._lineCount = lineLimit;
582582

583-
// Compute max intrinsic width and min intrinsic width
584-
// max intrinsic width := maximum width this paragraph could possibly expand, without any constraints,
585-
// which equals the length of the maximum hard-break line
586-
this._maxIntrinsicWidth = 0;
587-
float lineBlockWidth = 0;
588-
for (int i = 0; i < this._lineWidthCount; ++i) {
589-
lineBlockWidth += this._lineWidths[i];
590-
if (this._lineRanges[i].hardBreak) {
591-
this._maxIntrinsicWidth = Mathf.Max(lineBlockWidth, this._maxIntrinsicWidth);
592-
lineBlockWidth = 0;
593-
}
594-
}
595-
596583
// min intrinsic width := minimum width this paragraph has to take, which equals the maximum word width
597-
this._maxIntrinsicWidth = Mathf.Max(lineBlockWidth, this._maxIntrinsicWidth);
598-
599584
if (this._paragraphStyle.maxLines == 1 || (this._paragraphStyle.maxLines == null &&
600585
this._paragraphStyle.ellipsized())) {
601586
this._minIntrinsicWidth = this.maxIntrinsicWidth;
@@ -906,6 +891,7 @@ void _resetLineBreaker(LineBreaker lineBreaker, int blockStart, int blockSize, i
906891

907892
int _addStyleRuns(LineBreaker lineBreaker, ref int runIndex, int blockStart, int blockEnd) {
908893
int countRuns = 0;
894+
float lineBlockWidth = 0;
909895
while (runIndex < this._runs.size) {
910896
var run = this._runs.getRun(runIndex);
911897
if (run.start >= blockEnd) {
@@ -923,7 +909,8 @@ int _addStyleRuns(LineBreaker lineBreaker, ref int runIndex, int blockStart, int
923909

924910
int runStart = Mathf.Max(run.start, blockStart) - blockStart;
925911
int runEnd = Mathf.Min(run.end, blockEnd) - blockStart;
926-
lineBreaker.addStyleRun(run.style, runStart, runEnd);
912+
lineBlockWidth += lineBreaker.addStyleRun(run.style, runStart, runEnd);
913+
927914
countRuns++;
928915

929916
if (run.end > blockEnd) {
@@ -932,6 +919,7 @@ int _addStyleRuns(LineBreaker lineBreaker, ref int runIndex, int blockStart, int
932919

933920
runIndex++;
934921
}
922+
this._maxIntrinsicWidth = Mathf.Max(lineBlockWidth, this._maxIntrinsicWidth);
935923

936924
return countRuns;
937925
}

0 commit comments

Comments
 (0)