Skip to content

Commit

Permalink
afs.core: correct log duplicate fonts when parse fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
MIRIMIRIM committed Aug 5, 2024
1 parent 2ab4b52 commit 5160aea
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions AssFontSubset.Core/src/SubsetByPyFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ await Task.Run(() =>
});
}

List<FontInfo> GetFontInfoFromFiles(string dir)
IEnumerable<IGrouping<string, FontInfo>> GetFontInfoFromFiles(string dir)
{
string[] supportFonts = [".ttf", ".otf", ".ttc", "otc"];
HashSet<string> HasTrueBoldOrItalicRecord = [];
Expand All @@ -63,7 +63,40 @@ List<FontInfo> GetFontInfoFromFiles(string dir)
_stopwatch.Stop();
_logger?.ZLogDebug($"Font file scanning completed, use {_stopwatch.ElapsedMilliseconds} ms");
_stopwatch.Reset();
return fontInfos;

if (TryCheckDuplicatFonts(fontInfos, out var fontInfoGroup))
{
throw new Exception($"Maybe have duplicate fonts in fonts directory");
}

return fontInfoGroup;
}

bool TryCheckDuplicatFonts(List<FontInfo> fontInfos, out IEnumerable<IGrouping<string, FontInfo>> fontInfoGroup)
{
var dupFonts = false;
fontInfoGroup = fontInfos.GroupBy(fontInfo => fontInfo.FamilyNames[FontConstant.LanguageIdEnUs]);
foreach (var group in fontInfoGroup)
{
if (group.Count() <= 1) continue;
var groupWithoutFileNames = group.GroupBy(fi => new
{
fi.Bold,
fi.Italic,
fi.Weight,
fi.Index,
fi.MaxpNumGlyphs,
fi.FamilyNames
}).ToList();

if (groupWithoutFileNames.Count > 1)
{
_logger?.ZLogError($"Duplicate fonts: {string.Join('、', groupWithoutFileNames.SelectMany(x => x.Select(fi=>fi.FileName)))}");
dupFonts = true;
}
}

return dupFonts;
}

Dictionary<AssFontInfo, List<Rune>> GetAssFontInfoFromFiles(FileInfo[] assFiles, string optDir, out Dictionary<string, AssData> assDataWithOutputName)
Expand Down Expand Up @@ -105,7 +138,7 @@ Dictionary<AssFontInfo, List<Rune>> GetAssFontInfoFromFiles(FileInfo[] assFiles,
return multiAssFonts;
}

Dictionary<string, List<SubsetFont>> GetSubsetFonts(List<FontInfo> fontInfos, Dictionary<AssFontInfo, List<Rune>> assFonts, out Dictionary<FontInfo, List<AssFontInfo>> fontMap)
Dictionary<string, List<SubsetFont>> GetSubsetFonts(IEnumerable<IGrouping<string, FontInfo>> fontInfos, Dictionary<AssFontInfo, List<Rune>> assFonts, out Dictionary<FontInfo, List<AssFontInfo>> fontMap)
{
_logger?.ZLogInformation($"Start generate subset font info");
_stopwatch.Start();
Expand All @@ -114,8 +147,8 @@ Dictionary<string, List<SubsetFont>> GetSubsetFonts(List<FontInfo> fontInfos, Di
fontMap = [];
List<AssFontInfo> matchedAssFontInfos = [];

var fiGroups = fontInfos.GroupBy(fontInfo => fontInfo.FamilyNames[FontConstant.LanguageIdEnUs]);
foreach (var fig in fiGroups)
// var fiGroups = fontInfos.GroupBy(fontInfo => fontInfo.FamilyNames[FontConstant.LanguageIdEnUs]);
foreach (var fig in fontInfos)
{
foreach (var afi in assFonts.Keys)
{
Expand Down

0 comments on commit 5160aea

Please sign in to comment.