浏览代码

cleanup

/main
fzhangtj 6 年前
当前提交
8ee8979a
共有 13 个文件被更改,包括 145 次插入215 次删除
  1. 3
      Runtime/rendering/editable.cs
  2. 1
      Runtime/rendering/paragraph.cs
  3. 19
      Runtime/ui/txt/layout.cs
  4. 9
      Runtime/ui/txt/layout_utils.cs
  5. 50
      Runtime/ui/txt/linebreaker.cs
  6. 101
      Runtime/ui/txt/paragraph.cs
  7. 8
      Runtime/ui/txt/word_separate.cs
  8. 34
      Runtime/ui/txt/wordbreaker.cs
  9. 2
      Runtime/widgets/basic.cs
  10. 2
      Runtime/widgets/routes.cs
  11. 8
      Tests/Editor/EditableTextWiget.cs
  12. 24
      Tests/Editor/Paragraph.cs
  13. 99
      Tests/Editor/IMGUIText.cs

3
Runtime/rendering/editable.cs


this.onCaretChanged = onCaretChanged;
this.onSelectionChanged = onSelectionChanged;
D.assert(this._maxLines == null || this._maxLines > 0);
D.assert(this._showCursor != null);
D.assert(!this._showCursor.value || cursorColor != null);

public int? maxLines {
get { return this._maxLines; }
set {
D.assert(value > 0);
D.assert(value == null || value > 0);
if (this._maxLines == value) {
return;
}

1
Runtime/rendering/paragraph.cs


public int? maxLines {
get { return this._textPainter.maxLines; }
set {
D.assert(this.maxLines == null || this.maxLines > 0);
if (this._textPainter.maxLines == value) {
return;
}

19
Runtime/ui/txt/layout.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
public class Layout {
int _start;
int _count;

Rect _bounds;
string _text;
TabStops _tabStops;
public static float measureText(double offset, string buf, int start, int count, TextStyle style,
public static float measureText(double offset, string buf, int start, int count, TextStyle style,
List<float> advances, int advanceOffset, TabStops tabStops) {
Layout layout = new Layout();
layout.setTabStops(tabStops);

advances[i + advanceOffset] = layoutAdv[i];
}
}
return layout.getAdvance();
}

else {
this._bounds = this._bounds.expandToInclude(rect);
}
advance = this._tabStops.nextTab((float)(this._advance + offset)) - this._advance;
advance = this._tabStops.nextTab((float) (this._advance + offset)) - this._advance;
public void setTabStops(TabStops tabStops) {
this._tabStops = tabStops;
}

public float getX(int index) {
return this._positions[index];
}
public float getY(int index) {
return 0;
}

9
Runtime/ui/txt/layout_utils.cs


namespace Unity.UIWidgets.ui {
}
}

50
Runtime/ui/txt/linebreaker.cs


using UnityEngine;
namespace Unity.UIWidgets.ui {
public void set(List<int> stops, int tabWidth) {
this._stops.Clear();
if (stops != null) {

this._font = font;
this._fontSize = size;
}
public float nextTab(float widthSoFar) {
for (int i = 0; i < this._stops.Count; i++) {
if (this._stops[i] > widthSoFar) {

if (this._tabWidth == int.MaxValue) {
this._font.RequestCharactersInTexture(" ", this._fontSize);
CharacterInfo characterInfo;
this._font.GetCharacterInfo(' ' , out characterInfo, this._fontSize);
this._font.GetCharacterInfo(' ', out characterInfo, this._fontSize);
this._tabWidth = characterInfo.advance * kTabSpaceCount;
}

return (float)(Math.Floor(widthSoFar / this._tabWidth + 1) * this._tabWidth);
return (float) (Math.Floor(widthSoFar / this._tabWidth + 1) * this._tabWidth);
}
}

public double preBreak;
public float penalty;
public double postBreak;
public int preSpaceCount;
public int postSpaceCount;

string _textBuf;
int _textOffset;
int _textLength;

TabStops _tabStops;
int mFirstTabIndex;
List<Candidate> _candidates = new List<Candidate>();
this._pushBreak(cand.offset, (float)(cand.postBreak - this._preBreak));
this._pushBreak(cand.offset, (float) (cand.postBreak - this._preBreak));
return this._breaks.Count;
}

this._charWidths.AddRange(Enumerable.Repeat(0.0f, size - this._charWidths.Count));
}
}
public void setText(string text, int textOffset, int textLength) {
this._textBuf = text;
this._textOffset = textOffset;

public float addStyleRun(TextStyle style, int start, int end) {
float width = 0.0f;
if (style != null) {
width = Layout.measureText(this._width - this._preBreak, this._textBuf,
width = Layout.measureText(this._width - this._preBreak, this._textBuf,
start + this._textOffset, end - start, style,
this._charWidths, start, this._tabStops);
}

for (int i = start; i < end; i++) {
char c = this._textBuf[i + this._textOffset];
if (c == '\t') {
this._width = this._preBreak + this._tabStops.nextTab((float)(this._width - this._preBreak));
if (this.mFirstTabIndex == Int32.MaxValue) {
this._width = this._preBreak + this._tabStops.nextTab((float) (this._width - this._preBreak));
if (this.mFirstTabIndex == int.MaxValue) {
this.mFirstTabIndex = i;
}
}

int wordStart = this._wordBreaker.wordStart();
int wordEnd = this._wordBreaker.wordEnd();
if (style != null || current == end || this._charWidths[current] > 0) {
this._addWordBreak(current, this._width, postBreak, this._spaceCount, postSpaceCount, 0);
this._addWordBreak(current, this._width, postBreak, this._spaceCount, postSpaceCount, 0);
}
lastBreak = current;

}
return width;
}

this._tabStops = tabStops;
}
void _addWordBreak(int offset, double preBreak, double postBreak, int preSpaceCount, int postSpaceCount, float penalty) {
void _addWordBreak(int offset, double preBreak, double postBreak, int preSpaceCount, int postSpaceCount,
float penalty) {
Candidate cand = new Candidate();
double width = this._candidates[this._candidates.Count - 1].preBreak;
if (postBreak - width > this._lineWidth) {

cand.penalty = ScoreDesperate;
this._addCandidate(cand);
width += w;
cand.offset = offset;
cand.preBreak = preBreak;
cand.postBreak = postBreak;

this._addCandidate(cand);
}
void _addCandidate(Candidate cand) {
int candIndex = this._candidates.Count;

this._bestScore = cand.penalty;
}
}
this._pushBreak(bestCandidate.offset, (float)(bestCandidate.postBreak - this._preBreak));
this._pushBreak(bestCandidate.offset, (float) (bestCandidate.postBreak - this._preBreak));
this._bestScore = ScoreInfty;
this._lastBreak = this._bestBreak;
this._preBreak = bestCandidate.preBreak;

this._breaks.Add(offset);
this._widths.Add(width);
}
}
}

101
Runtime/ui/txt/paragraph.cs


public Range<double> xPos;
public readonly List<GlyphPosition> positions;
public CodeUnitRun(List<GlyphPosition> positions, Range<int> cu, int line, Range<double> xPos,
FontMetrics fontMetrics, TextDirection direction) {
public CodeUnitRun(List<GlyphPosition> positions, Range<int> cu, int line, Range<double> xPos,
FontMetrics fontMetrics, TextDirection direction) {
this.lineNumber = line;
this.codeUnits = cu;
this.xPos = xPos;

}
}
public class FontMetrics {
public readonly double ascent;
public readonly double leading = 0.0;

if (font.GetCharacterInfo('x', out charInfo, fontSize)) {
fxHeight = charInfo.glyphHeight;
}
return new FontMetrics(ascent, descent, fxHeight: fxHeight);
}
}

if (ReferenceEquals(null, obj)) {
return false;
}
return obj is Range<T> && this.Equals((Range<T>) obj);
}

}
public static class RangeUtils {
public class GlyphLine {
public readonly List<GlyphPosition> positions;

}
}
public class LineRange {
public LineRange(int start, int end, int endExcludingWhitespace, int endIncludingNewLine, bool hardBreak) {
this.start = start;

}
const int TabSpaceCount = 4;
bool _needsLayout = true;

const double kDoubleDecorationSpacing = 3.0;
// This function determines whether a character is a space that disappears at end of line.
// It is the Unicode set: [[:General_Category=Space_Separator:]-[:Line_Break=Glue:]],
// plus '\n'.
// Note: all such characters are in the BMP, so it's ok to use code units for this.
public double height {
get { return this._lineHeights.Count == 0 ? 0 : this._lineHeights[this._lineHeights.Count - 1]; }
}

}
var textStyle = this._paragraphStyle.getTextStyle();
this._tabStops.setFont(FontManager.instance.getOrCreate(textStyle.fontFamily).font, textStyle.UnityFontSize);
this._tabStops.setFont(FontManager.instance.getOrCreate(textStyle.fontFamily).font,
textStyle.UnityFontSize);
this._paintRecords.Clear();
this._lineHeights.Clear();
this._lineBaseLines.Clear();

int styleRunIndex = 0;
double yOffset = 0;
double preMaxDescent = 0;
// Break the line into words if justification should be applied.
int wordIndex = 0;
bool justifyLine = this._paragraphStyle.textAlign == TextAlign.justify &&

double runXOffset = 0;
double justifyXOffset = 0;
lineCodeUnitRuns.Clear();
List<GlyphPosition> lineGlyphPositions = new List<GlyphPosition>();
List<PaintRecord> paintRecords = new List<PaintRecord>();
for (int i = 0; i < lineRuns.Count; ++i) {

continue;
}
double wordStartPosition = Double.NaN;
var layoutAdvances = layout.getAdvances();
double wordStartPosition = double.NaN;
// var layoutAdvances = layout.getAdvances();
builder.allocRunPos(run.style, this._text, textStart, textCount);
builder.setBounds(layout.getBounds());
glyphPositions.Clear();

);
float glyphAdvance = layout.getCharAdvance(glyphIndex);
glyphPositions.Add(new GlyphPosition(runXOffset + glyphXOffset, glyphAdvance,
glyphPositions.Add(new GlyphPosition(runXOffset + glyphXOffset, glyphAdvance,
new Range<int>(textStart + glyphIndex, textStart + glyphIndex + 1)));
if (wordIndex < words.Count && words[wordIndex].start == run.start + glyphIndex) {
wordStartPosition = runXOffset + glyphXOffset;

wordIndex++;
if (!double.IsNaN(wordStartPosition)) {
double wordWidth = glyphPositions[glyphPositions.Count - 1].xPos.end - wordStartPosition;
double wordWidth =
glyphPositions[glyphPositions.Count - 1].xPos.end - wordStartPosition;
}
if (glyphPositions.Count == 0) {

));
lineGlyphPositions.AddRange(glyphPositions);
var codeUnitPositions = new List<GlyphPosition>(glyphPositions);
lineCodeUnitRuns.Add(new CodeUnitRun(codeUnitPositions, new Range<int>(run.start, run.end), lineNumber,
new Range<double>(glyphPositions[0].xPos.start, glyphPositions[glyphPositions.Count - 1].xPos.end),
lineCodeUnitRuns.Add(new CodeUnitRun(codeUnitPositions, new Range<int>(run.start, run.end),
lineNumber,
new Range<double>(glyphPositions[0].xPos.start,
glyphPositions[glyphPositions.Count - 1].xPos.end),
}
double lineXOffset = this.getLineXOffset(runXOffset);

}
for (int i = 0; i < lineGlyphPositions.Count; ++i) {
lineGlyphPositions[i] = lineGlyphPositions[i].shift(lineXOffset);
}

double maxLineSpacing = 0;
double maxDescent = 0;
var updateLineMetrics = new Action<FontMetrics, TextStyle>((FontMetrics metrics, TextStyle style) => {
var updateLineMetrics = new Action<FontMetrics, TextStyle>((metrics, style) => {
? -metrics.ascent * style.height
? -metrics.ascent * style.height
this._ideographicBaseline = (metrics.underlinePosition??0.0 - metrics.ascent) * style.height;
this._ideographicBaseline =
(metrics.underlinePosition ?? 0.0 - metrics.ascent) * style.height;
}
}

var defaultFont = FontManager.instance.getOrCreate(defaultStyle.fontFamily).font;
var metrics = FontMetrics.fromFont(defaultFont, defaultStyle.UnityFontSize);
updateLineMetrics(metrics, defaultStyle);
+ (maxLineSpacing + maxDescent)/*Math.Round(maxLineSpacing + maxDescent)*/);
+ Math.Round(maxLineSpacing + maxDescent));
yOffset += maxLineSpacing + preMaxDescent/*Math.Round(maxLineSpacing + preMaxDescent)*/;
yOffset += Math.Round(maxLineSpacing + preMaxDescent);
preMaxDescent = maxDescent;
foreach (var paintRecord in paintRecords) {

lineBlockWidth = 0;
}
}
this._maxIntrinsicWidth = Math.Max(lineBlockWidth, this._maxIntrinsicWidth);
if (this._paragraphStyle.maxLines == 1 || (this._paragraphStyle.maxLines == null &&

continue;
}
double top = (run.lineNumber == 0) ? 0 : this._lineHeights[run.lineNumber - 1];
double top = (run.lineNumber == 0) ? 0 : this._lineHeights[run.lineNumber - 1];
double bottom = this._lineHeights[run.lineNumber];
double left, right;
if (run.codeUnits.start >= start && run.codeUnits.end <= end) {

}
}
if (left == Double.MaxValue || right == Double.MinValue) {
if (left == double.MaxValue || right == double.MinValue) {
boxs.Add(TextBox.fromLTBD(left, top, right, bottom, run.direction));
}

if (this._text.Length == 0 || this._text[this._text.Length - 1] != '\n') {
return null;
}
var lineNumber = this.getLineCount() - 1;
var top = (lineNumber > 0) ? this._lineHeights[lineNumber - 1] : 0;
var bottom = this._lineHeights[lineNumber];

break;
}
}
} else {
}
else {
return new PositionWithAffinity(gp.codeUnits.end, TextAffinity.upstream);
}
}

this._lineRanges.Clear();
this._lineWidths.Clear();
this._maxIntrinsicWidth = 0;
var newLinePositions = new List<int>();
for (var i = 0; i < this._text.Length; i++) {
if (this._text[i] == '\n') {

continue;
}
lineBreaker.setLineWidth((float)this._width);
lineBreaker.setLineWidth((float) this._width);
lineBreaker.resize(blockSize);
lineBreaker.setTabStops(this._tabStops);
lineBreaker.setText(this._text, blockStart, blockSize);

if (!inWord && !isSpace) {
wordStart = i;
inWord = true;
} else if (inWord && isSpace) {
}
else if (inWord && isSpace) {
if (inWord) {
words.Add(new Range<int>(wordStart, end));
}

var paint = new Paint();
if (record.style.decorationColor == null) {
paint.color = record.style.color;
} else {
}
else {
paint.color = record.style.decorationColor;
}

}
if (decoration != null && decoration.contains(TextDecoration.overline)) {
yOffset = metrics.ascent;
yOffset += metrics.ascent;
canvas.drawLine(new Offset(x, y + yOffset), new Offset(x + width, y + yOffset), paint);
yOffset = yOffsetOriginal;
}

if (this._paragraphStyle.textAlign == TextAlign.right) {
return this._width - lineTotalAdvance;
} else if (this._paragraphStyle.textAlign == TextAlign.center) {
}
else if (this._paragraphStyle.textAlign == TextAlign.center) {
return (this._width - lineTotalAdvance) / 2;
}
else {

return (value & 0xF800) == 0xD800;
}
}
}
}

8
Runtime/ui/txt/word_separate.cs


if (index >= this._text.Length) {
return new Range<int>(0, 0);
}
var t = this.classifyChar(index);
int start = index;
for (int i = index; i >= 0; --i) {

}
start = i;
}
}

if (this.classifyChar(i) != t) {
break;
}
return new Range<int>(start, end + 1);
}

return characterType.WhiteSpace;
}
}
}

34
Runtime/ui/txt/wordbreaker.cs


using System;
public const uint U16_SURROGATE_OFFSET = ((0xd800<<10)+0xdc00-0x10000);
public const uint U16_SURROGATE_OFFSET = ((0xd800 << 10) + 0xdc00 - 0x10000);
string _text;
int _offset;
int _size;

bool _inEmailOrUrl;
public int next() {
this._last = this._current;
this._detectEmailOrUrl();

else {
this._current = this._findNextBreakNormal();
}
return this._current;
}

this._scanOffset = 0;
this._inEmailOrUrl = false;
}
public int wordStart() {
if (this._inEmailOrUrl) {
return this._last;

result = ix;
}
return result;
}

result = ix;
}
return result;
}

public void finish() {
this._text = null;
}

}
for (;this._current < this._size;++this._current) {
for (; this._current < this._size; ++this._current) {
}
static uint nextCode(string text, ref int index, int end) {

public static bool isLeadSurrogate(uint c) {
return ((c) & 0xfffffc00) == 0xd800;
}
return (char)(((uint) (lead) << 10) + (uint) (trail - U16_SURROGATE_OFFSET));
return (char) (((uint) (lead) << 10) + (uint) (trail - U16_SURROGATE_OFFSET));
}
}
}

2
Runtime/widgets/basic.cs


renderObject.softWrap = this.softWrap;
renderObject.overflow = this.overflow;
renderObject.textScaleFactor = this.textScaleFactor;
renderObject.maxLines = this.maxLines ?? 0; // todo: maxLines should be nullable.
renderObject.maxLines = this.maxLines;
}
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) {

2
Runtime/widgets/routes.cs


}
}
abstract class PopupRoute : ModalRoute {
public abstract class PopupRoute : ModalRoute {
protected PopupRoute(
RouteSettings settings = null
) : base(settings: settings) {

8
Tests/Editor/EditableTextWiget.cs


cursorColor: Color.fromARGB(255, 0, 0, 0)
)
);
this.windowAdapter.attachRootWidget(this.root);
this.windowAdapter.attachRootWidget(new WidgetsApp(window: this.windowAdapter, home: this.root,
pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) =>
new PageRouteBuilder(
settings: settings,
pageBuilder: (BuildContext context, Unity.UIWidgets.animation.Animation<double> animation,
Unity.UIWidgets.animation.Animation<double> secondaryAnimation) => builder(context)
)));
this.titleContent = new GUIContent("EditableTextWidget");
}

24
Tests/Editor/Paragraph.cs


return null;
}
RenderBox box(RenderParagraph p, int width = 400, int height = 200) {
RenderBox box(RenderParagraph p, int width = 200, int height = 200) {
return new RenderConstrainedOverflowBox(
minWidth: width,
maxWidth: width,

return this.box(
new RenderParagraph(new TextSpan("", children:
new List<TextSpan>() {
new TextSpan("wei", new TextStyle(fontSize:14.0, color: new Color(0xffff0000))),
new TextSpan("Real-time 3D revolutioni淡粉色的方式地方zes the animation pipeline ", null),
new TextSpan(style: new TextStyle(color: Color.fromARGB(255, 255, 0, 0)),
text: "for Disney Television Animation's “Baymax Dreams"),
new TextSpan(" Unity Widgets"),
new TextSpan(" Text"),
new TextSpan("Real-time 3D revolutionizes the animation pipeline "),
new TextSpan(style: new TextStyle(color: Color.fromARGB(125, 255, 0, 0)),
text: "Transparent Red Text\n\n"),
new TextSpan(style: new TextStyle(fontWeight: FontWeight.w700),
text: "Bold Text Test Bold Textfs Test: FontWeight.w70\n\n"),
new TextSpan(style: new TextStyle(fontStyle: FontStyle.italic),
text: "This is FontStyle.italic Text This is FontStyle.italic Text\n\n"),
new TextSpan(
style: new TextStyle(fontStyle: FontStyle.italic, fontWeight: FontWeight.w700),
text:
"This is FontStyle.italic And 发撒放豆腐sad 发生的 Bold Text This is FontStyle.italic And Bold Text\n\n"),
new TextSpan(style: new TextStyle(fontSize: 18),
text: "FontSize 18: Get a named matrix value from the shader."),
new TextSpan(style: new TextStyle(fontSize: 14),
text: "FontSize 14"),
})));
}

99
Tests/Editor/IMGUIText.cs


using UnityEditor;
using UnityEngine;
namespace UIWidgets.Tests {
public class IMGUIText: EditorWindow {
Font font;
string str = "wei";
Mesh mesh;
[MenuItem("UIWidgetsTests/IMGUIText")]
public static void getIMGUIText() {
EditorWindow.GetWindow(typeof(IMGUIText));
}
void OnEnable() {
font = Font.CreateDynamicFontFromOSFont("Helvetica", 14);
// Set the rebuild callback so that the mesh is regenerated on font changes.
Font.textureRebuilt += OnFontTextureRebuilt;
// Request characters.
font.RequestCharactersInTexture(str);
// Set up mesh.
mesh = new Mesh();
// Generate font mesh.
RebuildMesh();
}
void RebuildMesh()
{
// Generate a mesh for the characters we want to print.
var vertices = new Vector3[str.Length * 4];
var triangles = new int[str.Length * 6];
var uv = new Vector2[str.Length * 4];
Vector3 pos = new Vector3(20, 60f, 0.0f);
for (int i = 0; i < str.Length; i++)
{
// Get character rendering information from the font
CharacterInfo ch;
font.GetCharacterInfo(str[i], out ch);
vertices[4 * i + 0] = pos + new Vector3(ch.minX, -ch.maxY, 0);
vertices[4 * i + 1] = pos + new Vector3(ch.maxX, -ch.maxY, 0);
vertices[4 * i + 2] = pos + new Vector3(ch.maxX, -ch.minY, 0);
vertices[4 * i + 3] = pos + new Vector3(ch.minX, -ch.minY, 0);
uv[4 * i + 0] = ch.uvTopLeft;
uv[4 * i + 1] = ch.uvTopRight;
uv[4 * i + 2] = ch.uvBottomRight;
uv[4 * i + 3] = ch.uvBottomLeft;
triangles[6 * i + 0] = 4 * i + 0;
triangles[6 * i + 1] = 4 * i + 1;
triangles[6 * i + 2] = 4 * i + 2;
triangles[6 * i + 3] = 4 * i + 0;
triangles[6 * i + 4] = 4 * i + 2;
triangles[6 * i + 5] = 4 * i + 3;
// Advance character position
pos += new Vector3(ch.advance, 0, 0);
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uv;
}
public void setup() {
{
}
}
void OnDestroy()
{
Font.textureRebuilt -= OnFontTextureRebuilt;
}
void OnFontTextureRebuilt(Font changedFont)
{
if (changedFont != font)
return;
RebuildMesh();
}
void OnGUI() {
if (Event.current.type == EventType.Repaint) {
font.material.SetPass(0);
var tt = font.material.mainTexture;
font.material.color = Color.black;
Graphics.DrawMeshNow(this.mesh, Matrix4x4.identity);
}
}
void Update() {
font.RequestCharactersInTexture(str);
}
}
}
正在加载...
取消
保存