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

Commit 7044769

Browse files
authored
Merge pull request #378 from UnityTech/text_shadow
Implement text shadow.
2 parents 08b338e + 8157c99 commit 7044769

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

Runtime/painting/text_style.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class TextStyle : Diagnosticable, IEquatable<TextStyle> {
2424
public readonly Paint foreground;
2525
public readonly Paint background;
2626
public readonly string fontFamily;
27+
public readonly List<BoxShadow> shadows;
2728

2829
public List<string> fontFamilyFallback {
2930
get { return this._fontFamilyFallback; }
@@ -58,6 +59,7 @@ public TextStyle(bool inherit = true,
5859
float? decorationThickness = null,
5960
string fontFamily = null,
6061
List<string> fontFamilyFallback = null,
62+
List<BoxShadow> shadows = null,
6163
string debugLabel = null) {
6264
D.assert(color == null || foreground == null, () => _kColorForegroundWarning);
6365
D.assert(backgroundColor == null || background == null, () => _kColorBackgroundWarning);
@@ -80,22 +82,22 @@ public TextStyle(bool inherit = true,
8082
this.debugLabel = debugLabel;
8183
this.foreground = foreground;
8284
this.background = background;
85+
this.shadows = shadows;
8386
}
8487

8588
public RenderComparison compareTo(TextStyle other) {
86-
if (this.inherit != other.inherit || this.fontFamily != other.fontFamily
87-
|| this.fontSize != other.fontSize || this.fontWeight != other.fontWeight
88-
|| this.fontStyle != other.fontStyle ||
89-
this.letterSpacing != other.letterSpacing
90-
|| this.wordSpacing != other.wordSpacing ||
91-
this.textBaseline != other.textBaseline
92-
|| this.height != other.height || this.background != other.background) {
89+
if (this.inherit != other.inherit || this.fontFamily != other.fontFamily ||
90+
this.fontSize != other.fontSize || this.fontWeight != other.fontWeight ||
91+
this.fontStyle != other.fontStyle || this.letterSpacing != other.letterSpacing ||
92+
this.wordSpacing != other.wordSpacing || this.textBaseline != other.textBaseline ||
93+
this.height != other.height || this.background != other.background ||
94+
this.shadows.equalsList(other.shadows)) {
9395
return RenderComparison.layout;
9496
}
9597

9698
if (this.color != other.color || this.decoration != other.decoration ||
97-
this.decorationColor != other.decorationColor
98-
|| this.decorationStyle != other.decorationStyle) {
99+
this.decorationColor != other.decorationColor ||
100+
this.decorationStyle != other.decorationStyle) {
99101
return RenderComparison.paint;
100102
}
101103

@@ -122,6 +124,7 @@ public TextStyle apply(
122124
float decorationThicknessDelta = 0.0f,
123125
string fontFamily = null,
124126
List<string> fontFamilyFallback = null,
127+
List<BoxShadow> shadows = null,
125128
float fontSizeFactor = 1.0f,
126129
float fontSizeDelta = 0.0f,
127130
int fontWeightDelta = 0,
@@ -172,6 +175,7 @@ public TextStyle apply(
172175
decorationThickness: this.decorationThickness == null
173176
? null
174177
: this.decorationThickness * decorationThicknessFactor + decorationThicknessDelta,
178+
shadows: shadows ?? this.shadows,
175179
debugLabel: modifiedDebugLabel
176180
);
177181
}
@@ -213,6 +217,7 @@ public TextStyle merge(TextStyle other) {
213217
decorationColor: other.decorationColor,
214218
decorationStyle: other.decorationStyle,
215219
decorationThickness: other.decorationThickness,
220+
shadows: other.shadows,
216221
debugLabel: mergedDebugLabel
217222
);
218223
}
@@ -236,6 +241,7 @@ public TextStyle copyWith(
236241
Color decorationColor = null,
237242
TextDecorationStyle? decorationStyle = null,
238243
float? decorationThickness = null,
244+
List<BoxShadow> shadows = null,
239245
string debugLabel = null) {
240246
D.assert(color == null || foreground == null, () => _kColorForegroundWarning);
241247
D.assert(backgroundColor == null || background == null, () => _kColorBackgroundWarning);
@@ -267,6 +273,7 @@ public TextStyle copyWith(
267273
decorationThickness: decorationThickness ?? this.decorationThickness,
268274
foreground: foreground ?? this.foreground,
269275
background: background ?? this.background,
276+
shadows: shadows ?? this.shadows,
270277
debugLabel: newDebugLabel
271278
);
272279
}
@@ -304,6 +311,7 @@ public static TextStyle lerp(TextStyle a, TextStyle b, float t) {
304311
decorationColor: Color.lerp(null, b.decorationColor, t),
305312
decorationStyle: t < 0.5f ? null : b.decorationStyle,
306313
decorationThickness: t < 0.5f ? null : b.decorationThickness,
314+
shadows: t < 0.5f ? null : b.shadows,
307315
debugLabel: lerpDebugLabel
308316
);
309317
}
@@ -328,6 +336,7 @@ public static TextStyle lerp(TextStyle a, TextStyle b, float t) {
328336
decorationColor: Color.lerp(a.decorationColor, null, t),
329337
decorationStyle: t < 0.5f ? a.decorationStyle : null,
330338
decorationThickness: t < 0.5f ? a.decorationThickness : null,
339+
shadows: t < 0.5f ? a.shadows : null,
331340
debugLabel: lerpDebugLabel
332341
);
333342
}
@@ -365,6 +374,7 @@ public static TextStyle lerp(TextStyle a, TextStyle b, float t) {
365374
decorationThickness: MathUtils.lerpFloat(
366375
a.decorationThickness ?? b.decorationThickness ?? 0.0f,
367376
b.decorationThickness ?? a.decorationThickness ?? 0.0f, t),
377+
shadows: t < 0.5f ? a.shadows : b.shadows,
368378
debugLabel: lerpDebugLabel
369379
);
370380
}
@@ -471,7 +481,8 @@ public bool Equals(TextStyle other) {
471481
this.decorationThickness == other.decorationThickness &&
472482
Equals(this.foreground, other.foreground) &&
473483
Equals(this.background, other.background) &&
474-
CollectionUtils.equalsList(this.fontFamilyFallback, other.fontFamilyFallback) &&
484+
this.fontFamilyFallback.equalsList(other.fontFamilyFallback) &&
485+
this.shadows.equalsList(other.shadows) &&
475486
string.Equals(this.fontFamily, other.fontFamily);
476487
}
477488

@@ -512,6 +523,7 @@ public override int GetHashCode() {
512523
hashCode = (hashCode * 397) ^ (this.fontFamily != null ? this.fontFamily.GetHashCode() : 0);
513524
hashCode = (hashCode * 397) ^
514525
(this.fontFamilyFallback != null ? this.fontFamilyFallback.GetHashCode() : 0);
526+
hashCode = (hashCode * 397) ^ (this.shadows != null ? this.shadows.GetHashCode() : 0);
515527
return hashCode;
516528
}
517529
}

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ void _drawPicture(Picture picture, bool needsSave = true) {
870870
}
871871

872872
case DrawTextBlob cmd: {
873+
this._paintTextShadow(cmd.textBlob, cmd.offset);
873874
this._drawTextBlob(cmd.textBlob, (uiOffset.fromOffset(cmd.offset)).Value,
874875
uiPaint.fromPaint(cmd.paint));
875876
break;
@@ -994,6 +995,7 @@ void _drawUIPicture(uiPicture picture, bool needsSave = true) {
994995
}
995996

996997
case uiDrawTextBlob cmd: {
998+
this._paintTextShadow(cmd.textBlob, new Offset(cmd.offset.Value.dx, cmd.offset.Value.dy));
997999
this._drawTextBlob(cmd.textBlob, cmd.offset.Value, cmd.paint);
9981000
break;
9991001
}
@@ -1012,6 +1014,22 @@ void _drawUIPicture(uiPicture picture, bool needsSave = true) {
10121014
}
10131015
}
10141016

1017+
void _paintTextShadow(TextBlob? textBlob, Offset offset) {
1018+
D.assert(textBlob != null);
1019+
if (textBlob.Value.style.shadows != null && textBlob.Value.style.shadows.isNotEmpty()) {
1020+
textBlob.Value.style.shadows.ForEach(shadow => {
1021+
Paint paint = new Paint {
1022+
color = shadow.color,
1023+
maskFilter = shadow.blurRadius != 0
1024+
? MaskFilter.blur(BlurStyle.normal, shadow.blurRadius)
1025+
: null,
1026+
};
1027+
this._drawTextBlob(textBlob, uiOffset.fromOffset(shadow.offset + offset).Value,
1028+
uiPaint.fromPaint(paint));
1029+
});
1030+
}
1031+
}
1032+
10151033
void _drawTextBlob(TextBlob? textBlob, uiOffset offset, uiPaint paint) {
10161034
D.assert(textBlob != null);
10171035

@@ -1043,6 +1061,7 @@ void _drawTextBlob(TextBlob? textBlob, uiOffset offset, uiPaint paint) {
10431061
}
10441062

10451063
this._drawTextDrawMeshCallback(paint, null, null, false, 0, 0, tex, textBlobBounds, mesh, notEmoji);
1064+
10461065
}
10471066

10481067
public void flush(uiPicture picture) {

Runtime/ui/text.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Unity.UIWidgets.foundation;
34
using Unity.UIWidgets.painting;
45

56
namespace Unity.UIWidgets.ui {
@@ -99,6 +100,7 @@ class TextStyle : IEquatable<TextStyle> {
99100
public readonly string fontFamily = kDefaultFontFamily;
100101
public readonly Paint foreground;
101102
public readonly Paint background;
103+
public readonly List<BoxShadow> shadows;
102104

103105
internal UnityEngine.Color UnityColor {
104106
get { return this.color.toColor(); }
@@ -142,7 +144,8 @@ public static TextStyle applyStyle(TextStyle currentStyle, painting.TextStyle st
142144
decorationColor: style.decorationColor ?? currentStyle.decorationColor,
143145
fontFamily: style.fontFamily ?? currentStyle.fontFamily,
144146
foreground: style.foreground ?? currentStyle.foreground,
145-
background: style.background ?? currentStyle.background
147+
background: style.background ?? currentStyle.background,
148+
shadows: style.shadows ?? currentStyle.shadows
146149
);
147150
}
148151

@@ -159,7 +162,8 @@ public static TextStyle applyStyle(TextStyle currentStyle, painting.TextStyle st
159162
decorationColor: style.decorationColor,
160163
fontFamily: style.fontFamily,
161164
foreground: style.foreground,
162-
background: style.background
165+
background: style.background,
166+
shadows: style.shadows
163167
);
164168
}
165169

@@ -179,7 +183,8 @@ public bool Equals(TextStyle other) {
179183
Equals(this.decoration, other.decoration) &&
180184
Equals(this.decorationColor, other.decorationColor) &&
181185
this.decorationStyle == other.decorationStyle &&
182-
string.Equals(this.fontFamily, other.fontFamily);
186+
string.Equals(this.fontFamily, other.fontFamily) &&
187+
this.shadows.equalsList(other.shadows);
183188
}
184189

185190
public override bool Equals(object obj) {
@@ -212,6 +217,7 @@ public override int GetHashCode() {
212217
hashCode = (hashCode * 397) ^ (this.decorationColor != null ? this.decorationColor.GetHashCode() : 0);
213218
hashCode = (hashCode * 397) ^ this.decorationStyle.GetHashCode();
214219
hashCode = (hashCode * 397) ^ (this.fontFamily != null ? this.fontFamily.GetHashCode() : 0);
220+
hashCode = (hashCode * 397) ^ (this.shadows != null ? this.shadows.GetHashCode() : 0);
215221
return hashCode;
216222
}
217223
}
@@ -225,13 +231,21 @@ public override int GetHashCode() {
225231
}
226232

227233

228-
public TextStyle(Color color = null, float? fontSize = null,
229-
FontWeight fontWeight = null, FontStyle? fontStyle = null, float? letterSpacing = null,
230-
float? wordSpacing = null, TextBaseline? textBaseline = null, float? height = null,
231-
TextDecoration decoration = null, TextDecorationStyle? decorationStyle = null, Color decorationColor = null,
234+
public TextStyle(Color color = null,
235+
float? fontSize = null,
236+
FontWeight fontWeight = null,
237+
FontStyle? fontStyle = null,
238+
float? letterSpacing = null,
239+
float? wordSpacing = null,
240+
TextBaseline? textBaseline = null,
241+
float? height = null,
242+
TextDecoration decoration = null,
243+
TextDecorationStyle? decorationStyle = null,
244+
Color decorationColor = null,
232245
string fontFamily = null,
233246
Paint foreground = null,
234-
Paint background = null
247+
Paint background = null,
248+
List<BoxShadow> shadows = null
235249
) {
236250
this.color = color ?? this.color;
237251
this.fontSize = fontSize ?? this.fontSize;
@@ -248,6 +262,7 @@ public TextStyle(Color color = null, float? fontSize = null,
248262
this.fontFamily = fontFamily ?? this.fontFamily;
249263
this.foreground = foreground ?? this.foreground;
250264
this.background = background ?? this.background;
265+
this.shadows = shadows ?? this.shadows;
251266
}
252267
}
253268

0 commit comments

Comments
 (0)