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

Commit 73ada8e

Browse files
author
Yuncong Zhang
committed
Cleanup.
1 parent 2e3c44f commit 73ada8e

File tree

3 files changed

+64
-49
lines changed

3 files changed

+64
-49
lines changed

Runtime/ui/painting/txt/mesh_generator.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class MeshKey : PoolObject, IEquatable<MeshKey> {
99
public float scale;
1010

1111
public MeshKey() {
12-
1312
}
1413

1514
public static MeshKey create(long textBlobId, float scale) {
@@ -73,7 +72,6 @@ class MeshInfo : PoolObject {
7372
long _timeToLive;
7473

7574
public MeshInfo() {
76-
7775
}
7876

7977
public static MeshInfo create(MeshKey key, uiMeshMesh mesh, long textureVersion, int timeToLive = 5) {
@@ -99,16 +97,15 @@ public void touch(long timeTolive = 5) {
9997
}
10098
}
10199

102-
100+
103101
class TextBlobMesh : PoolObject {
104-
105102
static readonly Dictionary<MeshKey, MeshInfo> _meshes = new Dictionary<MeshKey, MeshInfo>();
106103
static long _frameCount = 0;
107-
104+
108105
public TextBlob textBlob;
109106
public float scale;
110107
public uiMatrix3 matrix;
111-
108+
112109
uiMeshMesh _mesh;
113110
bool _resolved;
114111

@@ -128,7 +125,7 @@ public static TextBlobMesh create(TextBlob textBlob, float scale, uiMatrix3 matr
128125
newMesh.matrix = matrix;
129126
return newMesh;
130127
}
131-
128+
132129
public static long frameCount {
133130
get { return _frameCount; }
134131
}
@@ -138,6 +135,7 @@ public static int meshCount {
138135
}
139136

140137
static List<MeshKey> _keysToRemove = new List<MeshKey>();
138+
141139
public static void tickNextFrame() {
142140
_frameCount++;
143141
D.assert(_keysToRemove.Count == 0);
@@ -147,11 +145,11 @@ public static void tickNextFrame() {
147145
}
148146
}
149147

150-
foreach (var key in _keysToRemove)
151-
{
148+
foreach (var key in _keysToRemove) {
152149
ObjectPool<MeshInfo>.release(_meshes[key]);
153150
_meshes.Remove(key);
154151
}
152+
155153
_keysToRemove.Clear();
156154
}
157155

@@ -161,29 +159,29 @@ public uiMeshMesh resolveMesh() {
161159
}
162160

163161
this._resolved = true;
164-
162+
165163
var style = this.textBlob.style;
166-
164+
167165
var text = this.textBlob.text;
168166
var key = MeshKey.create(this.textBlob.instanceId, this.scale);
169167
var fontInfo = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle);
170168
var font = fontInfo.font;
171-
169+
172170
_meshes.TryGetValue(key, out var meshInfo);
173171
if (meshInfo != null && meshInfo.textureVersion == fontInfo.textureVersion) {
174172
ObjectPool<MeshKey>.release(key);
175173
meshInfo.touch();
176174
this._mesh = meshInfo.mesh.transform(this.matrix);
177175
return this._mesh;
178176
}
179-
177+
180178
// Handling Emoji
181179
char startingChar = text[this.textBlob.textOffset];
182180
if (char.IsHighSurrogate(startingChar) || EmojiUtils.isSingleCharEmoji(startingChar)) {
183181
var vert = ObjectPool<uiList<Vector3>>.alloc();
184182
var tri = ObjectPool<uiList<int>>.alloc();
185183
var uvCoord = ObjectPool<uiList<Vector2>>.alloc();
186-
184+
187185
var metrics = FontMetrics.fromFont(font, style.UnityFontSize);
188186
var minMaxRect = EmojiUtils.getMinMaxRect(style.fontSize, metrics.ascent, metrics.descent);
189187
var minX = minMaxRect.left;
@@ -195,14 +193,16 @@ public uiMeshMesh resolveMesh() {
195193
char a = text[this.textBlob.textOffset + i];
196194
int code = a;
197195
if (char.IsHighSurrogate(a)) {
198-
D.assert(i+1 < this.textBlob.textSize);
199-
D.assert(this.textBlob.textOffset+i+1 < this.textBlob.text.Length);
200-
char b = text[this.textBlob.textOffset+i+1];
196+
D.assert(i + 1 < this.textBlob.textSize);
197+
D.assert(this.textBlob.textOffset + i + 1 < this.textBlob.text.Length);
198+
char b = text[this.textBlob.textOffset + i + 1];
201199
D.assert(char.IsLowSurrogate(b));
202200
code = char.ConvertToUtf32(a, b);
203-
} else if (char.IsLowSurrogate(a) || EmojiUtils.isEmptyEmoji(a)) {
201+
}
202+
else if (char.IsLowSurrogate(a) || EmojiUtils.isEmptyEmoji(a)) {
204203
continue;
205204
}
205+
206206
var uvRect = EmojiUtils.getUVRect(code);
207207

208208
var pos = this.textBlob.getPosition(i);
@@ -223,33 +223,36 @@ public uiMeshMesh resolveMesh() {
223223
uvCoord.Add(uvRect.topRight.toVector());
224224
uvCoord.Add(uvRect.topLeft.toVector());
225225

226-
if(char.IsHighSurrogate(a)) i++;
226+
if (char.IsHighSurrogate(a)) {
227+
i++;
228+
}
227229
}
228-
230+
229231
uiMeshMesh meshMesh = uiMeshMesh.create(null, vert, tri, uvCoord);
230-
232+
231233
if (_meshes.ContainsKey(key)) {
232234
ObjectPool<MeshInfo>.release(_meshes[key]);
233235
_meshes.Remove(key);
234236
}
237+
235238
_meshes[key] = MeshInfo.create(key, meshMesh, 0);
236239

237240
this._mesh = meshMesh.transform(this.matrix);
238241
return this._mesh;
239242
}
240-
243+
241244
var length = this.textBlob.textSize;
242245
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * this.scale);
243246

244247
var vertices = ObjectPool<uiList<Vector3>>.alloc();
245248
vertices.SetCapacity(length * 4);
246-
249+
247250
var triangles = ObjectPool<uiList<int>>.alloc();
248251
triangles.SetCapacity(length * 6);
249252

250253
var uv = ObjectPool<uiList<Vector2>>.alloc();
251254
uv.SetCapacity(length * 4);
252-
255+
253256
for (int charIndex = 0; charIndex < length; ++charIndex) {
254257
var ch = text[charIndex + this.textBlob.textOffset];
255258
// first char as origin for mesh position
@@ -261,9 +264,9 @@ public uiMeshMesh resolveMesh() {
261264
if (fontSizeToLoad == 0) {
262265
continue;
263266
}
264-
267+
265268
font.getGlyphInfo(ch, out var glyphInfo, fontSizeToLoad, style.UnityFontStyle);
266-
269+
267270
var minX = glyphInfo.minX / this.scale;
268271
var maxX = glyphInfo.maxX / this.scale;
269272
var minY = -glyphInfo.maxY / this.scale;
@@ -299,11 +302,12 @@ public uiMeshMesh resolveMesh() {
299302
}
300303

301304
uiMeshMesh mesh = vertices.Count > 0 ? uiMeshMesh.create(null, vertices, triangles, uv) : null;
302-
305+
303306
if (_meshes.ContainsKey(key)) {
304307
ObjectPool<MeshInfo>.release(_meshes[key]);
305308
_meshes.Remove(key);
306309
}
310+
307311
_meshes[key] = MeshInfo.create(key, mesh, fontInfo.textureVersion);
308312

309313
this._mesh = mesh.transform(this.matrix);

Runtime/ui/painting/txt/text_blob.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ internal TextBlob(string text, int textOffset, int textSize, float[] positions,
99
this.textSize = textSize;
1010
this.style = style;
1111
this._bounds = bounds;
12+
this._boundsInText = null;
1213
}
1314

1415
public Rect boundsInText {
1516
get {
16-
var pos = this.getPosition(0);
17-
return Rect.fromLTWH(this._bounds.xMin + pos, this._bounds.yMin,
18-
this._bounds.width, this._bounds.height);
17+
if (this._boundsInText == null) {
18+
var pos = this.getPosition(0);
19+
this._boundsInText = Rect.fromLTWH(this._bounds.xMin + pos, this._bounds.yMin,
20+
this._bounds.width, this._bounds.height);
21+
}
22+
23+
return this._boundsInText;
1924
}
2025
}
2126

@@ -37,6 +42,8 @@ public float getPosition(int i) {
3742
internal readonly TextStyle style;
3843
readonly UnityEngine.Rect _bounds; // bounds with positions[start] as origin
3944
readonly float[] _positions;
45+
46+
Rect _boundsInText;
4047
}
4148

4249
public struct TextBlobBuilder {
@@ -87,4 +94,4 @@ public TextBlob make() {
8794
return result;
8895
}
8996
}
90-
}
97+
}

Runtime/ui/txt/paragraph.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,29 @@ public bool didExceedMaxLines {
246246
get { return this._didExceedMaxLines; }
247247
}
248248

249-
static List<Paragraph> _paragraphs = new List<Paragraph>();
249+
static List<Paragraph> _paragraphPool = new List<Paragraph>();
250250

251251
public static Paragraph create() {
252-
if (_paragraphs.isEmpty()) {
252+
if (_paragraphPool.isEmpty()) {
253253
return new Paragraph();
254254
}
255255

256-
Paragraph ret = _paragraphs.last();
257-
_paragraphs.RemoveAt(_paragraphs.Count - 1);
256+
Paragraph ret = _paragraphPool.last();
257+
_paragraphPool.RemoveAt(_paragraphPool.Count - 1);
258258
return ret;
259259
}
260260

261261
public static void release(ref Paragraph paragraph) {
262262
if (paragraph != null) {
263-
_paragraphs.Add(paragraph);
263+
_paragraphPool.Add(paragraph);
264264
paragraph = null;
265265
}
266266
}
267267

268+
readonly Paint _textPaint = new Paint {
269+
filterMode = FilterMode.Bilinear
270+
};
271+
268272
public void paint(Canvas canvas, Offset offset) {
269273
for (int i = 0; i < this._paintRecordsCount; i++) {
270274
var paintRecord = this._paintRecords[i];
@@ -273,11 +277,8 @@ public void paint(Canvas canvas, Offset offset) {
273277

274278
for (int i = 0; i < this._paintRecordsCount; i++) {
275279
var paintRecord = this._paintRecords[i];
276-
var paint = new Paint {
277-
filterMode = FilterMode.Bilinear,
278-
color = paintRecord.style.color
279-
};
280-
canvas.drawTextBlob(paintRecord.text, paintRecord.shiftedOffset(offset), paint);
280+
this._textPaint.color = paintRecord.style.color;
281+
canvas.drawTextBlob(paintRecord.text, paintRecord.shiftedOffset(offset), this._textPaint);
281282
this.paintDecorations(canvas, paintRecord, offset);
282283
}
283284
}
@@ -919,6 +920,7 @@ int _addStyleRuns(LineBreaker lineBreaker, ref int runIndex, int blockStart, int
919920

920921
runIndex++;
921922
}
923+
922924
this._maxIntrinsicWidth = Mathf.Max(lineBlockWidth, this._maxIntrinsicWidth);
923925

924926
return countRuns;
@@ -1000,20 +1002,19 @@ void paintDecorations(Canvas canvas, PaintRecord record, Offset baseOffset) {
10001002
return;
10011003
}
10021004

1003-
var paint = new Paint();
10041005
if (record.style.decorationColor == null) {
1005-
paint.color = record.style.color;
1006+
this._textPaint.color = record.style.color;
10061007
}
10071008
else {
1008-
paint.color = record.style.decorationColor;
1009+
this._textPaint.color = record.style.decorationColor;
10091010
}
10101011

10111012

10121013
var width = record.runWidth;
10131014
var metrics = record.metrics;
10141015
float underLineThickness = metrics.underlineThickness ?? (record.style.fontSize / 14.0f);
1015-
paint.style = PaintingStyle.stroke;
1016-
paint.strokeWidth = underLineThickness;
1016+
this._textPaint.style = PaintingStyle.stroke;
1017+
this._textPaint.strokeWidth = underLineThickness;
10171018
var recordOffset = record.shiftedOffset(baseOffset);
10181019
var x = recordOffset.dx;
10191020
var y = recordOffset.dy;
@@ -1033,23 +1034,26 @@ void paintDecorations(Canvas canvas, PaintRecord record, Offset baseOffset) {
10331034
if (decoration != null && decoration.contains(TextDecoration.underline)) {
10341035
// underline
10351036
yOffset += metrics.underlinePosition ?? underLineThickness;
1036-
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
1037+
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), this._textPaint);
10371038
yOffset = yOffsetOriginal;
10381039
}
10391040

10401041
if (decoration != null && decoration.contains(TextDecoration.overline)) {
10411042
yOffset += metrics.ascent;
1042-
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
1043+
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), this._textPaint);
10431044
yOffset = yOffsetOriginal;
10441045
}
10451046

10461047
if (decoration != null && decoration.contains(TextDecoration.lineThrough)) {
10471048
yOffset += (decorationCount - 1.0f) * underLineThickness * kFloatDecorationSpacing / -2.0f;
10481049
yOffset += metrics.strikeoutPosition ?? (metrics.fxHeight ?? 0) / -2.0f;
1049-
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
1050+
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), this._textPaint);
10501051
yOffset = yOffsetOriginal;
10511052
}
10521053
}
1054+
1055+
this._textPaint.style = PaintingStyle.fill;
1056+
this._textPaint.strokeWidth = 0;
10531057
}
10541058

10551059
void paintBackground(Canvas canvas, PaintRecord record, Offset baseOffset) {

0 commit comments

Comments
 (0)