浏览代码

Make doLayout static.

/main
Yuncong Zhang 5 年前
当前提交
bef756b1
共有 5 个文件被更改,包括 179 次插入1144 次删除
  1. 120
      Runtime/ui/painting/txt/text_blob.cs
  2. 173
      Runtime/ui/txt/layout.cs
  3. 23
      Runtime/ui/txt/linebreaker.cs
  4. 1001
      Runtime/ui/txt/paragraph.cs
  5. 6
      Runtime/ui/txt/styled_runs.cs

120
Runtime/ui/painting/txt/text_blob.cs


namespace Unity.UIWidgets.ui {
public class TextBlob {
internal TextBlob(string text, int textOffset, int textSize, Vector2d[] positions, Rect bounds, TextStyle style) {
this.instanceId = ++_nextInstanceId;
this.positions = positions;
this.text = text;
this.textOffset = textOffset;
this.textSize = textSize;
this.style = style;
this.bounds = bounds;
}
public Rect boundsInText {
get { return this.bounds.shift(new Offset(this.positions[0].x, this.positions[0].y)); }
}
static long _nextInstanceId = 0;
internal readonly long instanceId;
internal readonly string text;
internal readonly int textOffset;
internal readonly int textSize;
internal readonly Vector2d[] positions;
internal readonly TextStyle style;
internal readonly Rect bounds; // bounds with positions[start] as origin
}
public class TextBlobBuilder {
TextStyle _style;
public Vector2d[] positions;
string _text;
int _textOffset;
int _size;
Rect _bounds;
public void allocRunPos(painting.TextStyle style, string text, int offset, int size, float textScaleFactor = 1.0f) {
this.allocRunPos(TextStyle.applyStyle(null, style, textScaleFactor), text, offset, size);
}
internal void allocRunPos(TextStyle style, string text, int offset, int size) {
this._style = style;
this._text = text;
this._textOffset = offset;
this._size = size;
if (this.positions == null || this.positions.Length < size) {
this.positions = new Vector2d[size];
}
}
public void setBounds(Rect bounds) {
this._bounds = bounds;
}
public TextBlob make() {
var result = new TextBlob(this._text, this._textOffset,
this._size, this.positions, this._bounds, this._style);
this.positions = null;
return result;
}
}
namespace Unity.UIWidgets.ui {
public class TextBlob {
internal TextBlob(string text, int textOffset, int textSize, Vector2d[] positions, Rect bounds, TextStyle style) {
this.instanceId = ++_nextInstanceId;
this.positions = positions;
this.text = text;
this.textOffset = textOffset;
this.textSize = textSize;
this.style = style;
this.bounds = bounds;
}
public Rect boundsInText {
get { return this.bounds.shift(new Offset(this.positions[0].x, this.positions[0].y)); }
}
static long _nextInstanceId = 0;
internal readonly long instanceId;
internal readonly string text;
internal readonly int textOffset;
internal readonly int textSize;
internal readonly Vector2d[] positions;
internal readonly TextStyle style;
internal readonly Rect bounds; // bounds with positions[start] as origin
}
public class TextBlobBuilder {
TextStyle _style;
public Vector2d[] positions;
string _text;
int _textOffset;
int _size;
Rect _bounds;
public void allocRunPos(painting.TextStyle style, string text, int offset, int size, float textScaleFactor = 1.0f) {
this.allocRunPos(TextStyle.applyStyle(null, style, textScaleFactor), text, offset, size);
}
internal void allocRunPos(TextStyle style, string text, int offset, int size) {
this._style = style;
this._text = text;
this._textOffset = offset;
this._size = size;
if (this.positions == null || this.positions.Length < size) {
this.positions = new Vector2d[size];
}
}
public void setBounds(Rect bounds) {
this._bounds = bounds;
}
public TextBlob make() {
var result = new TextBlob(this._text, this._textOffset,
this._size, this.positions, this._bounds, this._style);
this.positions = null;
return result;
}
}
}

173
Runtime/ui/txt/layout.cs


using System;
class Layout {
int _start;
int _count;

UnityEngine.Rect _bounds;
TabStops _tabStops;
static UnityEngine.Rect _innerBounds; // Used to pass bounds from static to non-static doLayout
Layout layout = new Layout();
layout.setTabStops(tabStops);
layout.doLayout(offset, buff, start, count, style);
if (advances != null) {
var layoutAdv = layout.getAdvances();
for (int i = 0; i < count; i++) {
advances[i + advanceOffset] = layoutAdv[i];
}
}
return layout.getAdvance();
return _doLayout(offset, buff, start, count, style, advances, null, advanceOffset, tabStops);
}
public void doLayout(float offset, TextBuff buff, int start, int count, TextStyle style) {

this._positions.reset(count);
this._advance = 0;
this._bounds = default;
_innerBounds = default;
this._advance = _doLayout(offset, buff, start, count, style, this._advances, this._positions, 0,
this._tabStops);
this._bounds = _innerBounds;
this._count = count;
}
static float _doLayout(float offset, TextBuff buff, int start, int count, TextStyle style,
List<float> advances, List<float> positions, int advanceOffset, TabStops tabStops) {
float advance = 0;
this.layoutEmoji(buff.text.Substring(buff.offset + start, count), style, font, start, count);
advance = _layoutEmoji(buff.text.Substring(buff.offset + start, count), style, font, count,
advances, positions, advanceOffset, advance);
}
else {
font.RequestCharactersInTextureSafe(buff.text, style.UnityFontSize, style.UnityFontStyle);

for (int iter = start; iter < start + count; iter = wordend) {
wordend = LayoutUtils.getNextWordBreakForCache(buff, iter);
int wordCount = Mathf.Min(start + count, wordend) - iter;
this.layoutWord(offset, iter - start, buff.subBuff(wordstart, wordend - wordstart),
iter - wordstart, wordCount, style, font);
advance = _layoutWord(offset, iter - start, buff.subBuff(wordstart, wordend - wordstart),
iter - wordstart, wordCount, style, font, advances, positions,
advanceOffset, advance, tabStops);
this._count = count;
return advance;
void layoutWord(float offset, int layoutOffset,
TextBuff buff, int start, int wordCount, TextStyle style, Font font) {
static float _layoutWord(float offset, int layoutOffset,
TextBuff buff, int start, int wordCount, TextStyle style, Font font, List<float> advances,
List<float> positions, int advanceOffset, float initAdvance, TabStops tabStops) {
float x = this._advance;
float x = initAdvance;
this._advances[i + layoutOffset] += letterSpaceHalfLeft + wordSpacing;
if (advances != null) {
advances[i + layoutOffset + advanceOffset] = letterSpaceHalfLeft + wordSpacing;
}
this._advances[i - 1 + layoutOffset] += letterSpaceHalfRight;
this._advances[i + layoutOffset] += letterSpaceHalfLeft;
if (advances != null) {
advances[i - 1 + layoutOffset + advanceOffset] += letterSpaceHalfRight;
advances[i + layoutOffset + advanceOffset] = letterSpaceHalfLeft;
}
x += letterSpace;
}

var minY = -glyphInfo.maxY;
var maxY = -glyphInfo.minY;
if (this._bounds.width <= 0 || this._bounds.height <= 0) {
this._bounds = UnityEngine.Rect.MinMaxRect(
minX, minY, maxX, maxY);
} else {
if (minX < this._bounds.x) {
this._bounds.x = minX;
if (_innerBounds.width <= 0 || _innerBounds.height <= 0) {
_innerBounds.x = minX;
_innerBounds.y = minY;
_innerBounds.xMax = maxX;
_innerBounds.yMax = maxY;
}
else {
if (minX < _innerBounds.x) {
_innerBounds.x = minX;
if (minY < this._bounds.y) {
this._bounds.y = minY;
if (minY < _innerBounds.y) {
_innerBounds.y = minY;
if (maxX > this._bounds.xMax) {
this._bounds.xMax = maxX;
if (maxX > _innerBounds.xMax) {
_innerBounds.xMax = maxX;
if (maxY > this._bounds.yMax) {
this._bounds.yMax = maxY;
if (maxY > _innerBounds.yMax) {
_innerBounds.yMax = maxY;
this._positions[i + layoutOffset] = x;
if (positions != null) {
positions[i + layoutOffset] = x;
}
advance = this._tabStops.nextTab((this._advance + offset)) - this._advance;
advance = tabStops.nextTab((initAdvance + offset)) - initAdvance;
this._advances[i + layoutOffset] += advance;
if (advances != null) {
advances[i + layoutOffset + advanceOffset] += advance;
}
this._advances[i + layoutOffset] += letterSpaceHalfRight;
if (advances != null) {
advances[i + layoutOffset + advanceOffset] += letterSpaceHalfRight;
}
this._advance = x;
return x;
void layoutEmoji(string text, TextStyle style, Font font, int start, int count) {
static float _layoutEmoji(string text, TextStyle style, Font font, int count, List<float> advances,
List<float> positions, int advanceOffset, float initAdvance) {
var metrics = FontMetrics.fromFont(font, style.UnityFontSize);
float x = initAdvance;
float x = this._advance;
if (EmojiUtils.isSingleCharNonEmptyEmoji(c) || char.IsHighSurrogate(c)) {
float letterSpace = style.letterSpacing;
float letterSpaceHalfLeft = letterSpace * 0.5f;

this._advances[i] += letterSpaceHalfLeft;
if (advances != null) {
advances[i + advanceOffset] = letterSpaceHalfLeft;
}
var metrics = FontMetrics.fromFont(font, style.UnityFontSize);
var minX = x;
var maxX = metrics.descent - metrics.ascent + x;

if (this._bounds.width <= 0 || this._bounds.height <= 0) {
this._bounds = UnityEngine.Rect.MinMaxRect(
minX, minY, maxX, maxY);
if (_innerBounds.width <= 0 || _innerBounds.height <= 0) {
_innerBounds.x = minX;
_innerBounds.y = minY;
_innerBounds.xMax = maxX;
_innerBounds.yMax = maxY;
if (minX < this._bounds.x) {
this._bounds.x = minX;
if (minX < _innerBounds.x) {
_innerBounds.x = minX;
if (minY < this._bounds.y) {
this._bounds.y = minY;
if (minY < _innerBounds.y) {
_innerBounds.y = minY;
if (maxX > this._bounds.xMax) {
this._bounds.xMax = maxX;
if (maxX > _innerBounds.xMax) {
_innerBounds.xMax = maxX;
if (maxY > this._bounds.yMax) {
this._bounds.yMax = maxY;
if (maxY > _innerBounds.yMax) {
_innerBounds.yMax = maxY;
this._positions[i] = x;
if (positions != null) {
positions[i] = x;
}
this._advances[i] += advance;
this._advances[i] += letterSpaceHalfRight;
if (advances != null) {
advances[i + advanceOffset] += advance;
advances[i + advanceOffset] += letterSpaceHalfRight;
}
this._advances[i] = 0;
this._positions[i] = x;
}
if (advances != null) {
advances[i + advanceOffset] = 0;
}
this._advance = x;
if (positions != null) {
positions[i] = x;
}
}
return x;
}
public void setTabStops(TabStops tabStops) {

23
Runtime/ui/txt/linebreaker.cs


int _fontSize;
int _spaceAdvance;
const int kTabSpaceCount = 4;
List<int> _stops = new List<int>();

}
this._font = font;
this._fontSize = size;
// Recompute the advance of space (' ') if font size changes
if (this._fontSize != size) {
this._fontSize = size;
this._font.RequestCharactersInTextureSafe(" ", this._fontSize);
this._font.getGlyphInfo(' ', out var glyphInfo, this._fontSize, UnityEngine.FontStyle.Normal);
this._spaceAdvance = glyphInfo.advance;
}
}
public float nextTab(float widthSoFar) {

if (this._tabWidth == int.MaxValue) {
if (this._fontSize > 0) {
this._font.RequestCharactersInTextureSafe(" ", this._fontSize);
this._font.getGlyphInfo(' ', out var glyphInfo, this._fontSize, UnityEngine.FontStyle.Normal);
this._tabWidth = glyphInfo.advance * kTabSpaceCount;
this._tabWidth = this._spaceAdvance * kTabSpaceCount;
}
}

}
int current = this._wordBreaker.current();
int afterWord = start;
int lastBreak = start;
float lastBreakWidth = this._width;
float postBreak = this._width;
int postSpaceCount = this._spaceCount;

if (!LayoutUtils.isLineEndSpace(c)) {
postBreak = this._width;
postSpaceCount = this._spaceCount;
afterWord = i + 1;
int wordStart = this._wordBreaker.wordStart();
int wordEnd = this._wordBreaker.wordEnd();
lastBreak = current;
lastBreakWidth = this._width;
current = this._wordBreaker.next();
}
}

1001
Runtime/ui/txt/paragraph.cs
文件差异内容过多而无法显示
查看文件

6
Runtime/ui/txt/styled_runs.cs


readonly List<TextStyle> styles = new List<TextStyle>();
readonly List<IndexedRun> runs = new List<IndexedRun>();
public class RunIterator {
public struct RunIterator {
int _charIndex;
int _runIndex;
StyledRuns _runs;

}
}
internal class Run {
internal struct Run {
public readonly TextStyle style;
public readonly int start;
public readonly int end;

}
internal class IndexedRun {
public readonly int styleIndex = 0;
public readonly int styleIndex;
public readonly int start;
public int end;

正在加载...
取消
保存