예제 #1
0
        public bool CheckValid()
        {
            CheckDisposed();

            string caption = FwCoreDlgControls.kstidError;

            ScriptSubtag scriptSubtag = ScriptSubtag;

            // Can't allow a script name without an abbreviation.
            if (scriptSubtag == null && !string.IsNullOrEmpty(m_scriptName.Text.Trim()))
            {
                MessageBox.Show(FindForm(), FwCoreDlgControls.kstidMissingScrAbbr, caption);
                return(false);
            }
            if (scriptSubtag != null && scriptSubtag.IsPrivateUse)
            {
                if (!LangTagUtils.GetScriptSubtag(scriptSubtag.Code).IsPrivateUse)
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidDupScrAbbr, caption);
                    return(false);
                }
                if (!scriptSubtag.IsValid)
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidInvalidScrAbbr, caption);
                    return(false);
                }
            }

            RegionSubtag regionSubtag = RegionSubtag;

            // Can't allow a country name without an abbreviation.
            if (regionSubtag == null && !string.IsNullOrEmpty(m_regionName.Text.Trim()))
            {
                MessageBox.Show(FindForm(), FwCoreDlgControls.kstidMissingRgnAbbr, caption);
                return(false);
            }
            if (regionSubtag != null && regionSubtag.IsPrivateUse)
            {
                if (!LangTagUtils.GetRegionSubtag(regionSubtag.Code).IsPrivateUse)
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidDupRgnAbbr, caption);
                    return(false);
                }
                if (!regionSubtag.IsValid)
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidInvalidRgnAbbr, caption);
                    return(false);
                }
            }

            VariantSubtag variantSubtag = VariantSubtag;

            // Can't allow a variant name without an abbreviation.
            if (variantSubtag == null && !string.IsNullOrEmpty(m_variantName.Text.Trim()))
            {
                MessageBox.Show(FindForm(), FwCoreDlgControls.kstidMissingVarAbbr, caption);
                return(false);
            }
            if (variantSubtag != null && variantSubtag.IsPrivateUse)
            {
                if (!LangTagUtils.GetVariantSubtag(variantSubtag.Code).IsPrivateUse)
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidDupVarAbbr, caption);
                    return(false);
                }
                if (!variantSubtag.IsValid)
                {
                    MessageBox.Show(FindForm(), FwCoreDlgControls.kstidInvalidVarAbbr, caption);
                    return(false);
                }
            }
            if (variantSubtag != null)
            {
                var parts = variantSubtag.Code.Split('-').ToList();
                // If these subtags are private use, the first element of each must also be distinct.
                if (m_ws.LanguageSubtag.IsPrivateUse)
                {
                    parts.Add(m_ws.LanguageSubtag.Code.Split('-').First());
                }
                if (scriptSubtag != null && scriptSubtag.IsPrivateUse)
                {
                    parts.Add(scriptSubtag.Code.Split('-').First());
                }
                if (regionSubtag != null && regionSubtag.IsPrivateUse)
                {
                    parts.Add(regionSubtag.Code.Split('-').First());
                }
                var uniqueParts = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                foreach (var part in parts)
                {
                    if (uniqueParts.Contains(part))
                    {
                        MessageBox.Show(FindForm(), String.Format(FwCoreDlgControls.kstidDuplicateParts, part), caption);
                        return(false);
                    }
                    uniqueParts.Add(part);
                }
            }

            return(true);
        }
예제 #2
0
        public void LanguageAndVariantTags()
        {
            // A new writing system has a Language tag of qaa. This is also its language tag. The others are null.
            var ws = new PalasoWritingSystem();

            VerifySubtagCodes(ws, "qaa", null, null, null, "qaa");
            VerifyComponents(ws, "qaa", "", "", "", "qaa");

            ws.LanguageSubtag = LangTagUtils.GetLanguageSubtag("en");
            VerifySubtagCodes(ws, "en", null, null, null, "en");
            VerifyComponents(ws, "en", "", "", "", "en");
            Assert.That(ws.LanguageName, Is.EqualTo("English"));

            ws.LanguageSubtag = new LanguageSubtag("kal", "Kalaba", true, "");
            Assert.That(ws.LanguageName, Is.EqualTo("Kalaba"));
            VerifySubtagCodes(ws, "kal", null, null, null, "qaa-x-kal");
            VerifyComponents(ws, "qaa", "", "", "x-kal", "qaa-x-kal");
            Assert.That(ws.LanguageSubtag.Name, Is.EqualTo("Kalaba"));

            // This is a region code that is valid, so we don't store it in the private-use area of our code.
            ws.RegionSubtag = LangTagUtils.GetRegionSubtag("QN");
            VerifySubtagCodes(ws, "kal", null, "QN", null, "qaa-QN-x-kal");
            VerifyComponents(ws, "qaa", "", "QN", "x-kal", "qaa-QN-x-kal");

            // This is a standard region (Norway).
            ws.RegionSubtag = LangTagUtils.GetRegionSubtag("NO");
            VerifySubtagCodes(ws, "kal", null, "NO", null, "qaa-NO-x-kal");
            VerifyComponents(ws, "qaa", "", "NO", "x-kal", "qaa-NO-x-kal");

            // A private region
            ws.RegionSubtag = LangTagUtils.GetRegionSubtag("ZD");
            VerifySubtagCodes(ws, "kal", null, "ZD", null, "qaa-QM-x-kal-ZD");
            VerifyComponents(ws, "qaa", "", "QM", "x-kal-ZD", "qaa-QM-x-kal-ZD");

            // Add a private script
            ws.ScriptSubtag = LangTagUtils.GetScriptSubtag("Zfdr");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", null, "qaa-Qaaa-QM-x-kal-Zfdr-ZD");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "x-kal-Zfdr-ZD", "qaa-Qaaa-QM-x-kal-Zfdr-ZD");

            // Change it to a standard one
            ws.ScriptSubtag = LangTagUtils.GetScriptSubtag("Phnx");
            VerifySubtagCodes(ws, "kal", "Phnx", "ZD", null, "qaa-Phnx-QM-x-kal-ZD");
            VerifyComponents(ws, "qaa", "Phnx", "QM", "x-kal-ZD", "qaa-Phnx-QM-x-kal-ZD");

            // To the standard private-use marker
            ws.ScriptSubtag = LangTagUtils.GetScriptSubtag("Qaaa");
            VerifySubtagCodes(ws, "kal", "Qaaa", "ZD", null, "qaa-Qaaa-QM-x-kal-Qaaa-ZD");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "x-kal-Qaaa-ZD", "qaa-Qaaa-QM-x-kal-Qaaa-ZD");


            // Back to the special one
            ws.ScriptSubtag = LangTagUtils.GetScriptSubtag("Zfdr");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", null, "qaa-Qaaa-QM-x-kal-Zfdr-ZD");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "x-kal-Zfdr-ZD", "qaa-Qaaa-QM-x-kal-Zfdr-ZD");

            // Add a standard variant
            ws.VariantSubtag = LangTagUtils.GetVariantSubtag("fonipa");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", "fonipa", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "fonipa-x-kal-Zfdr-ZD", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD");

            // Change it to a combination one
            ws.VariantSubtag = LangTagUtils.GetVariantSubtag("fonipa-x-etic");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", "fonipa-x-etic", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD-etic");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "fonipa-x-kal-Zfdr-ZD-etic", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD-etic");

            // Back to no variant.
            ws.VariantSubtag = null;
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", null, "qaa-Qaaa-QM-x-kal-Zfdr-ZD");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "x-kal-Zfdr-ZD", "qaa-Qaaa-QM-x-kal-Zfdr-ZD");

            // Try a double combination
            ws.VariantSubtag = LangTagUtils.GetVariantSubtag("fonipa-1996-x-etic-emic");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", "fonipa-1996-x-etic-emic", "qaa-Qaaa-QM-fonipa-1996-x-kal-Zfdr-ZD-etic-emic");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "fonipa-1996-x-kal-Zfdr-ZD-etic-emic", "qaa-Qaaa-QM-fonipa-1996-x-kal-Zfdr-ZD-etic-emic");

            // Drop a piece out of each
            ws.VariantSubtag = LangTagUtils.GetVariantSubtag("fonipa-x-etic");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", "fonipa-x-etic", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD-etic");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "fonipa-x-kal-Zfdr-ZD-etic", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD-etic");

            // Soemthing totally unknown
            ws.VariantSubtag = LangTagUtils.GetVariantSubtag("fonipa-x-blah");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", "fonipa-x-blah", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD-blah");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "fonipa-x-kal-Zfdr-ZD-blah", "qaa-Qaaa-QM-fonipa-x-kal-Zfdr-ZD-blah");

            // Drop just the standard part
            ws.VariantSubtag = LangTagUtils.GetVariantSubtag("x-blah");
            VerifySubtagCodes(ws, "kal", "Zfdr", "ZD", "x-blah", "qaa-Qaaa-QM-x-kal-Zfdr-ZD-blah");
            VerifyComponents(ws, "qaa", "Qaaa", "QM", "x-kal-Zfdr-ZD-blah", "qaa-Qaaa-QM-x-kal-Zfdr-ZD-blah");

            // No longer a custom language
            ws.LanguageSubtag = LangTagUtils.GetLanguageSubtag("en");
            VerifySubtagCodes(ws, "en", "Zfdr", "ZD", "x-blah", "en-Qaaa-QM-x-Zfdr-ZD-blah");
            VerifyComponents(ws, "en", "Qaaa", "QM", "x-Zfdr-ZD-blah", "en-Qaaa-QM-x-Zfdr-ZD-blah");

            // No longer a custom script
            ws.ScriptSubtag = null;
            VerifySubtagCodes(ws, "en", null, "ZD", "x-blah", "en-QM-x-ZD-blah");
            VerifyComponents(ws, "en", "", "QM", "x-ZD-blah", "en-QM-x-ZD-blah");

            // No longer a custom region
            ws.RegionSubtag = null;
            VerifySubtagCodes(ws, "en", null, null, "x-blah", "en-x-blah");
            VerifyComponents(ws, "en", "", "", "x-blah", "en-x-blah");

            // No more variant
            ws.VariantSubtag = null;
            VerifySubtagCodes(ws, "en", null, null, null, "en");
            VerifyComponents(ws, "en", "", "", "", "en");
        }
예제 #3
0
        /// <summary>
        /// Reads the top level special element.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ws">The ws.</param>
        protected override void ReadTopLevelSpecialElement(XmlReader reader, WritingSystemDefinition ws)
        {
            if (reader.GetAttribute("xmlns:fw") != null)
            {
                reader.ReadStartElement("special");
                var fwWs = (PalasoWritingSystem)ws;
                fwWs.DefaultFontFeatures = GetSpecialValue(reader, "fw", "defaultFontFeatures");
                bool graphiteEnabled;
                if (bool.TryParse(GetSpecialValue(reader, "fw", "graphiteEnabled"), out graphiteEnabled))
                {
                    fwWs.IsGraphiteEnabled = graphiteEnabled;
                }
                fwWs.LegacyMapping       = GetSpecialValue(reader, "fw", "legacyMapping");
                fwWs.MatchedPairs        = GetSpecialValue(reader, "fw", "matchedPairs");
                fwWs.PunctuationPatterns = GetSpecialValue(reader, "fw", "punctuationPatterns");
                fwWs.QuotationMarks      = GetSpecialValue(reader, "fw", "quotationMarks");
                fwWs.RegionName          = GetSpecialValue(reader, "fw", "regionName");
                fwWs.ScriptName          = GetSpecialValue(reader, "fw", "scriptName");
                fwWs.ValidChars          = GetSpecialValue(reader, "fw", "validChars");
                fwWs.VariantName         = GetSpecialValue(reader, "fw", "variantName");
                if (fwWs.VariantName == null)
                {
                    var variant = fwWs.VariantSubtag;
                    if (variant != null)
                    {
                        var stdVariant = LangTagUtils.GetVariantSubtag(variant.Code);
                        if (stdVariant.Name != null)
                        {
                            fwWs.VariantName = stdVariant.Name;
                        }
                        else
                        {
                            var variantName = variant.ToString();
                            if (variantName.StartsWith("x-", StringComparison.OrdinalIgnoreCase))
                            {
                                variantName = variantName.Substring(2);
                            }
                            fwWs.VariantName = variantName;
                        }
                    }
                }
                if (fwWs.RegionName == null)
                {
                    var region = fwWs.RegionSubtag;
                    if (region != null)
                    {
                        var stdRegion = LangTagUtils.GetRegionSubtag(region.Code);
                        if (stdRegion.Name != null)
                        {
                            fwWs.RegionName = stdRegion.Name;
                        }
                        else
                        {
                            var regionName = region.ToString();
                            if (regionName.StartsWith("x-", StringComparison.OrdinalIgnoreCase))
                            {
                                regionName = regionName.Substring(2);
                            }
                            fwWs.RegionName = regionName;
                        }
                    }
                }
                if (fwWs.ScriptName == null)
                {
                    var script = fwWs.ScriptSubtag;
                    if (script != null)
                    {
                        var stdScript = LangTagUtils.GetScriptSubtag(script.Code);
                        if (stdScript.Name != null)
                        {
                            fwWs.ScriptName = stdScript.Name;
                        }
                        else
                        {
                            var scriptName = script.ToString();
                            if (scriptName.StartsWith("x-", StringComparison.OrdinalIgnoreCase))
                            {
                                scriptName = scriptName.Substring(2);
                            }
                            fwWs.ScriptName = scriptName;
                        }
                    }
                }
                var lcidString = GetSpecialValue(reader, "fw", "windowsLCID");
                ((ILegacyWritingSystemDefinition)fwWs).WindowsLcid = lcidString;

                ReadSpecialEndElement(reader);
            }

            else
            {
                base.ReadTopLevelSpecialElement(reader, ws);
            }
        }