Make it possible to use AXIS fonts with exactly specified size (#797)

This commit is contained in:
kizer 2022-04-16 02:52:18 +09:00 committed by GitHub
parent 3f73e4f478
commit 8310a695a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 37 deletions

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using Dalamud.Configuration.Internal;
@ -229,7 +230,10 @@ namespace Dalamud.Interface.GameFonts
}
if (needRebuild)
{
Log.Information("[GameFontManager] Calling RebuildFonts because {0} has been requested.", style.ToString());
this.interfaceManager.RebuildFonts();
}
return new(this, style);
}
@ -287,7 +291,8 @@ namespace Dalamud.Interface.GameFonts
/// <summary>
/// Build fonts before plugins do something more. To be called from InterfaceManager.
/// </summary>
public void BuildFonts()
/// <param name="forceMinSize">Whether to load fonts in minimum sizes.</param>
public void BuildFonts(bool forceMinSize)
{
unsafe
{
@ -305,7 +310,7 @@ namespace Dalamud.Interface.GameFonts
{
var rectIds = this.glyphRectIds[style] = new();
var fdt = this.fdts[(int)style.FamilyAndSize];
var fdt = this.fdts[(int)(forceMinSize ? style.FamilyWithMinimumSize : style.FamilyAndSize)];
if (fdt == null)
continue;
@ -318,7 +323,7 @@ namespace Dalamud.Interface.GameFonts
if (c < 32 || c >= 0xFFFF)
continue;
var widthAdjustment = style.CalculateWidthAdjustment(fdt, glyph);
var widthAdjustment = style.CalculateBaseWidthAdjustment(fdt, glyph);
rectIds[c] = Tuple.Create(
io.Fonts.AddCustomRectFontGlyph(
font,
@ -338,7 +343,8 @@ namespace Dalamud.Interface.GameFonts
/// <summary>
/// Post-build fonts before plugins do something more. To be called from InterfaceManager.
/// </summary>
public unsafe void AfterBuildFonts()
/// <param name="forceMinSize">Whether to load fonts in minimum sizes.</param>
public unsafe void AfterBuildFonts(bool forceMinSize)
{
var ioFonts = ImGui.GetIO().Fonts;
ioFonts.GetTexDataAsRGBA32(out byte* pixels8, out var width, out var height);
@ -347,7 +353,8 @@ namespace Dalamud.Interface.GameFonts
foreach (var (style, font) in this.fonts)
{
var fdt = this.fdts[(int)style.FamilyAndSize];
var fdt = this.fdts[(int)(forceMinSize ? style.FamilyWithMinimumSize : style.FamilyAndSize)];
var scale = style.SizePt / fdt.FontHeader.Size;
var fontPtr = font.NativePtr;
fontPtr->ConfigData->SizePixels = fontPtr->FontSize = fdt.FontHeader.Size * 4 / 3;
fontPtr->Ascent = fdt.FontHeader.Ascent;
@ -363,19 +370,15 @@ namespace Dalamud.Interface.GameFonts
}
}
fixed (char* c = FontNames[(int)style.FamilyAndSize])
{
for (var j = 0; j < 40; j++)
fontPtr->ConfigData->Name[j] = 0;
Encoding.UTF8.GetBytes(c, FontNames[(int)style.FamilyAndSize].Length, fontPtr->ConfigData->Name, 40);
}
var nameBytes = Encoding.UTF8.GetBytes(style.ToString() + "\0");
Marshal.Copy(nameBytes, 0, (IntPtr)font.ConfigData.Name.Data, Math.Min(nameBytes.Length, font.ConfigData.Name.Count));
foreach (var (c, (rectId, glyph)) in this.glyphRectIds[style])
{
var rc = ioFonts.GetCustomRectByIndex(rectId);
var sourceBuffer = this.texturePixels[glyph.TextureFileIndex];
var sourceBufferDelta = glyph.TextureChannelByteIndex;
var widthAdjustment = style.CalculateWidthAdjustment(fdt, glyph);
var widthAdjustment = style.CalculateBaseWidthAdjustment(fdt, glyph);
if (widthAdjustment == 0)
{
for (var y = 0; y < glyph.BoundingHeight; y++)
@ -401,10 +404,10 @@ namespace Dalamud.Interface.GameFonts
for (var y = 0; y < glyph.BoundingHeight; y++)
{
float xDelta = xbold;
if (style.SkewStrength > 0)
xDelta += style.SkewStrength * (fdt.FontHeader.LineHeight - glyph.CurrentOffsetY - y) / fdt.FontHeader.LineHeight;
else if (style.SkewStrength < 0)
xDelta -= style.SkewStrength * (glyph.CurrentOffsetY + y) / fdt.FontHeader.LineHeight;
if (style.BaseSkewStrength > 0)
xDelta += style.BaseSkewStrength * (fdt.FontHeader.LineHeight - glyph.CurrentOffsetY - y) / fdt.FontHeader.LineHeight;
else if (style.BaseSkewStrength < 0)
xDelta -= style.BaseSkewStrength * (glyph.CurrentOffsetY + y) / fdt.FontHeader.LineHeight;
var xDeltaInt = (int)Math.Floor(xDelta);
var xness = xDelta - xDeltaInt;
for (var x = 0; x < glyph.BoundingWidth; x++)
@ -433,11 +436,9 @@ namespace Dalamud.Interface.GameFonts
}
}
}
}
foreach (var font in this.fonts.Values)
{
CopyGlyphsAcrossFonts(InterfaceManager.DefaultFont, font, true, false);
UnscaleFont(font, 1 / scale, false);
font.BuildLookupTable();
}
}