浏览代码

fix text blur issue

/main
fzhangtj 6 年前
当前提交
d1af1013
共有 5 个文件被更改,包括 95 次插入73 次删除
  1. 28
      Assets/UIWidgets/Tests/Paragraph.cs
  2. 25
      Assets/UIWidgets/ui/txt/linebreaker.cs
  3. 99
      Assets/UIWidgets/ui/txt/paragraph.cs
  4. 13
      Assets/UIWidgets/ui/txt/utils.cs
  5. 3
      Assets/UIWidgets/ui/txt/utils.cs.meta

28
Assets/UIWidgets/Tests/Paragraph.cs


using UnityEngine;
using Color = UIWidgets.ui.Color;
using FontStyle = UIWidgets.ui.FontStyle;
using Rect = UIWidgets.ui.Rect;
namespace UIWidgets.Tests
{

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

alignment: Alignment.center,
child: new RenderDecoratedBox(
decoration: new BoxDecoration(
color: new Color(0xFFFFFFFF),
borderRadius: BorderRadius.all(3),
border: Border.all(Color.fromARGB(255, 255, 0, 0), 1)
),
child: new RenderPadding(EdgeInsets.all(10), p
)
)
);
child: p
)
;
}
private RenderBox flexItemBox(RenderParagraph p, int width = 200, int height = 150)

mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center);
var height = 120;
new RenderParagraph(new TextSpan("Align To Left\nMaterials define how light reacts with the " +
new RenderParagraph(new TextSpan(EditorGUIUtility.pixelsPerPoint.ToString() + "Align To Left\nMaterials define how light reacts with the " +
new RenderParagraph(new TextSpan("Align To Right\nMaterials define how light reacts with the " +
new RenderParagraph(new TextSpan(EditorGUIUtility.pixelsPerPoint.ToString() + "Align To Rgit\nMaterials define how light reacts with the " +
new RenderParagraph(new TextSpan("Align To Center\nMaterials define how light reacts with the " +
new RenderParagraph(new TextSpan(EditorGUIUtility.pixelsPerPoint.ToString() + "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),
height: height

new RenderParagraph(new TextSpan("", children:
new List<TextSpan>()
{
new TextSpan("Real-time 3D revolutionizes:\n the animation pipeline.\n\n\revolutionizesn\n\nReal-time 3D revolutionizes the animation pipeline ", null),
new TextSpan("Real-time 3D revolutionizes:\n the animation pipeline.\n\n\nrevolutionizesn\n\nReal-time 3D revolutionizes the animation pipeline ", null),
})), 200, 80);
}

text: "Height 1.2 Text:" + text),
new TextSpan(style: new painting.TextStyle(height: 1.5),
text: "Height 1.5 Text:" + text),
})));
})), width: 300, height: 300);
}
}

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


private StyledRuns _runs;
public Vector2[] _characterPositions;
public float[] _characterWidth;
public Vector2d[] _characterPositions;
public double[] _characterWidth;
private float _width;
private double _width;
private int _lineStart;
private int _wordStart;
private int _spaceCount = 0;

private List<LineInfo> _lines;
public void setup(string text, StyledRuns runs, Font[] styleRunFonts, float width, Vector2[] characterPositions, float[] characterWidth)
public void setup(string text, StyledRuns runs, Font[] styleRunFonts, double width, Vector2d[] characterPositions, double[] characterWidth)
{
_text = text;
_runs = runs;

_wordStart = blockStart;
_spaceCount = 0;
float offsetX = 0.0f;
double offsetX = 0.0;
var runIterator = _runs.iterator();
for (var charIndex = blockStart; charIndex < blockEnd; charIndex++)
{

var style = run.style;
CharacterInfo charInfo;
var result = font.GetCharacterInfo(_text[charIndex], out charInfo, 0, run.style.UnityFontStyle);
Debug.Assert(result, "fail to get character info");
var charInfo = new CharacterInfo();
if (_text[charIndex] == '\t')
{
_spaceCount++;

float tabSize = charInfo.advance * tabCount;
var newX = (float)Math.Floor(((offsetX / tabSize) + 1) * tabSize);
double tabSize = charInfo.advance * tabCount;
var newX = Math.Floor(((offsetX / tabSize) + 1) * tabSize);
if (newX > _width && _lineStart != charIndex)
{
_characterWidth[charIndex] = tabSize;

}
else if (_text[charIndex] == ' ')
{
font.GetCharacterInfo(_text[charIndex], out charInfo, 0, run.style.UnityFontStyle);
_spaceCount++;
_characterPositions[charIndex].x = offsetX;
_characterWidth[charIndex] = charInfo.advance;

else
{
font.GetCharacterInfo(_text[charIndex], out charInfo, 0, run.style.UnityFontStyle);
if (_spaceCount > 0 || blockStart == charIndex)
{
_wordStart = charIndex;

{
return;
}
var offset = new Vector2(-_characterPositions[end].x, 0);
var offset = new Vector2d(-_characterPositions[end].x, 0);
_characterPositions[end].x = 0;
if (end < last)
{

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


namespace UIWidgets.ui
{
public struct Vector2d
{
public double x;
public double y;
public Vector2d(double x = 0.0, double y = 0.0)
{
this.x = x;
this.y = y;
}
public static Vector2d operator +(Vector2d a, Vector2d b)
{
return new Vector2d(a.x + b.x, a.y + b.y);
}
public static Vector2d operator -(Vector2d a, Vector2d b)
{
return new Vector2d(a.x - b.x, a.y - b.y);
}
}
public class Paragraph
{
struct Range<T>: IEquatable<Range<T>>

public readonly int endIncludingNewLine;
public readonly bool hardBreak;
}
class LayoutContext
{
public int width;
public int index;
public Vector2 offset;
public TextStyle style;
public Font font;
public int wordStart;
public int lineStart;
public int prevWordEnd;
}
private static readonly Shader textShader;

private ParagraphStyle _paragraphStyle;
private List<LineRange> _lineRanges = new List<LineRange>();
private List<double> _lineWidths = new List<double>();
private Vector2[] _characterPositions;
private Vector2d[] _characterPositions;
private float[] _characterWidths;
private double[] _characterWidths;
private LayoutContext context;
private bool _didExceedMaxLines;
// private double _characterWidth;

public double alphabeticBaseline
{
get { return 0.0; }
get { return _alphabeticBaseline; }
get { return 0.0; }
get { return _ideographicBaseline; }
}
public bool didExceedMaxLines

double xOffset = getLineXOffset(lineTotalAdvance);
if (xOffset > 0 || xOffset < 0)
{
offsetCharacters(new Vector2((float)xOffset, 0),
offsetCharacters(new Vector2d(xOffset, 0),
_characterPositions, line.start, line.endExcludingWhitespace);
}
}

_styleRunFonts = null;
}
public static void offsetCharacters(Vector2 offset, Vector2[] characterPos, int start, int end)
public static void offsetCharacters(Vector2d offset, Vector2d[] characterPos, int start, int end)
{
if (characterPos != null)
{

private void setup()
{
_characterPositions = new Vector2[_text.Length];
if (_characterPositions == null || _characterPositions.Length < _text.Length)
{
_characterPositions = new Vector2d[_text.Length];
}
_characterWidths = new float[_text.Length];
_characterWidths = new double[_text.Length];
if (_styleRunFonts == null)
{
_styleRunFonts = new Font[_runs.size];

_ideographicBaseline = maxAscent; // todo Properly implement ideographic_baseline
}
lastDescent = maxDescent;
yOffset += maxAscent + lastDescent;
yOffset = Utils.PixelCorrectRound(yOffset + maxAscent + lastDescent);
_characterPositions[charIndex].y = (float)yOffset;
_characterPositions[charIndex].y = yOffset;
_lineHeights.Add((_lineHeights.Count == 0 ? 0 : _lineHeights[_lineHeights.Count - 1]) +
Math.Round(maxAscent + maxDescent));
}

newLinePositions.Add(_text.Length);
var lineBreaker = new LineBreaker();
lineBreaker.setup(_text, _runs, _styleRunFonts, (float)_width, _characterPositions, _characterWidths);
lineBreaker.setup(_text, _runs, _styleRunFonts, _width, _characterPositions, _characterWidths);
for (var newlineIndex = 0; newlineIndex < newLinePositions.Count; ++newlineIndex)
{

var vertices = new Vector3[_text.Length * 4];
var triangles = new int[_text.Length * 6];
var uv = new Vector2[_text.Length * 4];
Vector3 offset = new Vector3((float)x, (float)y, 0);
Vector3 offset = new Vector3((float)Utils.PixelCorrectRound(x), (float)Utils.PixelCorrectRound(y), 0);
if (_text[charIndex] != '\n')
if (_text[charIndex] != '\n' && _text[charIndex] != '\t')
var result = font.GetCharacterInfo(_text[charIndex], out charInfo, run.style.UnityFontSize, run.style.UnityFontStyle);
Debug.Assert(result, "fail to get character info");
font.GetCharacterInfo(_text[charIndex], out charInfo, run.style.UnityFontSize, run.style.UnityFontStyle);
vertices[4 * charIndex + 0] = offset + new Vector3(position.x + charInfo.minX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 1] = offset + new Vector3(position.x + charInfo.maxX, position.y - charInfo.maxY, 0);
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);
vertices[4 * charIndex + 0] = offset + new Vector3((float)(position.x + charInfo.minX),
(float)(position.y - charInfo.maxY), 0);
vertices[4 * charIndex + 1] = offset + new Vector3((float)(position.x + charInfo.maxX),
(float)(position.y - charInfo.maxY), 0);
vertices[4 * charIndex + 2] = offset + new Vector3(
(float)(position.x + charInfo.maxX), (float)(position.y - charInfo.minY), 0);
vertices[4 * charIndex + 3] = offset + new Vector3(
(float)(position.x + charInfo.minX), (float)(position.y - charInfo.minY), 0);
}
else
{

uv[4 * charIndex + 1] = charInfo.uvTopRight;
uv[4 * charIndex + 2] = charInfo.uvBottomRight;
uv[4 * charIndex + 3] = charInfo.uvBottomLeft;
}
triangles[6 * charIndex + 0] = 4 * charIndex + 0;

triangles[6 * charIndex + 5] = 4 * charIndex + 3;
}
for (var i = 0; i < vertices.Length; i++)
{
vertices[i].x = (float)Math.Round(vertices[i].x);
vertices[i].y = (float)Math.Round(vertices[i].y);
}
// for (var i = 0; i < vertices.Length; i++)
// {
// vertices[i].x = (float)Math.Round(vertices[i].x);
// vertices[i].y = (float)Math.Round(vertices[i].y);
// }
var mesh = new Mesh()
{

if (align == TextAlign.right) {
return _width - lineTotalAdvance;
} else if (align == TextAlign.center) {
return (_width - lineTotalAdvance) / 2;
return Utils.PixelCorrectRound((_width - lineTotalAdvance) / 2);
} else {
return 0;
}

double justifyOffset = 0.0;
foreach (var word in words)
{
offsetCharacters(new Vector2((float)(justifyOffset), 0.0f),
offsetCharacters(new Vector2d(justifyOffset),
justifyOffset = Utils.PixelCorrectRound(justifyOffset);
}
}

13
Assets/UIWidgets/ui/txt/utils.cs


using System;
using UnityEditor;
namespace UIWidgets.ui
{
internal class Utils
{
public static double PixelCorrectRound(double v)
{
return Math.Round(v * EditorGUIUtility.pixelsPerPoint) / EditorGUIUtility.pixelsPerPoint;
}
}
}

3
Assets/UIWidgets/ui/txt/utils.cs.meta


fileFormatVersion: 2
guid: cd4e5380cbc9438a8d2c8a060b110ea1
timeCreated: 1536199372
正在加载...
取消
保存