浏览代码

text decoration

/main
fzhangtj 6 年前
当前提交
8de2a7b2
共有 13 个文件被更改,包括 536 次插入114 次删除
  1. 39
      Assets/UIWidgets/Tests/CanvasAndLayers.cs
  2. 30
      Assets/UIWidgets/Tests/Paragraph.cs
  3. 117
      Assets/UIWidgets/painting/text_style.cs
  4. 12
      Assets/UIWidgets/ui/painting/canvas.cs
  5. 41
      Assets/UIWidgets/ui/painting/canvas_impl.cs
  6. 7
      Assets/UIWidgets/ui/painting/draw_cmd.cs
  7. 6
      Assets/UIWidgets/ui/painting/painting.cs
  8. 34
      Assets/UIWidgets/ui/painting/picture.cs
  9. 2
      Assets/UIWidgets/ui/painting/txt/mesh_generator.cs
  10. 216
      Assets/UIWidgets/ui/text.cs
  11. 2
      Assets/UIWidgets/ui/txt/linebreaker.cs
  12. 18
      Assets/UIWidgets/ui/txt/paint_record.cs
  13. 126
      Assets/UIWidgets/ui/txt/paragraph.cs

39
Assets/UIWidgets/Tests/CanvasAndLayers.cs


this.clipRect,
this.clipRRect,
this.saveLayer,
this.drawLine,
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;

canvas.drawPloygon4(
new[] {new Offset(10, 150), new Offset(10, 160), new Offset(140, 120), new Offset(110, 180)},
paint);
}
void drawLine() {
var canvas = new CanvasImpl();
var paint = new Paint {
color = new Color(0xFFFF0000),
};
canvas.drawLine(
new Offset(10, 20),
new Offset(50, 20),
paint);
canvas.drawLine(
new Offset(10, 10),
new Offset(100, 100),
paint);
canvas.drawLine(
new Offset(10, 10),
new Offset(10, 50),
paint);
canvas.drawLine(
new Offset(40, 10),
new Offset(90, 10),
paint);
var widthPaint = new Paint {
color = new Color(0xFFFF0000),
strokeWidth = 4,
};
canvas.drawLine(
new Offset(40, 20),
new Offset(120, 190),
widthPaint);
}
void drawRect() {

30
Assets/UIWidgets/Tests/Paragraph.cs


this.textHeight,
this.textOverflow,
this.textAlign,
this.textDecoration,
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;

})));
}
RenderBox textDecoration()
{
return box(
new RenderParagraph(new TextSpan(style: new painting.TextStyle(height:1.2), text: "", children:
new List<TextSpan>()
{
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.underline),
text: "Real-time 3D revolution\n"),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.underline, decorationStyle: TextDecorationStyle.doubleLine),
text: "Double line Real-time 3D revolution\n"),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.underline, fontSize: 24),
text: "Real-time 3D revolution\n"),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.overline),
text: "Over line Real-time 3D revolution\n"),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.overline, decorationStyle: TextDecorationStyle.doubleLine),
text: "Over line Real-time 3D revolution\n"),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.lineThrough),
text: "Line through Real-time 3D revolution\n"),
new TextSpan(style: new painting.TextStyle(color: Color.fromARGB(255, 255, 0, 0),
decoration: TextDecoration.lineThrough, decorationColor:Color.fromARGB(255, 0, 255, 0)),
text: "Color Line through Real-time 3D revolution\n"),
})), width: 400);
}
RenderBox textAlign()
{

117
Assets/UIWidgets/painting/text_style.cs


namespace UIWidgets.painting
{
public class TextStyle : Diagnosticable
public class TextStyle : Diagnosticable, IEquatable<TextStyle>
{
public static readonly double _defaultFontSize = 14.0;
public readonly bool inherit;

public readonly TextBaseline? textBaseline;
public readonly double? height;
public readonly TextDecoration decoration;
public readonly Color decorationColor;
public readonly TextDecorationStyle? decorationStyle;
public readonly Paint background;
const string _kDefaultDebugLabel = "unknown";

TextBaseline? textBaseline = null, double? height = null, TextDecoration decoration = null,
TextBaseline? textBaseline = null, double? height = null, Paint background = null,
TextDecoration decoration = null,
Color decorationColor = null, TextDecorationStyle? decorationStyle = null,
string fontFamily = null, string debugLabel = null)
{
this.inherit = inherit;

this.textBaseline = textBaseline;
this.height = height;
this.decoration = decoration;
this.decorationColor = decorationColor;
this.decorationStyle = decorationStyle;
this.background = background;
}
public ui.TextStyle getTextStyle(double textScaleFactor = 1.0)

decoration: decoration,
decorationColor: decorationColor,
decorationStyle: decorationStyle,
fontWeight: fontWeight,
fontStyle: fontStyle,
fontSize: fontSize == null ? null : fontSize * textScaleFactor,

height: height,
fontFamily: fontFamily
fontFamily: fontFamily,
background: background
);
}

|| fontSize != other.fontSize || fontWeight != other.fontWeight
|| fontStyle != other.fontStyle || letterSpacing != other.letterSpacing
|| wordSpacing != other.wordSpacing || textBaseline != other.textBaseline
|| height != other.height)
|| height != other.height || background != other.background)
if (color != other.color || decoration != other.decoration)
if (color != other.color || decoration != other.decoration || decorationColor != other.decorationColor
|| decorationStyle != other.decorationStyle)
{
return RenderComparison.paint;
}

{
return other;
}
mergedDebugLabel = string.Format("({0}).merge({1})", debugLabel??_kDefaultDebugLabel, other.debugLabel ?? _kDefaultDebugLabel);
mergedDebugLabel = string.Format("({0}).merge({1})", debugLabel ?? _kDefaultDebugLabel,
other.debugLabel ?? _kDefaultDebugLabel);
return true;
});

textBaseline: other.textBaseline,
height: other.height,
decoration: other.decoration,
decorationColor: other.decorationColor,
decorationStyle: other.decorationStyle,
debugLabel: mergedDebugLabel
);
}

Paint background = null,
TextDecoration decoration = null,
Color decorationColor = null,
TextDecorationStyle? decorationStyle = null,
D.assert(() => {
D.assert(() =>
{
}
}
return new TextStyle(
inherit: inherit,
color: color ?? this.color,

textBaseline: textBaseline ?? this.textBaseline,
height: height ?? this.height,
decoration: decoration ?? this.decoration,
decorationColor: decorationColor ?? this.decorationColor,
decorationStyle: decorationStyle ?? this.decorationStyle,
background: background ?? this.background,
debugLabel: newDebugLabel
);
}

styles.Add(new DiagnosticsProperty<double?>("wordSpacing", wordSpacing, defaultValue: null));
styles.Add(new EnumProperty<TextBaseline?>("baseline", textBaseline, defaultValue: null));
styles.Add(new DiagnosticsProperty<double?>("height", height, defaultValue: null));
styles.Add(new StringProperty("background", background == null ? null : background.ToString(), defaultValue: null, quoted: false));
List<String> decorationDescription = new List<String>();
List<string> decorationDescription = new List<string>();
if (decorationStyle != null)
{
decorationDescription.Add(decorationStyle.ToString());
}
styles.Add(new DiagnosticsProperty<Color>("decorationColor", decorationColor, defaultValue: null,
level: DiagnosticLevel.fine));
if (decorationColor != null)
{
decorationDescription.Add(decorationColor.ToString());
}
styles.Add(new DiagnosticsProperty<TextDecoration>("decoration", decoration, defaultValue: null,
level: DiagnosticLevel.hidden));
if (decoration != null)

if (!styleSpecified)
properties.add(new FlagProperty("inherit", value: inherit, ifTrue: "<all styles inherited>",
ifFalse: "<no style specified>"));
}
public bool Equals(TextStyle other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return inherit == other.inherit && Equals(color, other.color) && fontSize.Equals(other.fontSize) &&
fontWeight == other.fontWeight && fontStyle == other.fontStyle &&
letterSpacing.Equals(other.letterSpacing) && wordSpacing.Equals(other.wordSpacing) &&
textBaseline == other.textBaseline && height.Equals(other.height) &&
Equals(decoration, other.decoration) && Equals(decorationColor, other.decorationColor) &&
decorationStyle == other.decorationStyle && Equals(background, other.background) &&
string.Equals(fontFamily, other.fontFamily);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((TextStyle) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = inherit.GetHashCode();
hashCode = (hashCode * 397) ^ (color != null ? color.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ fontSize.GetHashCode();
hashCode = (hashCode * 397) ^ fontWeight.GetHashCode();
hashCode = (hashCode * 397) ^ fontStyle.GetHashCode();
hashCode = (hashCode * 397) ^ letterSpacing.GetHashCode();
hashCode = (hashCode * 397) ^ wordSpacing.GetHashCode();
hashCode = (hashCode * 397) ^ textBaseline.GetHashCode();
hashCode = (hashCode * 397) ^ height.GetHashCode();
hashCode = (hashCode * 397) ^ (decoration != null ? decoration.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (decorationColor != null ? decorationColor.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ decorationStyle.GetHashCode();
hashCode = (hashCode * 397) ^ (background != null ? background.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (fontFamily != null ? fontFamily.GetHashCode() : 0);
return hashCode;
}
}
public static bool operator ==(TextStyle left, TextStyle right)
{
return Equals(left, right);
}
public static bool operator !=(TextStyle left, TextStyle right)
{
return !Equals(left, right);
}
public override string toStringShort()
{
return this.GetType().FullName;
}
}
}

12
Assets/UIWidgets/ui/painting/canvas.cs


void drawImageRect(Rect src, Rect dst, Paint paint, Image image);
void drawLine(Offset from, Offset to, Paint paint);
void concat(Matrix4x4 transform);
void save();

image = image,
src = src,
dst = dst,
});
}
public void drawLine(Offset from, Offset to, Paint paint)
{
this._recorder.addDrawCmd(new DrawLine
{
from = from,
to = to,
paint = paint,
});
}

41
Assets/UIWidgets/ui/painting/canvas_impl.cs


else if (drawCmd is DrawRect) {
var drawRect = (DrawRect) drawCmd;
this.drawRect(drawRect.rect, drawRect.borderWidth, drawRect.borderRadius, drawRect.paint);
} else if (drawCmd is DrawLine)
{
var drawLine = (DrawLine) drawCmd;
this.drawLine(drawLine.from, drawLine.to, drawLine.paint);
}
else if (drawCmd is DrawRectShadow) {
var drawRectShadow = (DrawRectShadow) drawCmd;

}
}
public void drawLine(Offset from, Offset to, Paint paint)
{
var color = paint.color;
Offset vect = to - from;
var distance = vect.distance;
if (color.alpha > 0 && distance > 0)
{
var halfWidth = paint.strokeWidth * 0.5;
var diff = vect / distance * halfWidth;
diff = new Offset(diff.dy, -diff.dx);
this.prepareGL(CanvasImpl.linesMat);
CanvasImpl.linesMat.SetPass(0);
var points = new[]
{
(from + diff).toVector(),
(from - diff).toVector(),
(to - diff).toVector(),
(to + diff).toVector(),
};
GL.Begin(GL.QUADS);
GL.Color(color.toColor());
for (int i = 0; i < points.Length; ++i)
{
GL.Vertex(points[i]);
}
GL.End();
}
}
public void concat(Matrix4x4 transform) {
this._transform = transform * this._transform;
}

public void drawTextBlob(TextBlob textBlob, double x, double y)
{
var mesh = MeshGenrator.generateMesh(textBlob, x, y);
var font = FontManager.instance.getOrCreate(textBlob.style.safeFontFamily, textBlob.style.UnityFontSize);
var font = FontManager.instance.getOrCreate(textBlob.style.fontFamilyOrDefault, textBlob.style.UnityFontSize);
prepareGL(font.material);
font.material.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);

GL.MultMatrix(this._transform);
}
//
// private double PixelRound(double v)
// {
// return Math.Floor(v * EditorGUIUtility.pixelsPerPoint) EditorGUIUtility.pixelsPerPoint;
// }
private class ClipRec {
public ClipRec(Matrix4x4 transform, Rect rect = null, RRect rrect = null) {

7
Assets/UIWidgets/ui/painting/draw_cmd.cs


public double y;
}
public class DrawLine : DrawCmd
{
public Offset from;
public Offset to;
public Paint paint;
}
}

6
Assets/UIWidgets/ui/painting/painting.cs


public static bool operator !=(Color a, Color b) {
return !(a == b);
}
public override string ToString()
{
return string.Format("Color(0x{0:X8})", value);
}
public double strokeWidth = 1;
}
public static class Conversions {

34
Assets/UIWidgets/ui/painting/picture.cs


this._isInLayer,
null
));
} else if (drawCmd is DrawSaveLayer) {
} else if (drawCmd is DrawLine)
{
var drawLine = (DrawLine) drawCmd;
Offset vect = drawLine.to - drawLine.from;
var distance = vect.distance;
if (distance > 0)
{
var halfWidth = drawLine.paint.strokeWidth * 0.5;
var diff = vect / distance * halfWidth;
diff = new Offset(diff.dy, -diff.dx);
var offsets = new Offset[]
{
drawLine.from + diff,
drawLine.from - diff,
drawLine.to + diff,
drawLine.to - diff,
};
var minX = offsets[0].dx;
var maxX = offsets[0].dx;
var minY = offsets[0].dy;
var maxY = offsets[0].dy;
for (int i = 1; i < offsets.Length; i++)
{
minX = Math.Min(minX, offsets[i].dx);
maxX = Math.Max(maxX, offsets[i].dx);
minY = Math.Min(minY, offsets[i].dy);
maxY = Math.Min(maxY, offsets[i].dy);
}
this.addPaintBounds(Rect.fromLTRB(minX, minY, maxX, maxY));
}
}
else if (drawCmd is DrawSaveLayer) {
this.stack.Push(new CanvasRec(
this._transform,
this._clipRect,

2
Assets/UIWidgets/ui/painting/txt/mesh_generator.cs


public static Mesh generateMesh(TextBlob textBlob, double x, double y)
{
var style = textBlob.style;
var font = FontManager.instance.getOrCreate(style.safeFontFamily, style.UnityFontSize);
var font = FontManager.instance.getOrCreate(style.fontFamilyOrDefault, style.UnityFontSize);
var length = textBlob.end - textBlob.start;
var vertices = new Vector3[length * 4];
var triangles = new int[length * 6];

216
Assets/UIWidgets/ui/text.cs


using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UIWidgets.foundation;
namespace UIWidgets.ui {
public enum FontStyle {
namespace UIWidgets.ui
{
public enum FontStyle
{
/// Use the upright glyphs
normal,

public enum TextBaseline {
public enum TextBaseline
{
public enum TextAlign {
public enum TextAlign
{
/// Align the text on the left edge of the container.
left,

justify,
}
public class ParagraphConstraints: IEquatable<ParagraphConstraints>
public class ParagraphConstraints : IEquatable<ParagraphConstraints>
public ParagraphConstraints(double width)
{
this.width = width;

}
}
public class TextStyle:IEquatable<TextStyle>
public class TextStyle : IEquatable<TextStyle>
{
public static readonly string defaultFontFamily = "Helvetica";
public static readonly double defaultFontSize = 14.0;

public Color color;
public double? fontSize;
public FontWeight? fontWeight;
public FontStyle? fontStyle;
public double? letterSpacing;
public double? wordSpacing;
public TextBaseline? textBaseline;
public double? height;
public TextDecoration decoration;
public string fontFamily;
public readonly Color color;
public readonly double? fontSize;
public readonly FontWeight? fontWeight;
public readonly FontStyle? fontStyle;
public readonly double? letterSpacing;
public readonly double? wordSpacing;
public readonly TextBaseline? textBaseline;
public readonly double? height;
public readonly TextDecoration decoration;
public readonly Color decorationColor;
public readonly TextDecorationStyle? decorationStyle;
public readonly string fontFamily;
public readonly Paint background;
public FontStyle safeFontStyle
public FontStyle fontStyleOrDefault
public string safeFontFamily
public string fontFamilyOrDefault
public double safeFontSize
public double fontSizeOrDefault
}
public Color colorOrDefault
{
get { return color ?? defaultColor; }
public TextDecorationStyle decorationStyleOrDefault
{
get { return decorationStyle ?? TextDecorationStyle.solid; }
}
public FontWeight safeFontWeight
{
get { return fontWeight ?? defaultFontWeight; }

{
get { return (color ?? defaultColor).toColor(); }
}
if (safeFontStyle == FontStyle.italic)
if (fontStyleOrDefault == FontStyle.italic)
{
if (safeFontWeight == FontWeight.w700)
{

{
return UnityEngine.FontStyle.Italic;
}
} else if (safeFontWeight == FontWeight.w700)
}
else if (safeFontWeight == FontWeight.w700)
{
return UnityEngine.FontStyle.Bold;
}

public int UnityFontSize
{
get { return (int) safeFontSize; }
get { return (int) fontSizeOrDefault; }
var ret = new TextStyle();
ret.color = style.color??color;
ret.fontSize = style.fontSize??fontSize;
ret.fontWeight = style.fontWeight??fontWeight;
ret.fontStyle = style.fontStyle??fontStyle;
ret.letterSpacing = style.letterSpacing??letterSpacing;
ret.textBaseline = style.textBaseline??textBaseline;
ret.height = style.height??height;
ret.decoration = style.decoration??decoration;
ret.fontFamily = style.fontFamily??fontFamily;
var ret = new TextStyle(
color: style.color ?? color,
fontSize: style.fontSize ?? fontSize,
fontWeight: style.fontWeight ?? fontWeight,
fontStyle: style.fontStyle ?? fontStyle,
letterSpacing: style.letterSpacing ?? letterSpacing,
textBaseline: style.textBaseline ?? textBaseline,
height: style.height ?? height,
decoration: style.decoration ?? decoration,
decorationColor: style.decorationColor ?? decorationColor,
decorationStyle: style.decorationStyle ?? decorationStyle,
background: style.background ?? background,
fontFamily: style.fontFamily ?? fontFamily
);
return color == other.color && fontSize == other.fontSize && fontWeight == other.fontWeight &&
fontStyle == other.fontStyle && letterSpacing == other.letterSpacing &&
wordSpacing == other.wordSpacing && textBaseline == other.textBaseline &&
height == other.height && decoration == other.decoration && fontFamily == other.fontFamily;
return Equals(color, other.color) && fontSize.Equals(other.fontSize) && fontWeight == other.fontWeight &&
fontStyle == other.fontStyle && letterSpacing.Equals(other.letterSpacing) &&
wordSpacing.Equals(other.wordSpacing) && textBaseline == other.textBaseline &&
height.Equals(other.height) && Equals(decoration, other.decoration) &&
Equals(decorationColor, other.decorationColor) && decorationStyle == other.decorationStyle &&
string.Equals(fontFamily, other.fontFamily);
}
public override bool Equals(object obj)

hashCode = (hashCode * 397) ^ textBaseline.GetHashCode();
hashCode = (hashCode * 397) ^ height.GetHashCode();
hashCode = (hashCode * 397) ^ (decoration != null ? decoration.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (decorationColor != null ? decorationColor.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ decorationStyle.GetHashCode();
hashCode = (hashCode * 397) ^ (fontFamily != null ? fontFamily.GetHashCode() : 0);
return hashCode;
}

return !Equals(left, right);
}
public TextStyle(Color color = null, double? fontSize = default(double?), FontWeight? fontWeight = default(FontWeight?), FontStyle? fontStyle = default(FontStyle?), double? letterSpacing = default(double?), double? wordSpacing = default(double?), TextBaseline? textBaseline = default(TextBaseline?), double? height = default(double?), TextDecoration decoration = null, string fontFamily = null)
public TextStyle(Color color = null, double? fontSize = default(double?),
FontWeight? fontWeight = default(FontWeight?),
FontStyle? fontStyle = default(FontStyle?), double? letterSpacing = default(double?),
double? wordSpacing = default(double?), TextBaseline? textBaseline = default(TextBaseline?),
double? height = default(double?), TextDecoration decoration = null, Color decorationColor = null,
TextDecorationStyle? decorationStyle = null, string fontFamily = null, Paint background = null)
{
this.color = color;
this.fontSize = fontSize;

this.height = height;
this.decoration = decoration;
this.fontFamily = fontFamily;
this.decorationStyle = decorationStyle;
this.decorationColor = decorationColor;
this.background = background;
public class ParagraphStyle: IEquatable<ParagraphStyle>
public class ParagraphStyle : IEquatable<ParagraphStyle>
public ParagraphStyle(TextAlign? textAlign = null,
TextDirection? textDirection = null,
FontWeight? fontWeight = null,
FontStyle? fontStyle = null,
int? maxLines = null,
double? fontSize = null,
string fontFamily = null,
public ParagraphStyle(TextAlign? textAlign = null,
TextDirection? textDirection = null,
FontWeight? fontWeight = null,
FontStyle? fontStyle = null,
int? maxLines = null,
double? fontSize = null,
string fontFamily = null,
double? lineHeight = null, // todo
string ellipsis = null)
{

{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return textAlign == other.textAlign && textDirection == other.textDirection && fontWeight == other.fontWeight && fontStyle == other.fontStyle && maxLines == other.maxLines && fontSize.Equals(other.fontSize) && string.Equals(fontFamily, other.fontFamily) && lineHeight.Equals(other.lineHeight) && string.Equals(ellipsis, other.ellipsis);
return textAlign == other.textAlign && textDirection == other.textDirection &&
fontWeight == other.fontWeight && fontStyle == other.fontStyle && maxLines == other.maxLines &&
fontSize.Equals(other.fontSize) && string.Equals(fontFamily, other.fontFamily) &&
lineHeight.Equals(other.lineHeight) && string.Equals(ellipsis, other.ellipsis);
}
public override bool Equals(object obj)

if (obj.GetType() != this.GetType()) return false;
return Equals((ParagraphStyle) obj);
}
public static bool operator ==(ParagraphStyle a, ParagraphStyle b) {
public static bool operator ==(ParagraphStyle a, ParagraphStyle b)
{
public static bool operator !=(ParagraphStyle a, ParagraphStyle b) {
public static bool operator !=(ParagraphStyle a, ParagraphStyle b)
{
return !(a == b);
}

fontFamily: fontFamily,
fontSize: fontSize,
height: lineHeight
);
);
}
public TextAlign TextAlign

public readonly double? lineHeight;
public readonly string ellipsis;
}
public class TextDecoration: IEquatable<TextDecoration>
public enum TextDecorationStyle
{
solid,
doubleLine,
}
public class TextDecoration : IEquatable<TextDecoration>
{
public bool Equals(TextDecoration other)
{

{
return mask;
}
public static bool operator ==(TextDecoration a, TextDecoration b) {
public static bool operator ==(TextDecoration a, TextDecoration b)
{
public static bool operator !=(TextDecoration a, TextDecoration b) {
public static bool operator !=(TextDecoration a, TextDecoration b)
{
public readonly int mask;
public TextDecoration(int mask)

bool contains(TextDecoration other) {
public bool contains(TextDecoration other)
{
public enum TextDirection {
public enum TextDirection
{
public enum TextAffinity {
public enum TextAffinity
{
upstream,
downstream,
}

}
}
public class TextBox: IEquatable<TextBox>
public class TextBox : IEquatable<TextBox>
public readonly double bottom;
public readonly TextDirection direction;

this.bottom = bottom;
this.direction = direction;
}
public static TextBox fromLTBD(double left, double top, double right, double bottom, TextDirection direction)
{
return new TextBox(left, top, right, bottom, direction);

{
get { return direction == TextDirection.ltr ? left : right; }
}
public double end
{
get { return direction == TextDirection.ltr ? right : left; }

{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return left.Equals(other.left) && top.Equals(other.top) && right.Equals(other.right) && bottom.Equals(other.bottom) && direction == other.direction;
return left.Equals(other.left) && top.Equals(other.top) && right.Equals(other.right) &&
bottom.Equals(other.bottom) && direction == other.direction;
}
public override bool Equals(object obj)

public override string ToString()
{
return string.Format("Left: {0}, Top: {1}, Right: {2}, Bottom: {3}, Direction: {4}", left, top, right, bottom, direction);
return string.Format("Left: {0}, Top: {1}, Right: {2}, Bottom: {3}, Direction: {4}", left, top, right,
bottom, direction);
}
public override int GetHashCode()

2
Assets/UIWidgets/ui/txt/linebreaker.cs


{
runIterator.nextTo(charIndex);
var run = runIterator.run;
var font = FontManager.instance.getOrCreate(run.style.safeFontFamily, run.style.UnityFontSize);
var font = FontManager.instance.getOrCreate(run.style.fontFamilyOrDefault, run.style.UnityFontSize);
var style = run.style;
var charInfo = new CharacterInfo();

18
Assets/UIWidgets/ui/txt/paint_record.cs


{
public class PaintRecord
{
public PaintRecord(TextStyle style, TextBlob _text,
public PaintRecord(TextStyle style, Offset offset, TextBlob _text,
FontMetrics metrics,
int line, double runWidth)
{
this._style = style;

this._metrics = metrics;
this._offset = offset;
}
public TextBlob text

get { return _runWidth; }
}
public Offset offset
{
get { return _offset; }
set { _offset = value; }
}
public FontMetrics metrics
{
get { return _metrics; }
}
private Offset _offset;
private FontMetrics _metrics;
}
}

126
Assets/UIWidgets/ui/txt/paragraph.cs


{
public readonly double ascent;
public readonly double descent;
public readonly double? underlineThickness;
public readonly double? underlinePosition;
public readonly double? strikeoutPosition;
public readonly double? fxHeight;
public FontMetrics(double ascent, double descent)
public FontMetrics(double ascent, double descent,
double? underlineThickness = null, double? underlinePosition = null, double? strikeoutPosition = null,
double? fxHeight = null)
this.underlineThickness = underlineThickness;
this.underlinePosition = underlinePosition;
this.strikeoutPosition = strikeoutPosition;
this.fxHeight = fxHeight;
}
public static FontMetrics fromFont(Font font, double? height)
{
var ascent = font.ascent * (height??1.0);
var descent = (font.lineHeight - font.ascent) * (height??1.0);
double? fxHeight = null;
font.RequestCharactersInTexture("x");
CharacterInfo charInfo;
if (font.GetCharacterInfo('x', out charInfo))
{
fxHeight = charInfo.glyphHeight;
}
return new FontMetrics(ascent, descent, fxHeight: fxHeight);
}
}

private double _width;
public const char CHAR_NBSP = '\u00A0';
private const double kDoubleDecorationSpacing = 3.0;
public static bool isWordSpace(char ch)
{
return ch == ' ' || ch == CHAR_NBSP;

public void paint(Canvas canvas, double x, double y)
{
var baseOffset = new Offset(x, y);
paintDecorations(canvas, paintRecord, baseOffset);
}
}

var run = _runs.getRun(i);
if (run.start < run.end)
{
var font = FontManager.instance.getOrCreate(run.style.safeFontFamily, run.style.UnityFontSize);
var font = FontManager.instance.getOrCreate(run.style.fontFamilyOrDefault, run.style.UnityFontSize);
font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), 0,
run.style.UnityFontStyle);
}

double yOffset = 0;
var runIndex = 0;
double lastDescent = 0.0f;
var linePaintRecords = new List<PaintRecord>();
linePaintRecords.Clear();
var font = FontManager.instance.getOrCreate(run.style.safeFontFamily, run.style.UnityFontSize);
var font = FontManager.instance.getOrCreate(run.style.fontFamilyOrDefault, run.style.UnityFontSize);
var metrics = FontMetrics.fromFont(font, run.style.height);
if (ascent > maxAscent)
if (metrics.ascent > maxAscent)
maxAscent = ascent;
maxAscent = metrics.ascent;
if (descent > maxDescent)
if (metrics.descent > maxDescent)
maxDescent = descent;
maxDescent = metrics.descent;
}
int start = Math.Max(run.start, line.start);

_characterPositions[end - 1].x + _characterWidths[end - 1] -
_characterPositions[start].x,
descent);
_paintRecords.Add(new PaintRecord(run.style, new TextBlob(
_text, start, end, _characterPositions, run.style, bounds),
linePaintRecords.Add(new PaintRecord(run.style, new Offset(_characterPositions[start].x, yOffset)
, new TextBlob(
_text, start, end, _characterPositions, run.style, bounds), metrics,
new IndexRange(start, end), lineNumber, new FontMetrics(ascent, descent), TextDirection.ltr));
new IndexRange(start, end), lineNumber, metrics, TextDirection.ltr));
}
}

}
lastDescent = maxDescent;
yOffset = Utils.PixelCorrectRound(yOffset + maxAscent + lastDescent);
foreach (var record in linePaintRecords)
{
record.offset = new Offset(record.offset.dx, yOffset);
}
_paintRecords.AddRange(linePaintRecords);
for (var charIndex = line.start; charIndex < line.end; charIndex++)
{
_characterPositions[charIndex].y = yOffset;

}
return words;
}
private void paintDecorations(Canvas canvas, PaintRecord record, Offset baseOffset)
{
if (record.style.decoration == null || record.style.decoration == TextDecoration.none)
{
return;
}
var paint = new Paint();
if (record.style.decorationColor == null)
{
paint.color = record.style.colorOrDefault;
}
else
{
paint.color = record.style.decorationColor;
}
var width = record.runWidth;
var metrics = record.metrics;
double underLineThickness = metrics.underlineThickness ?? (record.style.fontSizeOrDefault / 14.0);
paint.strokeWidth = underLineThickness;
var recordOffset = baseOffset + record.offset;
var x = recordOffset.dx;
var y = recordOffset.dy;
int decorationCount = 1;
switch (record.style.decorationStyleOrDefault)
{
case TextDecorationStyle.doubleLine:
decorationCount = 2;
break;
}
var decoration = record.style.decoration;
for (int i = 0; i < decorationCount; i++)
{
double yOffset = i * underLineThickness * kDoubleDecorationSpacing;
double yOffsetOriginal = yOffset;
if (decoration != null && decoration.contains(TextDecoration.underline))
{
// underline
yOffset += metrics.underlinePosition ?? underLineThickness;
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
yOffset = yOffsetOriginal;
}
if (decoration != null && decoration.contains(TextDecoration.overline))
{
yOffset -= metrics.ascent;
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
yOffset = yOffsetOriginal;
}
if (decoration != null && decoration.contains(TextDecoration.lineThrough))
{
yOffset += (decorationCount - 1.0) * underLineThickness * kDoubleDecorationSpacing / -2.0;
yOffset += metrics.strikeoutPosition ?? (metrics.fxHeight??0) / -2.0;
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
yOffset = yOffsetOriginal;
}
}
}
}
正在加载...
取消
保存