Example #1
0
        /// <summary>
        /// Parses a <see cref="RelativePoint"/> string.
        /// </summary>
        /// <param name="s">The string.</param>
        /// <returns>The parsed <see cref="RelativePoint"/>.</returns>
        public static RelativePoint Parse(string s)
        {
            using (var tokenizer = new StringTokenizer(s, CultureInfo.InvariantCulture, exceptionMessage: "Invalid RelativePoint."))
            {
                var x = tokenizer.ReadString();
                var y = tokenizer.ReadString();

                var unit  = RelativeUnit.Absolute;
                var scale = 1.0;

                if (x.EndsWith("%"))
                {
                    if (!y.EndsWith("%"))
                    {
                        throw new FormatException("If one coordinate is relative, both must be.");
                    }

                    x     = x.TrimEnd('%');
                    y     = y.TrimEnd('%');
                    unit  = RelativeUnit.Relative;
                    scale = 0.01;
                }

                return(new RelativePoint(
                           double.Parse(x, CultureInfo.InvariantCulture) * scale,
                           double.Parse(y, CultureInfo.InvariantCulture) * scale,
                           unit));
            }
        }
Example #2
0
        /// <summary>
        /// Parses a <see cref="RelativeRect"/> string.
        /// </summary>
        /// <param name="s">The string.</param>
        /// <returns>The parsed <see cref="RelativeRect"/>.</returns>
        public static RelativeRect Parse(string s)
        {
            using (var tokenizer = new StringTokenizer(s, exceptionMessage: "Invalid RelativeRect"))
            {
                var x      = tokenizer.ReadString();
                var y      = tokenizer.ReadString();
                var width  = tokenizer.ReadString();
                var height = tokenizer.ReadString();

                var unit  = RelativeUnit.Absolute;
                var scale = 1.0;

                var xRelative      = x.EndsWith("%", StringComparison.Ordinal);
                var yRelative      = y.EndsWith("%", StringComparison.Ordinal);
                var widthRelative  = width.EndsWith("%", StringComparison.Ordinal);
                var heightRelative = height.EndsWith("%", StringComparison.Ordinal);

                if (xRelative && yRelative && widthRelative && heightRelative)
                {
                    x      = x.TrimEnd(PercentChar);
                    y      = y.TrimEnd(PercentChar);
                    width  = width.TrimEnd(PercentChar);
                    height = height.TrimEnd(PercentChar);

                    unit  = RelativeUnit.Relative;
                    scale = 0.01;
                }
                else if (xRelative || yRelative || widthRelative || heightRelative)
                {
                    throw new FormatException("If one coordinate is relative, all must be.");
                }

                return(new RelativeRect(
                           double.Parse(x, CultureInfo.InvariantCulture) * scale,
                           double.Parse(y, CultureInfo.InvariantCulture) * scale,
                           double.Parse(width, CultureInfo.InvariantCulture) * scale,
                           double.Parse(height, CultureInfo.InvariantCulture) * scale,
                           unit));
            }
        }