Files
RobustToolbox/Robust.Client/Graphics/Font.cs
T
SilverandGitHub 25926a17b7 Renames SS14.* to Robust.* (#793)
RobustToolbox projects should be named Robust.* 

This PR changes the RobustToolbox projects from SS14.* to Robust.*

Updates SS14.* prefixes/namespaces to Robust.*
Updates SpaceStation14.sln to RobustToolbox.sln
Updates MSBUILD/SS14.* to MSBUILD/Robust.*
Updates CSProject and MSBuild references for the above
Updates git_helper.py
Removes Runserver and Runclient as they are unusable
2019-04-15 20:24:59 -06:00

191 lines
6.4 KiB
C#

using System;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Interfaces.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
namespace Robust.Client.Graphics
{
/// <summary>
/// A generic font for rendering of text.
/// Does not contain properties such as size. Those are specific to children such as <see cref="VectorFont" />
/// </summary>
public abstract class Font
{
internal abstract Godot.Font GodotFont { get; }
/// <summary>
/// The maximum amount a glyph goes above the baseline.
/// </summary>
public virtual int Ascent => (int?)GodotFont?.GetAscent() ?? default;
/// <summary>
/// The maximum glyph height of a line of text, not relative to the baseline.
/// </summary>
public virtual int Height => (int?)GodotFont?.GetHeight() ?? default;
/// <summary>
/// The maximum amount a glyph drops below the baseline.
/// </summary>
public virtual int Descent => (int?)GodotFont?.GetDescent() ?? default;
/// <summary>
/// The distance between the baselines of two consecutive lines.
/// Basically, if you encounter a new line, this is how much you need to move down the cursor.
/// </summary>
public virtual int LineHeight => Height;
/// <summary>
/// The distance between the edges of two consecutive lines.
/// </summary>
public int LineSeparation => LineHeight - Height;
public static implicit operator Godot.Font(Font font)
{
return font?.GodotFont;
}
// Yes, I am aware that using char is bad.
// At the same time the font system is nowhere close to rendering Unicode so...
/// <summary>
/// Draw a character at a certain baseline position on screen.
/// </summary>
/// <param name="handle">The drawing handle to draw to.</param>
/// <param name="chr">
/// The Unicode code point to draw. Yes I'm aware about UTF-16 being crap,
/// do you think this system can draw anything except ASCII?
/// </param>
/// <param name="baseline">The baseline from which to draw the character.</param>
/// <param name="color">The color of the character to draw.</param>
/// <returns>How much to advance the cursor to draw the next character.</returns>
public abstract float DrawChar(DrawingHandleScreen handle, char chr, Vector2 baseline, Color color);
/// <summary>
/// Gets metrics describing the dimensions and positioning of a single glyph in the font.
/// </summary>
/// <param name="chr">The character to fetch the glyph metrics for.</param>
/// <returns>
/// <c>null</c> if this font does not have a glyph for the specified character,
/// otherwise the metrics you asked for.
/// </returns>
/// <seealso cref="TryGetCharMetrics"/>
public abstract CharMetrics? GetCharMetrics(char chr);
/// <summary>
/// Try-pattern version of <see cref="GetCharMetrics"/>.
/// </summary>
public bool TryGetCharMetrics(char chr, out CharMetrics metrics)
{
var maybe = GetCharMetrics(chr);
if (maybe.HasValue)
{
metrics = maybe.Value;
return true;
}
metrics = default;
return false;
}
}
/// <summary>
/// Font type that renders vector fonts such as OTF and TTF fonts from a <see cref="FontResource"/>
/// </summary>
public sealed class VectorFont : Font
{
public int Size { get; }
internal override Godot.Font GodotFont => _font;
private readonly Godot.DynamicFont _font;
internal IFontInstanceHandle Handle { get; }
public override int Ascent => Handle?.Ascent ?? base.Ascent;
public override int Descent => Handle?.Descent ?? base.Descent;
public override int Height => Handle?.Height ?? base.Height;
public override int LineHeight => Handle?.LineHeight ?? base.LineHeight;
public VectorFont(FontResource res, int size)
{
Size = size;
if (GameController.OnGodot)
{
_font = new Godot.DynamicFont
{
FontData = res.FontData,
Size = size,
};
}
else
{
Handle = IoCManager.Resolve<IFontManagerInternal>().MakeInstance(res.FontFaceHandle, size);
}
}
public override float DrawChar(DrawingHandleScreen handle, char chr, Vector2 baseline, Color color)
{
DebugTools.Assert(!GameController.OnGodot);
var metrics = Handle.GetCharMetrics(chr);
if (!metrics.HasValue)
{
return 0;
}
var texture = Handle.GetCharTexture(chr);
if (texture == null)
{
return metrics.Value.Advance;
}
baseline += new Vector2(metrics.Value.BearingX, -metrics.Value.BearingY);
handle.DrawTexture(texture, baseline, color);
return metrics.Value.Advance;
}
public override CharMetrics? GetCharMetrics(char chr)
{
return Handle.GetCharMetrics(chr);
}
}
internal sealed class GodotWrapFont : Font
{
public GodotWrapFont(Godot.Font godotFont)
{
GodotFont = godotFont;
}
internal override Godot.Font GodotFont { get; }
public override float DrawChar(DrawingHandleScreen handle, char chr, Vector2 baseline, Color color)
{
throw new NotImplementedException();
}
public override CharMetrics? GetCharMetrics(char chr)
{
throw new NotImplementedException();
}
}
public sealed class DummyFont : Font
{
internal override Godot.Font GodotFont => null;
public override float DrawChar(DrawingHandleScreen handle, char chr, Vector2 baseline, Color color)
{
// Nada, it's a dummy after all.
return 0;
}
public override CharMetrics? GetCharMetrics(char chr)
{
// Nada, it's a dummy after all.
return null;
}
}
}