@@ -198,6 +198,8 @@ public LineRange(int start, int end, int endExcludingWhitespace, int endIncludin
198
198
int _lineCount ;
199
199
int _paintRecordsCount ;
200
200
int _codeUnitRunsCount ;
201
+ int _lineRangeCount ;
202
+ int _lineWidthCount ;
201
203
bool _didExceedMaxLines ;
202
204
TabStops _tabStops = new TabStops ( ) ;
203
205
@@ -299,34 +301,34 @@ public void layout(ParagraphConstraints constraints) {
299
301
300
302
int lineStyleRunsCount = this . _computeLineBreak ( ) ;
301
303
302
- if ( this . _glyphLines == null || this . _glyphLines . Length < this . _lineRanges . Count ) {
303
- this . _glyphLines = new GlyphLine [ this . _lineRanges . Count ] ;
304
+ if ( this . _glyphLines == null || this . _glyphLines . Length < this . _lineRangeCount ) {
305
+ this . _glyphLines = new GlyphLine [ LayoutUtils . minPowerOfTwo ( this . _lineRangeCount ) ] ;
304
306
}
305
307
306
- // if (this._lineBaseLines == null || this._lineBaseLines.Length < this._lineRanges.Count ) {
307
- // this._lineBaseLines = new float[this._lineRanges.Count ];
308
+ // if (this._lineBaseLines == null || this._lineBaseLines.Length < this._lineRangeCount ) {
309
+ // this._lineBaseLines = new float[this._lineRangeCount ];
308
310
// }
309
311
310
- if ( this . _lineHeights == null || this . _lineHeights . Length < this . _lineRanges . Count ) {
311
- this . _lineHeights = new float [ this . _lineRanges . Count ] ;
312
+ if ( this . _lineHeights == null || this . _lineHeights . Length < this . _lineRangeCount ) {
313
+ this . _lineHeights = new float [ LayoutUtils . minPowerOfTwo ( this . _lineRangeCount ) ] ;
312
314
}
313
315
314
316
if ( this . _paintRecords == null || this . _paintRecords . Length < lineStyleRunsCount ) {
315
- this . _paintRecords = new PaintRecord [ lineStyleRunsCount ] ;
317
+ this . _paintRecords = new PaintRecord [ LayoutUtils . minPowerOfTwo ( lineStyleRunsCount ) ] ;
316
318
}
317
319
318
320
this . _paintRecordsCount = 0 ;
319
321
320
322
if ( this . _codeUnitRuns == null || this . _codeUnitRuns . Length < lineStyleRunsCount ) {
321
- this . _codeUnitRuns = new CodeUnitRun [ lineStyleRunsCount ] ;
323
+ this . _codeUnitRuns = new CodeUnitRun [ LayoutUtils . minPowerOfTwo ( lineStyleRunsCount ) ] ;
322
324
}
323
325
324
326
this . _codeUnitRunsCount = 0 ;
325
327
326
328
int styleMaxLines = this . _paragraphStyle . maxLines ?? int . MaxValue ;
327
- this . _didExceedMaxLines = this . _lineRanges . Count > styleMaxLines ;
329
+ this . _didExceedMaxLines = this . _lineRangeCount > styleMaxLines ;
328
330
329
- var lineLimit = Mathf . Min ( styleMaxLines , this . _lineRanges . Count ) ;
331
+ var lineLimit = Mathf . Min ( styleMaxLines , this . _lineRangeCount ) ;
330
332
int styleRunIndex = 0 ;
331
333
float yOffset = 0 ;
332
334
float preMaxDescent = 0 ;
@@ -337,12 +339,12 @@ public void layout(ParagraphConstraints constraints) {
337
339
338
340
// All text blobs share a single position buffer, which is big enough taking ellipsis into consideration
339
341
if ( this . _textBlobPositions == null || this . _textBlobPositions . Length < ellipsizedLength ) {
340
- this . _textBlobPositions = new float [ ellipsizedLength ] ;
342
+ this . _textBlobPositions = new float [ LayoutUtils . minPowerOfTwo ( ellipsizedLength ) ] ;
341
343
}
342
344
builder . setPositions ( this . _textBlobPositions ) ;
343
345
// this._glyphLines and this._codeUnitRuns will refer to this array for glyph positions
344
346
if ( this . _glyphPositions == null || this . _glyphPositions . Length < ellipsizedLength ) {
345
- this . _glyphPositions = new GlyphPosition [ ellipsizedLength ] ;
347
+ this . _glyphPositions = new GlyphPosition [ LayoutUtils . minPowerOfTwo ( ellipsizedLength ) ] ;
346
348
}
347
349
348
350
// Pointer to the _glyphPositions array, to keep track of where the next glyph is stored
@@ -389,11 +391,11 @@ public void layout(ParagraphConstraints constraints) {
389
391
// TODO: find a way to compute the maxTextCount for the entire paragraph, so that this allocation
390
392
// happens only once
391
393
if ( _advancesBuffer == null || _advancesBuffer . Length < maxTextCount ) {
392
- _advancesBuffer = new float [ maxTextCount ] ;
394
+ _advancesBuffer = new float [ LayoutUtils . minPowerOfTwo ( maxTextCount ) ] ;
393
395
}
394
396
395
397
if ( _positionsBuffer == null || _positionsBuffer . Length < maxTextCount ) {
396
- _positionsBuffer = new float [ maxTextCount ] ;
398
+ _positionsBuffer = new float [ LayoutUtils . minPowerOfTwo ( maxTextCount ) ] ;
397
399
}
398
400
399
401
// Keep of the position in _glyphPositions before evaluating this line
@@ -573,7 +575,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
573
575
}
574
576
575
577
int lineStart = lineRange . start ;
576
- int nextLineStart = lineNumber < this . _lineRanges . Count - 1
578
+ int nextLineStart = lineNumber < this . _lineRangeCount - 1
577
579
? this . _lineRanges [ lineNumber + 1 ] . start
578
580
: this . _text . Length ;
579
581
this . _glyphLines [ lineNumber ] =
@@ -588,7 +590,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
588
590
this . _lineCount = lineLimit ;
589
591
this . _maxIntrinsicWidth = 0 ;
590
592
float lineBlockWidth = 0 ;
591
- for ( int i = 0 ; i < this . _lineWidths . Count ; ++ i ) {
593
+ for ( int i = 0 ; i < this . _lineWidthCount ; ++ i ) {
592
594
lineBlockWidth += this . _lineWidths [ i ] ;
593
595
if ( this . _lineRanges [ i ] . hardBreak ) {
594
596
this . _maxIntrinsicWidth = Mathf . Max ( lineBlockWidth , this . _maxIntrinsicWidth ) ;
@@ -689,7 +691,7 @@ public List<TextBox> getRectsForRange(int start, int end) {
689
691
boxs . Add ( TextBox . fromLTBD ( left , top , right , bottom , run . direction ) ) ;
690
692
}
691
693
692
- for ( int lineNumber = 0 ; lineNumber < this . _lineRanges . Count ; ++ lineNumber ) {
694
+ for ( int lineNumber = 0 ; lineNumber < this . _lineRangeCount ; ++ lineNumber ) {
693
695
var line = this . _lineRanges [ lineNumber ] ;
694
696
if ( line . start >= end ) {
695
697
break ;
@@ -824,8 +826,8 @@ public int getLineCount() {
824
826
}
825
827
826
828
int _computeLineBreak ( ) {
827
- this . _lineRanges . Clear ( ) ;
828
- this . _lineWidths . Clear ( ) ;
829
+ this . _lineRangeCount = 0 ;
830
+ this . _lineWidthCount = 0 ;
829
831
this . _maxIntrinsicWidth = 0 ;
830
832
831
833
int lineLimit = this . _paragraphStyle . ellipsized ( )
@@ -838,7 +840,7 @@ int _computeLineBreak() {
838
840
int runIndex = 0 ;
839
841
int countRuns = 0 ;
840
842
for ( var newlineIndex = 0 ; newlineIndex < newLineCount ; ++ newlineIndex ) {
841
- if ( lineLimit != 0 && this . _lineRanges . Count >= lineLimit ) {
843
+ if ( lineLimit != 0 && this . _lineRangeCount >= lineLimit ) {
842
844
break ;
843
845
}
844
846
var blockStart = newlineIndex > 0 ? newLinePositions [ newlineIndex - 1 ] + 1 : 0 ;
@@ -848,12 +850,12 @@ int _computeLineBreak() {
848
850
this . _addEmptyLine ( blockStart , blockEnd ) ;
849
851
continue ;
850
852
}
851
- if ( lineLimit != 0 && this . _lineRanges . Count >= lineLimit ) {
853
+ if ( lineLimit != 0 && this . _lineRangeCount >= lineLimit ) {
852
854
break ;
853
855
}
854
856
855
857
this . _resetLineBreaker ( lineBreaker , blockStart , blockSize ,
856
- lineLimit == 0 ? 0 : lineLimit - this . _lineRanges . Count ) ;
858
+ lineLimit == 0 ? 0 : lineLimit - this . _lineRangeCount ) ;
857
859
countRuns += this . _addStyleRuns ( lineBreaker , ref runIndex , blockStart , blockEnd ) ;
858
860
859
861
int breaksCount = lineBreaker . computeBreaks ( ) ;
@@ -866,10 +868,27 @@ int _computeLineBreak() {
866
868
return countRuns ;
867
869
}
868
870
871
+ void _addLineRangeAndWidth ( LineRange lineRange , float width ) {
872
+ if ( this . _lineRanges . Count <= this . _lineRangeCount ) {
873
+ this . _lineRanges . Add ( lineRange ) ;
874
+ this . _lineRangeCount ++ ;
875
+ }
876
+ else {
877
+ this . _lineRanges [ this . _lineRangeCount ++ ] = lineRange ;
878
+ }
879
+
880
+ if ( this . _lineWidths . Count <= this . _lineWidthCount ) {
881
+ this . _lineWidths . Add ( width ) ;
882
+ this . _lineWidthCount ++ ;
883
+ }
884
+ else {
885
+ this . _lineWidths [ this . _lineWidthCount ++ ] = width ;
886
+ }
887
+ }
888
+
869
889
void _addEmptyLine ( int blockStart , int blockEnd ) {
870
- this . _lineRanges . Add ( new LineRange ( blockStart , blockEnd , blockEnd ,
871
- blockEnd < this . _text . Length ? blockEnd + 1 : blockEnd , true ) ) ;
872
- this . _lineWidths . Add ( 0 ) ;
890
+ this . _addLineRangeAndWidth ( new LineRange ( blockStart , blockEnd , blockEnd ,
891
+ blockEnd < this . _text . Length ? blockEnd + 1 : blockEnd , true ) , 0 ) ;
873
892
}
874
893
875
894
void _resetLineBreaker ( LineBreaker lineBreaker , int blockStart , int blockSize , int lineLimit ) {
@@ -926,15 +945,14 @@ void _updateBreaks(LineBreaker lineBreaker, int breaksCount, int blockStart, int
926
945
lineEndExcludingWhitespace -- ;
927
946
}
928
947
929
- this . _lineRanges . Add ( new LineRange ( lineStart , lineEnd ,
930
- lineEndExcludingWhitespace , lineEndIncludingNewline , hardBreak ) ) ;
931
- this . _lineWidths . Add ( lineBreaker . getWidth ( i ) ) ;
948
+ this . _addLineRangeAndWidth ( new LineRange ( lineStart , lineEnd ,
949
+ lineEndExcludingWhitespace , lineEndIncludingNewline , hardBreak ) , lineBreaker . getWidth ( i ) ) ;
932
950
}
933
951
}
934
952
935
953
int _computeMaxWordCount ( ) {
936
954
int max = 0 ;
937
- for ( int lineNumber = 0 ; lineNumber < this . _lineRanges . Count ; lineNumber ++ ) {
955
+ for ( int lineNumber = 0 ; lineNumber < this . _lineRangeCount ; lineNumber ++ ) {
938
956
var inWord = false ;
939
957
int wordCount = 0 , start = this . _lineRanges [ lineNumber ] . start , end = this . _lineRanges [ lineNumber ] . end ;
940
958
for ( int i = start ; i < end ; ++ i ) {
0 commit comments