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

Commit c911ac9

Browse files
author
Yuncong Zhang
committed
Make GlyphPosition.codeUnits int. Remove CodeUnitRuns._positions.
1 parent c164d2e commit c911ac9

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

Runtime/ui/txt/paragraph.cs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@ struct CodeUnitRun {
1414
public readonly int start;
1515
public readonly int count;
1616

17-
readonly GlyphPosition[] _positions;
18-
19-
public CodeUnitRun(GlyphPosition[] positions, Range<int> cu, Range<float> xPos, int line,
17+
public CodeUnitRun(Range<int> cu, Range<float> xPos, int line,
2018
TextDirection direction, int start, int count) {
2119
this.lineNumber = line;
2220
this.codeUnits = cu;
2321
this.xPos = xPos;
24-
this._positions = positions;
2522
this.direction = direction;
2623
this.start = start;
2724
this.count = count;
2825
}
2926

30-
public GlyphPosition get(int i) {
27+
public GlyphPosition get(int i, GlyphPosition[] glyphPositions) {
3128
D.assert(i < this.count);
32-
return this._positions[this.start + i];
29+
return glyphPositions[this.start + i];
3330
}
3431
}
3532

@@ -89,11 +86,11 @@ public PositionWithAffinity(int p, TextAffinity a) {
8986

9087
struct GlyphPosition {
9188
public Range<float> xPos;
92-
public readonly Range<int> codeUnits;
89+
public readonly int codeUnit;
9390

94-
public GlyphPosition(float start, float advance, Range<int> codeUnits) {
91+
public GlyphPosition(float start, float advance, int codeUnit) {
9592
this.xPos = new Range<float>(start, start + advance);
96-
this.codeUnits = codeUnits;
93+
this.codeUnit = codeUnit;
9794
}
9895

9996
public void shiftSelf(float shift) {
@@ -190,7 +187,7 @@ public LineRange(int start, int end, int endExcludingWhitespace, int endIncludin
190187
List<float> _lineWidths = new List<float>();
191188
// float[] _lineBaseLines;
192189
GlyphLine[] _glyphLines;
193-
GlyphPosition[] glyphPositions;
190+
GlyphPosition[] _glyphPositions;
194191
PaintRecord[] _paintRecords;
195192
CodeUnitRun[] _codeUnitRuns;
196193
float[] _lineHeights;
@@ -326,11 +323,11 @@ public void layout(ParagraphConstraints constraints) {
326323
}
327324
builder.setPositions(this._textBlobPositions);
328325
// this._glyphLines and this._codeUnitRuns will refer to this array for glyph positions
329-
if (this.glyphPositions == null || this.glyphPositions.Length < ellipsizedLength) {
330-
this.glyphPositions = new GlyphPosition[ellipsizedLength];
326+
if (this._glyphPositions == null || this._glyphPositions.Length < ellipsizedLength) {
327+
this._glyphPositions = new GlyphPosition[ellipsizedLength];
331328
}
332329

333-
// Pointer to the glyphPositions array, to keep track of where the next glyph is stored
330+
// Pointer to the _glyphPositions array, to keep track of where the next glyph is stored
334331
int pGlyphPositions = 0;
335332

336333
// Compute max(NumberOfWords(line) for line in lines), to determine the size of word buffers
@@ -381,7 +378,7 @@ public void layout(ParagraphConstraints constraints) {
381378
_positionsBuffer = new float[maxTextCount];
382379
}
383380

384-
// Keep of the position in glyphPositions before evaluating this line
381+
// Keep of the position in _glyphPositions before evaluating this line
385382
int glyphPositionLineStart = pGlyphPositions;
386383

387384
if (lineStyleRunCount != 0) {
@@ -407,7 +404,7 @@ public void layout(ParagraphConstraints constraints) {
407404
int textStart = start;
408405
int textEnd = end;
409406
int textCount = textEnd - textStart;
410-
// Keep track of the pointer to glyphPositions in the start of this run
407+
// Keep track of the pointer to _glyphPositions in the start of this run
411408
int glyphPositionStyleRunStart = pGlyphPositions;
412409

413410
// Ellipsize the text if ellipsis string is set, and this is the last lineStyleRun of
@@ -446,8 +443,8 @@ public void layout(ParagraphConstraints constraints) {
446443
float glyphXOffset = _positionsBuffer[glyphIndex] + justifyXOffset;
447444
float glyphAdvance = _advancesBuffer[glyphIndex];
448445
builder.setPosition(glyphIndex, glyphXOffset);
449-
this.glyphPositions[pGlyphPositions++] = new GlyphPosition(runXOffset + glyphXOffset,
450-
glyphAdvance, new Range<int>(textStart + glyphIndex, textStart + glyphIndex + 1));
446+
this._glyphPositions[pGlyphPositions++] = new GlyphPosition(runXOffset + glyphXOffset,
447+
glyphAdvance, textStart + glyphIndex);
451448
if (wordIndex < wordCount) {
452449
Range<int> word = _wordsBuffer[wordIndex];
453450
// Run into the start of current word, record the start position of this word
@@ -467,7 +464,7 @@ public void layout(ParagraphConstraints constraints) {
467464
// width of this word, and update the entire word
468465
if (!float.IsNaN(wordStartPosition)) {
469466
maxWordWidth = Mathf.Max(maxWordWidth,
470-
this.glyphPositions[pGlyphPositions - 1].xPos.end - wordStartPosition);
467+
this._glyphPositions[pGlyphPositions - 1].xPos.end - wordStartPosition);
471468
wordStartPosition = float.NaN;
472469
}
473470
}
@@ -485,9 +482,8 @@ public void layout(ParagraphConstraints constraints) {
485482

486483
// Create code unit run
487484
this._codeUnitRuns[this._codeUnitRunsCount++] = new CodeUnitRun(
488-
this.glyphPositions,
489485
new Range<int>(start, end),
490-
new Range<float>(this.glyphPositions[0].xPos.start, this.glyphPositions.last().xPos.end),
486+
new Range<float>(this._glyphPositions[0].xPos.start, this._glyphPositions.last().xPos.end),
491487
lineNumber, TextDirection.ltr, glyphPositionStyleRunStart, textCount);
492488

493489
lineStyleRunIndex++;
@@ -544,9 +540,9 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
544540
preMaxDescent = maxDescent;
545541
float lineXOffset = this.getLineXOffset(runXOffset);
546542
int count = pGlyphPositions - glyphPositionLineStart;
547-
if (lineXOffset != 0 && this.glyphPositions != null) {
543+
if (lineXOffset != 0 && this._glyphPositions != null) {
548544
for (int i = 0; i < count; ++i) {
549-
this.glyphPositions[this.glyphPositions.Length - i - 1].shiftSelf(lineXOffset);
545+
this._glyphPositions[this._glyphPositions.Length - i - 1].shiftSelf(lineXOffset);
550546
}
551547
}
552548

@@ -555,7 +551,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
555551
? this._lineRanges[lineNumber + 1].start
556552
: this._text.Length;
557553
this._glyphLines[lineNumber] =
558-
new GlyphLine(this.glyphPositions, glyphPositionLineStart, count, nextLineStart - lineStart);
554+
new GlyphLine(this._glyphPositions, glyphPositionLineStart, count, nextLineStart - lineStart);
559555
for (int i = 0; i < lineStyleRunCount; i++) {
560556
var paintRecord = this._paintRecords[this._paintRecordsCount - 1 - i];
561557
paintRecord.shift(lineXOffset, yOffset);
@@ -646,8 +642,8 @@ public List<TextBox> getRectsForRange(int start, int end) {
646642
left = float.MaxValue;
647643
right = float.MinValue;
648644
for (int i = 0; i < run.count; i++) {
649-
var gp = run.get(i);
650-
if (gp.codeUnits.start >= start && gp.codeUnits.end <= end) {
645+
var gp = run.get(i, this._glyphPositions);
646+
if (gp.codeUnit >= start && gp.codeUnit + 1 <= end) {
651647
left = Mathf.Min(left, gp.xPos.start);
652648
right = Mathf.Max(right, gp.xPos.end);
653649
}
@@ -746,12 +742,12 @@ internal PositionWithAffinity getGlyphPositionAtCoordinate(float dx, float dy) {
746742

747743
if (!gpSet) {
748744
GlyphPosition lastGlyph = glyphLine.last();
749-
return new PositionWithAffinity(lastGlyph.codeUnits.end, TextAffinity.upstream);
745+
return new PositionWithAffinity(lastGlyph.codeUnit + 1, TextAffinity.upstream);
750746
}
751747

752748
TextDirection direction = TextDirection.ltr;
753749
foreach (var run in this._codeUnitRuns) {
754-
if (gp.codeUnits.start >= run.codeUnits.start && gp.codeUnits.end <= run.codeUnits.end) {
750+
if (gp.codeUnit >= run.codeUnits.start && gp.codeUnit + 1 <= run.codeUnits.end) {
755751
direction = run.direction;
756752
break;
757753
}
@@ -760,10 +756,10 @@ internal PositionWithAffinity getGlyphPositionAtCoordinate(float dx, float dy) {
760756
float glyphCenter = (gp.xPos.start + gp.xPos.end) / 2;
761757
if ((direction == TextDirection.ltr && dx < glyphCenter) ||
762758
(direction == TextDirection.rtl && dx >= glyphCenter)) {
763-
return new PositionWithAffinity(gp.codeUnits.start, TextAffinity.downstream);
759+
return new PositionWithAffinity(gp.codeUnit, TextAffinity.downstream);
764760
}
765761

766-
return new PositionWithAffinity(gp.codeUnits.end, TextAffinity.upstream);
762+
return new PositionWithAffinity(gp.codeUnit + 1, TextAffinity.upstream);
767763
}
768764

769765
public int getLine(TextPosition position) {

0 commit comments

Comments
 (0)