コード例 #1
0
ファイル: PointFConverter.cs プロジェクト: alexandrebaker/Eto
		/// <summary>
		/// Converts the specified value to a <see cref="PointF"/>
		/// </summary>
		/// <param name="context">Conversion context</param>
		/// <param name="culture">Culture to perform the conversion</param>
		/// <param name="value">Value to convert</param>
		/// <returns>A new instance of a <see cref="PointF"/> converted from the specified <paramref name="value"/></returns>
		public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			var text = value as string;
			if (text != null) {
				var parts = text.Split (culture.TextInfo.ListSeparator.ToCharArray ());
				if (parts.Length != 2)
					throw new ArgumentException (string.Format (CultureInfo.CurrentCulture, "Cannot parse value '{0}' as point.  Should be in the form of 'x,y'", text));

				var converter = new SingleConverter ();
				return new PointF (
					(float)converter.ConvertFromString (context, culture, parts [0]),
					(float)converter.ConvertFromString (context, culture, parts [1])
				);
			}
			return base.ConvertFrom (context, culture, value);
		}
コード例 #2
0
ファイル: Unit.cs プロジェクト: dbre2/dynamic-image
        internal Unit(string value, CultureInfo culture, UnitType defaultType)
        {
            if (string.IsNullOrEmpty(value))
            {
                _value = 0.0f;
                _type = (UnitType) 0;
            }
            else
            {
                if (culture == null)
                    culture = CultureInfo.CurrentCulture;

                string str = value.Trim().ToLower(CultureInfo.InvariantCulture);
                int length = str.Length;
                int num2 = -1;
                for (int i = 0; i < length; i++)
                {
                    char ch = str[i];
                    if (((ch < '0') || (ch > '9')) && (((ch != '-') && (ch != '.')) && (ch != ',')))
                        break;
                    num2 = i;
                }
                if (num2 == -1)
                    throw new FormatException(string.Format("'{0}' cannot be parsed as a unit as there are no numeric values in it. Examples of valid unit strings are '30px' and '50%'.", value));

                if (num2 < (length - 1))
                    _type = GetTypeFromString(str.Substring(num2 + 1).Trim());
                else
                    _type = defaultType;

                string text = str.Substring(0, num2 + 1);
                try
                {
                    TypeConverter converter = new SingleConverter();
                    _value = (float) converter.ConvertFromString(null, culture, text);
                    if (_type == UnitType.Pixel)
                        _value = (int) _value;
                }
                catch
                {
                    throw new FormatException(string.Format("The numeric part ('{1}') of '{0}' cannot be parsed as a numeric part of a {2} unit.", value, text, _type.ToString("G")));
                }

                if ((_value < -32768.0) || (_value > 32767.0))
                    throw new ArgumentOutOfRangeException("value");
            }
        }
コード例 #3
0
ファイル: SizeFConverter.cs プロジェクト: nlhepler/mono
		public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			string s = value as string;
			if (s == null)
				return base.ConvertFrom (context, culture, value);

			string[] subs = s.Split (culture.TextInfo.ListSeparator.ToCharArray ());

			SingleConverter converter = new SingleConverter ();
			float[] numSubs = new float[subs.Length];
			for (int i = 0; i < numSubs.Length; i++) {
				numSubs[i] = (float) converter.ConvertFromString (context, culture, subs[i]);
			}

			if (subs.Length != 2)
				throw new ArgumentException ("Failed to parse Text(" + s + ") expected text in the format \"Width,Height.\"");

			return new SizeF (numSubs[0], numSubs[1]);

		}
コード例 #4
0
ファイル: Unit.cs プロジェクト: krytht/DotNetReferenceSource
        internal Unit(string value, CultureInfo culture, UnitType defaultType) {
            if (String.IsNullOrEmpty(value)) {
                this.value = 0;
                this.type = (UnitType)0;
            }
            else {
                if (culture == null) {
                    culture = CultureInfo.CurrentCulture;
                }
                
                // This is invariant because it acts like an enum with a number together. 
                // The enum part is invariant, but the number uses current culture. 
                string trimLcase = value.Trim().ToLower(CultureInfo.InvariantCulture);
                int len = trimLcase.Length;

                int lastDigit = -1;
                for (int i = 0; i < len; i++) {
                    char ch = trimLcase[i];
                    if (((ch < '0') || (ch > '9')) && (ch != '-') && (ch != '.') && (ch != ','))
                        break;
                    lastDigit = i;
                }
                if (lastDigit == -1) {
                    throw new FormatException(SR.GetString(SR.UnitParseNoDigits, value));
                }
                if (lastDigit < len - 1) {
                    type = (UnitType)GetTypeFromString(trimLcase.Substring(lastDigit+1).Trim());
                }
                else {
                    type = defaultType;
                }

                string numericPart = trimLcase.Substring(0, lastDigit+1);
                // Cannot use Double.FromString, because we don't use it in the ToString implementation
                try {
                    TypeConverter converter = new SingleConverter();
                    this.value = (Single)converter.ConvertFromString(null, culture, numericPart);
                    
                    if (type == UnitType.Pixel) {
                        this.value = (int)this.value;
                    }
                }
                catch {
                    throw new FormatException(SR.GetString(SR.UnitParseNumericPart, value, numericPart, type.ToString("G")));
                }
                if ((this.value < MinValue) || (this.value > MaxValue)) {
                    throw new ArgumentOutOfRangeException("value");
                }
            }
        }