Beispiel #1
0
        /// <summary>
        /// 文字列から font-size 値を読み取って、このインスタンスに反映させます。
        /// </summary>
        /// <param name="text">新しい font-size の値を表現する文字列を指定します。</param>
        /// <param name="index">読み取り開始位置を指定します。読み取り終了位置を返します。</param>
        public void Parse(string text, ref int index)
        {
            const string READ_NUMBER_ERROR = @"[afh.Renderer.LengthValue.Parse]
数値部分の読み取りに失敗しました。
入力文字列: ";
            const string READ_UNIT_ERROR   = @"[afh.Renderer.LengthValue.Parse]
単位部分の読み取りに失敗しました。
入力文字列:     {0}
読み取った単位: {1}";
            //-------------------------------------------------------
            string num = "";
            string uni = "";

            // 数値部分の切り出し
            while (index < text.Length)
            {
                char c = text[index++];
                if ('0' <= c && c <= '9' || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-')
                {
                    num += c;
                }
                else if ('0' <= c && c <= '9' || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-')
                {
                    num += (c + '0' - '0');
                }
                else
                {
                    break;
                }
            }
            {
                char endc = num[num.Length - 1];
                if (endc == 'e' || endc == 'E')
                {
                    uni = "e";
                    num = num.Substring(0, num.Length - 1);
                }
            }

            // 数値部分の読み取り
            float number;

            try{
                number = float.Parse(num);
            }catch (System.FormatException e) {
                throw new System.FormatException(READ_NUMBER_ERROR + text, e);
            }

            // 空白飛ばし
            while (index < text.Length)
            {
                afh.Parse.LetterType ctype = afh.Parse.LetterType.GetLetterType(text[index++]);
                if (!ctype.IsInvalid && !ctype.IsSpace)
                {
                    break;
                }
            }

            // 単位部分の切り出し
            while (index < text.Length)
            {
                char c = text[index++];
                afh.Parse.LetterType ctype = afh.Parse.LetterType.GetLetterType(c);
                if (ctype.IsInvalid || ctype.IsSpace)
                {
                    break;
                }

                if ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z')
                {
                    uni += (c + 'A' - 'A');
                }
                else
                {
                    uni += c;
                }
            }

            // 単位部分の読み取り
            LengthUnit unit;

            if (!dic_unames.TryGetValue(uni.ToLower(), out unit))
            {
                throw new System.FormatException(string.Format(READ_UNIT_ERROR, text, uni));
            }

            this.unit  = unit;
            this.value = number;
        }
Beispiel #2
0
        /// <summary>
        /// 文字列から font-size 値を読み取って、このインスタンスに反映させます。
        /// </summary>
        /// <param name="text">新しい font-size の値を表現する文字列を指定します。</param>
        public void Parse(string text)
        {
            int i = 0;

            // 数値部読み取り
            string num = "";
            float  number;

            while (i < text.Length)
            {
                char c = text[i++];
                if ('0' <= c && c <= '9' || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-')
                {
                    num += c;
                }
                else if ('0' <= c && c <= '9' || c == '.' || c == 'e' || c == 'E' || c == '+' || c == '-')
                {
                    num += (c + '0' - '0');
                }
                else
                {
                    break;
                }
            }
            try{
                number = float.Parse(num);
            }catch (System.FormatException e) {
                throw new System.FormatException("[afh.Renderer.Length.Parse]\r\n    数値部分の読み取りに失敗しました。\r\n    入力文字列: " + text, e);
            }

            // 空白飛ばし
            while (i < text.Length)
            {
                afh.Parse.LetterType ctype = afh.Parse.LetterType.GetLetterType(text[i++]);
                if (!ctype.IsInvalid && !ctype.IsSpace)
                {
                    break;
                }
            }

            // 単位部読み取り
            string uni = "";

            while (i < text.Length)
            {
                char c = text[i++];
                afh.Parse.LetterType ctype = afh.Parse.LetterType.GetLetterType(c);
                if (ctype.IsInvalid || ctype.IsSpace)
                {
                    break;
                }

                if ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z')
                {
                    uni += (c + 'A' - 'A');
                }
                else
                {
                    uni += c;
                }
            }
            LengthUnit unit;

            if (!dic_unames.TryGetValue(uni.ToLower(), out unit))
            {
                throw new System.FormatException("[afh.Renderer.Length.Parse]\r\n    単位部分の読み取りに失敗しました。\r\n    入力文字列: " + text + "\r\n    読み取った単位: " + uni);
            }

            this.unit   = unit;
            this.number = number;
            this.UpdateValue();
        }