浏览代码

Make Layout static.

/main
Yuncong Zhang 5 年前
当前提交
9673f184
共有 2 个文件被更改,包括 61 次插入99 次删除
  1. 108
      Runtime/ui/txt/layout.cs
  2. 52
      Runtime/ui/txt/paragraph.cs

108
Runtime/ui/txt/layout.cs


using System.Collections.Generic;
class Layout {
int _count;
float[] _advances;
float[] _positions;
float _advance;
UnityEngine.Rect _bounds;
static float _x, _y, _maxX, _maxY; // Used to pass bounds from static to non-static doLayout
static class Layout {
return _doLayout(offset, text, start, count, style, advances, null, advanceOffset, tabStops);
return _doLayout(offset, text, start, count, style, advances, null, advanceOffset, tabStops, out var bounds);
}
public static int computeTruncateCount(string text, int start, int count, TextStyle style, float advanceLimit) {

return 0;
}
public void doLayout(float offset, string text, int start, int count, TextStyle style, TabStops tabStops) {
this._count = count;
this.allocAdvancesAndPositions(count);
_x = _y = _maxX = _maxY = 0;
this._advance = _doLayout(offset, text, start, count, style, this._advances, this._positions, 0,
tabStops);
this._bounds.Set(_x, _y, _maxX - _x, _maxY - _y);
public static float doLayout(float offset, string text, int start, int count, float[] advances, float[] positions, TextStyle style, TabStops tabStops, out UnityEngine.Rect bounds) {
return _doLayout(offset, text, start, count, style, advances, positions, 0,
tabStops, out bounds);
}
public static void computeCharWidths(string text, int start, int count, TextStyle style, float[] advances, int advanceOffset) {

}
static float _doLayout(float offset, string text, int start, int count, TextStyle style,
float[] advances, float[] positions, int advanceOffset, TabStops tabStops) {
float[] advances, float[] positions, int advanceOffset, TabStops tabStops, out UnityEngine.Rect bounds) {
bounds = new UnityEngine.Rect();
advance = _layoutEmoji(text, start, style, font, count,
advances, positions, advanceOffset, advance);
advance = _layoutEmoji(text, start, style, font, count, advances, positions, advanceOffset, advance, ref bounds);
}
else {
// According to the logic of Paragraph.layout, it is assured that all the characters are requested

wordend = LayoutUtils.getNextWordBreak(text, iter, start + count);
advance = _layoutWord(offset, iter - start, text, iter,
wordend - iter, style, font, advances, positions, advanceOffset, advance,
tabStops);
tabStops, ref bounds);
}
}

static float _layoutWord(float offset, int layoutOffset,
string text, int start, int wordCount, TextStyle style, Font font, float[] advances,
float[] positions, int advanceOffset, float initAdvance, TabStops tabStops) {
float[] positions, int advanceOffset, float initAdvance, TabStops tabStops, ref UnityEngine.Rect bounds) {
float wordSpacing =
wordCount == 1 && LayoutUtils.isWordSpace(text[start]) ? style.wordSpacing : 0;

}
if (font.getGlyphInfo(ch, out var glyphInfo, style.UnityFontSize, style.UnityFontStyle)) {
_updateInnerBounds(glyphInfo, x);
_updateBounds(glyphInfo, x, ref bounds);
}
if (positions != null) {

}
static float _layoutEmoji(string text, int start, TextStyle style, Font font, int count, float[] advances,
float[] positions, int advanceOffset, float initAdvance) {
float[] positions, int advanceOffset, float initAdvance, ref UnityEngine.Rect bounds) {
var metrics = FontMetrics.fromFont(font, style.UnityFontSize);
float x = initAdvance;
for (int i = 0; i < count; i++) {

var maxX = metrics.descent - metrics.ascent + x;
var minY = metrics.ascent;
var maxY = metrics.descent;
_updateInnerBounds(minX, maxX, minY, maxY);
_updateBounds(minX, maxX, minY, maxY, ref bounds);
if (positions != null) {
positions[i] = x;

return x;
}
static void _updateInnerBounds(CharacterInfo glyphInfo, float x) {
static void _updateBounds(CharacterInfo glyphInfo, float x, ref UnityEngine.Rect bounds) {
_updateInnerBounds(minX, maxX, minY, maxY);
_updateBounds(minX, maxX, minY, maxY, ref bounds);
static void _updateInnerBounds(float minX, float maxX, float minY, float maxY) {
if (_maxX - _x <= 0 || _maxY - _y <= 0) {
_x = minX;
_y = minY;
_maxX = maxX;
_maxY = maxY;
static void _updateBounds(float minX, float maxX, float minY, float maxY, ref UnityEngine.Rect bounds) {
if (bounds.width <= 0 || bounds.height <= 0) {
bounds.Set(minX, minY, maxX - minX, maxY - minY);
if (minX < _x) {
_x = minX;
if (minX < bounds.x) {
bounds.x = minX;
if (minY < _y) {
_y = minY;
if (minY < bounds.y) {
bounds.y = minY;
if (maxX > _maxX) {
_maxX = maxX;
if (maxX > bounds.xMax) {
bounds.xMax = maxX;
if (maxY > _maxY) {
_maxY = maxY;
if (maxY > bounds.yMax) {
bounds.yMax = maxY;
}
}
}

font.RequestCharactersInTextureSafe(text, style.UnityFontSize, style.UnityFontStyle);
}
public void allocAdvancesAndPositions(int count) {
if (this._advances == null || this._advances.Length < count) {
this._advances = new float[count];
}
if (this._positions == null || this._positions.Length < count) {
this._positions = new float[count];
}
}
public int nGlyphs() {
return this._count;
}
public float[] getAdvances() {
return this._advances;
}
public float getAdvance() {
return this._advance;
}
public float getX(int index) {
return this._positions[index];
}
public float getY(int index) {
return 0;
}
public float getCharAdvance(int index) {
return this._advances[index];
}
public UnityEngine.Rect translatedBounds() {
return new UnityEngine.Rect(this._bounds.x - this._positions[0],
this._bounds.y, this._bounds.width, this._bounds.height);
}
}
}

52
Runtime/ui/txt/paragraph.cs


float _minIntrinsicWidth;
float _alphabeticBaseline;
float _ideographicBaseline;
float[] _characterWidths;
List<float> _lineHeights = new List<float>();
List<PaintRecord> _paintRecords = new List<PaintRecord>();
List<CodeUnitRun> _codeUnitRuns = new List<CodeUnitRun>();

float preMaxDescent = 0;
float maxWordWidth = 0;
Layout layout = new Layout();
TextBlobBuilder builder = new TextBlobBuilder();
int ellipsizedLength = this._text.Length + (this._paragraphStyle.ellipsis?.Length ?? 0);
builder.allocPos(ellipsizedLength);

}
Range<int>[] words = new Range<int>[maxWordCount];
float[] positions = null;
float[] advances = null;
builder, layout, words, glyphPositions, ref pGlyphPositions
builder, words, glyphPositions, ref pGlyphPositions, ref advances, ref positions
);
}

void _computePaintRecordsFromLine(int lineNumber, ref int lineLimit, ref int styleRunIndex,
ref float maxWordWidth, ref float yOffset, ref float preMaxDescent, TextBlobBuilder builder, Layout layout,
Range<int>[] words, GlyphPosition[] glyphPositions, ref int pGlyphPositions) {
ref float maxWordWidth, ref float yOffset, ref float preMaxDescent, TextBlobBuilder builder,
Range<int>[] words, GlyphPosition[] glyphPositions, ref int pGlyphPositions, ref float[] advances, ref float[] positions) {
var lineRange = this._lineRanges[lineNumber];
int wordIndex = 0;
float runXOffset = 0;

int lineStyleRunCount = this._countLineStyleRuns(lineRange, styleRunIndex, out int maxTextCount);
layout.allocAdvancesAndPositions(maxTextCount);
if (advances == null || advances.Length < maxTextCount) {
advances = new float[maxTextCount];
}
if (positions == null || positions.Length < maxTextCount) {
positions = new float[maxTextCount];
}
int glyphPositionStart = pGlyphPositions;
if (lineStyleRunCount != 0) {

var isLastLineStyleRun = lineStyleRunIndex == lineStyleRunCount - 1;
this._paintRecords.Add(this._generatePaintRecordFromLineStyleRun(
new LineStyleRun(start, end, styleRun.style),
layout,
builder,
isLastLineStyleRun,
lineNumber,

ref tMaxWordWidth,
words,
glyphPositions,
ref pGlyphPositions));
ref pGlyphPositions,
ref advances,
ref positions));
}
}

PaintRecord _generatePaintRecordFromLineStyleRun(
LineStyleRun run,
Layout layout, TextBlobBuilder builder,
TextBlobBuilder builder,
bool isLastLineStyleRun,
int lineNumber,
bool justifyLine,

ref float maxWordWidth,
Range<int>[] words,
GlyphPosition[] glyphPositions,
ref int pLineGlyphPositions) {
ref int pLineGlyphPositions,
ref float[] advances,
ref float[] positions) {
string text = this._text;
int textStart = run.start;
int textEnd = run.end;

}
}
layout.doLayout(runXOffset, text, textStart, textCount, run.style, this._tabStops);
if (advances == null || advances.Length < textCount) {
advances = new float[textCount];
}
if (positions == null || positions.Length < textCount) {
positions = new float[textCount];
}
float advance = Layout.doLayout(runXOffset, text, textStart, textCount, advances, positions, run.style, this._tabStops, out var bounds);
builder.setBounds(layout.translatedBounds());
bounds.x -= positions[0];
builder.setBounds(bounds);
builder, layout,
builder,
advances,
positions,
run.start,
runXOffset,
wordGapWidth,

ref maxWordWidth);
TextBlob textBlob = builder.make();
float advance = layout.getAdvance();
PaintRecord paintRecord = this._generatePaintRecord(run.style, textBlob, runXOffset, advance);
runXOffset += advance;
this._codeUnitRuns.Add(this._generateCodeUnitRun(run, lineNumber, glyphPositions,

}
void _populateGlyphPositions(int textStart, int textCount,
TextBlobBuilder builder, Layout layout,
TextBlobBuilder builder,
float[] advances,
float[] positions,
int runStart,
float runXOffset,
float wordGapWidth,

D.assert(textCount != 0);
float wordStartPosition = float.NaN;
for (int glyphIndex = 0; glyphIndex < textCount; ++glyphIndex) {
float glyphXOffset = layout.getX(glyphIndex) + justifyXOffset;
float glyphAdvance = layout.getCharAdvance(glyphIndex);
float glyphXOffset = positions[glyphIndex] + justifyXOffset;
float glyphAdvance = advances[glyphIndex];
builder.setPosition(glyphIndex, glyphXOffset);
glyphPositions[pLineGlyphPositions++] = new GlyphPosition(runXOffset + glyphXOffset,
glyphAdvance, new Range<int>(textStart + glyphIndex, textStart + glyphIndex + 1));

正在加载...
取消
保存