/// <summary> Exports vernacular language character set to an ldml file </summary> private static void LdmlExport(string filePath, string vernacularBcp47, List <string> validChars) { var wsr = LdmlInFolderWritingSystemRepository.Initialize(filePath); var wsf = new LdmlInFolderWritingSystemFactory(wsr); wsf.Create(vernacularBcp47, out var wsDef); // If there isn't already a main character set defined, // make one and add it to the writing system definition. if (!wsDef.CharacterSets.TryGet("main", out var chars)) { chars = new CharacterSetDefinition("main"); wsDef.CharacterSets.Add(chars); } // Replace all the characters found with our copy of the character set chars.Characters.Clear(); foreach (var character in validChars) { chars.Characters.Add(character); } // Write out the new definition wsr.Set(wsDef); wsr.Save(); }
private void AddCharactersToWritingSystem(CoreWritingSystemDefinition ws, string charSetType, List <string> characters) { if (characters.Count == 0) { ws.CharacterSets.Remove(charSetType); } else { CharacterSetDefinition charSet; if (!ws.CharacterSets.TryGet(charSetType, out charSet)) { charSet = new CharacterSetDefinition(charSetType); ws.CharacterSets.Add(charSet); } charSet.Characters.Clear(); foreach (string chr in characters) { charSet.Characters.Add(chr); } } }
/// <summary> Exports main character set from a project to an ldml file </summary> private void LdmlExport(string filePath, string langTag) { // SLDR is the SIL Locale Data repository, it is necessary for reading/writing ldml // It is being initialized in offline mode here to only pull local data Sldr.Initialize(true); try { var wsr = LdmlInFolderWritingSystemRepository.Initialize(filePath); var wsf = new LdmlInFolderWritingSystemFactory(wsr); wsf.Create(langTag, out var wsDef); var proj = _projService.GetProject(_projectId).Result; // If there isn't already a main character set defined, make one and add it to the writing system // definition if (!wsDef.CharacterSets.TryGet("main", out var chars)) { chars = new CharacterSetDefinition("main"); wsDef.CharacterSets.Add(chars); } // Replace all the characters found with our copy of the character set chars.Characters.Clear(); foreach (var character in proj.ValidCharacters) { chars.Characters.Add(character); } // Write out the new definition wsr.Set(wsDef); wsr.Save(); } finally { // If there was somehow an error above, we still want to cleanup to prevent unhelpful errors later Sldr.Cleanup(); } }
public void Roundtrip_LdmlAlternateCharacters_Ignored() { // Test when reading LDML that elements with alt attribute are ignored. // Because we don't write elements with alt attribute, this test // starts by writing a set content using (var environment = new TestEnvironment()) { string content = @"<?xml version='1.0' encoding='utf-8'?> <ldml> <identity> <version number='' /> <language type='en' /> <script type='Latn' /> </identity> <characters> <exemplarCharacters alt='capital'>[A-Z{AZ}]</exemplarCharacters> <exemplarCharacters>[a-z{az}]</exemplarCharacters> </characters> </ldml>".Replace("\'", "\""); var main = new CharacterSetDefinition("main"); for (int i = 'a'; i <= (int)'z'; i++) main.Characters.Add(((char)i).ToString(CultureInfo.InvariantCulture)); main.Characters.Add("az"); File.WriteAllText(environment.FilePath("test.ldml"), content); var ldmlAdaptor = new LdmlDataMapper(new TestWritingSystemFactory()); var wsFromLdml = new WritingSystemDefinition(); ldmlAdaptor.Read(environment.FilePath("test.ldml"), wsFromLdml); Assert.That(wsFromLdml.CharacterSets.Count, Is.EqualTo(1)); Assert.That(wsFromLdml.CharacterSets["main"].ValueEquals(main)); var wsToLdml = new WritingSystemDefinition("en", "Latn", "", ""); wsToLdml.CharacterSets.Add(main); ldmlAdaptor.Write(environment.FilePath("test2.ldml"), wsToLdml, new MemoryStream(File.ReadAllBytes(environment.FilePath("test.ldml")))); AssertThatXmlIn.File(environment.FilePath("test2.ldml")) .HasAtLeastOneMatchForXpath("/ldml/characters/exemplarCharacters[text()='[a-z{az}]']", environment.NamespaceManager); AssertThatXmlIn.File(environment.FilePath("test2.ldml")) .HasAtLeastOneMatchForXpath("/ldml/characters/exemplarCharacters[@alt='capital' and text()='[A-Z{AZ}]']", environment.NamespaceManager); } }
public void Roundtrip_LdmlCharacters() { using (var environment = new TestEnvironment()) { var index = new CharacterSetDefinition("index"); for (int i = 'A'; i <= (int) 'Z'; i++) index.Characters.Add(((char) i).ToString(CultureInfo.InvariantCulture)); index.Characters.Add("AZ"); var main = new CharacterSetDefinition("main"); for (int i = 'a'; i <= (int) 'z'; i++) main.Characters.Add(((char) i).ToString(CultureInfo.InvariantCulture)); main.Characters.Add("az"); var footnotes = new CharacterSetDefinition("footnotes") {Characters = {"¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "¹⁰"}}; var numeric = new CharacterSetDefinition("numeric") {Characters = {"๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"}}; var wsToLdml = new WritingSystemDefinition("en", "Latn", "", ""); wsToLdml.CharacterSets.Add(index); wsToLdml.CharacterSets.Add(main); wsToLdml.CharacterSets.Add(footnotes); wsToLdml.CharacterSets.Add(numeric); var ldmlAdaptor = new LdmlDataMapper(new TestWritingSystemFactory()); ldmlAdaptor.Write(environment.FilePath("test.ldml"), wsToLdml, null); AssertThatXmlIn.File(environment.FilePath("test.ldml")) .HasAtLeastOneMatchForXpath("/ldml/characters/exemplarCharacters[@type='index' and text()='[A-Z{AZ}]']", environment.NamespaceManager); AssertThatXmlIn.File(environment.FilePath("test.ldml")) .HasAtLeastOneMatchForXpath("/ldml/characters/exemplarCharacters[text()='[a-z{az}]']", environment.NamespaceManager); // Character set in XPath is escaped differently from the actual file AssertThatXmlIn.File(environment.FilePath("test.ldml")) .HasAtLeastOneMatchForXpath("/ldml/characters/special/sil:exemplarCharacters[@type='footnotes' and text()='[¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ {¹⁰}]']", environment.NamespaceManager); AssertThatXmlIn.File(environment.FilePath("test.ldml")) .HasAtLeastOneMatchForXpath("/ldml/numbers/defaultNumberingSystem[text()='standard']", environment.NamespaceManager); AssertThatXmlIn.File(environment.FilePath("test.ldml")) .HasAtLeastOneMatchForXpath("/ldml/numbers/numberingSystem[@id='standard' and @type='numeric' and @digits='๐๑๒๓๔๕๖๗๘๙']", environment.NamespaceManager); var wsFromLdml = new WritingSystemDefinition(); ldmlAdaptor.Read(environment.FilePath("test.ldml"), wsFromLdml); Assert.That(wsFromLdml.CharacterSets["index"].ValueEquals(index), Is.True); Assert.That(wsFromLdml.CharacterSets["main"].ValueEquals(main), Is.True); Assert.That(wsFromLdml.CharacterSets["footnotes"].ValueEquals(footnotes), Is.True); Assert.That(wsFromLdml.CharacterSets["numeric"].ValueEquals(numeric), Is.True); } }
public void Migrate_OriginalFileContainsFwNamespaceWithOldValidChars_ValidCharsIsMigratedFromLegacyOverridesFile() { using (var environment = new TestEnvironment()) { environment.WriteLdmlFile( "test.ldml", LdmlContentForTests.Version0WithOldFwValidChars("en", "", "US", "")); var migrator = new LdmlInFolderWritingSystemRepositoryMigrator(environment.LdmlPath, environment.OnMigrateCallback); migrator.Migrate(); var repo = new TestLdmlInFolderWritingSystemRepository(environment.LdmlPath); migrator.ResetRemovedProperties(repo); var main = new CharacterSetDefinition("main") {Characters = {"x", "y", "z"}}; WritingSystemDefinition ws = repo.Get("en-US"); Assert.That(ws.CharacterSets["main"].ValueEquals(main)); Assert.That(ws.CharacterSets.Contains("numeric"), Is.False); Assert.That(ws.CharacterSets.Contains("punctuation"), Is.False); } }
public void Migrate_OriginalFileContainsFwNamespace_InfoIsMigrated() { using (var environment = new TestEnvironment()) { environment.WriteLdmlFile( "test.ldml", LdmlContentForTests.Version0WithFw("en", "x-Kala", "x-AP", "1996-x-myOwnVariant")); var migrator = new LdmlInFolderWritingSystemRepositoryMigrator(environment.LdmlPath, environment.OnMigrateCallback); migrator.Migrate(); var repo = new TestLdmlInFolderWritingSystemRepository(environment.LdmlPath); migrator.ResetRemovedProperties(repo); var other = new FontDefinition("Arial") {Features = "order=3 children=2 color=red createDate=1996", Roles = FontRoles.Default}; var main = new CharacterSetDefinition("main") { Characters = { "α", "Α", "ά", "ὰ", "ᾷ", "ἀ", "Ἀ", "ἁ", "Ἁ", "ἄ", "Ἄ", "ἂ", "ἅ", "Ἅ", "ἃ", "Ἃ", "ᾶ", "ᾳ", "ᾴ", "ἆ", "Ἆ", "ᾄ", "ᾅ", "β", "Β", "γ", "Γ", "δ", "Δ", "ε", "Ε", "έ", "ὲ", "ἐ", "Ἐ", "ἑ", "Ἑ", "ἔ", "Ἔ", "ἕ", "Ἕ", "ἓ", "Ἓ", "ζ", "Ζ", "η", "Η", "ή", "ὴ", "ῇ", "ἠ", "Ἠ", "ἡ", "Ἡ", "ἤ", "Ἤ", "ἢ", "ἥ", "Ἥ", "Ἢ", "ἣ", "ᾗ", "ῆ", "ῃ", "ῄ", "ἦ", "Ἦ", "ᾖ", "ἧ", "ᾐ", "ᾑ", "ᾔ", "θ", "Θ", "ι", "ί", "ὶ", "ϊ", "ΐ", "ῒ", "ἰ", "Ἰ", "ἱ", "Ἱ", "ἴ", "Ἴ", "ἵ", "Ἵ", "ἳ", "ῖ", "ἶ", "ἷ", "κ", "Κ", "λ", "Λ", "μ", "Μ", "ν", "Ν", "ξ", "Ξ", "ο", "Ο", "ό", "ὸ", "ὀ", "Ὀ", "ὁ", "Ὁ", "ὄ", "Ὄ", "ὅ", "ὂ", "Ὅ", "ὃ", "Ὃ", "π", "Π", "ρ", "Ρ", "ῥ", "Ῥ", "σ", "ς", "Σ", "τ", "Τ", "υ", "Υ", "ύ", "ὺ", "ϋ", "ΰ", "ῢ", "ὐ", "ὑ", "Ὑ", "ὔ", "ὕ", "ὒ", "Ὕ", "ὓ", "ῦ", "ὖ", "ὗ", "Ὗ", "φ", "Φ", "χ", "Χ", "ψ", "Ψ", "ω", "ώ", "ὼ", "ῷ", "ὠ", "ὡ", "Ὡ", "ὤ", "Ὤ", "ὢ", "ὥ", "Ὥ", "ᾧ", "ῶ", "ῳ", "ῴ", "ὦ", "Ὦ", "ὧ", "Ὧ", "ᾠ" } }; var numeric = new CharacterSetDefinition("numeric") {Characters = {"๐", "๑", "๒", "๓", "๔", "๕", "๖", "๗", "๘", "๙"}}; var punctuation = new CharacterSetDefinition("punctuation") {Characters = {" ", "-", ",", ".", "’", "«", "»", "(", ")", "[", "]"}}; WritingSystemDefinition ws = repo.Get("en-Qaaa-QM-1996-x-Kala-AP-myOwnVar"); Assert.That(ws.DefaultFont.ValueEquals(other)); Assert.That(ws.WindowsLcid, Is.EqualTo("4321")); Assert.That(ws.CharacterSets["main"].ValueEquals(main)); Assert.That(ws.CharacterSets["numeric"].ValueEquals(numeric)); Assert.That(ws.CharacterSets["punctuation"].ValueEquals(punctuation)); // ScriptName, RegionName, VariantName, LegacyMapping, IsGraphiteEnabled Assert.That(ws.LegacyMapping, Is.EqualTo("SomeMapper")); Assert.That(ws.IsGraphiteEnabled, Is.True); Assert.That(ws.Script.Name, Is.EqualTo("scriptName")); Assert.That(ws.Region.Name, Is.EqualTo("regionName")); Assert.That(ws.Variants[1].Name, Is.EqualTo("aVarName")); } }
// Numbering system gets added to the character set definition private void ReadNumbersElement(XElement numbersElem, WritingSystemDefinition ws) { XElement defaultNumberingSystemElem = numbersElem.NonAltElement("defaultNumberingSystem"); if (defaultNumberingSystemElem != null) { var id = (string) defaultNumberingSystemElem; XElement numberingSystemsElem = numbersElem.NonAltElements("numberingSystem") .FirstOrDefault(e => id == (string) e.Attribute("id") && (string) e.Attribute("type") == "numeric"); if (numberingSystemsElem != null) { var csd = new CharacterSetDefinition("numeric"); // Only handle numeric types var digits = (string) numberingSystemsElem.Attribute("digits"); foreach (char charItem in digits) csd.Characters.Add(charItem.ToString(CultureInfo.InvariantCulture)); ws.CharacterSets.Add(csd); } } }
private void ReadExemplarCharactersElem(XElement exemplarCharactersElem, WritingSystemDefinition ws) { string type = (string) exemplarCharactersElem.Attribute("type") ?? "main"; var csd = new CharacterSetDefinition(type); var unicodeSet = (string) exemplarCharactersElem; csd.Characters.AddRange(type == "footnotes" ? unicodeSet.Trim('[', ']').Split(' ').Select(c => c.Trim('{', '}')) : UnicodeSet.ToCharacters(unicodeSet)); ws.CharacterSets.Add(csd); }