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

Commit 4124a88

Browse files
author
Yuncong Zhang
committed
Resize the emoji.
1 parent 7756b38 commit 4124a88

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

Runtime/ui/painting/txt/mesh_generator.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,29 @@ public MeshMesh resolveMesh() {
136136

137137
var text = this.textBlob.text;
138138
var key = new MeshKey(this.textBlob.instanceId, this.scale);
139+
var fontInfo = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle);
140+
var font = fontInfo.font;
141+
139142
if (char.IsHighSurrogate(text[this.textBlob.textOffset])) {
140143
D.assert(this.textBlob.textSize == 2);
141144

142145
char a = text[this.textBlob.textOffset], b = text[this.textBlob.textOffset+1];
143146
D.assert(char.IsLowSurrogate(b));
144147

145148
var pos = this.textBlob.positions[0];
149+
var metrics = FontMetrics.fromFont(font, style.UnityFontSize);
150+
151+
var minMaxRect = EmojiUtils.getMinMaxRect(style.fontSize, metrics.ascent, metrics.descent);
152+
var minX = minMaxRect.left;
153+
var maxX = minMaxRect.right;
154+
var minY = minMaxRect.top;
155+
var maxY = minMaxRect.bottom;
156+
146157
var vert = new List<Vector3> {
147-
new Vector3(pos.x, pos.y - style.fontSize, 0),
148-
new Vector3(pos.x + style.fontSize, pos.y - style.fontSize, 0),
149-
new Vector3(pos.x + style.fontSize, pos.y, 0),
150-
new Vector3(pos.x, pos.y, 0),
158+
new Vector3(pos.x + minX, pos.y + minY, 0),
159+
new Vector3(pos.x + maxX, pos.y + minY, 0),
160+
new Vector3(pos.x + maxX, pos.y + maxY, 0),
161+
new Vector3(pos.x + minX, pos.y + maxY, 0),
151162
};
152163
var tri = new List<int> {
153164
0, 1, 2, 0, 2, 3,
@@ -167,7 +178,6 @@ public MeshMesh resolveMesh() {
167178
return this._mesh;
168179
}
169180

170-
var fontInfo = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle);
171181

172182
_meshes.TryGetValue(key, out var meshInfo);
173183
if (meshInfo != null && meshInfo.textureVersion == fontInfo.textureVersion) {
@@ -176,7 +186,6 @@ public MeshMesh resolveMesh() {
176186
return this._mesh;
177187
}
178188

179-
var font = fontInfo.font;
180189
var length = this.textBlob.textSize;
181190
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * this.scale);
182191

Runtime/ui/txt/emoji.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static Image image {
1414
Resources.Load<Texture2D>("Emoji")
1515
);
1616
}
17-
catch (Exception e) {
17+
catch (Exception) {
1818
_image = null;
1919
}
2020
}
@@ -45,6 +45,10 @@ public static Image image {
4545
public const int rowCount = 4;
4646
public const int colCount = 4;
4747

48+
public static Rect getMinMaxRect(float fontSize, float ascent, float descent) {
49+
return Rect.fromLTWH(fontSize * 0.05f, descent - fontSize, fontSize * 0.9f, fontSize * 0.9f);
50+
}
51+
4852
public static Rect getUVRect(int code) {
4953
bool exist = emojiLookupTable.TryGetValue(code, out int index);
5054
if (exist) {

Runtime/ui/txt/layout.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ public void doLayout(float offset, TextBuff buff, int start, int count, TextStyl
3838
this._advance = 0;
3939
this._bounds = default;
4040

41-
Font font;
41+
Font font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
4242

4343
if (char.IsHighSurrogate(buff.text[buff.offset + start])) {
4444
D.assert(count == 2);
4545
D.assert(char.IsLowSurrogate(buff.text[buff.offset + start+1]));
4646

47-
this.layoutEmoji(style);
47+
this.layoutEmoji(style, font);
4848
}
4949
else {
50-
font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
5150
font.RequestCharactersInTextureSafe(buff.text, style.UnityFontSize, style.UnityFontStyle);
5251

5352
int wordstart = start == buff.size
@@ -128,19 +127,21 @@ void layoutWord(float offset, int layoutOffset,
128127
this._advance = x;
129128
}
130129

131-
void layoutEmoji(TextStyle style) {
130+
void layoutEmoji(TextStyle style, Font font) {
132131
float x = this._advance;
133132
float letterSpace = style.letterSpacing;
134133
float letterSpaceHalfLeft = letterSpace * 0.5f;
135134
float letterSpaceHalfRight = letterSpace - letterSpaceHalfLeft;
136135

137136
x += letterSpaceHalfLeft;
138137
this._advances[0] += letterSpaceHalfLeft;
138+
139+
var metrics = FontMetrics.fromFont(font, style.UnityFontSize);
139140

140141
var minX = x;
141-
var maxX = style.fontSize + x;
142-
var minY = -style.fontSize;
143-
var maxY = 0;
142+
var maxX = metrics.descent - metrics.ascent + x;
143+
var minY = metrics.ascent;
144+
var maxY = metrics.descent;
144145

145146
if (this._bounds.width <= 0 || this._bounds.height <= 0) {
146147
this._bounds = UnityEngine.Rect.MinMaxRect(

Tests/Editor/Paragraph.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,17 @@ RenderBox text() {
120120
new TextSpan(style: new TextStyle(fontSize: 24),
121121
text: "Emoji \ud83d\ude0a\ud83d\ude0b\ud83d\ude0d\ud83d\ude0e\ud83d\ude00"),
122122
new TextSpan(style: new TextStyle(fontSize: 14),
123-
text: "Emoji \ud83d\ude0a\ud83d\ude0b\ud83d\ude0d\ud83d\ude0e\ud83d\ude00"),
123+
text: "Emoji \ud83d\ude0a\ud83d\ude0b\ud83d\ude0d\ud83d\ude0e\ud83d\ude00 Emoji"),
124+
new TextSpan(style: new TextStyle(fontSize: 18),
125+
text: "Emoji \ud83d\ude01\ud83d\ude02\ud83d\ude03\ud83d\ude04\ud83d\ude05"),
126+
new TextSpan(style: new TextStyle(fontSize: 18),
127+
text: "Emoji \ud83d\ude01\ud83d\ude02\ud83d\ude03\ud83d\ude04\ud83d\ude05 Emoji Emoji"),
128+
new TextSpan(style: new TextStyle(fontSize: 18),
129+
text: "Emoji \ud83d\ude01\ud83d\ude02\ud83d\ude03\ud83d\ude04\ud83d\ude05 Emoji Emoji Emoji"),
124130
new TextSpan(style: new TextStyle(fontSize: 18),
125-
text: "\ud83d\ude01\ud83d\ude02\ud83d\ude03\ud83d\ude04\ud83d\ude05"),
131+
text: "Emoji \ud83d\ude01\ud83d\ude02\ud83d\ude03\ud83d\ude04\ud83d\ude05"),
126132
new TextSpan(style: new TextStyle(fontSize: 24),
127-
text: "\ud83d\ude06\ud83d\ude1C\ud83d\ude18\ud83d\ude2D\ud83d\ude0C\ud83d\ude1E"),
133+
text: "Emoji \ud83d\ude06\ud83d\ude1C\ud83d\ude18\ud83d\ude2D\ud83d\ude0C\ud83d\ude1EEmoji"),
128134
new TextSpan(style: new TextStyle(fontSize: 14),
129135
text: "FontSize 14"),
130136
})));

0 commit comments

Comments
 (0)