151 lines
No EOL
5 KiB
C#
151 lines
No EOL
5 KiB
C#
using JD.MapFileFontReader.Service.Configuration;
|
|
using Newtonsoft.Json;
|
|
using Plume.Interfaces.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace JD.MapFileFontReader.Service
|
|
{
|
|
public class MapFileFontReaderService : IMapFileFontReaderService
|
|
{
|
|
private FontReaderConfiguration _configuration;
|
|
private readonly ICsvDataReader _reader;
|
|
|
|
public MapFileFontReaderService(ICsvDataReader reader)
|
|
{
|
|
_reader = reader;
|
|
}
|
|
|
|
public MapFileFontReaderService()
|
|
// Use EPPlus as default data reader
|
|
:this(new EPPlusCsvDataReader())
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Returns all characters from a list of files
|
|
/// </summary>
|
|
/// <param name="files"></param>
|
|
/// <returns></returns>
|
|
public HashSet<char> GetAllCharacters(IEnumerable<string> files)
|
|
{
|
|
var characters = new HashSet<char>();
|
|
foreach (var file in files)
|
|
{
|
|
var fileCharacters = GetAllCharacters(file).Distinct();
|
|
characters.UnionWith(fileCharacters);
|
|
}
|
|
return characters;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all characters from a file
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
/// <returns></returns>
|
|
private IEnumerable<char> GetAllCharacters(string filePath)
|
|
{
|
|
if (Path.GetExtension(filePath) == _configuration.WorksheetFileExtension)
|
|
{
|
|
var csv = _reader.FromExcelFile(filePath, _configuration.FontWorksheetName);
|
|
|
|
foreach (var row in csv.Rows)
|
|
{
|
|
foreach (var character in row[_configuration.FontColumnName])
|
|
{
|
|
yield return character;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var characters = File.ReadAllText(filePath).ToCharArray();
|
|
foreach (var character in characters)
|
|
{
|
|
yield return character;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all unicode blocks for asian languages
|
|
/// </summary>
|
|
public UnicodeBlock[] GetAsianUnicodeBlocks()
|
|
{
|
|
return new[]
|
|
{
|
|
UnicodeBlock.CJKUnifiedIdeographs,
|
|
UnicodeBlock.CJKUnifiedIdeographsExtensionsA,
|
|
UnicodeBlock.CJKCompatibilityIdeographs,
|
|
UnicodeBlock.CJKCompatibilityForms,
|
|
UnicodeBlock.CJKRadicalsSupplement,
|
|
UnicodeBlock.CJKSymbolsandPunctuation,
|
|
UnicodeBlock.Katakana,
|
|
UnicodeBlock.Hiragana,
|
|
UnicodeBlock.Kanbun,
|
|
UnicodeBlock.HangulSyllables,
|
|
UnicodeBlock.HangulJamo,
|
|
UnicodeBlock.HangulCompatibilityJamo,
|
|
UnicodeBlock.CombiningDiacriticalMarks,
|
|
UnicodeBlock.PrivateUse,
|
|
UnicodeBlock.HalfwidthandFullwidthForms,
|
|
UnicodeBlock.LatinExtendedAdditional,
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all unicode blocks for european languages
|
|
/// </summary>
|
|
public UnicodeBlock[] GetEuropeanUnicodeBlocks()
|
|
{
|
|
return new[]
|
|
{
|
|
UnicodeBlock.BasicLatin,
|
|
UnicodeBlock.Latin1Supplement,
|
|
UnicodeBlock.LatinExtendedA,
|
|
UnicodeBlock.LatinExtendedB,
|
|
UnicodeBlock.SpacingModifierLetters,
|
|
UnicodeBlock.CombiningDiacriticalMarks,
|
|
UnicodeBlock.GeneralPunctuation,
|
|
UnicodeBlock.CurrencySymbols,
|
|
UnicodeBlock.Cyrillic,
|
|
UnicodeBlock.CyrillicSupplement,
|
|
UnicodeBlock.LetterlikeSymbols,
|
|
UnicodeBlock.NumberForms,
|
|
UnicodeBlock.MathematicalOperators,
|
|
UnicodeBlock.GeometricShapes,
|
|
UnicodeBlock.Specials,
|
|
UnicodeBlock.IsGreek,
|
|
};
|
|
}
|
|
|
|
public bool IsAsianLanguage(string fileName)
|
|
{
|
|
return _configuration.AsianLanguageNames.Any(fileName.Contains);
|
|
}
|
|
|
|
public string ConvertToUnicode(string contentToConvert)
|
|
{
|
|
var conversionResult = contentToConvert.Select(t => $"U+{Convert.ToUInt16(t):X4}");
|
|
|
|
return string.Concat(conversionResult);
|
|
}
|
|
|
|
public void Initialize(IServiceContext serviceContext)
|
|
{
|
|
var configFilePath = @"Configuration/FontReaderConfiguration.json";
|
|
|
|
if (!File.Exists(configFilePath))
|
|
{
|
|
throw new FileNotFoundException("Unable to load the configuration file", configFilePath);
|
|
}
|
|
|
|
_configuration = JsonConvert.DeserializeObject<FontReaderConfiguration>(File.ReadAllText(configFilePath));
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
}
|
|
} |