// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; namespace Spludlow.Drawing { public class Unicode { private static string CacheName = "Spludlow.Drawing.UnicodeBlocks"; private static string UnicodeBlocksFilename = Spludlow.Config.ProgramData + @"\Data\UnicodeBlocks.txt"; public static DataTable BlocksTable() { DataTable table = (DataTable)Spludlow.Caching.Get(CacheName); if (table == null) { if (File.Exists(UnicodeBlocksFilename) == false) { table = CreateBlocksTable(); Spludlow.Data.TextTable.Write(UnicodeBlocksFilename, table); } else { table = Spludlow.Data.TextTable.ReadFile(UnicodeBlocksFilename); } Spludlow.Caching.Set(CacheName, table); } if (table.DataSet != null) table.DataSet.Tables.Remove(table); return table; } public static DataTable CreateBlocksTable() { string url = "http://www.unicode.org/Public/UCD/latest/ucd/Blocks.txt"; DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "StartCode EndCode BlockName HexRange", "Int32* Int32 String String", }); string text = Spludlow.Net.Http.GetText(url, Encoding.UTF8); using (StringReader reader = new StringReader(text)) { string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (line.Length == 0 || line.StartsWith("#") == true) continue; int index; index = line.IndexOf(";"); string range = line.Substring(0, index).Trim(); string name = line.Substring(index + 1).Trim(); string[] rangeParts = Spludlow.Text.Split(range, '.', true); int start = Int32.Parse(rangeParts[0], System.Globalization.NumberStyles.HexNumber); int end = Int32.Parse(rangeParts[1], System.Globalization.NumberStyles.HexNumber); table.Rows.Add(start, end, name, range); } } return table; } } }