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

Commit 04df360

Browse files
author
Yuncong Zhang
committed
Make advances, positions and words global static.
1 parent 19dee77 commit 04df360

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

Runtime/ui/painting/txt/text_blob.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public float getPosition(int i) {
4040
readonly float[] _positions;
4141
}
4242

43-
public class TextBlobBuilder {
43+
public struct TextBlobBuilder {
4444
TextStyle _style;
4545
float[] _positions;
4646
string _text;

Runtime/ui/txt/paragraph.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ public LineRange(int start, int end, int endExcludingWhitespace, int endIncludin
200200
bool _didExceedMaxLines;
201201
TabStops _tabStops = new TabStops();
202202

203+
static float[] _advancesBuffer;
204+
static float[] _positionsBuffer;
205+
static Range<int>[] _wordsBuffer;
206+
203207
// private float _characterWidth;
204208

205209
float _width;
@@ -296,10 +300,10 @@ public void layout(ParagraphConstraints constraints) {
296300
if (maxWordCount == 0) {
297301
return;
298302
}
299-
Range<int>[] words = new Range<int>[maxWordCount];
300-
301-
float[] positions = null;
302-
float[] advances = null;
303+
304+
if (_wordsBuffer == null || _wordsBuffer.Length < maxWordCount) {
305+
_wordsBuffer = new Range<int>[maxWordCount];
306+
}
303307

304308
// Iterate through line ranges
305309
for (int lineNumber = 0; lineNumber < lineLimit; ++lineNumber) {
@@ -316,7 +320,7 @@ public void layout(ParagraphConstraints constraints) {
316320
// This is still not taken care of in the flutter engine.
317321
!(this._paragraphStyle.ellipsized() && this._paragraphStyle.maxLines == null);
318322

319-
int wordCount = this._findWords(lineRange.start, lineRange.end, words);
323+
int wordCount = this._findWords(lineRange.start, lineRange.end, _wordsBuffer);
320324
float wordGapWidth = !(justifyLine && wordCount > 1)
321325
? 0
322326
: (this._width - this._lineWidths[lineNumber]) / (wordCount - 1);
@@ -334,12 +338,12 @@ public void layout(ParagraphConstraints constraints) {
334338
// Allocate the advances and positions to store the layout result
335339
// TODO: find a way to compute the maxTextCount for the entire paragraph, so that this allocation
336340
// happens only once
337-
if (advances == null || advances.Length < maxTextCount) {
338-
advances = new float[maxTextCount];
341+
if (_advancesBuffer == null || _advancesBuffer.Length < maxTextCount) {
342+
_advancesBuffer = new float[maxTextCount];
339343
}
340344

341-
if (positions == null || positions.Length < maxTextCount) {
342-
positions = new float[maxTextCount];
345+
if (_positionsBuffer == null || _positionsBuffer.Length < maxTextCount) {
346+
_positionsBuffer = new float[maxTextCount];
343347
}
344348

345349
// Keep of the position in glyphPositions before evaluating this line
@@ -393,24 +397,24 @@ public void layout(ParagraphConstraints constraints) {
393397
}
394398

395399
float advance = Layout.doLayout(runXOffset, text, textStart, textCount, style,
396-
advances, positions, this._tabStops, out var bounds);
400+
_advancesBuffer, _positionsBuffer, this._tabStops, out var bounds);
397401

398402
builder.allocRunPos(style, text, textStart, textCount);
399403
// bounds relative to first character
400-
bounds.x -= positions[0];
404+
bounds.x -= _positionsBuffer[0];
401405
builder.setBounds(bounds);
402406

403407
// Update the max width of the words
404408
// Fill in the glyph positions, and the positions of the text blob builder
405409
float wordStartPosition = float.NaN;
406410
for (int glyphIndex = 0; glyphIndex < textCount; ++glyphIndex) {
407-
float glyphXOffset = positions[glyphIndex] + justifyXOffset;
408-
float glyphAdvance = advances[glyphIndex];
411+
float glyphXOffset = _positionsBuffer[glyphIndex] + justifyXOffset;
412+
float glyphAdvance = _advancesBuffer[glyphIndex];
409413
builder.setPosition(glyphIndex, glyphXOffset);
410414
glyphPositions[pGlyphPositions++] = new GlyphPosition(runXOffset + glyphXOffset,
411415
glyphAdvance, new Range<int>(textStart + glyphIndex, textStart + glyphIndex + 1));
412416
if (wordIndex < wordCount) {
413-
Range<int> word = words[wordIndex];
417+
Range<int> word = _wordsBuffer[wordIndex];
414418
// Run into the start of current word, record the start position of this word
415419
if (word.start == start + glyphIndex) {
416420
wordStartPosition = runXOffset + glyphXOffset;

0 commit comments

Comments
 (0)