JD2022-TU1/main/tools/services/JD.MapFileFontReader.Service/UnicodeBlock.cs

138 lines
No EOL
4.7 KiB
C#

using System;
using System.Text.RegularExpressions;
namespace JD.MapFileFontReader.Service
{
public enum UnicodeBlock
{
[UnicodeRegexMatch(@"\p{IsCJKUnifiedIdeographs}")]
CJKUnifiedIdeographs,
[UnicodeRegexMatch(@"\p{IsCJKUnifiedIdeographsExtensionA}")]
CJKUnifiedIdeographsExtensionsA,
[UnicodeRegexMatch(@"\p{IsCJKCompatibilityIdeographs}")]
CJKCompatibilityIdeographs,
[UnicodeRegexMatch(@"\p{IsCJKCompatibilityForms}")]
CJKCompatibilityForms,
[UnicodeRegexMatch(@"\p{IsCJKRadicalsSupplement}")]
CJKRadicalsSupplement,
[UnicodeRegexMatch(@"\p{IsCJKSymbolsandPunctuation}")]
CJKSymbolsandPunctuation,
[UnicodeRegexMatch(@"\p{IsKatakana}")]
Katakana,
[UnicodeRegexMatch(@"\p{IsHiragana}")]
Hiragana,
[UnicodeRegexMatch(@"\p{IsKanbun}")]
Kanbun,
[UnicodeRegexMatch(@"\p{IsHangulSyllables}")]
HangulSyllables,
[UnicodeRegexMatch(@"\p{IsHangulJamo}")]
HangulJamo,
[UnicodeRegexMatch(@"\p{IsHangulCompatibilityJamo}")]
HangulCompatibilityJamo,
[UnicodeRegexMatch(@"\p{IsBasicLatin}")]
BasicLatin,
[UnicodeRegexMatch(@"\p{IsLatin-1Supplement}")]
Latin1Supplement,
[UnicodeRegexMatch(@"\p{IsLatinExtended-A}")]
LatinExtendedA,
[UnicodeRegexMatch(@"\p{IsLatinExtended-B}")]
LatinExtendedB,
[UnicodeRegexMatch(@"\p{IsLatinExtendedAdditional}")]
LatinExtendedAdditional,
[UnicodeRegexMatch(@"\p{IsGreek}")]
IsGreek,
[UnicodeRegexMatch(@"\p{IsSpacingModifierLetters}")]
SpacingModifierLetters,
[UnicodeRegexMatch(@"\p{IsCombiningDiacriticalMarks}")]
CombiningDiacriticalMarks,
[UnicodeRegexMatch(@"\p{IsGeneralPunctuation}")]
GeneralPunctuation,
[UnicodeRegexMatch(@"\p{IsCurrencySymbols}")]
CurrencySymbols,
[UnicodeRegexMatch(@"\p{IsLetterlikeSymbols}")]
LetterlikeSymbols,
[UnicodeRegexMatch(@"\p{IsNumberForms}")]
NumberForms,
[UnicodeRegexMatch(@"\p{IsMathematicalOperators}")]
MathematicalOperators,
[UnicodeRegexMatch(@"\p{IsGeometricShapes}")]
GeometricShapes,
[UnicodeRegexMatch(@"\p{IsHalfwidthandFullwidthForms}")]
HalfwidthandFullwidthForms,
[UnicodeRegexMatch(@"\p{IsSpecials}")]
Specials,
[UnicodeRegexMatch(@"\p{IsCyrillic}")]
Cyrillic,
[UnicodeRegexMatch(@"\p{IsCyrillicSupplement}")]
CyrillicSupplement,
[UnicodeRegexMatch(@"\p{IsPrivateUse}")]
PrivateUse,
[SpecialCharacterMatch]
UbisoftSpecial
}
/// <summary>
/// Base attribute to indicate if a unicode block match a specific character
/// </summary>
public abstract class CharacterMatchAttribute : Attribute
{
public abstract bool IsMatch(char character);
}
/// <summary>
/// Indicate if a unicode block match a specific character, based on a regex
/// </summary>
public class UnicodeRegexMatch : CharacterMatchAttribute
{
private readonly Regex _regex;
public UnicodeRegexMatch(string regex)
{
_regex = new Regex(regex);
}
/// <inheritdoc />
public override bool IsMatch(char character)
{
return _regex.IsMatch(character.ToString());
}
}
/// <summary>
/// Specific match for Special characters, specific to Ubisoft
/// </summary>
public class SpecialCharacterMatch : CharacterMatchAttribute
{
// These characters are used for consoles logos, and correspond to ⅜, ⅝, and ⅞
// The special characters need to be added here, otherwise they won't be handled
public override bool IsMatch(char character)
{
return character == 0x215c || character == 0x215d || character == 0x215e;
}
}
/// <summary>
/// Extensions to work with unicode blocks
/// </summary>
public static class UnicodeBlockExtensions
{
public static bool IsMatch(this UnicodeBlock block, char c)
{
var attribute = GetAttributeOfType<CharacterMatchAttribute>(block);
if (attribute != null)
{
return attribute.IsMatch(c);
}
return false;
}
private static T GetAttributeOfType<T>(this Enum enumVal) where T : System.Attribute
{
var type = enumVal.GetType();
var memInfo = type.GetMember(enumVal.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
return (attributes.Length > 0) ? (T)attributes[0] : null;
}
}
}