Skip to content

Commit

Permalink
Implemented parsing of almost all richtext tags recognized by Unity U…
Browse files Browse the repository at this point in the history
…I and TextMeshPro. (#36)

Richtext values are placed in TextConfiguration. Many of them in a stack (as done by TextMeshPro), which enables enable nesting. It is up to the layout engine to use these metrics correctly when generating the glyphs, lines, and pages.  Adopting the layout engine of TextMeshPro would be several thousand lines of code and is not done in this pull request.
  • Loading branch information
Fribur authored Apr 2, 2024
1 parent 82e4e51 commit 5329927
Show file tree
Hide file tree
Showing 46 changed files with 2,533 additions and 1,029 deletions.
56 changes: 24 additions & 32 deletions Calligraphics/Authoring/TextRendererAuthoring.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using Latios.Authoring;
using Latios.Calligraphics.Rendering.Authoring;
using Latios.Kinemation.Authoring;
using System;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Entities.Graphics;
Expand All @@ -18,40 +18,28 @@ namespace Latios.Calligraphics.Authoring
[AddComponentMenu("Latios/Calligraphics/Text Renderer")]
public class TextRendererAuthoring : MonoBehaviour
{
[Multiline]
[TextArea(5, 10)]
public string text;

public float fontSize = 12f;
public bool wordWrap = true;
public float maxLineWidth = float.MaxValue;
public HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left;
public VerticalAlignment verticalAlignment = VerticalAlignment.Top;
public float fontSize = 12f;
public bool wordWrap = true;
public float maxLineWidth = float.MaxValue;
public HorizontalAlignmentOptions horizontalAlignment = HorizontalAlignmentOptions.Left;
public VerticalAlignmentOptions verticalAlignment = VerticalAlignmentOptions.Top;
public bool isOrthographic;
public FontStyles fontStyle;
public FontWeight fontWeight;

public Color32 color = UnityEngine.Color.white;

public List<FontMaterialPair> fontsAndMaterials;

public enum HorizontalAlignment : byte
{
Left = 0x0,
Right = 0x1,
Center = 0x2,
Justified = 0x3
}

public enum VerticalAlignment : byte
{
Top = 0x0,
Middle = 0x1 << 2,
Bottom = 0x2 << 2,
}
}

[Serializable]
public struct FontMaterialPair
{
public FontAsset font;
public Material overrideMaterial;
public Material overrideMaterial;

public Material material => overrideMaterial == null ? font.material : overrideMaterial;
}
Expand Down Expand Up @@ -84,10 +72,14 @@ public override void Bake(TextRendererAuthoring authoring)
calliString.Append(authoring.text);
AddComponent(entity, new TextBaseConfiguration
{
fontSize = authoring.fontSize,
color = authoring.color,
fontSize = authoring.fontSize,
color = authoring.color,
maxLineWidth = math.select(float.MaxValue, authoring.maxLineWidth, authoring.wordWrap),
alignMode = (AlignMode)(((byte)authoring.horizontalAlignment) | ((byte)authoring.verticalAlignment))
lineJustification = authoring.horizontalAlignment,
verticalAlignment = authoring.verticalAlignment,
isOrthographic = authoring.isOrthographic,
fontStyle = authoring.fontStyle,
fontWeight = authoring.fontWeight,
});
}

Expand All @@ -100,16 +92,16 @@ void AddFontRendering(Entity entity, FontMaterialPair fontMaterialPair)
var layer = GetLayer();
this.BakeTextBackendMeshAndMaterial(new MeshRendererBakeSettings
{
targetEntity = entity,
targetEntity = entity,
renderMeshDescription = new RenderMeshDescription
{
FilterSettings = new RenderFilterSettings
{
Layer = layer,
Layer = layer,
RenderingLayerMask = (uint)(1 << layer),
ShadowCastingMode = ShadowCastingMode.Off,
ReceiveShadows = false,
MotionMode = MotionVectorGenerationMode.Object,
ShadowCastingMode = ShadowCastingMode.Off,
ReceiveShadows = false,
MotionMode = MotionVectorGenerationMode.Object,
StaticShadowCaster = false,
},
LightProbeUsage = LightProbeUsage.Off,
Expand Down
31 changes: 31 additions & 0 deletions Calligraphics/Components/FixedStack64Bytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Unity.Collections;

namespace Latios.Calligraphics
{

public struct FixedStack64Bytes<T> where T : unmanaged
{
FixedList64Bytes<T> m_buffer;
public bool IsEmpty => m_buffer.IsEmpty;
public void Add(in T item) => m_buffer.Add(in item);
public T Pop()
{
var result = m_buffer[m_buffer.Length - 1];
m_buffer.RemoveAt(m_buffer.Length - 1);
return result;
}
public T Peek()
{
var result = m_buffer[m_buffer.Length - 1];
return result;
}
/// <summary> Function to pop stack, and return the new resulting last item. Will not pop root.</summary>
public T RemoveExceptRoot()
{
if (m_buffer.Length > 1)
Pop();
return Peek();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions Calligraphics/Components/FontBlob.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine.TextCore.LowLevel;

namespace Latios.Calligraphics
{
//TODO: Underlay, Bold, Smallcaps
public struct FontBlob
{
public BlobArray<GlyphBlob> characters;
public float ascentLine;
public float descentLine;
public float lineHeight;
public float pointSize;
public float scale;
public float ascentLine;
public float descentLine;
public float lineHeight;
public float pointSize;
public float scale;

public float baseLine;
public float atlasWidth;
Expand All @@ -21,24 +22,32 @@ public struct FontBlob
public float regularStyleWeight;
public float boldStyleSpacing;
public float boldStyleWeight;
public float italicsStyleSlant;
public byte italicsStyleSlant;

public float capLine;

public float subscriptOffset;
public float subscriptSize;
public float superscriptOffset;
public float superscriptSize;

//public float underlineOffset;

/// <summary>
/// Padding that is read from material properties
/// </summary>
public float materialPadding;

public float baseScale => 1f / pointSize * scale * .1f;

public const float smallCapsScaleMultiplier = .8f;
public const float smallCapsScaleMultiplier = .8f;
public const float orthographicScaleMultiplier = 10f;
}

public struct GlyphBlob
{
public uint unicode;
public uint glyphIndex;
public uint unicode;
public BlobArray<float2> vertices;
public BlobArray<float2> uv;
public BlobArray<float2> uv2;
Expand Down Expand Up @@ -74,17 +83,26 @@ public struct GlyphBlob

public struct AdjustmentPair
{
public FontFeatureLookupFlags fontFeatureLookupFlags;
public GlyphAdjustment firstAdjustment;
public GlyphAdjustment secondAdjustment;
}

public struct GlyphAdjustment
{
public uint glyphUnicode;
public uint glyphUnicode;
public float xPlacement;
public float yPlacement;
public float xAdvance;
public float yAdvance;
public static GlyphAdjustment operator +(GlyphAdjustment a, GlyphAdjustment b)
=> new GlyphAdjustment
{
xPlacement = a.xPlacement + b.xPlacement,
yPlacement = a.yPlacement + b.yPlacement,
xAdvance = a.xAdvance + b.xAdvance,
yAdvance = a.yAdvance + b.yAdvance,
};
}
}

Expand Down
20 changes: 13 additions & 7 deletions Calligraphics/Components/TestRendererAspect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;

namespace Latios.Calligraphics
{
Expand All @@ -9,7 +7,7 @@ namespace Latios.Calligraphics
/// </summary>
public readonly partial struct TextRendererAspect : IAspect
{
readonly DynamicBuffer<CalliByte> m_string;
readonly DynamicBuffer<CalliByte> m_string;
readonly RefRW<TextBaseConfiguration> m_baseConfig;

/// <summary>
Expand Down Expand Up @@ -45,12 +43,20 @@ public UnityEngine.Color32 color
}

/// <summary>
/// The horizontal and vertical alignment modes of the text
/// The horizontal alignment modes of the text
/// </summary>
public AlignMode alignMode
public HorizontalAlignmentOptions lineJustification
{
get => m_baseConfig.ValueRO.alignMode;
set => m_baseConfig.ValueRW.alignMode = value;
get => m_baseConfig.ValueRO.lineJustification;
set => m_baseConfig.ValueRW.lineJustification = value;
}
/// <summary>
/// The vertical alignment modes of the text
/// </summary>
public VerticalAlignmentOptions verticalAlignment
{
get => m_baseConfig.ValueRO.verticalAlignment;
set => m_baseConfig.ValueRW.verticalAlignment = value;
}
}
}
Expand Down
39 changes: 23 additions & 16 deletions Calligraphics/Components/TextComponents.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.TextCore.Text;

namespace Latios.Calligraphics
{
Expand Down Expand Up @@ -34,9 +34,16 @@ public struct TextBaseConfiguration : IComponentData
/// </summary>
public Color32 color;
/// <summary>
/// The horizontal and vertical alignment modes of the text
/// The horizontal alignment modes of the text
/// </summary>
public AlignMode alignMode;
public HorizontalAlignmentOptions lineJustification;
/// <summary>
/// The horizontal alignment modes of the text
/// </summary>
public VerticalAlignmentOptions verticalAlignment;
public bool isOrthographic;
public FontStyles fontStyle;
public FontWeight fontWeight;
}

/// <summary>
Expand Down Expand Up @@ -102,21 +109,21 @@ public enum WriteMask : byte
}

/// <summary>
/// The packed alignment modes for both horizontal and vertical directions.
/// Horizontal text alignment options.
/// </summary>
public enum HorizontalAlignmentOptions
{
Left = 0x1, Center = 0x2, Right = 0x4, Justified = 0x8, Flush = 0x10, Geometry = 0x20
}

/// <summary>
/// Vertical text alignment options.
/// </summary>
public enum AlignMode : byte
public enum VerticalAlignmentOptions
{
Left = 0x0,
Right = 0x1,
Center = 0x2,
Justified = 0x3,
//
Top = 0x0,
Middle = 0x1 << 2,
Bottom = 0x2 << 2,
//
HorizontalMask = 0x3,
VerticalMask = 0xc,
Top = 0x100, Middle = 0x200, Bottom = 0x400, Baseline = 0x800, Geometry = 0x1000, Capline = 0x2000,
}

public enum FontWeight { Thin = 100, ExtraLight = 200, Light = 300, Regular = 400, Medium = 500, SemiBold = 600, Bold = 700, Heavy = 800, Black = 900 };
}

Loading

0 comments on commit 5329927

Please sign in to comment.