public PDFResource GetLinearShadingPattern(PDFGraphics g, string key, PDFGradientLinearDescriptor descriptor, PDFRect bounds)
        {
            PDFLinearShadingPattern pattern = new PDFLinearShadingPattern(g.Container.Document, key, descriptor, bounds);

            return(pattern);
        }
        /// <summary>
        /// Parses a linear gradient from a string without decorations e.g. "to top right, red, green
        /// </summary>
        /// <param name="value"></param>
        /// <param name="linear"></param>
        /// <returns></returns>
        public static bool TryParseLinear(string value, out PDFGradientLinearDescriptor linear)
        {
            linear = null;
            string[] all = _splitter.Split(value);
            if (all.Length == 0)
            {
                return(false);
            }

            int    colorStopIndex = 0;
            double angle;

            if (all[0].StartsWith("to "))
            {
                var           ga = all[0].Substring(3).Trim().Replace(" ", "_");
                GradientAngle parsed;
                if (Enum.TryParse(ga, true, out parsed))
                {
                    angle = (double)parsed;
                }
                else
                {
                    return(false);
                }
                colorStopIndex = 1;
            }
            else if (char.IsNumber(all[0], 0))
            {
                var deg = all[0];

                if (deg.EndsWith("deg"))
                {
                    deg = deg.Substring(0, deg.Length - 3);
                }

                if (!double.TryParse(deg, out angle))
                {
                    return(false);
                }

                colorStopIndex = 1;
            }
            else
            {
                angle = (double)GradientAngle.Bottom;
            }

            PDFGradientColor[] colors = new PDFGradientColor[all.Length - colorStopIndex];

            for (int i = 0; i < colors.Length; i++)
            {
                PDFGradientColor parsed;
                if (PDFGradientColor.TryParse(all[i + colorStopIndex], out parsed))
                {
                    colors[i] = parsed;
                }
                else
                {
                    return(false);
                }
            }

            linear = new PDFGradientLinearDescriptor()
            {
                Angle = angle, Colors = new List <PDFGradientColor>(colors)
            };
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Attempts to parse a css style gradient string into a specific gradient descriptor
        /// </summary>
        /// <param name="value">The string to parse e.g. radial-gradient(red, green)</param>
        /// <param name="descriptor">Set to the parsed descriptor value</param>
        /// <returns>True if the gradient was parsed correctly</returns>
        public static bool TryParse(string value, out PDFGradientDescriptor descriptor)
        {
            descriptor = null;

            if (null == value)
            {
                return(false);
            }

            value = value.Trim();


            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }

            else if (value.StartsWith("linear-gradient"))
            {
                value = value.Substring("linear-gradient".Length).Trim();
                PDFGradientLinearDescriptor linear;

                if (value.StartsWith("(") && value.EndsWith(")") && PDFGradientLinearDescriptor.TryParseLinear(value.Substring(1, value.Length - 2), out linear))
                {
                    linear.Repeating = false;
                    descriptor       = linear;
                    return(true);
                }
            }
            else if (value.StartsWith("repeating-linear-gradient"))
            {
                value = value.Substring("repeating-linear-gradient".Length).Trim();
                PDFGradientLinearDescriptor linear;

                if (value.StartsWith("(") && value.EndsWith(")") && PDFGradientLinearDescriptor.TryParseLinear(value.Substring(1, value.Length - 2), out linear))
                {
                    linear.Repeating = true;
                    descriptor       = linear;
                    return(true);
                }
            }
            else if (value.StartsWith("radial-gradient"))
            {
                value = value.Substring("radial-gradient".Length).Trim();
                PDFGradientRadialDescriptor radial;

                if (value.StartsWith("(") && value.EndsWith(")") && PDFGradientRadialDescriptor.TryParseRadial(value.Substring(1, value.Length - 2), out radial))
                {
                    radial.Repeating = false;
                    descriptor       = radial;
                    return(true);
                }
            }
            else if (value.StartsWith("repeating-radial-gradient"))
            {
                value = value.Substring("repeating-radial-gradient".Length).Trim();
                PDFGradientRadialDescriptor radial;

                if (value.StartsWith("(") && value.EndsWith(")") && PDFGradientRadialDescriptor.TryParseRadial(value.Substring(1, value.Length - 2), out radial))
                {
                    radial.Repeating = true;
                    descriptor       = radial;
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 4
0
 public PDFGradientLinearBrush(PDFGradientLinearDescriptor descriptor) : base(descriptor)
 {
     this._descriptor = descriptor;
 }