Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up Unicode Data lookups and remove reflection. #281

Merged
merged 8 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions SixLabors.Fonts.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29512.175
# Visual Studio Version 17
VisualStudioVersion = 17.2.32519.379
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_root", "_root", "{C317F1B1-D75E-4C6D-83EB-80367343E0D7}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -74,10 +74,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnicodeTestData", "UnicodeT
tests\UnicodeTestData\README.md = tests\UnicodeTestData\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SixLabors.Fonts.Benchmarks", "tests\SixLabors.Fonts.Benchmarks\SixLabors.Fonts.Benchmarks\SixLabors.Fonts.Benchmarks.csproj", "{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{09e744ec-4852-4fc7-be78-c1b399f17967}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Expand All @@ -103,6 +102,10 @@ Global
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABB6E111-672F-4846-88D6-C49C6CD01606}.Release|Any CPU.Build.0 = Release|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -117,8 +120,12 @@ Global
{CFCC940C-DEA3-42CC-9626-0B7D09289FF4} = {7CDD4908-7CCD-4945-860C-D2F1732D3AE6}
{ABB6E111-672F-4846-88D6-C49C6CD01606} = {249327CF-1415-428B-8EEA-8C7705B1DE8F}
{654DD381-B93D-4459-B669-296F5D9172ED} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
{FB8FDC5F-7FEB-4132-9133-C25E05C0B3D9} = {56801022-D71A-4FBE-BC5B-CBA08E2284EC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {38F4B47F-4F74-40F5-8707-C0EF1D0BDF92}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems*{09e744ec-4852-4fc7-be78-c1b399f17967}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
13 changes: 0 additions & 13 deletions src/SixLabors.Fonts/SixLabors.Fonts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@
<AssemblyVersion Condition="'$(IsContinuousIntegration)'==''">1.0.0.0</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<None Remove="Unicode\Resources\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Unicode\Resources\**" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\shared-infrastructure\branding\icons\fonts\sixlabors.fonts.128.png" Pack="true" PackagePath="" />
</ItemGroup>
Expand All @@ -43,10 +35,5 @@
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<Resource Include="Unicode\Resources\Grapheme.trie" />
<Resource Include="Unicode\Resources\UnicodeCategory.trie" />
</ItemGroup>

<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />
</Project>
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/Unicode/BidiClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public readonly struct BidiClass
/// </summary>
/// <param name="codePoint">The codepoint.</param>
public BidiClass(CodePoint codePoint)
=> this.bidiValue = UnicodeData.GetBidiData(codePoint.Value);
=> this.bidiValue = UnicodeData.GetBidiData((uint)codePoint.Value);

/// <summary>
/// Gets the Unicode Bidirectional character type.
Expand Down
38 changes: 17 additions & 21 deletions src/SixLabors.Fonts/Unicode/CodePoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,18 @@ public static bool IsTabulation(CodePoint codePoint)
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns><see langword="true"/> if <paramref name="codePoint"/> is a new line indicator; otherwise, <see langword="false"/></returns>
public static bool IsNewLine(CodePoint codePoint)
{
// See https://www.unicode.org/standard/reports/tr13/tr13-5.html
switch (codePoint.Value)
{
case 0x000A: // LINE FEED (LF)
case 0x000B: // LINE TABULATION (VT)
case 0x000C: // FORM FEED (FF)
case 0x000D: // CARRIAGE RETURN (CR)
case 0x0085: // NEXT LINE (NEL)
case 0x2028: // LINE SEPARATOR (LS)
case 0x2029: // PARAGRAPH SEPARATOR (PS)
return true;
default:
return false;
}
}
=> codePoint.Value switch
{
// See https://www.unicode.org/standard/reports/tr13/tr13-5.html
0x000A // LINE FEED (LF)
or 0x000B // LINE TABULATION (VT)
or 0x000C // FORM FEED (FF)
or 0x000D // CARRIAGE RETURN (CR)
or 0x0085 // NEXT LINE (NEL)
or 0x2028 // LINE SEPARATOR (LS)
or 0x2029 => true, // PARAGRAPH SEPARATOR (PS)
_ => false,
};

/// <summary>
/// Returns the number of codepoints in a given string buffer.
Expand Down Expand Up @@ -518,7 +514,7 @@ public static BidiClass GetBidiClass(CodePoint codePoint)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetBidiMirror(CodePoint codePoint, out CodePoint mirror)
{
uint value = UnicodeData.GetBidiMirror(codePoint.Value);
uint value = UnicodeData.GetBidiMirror(codePoint.value);

if (value == 0u)
{
Expand Down Expand Up @@ -561,15 +557,15 @@ public static bool TryGetVerticalMirror(CodePoint codePoint, out CodePoint mirro
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns>The <see cref="LineBreakClass"/>.</returns>
public static LineBreakClass GetLineBreakClass(CodePoint codePoint)
=> UnicodeData.GetLineBreakClass(codePoint.Value);
=> UnicodeData.GetLineBreakClass(codePoint.value);

/// <summary>
/// Gets the <see cref="GraphemeClusterClass"/> for the given codepoint.
/// </summary>
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns>The <see cref="GraphemeClusterClass"/>.</returns>
public static GraphemeClusterClass GetGraphemeClusterClass(CodePoint codePoint)
=> UnicodeData.GetGraphemeClusterClass(codePoint.Value);
=> UnicodeData.GetGraphemeClusterClass(codePoint.value);

/// <summary>
/// Gets the <see cref="JoiningClass"/> for the given codepoint.
Expand All @@ -585,7 +581,7 @@ internal static JoiningClass GetJoiningClass(CodePoint codePoint)
/// <param name="codePoint">The codepoint to evaluate.</param>
/// <returns>The <see cref="ScriptClass"/>.</returns>
internal static ScriptClass GetScriptClass(CodePoint codePoint)
=> UnicodeData.GetScriptClass(codePoint.Value);
=> UnicodeData.GetScriptClass(codePoint.value);

/// <summary>
/// Gets the <see cref="UnicodeCategory"/> for the given codepoint.
Expand All @@ -599,7 +595,7 @@ public static UnicodeCategory GetGeneralCategory(CodePoint codePoint)
return (UnicodeCategory)(AsciiCharInfo[codePoint.Value] & UnicodeCategoryMask);
}

return UnicodeData.GetUnicodeCategory(codePoint.Value);
return UnicodeData.GetUnicodeCategory(codePoint.value);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/Unicode/JoiningClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public readonly struct JoiningClass
public JoiningClass(CodePoint codePoint)
{
UnicodeCategory category = CodePoint.GetGeneralCategory(codePoint);
uint value = UnicodeData.GetJoiningClass(codePoint.Value);
uint value = UnicodeData.GetJoiningClass((uint)codePoint.Value);
this.JoiningType = GetJoiningType(codePoint, value, category);
this.JoiningGroup = (JoiningGroup)((value >> 16) & 0xFF);
}
Expand Down
Binary file not shown.
Loading