using System.Diagnostics;
using System.Globalization;
namespace System.Unicode
{
/// Provides information on a specific unicode code point.
[DebuggerDisplay(@"{CodePoint.ToString(""X4""),nq} - {Name,nq}")]
public struct UnicodeCharInfo
{
/// The code point.
private readonly int codePoint;
/// The name of the code point.
private readonly string name;
internal readonly int unicodeCharacterDataIndex;
private readonly int unihanCharacterDataIndex;
private readonly string block;
private ref UnicodeCharacterData UnicodeCharacterData => ref UnicodeInfo.GetUnicodeCharacterData(unicodeCharacterDataIndex);
private ref UnihanCharacterData UnihanCharacterData => ref UnicodeInfo.GetUnihanCharacterData(unihanCharacterDataIndex);
/// Gets the code point as an UTF-32 value.
public int CodePoint => codePoint;
/// Gets the code point name.
/// This is the Name Unicode property.
[ValueName("Name"), ValueName("na")]
public string Name => name;
/// Gets the name aliases defined for the code point.
/// This is the Name_Alias Unicode property.
[ValueName("Name_Alias")]
public UnicodeNameAliasCollection NameAliases => new UnicodeNameAliasCollection(UnicodeCharacterData.NameAliases);
/// Gets the category defined for the code point.
/// This is the General_Category Unicode property.
[ValueName("General_Category"), ValueName("gc")]
public UnicodeCategory Category => UnicodeCharacterData.Category;
/// Gets the name of the block where the code point is located.
/// This is the Block Unicode property.
[ValueName("Block"), ValueName("blk")]
public string Block => block ?? UnicodeInfo.DefaultBlock;
/// Gets the canonical combining class defined for the code point.
/// This is the Canonical_Combining_Class Unicode property.
[ValueName("Canonical_Combining_Class"), ValueName("ccc")]
public CanonicalCombiningClass CanonicalCombiningClass => UnicodeCharacterData.CanonicalCombiningClass;
/// Gets the bidirectional class defined for the code point.
/// This is the Bidi_Class Unicode property.
[ValueName("Bidi_Class"), ValueName("bc")]
public BidirectionalClass BidirectionalClass => UnicodeCharacterData.BidirectionalClass;
/// Gets the decomposition type defined for the code point.
/// This is the Decomposition_Type Unicode property.
[ValueName("Decomposition_Type"), ValueName("dt")]
public CompatibilityFormattingTag DecompositionType => UnicodeCharacterData.DecompositionType;
/// Gets the decomposition mapping defined for the code point.
/// This is the Decomposition_Mapping Unicode property.
[ValueName("Decomposition_Mapping"), ValueName("dm")]
public string DecompositionMapping => UnicodeCharacterData.DecompositionMapping;
/// Gets the numeric type defined for the code point.
///
/// This is the Numeric_Type Unicode property.
/// When this value is defined to something other than , the indicates the numeric value of the code point.
/// The value of this property may be influenced by Unihan data, which will set it to .
/// In this case, the property will indicate the origin of the numeric value in Unihan data.
///
[ValueName("Numeric_Type"), ValueName("nt")]
public UnicodeNumericType NumericType => unihanCharacterDataIndex >= 0 ? UnihanCharacterData.NumericType != UnihanNumericType.None ? UnicodeNumericType.Numeric : UnicodeNumericType.None : UnicodeCharacterData.NumericType;
/// Gets the Unihan numeric type defined for the code point.
/// The value of this property indicates which of the kPrimaryNumeric, kAccountingNumeric, or kOtherNumeric Unihan property is set, if any.
public UnihanNumericType UnihanNumericType => unihanCharacterDataIndex >= 0 ? UnihanCharacterData.NumericType : UnihanNumericType.None;
/// Gets the numeric value defined for the code point.
///
/// This is the Numeric_Value Unicode property.
/// This value may come from Unihan data.
/// When this property set, the and indicates the nature of the numeric value.
///
[ValueName("Numeric_Value"), ValueName("nv")]
public UnicodeRationalNumber? NumericValue
{
get
{
if (unihanCharacterDataIndex >= 0)
{
ref var unihanCharacterData = ref UnihanCharacterData;
if (unihanCharacterData.NumericType != UnihanNumericType.None)
{
return new UnicodeRationalNumber(unihanCharacterData.NumericValue, 1);
}
}
return UnicodeCharacterData.NumericValue;
}
}
/// Gets a value indicating whether the character is mirrored in bidirectional text.
/// This is the Bidi_Mirrored Unicode property.
[ValueName("Bidi_Mirrored")]
public bool BidirectionalMirrored => UnicodeCharacterData.BidirectionalMirrored;
/// Gets the Unicode 1 name of the code point.
/// This is the Unicode_1_Name Unicode property.
[ValueName("Unicode_1_Name"), ValueName("na1")]
public string OldName => UnicodeCharacterData.OldName;
/// Gets the simple uppercase mapping defined for the code point.
/// This is the Simple_Uppercase_Mapping Unicode property.
[ValueName("Simple_Uppercase_Mapping"), ValueName("suc")]
public string SimpleUpperCaseMapping => UnicodeCharacterData.SimpleUpperCaseMapping;
/// Gets the simple lowercase mapping defined for the code point.
/// This is the Simple_Lowercase_Mapping Unicode property.
[ValueName("Simple_Lowercase_Mapping"), ValueName("slc")]
public string SimpleLowerCaseMapping => UnicodeCharacterData.SimpleLowerCaseMapping;
/// Gets the simple titlecase mapping defined for the code point.
/// This is the Simple_Titlecase_Mapping Unicode property.
[ValueName("Simple_Titlecase_Mapping"), ValueName("stc")]
public string SimpleTitleCaseMapping => UnicodeCharacterData.SimpleTitleCaseMapping;
/// Gets a value indicating which of the boolean contributory properties are defined for the code point.
///
/// The Unicode standard indicates contributory properties as neither normative nor informational.
/// However, contributory properties are used by Unicode to define the code properties.
/// The corresponding core properties may be accessed from the member.
///
public ContributoryProperties ContributoryProperties => UnicodeCharacterData.ContributoryProperties;
/// Gets a value indicating which of the boolean core properties are defined for the code point.
/// The core properties are computed by combining various character information together with contributory properties.
public CoreProperties CoreProperties => UnicodeCharacterData.CoreProperties;
/// Gets a value indicating which of the boolean emoji properties are defined for the code point.
/// The emoji properties are provided by the Unicode Emoji standard, which is not directly part of UCD.
public EmojiProperties EmojiProperties => UnicodeCharacterData.EmojiProperties;
/// Gets a collection of cross references associated with the code point.
/// The cross references have been extracted from Unicode data but are not normative.
public UnicodeCrossReferenceCollection CrossRerefences => new UnicodeCrossReferenceCollection(UnicodeCharacterData.CrossRerefences);
/// Gets the radical and stroke count for the code point.
///
/// This is the Unicode_Radical_Stroke Unicode property, defined as kRSUnicode in Unihan data.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kRSUnicode"), ValueName("cjkRSUnicode"), ValueName("Unicode_Radical_Stroke"), ValueName("URS")]
public UnicodeRadicalStrokeCountCollection UnicodeRadicalStrokeCounts => new UnicodeRadicalStrokeCountCollection(UnihanCharacterData.UnicodeRadicalStrokeCounts);
/// Gets the definition of the character from the Unihan data.
///
/// This is the kDefinition Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kDefinition")]
public string Definition => UnihanCharacterData.Definition;
/// Gets the Mandarin reading of the character from the Unihan data.
///
/// This is the kMandarin Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kMandarin")]
public string MandarinReading => UnihanCharacterData.MandarinReading;
/// Gets the Cantonese reading of the character from the Unihan data.
///
/// This is the kCantonese Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kCantonese")]
public string CantoneseReading => UnihanCharacterData.CantoneseReading;
/// Gets the Japanese Kun reading of the character from the Unihan data.
///
/// This is the kJapaneseKun Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kJapaneseKun")]
public string JapaneseKunReading => UnihanCharacterData.JapaneseKunReading;
/// Gets the Japanese On reading of the character from the Unihan data.
///
/// This is the kJapaneseOn Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kJapaneseOn")]
public string JapaneseOnReading => UnihanCharacterData.JapaneseOnReading;
/// Gets the Korean reading of the character from the Unihan data.
///
/// This is the kKorean Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kKorean")]
public string KoreanReading => UnihanCharacterData.KoreanReading;
/// Gets the Hangul reading of the character from the Unihan data.
///
/// This is the kHangul Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kHangul")]
public string HangulReading => UnihanCharacterData.HangulReading;
/// Gets the Vietnamese reading of the character from the Unihan data.
///
/// This is the kVietnamese Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kVietnamese")]
public string VietnameseReading => UnihanCharacterData.VietnameseReading;
/// Gets the simplified variant of the character from the Unihan data.
///
/// This is the kSimplifiedVariant Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kSimplifiedVariant")]
public string SimplifiedVariant => UnihanCharacterData.SimplifiedVariant;
/// Gets the traditional variant of the character from the Unihan data.
///
/// This is the kTraditionalVariant Unicode property.
/// This property is only ever useful when the character is a CJK ideograph.
///
[ValueName("kTraditionalVariant")]
public string TraditionalVariant => UnihanCharacterData.TraditionalVariant;
internal UnicodeCharInfo(int codePoint, int unicodeCharacterDataIndex, int unihanCharacterDataIndex, string block)
{
this.codePoint = codePoint;
this.name = unicodeCharacterDataIndex >= 0 ? UnicodeInfo.GetName(codePoint, ref UnicodeInfo.GetUnicodeCharacterData(unicodeCharacterDataIndex)) : null;
this.unicodeCharacterDataIndex = unicodeCharacterDataIndex;
this.unihanCharacterDataIndex = unihanCharacterDataIndex;
this.block = block;
}
}
}