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

Commit b0f6491

Browse files
author
Yuncong Zhang
committed
Cleanup. Use UnityEngine.Rect in textblob.
1 parent 7a5594f commit b0f6491

File tree

5 files changed

+10
-58
lines changed

5 files changed

+10
-58
lines changed

Runtime/painting/text_painter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public Offset getOffsetForCaret(TextPosition position, Rect caretPrototype) {
212212
var prevCodeUnit = this._text.codeUnitAt(offset);
213213
if (prevCodeUnit == null) // out of upper bounds
214214
{
215-
TextBox? rectNextLine = this._paragraph.getNextLineStartRect();
215+
var rectNextLine = this._paragraph.getNextLineStartRect();
216216
if (rectNextLine != null) {
217217
return new Offset(((TextBox) rectNextLine).start, ((TextBox) rectNextLine).top);
218218
}

Runtime/ui/painting/txt/text_blob.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
namespace Unity.UIWidgets.ui {
22
public struct TextBlob {
33
internal TextBlob(string text, int textOffset, int textSize, float[] positions,
4-
float minX, float minY, float width, float height, TextStyle style) {
4+
UnityEngine.Rect bounds, TextStyle style) {
55
this.instanceId = ++_nextInstanceId;
66
this.text = text;
77
this.textOffset = textOffset;
88
this.textSize = textSize;
99
this.style = style;
10-
this._minX = minX;
11-
this._minY = minY;
12-
this._width = width;
13-
this._height = height;
10+
this._bounds = bounds;
1411
this._positions = positions;
1512
}
1613

1714
public Rect boundsInText {
1815
get {
1916
var pos = this._positions[this.textOffset];
20-
return Rect.fromLTWH(this._minX + pos, this._minY, this._width, this._height);
17+
return Rect.fromLTWH(this._bounds.xMin + pos, this._bounds.yMin, this._bounds.width, this._bounds.height);
2118
}
2219
}
2320

2421
public Rect shiftedBoundsInText(Offset offset) {
2522
var pos = this._positions[this.textOffset];
26-
return Rect.fromLTWH(this._minX + pos + offset.dx, this._minY + offset.dy, this._width, this._height);
23+
return Rect.fromLTWH(this._bounds.xMin + pos + offset.dx, this._bounds.yMin + offset.dy, this._bounds.width, this._bounds.height);
2724
}
2825

2926
public float getPosition(int i) {
@@ -36,7 +33,7 @@ public float getPosition(int i) {
3633
internal readonly int textOffset;
3734
internal readonly int textSize;
3835
internal readonly TextStyle style;
39-
readonly float _minX, _minY, _width, _height; // bounds with positions[start] as origin
36+
readonly UnityEngine.Rect _bounds; // bounds with positions[start] as origin
4037
readonly float[] _positions;
4138
}
4239

@@ -46,7 +43,7 @@ public struct TextBlobBuilder {
4643
string _text;
4744
int _textOffset;
4845
int _size;
49-
float _minX, _minY, _width, _height;
46+
UnityEngine.Rect _bounds;
5047

5148
public void allocRunPos(painting.TextStyle style, string text, int offset, int size,
5249
float textScaleFactor = 1.0f) {
@@ -78,20 +75,13 @@ public void setPositions(float[] positions) {
7875
this._positions = positions;
7976
}
8077

81-
public void setBounds(float minX, float minY, float width, float height) {
82-
this._minX = minX;
83-
this._minY = minY;
84-
this._width = width;
85-
this._height = height;
86-
}
87-
8878
public void setBounds(UnityEngine.Rect bounds) {
89-
this.setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
79+
this._bounds = bounds;
9080
}
9181

9282
public TextBlob make() {
9383
var result = new TextBlob(this._text, this._textOffset,
94-
this._size, this._positions, this._minX, this._minY, this._width, this._height, this._style);
84+
this._size, this._positions, this._bounds, this._style);
9585
return result;
9686
}
9787
}

Runtime/ui/text.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,6 @@ public float end {
610610
}
611611

612612
public bool Equals(TextBox other) {
613-
if (ReferenceEquals(null, other)) {
614-
return false;
615-
}
616-
617-
if (ReferenceEquals(this, other)) {
618-
return true;
619-
}
620-
621613
return this.left.Equals(other.left) && this.top.Equals(other.top) && this.right.Equals(other.right) &&
622614
this.bottom.Equals(other.bottom) && this.direction == other.direction;
623615
}
@@ -627,10 +619,6 @@ public override bool Equals(object obj) {
627619
return false;
628620
}
629621

630-
if (ReferenceEquals(this, obj)) {
631-
return true;
632-
}
633-
634622
if (obj.GetType() != this.GetType()) {
635623
return false;
636624
}

Runtime/ui/txt/layout_utils.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,6 @@ public static bool isLineEndSpace(char c) {
1111
c == 0x205F || c == 0x3000;
1212
}
1313

14-
public static int getPrevWordBreakForCache(TextBuff buff, int offset) {
15-
int len = buff.size;
16-
if (offset == 0) {
17-
return 0;
18-
}
19-
20-
if (offset > len) {
21-
offset = len;
22-
}
23-
if (isWordBreakBefore(buff.charAt(offset - 1))) {
24-
return offset - 1;
25-
}
26-
for (int i = offset - 1; i > 0; i--) {
27-
// Since i steps down, isWordBreakAfter(i) has already been checked in the previous step
28-
// isWordBreakBeforeNotAfter cuts that part out to save time.
29-
if (isWordBreakBeforeNotAfter(buff.charAt(i)) || isWordBreakAfter(buff.charAt(i - 1))) {
30-
return i;
31-
}
32-
}
33-
return 0;
34-
}
35-
3614

3715
public static int getNextWordBreak(string text, int offset, int maxOffset) {
3816
int len = text.Length;
@@ -65,10 +43,6 @@ public static bool isWordBreakBefore(ushort c) {
6543
return isWordBreakAfter(c) || (c >= 0x3400 && c <= 0x9fff);
6644
}
6745

68-
public static bool isWordBreakBeforeNotAfter(ushort c) {
69-
return c >= 3400 && c <= 0x9fff;
70-
}
71-
7246
public static int minPowerOfTwo(int i) {
7347
// Assume that int is 32 bit
7448
i--;

Tests/Editor/CanvasAndLayers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void drawLine() {
264264
canvas.scale(3);
265265
TextBlobBuilder builder = new TextBlobBuilder();
266266
string text = "This is a text blob";
267-
builder.setBounds(-10, -20, 200, 50);
267+
builder.setBounds(new Rect(-10, -20, 200, 50));
268268
builder.setPositions(new float[] {
269269
10,
270270
20,

0 commit comments

Comments
 (0)