浏览代码

update

/main
fzhangtj 6 年前
当前提交
298a9754
共有 11 个文件被更改,包括 222 次插入120 次删除
  1. 13
      Assets/UIWidgets/Tests/Text.cs
  2. 2
      Assets/UIWidgets/painting/text_painter.cs
  3. 7
      Assets/UIWidgets/painting/text_style.cs
  4. 11
      Assets/UIWidgets/ui/painting/canvas.cs
  5. 11
      Assets/UIWidgets/ui/painting/canvas_impl.cs
  6. 4
      Assets/UIWidgets/ui/painting/draw_cmd.cs
  7. 8
      Assets/UIWidgets/ui/painting/picture.cs
  8. 7
      Assets/UIWidgets/ui/text.cs
  9. 46
      Assets/UIWidgets/ui/txt/linebreaker.cs
  10. 155
      Assets/UIWidgets/ui/txt/paragraph.cs
  11. 78
      Assets/UIWidgets/ui/txt/styled_runs.cs

13
Assets/UIWidgets/Tests/Text.cs


TextBaseline textBaseline, double height, TextDecoration decoration
*
*/
var style1 = new painting.TextStyle(true, Color.fromARGB(1, 2, 2, 2), null, FontWeight.w400, FontStyle.normal, 1,
var style1 = new painting.TextStyle(true, Color.fromARGB(255, 0, 0, 0), null, FontWeight.w400, FontStyle.normal, 1,
var style2 = new painting.TextStyle(true, Color.fromARGB(1, 2, 2, 2), null, FontWeight.w400, FontStyle.normal, 1,
1, TextBaseline.alphabetic, 1, TextDecoration.none);
var style2 = new painting.TextStyle(true, Color.fromARGB(255, 255, 0, 0), null, FontWeight.w400, FontStyle.normal, 1,
1, TextBaseline.alphabetic, 1, TextDecoration.none, fontFamily:"Helvetica");
minWidth: 100,
maxWidth: 100,
minWidth: 200,
maxWidth: 200,
alignment: Alignment.center,
// child: new RenderParagraph(new TextSpan(style1, "is is FontStyle fontStyle, double letterSpacing, double wordSpacing, ",
// null))
child: new RenderParagraph(new TextSpan(style1, "",
new List<TextSpan>()
{

2
Assets/UIWidgets/painting/text_painter.cs


public void paint(Canvas canvas, Offset offset)
{
Debug.Assert(_needsLayout);
Debug.Assert(!_needsLayout);
_paragraph.paint(canvas, offset.dx, offset.dy);
}

7
Assets/UIWidgets/painting/text_style.cs


public readonly TextDecoration decoration;
public readonly string fontFamily;
public TextStyle(bool? inherit, Color color, double? fontSize, FontWeight? fontWeight,
FontStyle? fontStyle, double? letterSpacing, double? wordSpacing,
TextBaseline? textBaseline, double? height, TextDecoration decoration)
public TextStyle(bool? inherit = null, Color color = null, double? fontSize = null, FontWeight? fontWeight = null,
FontStyle? fontStyle = null, double? letterSpacing = null, double? wordSpacing = null,
TextBaseline? textBaseline = null, double? height = null, TextDecoration decoration = null, string fontFamily = null)
{
this.inherit = inherit;
this.color = color;

this.textBaseline = textBaseline;
this.height = height;
this.decoration = decoration;
this.fontFamily = fontFamily;
}
public ui.TextStyle getTextStyle(double textScaleFactor = 1.0)

11
Assets/UIWidgets/ui/painting/canvas.cs


void clipRect(Rect rect);
void clipRRect(RRect rrect);
void drawMesh(Mesh mesh, Material material);
}
public class RecorderCanvas : Canvas {

public void clipRRect(RRect rrect) {
this._recorder.addDrawCmd(new DrawClipRRect {
rrect = rrect,
});
}
public void drawMesh(Mesh mesh, Material material)
{
this._recorder.addDrawCmd(new DrawMesh() {
mesh = mesh,
material = material,
});
}
}

11
Assets/UIWidgets/ui/painting/canvas_impl.cs


GL.End();
}
}
public void drawRect(Rect rect, BorderWidth borderWidth, BorderRadius borderRadius, Paint paint) {
this.prepareGL(CanvasImpl.guiRoundedRectMat);

new UnityEngine.Rect(0.0f, 0.0f, 1f, 1f), 0, 0, 0, 0,
paint.color.toColor(), CanvasImpl.guiRoundedRectMat);
}
public void drawRectShadow(Rect rect, Paint paint) {
this.prepareGL(CanvasImpl.shadowRectMat);

} else if (drawCmd is DrawClipRRect) {
var drawClipRRect = (DrawClipRRect) drawCmd;
this.clipRRect(drawClipRRect.rrect);
} else if (drawCmd is DrawMesh) {
var drawMesh = (DrawMesh) drawCmd;
this.drawMesh(drawMesh.mesh, drawMesh.material);
} else {
throw new Exception("unknown drawCmd: " + drawCmd);
}

}
this.pushClipRRect(rect, this._transform);
}
public void drawMesh(Mesh mesh, Material material)
{
material.SetPass(0);
Graphics.DrawMeshNow(mesh, Matrix4x4.identity);
}
private void pushClipRect(Rect clipRect, Matrix4x4 transform) {

4
Assets/UIWidgets/ui/painting/draw_cmd.cs


public class DrawMesh : DrawCmd {
public Mesh mesh;
public Material material;
}
}

8
Assets/UIWidgets/ui/painting/picture.cs


} else if (drawCmd is DrawClipRRect) {
var drawClipRRect = (DrawClipRRect) drawCmd;
this.addClipRect(drawClipRRect.rrect.outerRect);
} else {
} else if (drawCmd is DrawMesh) {
var bounds = ((DrawMesh)drawCmd).mesh.bounds;
var rect = Rect.fromLTRB(bounds.min.x, bounds.min.y, bounds.max.x, bounds.max.y);
this.addPaintBounds(rect);
} else
{
throw new Exception("unknown drawCmd: " + drawCmd);
}
}

7
Assets/UIWidgets/ui/text.cs


using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
namespace UIWidgets.ui {

public static readonly double defaultFontSize = 14.0;
public static readonly FontWeight defaultFontWeight = FontWeight.w400;
public static readonly FontStyle defaultFontStyle = FontStyle.normal;
public static readonly Color defaultColor = Color.fromARGB(255, 255, 255, 255);
public Color color;
public double? fontSize;
public FontWeight? fontWeight;

public FontWeight safeFontWeight
{
get { return fontWeight ?? defaultFontWeight; }
}
public UnityEngine.Color UnityColor
{
get { return (color ?? defaultColor).toColor(); }
}
public UnityEngine.FontStyle UnityFontStyle

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


public Vector2[] _characterPositions;
public float[] _characterWidth;
// private List<int> breaks;
// private int _preWordEnd;
private Font[] _styleRunFonts;
// private double lengths
public void setup(string text, StyledRuns runs, float width, Vector2[] characterPositions, float[] characterWidth)
public void setup(string text, StyledRuns runs, Font[] styleRunFonts, float width, Vector2[] characterPositions, float[] characterWidth)
{
_text = text;
_runs = runs;

_styleRunFonts = styleRunFonts;
}
public List<LineInfo> getLines()

{
_lines = new List<LineInfo>();
float offsetX = 0.0f;
var runIterator = _runs.iterator();
var run = _runs.getRun(_runIndex);
while ((run == null || run.end <= charIndex || charIndex < run.start) &&
_runIndex + 1 < _runs.size)
{
_runIndex++;
run = _runs.getRun(_runIndex);
}
runIterator.nextTo(charIndex);
var run = runIterator.run;
var font = _styleRunFonts[runIterator.runIndex];
run.font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), run.style.UnityFontSize, run.style.UnityFontStyle);
font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), 0, run.style.UnityFontStyle);
var font = run.font;
var result = font.GetCharacterInfo(_text[charIndex], out charInfo);
// _characterSize[charIndex] = style.UnityFontSize;
//
var result = font.GetCharacterInfo(_text[charIndex], out charInfo, 0, run.style.UnityFontStyle);
if (_text[charIndex] == '\t')
{
_spaceCount++;

}
/*
* CharacterInfo charInfo;
context.font.GetCharacterInfo(c, out charInfo,
context.style.UnityFontSize, context.style.UnityFontStyle);
_characterInfos[context.index] = charInfo;
_characterPositions[context.index].x = context.offset.x;
// _characterPositions[context.index].y = context.offset.y;
if (context.offset.x + charInfo.advance > _width)
{
wordWrap(context);
}
return null;
*/
}
makeLine(blockEnd, blockEnd);

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


private List<double> _lineWidths = new List<double>();
private Vector2[] _characterPositions;
private CharacterInfo[] _characterInfos;
private Font[] _fonts;
private double _maxIntrinsicWidth;
private double _minIntrinsicWidth;
private Font[] _styleRunFonts;
private float[] _characterWidths;
private float[] _characterSize;

public void paint(Canvas canvas, double x, double y)
{
var mesh = generateMesh();
//var mesh = ;
for (int runIndex = 0; runIndex < _runs.size; ++runIndex)
{
var run = _runs.getRun(runIndex);
if (run.start < run.end)
{
var font = _styleRunFonts[runIndex];
var mesh = generateMesh(x, y, font, run);
canvas.drawMesh(mesh, font.material);
}
}
// canvas.drawMesh(mesh, _text, _runs);
}
public void layout(ParagraphConstraints constraints)

// return;
// }
_needsLayout = false;
_width = Math.Floor(constraints.width);

layoutLines();
_maxIntrinsicWidth = 0;
double lineBlockWidth = 0;
for (int i = 0; i < _lineRanges.Count; ++i)
{
var line = _lineRanges[i];
lineBlockWidth += _lineWidths[i];
if (line.hardBreak)
{
_maxIntrinsicWidth = Math.Max(lineBlockWidth, _maxIntrinsicWidth);
lineBlockWidth = 0;
}
}
}

_text = text;
_runs = runs;
_needsLayout = true;
_styleRunFonts = null;
}
public void setParagraphStyle(ParagraphStyle style)

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

}
}
}
private Offset insertCharacter(char c, LayoutContext context)
{
CharacterInfo charInfo;
context.font.GetCharacterInfo(c, out charInfo,
context.style.UnityFontSize, context.style.UnityFontStyle);
_characterInfos[context.index] = charInfo;
_characterPositions[context.index].x = context.offset.x;
// _characterPositions[context.index].y = context.offset.y;
if (context.offset.x + charInfo.advance > _width)
{
wordWrap(context);
}
return null;
}
private void wordWrap(LayoutContext context)
{

}
}
private void setup()
{
_characterPositions = new Vector2[_text.Length];

_fonts = new Font[_text.Length];
if (_styleRunFonts == null)
{
_styleRunFonts = new Font[_runs.size];
for (int i = 0; i < _styleRunFonts.Length; ++i)
{
var run = _runs.getRun(i);
if (run.start < run.end)
{
_styleRunFonts[i] = Font.CreateDynamicFontFromOSFont(run.style.safeFontFamily,
run.style.UnityFontSize);
}
}
}
// for (var i = 0; i < _runs.size; i++)

for (;runIndex < _runs.size;runIndex++)
{
var run = _runs.getRun(runIndex);
var ascent = run.font.ascent * (run.style.height??1.0);
var descent = (run.font.lineHeight - run.font.ascent) * (run.style.height??1.0);
if (ascent > maxAscent)
if (run.start < run.end)
maxAscent = ascent;
}
if (descent > maxDescent)
{
maxDescent = descent;
var font = _styleRunFonts[runIndex];
var ascent = font.ascent * (run.style.height??1.0);
var descent = (font.lineHeight - font.ascent) * (run.style.height??1.0);
if (ascent > maxAscent)
{
maxAscent = ascent;
}
if (descent > maxDescent)
{
maxDescent = descent;
}
if (runIndex + 1 < _runs.size)
{
var nextRun = _runs.getRun(runIndex + 1);

{
_characterPositions[charIndex].y = (float)yOffset;
}
}
}

int runIndex = 0;
StyledRuns.Run lastRun = null;
var lineBreaker = new LineBreaker();
lineBreaker.setup(_text, _runs, (float)_width, _characterPositions, _characterWidths);
lineBreaker.setup(_text, _runs, _styleRunFonts, (float)_width, _characterPositions, _characterWidths);
for (var newlineIndex = 0; newlineIndex < newLinePositions.Count; ++newlineIndex)
{

}
private Mesh generateMesh()
private Mesh generateMesh(double x, double y, Font font, StyledRuns.Run run)
Vector3 pos = Vector3.zero;
Vector3 pos = new Vector3((float)x, (float)y, 0);
var offset = new Vector2((float) x, (float) y);
font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), run.style.UnityFontSize, run.style.UnityFontStyle);
for (int charIndex = run.start; charIndex < run.end; ++charIndex)
{
CharacterInfo charInfo;
var result = font.GetCharacterInfo(_text[charIndex], out charInfo);
var position = _characterPositions[charIndex] + offset;
vertices[4 * charIndex + 0] = pos + new Vector3(position.x + charInfo.minX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 1] = pos + new Vector3(position.x + charInfo.maxX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 2] = pos + new Vector3(position.x + charInfo.maxX, position.y - charInfo.minY, 0);
vertices[4 * charIndex + 3] = pos + new Vector3(position.x + charInfo.minX, position.y - charInfo.minY, 0);
int runIndex = 0;
for (int charIndex = 0; charIndex < _text.Length; charIndex++)
{
var run = _runs.getRun(runIndex);
while ((run == null || run.end <= charIndex || charIndex < run.start) &&
charIndex + 1 < _runs.size)
if (_text[charIndex] != ' ' && _text[charIndex] != '\t')
charIndex++;
run = _runs.getRun(charIndex);
uv[4 * charIndex + 0] = charInfo.uvTopLeft;
uv[4 * charIndex + 1] = charInfo.uvTopRight;
uv[4 * charIndex + 2] = charInfo.uvBottomRight;
uv[4 * charIndex + 3] = charInfo.uvBottomLeft;
if (run.start == charIndex)
else
run.font.RequestCharactersInTexture(_text.Substring(run.start, run.end - run.start), run.style.UnityFontSize, run.style.UnityFontStyle);
uv[4 * charIndex + 0] = Vector2.zero;
uv[4 * charIndex + 1] = Vector2.zero;
uv[4 * charIndex + 2] = Vector2.zero;
uv[4 * charIndex + 3] = Vector2.zero;
CharacterInfo charInfo;
var result = run.font.GetCharacterInfo(_text[charIndex], out charInfo);
var position = _characterPositions[charIndex];
vertices[4 * charIndex + 0] = pos + new Vector3(charInfo.minX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 1] = pos + new Vector3(charInfo.maxX, position.y - charInfo.maxY, 0);
vertices[4 * charIndex + 2] = pos + new Vector3(charInfo.maxX, position.y + charInfo.minY, 0);
vertices[4 * charIndex + 3] = pos + new Vector3(charInfo.minX, position.y + charInfo.minY, 0);
uv[4 * charIndex + 0] = charInfo.uvTopLeft;
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 + 1] = 4 * charIndex + 1;

triangles[6 * charIndex + 4] = 4 * charIndex + 2;
triangles[6 * charIndex + 5] = 4 * charIndex + 3;
}
var mesh = new Mesh()
{
vertices = vertices,

var colors = new UnityEngine.Color[vertices.Length];
for (var i = 0; i < colors.Length; i++)
{
colors[i] = run.style.UnityColor;
}
mesh.colors = colors;
return mesh;
}

78
Assets/UIWidgets/ui/txt/styled_runs.cs


using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UIWidgets.ui

private readonly List<TextStyle> styles = new List<TextStyle>();
private readonly List<IndexedRun> runs = new List<IndexedRun>();
public class RunIterator
{
private int _charIndex;
private int _runIndex;
private StyledRuns _runs;
public void nextTo(int index)
{
if (_charIndex > index)
{
throw new ArgumentException("can to move back");
}
_charIndex = index;
while (_runIndex < _runs.size)
{
var run = _runs.getRun(_runIndex);
if (run.start <= _charIndex && _charIndex < run.end)
{
break;
}
_runIndex++;
}
}
public Run run
{
get { return _runs.getRun(_runIndex); }
}
public int charIndex
{
get { return _charIndex; }
}
public int runIndex
{
get { return _runIndex; }
}
public bool end
{
get
{
return runIndex >= _runs.size;
}
}
internal RunIterator(StyledRuns runs)
{
_charIndex = 0;
_runIndex = 0;
_runs = runs;
}
}
public Font _font;
public Run(TextStyle style, int start, int end)
{

}
public Font font
{
get
{
if (_font == null)
{
_font = Font.CreateDynamicFontFromOSFont(style.safeFontFamily,
(style.UnityFontSize));
}
return _font;
}
}
}

return new Run(styles[run.styleIndex], run.start, run.end);
}
public RunIterator iterator()
{
return new RunIterator(this);
}
public int size
{
get { return runs.Count; }

正在加载...
取消
保存