浏览代码

text aligment

/main
fzhangtj 6 年前
当前提交
d6a34afe
共有 3 个文件被更改,包括 117 次插入15 次删除
  1. 35
      Assets/UIWidgets/Tests/Paragraph.cs
  2. 5
      Assets/UIWidgets/ui/text.cs
  3. 92
      Assets/UIWidgets/ui/txt/paragraph.cs

35
Assets/UIWidgets/Tests/Paragraph.cs


this._options = new Func<RenderBox>[] {
this.text,
this.textHeight,
this.textOverflow
this.textOverflow,
this.textAlign,
};
this._optionStrings = this._options.Select(x => x.Method.Name).ToArray();
this._selected = 0;

new TextSpan(style: new painting.TextStyle(fontSize: 14),
text: "FontSize 14"),
})));
}
RenderBox textAlign()
{
// var flexbox = new RenderFlex(
// direction: Axis.horizontal,
// crossAxisAlignment: CrossAxisAlignment.center);
//
// flexbox.add(box(
// new RenderParagraph(new TextSpan("Align To Left\nMaterials define how light reacts with the " +
// "surface of a model, and are an essential ingredient in making " +
// "believable visuals. When you’ve created a "), textAlign: TextAlign.left)
// ));
// flexbox.add(box(
// new RenderParagraph(new TextSpan("Align To Right\nMaterials define how light reacts with the " +
// "surface of a model, and are an essential ingredient in making " +
// "believable visuals. When you’ve created a "), textAlign: TextAlign.right)
// ));
// flexbox.add(box(
// new RenderParagraph(new TextSpan("Align To Center\nMaterials define how light reacts with the " +
// "surface of a model, and are an essential ingredient in making " +
// "believable visuals. When you’ve created a "), textAlign: TextAlign.center)
// ));
//return flexbox;
return box(
new RenderParagraph(new TextSpan("Align To Center\nMaterials define how light reacts with the " +
"surface of a model, and are an essential ingredient in making " +
"believable visuals. When you’ve created a when you want to un-tether " +
"the specular color from the material’s albedo. This is the case with " +
"non-metal \t materials or "), textAlign: TextAlign.center)
);
}
RenderBox textOverflow()

5
Assets/UIWidgets/ui/text.cs


);
}
public TextAlign TextAlign
{
get { return textAlign ?? TextAlign.left; }
}
public readonly TextAlign? textAlign;
public readonly TextDirection? textDirection;
public readonly FontWeight? fontWeight;

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


return ch == ' ' || ch == CHAR_NBSP;
}
// 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.
static bool isLineEndSpace(char c) {
return c == '\n' || c == ' ' || c == 0x1680 || (0x2000 <= c && c <= 0x200A && c != 0x2007) ||
c == 0x205F || c == 0x3000;
}
public double height
{
get { return _lineHeights.Count == 0 ? 0 : _lineHeights[_lineHeights.Count - 1]; }

{
maxWordWidth = wordWidth;
}
});
});
if (_paragraphStyle.TextAlign == TextAlign.justify && !_lineRanges[lineNumber].hardBreak
&& lineNumber != lineLimits - 1)
{
justifyLine(lineNumber, words);
} else if (line.endExcludingWhitespace > line.start)
{
Debug.Assert(!isLineEndSpace(_text[line.endExcludingWhitespace - 1]));
var lineTotalAdvance = _characterPositions[line.endExcludingWhitespace - 1].x +
_characterWidths[line.endExcludingWhitespace - 1];
double xOffset = getLineXOffset(lineTotalAdvance);
if (xOffset > 0 || xOffset < 0)
{
offsetCharacters(new Vector2((float)xOffset, 0),
_characterPositions, line.start, line.endExcludingWhitespace);
}
}
}

_characterPositions[charIndex].y = (float)yOffset;
}
_lineHeights.Add((_lineHeights.Count == 0 ? 0 : _lineHeights[_lineHeights.Count - 1]) +
Math.Round(maxAscent + maxDescent));
}

var line = lines[i];
var end = i + 1 < lines.Count ? lines[i + 1].start : blockEnd;
var nonWhiteSpace = end - 1;
while (nonWhiteSpace >= line.start && _text[nonWhiteSpace] == ' ' || _text[nonWhiteSpace] == '\t')
var nonSpaceEnd = end;
while (nonSpaceEnd > line.start && isLineEndSpace(_text[nonSpaceEnd - 1]))
nonWhiteSpace--;
nonSpaceEnd--;
_lineRanges.Add(new LineRange(line.start, end, nonWhiteSpace, end + 1, end == blockEnd));
_lineRanges.Add(new LineRange(line.start, end, nonSpaceEnd, end + 1, end == blockEnd));
_lineWidths.Add(line.width);
}
}

vertices[4 * charIndex + 2] = offset + new Vector3(position.x + charInfo.maxX, position.y - charInfo.minY, 0);
vertices[4 * charIndex + 3] = offset + new Vector3(position.x + charInfo.minX, position.y - charInfo.minY, 0);
if (_text[charIndex] != ' ' && _text[charIndex] != '\t' && _text[charIndex] != '\n')
if (isWordSpace(_text[charIndex]) || isLineEndSpace(_text[charIndex]) || _text[charIndex] == '\t')
{
uv[4 * charIndex + 0] = Vector2.zero;
uv[4 * charIndex + 1] = Vector2.zero;
uv[4 * charIndex + 2] = Vector2.zero;
uv[4 * charIndex + 3] = Vector2.zero;
} else
{
uv[4 * charIndex + 0] = charInfo.uvTopLeft;
uv[4 * charIndex + 1] = charInfo.uvTopRight;

}
else
{
uv[4 * charIndex + 0] = Vector2.zero;
uv[4 * charIndex + 1] = Vector2.zero;
uv[4 * charIndex + 2] = Vector2.zero;
uv[4 * charIndex + 3] = Vector2.zero;
}
triangles[6 * charIndex + 0] = 4 * charIndex + 0;
triangles[6 * charIndex + 1] = 4 * charIndex + 1;

return mesh;
}
private double getLineXOffset(double lineTotalAdvance) {
if (double.IsInfinity(_width))
{
return 0;
}
var align = _paragraphStyle.TextAlign;
if (align == TextAlign.right) {
return _width - lineTotalAdvance;
} else if (align == TextAlign.center) {
return (_width - lineTotalAdvance) / 2;
} else {
return 0;
}
}
private void justifyLine(int lineNumber, List<Range<int>> words)
{
if (words.Count <= 1)
{
return;
}
var line = _lineRanges[lineNumber];
Debug.Assert(!isLineEndSpace(_text[line.endExcludingWhitespace - 1]));
var lineTotalAdvance = _characterPositions[line.endExcludingWhitespace - 1].x +
_characterWidths[line.endExcludingWhitespace - 1];
double gapWidth = (_width - lineTotalAdvance) / (words.Count - 1);
double justifyOffset = 0.0;
foreach (var word in words)
{
offsetCharacters(new Vector2((float)(justifyOffset), 0.0f),
_characterPositions, word.start, word.end);
justifyOffset += gapWidth;
}
}
private List<Range<int>> findWords(int start, int end)
{
var inWord = false;

正在加载...
取消
保存