浏览代码

fix font size 0 issue & text background

/main
fzhangtj 6 年前
当前提交
1461d376
共有 9 个文件被更改,包括 159 次插入35 次删除
  1. 1
      Runtime/painting/text_style.cs
  2. 2
      Runtime/ui/painting/canvas_impl.cs
  3. 63
      Runtime/ui/painting/txt/font_manager.cs
  4. 29
      Runtime/ui/painting/txt/mesh_generator.cs
  5. 9
      Runtime/ui/txt/layout.cs
  6. 11
      Runtime/ui/txt/linebreaker.cs
  7. 25
      Runtime/ui/txt/paragraph.cs
  8. 43
      Samples/UIWidgetSample/txt/TextStyleSample.cs
  9. 11
      Samples/UIWidgetSample/txt/TextStyleSample.cs.meta

1
Runtime/painting/text_style.cs


decoration: other.decoration,
decorationColor: other.decorationColor,
decorationStyle: other.decorationStyle,
background: other.background,
debugLabel: mergedDebugLabel
);
}

2
Runtime/ui/painting/canvas_impl.cs


var font = FontManager.instance.getOrCreate(textBlob.style.fontFamily, style.fontWeight, style.fontStyle).font;
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * scale);
var subText = textBlob.text.Substring(textBlob.textOffset, textBlob.textSize);
font.RequestCharactersInTexture(subText, fontSizeToLoad, style.UnityFontStyle);
font.RequestCharactersInTextureSafe(subText, fontSizeToLoad, style.UnityFontStyle);
var tex = font.material.mainTexture;

63
Runtime/ui/painting/txt/font_manager.cs


}
class GlyphInfo {
public static GlyphInfo empty = new GlyphInfo(Rect.zero,0, 0, new Vector2(0, 0),
new Vector2(0, 0), new Vector2(0, 0), new Vector2(0, 0));
public readonly Rect rect;
public readonly float advance;
public readonly float glyphHeight;
public readonly Vector2 uvTopLeft;
public readonly Vector2 uvTopRight;
public readonly Vector2 uvBottomLeft;
public readonly Vector2 uvBottomRight;
public GlyphInfo(CharacterInfo info) {
this.rect = Rect.fromLTRB(info.minX, -info.maxY, info.maxX, -info.minY);
this.advance = info.advance;
this.glyphHeight = info.glyphHeight;
this.uvTopLeft = info.uvTopLeft;
this.uvTopRight = info.uvTopRight;
this.uvBottomLeft = info.uvBottomLeft;
this.uvBottomRight = info.uvBottomRight;
}
public GlyphInfo(Rect rect, float advance, float glyphHeight,
Vector2 uvTopLeft, Vector2 uvTopRight, Vector2 uvBottomLeft, Vector2 uvBottomRight) {
this.rect = rect;
this.advance = advance;
this.glyphHeight = glyphHeight;
this.uvTopLeft = uvTopLeft;
this.uvTopRight = uvTopRight;
this.uvBottomLeft = uvBottomLeft;
this.uvBottomRight = uvBottomRight;
}
}
{
public static CharacterInfo getCharacterInfo(this Font font, char ch, int fontSize, UnityEngine.FontStyle fontStyle)
{
{
internal static GlyphInfo getGlyphInfo(this Font font, char ch, int fontSize, UnityEngine.FontStyle fontStyle) {
if (fontSize <= 0) {
return GlyphInfo.empty;
}
if (!success && !char.IsControl(ch)) {
Debug.LogWarning($"character info not found from the given font: character '{ch}' (code{(int)ch}) font: ${font.name}");
if (!success) {
if (!char.IsControl(ch)) {
Debug.LogWarning($"character info not found from the given font: character '{ch}' (code{(int)ch}) font: ${font.name}");
}
return GlyphInfo.empty;
}
return new GlyphInfo(info);
}
internal static void RequestCharactersInTextureSafe(this Font font, string text, int fontSize,
UnityEngine.FontStyle fontStyle = UnityEngine.FontStyle.Normal) {
if (fontSize <= 0) {
return;
return info;
font.RequestCharactersInTexture(text, fontSize, fontStyle);
}
}
}

29
Runtime/ui/painting/txt/mesh_generator.cs


continue;
}
CharacterInfo charInfo = font.getCharacterInfo(ch, fontSizeToLoad, style.UnityFontStyle);
var minX = charInfo.minX / this._scale;
var maxX = charInfo.maxX / this._scale;
var minY = charInfo.minY / this._scale;
var maxY = charInfo.maxY / this._scale;
if (fontSizeToLoad == 0) {
continue;
}
var glyphInfo = font.getGlyphInfo(ch, fontSizeToLoad, style.UnityFontStyle);
var minX = glyphInfo.rect.left / this._scale;
var maxX = glyphInfo.rect.right / this._scale;
var minY = glyphInfo.rect.top / this._scale;
var maxY = glyphInfo.rect.bottom / this._scale;
vertices.Add(new Vector3((position.x + minX), (position.y - maxY), 0));
vertices.Add(new Vector3((position.x + maxX), (position.y - maxY), 0));
vertices.Add(new Vector3((position.x + maxX), (position.y - minY), 0));
vertices.Add(new Vector3((position.x + minX), (position.y - minY), 0));
vertices.Add(new Vector3((position.x + minX), (position.y + minY), 0));
vertices.Add(new Vector3((position.x + maxX), (position.y + minY), 0));
vertices.Add(new Vector3((position.x + maxX), (position.y + maxY), 0));
vertices.Add(new Vector3((position.x + minX), (position.y + maxY), 0));
triangles.Add(baseIndex);
triangles.Add(baseIndex + 1);

triangles.Add(baseIndex + 3);
uv.Add(charInfo.uvTopLeft);
uv.Add(charInfo.uvTopRight);
uv.Add(charInfo.uvBottomRight);
uv.Add(charInfo.uvBottomLeft);
uv.Add(glyphInfo.uvTopLeft);
uv.Add(glyphInfo.uvTopRight);
uv.Add(glyphInfo.uvBottomRight);
uv.Add(glyphInfo.uvBottomLeft);
}
if (vertices.Count == 0) {

9
Runtime/ui/txt/layout.cs


this._positions.Clear();
this._count = count;
var font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
font.RequestCharactersInTexture(this._text.Substring(start, count),
font.RequestCharactersInTextureSafe(this._text.Substring(start, count),
style.UnityFontSize,
style.UnityFontStyle);

int charIndex = start + i;
var ch = text[charIndex];
CharacterInfo characterInfo = font.getCharacterInfo(ch, style.UnityFontSize, style.UnityFontStyle);
var glyphInfo = font.getGlyphInfo(ch, style.UnityFontSize, style.UnityFontStyle);
var rect = Rect.fromLTRB(characterInfo.minX, -characterInfo.maxY, characterInfo.maxX,
-characterInfo.minY);
var rect = glyphInfo.rect;
rect = rect.translate(this._advance, 0);
if (this._bounds == null || this._bounds.isEmpty) {
this._bounds = rect;

}
this._positions.Add(this._advance);
float advance = characterInfo.advance;
float advance = glyphInfo.advance;
if (ch == '\t') {
advance = this._tabStops.nextTab((this._advance + offset)) - this._advance;
}

11
Runtime/ui/txt/linebreaker.cs


using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

}
if (this._tabWidth == int.MaxValue) {
this._font.RequestCharactersInTexture(" ", this._fontSize);
CharacterInfo characterInfo = this._font.getCharacterInfo(' ', this._fontSize, UnityEngine.FontStyle.Normal);
this._tabWidth = characterInfo.advance * kTabSpaceCount;
this._font.RequestCharactersInTextureSafe(" ", this._fontSize);
if (this._fontSize > 0) {
var glyphInfo = this._font.getGlyphInfo(' ', this._fontSize, UnityEngine.FontStyle.Normal);
this._tabWidth = (int)Math.Round(glyphInfo.advance * kTabSpaceCount);
}
}
if (this._tabWidth == 0) {

25
Runtime/ui/txt/paragraph.cs


public static FontMetrics fromFont(Font font, int fontSize) {
var ascent = -font.ascent * fontSize / font.fontSize;
var descent = (font.lineHeight - font.ascent) * fontSize / font.fontSize;
float? fxHeight = null;
font.RequestCharactersInTexture("x", fontSize);
CharacterInfo charInfo;
if (font.GetCharacterInfo('x', out charInfo, fontSize)) {
fxHeight = charInfo.glyphHeight;
}
font.RequestCharactersInTextureSafe("x", fontSize, UnityEngine.FontStyle.Normal);
var glyphInfo = font.getGlyphInfo('x', fontSize, UnityEngine.FontStyle.Normal);
float fxHeight = glyphInfo.glyphHeight;
return new FontMetrics(ascent, descent, fxHeight: fxHeight);
}

}
public void paint(Canvas canvas, Offset offset) {
foreach (var paintRecord in this._paintRecords) {
this.paintBackground(canvas, paintRecord, offset);
}
foreach (var paintRecord in this._paintRecords) {
var paint = new Paint {
filterMode = FilterMode.Bilinear,

yOffset = yOffsetOriginal;
}
}
}
void paintBackground(Canvas canvas, PaintRecord record, Offset baseOffset) {
if (record.style.background == null) {
return;
}
var metrics = record.metrics;
Rect rect = Rect.fromLTRB(0, metrics.ascent, record.runWidth, metrics.descent);
rect = rect.shift(baseOffset + record.offset);
canvas.drawRect(rect, record.style.background);
}
float getLineXOffset(float lineTotalAdvance) {

43
Samples/UIWidgetSample/txt/TextStyleSample.cs


using System.Collections.Generic;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
namespace UIWidgetsSample {
public class TextStyleSample : UIWidgetsSamplePanel {
protected override Widget createWidget() {
return new MaterialApp(
title: "Text Style",
home: new TextStyleSampleWidget()
);
}
}
class TextStyleSampleWidget : StatelessWidget {
public override Widget build(BuildContext context) {
var fontStyleTexts = new List<Widget> {
new Text("text", style: new TextStyle(fontSize: 18)),
new Text("text with font size 0 below", style: new TextStyle(fontSize: 14)),
new Text("font size 0", style: new TextStyle(fontSize: 0)),
new Text("text with font size 0 above", style: new TextStyle(fontSize: 14)),
new Text("text with font size 0.3f", style: new TextStyle(fontSize: 0.3f)),
new Text("Text with background", style: new TextStyle(fontSize: 14, background:
new Paint(){color = new Color(0xFF00FF00)})),
};
return new Scaffold(
appBar: new AppBar(
title: new Text("Text Style")
),
body: new Card(
child: new DefaultTextStyle(
style: new TextStyle(fontSize: 40, fontFamily: "Roboto"),
child: new ListView(children: fontStyleTexts))
)
);
}
}
}

11
Samples/UIWidgetSample/txt/TextStyleSample.cs.meta


fileFormatVersion: 2
guid: 1de691b215926469a959e54492262ff4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存