Dalamud/Dalamud/Interface/FontIdentifier/SingleFontSpec.cs
Haselnussbomber c93f04f0e4
Code cleanup (#2439)
* Use new Lock objects

* Fix CA1513: Use ObjectDisposedException.ThrowIf

* Fix CA1860: Avoid using 'Enumerable.Any()' extension method

* Fix IDE0028: Use collection initializers or expressions

* Fix CA2263: Prefer generic overload when type is known

* Fix CA1862: Use the 'StringComparison' method overloads to perform case-insensitive string comparisons

* Fix IDE0270: Null check can be simplified

* Fix IDE0280: Use 'nameof'

* Fix IDE0009: Add '.this'

* Fix IDE0007: Use 'var' instead of explicit type

* Fix IDE0062: Make local function static

* Fix CA1859: Use concrete types when possible for improved performance

* Fix IDE0066: Use switch expression

Only applied to where it doesn't look horrendous.

* Use is over switch

* Fix CA1847: Use String.Contains(char) instead of String.Contains(string) with single characters

* Fix SYSLIB1045: Use 'GeneratedRegexAttribute' to generate the regular expression implementation at compile-time.

* Fix CA1866: Use 'string.EndsWith(char)' instead of 'string.EndsWith(string)' when you have a string with a single char

* Fix IDE0057: Substring can be simplified

* Fix IDE0059: Remove unnecessary value assignment

* Fix CA1510: Use ArgumentNullException throw helper

* Fix IDE0300: Use collection expression for array

* Fix IDE0250: Struct can be made 'readonly'

* Fix IDE0018: Inline variable declaration

* Fix CA1850: Prefer static HashData method over ComputeHash

* Fi CA1872: Prefer 'Convert.ToHexString' and 'Convert.ToHexStringLower' over call chains based on 'BitConverter.ToString'

* Update ModuleLog instantiations

* Organize usings
2026-01-06 08:36:55 -08:00

153 lines
5 KiB
C#

using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Text;
using Dalamud.Bindings.ImGui;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Interface.Utility;
using Newtonsoft.Json;
namespace Dalamud.Interface.FontIdentifier;
/// <summary>
/// Represents a user's choice of a single font.
/// </summary>
[SuppressMessage(
"StyleCop.CSharp.OrderingRules",
"SA1206:Declaration keywords should follow order",
Justification = "public required")]
public record SingleFontSpec : IFontSpec
{
/// <summary>
/// Gets the font id.
/// </summary>
[JsonProperty]
public required IFontId FontId { get; init; }
/// <inheritdoc/>
[JsonProperty]
public float SizePx { get; init; } = 16;
/// <inheritdoc/>
[JsonIgnore]
public float SizePt
{
get => (this.SizePx * 3) / 4;
init => this.SizePx = (value * 4) / 3;
}
/// <inheritdoc/>
[JsonIgnore]
public float LineHeightPx => MathF.Round(this.SizePx * this.LineHeight);
/// <summary>
/// Gets the line height ratio to the font size.
/// </summary>
[JsonProperty]
public float LineHeight { get; init; } = 1f;
/// <summary>
/// Gets the glyph offset in pixels.
/// </summary>
[JsonProperty]
public Vector2 GlyphOffset { get; init; }
/// <summary>
/// Gets the letter spacing in pixels.
/// </summary>
[JsonProperty]
public float LetterSpacing { get; init; }
/// <summary>
/// Gets the glyph ranges.
/// </summary>
[JsonProperty]
public ushort[]? GlyphRanges { get; init; }
/// <inheritdoc/>
public string ToLocalizedString(string localeCode)
{
var sb = new StringBuilder();
sb.Append(this.FontId.Family.GetLocalizedName(localeCode));
sb.Append($"({this.FontId.GetLocalizedName(localeCode)}, {this.SizePt}pt");
if (Math.Abs(this.LineHeight - 1f) > 0.000001f)
sb.Append($", LH={this.LineHeight:0.##}");
if (this.GlyphOffset != default)
sb.Append($", O={this.GlyphOffset.X:0.##},{this.GlyphOffset.Y:0.##}");
if (this.LetterSpacing != 0f)
sb.Append($", LS={this.LetterSpacing:0.##}");
sb.Append(')');
return sb.ToString();
}
/// <inheritdoc/>
public override string ToString() => this.ToLocalizedString("en");
/// <inheritdoc/>
public IFontHandle CreateFontHandle(IFontAtlas atlas, FontAtlasBuildStepDelegate? callback = null) =>
atlas.NewDelegateFontHandle(tk =>
{
tk.OnPreBuild(e => e.Font = this.AddToBuildToolkit(e));
callback?.Invoke(tk);
});
/// <inheritdoc/>
public ImFontPtr AddToBuildToolkit(IFontAtlasBuildToolkitPreBuild tk, ImFontPtr mergeFont = default)
{
var font = this.FontId.AddToBuildToolkit(
tk,
new()
{
SizePx = this.SizePx,
GlyphRanges = this.GlyphRanges,
MergeFont = mergeFont,
});
tk.RegisterPostBuild(
() =>
{
// Multiplication by scale will be done with global scale, outside of this handling.
var scale = tk.GetFontScaleMode(font) == FontScaleMode.UndoGlobalScale ? 1 / tk.Scale : 1;
var roundUnit = tk.GetFontScaleMode(font) == FontScaleMode.SkipHandling ? 1 : 1 / tk.Scale;
var newAscent = MathF.Round((font.Ascent * this.LineHeight) / roundUnit) * roundUnit;
var newFontSize = MathF.Round((font.FontSize * this.LineHeight) / roundUnit) * roundUnit;
var shiftDown = MathF.Round((newFontSize - font.FontSize) / 2f / roundUnit) * roundUnit;
font.Ascent = newAscent;
font.FontSize = newFontSize;
font.Descent = newFontSize - font.Ascent;
var lookup = new BitArray(ushort.MaxValue + 1, this.GlyphRanges is null);
if (this.GlyphRanges is not null)
{
for (var i = 0; i < this.GlyphRanges.Length && this.GlyphRanges[i] != 0; i += 2)
{
var to = (int)this.GlyphRanges[i + 1];
for (var j = this.GlyphRanges[i]; j <= to; j++)
lookup[j] = true;
}
}
var dax = MathF.Round((this.LetterSpacing * scale) / roundUnit) * roundUnit;
var dxy0 = this.GlyphOffset * scale;
dxy0 /= roundUnit;
dxy0 = new(MathF.Round(dxy0.X), MathF.Round(dxy0.Y));
dxy0 *= roundUnit;
dxy0.Y += shiftDown;
var dxy = new Vector4(dxy0, dxy0.X, dxy0.Y);
foreach (ref var glyphReal in font.GlyphsWrapped().DataSpan)
{
if (!lookup[glyphReal.Codepoint])
continue;
glyphReal.XY += dxy;
glyphReal.AdvanceX += dax;
}
});
return font;
}
}