Added Unihan data.
This commit is contained in:
@@ -10,7 +10,8 @@ namespace System.Unicode
|
||||
public struct UnicodeCharInfo
|
||||
{
|
||||
private readonly int codePoint;
|
||||
private readonly UnicodeCharacterData characterData;
|
||||
private readonly UnicodeCharacterData unicodeCharacterData;
|
||||
private readonly UnihanCharacterData unihanCharacterData;
|
||||
private readonly string block;
|
||||
|
||||
public int CodePoint { get { return codePoint; } }
|
||||
@@ -19,32 +20,46 @@ namespace System.Unicode
|
||||
{
|
||||
get
|
||||
{
|
||||
return characterData.Name == null || characterData.CodePointRange.IsSingleCodePoint ?
|
||||
characterData.Name :
|
||||
characterData.Name + "-" + codePoint.ToString("X4");
|
||||
return unicodeCharacterData.Name == null || unicodeCharacterData.CodePointRange.IsSingleCodePoint ?
|
||||
unicodeCharacterData.Name :
|
||||
unicodeCharacterData.Name + "-" + codePoint.ToString("X4");
|
||||
}
|
||||
}
|
||||
|
||||
public UnicodeCategory Category { get { return characterData.Category; } }
|
||||
public UnicodeCategory Category { get { return unicodeCharacterData.Category; } }
|
||||
public string Block { get { return block; } }
|
||||
public CanonicalCombiningClass CanonicalCombiningClass { get { return characterData.CanonicalCombiningClass; } }
|
||||
public BidirectionalClass BidirectionalClass { get { return characterData.BidirectionalClass; } }
|
||||
public CompatibilityFormattingTag DecompositionType { get { return characterData.DecompositionType; } }
|
||||
public string DecompositionMapping { get { return characterData.DecompositionMapping; } }
|
||||
public UnicodeNumericType NumericType { get { return characterData.NumericType; } }
|
||||
public UnicodeRationalNumber? NumericValue { get { return characterData.NumericValue; } }
|
||||
public bool BidirectionalMirrored { get { return characterData.BidirectionalMirrored; } }
|
||||
public string OldName { get { return characterData.OldName; } }
|
||||
public string SimpleUpperCaseMapping { get { return characterData.SimpleUpperCaseMapping; } }
|
||||
public string SimpleLowerCaseMapping { get { return characterData.SimpleLowerCaseMapping; } }
|
||||
public string SimpleTitleCaseMapping { get { return characterData.SimpleTitleCaseMapping; } }
|
||||
public ContributoryProperties ContributoryProperties { get { return characterData.ContributoryProperties; } }
|
||||
public CoreProperties CoreProperties { get { return characterData.CoreProperties; } }
|
||||
public CanonicalCombiningClass CanonicalCombiningClass { get { return unicodeCharacterData.CanonicalCombiningClass; } }
|
||||
public BidirectionalClass BidirectionalClass { get { return unicodeCharacterData.BidirectionalClass; } }
|
||||
public CompatibilityFormattingTag DecompositionType { get { return unicodeCharacterData.DecompositionType; } }
|
||||
public string DecompositionMapping { get { return unicodeCharacterData.DecompositionMapping; } }
|
||||
public UnicodeNumericType NumericType { get { return unihanCharacterData != null ? unihanCharacterData.NumericType != UnihanNumericType.None ? UnicodeNumericType.Numeric : UnicodeNumericType.None : unicodeCharacterData.NumericType; } }
|
||||
public UnihanNumericType UnihanNumericType { get { return unihanCharacterData != null ? unihanCharacterData.NumericType : UnihanNumericType.None; } }
|
||||
public UnicodeRationalNumber? NumericValue { get { return unihanCharacterData != null && unihanCharacterData.NumericType != UnihanNumericType.None ? new UnicodeRationalNumber(unihanCharacterData.NumericValue, 1) : unicodeCharacterData.NumericValue; } }
|
||||
public bool BidirectionalMirrored { get { return unicodeCharacterData.BidirectionalMirrored; } }
|
||||
public string OldName { get { return unicodeCharacterData.OldName; } }
|
||||
public string SimpleUpperCaseMapping { get { return unicodeCharacterData.SimpleUpperCaseMapping; } }
|
||||
public string SimpleLowerCaseMapping { get { return unicodeCharacterData.SimpleLowerCaseMapping; } }
|
||||
public string SimpleTitleCaseMapping { get { return unicodeCharacterData.SimpleTitleCaseMapping; } }
|
||||
public ContributoryProperties ContributoryProperties { get { return unicodeCharacterData.ContributoryProperties; } }
|
||||
public CoreProperties CoreProperties { get { return unicodeCharacterData.CoreProperties; } }
|
||||
|
||||
internal UnicodeCharInfo(int codePoint, UnicodeCharacterData characterData, string block)
|
||||
public string Definition { get { return unihanCharacterData?.Definition; } }
|
||||
public string MandarinReading { get { return unihanCharacterData?.MandarinReading; } }
|
||||
public string CantoneseReading { get { return unihanCharacterData?.CantoneseReading; } }
|
||||
public string JapaneseKunReading { get { return unihanCharacterData?.JapaneseKunReading; } }
|
||||
public string JapaneseOnReading { get { return unihanCharacterData?.JapaneseOnReading; } }
|
||||
public string KoreanReading { get { return unihanCharacterData?.KoreanReading; } }
|
||||
public string HangulReading { get { return unihanCharacterData?.HangulReading; } }
|
||||
public string VietnameseReading { get { return unihanCharacterData?.VietnameseReading; } }
|
||||
|
||||
public string SimplifiedVariant { get { return unihanCharacterData?.SimplifiedVariant; } }
|
||||
public string TraditionalVariant { get { return unihanCharacterData?.TraditionalVariant; } }
|
||||
|
||||
internal UnicodeCharInfo(int codePoint, UnicodeCharacterData unicodeCharacterData, UnihanCharacterData unihanCharacterData, string block)
|
||||
{
|
||||
this.codePoint = codePoint;
|
||||
this.characterData = characterData;
|
||||
this.unicodeCharacterData = unicodeCharacterData;
|
||||
this.unihanCharacterData = unihanCharacterData;
|
||||
this.block = block;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,10 +23,6 @@ namespace System.Unicode
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Version unicodeVersion;
|
||||
private readonly UnicodeCharacterData[] characterData;
|
||||
private readonly UnicodeBlock[] blockEntries;
|
||||
|
||||
public static UnicodeInfo FromStream(Stream stream)
|
||||
{
|
||||
using (var reader = new BinaryReader(stream, Encoding.UTF8))
|
||||
@@ -44,11 +40,11 @@ namespace System.Unicode
|
||||
|
||||
var unicodeVersion = new Version(reader.ReadUInt16(), reader.ReadByte());
|
||||
|
||||
var characterDataEntries = new UnicodeCharacterData[ReadCodePoint(reader)];
|
||||
var unicodeCharacterDataEntries = new UnicodeCharacterData[ReadCodePoint(reader)];
|
||||
|
||||
for (int i = 0; i < characterDataEntries.Length; ++i)
|
||||
for (int i = 0; i < unicodeCharacterDataEntries.Length; ++i)
|
||||
{
|
||||
characterDataEntries[i] = ReadCharacterDataEntry(reader);
|
||||
unicodeCharacterDataEntries[i] = ReadUnicodeCharacterDataEntry(reader);
|
||||
}
|
||||
|
||||
var blockEntries = new UnicodeBlock[reader.ReadByte()];
|
||||
@@ -56,13 +52,20 @@ namespace System.Unicode
|
||||
for (int i = 0; i < blockEntries.Length; ++i)
|
||||
{
|
||||
blockEntries[i] = ReadBlockEntry(reader);
|
||||
}
|
||||
}
|
||||
|
||||
return new UnicodeInfo(unicodeVersion, characterDataEntries, blockEntries);
|
||||
var unihanCharacterDataEntries = new UnihanCharacterData[ReadCodePoint(reader)];
|
||||
|
||||
for (int i = 0; i < unihanCharacterDataEntries.Length; ++i)
|
||||
{
|
||||
unihanCharacterDataEntries[i] = ReadUnihanCharacterDataEntry(reader);
|
||||
}
|
||||
|
||||
return new UnicodeInfo(unicodeVersion, unicodeCharacterDataEntries, unihanCharacterDataEntries, blockEntries);
|
||||
}
|
||||
}
|
||||
|
||||
private static UnicodeCharacterData ReadCharacterDataEntry(BinaryReader reader)
|
||||
private static UnicodeCharacterData ReadUnicodeCharacterDataEntry(BinaryReader reader)
|
||||
{
|
||||
var fields = (UcdFields)reader.ReadUInt16();
|
||||
|
||||
@@ -107,6 +110,46 @@ namespace System.Unicode
|
||||
);
|
||||
}
|
||||
|
||||
private static UnihanCharacterData ReadUnihanCharacterDataEntry(BinaryReader reader)
|
||||
{
|
||||
var fields = (UnihanFields)reader.ReadUInt16();
|
||||
|
||||
int codePoint = UnihanCharacterData.UnpackCodePoint(ReadCodePoint(reader));
|
||||
|
||||
var numericType = (UnihanNumericType)((int)(fields & UnihanFields.OtherNumeric));
|
||||
long numericValue = numericType != UnihanNumericType.None ?
|
||||
reader.ReadInt64() :
|
||||
0;
|
||||
|
||||
string definition = (fields & UnihanFields.Definition) != 0 ? reader.ReadString() : null;
|
||||
string mandarinReading = (fields & UnihanFields.MandarinReading) != 0 ? reader.ReadString() : null;
|
||||
string cantoneseReading = (fields & UnihanFields.CantoneseReading) != 0 ? reader.ReadString() : null;
|
||||
string japaneseKunReading = (fields & UnihanFields.JapaneseKunReading) != 0 ? reader.ReadString() : null;
|
||||
string japaneseOnReading = (fields & UnihanFields.JapaneseOnReading) != 0 ? reader.ReadString() : null;
|
||||
string koreanReading = (fields & UnihanFields.KoreanReading) != 0 ? reader.ReadString() : null;
|
||||
string hangulReading = (fields & UnihanFields.HangulReading) != 0 ? reader.ReadString() : null;
|
||||
string vietnameseReading = (fields & UnihanFields.VietnameseReading) != 0 ? reader.ReadString() : null;
|
||||
string simplifiedVariant = (fields & UnihanFields.SimplifiedVariant) != 0 ? reader.ReadString() : null;
|
||||
string traditionalVariant = (fields & UnihanFields.TraditionalVariant) != 0 ? reader.ReadString() : null;
|
||||
|
||||
return new UnihanCharacterData
|
||||
(
|
||||
codePoint,
|
||||
numericType,
|
||||
numericValue,
|
||||
definition,
|
||||
mandarinReading,
|
||||
cantoneseReading,
|
||||
japaneseKunReading,
|
||||
japaneseOnReading,
|
||||
koreanReading,
|
||||
hangulReading,
|
||||
vietnameseReading,
|
||||
simplifiedVariant,
|
||||
traditionalVariant
|
||||
);
|
||||
}
|
||||
|
||||
private static UnicodeBlock ReadBlockEntry(BinaryReader reader)
|
||||
{
|
||||
return new UnicodeBlock(new UnicodeCharacterRange(ReadCodePoint(reader), ReadCodePoint(reader)), reader.ReadString());
|
||||
@@ -140,27 +183,57 @@ namespace System.Unicode
|
||||
}
|
||||
}
|
||||
|
||||
internal UnicodeInfo(Version unicodeVersion, UnicodeCharacterData[] characterData, UnicodeBlock[] blockEntries)
|
||||
private readonly Version unicodeVersion;
|
||||
private readonly UnicodeCharacterData[] unicodeCharacterData;
|
||||
private readonly UnihanCharacterData[] unihanCharacterData;
|
||||
private readonly UnicodeBlock[] blockEntries;
|
||||
|
||||
internal UnicodeInfo(Version unicodeVersion, UnicodeCharacterData[] unicodeCharacterData, UnihanCharacterData[] unihanCharacterData, UnicodeBlock[] blockEntries)
|
||||
{
|
||||
this.unicodeVersion = unicodeVersion;
|
||||
this.characterData = characterData;
|
||||
this.unicodeCharacterData = unicodeCharacterData;
|
||||
this.unihanCharacterData = unihanCharacterData;
|
||||
this.blockEntries = blockEntries;
|
||||
}
|
||||
|
||||
public Version UnicodeVersion { get { return unicodeVersion; } }
|
||||
|
||||
private UnicodeCharacterData FindCodePoint(int codePoint)
|
||||
private UnicodeCharacterData FindUnicodeCodePoint(int codePoint)
|
||||
{
|
||||
int minIndex = 0;
|
||||
int maxIndex = characterData.Length - 1;
|
||||
int maxIndex = unicodeCharacterData.Length - 1;
|
||||
|
||||
do
|
||||
{
|
||||
int index = (minIndex + maxIndex) >> 1;
|
||||
|
||||
int Δ = characterData[index].CodePointRange.CompareCodePoint(codePoint);
|
||||
int Δ = unicodeCharacterData[index].CodePointRange.CompareCodePoint(codePoint);
|
||||
|
||||
if (Δ == 0) return characterData[index];
|
||||
if (Δ == 0) return unicodeCharacterData[index];
|
||||
else if (Δ < 0) maxIndex = index - 1;
|
||||
else minIndex = index + 1;
|
||||
} while (minIndex <= maxIndex);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private UnihanCharacterData FindUnihanCodePoint(int codePoint)
|
||||
{
|
||||
int minIndex;
|
||||
int maxIndex;
|
||||
|
||||
if (unihanCharacterData.Length == 0 || codePoint < unihanCharacterData[minIndex = 0].CodePoint || codePoint > unihanCharacterData[maxIndex = unicodeCharacterData.Length - 1].CodePoint)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
int index = (minIndex + maxIndex) >> 1;
|
||||
|
||||
int Δ = codePoint - unihanCharacterData[index].CodePoint;
|
||||
|
||||
if (Δ == 0) return unihanCharacterData[index];
|
||||
else if (Δ < 0) maxIndex = index - 1;
|
||||
else minIndex = index + 1;
|
||||
} while (minIndex <= maxIndex);
|
||||
@@ -196,7 +269,7 @@ namespace System.Unicode
|
||||
|
||||
public UnicodeCharInfo GetCharInfo(int codePoint)
|
||||
{
|
||||
return new UnicodeCharInfo(codePoint, FindCodePoint(codePoint), GetBlockName(codePoint));
|
||||
return new UnicodeCharInfo(codePoint, FindUnicodeCodePoint(codePoint), FindUnihanCodePoint(codePoint), GetBlockName(codePoint));
|
||||
}
|
||||
|
||||
public UnicodeBlock[] GetBlocks()
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace System.Unicode
|
||||
{
|
||||
public readonly int CodePoint;
|
||||
public readonly UnihanNumericType NumericType;
|
||||
public readonly ulong NumericValue;
|
||||
public readonly long NumericValue;
|
||||
public readonly string Definition;
|
||||
public readonly string MandarinReading;
|
||||
public readonly string CantoneseReading;
|
||||
@@ -22,6 +22,38 @@ namespace System.Unicode
|
||||
public readonly string SimplifiedVariant;
|
||||
public readonly string TraditionalVariant;
|
||||
|
||||
internal UnihanCharacterData
|
||||
(
|
||||
int codePoint,
|
||||
UnihanNumericType numericType,
|
||||
long numericValue,
|
||||
string definition,
|
||||
string mandarinReading,
|
||||
string cantoneseReading,
|
||||
string japaneseKunReading,
|
||||
string japaneseOnReading,
|
||||
string koreanReading,
|
||||
string hangulReading,
|
||||
string vietnameseReading,
|
||||
string simplifiedVariant,
|
||||
string traditionalVariant
|
||||
)
|
||||
{
|
||||
CodePoint = codePoint;
|
||||
NumericType = numericType;
|
||||
NumericValue = numericValue;
|
||||
Definition = definition;
|
||||
MandarinReading = mandarinReading;
|
||||
CantoneseReading = cantoneseReading;
|
||||
JapaneseKunReading = japaneseKunReading;
|
||||
JapaneseOnReading = japaneseOnReading;
|
||||
KoreanReading = koreanReading;
|
||||
HangulReading = hangulReading;
|
||||
VietnameseReading = vietnameseReading;
|
||||
SimplifiedVariant = simplifiedVariant;
|
||||
TraditionalVariant = traditionalVariant;
|
||||
}
|
||||
|
||||
internal static int PackCodePoint(int codePoint)
|
||||
{
|
||||
if (codePoint >= 0x3400)
|
||||
|
||||
@@ -10,9 +10,9 @@ namespace System.Unicode
|
||||
internal enum UnihanFields : ushort
|
||||
{
|
||||
// NumericType / NumericValue : Not exactly a bit mask here…
|
||||
AccountingNumeric = 1,
|
||||
OtherNumeric = 2,
|
||||
PrimaryNumeric = 3,
|
||||
PrimaryNumeric = 1,
|
||||
AccountingNumeric = 2,
|
||||
OtherNumeric = 3,
|
||||
|
||||
Definition = 4,
|
||||
MandarinReading = 8,
|
||||
|
||||
@@ -10,11 +10,11 @@ namespace System.Unicode
|
||||
public enum UnihanNumericType : byte
|
||||
{
|
||||
None = 0,
|
||||
[ValueName("kAccountingNumeric")]
|
||||
Accounting = 1,
|
||||
[ValueName("kOtherNumeric")]
|
||||
Other = 2,
|
||||
[ValueName("kPrimaryNumeric")]
|
||||
Primary = 3
|
||||
Primary = 1,
|
||||
[ValueName("kAccountingNumeric")]
|
||||
Accounting = 2,
|
||||
[ValueName("kOtherNumeric")]
|
||||
Other = 3,
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user