public static double Solve_Object_Axis_Position(CssValue Pos, double ObjectArea, double ObjectSize) {/* https://www.w3.org/TR/css-backgrounds-3/#the-background-position */ if (Pos is null) { throw new ArgumentNullException(nameof(Pos)); } if (Pos.Type != ECssValueTypes.PERCENT) { throw new ArgumentException("Pos must be a CSS percent-type value!"); } Contract.EndContractBlock(); var P = Pos.AsDecimal(); return((P * ObjectArea) - (P * ObjectSize)); }
/// <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)); }