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

Implement text shadow. #378

Merged
merged 2 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions Runtime/painting/text_style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TextStyle : Diagnosticable, IEquatable<TextStyle> {
public readonly Paint foreground;
public readonly Paint background;
public readonly string fontFamily;
public readonly List<BoxShadow> shadows;

public List<string> fontFamilyFallback {
get { return this._fontFamilyFallback; }
Expand Down Expand Up @@ -58,6 +59,7 @@ public TextStyle(bool inherit = true,
float? decorationThickness = null,
string fontFamily = null,
List<string> fontFamilyFallback = null,
List<BoxShadow> shadows = null,
string debugLabel = null) {
D.assert(color == null || foreground == null, () => _kColorForegroundWarning);
D.assert(backgroundColor == null || background == null, () => _kColorBackgroundWarning);
Expand All @@ -80,22 +82,22 @@ public TextStyle(bool inherit = true,
this.debugLabel = debugLabel;
this.foreground = foreground;
this.background = background;
this.shadows = shadows;
}

public RenderComparison compareTo(TextStyle other) {
if (this.inherit != other.inherit || this.fontFamily != other.fontFamily
|| this.fontSize != other.fontSize || this.fontWeight != other.fontWeight
|| this.fontStyle != other.fontStyle ||
this.letterSpacing != other.letterSpacing
|| this.wordSpacing != other.wordSpacing ||
this.textBaseline != other.textBaseline
|| this.height != other.height || this.background != other.background) {
if (this.inherit != other.inherit || this.fontFamily != other.fontFamily ||
this.fontSize != other.fontSize || this.fontWeight != other.fontWeight ||
this.fontStyle != other.fontStyle || this.letterSpacing != other.letterSpacing ||
this.wordSpacing != other.wordSpacing || this.textBaseline != other.textBaseline ||
this.height != other.height || this.background != other.background ||
this.shadows.equalsList(other.shadows)) {
return RenderComparison.layout;
}

if (this.color != other.color || this.decoration != other.decoration ||
this.decorationColor != other.decorationColor
|| this.decorationStyle != other.decorationStyle) {
this.decorationColor != other.decorationColor ||
this.decorationStyle != other.decorationStyle) {
return RenderComparison.paint;
}

Expand All @@ -122,6 +124,7 @@ public TextStyle apply(
float decorationThicknessDelta = 0.0f,
string fontFamily = null,
List<string> fontFamilyFallback = null,
List<BoxShadow> shadows = null,
float fontSizeFactor = 1.0f,
float fontSizeDelta = 0.0f,
int fontWeightDelta = 0,
Expand Down Expand Up @@ -172,6 +175,7 @@ public TextStyle apply(
decorationThickness: this.decorationThickness == null
? null
: this.decorationThickness * decorationThicknessFactor + decorationThicknessDelta,
shadows: shadows ?? this.shadows,
debugLabel: modifiedDebugLabel
);
}
Expand Down Expand Up @@ -213,6 +217,7 @@ public TextStyle merge(TextStyle other) {
decorationColor: other.decorationColor,
decorationStyle: other.decorationStyle,
decorationThickness: other.decorationThickness,
shadows: other.shadows,
debugLabel: mergedDebugLabel
);
}
Expand All @@ -236,6 +241,7 @@ public TextStyle copyWith(
Color decorationColor = null,
TextDecorationStyle? decorationStyle = null,
float? decorationThickness = null,
List<BoxShadow> shadows = null,
string debugLabel = null) {
D.assert(color == null || foreground == null, () => _kColorForegroundWarning);
D.assert(backgroundColor == null || background == null, () => _kColorBackgroundWarning);
Expand Down Expand Up @@ -267,6 +273,7 @@ public TextStyle copyWith(
decorationThickness: decorationThickness ?? this.decorationThickness,
foreground: foreground ?? this.foreground,
background: background ?? this.background,
shadows: shadows ?? this.shadows,
debugLabel: newDebugLabel
);
}
Expand Down Expand Up @@ -304,6 +311,7 @@ public static TextStyle lerp(TextStyle a, TextStyle b, float t) {
decorationColor: Color.lerp(null, b.decorationColor, t),
decorationStyle: t < 0.5f ? null : b.decorationStyle,
decorationThickness: t < 0.5f ? null : b.decorationThickness,
shadows: t < 0.5f ? null : b.shadows,
debugLabel: lerpDebugLabel
);
}
Expand All @@ -328,6 +336,7 @@ public static TextStyle lerp(TextStyle a, TextStyle b, float t) {
decorationColor: Color.lerp(a.decorationColor, null, t),
decorationStyle: t < 0.5f ? a.decorationStyle : null,
decorationThickness: t < 0.5f ? a.decorationThickness : null,
shadows: t < 0.5f ? a.shadows : null,
debugLabel: lerpDebugLabel
);
}
Expand Down Expand Up @@ -365,6 +374,7 @@ public static TextStyle lerp(TextStyle a, TextStyle b, float t) {
decorationThickness: MathUtils.lerpFloat(
a.decorationThickness ?? b.decorationThickness ?? 0.0f,
b.decorationThickness ?? a.decorationThickness ?? 0.0f, t),
shadows: t < 0.5f ? a.shadows : b.shadows,
debugLabel: lerpDebugLabel
);
}
Expand Down Expand Up @@ -471,7 +481,8 @@ public bool Equals(TextStyle other) {
this.decorationThickness == other.decorationThickness &&
Equals(this.foreground, other.foreground) &&
Equals(this.background, other.background) &&
CollectionUtils.equalsList(this.fontFamilyFallback, other.fontFamilyFallback) &&
this.fontFamilyFallback.equalsList(other.fontFamilyFallback) &&
this.shadows.equalsList(other.shadows) &&
string.Equals(this.fontFamily, other.fontFamily);
}

Expand Down Expand Up @@ -512,6 +523,7 @@ public override int GetHashCode() {
hashCode = (hashCode * 397) ^ (this.fontFamily != null ? this.fontFamily.GetHashCode() : 0);
hashCode = (hashCode * 397) ^
(this.fontFamilyFallback != null ? this.fontFamilyFallback.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (this.shadows != null ? this.shadows.GetHashCode() : 0);
return hashCode;
}
}
Expand Down
19 changes: 19 additions & 0 deletions Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,7 @@ void _drawPicture(Picture picture, bool needsSave = true) {
}

case DrawTextBlob cmd: {
this._paintTextShadow(cmd.textBlob, cmd.offset);
this._drawTextBlob(cmd.textBlob, (uiOffset.fromOffset(cmd.offset)).Value,
uiPaint.fromPaint(cmd.paint));
break;
Expand Down Expand Up @@ -994,6 +995,7 @@ void _drawUIPicture(uiPicture picture, bool needsSave = true) {
}

case uiDrawTextBlob cmd: {
this._paintTextShadow(cmd.textBlob, new Offset(cmd.offset.Value.dx, cmd.offset.Value.dy));
this._drawTextBlob(cmd.textBlob, cmd.offset.Value, cmd.paint);
break;
}
Expand All @@ -1012,6 +1014,22 @@ void _drawUIPicture(uiPicture picture, bool needsSave = true) {
}
}

void _paintTextShadow(TextBlob? textBlob, Offset offset) {
D.assert(textBlob != null);
if (textBlob.Value.style.shadows != null && textBlob.Value.style.shadows.isNotEmpty()) {
textBlob.Value.style.shadows.ForEach(shadow => {
Paint paint = new Paint {
color = shadow.color,
maskFilter = shadow.blurRadius != 0
? MaskFilter.blur(BlurStyle.normal, shadow.blurRadius)
: null,
};
this._drawTextBlob(textBlob, uiOffset.fromOffset(shadow.offset + offset).Value,
uiPaint.fromPaint(paint));
});
}
}

void _drawTextBlob(TextBlob? textBlob, uiOffset offset, uiPaint paint) {
D.assert(textBlob != null);

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

this._drawTextDrawMeshCallback(paint, null, null, false, 0, 0, tex, textBlobBounds, mesh, notEmoji);

}

public void flush(uiPicture picture) {
Expand Down
31 changes: 23 additions & 8 deletions Runtime/ui/text.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.painting;

namespace Unity.UIWidgets.ui {
Expand Down Expand Up @@ -99,6 +100,7 @@ class TextStyle : IEquatable<TextStyle> {
public readonly string fontFamily = kDefaultFontFamily;
public readonly Paint foreground;
public readonly Paint background;
public readonly List<BoxShadow> shadows;

internal UnityEngine.Color UnityColor {
get { return this.color.toColor(); }
Expand Down Expand Up @@ -142,7 +144,8 @@ public static TextStyle applyStyle(TextStyle currentStyle, painting.TextStyle st
decorationColor: style.decorationColor ?? currentStyle.decorationColor,
fontFamily: style.fontFamily ?? currentStyle.fontFamily,
foreground: style.foreground ?? currentStyle.foreground,
background: style.background ?? currentStyle.background
background: style.background ?? currentStyle.background,
shadows: style.shadows ?? currentStyle.shadows
);
}

Expand All @@ -159,7 +162,8 @@ public static TextStyle applyStyle(TextStyle currentStyle, painting.TextStyle st
decorationColor: style.decorationColor,
fontFamily: style.fontFamily,
foreground: style.foreground,
background: style.background
background: style.background,
shadows: style.shadows
);
}

Expand All @@ -179,7 +183,8 @@ public bool Equals(TextStyle other) {
Equals(this.decoration, other.decoration) &&
Equals(this.decorationColor, other.decorationColor) &&
this.decorationStyle == other.decorationStyle &&
string.Equals(this.fontFamily, other.fontFamily);
string.Equals(this.fontFamily, other.fontFamily) &&
this.shadows.equalsList(other.shadows);
}

public override bool Equals(object obj) {
Expand Down Expand Up @@ -212,6 +217,7 @@ public override int GetHashCode() {
hashCode = (hashCode * 397) ^ (this.decorationColor != null ? this.decorationColor.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.decorationStyle.GetHashCode();
hashCode = (hashCode * 397) ^ (this.fontFamily != null ? this.fontFamily.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (this.shadows != null ? this.shadows.GetHashCode() : 0);
return hashCode;
}
}
Expand All @@ -225,13 +231,21 @@ public override int GetHashCode() {
}


public TextStyle(Color color = null, float? fontSize = null,
FontWeight fontWeight = null, FontStyle? fontStyle = null, float? letterSpacing = null,
float? wordSpacing = null, TextBaseline? textBaseline = null, float? height = null,
TextDecoration decoration = null, TextDecorationStyle? decorationStyle = null, Color decorationColor = null,
public TextStyle(Color color = null,
float? fontSize = null,
FontWeight fontWeight = null,
FontStyle? fontStyle = null,
float? letterSpacing = null,
float? wordSpacing = null,
TextBaseline? textBaseline = null,
float? height = null,
TextDecoration decoration = null,
TextDecorationStyle? decorationStyle = null,
Color decorationColor = null,
string fontFamily = null,
Paint foreground = null,
Paint background = null
Paint background = null,
List<BoxShadow> shadows = null
) {
this.color = color ?? this.color;
this.fontSize = fontSize ?? this.fontSize;
Expand All @@ -248,6 +262,7 @@ public TextStyle(Color color = null, float? fontSize = null,
this.fontFamily = fontFamily ?? this.fontFamily;
this.foreground = foreground ?? this.foreground;
this.background = background ?? this.background;
this.shadows = shadows ?? this.shadows;
}
}

Expand Down