Skip to content

Commit

Permalink
afs.core: fix #10, and add some tests about AssFont.IsMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
MIRIMIRIM committed May 8, 2024
1 parent c86347b commit 465d873
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AssFontSubset.Core/src/AssFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static bool IsMatch(AssFontInfo afi, FontInfo fi)
}
else if (afi.Weight == 1)
{
boldMatch = !fi.MaybeHasTrueBoldOrItalic || fi.Bold;
boldMatch = fi.Bold || (!fi.MaybeHasTrueBoldOrItalic && !fi.Bold && !fi.Italic);
}
else if (afi.Weight == fi.Weight)
{
Expand All @@ -48,7 +48,7 @@ public static bool IsMatch(AssFontInfo afi, FontInfo fi)
{
italicMatch = true;
}
else if (afi.Italic == true && !fi.MaybeHasTrueBoldOrItalic)
else if (afi.Italic == true && (!fi.MaybeHasTrueBoldOrItalic && !fi.Bold && !fi.Italic))
{
italicMatch = true;
}
Expand Down
27 changes: 27 additions & 0 deletions AssFontSubset.CoreTests/AssFontSubset.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AssFontSubset.Core\AssFontSubset.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
72 changes: 72 additions & 0 deletions AssFontSubset.CoreTests/src/AssFontTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Mobsub.SubtitleParse.AssTypes;
using static AssFontSubset.Core.AssFont;

namespace AssFontSubset.Core.Tests;

[TestClass]
public class AssFontTests
{
[TestMethod]
public void IsMatchTestTrueBIZ()
{
var fn = "Times New Roman";
var fnChs = "Times New Roman";

var afi = new AssFontInfo() { Name = fn, Weight = 0, Italic = false };
var afiB = new AssFontInfo() { Name = fn, Weight = 1, Italic = false };
var afiI = new AssFontInfo() { Name = fn, Weight = 0, Italic = true };
var afiZ = new AssFontInfo() { Name = fn, Weight = 1, Italic = true };
var afi4 = new AssFontInfo() { Name = fn, Weight = 400, Italic = false };

var fi = new FontInfo() { FamilyName = fn, FamilyNameChs = fnChs, Bold = false, Italic = false, MaybeHasTrueBoldOrItalic = true, Weight = 400 };
var fiB = new FontInfo() { FamilyName = fn, FamilyNameChs = fnChs, Bold = true, Italic = false, MaybeHasTrueBoldOrItalic = false, Weight = 700 };
var fiI = new FontInfo() { FamilyName = fn, FamilyNameChs = fnChs, Bold = false, Italic = true, MaybeHasTrueBoldOrItalic = false, Weight = 400 };
var fiZ = new FontInfo() { FamilyName = fn, FamilyNameChs = fnChs, Bold = true, Italic = true, MaybeHasTrueBoldOrItalic = false, Weight = 700 };

var afL = new List<AssFontInfo>() { afi, afiB, afiI, afiZ, afi4 };
var fiL = new List<FontInfo>() { fi, fiB, fiI, fiZ };
foreach (var a in afL)
{
foreach (var f in fiL)
{
if ((a == afi && f == fi)
|| (a == afiB && f == fiB)
|| (a == afiI && f == fiI)
|| (a == afiZ && f == fiZ)
|| (a == afi4 && f == fi)
)
{
Assert.IsTrue(IsMatch(a, f));
}
else
{
Assert.IsFalse(IsMatch(a, f));
}
}
}
}

[TestMethod]
public void IsMatchTestFakeBIZ()
{
var fn = "FZLanTingHei-R-GBK";
var fnChs = "方正兰亭黑_GBK";

var afi = new AssFontInfo() { Name = fn, Weight = 0, Italic = false };
var afiBF = new AssFontInfo() { Name = fn, Weight = 1, Italic = false };
var afiIF = new AssFontInfo() { Name = fn, Weight = 0, Italic = true };
var afiZF = new AssFontInfo() { Name = fn, Weight = 1, Italic = true };
var afiChs = new AssFontInfo() { Name = fnChs, Weight = 0, Italic = false };
var afiBFChs = new AssFontInfo() { Name = fnChs, Weight = 1, Italic = false };
var afiIFChs = new AssFontInfo() { Name = fnChs, Weight = 0, Italic = true };
var afiZFChs = new AssFontInfo() { Name = fnChs, Weight = 1, Italic = true };

var fi = new FontInfo() { FamilyName = fn, FamilyNameChs = fnChs, Bold = false, Italic = false, MaybeHasTrueBoldOrItalic = false, Weight = 400 };

var afL = new List<AssFontInfo>() { afi, afiBF, afiIF, afiZF, afiChs, afiBFChs, afiIFChs, afiZFChs };
foreach ( var af in afL )
{
Assert.IsTrue(IsMatch(af, fi));
}
}
}
6 changes: 6 additions & 0 deletions AssFontSubset.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssFontSubset.Core", "AssFo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssFontSubset.Avalonia", "AssFontSubset.Avalonia\AssFontSubset.Avalonia.csproj", "{228541CF-C1A6-48C5-8B60-95538E049C25}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AssFontSubset.Core.Tests", "AssFontSubset.CoreTests\AssFontSubset.Core.Tests.csproj", "{E003AA3B-7C95-4FA3-AB7D-529B71BBD85D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{228541CF-C1A6-48C5-8B60-95538E049C25}.Debug|Any CPU.Build.0 = Debug|Any CPU
{228541CF-C1A6-48C5-8B60-95538E049C25}.Release|Any CPU.ActiveCfg = Release|Any CPU
{228541CF-C1A6-48C5-8B60-95538E049C25}.Release|Any CPU.Build.0 = Release|Any CPU
{E003AA3B-7C95-4FA3-AB7D-529B71BBD85D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E003AA3B-7C95-4FA3-AB7D-529B71BBD85D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E003AA3B-7C95-4FA3-AB7D-529B71BBD85D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E003AA3B-7C95-4FA3-AB7D-529B71BBD85D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 465d873

Please sign in to comment.