/// <summary>Sets up the bidirectional data.</summary> public static void Setup() { if (Blocks != null) { return; } // Get the bidi data now: #if UNITY // Get the entity file: UnityEngine.TextAsset bidiData = (UnityEngine.Resources.Load("BidirectionalData") as UnityEngine.TextAsset); if (bidiData == null) { return; } byte[] file = bidiData.bytes; // Create a reader: BinaryIO.Reader reader = new BinaryIO.Reader(file); int count = (int)reader.ReadCompressed(); Blocks = new BidiBlock[count]; int index = 0; // Setup all blocks: while (reader.More()) { // Read the block: Blocks[index++] = new BidiBlock( (int)reader.ReadCompressed(), (int)reader.ReadCompressed(), reader.ReadByte() ); } #else #warning Bidirectional data not available #endif }
/// <summary>Finds the nearest block for the given charcode and returns its mode. /// The mode is one of the BidiBlock options.</summary> public static int Get(int charCode) { if (Blocks == null) { // Always LTR: return(BidiBlock.LeftToRight); } int min = 0; int max = Blocks.Length - 1; while (min <= max) { // Midpoint: int mid = (min + max) / 2; // Get that block: BidiBlock nearest = Blocks[mid]; // Check: if (charCode >= nearest.Start && charCode <= nearest.End) { return(nearest.Mode); } else if (charCode < nearest.Start) { max = mid - 1; } else { min = mid + 1; } } // LTR otherwise: return(BidiBlock.LeftToRight); }