Fix AddDalamudDefaultFont on not using lodestone symbol fonts file

This commit is contained in:
Soreepeong 2023-11-29 14:13:11 +09:00 committed by Soreepeong
parent f8e6df1172
commit 701d006db8
4 changed files with 46 additions and 20 deletions

View file

@ -120,7 +120,9 @@ public interface IFontAtlasBuildToolkitPreBuild : IFontAtlasBuildToolkit
/// <summary> /// <summary>
/// Adds the default font known to the current font atlas.<br /> /// Adds the default font known to the current font atlas.<br />
/// <br /> /// <br />
/// Default font includes <see cref="AddFontAwesomeIconFont"/> and <see cref="AddExtraGlyphsForDalamudLanguage"/>. /// Includes <see cref="AddFontAwesomeIconFont"/> and <see cref="AddExtraGlyphsForDalamudLanguage"/>.<br />
/// As this involves adding multiple fonts, calling this function will set <see cref="IFontAtlasBuildToolkit.Font"/>
/// as the return value of this function, if it was empty before.
/// </summary> /// </summary>
/// <param name="sizePx">Font size in pixels.</param> /// <param name="sizePx">Font size in pixels.</param>
/// <param name="glyphRanges">The glyph ranges. Use <see cref="FontAtlasBuildToolkitUtilities"/>.ToGlyphRange to build.</param> /// <param name="glyphRanges">The glyph ranges. Use <see cref="FontAtlasBuildToolkitUtilities"/>.ToGlyphRange to build.</param>
@ -132,7 +134,8 @@ public interface IFontAtlasBuildToolkitPreBuild : IFontAtlasBuildToolkit
/// <br /> /// <br />
/// Note: if game symbols font file is requested but is unavailable, /// Note: if game symbols font file is requested but is unavailable,
/// then it will take the glyphs from game's built-in fonts, and everything in <paramref name="fontConfig"/> /// then it will take the glyphs from game's built-in fonts, and everything in <paramref name="fontConfig"/>
/// will be ignored but <see cref="SafeFontConfig.SizePx"/> and <see cref="SafeFontConfig.MergeFont"/>. /// will be ignored but <see cref="SafeFontConfig.SizePx"/>, <see cref="SafeFontConfig.MergeFont"/>,
/// and <see cref="SafeFontConfig.GlyphRanges"/>.
/// </summary> /// </summary>
/// <param name="asset">The font type.</param> /// <param name="asset">The font type.</param>
/// <param name="fontConfig">The font config.</param> /// <param name="fontConfig">The font config.</param>

View file

@ -211,7 +211,8 @@ internal class DelegateFontHandle : IFontHandle.IInternal
{ {
Log.Warning( Log.Warning(
"[{name}:Substance] {n} fonts added from {delegate} PreBuild call; " + "[{name}:Substance] {n} fonts added from {delegate} PreBuild call; " +
"did you mean to use {sfd}.{sfdprop} or {ifcp}.{ifcpprop}?", "Using the most recently added font. " +
"Did you mean to use {sfd}.{sfdprop} or {ifcp}.{ifcpprop}?",
this.Manager.Name, this.Manager.Name,
fontsVector.Length - fontCountPrevious, fontsVector.Length - fontCountPrevious,
nameof(FontAtlasBuildStepDelegate), nameof(FontAtlasBuildStepDelegate),
@ -262,7 +263,7 @@ internal class DelegateFontHandle : IFontHandle.IInternal
{ {
var distinct = var distinct =
fontsVector fontsVector
.DistinctBy(x => (nint)x.NativePtr) // Remove duplicates .DistinctBy(x => (nint)x.NativePtr) // Remove duplicates
.Where(x => x.ValidateUnsafe() is null) // Remove invalid entries without freeing them .Where(x => x.ValidateUnsafe() is null) // Remove invalid entries without freeing them
.ToArray(); .ToArray();

View file

@ -291,6 +291,8 @@ internal sealed partial class FontAtlasFactory
var font = this.AddDalamudAssetFont(DalamudAsset.NotoSansJpMedium, fontConfig); var font = this.AddDalamudAssetFont(DalamudAsset.NotoSansJpMedium, fontConfig);
this.AddExtraGlyphsForDalamudLanguage(fontConfig with { MergeFont = font }); this.AddExtraGlyphsForDalamudLanguage(fontConfig with { MergeFont = font });
this.AddGameSymbol(fontConfig with { MergeFont = font }); this.AddGameSymbol(fontConfig with { MergeFont = font });
if (this.Font.IsNull())
this.Font = font;
return font; return font;
} }
@ -316,7 +318,8 @@ internal sealed partial class FontAtlasFactory
return this.gameFontHandleSubstance.AttachGameSymbols( return this.gameFontHandleSubstance.AttachGameSymbols(
this, this,
fontConfig.MergeFont, fontConfig.MergeFont,
fontConfig.SizePx); fontConfig.SizePx,
fontConfig.GlyphRanges);
default: default:
return this.factory.AddFont( return this.factory.AddFont(

View file

@ -214,9 +214,7 @@ internal class GamePrebakedFontHandle : IFontHandle.IInternal
// Owned by this class, but ImFontPtr values still do not belong to this. // Owned by this class, but ImFontPtr values still do not belong to this.
private readonly Dictionary<GameFontStyle, ImFontPtr> fonts = new(); private readonly Dictionary<GameFontStyle, ImFontPtr> fonts = new();
private readonly Dictionary<GameFontStyle, Exception?> buildExceptions = new(); private readonly Dictionary<GameFontStyle, Exception?> buildExceptions = new();
private readonly Dictionary<ImFontPtr, List<(ImFontPtr Font, ushort[]? Ranges)>> fontCopyTargets = new();
private readonly Dictionary<GameFontStyle, ImFontPtr> fontsSymbolsOnly = new();
private readonly Dictionary<ImFontPtr, HashSet<ImFontPtr>> symbolsCopyTargets = new();
private readonly HashSet<ImFontPtr> templatedFonts = new(); private readonly HashSet<ImFontPtr> templatedFonts = new();
private readonly Dictionary<ImFontPtr, List<(char From, char To)>> lateBuildRanges = new(); private readonly Dictionary<ImFontPtr, List<(char From, char To)>> lateBuildRanges = new();
@ -250,23 +248,24 @@ internal class GamePrebakedFontHandle : IFontHandle.IInternal
/// <param name="toolkitPreBuild">The toolkitPostBuild.</param> /// <param name="toolkitPreBuild">The toolkitPostBuild.</param>
/// <param name="font">The font to attach to.</param> /// <param name="font">The font to attach to.</param>
/// <param name="sizePx">The font size in pixels.</param> /// <param name="sizePx">The font size in pixels.</param>
/// <param name="glyphRanges">The intended glyph ranges.</param>
/// <returns><paramref name="font"/> if it is not empty; otherwise a new font.</returns> /// <returns><paramref name="font"/> if it is not empty; otherwise a new font.</returns>
public ImFontPtr AttachGameSymbols(IFontAtlasBuildToolkitPreBuild toolkitPreBuild, ImFontPtr font, float sizePx) public ImFontPtr AttachGameSymbols(
IFontAtlasBuildToolkitPreBuild toolkitPreBuild,
ImFontPtr font,
float sizePx,
ushort[]? glyphRanges)
{ {
var style = new GameFontStyle(GameFontFamily.Axis, sizePx); var style = new GameFontStyle(GameFontFamily.Axis, sizePx);
if (!this.fontsSymbolsOnly.TryGetValue(style, out var symbolFont)) var referenceFont = this.GetOrCreateFont(style, toolkitPreBuild);
{
symbolFont = this.CreateFontPrivate(style, toolkitPreBuild, ' ', '\uFFFE', true);
this.fontsSymbolsOnly.Add(style, symbolFont);
}
if (font.IsNull()) if (font.IsNull())
font = this.CreateTemplateFont(style, toolkitPreBuild); font = this.CreateTemplateFont(style, toolkitPreBuild);
if (!this.symbolsCopyTargets.TryGetValue(symbolFont, out var set)) if (!this.fontCopyTargets.TryGetValue(referenceFont, out var copyTargets))
this.symbolsCopyTargets[symbolFont] = set = new(); this.fontCopyTargets[referenceFont] = copyTargets = new();
set.Add(font); copyTargets.Add((font, glyphRanges));
return font; return font;
} }
@ -342,7 +341,7 @@ internal class GamePrebakedFontHandle : IFontHandle.IInternal
for (var i = 0; i < pixels8Array.Length; i++) for (var i = 0; i < pixels8Array.Length; i++)
toolkitPostBuild.NewImAtlas.GetTexDataAsAlpha8(i, out pixels8Array[i], out widths[i], out heights[i]); toolkitPostBuild.NewImAtlas.GetTexDataAsAlpha8(i, out pixels8Array[i], out widths[i], out heights[i]);
foreach (var (style, font) in this.fonts.Concat(this.fontsSymbolsOnly)) foreach (var (style, font) in this.fonts)
{ {
try try
{ {
@ -585,10 +584,30 @@ internal class GamePrebakedFontHandle : IFontHandle.IInternal
} }
} }
foreach (var (source, targets) in this.symbolsCopyTargets) foreach (var (source, targets) in this.fontCopyTargets)
{ {
foreach (var target in targets) foreach (var target in targets)
ImGuiHelpers.CopyGlyphsAcrossFonts(source, target, true, true, SeIconCharMin, SeIconCharMax); {
if (target.Ranges is null)
{
ImGuiHelpers.CopyGlyphsAcrossFonts(source, target.Font, missingOnly: true);
}
else
{
for (var i = 0; i < target.Ranges.Length; i += 2)
{
if (target.Ranges[i] == 0)
break;
ImGuiHelpers.CopyGlyphsAcrossFonts(
source,
target.Font,
true,
true,
target.Ranges[i],
target.Ranges[i + 1]);
}
}
}
} }
} }