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

Commit 952ece9

Browse files
author
Yuncong Zhang
committed
Support single-char emoji.
1 parent 04e8fbc commit 952ece9

File tree

6 files changed

+317
-175
lines changed

6 files changed

+317
-175
lines changed

Runtime/painting/text_span.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class TextSpan : DiagnosticableTree, IEquatable<TextSpan> {
2121
public TextSpan(string text = "", TextStyle style = null, List<TextSpan> children = null,
2222
GestureRecognizer recognizer = null, HoverRecognizer hoverRecognizer = null) {
2323
this.text = text;
24-
this.splitedText = !string.IsNullOrEmpty(text) ? EmojiUtils.splitBySurrogatePair(text) : null;
24+
this.splitedText = !string.IsNullOrEmpty(text) ? EmojiUtils.splitByEmoji(text) : null;
2525
this.style = style;
2626
this.children = children;
2727
this.recognizer = recognizer;

Runtime/ui/painting/canvas_impl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void _drawTextBlob(TextBlob textBlob, Offset offset, Paint paint) {
752752
var subText = textBlob.text.Substring(textBlob.textOffset, textBlob.textSize);
753753

754754
Texture tex = null;
755-
bool alpha = !char.IsHighSurrogate(subText[0]);
755+
bool alpha = !char.IsHighSurrogate(subText[0]) && !EmojiUtils.isSingleCharEmoji(subText[0]);
756756
if (alpha) {
757757
font.RequestCharactersInTextureSafe(subText, fontSizeToLoad, style.UnityFontStyle);
758758
tex = font.material.mainTexture;

Runtime/ui/painting/txt/mesh_generator.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ public MeshMesh resolveMesh() {
139139
var fontInfo = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle);
140140
var font = fontInfo.font;
141141

142-
if (char.IsHighSurrogate(text[this.textBlob.textOffset])) {
143-
D.assert(this.textBlob.textSize % 2 == 0);
142+
// Handling Emoji
143+
char startingChar = text[this.textBlob.textOffset];
144+
if (char.IsHighSurrogate(startingChar) || EmojiUtils.isSingleCharEmoji(startingChar)) {
144145
var vert = new List<Vector3>();
145146
var tri = new List<int>();
146147
var uvCoord = new List<Vector2>();
@@ -151,29 +152,40 @@ public MeshMesh resolveMesh() {
151152
var minY = minMaxRect.top;
152153
var maxY = minMaxRect.bottom;
153154

154-
for (int i = 0; i < this.textBlob.textSize; i += 2) {
155-
char a = text[this.textBlob.textOffset+i], b = text[this.textBlob.textOffset+i+1];
156-
D.assert(char.IsLowSurrogate(b));
155+
for (int i = 0; i < this.textBlob.textSize; i++) {
156+
char a = text[this.textBlob.textOffset + i];
157+
int code = a;
158+
if (char.IsHighSurrogate(a)) {
159+
D.assert(i+1 < this.textBlob.textSize);
160+
D.assert(this.textBlob.textOffset+i+1 < this.textBlob.text.Length);
161+
char b = text[this.textBlob.textOffset+i+1];
162+
D.assert(char.IsLowSurrogate(b));
163+
code = char.ConvertToUtf32(a, b);
164+
} else if (char.IsLowSurrogate(a) || EmojiUtils.isEmptyEmoji(a)) {
165+
continue;
166+
}
167+
var uvRect = EmojiUtils.getUVRect(code);
157168

158169
var pos = this.textBlob.positions[i];
159-
170+
171+
int baseIndex = vert.Count;
160172
vert.Add(new Vector3(pos.x + minX, pos.y + minY, 0));
161173
vert.Add(new Vector3(pos.x + maxX, pos.y + minY, 0));
162174
vert.Add(new Vector3(pos.x + maxX, pos.y + maxY, 0));
163175
vert.Add(new Vector3(pos.x + minX, pos.y + maxY, 0));
164176

165-
tri.Add(i * 2);
166-
tri.Add(i * 2 + 1);
167-
tri.Add(i * 2 + 2);
168-
tri.Add(i * 2);
169-
tri.Add(i * 2 + 2);
170-
tri.Add(i * 2 + 3);
171-
var code = char.ConvertToUtf32(a, b);
172-
var uvRect = EmojiUtils.getUVRect(code);
177+
tri.Add(baseIndex);
178+
tri.Add(baseIndex + 1);
179+
tri.Add(baseIndex + 2);
180+
tri.Add(baseIndex);
181+
tri.Add(baseIndex + 2);
182+
tri.Add(baseIndex + 3);
173183
uvCoord.Add(uvRect.bottomLeft.toVector());
174184
uvCoord.Add(uvRect.bottomRight.toVector());
175185
uvCoord.Add(uvRect.topRight.toVector());
176186
uvCoord.Add(uvRect.topLeft.toVector());
187+
188+
if(char.IsHighSurrogate(a)) i++;
177189
}
178190
MeshMesh meshMesh = new MeshMesh(null, vert, tri, uvCoord);
179191
_meshes[key] = new MeshInfo(key, meshMesh, 0);

0 commit comments

Comments
 (0)