67 lines
2.5 KiB
C#
67 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BlockOutReplica.Presentation
|
|
{
|
|
/// <summary>Actual shipped Museo SDF glyphs and Unicode mapping; not an imitation TTF.</summary>
|
|
public sealed class OriginalSdfFontData : ScriptableObject
|
|
{
|
|
public string Family, Style, SourceFile, SourceSha256;
|
|
public int SourceNameOffset, SourceAtlasPathId, PointSize;
|
|
public float FaceScale = 1, LineHeight, Ascent, Descent, CapHeight, Padding = 10;
|
|
public Texture2D Atlas;
|
|
public Material RenderingMaterial;
|
|
public Glyph[] Glyphs;
|
|
public Profile[] Profiles;
|
|
private Dictionary<int, Glyph> lookup;
|
|
|
|
[Serializable] public sealed class Glyph
|
|
{
|
|
public int Unicode, Index;
|
|
public Rect Rect;
|
|
public float Width, Height, BearingX, BearingY, Advance, Scale = 1;
|
|
}
|
|
[Serializable] public sealed class Profile
|
|
{
|
|
public string Name, SourceMaterial;
|
|
public Color Face = Color.white, Outline = Color.black, Underlay = Color.clear;
|
|
public float FaceDilate, OutlineWidth, UnderlayDilate, UnderlayOffsetX, UnderlayOffsetY;
|
|
public float ScaleRatioA = 1;
|
|
public float ScaleRatioC = 1;
|
|
public bool OutlineEnabled, UnderlayEnabled;
|
|
}
|
|
|
|
public bool TryGetGlyph(char character, out Glyph glyph)
|
|
{
|
|
if (lookup == null)
|
|
{
|
|
lookup = new Dictionary<int, Glyph>();
|
|
foreach (Glyph item in Glyphs) lookup[item.Unicode] = item;
|
|
}
|
|
return lookup.TryGetValue(character, out glyph);
|
|
}
|
|
|
|
public bool Supports(string text)
|
|
{
|
|
foreach (char ch in text) if (ch != '\n' && ch != '\r' && !TryGetGlyph(ch, out _)) return false;
|
|
return true;
|
|
}
|
|
public float Measure(string text, float size)
|
|
{
|
|
float current = 0, maximum = 0;
|
|
foreach (char ch in text)
|
|
{
|
|
if (ch == '\n') {maximum = Mathf.Max(maximum, current); current = 0; continue;}
|
|
if (TryGetGlyph(ch, out Glyph glyph)) current += glyph.Advance * glyph.Scale * size / PointSize;
|
|
}
|
|
return Mathf.Max(maximum, current);
|
|
}
|
|
public Profile GetProfile(string role)
|
|
{
|
|
foreach (Profile profile in Profiles) if (profile.Name == role) return profile;
|
|
return Profiles.Length == 0 ? null : Profiles[0];
|
|
}
|
|
}
|
|
}
|