Example #1
0
        public static Rect2f Cover_Constraint_Algorithm(CssPrincipalBox Box, double Width, double Height)
        {/* Docs: https://www.w3.org/TR/css3-images/#cover-constraint */
            if (Box is null)
            {
                throw new ArgumentNullException(nameof(Box));
            }
            Contract.EndContractBlock();

            /* A cover constraint is resolved by setting the concrete object size to the smallest rectangle that has the object's intrinsic aspect ratio and additionally has neither width nor height smaller than the constraint rectangle's width and height, respectively. */
            var Ratio = (Box.Intrinsic_Ratio ?? 1f);

            if (Width > Height)
            {/* Formula: Height = Width / Max(ratio, (Width/minHeight)) */
                Height = (Width / MathExt.Max(Ratio, Width / Height));
                return(new Rect2f(Width, Height));
            }
            else
            {/* Formula: Width = Height * Max(1/ratio, (minHeight/Width)) */
                Width = (Height * MathExt.Max(1f / Ratio, Height / Width));
                return(new Rect2f(Width, Height));
            }
        }
Example #2
0
        /// <summary>
        /// The default sizing algorithm is a set of rules commonly used to find an object's concrete object size.
        /// It resolves the simultaneous constraints presented by the object's intrinsic dimensions and either an unconstrained specified size or one consisting of only a definite width and/or height.
        /// </summary>
        public static Rect2f Default_Sizing_Algorithm(CssPrincipalBox Box, CssValue Specified_Width, CssValue Specified_Height, double Default_Width, double Default_Height)
        {/* Docs: https://www.w3.org/TR/css3-images/#default-object-size */
            if (Box is null)
            {
                throw new ArgumentNullException(nameof(Box));
            }
            if (Specified_Width is null)
            {
                throw new ArgumentNullException(nameof(Specified_Width));
            }
            if (Specified_Height is null)
            {
                throw new ArgumentNullException(nameof(Specified_Height));
            }
            Contract.EndContractBlock();

            /* The default sizing algorithm is defined as follows: */

            /* If the specified size is a definite width and height, the concrete object size is given that width and height. */
            if (Specified_Width.IsDefinite && Specified_Height.IsDefinite)
            {
                return(new Rect2f(Specified_Width.AsDecimal(), Specified_Height.AsDecimal()));
            }

            /*
             * If the specified size is only a width or height (but not both) then the concrete object size is given that specified width or height. The other dimension is calculated as follows:
             * 1) If the object has an intrinsic aspect ratio, the missing dimension of the concrete object size is calculated using the intrinsic aspect ratio and the present dimension.
             * 2) Otherwise, if the missing dimension is present in the object's intrinsic dimensions, the missing dimension is taken from the object's intrinsic dimensions.
             * 3) Otherwise, the missing dimension of the concrete object size is taken from the default object size.
             */
            if (Specified_Width.HasValue ^ Specified_Height.HasValue)
            {
                if (Box.Intrinsic_Ratio.HasValue)
                {/* 1) If the object has an intrinsic aspect ratio, the missing dimension of the concrete object size is calculated using the intrinsic aspect ratio and the present dimension. */
                    if (Specified_Width.HasValue)
                    {
                        return(new Rect2f(Specified_Width.AsDecimal(), (Specified_Width.AsDecimal() / Box.Intrinsic_Ratio.Value)));
                    }
                    else
                    {
                        return(new Rect2f((Specified_Height.AsDecimal() * Box.Intrinsic_Ratio.Value), Specified_Height.AsDecimal()));
                    }
                }

                /* 2) Otherwise, if the missing dimension is present in the object's intrinsic dimensions, the missing dimension is taken from the object's intrinsic dimensions. */
                if (!Specified_Width.HasValue && Box.Intrinsic_Width.HasValue)
                {
                    return(new Rect2f(Box.Intrinsic_Width.Value, Specified_Height.AsDecimal()));
                }
                else if (!Specified_Height.HasValue && Box.Intrinsic_Height.HasValue)
                {
                    return(new Rect2f(Specified_Width.AsDecimal(), Box.Intrinsic_Height.Value));
                }

                /* 3) Otherwise, the missing dimension of the concrete object size is taken from the default object size. */
                if (Specified_Width.HasValue)
                {
                    return(new Rect2f(Specified_Width.AsDecimal(), Default_Height));
                }
                else
                {
                    return(new Rect2f(Default_Width, Specified_Height.AsDecimal()));
                }
            }

            /*
             * If the specified size has no constraints:
             * 1) If the object has an intrinsic height or width, its size is resolved as if its intrinsic size were given as the specified size.
             * 2) Otherwise, its size is resolved as a contain constraint against the default object size.
             */
            if (Box.Intrinsic_Width.HasValue || Box.Intrinsic_Height.HasValue)
            {
                if (Box.Intrinsic_Width.HasValue && Box.Intrinsic_Height.HasValue)
                {
                    return(new Rect2f(Box.Intrinsic_Width.Value, Box.Intrinsic_Height.Value));
                }
            }
            else if (Box.Intrinsic_Width.HasValue ^ Box.Intrinsic_Height.HasValue)
            {
                /* Step 1 */
                if (Box.Intrinsic_Ratio.HasValue)
                {/* 1) If the object has an intrinsic aspect ratio, the missing dimension of the concrete object size is calculated using the intrinsic aspect ratio and the present dimension. */
                    if (Box.Intrinsic_Width.HasValue)
                    {
                        return(new Rect2f(Box.Intrinsic_Width.Value, (Box.Intrinsic_Width.Value / Box.Intrinsic_Ratio.Value)));
                    }
                    else
                    {
                        return(new Rect2f((Box.Intrinsic_Height.Value * Box.Intrinsic_Ratio.Value), Box.Intrinsic_Height.Value));
                    }
                }

                /* Step 3 */
                if (Box.Intrinsic_Width.HasValue)
                {
                    return(new Rect2f(Box.Intrinsic_Width.Value, Default_Height));
                }
                else
                {
                    return(new Rect2f(Default_Width, Box.Intrinsic_Height.Value));
                }
            }

            /* 2) Otherwise, its size is resolved as a contain constraint against the default object size. */
            return(Contain_Constraint_Algorithm(Box, Default_Width, Default_Height));
        }