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

Commit aa25531

Browse files
author
Yuncong Zhang
committed
Reuse all lists in linebreaker, remove lineBaseLines.
1 parent b4e62ce commit aa25531

File tree

2 files changed

+61
-30
lines changed

2 files changed

+61
-30
lines changed

Runtime/ui/txt/linebreaker.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public static int[] newLinePositions(string text, out int count) {
108108
TextBuff _textBuf;
109109
float[] _charWidths;
110110
List<int> _breaks = new List<int>();
111+
int _breaksCount = 0;
111112
List<float> _widths = new List<float>();
113+
int _widthsCount = 0;
112114
WordBreaker _wordBreaker = new WordBreaker();
113115
float _width = 0.0f;
114116
float _preBreak;
@@ -120,19 +122,28 @@ public static int[] newLinePositions(string text, out int count) {
120122
TabStops _tabStops;
121123
int mFirstTabIndex;
122124
List<Candidate> _candidates = new List<Candidate>();
125+
int _candidatesCount = 0;
123126

124127
public int computeBreaks() {
125-
int nCand = this._candidates.Count;
128+
int nCand = this._candidatesCount;
126129
if (nCand > 0 && (nCand == 1 || this._lastBreak != nCand - 1)) {
127-
var cand = this._candidates.last();
130+
var cand = this._candidates[this._candidatesCount - 1];
128131
this._pushBreak(cand.offset, (cand.postBreak - this._preBreak));
129132
}
130133

131-
return this._breaks.Count;
134+
return this._breaksCount;
132135
}
133136

134-
public List<int> getBreaks() {
135-
return this._breaks;
137+
public int getBreaksCount() {
138+
return this._breaksCount;
139+
}
140+
141+
public int getBreak(int i) {
142+
return this._breaks[i];
143+
}
144+
145+
public float getWidth(int i) {
146+
return this._widths[i];
136147
}
137148

138149
public void resize(int size) {
@@ -145,11 +156,11 @@ public void setText(string text, int textOffset, int textLength) {
145156
this._textBuf = new TextBuff(text, textOffset, textLength);
146157
this._wordBreaker.setText(this._textBuf);
147158
this._wordBreaker.next();
148-
this._candidates.Clear();
159+
this._candidatesCount = 0;
149160
Candidate can = new Candidate {
150161
offset = 0, postBreak = 0, preBreak = 0, postSpaceCount = 0, preSpaceCount = 0, pre = 0
151162
};
152-
this._candidates.Add(can);
163+
this._addCandidateToList(can);
153164
this._lastBreak = 0;
154165
this._bestBreak = 0;
155166
this._bestScore = ScoreInfty;
@@ -208,14 +219,14 @@ public void addStyleRun(TextStyle style, int start, int end) {
208219
public void finish() {
209220
this._wordBreaker.finish();
210221
this._width = 0;
211-
this._candidates.Clear();
212-
this._widths.Clear();
213-
this._breaks.Clear();
222+
this._candidatesCount = 0;
223+
this._breaksCount = 0;
224+
this._widthsCount = 0;
214225
this._textBuf = default;
215226
}
216227

217-
public List<float> getWidths() {
218-
return this._widths;
228+
public int getWidthsCount() {
229+
return this._widthsCount;
219230
}
220231

221232
public void setTabStops(TabStops tabStops) {
@@ -225,7 +236,7 @@ public void setTabStops(TabStops tabStops) {
225236
void _addWordBreak(int offset, float preBreak, float postBreak, int preSpaceCount, int postSpaceCount,
226237
float penalty) {
227238

228-
float width = this._candidates.last().preBreak;
239+
float width = this._candidates[this._candidatesCount - 1].preBreak;
229240
if (postBreak - width > this._lineWidth) {
230241
this._addCandidatesInsideWord(width, offset, postSpaceCount);
231242
}
@@ -241,7 +252,7 @@ void _addWordBreak(int offset, float preBreak, float postBreak, int preSpaceCoun
241252
}
242253

243254
void _addCandidatesInsideWord(float width, int offset, int postSpaceCount) {
244-
int i = this._candidates.last().offset;
255+
int i = this._candidates[this._candidatesCount - 1].offset;
245256
width += this._charWidths[i++];
246257
for (; i < offset; i++) {
247258
float w = this._charWidths[i];
@@ -259,10 +270,19 @@ void _addCandidatesInsideWord(float width, int offset, int postSpaceCount) {
259270
}
260271
}
261272

273+
void _addCandidateToList(Candidate cand) {
274+
if (this._candidates.Count == this._candidatesCount) {
275+
this._candidates.Add(cand);
276+
this._candidatesCount++;
277+
}
278+
else {
279+
this._candidates[this._candidatesCount++] = cand;
280+
}
281+
}
262282

263283
void _addCandidate(Candidate cand) {
264-
int candIndex = this._candidates.Count;
265-
this._candidates.Add(cand);
284+
int candIndex = this._candidatesCount;
285+
this._addCandidateToList(cand);
266286
if (cand.postBreak - this._preBreak > this._lineWidth) {
267287
if (this._bestBreak == this._lastBreak) {
268288
this._bestBreak = candIndex;
@@ -299,9 +319,22 @@ void _pushGreedyBreak() {
299319
}
300320

301321
void _pushBreak(int offset, float width) {
302-
if (this.lineLimit == 0 || this._breaks.Count < this.lineLimit) {
303-
this._breaks.Add(offset);
304-
this._widths.Add(width);
322+
if (this.lineLimit == 0 || this._breaksCount < this.lineLimit) {
323+
if (this._breaks.Count == this._breaksCount) {
324+
this._breaks.Add(offset);
325+
this._breaksCount++;
326+
}
327+
else {
328+
this._breaks[this._breaksCount++] = offset;
329+
}
330+
331+
if (this._widths.Count == this._widthsCount) {
332+
this._widths.Add(width);
333+
this._widthsCount++;
334+
}
335+
else {
336+
this._widths[this._widthsCount++] = width;
337+
}
305338
}
306339
}
307340
}

Runtime/ui/txt/paragraph.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public LineRange(int start, int end, int endExcludingWhitespace, int endIncludin
188188
ParagraphStyle _paragraphStyle;
189189
List<LineRange> _lineRanges = new List<LineRange>();
190190
List<float> _lineWidths = new List<float>();
191-
float[] _lineBaseLines;
191+
// float[] _lineBaseLines;
192192
GlyphLine[] _glyphLines;
193193
float _maxIntrinsicWidth;
194194
float _minIntrinsicWidth;
@@ -287,9 +287,9 @@ public void layout(ParagraphConstraints constraints) {
287287
this._glyphLines = new GlyphLine[this._lineRanges.Count];
288288
}
289289

290-
if (this._lineBaseLines == null || this._lineBaseLines.Length < this._lineRanges.Count) {
291-
this._lineBaseLines = new float[this._lineRanges.Count];
292-
}
290+
// if (this._lineBaseLines == null || this._lineBaseLines.Length < this._lineRanges.Count) {
291+
// this._lineBaseLines = new float[this._lineRanges.Count];
292+
// }
293293

294294
if (this._lineHeights == null || this._lineHeights.Length < this._lineRanges.Count) {
295295
this._lineHeights = new float[this._lineRanges.Count];
@@ -536,7 +536,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
536536

537537
this._lineHeights[lineNumber] = ((lineNumber == 0 ? 0 : this._lineHeights[lineNumber - 1])
538538
+ Mathf.Round(maxLineSpacing + maxDescent));
539-
this._lineBaseLines[lineNumber] = this._lineHeights[lineNumber] - maxDescent;
539+
// this._lineBaseLines[lineNumber] = this._lineHeights[lineNumber] - maxDescent;
540540
yOffset += Mathf.Round(maxLineSpacing + preMaxDescent);
541541
preMaxDescent = maxDescent;
542542
float lineXOffset = this.getLineXOffset(runXOffset);
@@ -863,7 +863,7 @@ int _addStyleRuns(LineBreaker lineBreaker, ref int runIndex, int blockStart, int
863863
break;
864864
}
865865

866-
if (lineBreaker.lineLimit != 0 && lineBreaker.getBreaks().Count >= lineBreaker.lineLimit) {
866+
if (lineBreaker.lineLimit != 0 && lineBreaker.getBreaksCount() >= lineBreaker.lineLimit) {
867867
break;
868868
}
869869

@@ -888,12 +888,10 @@ int _addStyleRuns(LineBreaker lineBreaker, ref int runIndex, int blockStart, int
888888
}
889889

890890
void _updateBreaks(LineBreaker lineBreaker, int breaksCount, int blockStart, int blockEnd) {
891-
List<int> breaks = lineBreaker.getBreaks();
892-
List<float> widths = lineBreaker.getWidths();
893891
for (int i = 0; i < breaksCount; ++i) {
894-
var breakStart = i > 0 ? breaks[i - 1] : 0;
892+
var breakStart = i > 0 ? lineBreaker.getBreak(i - 1) : 0;
895893
var lineStart = breakStart + blockStart;
896-
var lineEnd = breaks[i] + blockStart;
894+
var lineEnd = lineBreaker.getBreak(i) + blockStart;
897895
bool hardBreak = lineEnd == blockEnd;
898896
var lineEndIncludingNewline =
899897
hardBreak && lineEnd < this._text.Length ? lineEnd + 1 : lineEnd;
@@ -905,7 +903,7 @@ void _updateBreaks(LineBreaker lineBreaker, int breaksCount, int blockStart, int
905903

906904
this._lineRanges.Add(new LineRange(lineStart, lineEnd,
907905
lineEndExcludingWhitespace, lineEndIncludingNewline, hardBreak));
908-
this._lineWidths.Add(widths[i]);
906+
this._lineWidths.Add(lineBreaker.getWidth(i));
909907
}
910908
}
911909

0 commit comments

Comments
 (0)