ParseToFloat() public static méthode

public static ParseToFloat ( string str ) : float
str string
Résultat float
Exemple #1
0
        private double getViewportProp(string propertyName, string inValue, double calcParentVP, double defaultValue, SvgLengthDirection dir)
        {
            double ret;

            inValue = inValue.Trim();

            if (inValue.Length > 0)
            {
                if (inValue.EndsWith("%"))
                {
                    double perc = SvgNumber.ParseToFloat(inValue.Substring(0, inValue.Length - 1)) / 100;
                    ret = calcParentVP * perc;
                }
                else
                {
                    ret = new SvgLength(this, propertyName, dir, inValue, String.Empty).Value;
                }
            }
            else
            {
                ret = defaultValue;
            }

            return(ret);
        }
Exemple #2
0
        public static float ParseToFloat(string str)
        {
            float val;
            int   index = str.IndexOfAny(new Char[] { 'E', 'e' });

            if (index > -1)
            {
                float number = SvgNumber.ParseToFloat(str.Substring(0, index));
                float power  = SvgNumber.ParseToFloat(str.Substring(index + 1));

                val = (float)Math.Pow(10, power) * number;
            }
            else
            {
                try
                {
                    val = Single.Parse(str, SvgNumber.Format);
                }
                catch (Exception e)
                {
                    throw new DomException(DomExceptionType.SyntaxErr, "Input string was not in a correct format: " + str, e);
                }
            }
            return(val);
        }
Exemple #3
0
 public static string ScientificToDec(string sc)
 {
     if (sc.IndexOfAny(new char[] { 'e', 'E' }) > -1)
     {
         sc = sc.Trim();
         // remove the unit
         Match match = reUnit.Match(sc);
         return(SvgNumber.ParseToFloat(sc.Substring(0, sc.Length - match.Length)).ToString(Format) + match.Value);
     }
     else
     {
         return(sc);
     }
 }
Exemple #4
0
        public SvgRect(string str)
        {
            string replacedStr = Regex.Replace(str, @"(\s|,)+", ",");

            string[] tokens = replacedStr.Split(new char[] { ',' });
            if (tokens.Length == 4)
            {
                this.x      = SvgNumber.ParseToFloat(tokens[0]);
                this.y      = SvgNumber.ParseToFloat(tokens[1]);
                this.width  = SvgNumber.ParseToFloat(tokens[2]);
                this.height = SvgNumber.ParseToFloat(tokens[3]);
            }
            else
            {
                throw new SvgException(SvgExceptionType.SvgInvalidValueErr, "Invalid SvgRect value: " + str);
            }
        }
Exemple #5
0
        private float[] getCoords(String segment)
        {
            float[] coords = new float[0];

            segment = segment.Substring(1);
            segment = segment.Trim();
            segment = segment.Trim(new char[] { ',' });

            if (segment.Length > 0)
            {
                string[] sCoords = coordSplit.Split(segment);

                coords = new float[sCoords.Length];
                for (int i = 0; i < sCoords.Length; i++)
                {
                    coords[i] = SvgNumber.ParseToFloat(sCoords[i]);
                }
            }
            return(coords);
        }
 public SvgAnimatedNumber(string str)
 {
     baseVal = SvgNumber.ParseToFloat(str);
     animVal = baseVal;
 }
Exemple #7
0
 public SvgNumber(string str)
 {
     _value = SvgNumber.ParseToFloat(str);
 }
Exemple #8
0
        public void FromString(string listString)
        {
            // remove existing list items
            Clear();

            if (listString != null)
            {
                int len = listString.Length; // temp

                if (len > 0)
                {
                    int  p = 0;             // pos
                    char c;                 // temp
                    int  sNum      = -1;    // start of the number
                    int  eNum      = -1;    // end of the number
                    bool seenComma = false; // to handle 123,,123
                    int  tempSNum  = -1;    // start of the number (held in temp until two numbers are found)
                    int  tempENum  = -1;    // end of the number (held in temp until two numbers are found)

                    // This used to be a regex-- it is *much* faster this way
                    while (p < len)
                    {
                        // Get the char in a temp
                        c = listString[p];

                        // TODO: worry about NEL?
                        if ((c == '\t') || (c == '\r') || (c == '\n') || (c == 0x20) || (c == ','))
                        {
                            // Special handling for two commas
                            if (c == ',')
                            {
                                if (seenComma && sNum < 0)
                                {
                                    throw new SvgException(SvgExceptionType.SvgInvalidValueErr);
                                }
                                seenComma = true;
                            }

                            // Are we in a number?
                            if (sNum >= 0)
                            {
                                // The end of the number is the previous char
                                eNum = p - 1;

                                // Is this the x or y?
                                if (tempSNum == -1)
                                {
                                    // must be the x, hang onto it for a second
                                    tempSNum = sNum;
                                    tempENum = eNum;
                                }
                                else
                                {
                                    // must be the y, use temp as x and append the item
                                    AppendItem(new SvgPoint((double)SvgNumber.ParseToFloat(listString.Substring(tempSNum, (tempENum - tempSNum) + 1)),
                                                            (double)SvgNumber.ParseToFloat(listString.Substring(sNum, (eNum - sNum) + 1))));
                                    tempSNum = -1;
                                    tempENum = -1;
                                }

                                // Reset
                                sNum      = -1;
                                eNum      = -1;
                                seenComma = false;
                            }
                        }
                        else if (sNum == -1)
                        {
                            sNum = p;
                        }
                        // OPTIMIZE: Right here we could check for [Ee] to save some time in IndexOfAny later

                        // Move to next char
                        p++;
                    }

                    // We need to handle the end of the buffer as a delimiter
                    if (sNum >= 0)
                    {
                        if (tempSNum == -1)
                        {
                            throw new SvgException(SvgExceptionType.SvgInvalidValueErr);
                        }

                        // The end of the number is the previous char
                        eNum = p - 1;
                        // must be the y, use temp as x and append the item
                        AppendItem(new SvgPoint((double)SvgNumber.ParseToFloat(listString.Substring(tempSNum, (tempENum - tempSNum) + 1)),
                                                (double)SvgNumber.ParseToFloat(listString.Substring(sNum, (eNum - sNum) + 1))));
                    }
                    else if (tempSNum != -1)
                    {
                        throw new SvgException(SvgExceptionType.SvgInvalidValueErr);
                    }
                }
            }
        }
Exemple #9
0
        protected virtual void GetGraphicsPath(ref PointF ctp)
        {
            gp = new GraphicsPath();

            if (this is SvgTextPositioningElement)
            {
                SvgTextPositioningElement tpElm = (SvgTextPositioningElement)this;
                ctp = this.GetCurrentTextPosition(tpElm, ctp);
            }
            string sBaselineShift = GetPropertyValue("baseline-shift").Trim();
            double shiftBy        = 0;

            if (sBaselineShift.Length > 0)
            {
                SvgTextElement textElement = this as SvgTextElement;
                if (textElement == null)
                {
                    textElement = (SvgTextElement)this.SelectSingleNode("ancestor::svg:text", this.OwnerDocument.NamespaceManager);
                }

                float textFontSize = textElement._getComputedFontSize();
                if (sBaselineShift.EndsWith("%"))
                {
                    shiftBy = SvgNumber.ParseToFloat(sBaselineShift.Substring(0, sBaselineShift.Length - 1)) / 100 * textFontSize;
                }
                else if (sBaselineShift == "sub")
                {
                    shiftBy = -0.6F * textFontSize;
                }
                else if (sBaselineShift == "super")
                {
                    shiftBy = 0.6F * textFontSize;
                }
                else if (sBaselineShift == "baseline")
                {
                    shiftBy = 0;
                }
                else
                {
                    shiftBy = SvgNumber.ParseToFloat(sBaselineShift);
                }
            }


            foreach (XmlNode child in this.ChildNodes)
            {
                gp.StartFigure();
                if (child.NodeType == XmlNodeType.Text)
                {
                    ctp.Y -= (float)shiftBy;
                    this.AddGraphicsPath(ref ctp, GetText(child));
                    ctp.Y += (float)shiftBy;
                }
                else if (child is SvgTRefElement)
                {
                    SvgTRefElement trChild = (SvgTRefElement)child;
                    trChild.GetGraphicsPath(ref ctp);
                }
                else if (child is SvgTextContentElement)
                {
                    SvgTextContentElement tcChild = (SvgTextContentElement)child;
                    tcChild.GetGraphicsPath(ref ctp);
                }
            }
        }
Exemple #10
0
        public SvgTransform(string str)
        {
            int    start      = str.IndexOf("(");
            string type       = str.Substring(0, start);
            string valuesList = (str.Substring(start + 1, str.Length - start - 2)).Trim();           //JR added trim
            Regex  re         = new Regex("[\\s\\,]+");

            valuesList = re.Replace(valuesList, ",");
            string[] valuesStr = valuesList.Split(new char[] { ',' });
            int      len       = valuesStr.GetLength(0);

            float[] values = new float[len];

            for (int i = 0; i < len; i++)
            {
                values.SetValue(SvgNumber.ParseToFloat(valuesStr[i]), i);
            }

            switch (type)
            {
            case "translate":
                switch (len)
                {
                case 1:
                    SetTranslate(values[0], 0);
                    break;

                case 2:
                    SetTranslate(values[0], values[1]);
                    break;

                default:
                    throw new ApplicationException("Wrong number of arguments in translate transform");
                }
                break;

            case "rotate":
                switch (len)
                {
                case 1:
                    SetRotate(values[0]);
                    break;

                case 3:
                    SetRotate(values[0], values[1], values[2]);
                    break;

                default:
                    throw new ApplicationException("Wrong number of arguments in rotate transform");
                }
                break;

            case "scale":
                switch (len)
                {
                case 1:
                    SetScale(values[0], values[0]);
                    break;

                case 2:
                    SetScale(values[0], values[1]);
                    break;

                default:
                    throw new ApplicationException("Wrong number of arguments in scale transform");
                }
                break;

            case "skewX":
                if (len != 1)
                {
                    throw new ApplicationException("Wrong number of arguments in skewX transform");
                }
                SetSkewX(values[0]);
                break;

            case "skewY":
                if (len != 1)
                {
                    throw new ApplicationException("Wrong number of arguments in skewY transform");
                }
                SetSkewY(values[0]);
                break;

            case "matrix":
                if (len != 6)
                {
                    throw new ApplicationException("Wrong number of arguments in matrix transform");
                }
                SetMatrix(
                    new SvgMatrix(
                        values[0],
                        values[1],
                        values[2],
                        values[3],
                        values[4],
                        values[5]
                        ));
                break;

            default:
                this.type = (short)SvgTransformType.Unknown;
                break;
            }
        }
        public SvgTransform(string str)
        {
            int    start      = str.IndexOf("(", StringComparison.OrdinalIgnoreCase);
            string type       = str.Substring(0, start);
            string valuesList = (str.Substring(start + 1, str.Length - start - 2)).Trim();           //JR added trim
            Regex  re         = new Regex("[\\s\\,]+");

            valuesList = re.Replace(valuesList, ",");

            string[] valuesStr = valuesList.Split(new char[] { ',' });
            int      len       = valuesStr.GetLength(0);

            double[] values = new double[len];

            try
            {
                for (int i = 0; i < len; i++)
                {
                    values[i] = SvgNumber.ParseNumber(valuesStr[i]);
                }
            }
            catch
            {
                List <double> valueList = new List <double>();
                foreach (Match m in SvgNumber.DoubleRegex.Matches(str))
                {
                    if (!string.IsNullOrEmpty(m.Value))
                    {
                        valueList.Add(SvgNumber.ParseToFloat(m.Value));
                    }
                }

                values = valueList.ToArray();
                len    = values.Length;
            }

            _values = values;

            switch (type.Trim())
            {
            case "translate":
                switch (len)
                {
                case 1:
                    SetTranslate(values[0], 0);
                    break;

                case 2:
                    SetTranslate(values[0], values[1]);
                    break;

                default:
                    throw new ApplicationException("Wrong number of arguments in translate transform");
                }
                break;

            case "rotate":
                switch (len)
                {
                case 1:
                    SetRotate(values[0]);
                    break;

                case 3:
                    SetRotate(values[0], values[1], values[2]);
                    break;

                default:
                    throw new ApplicationException("Wrong number of arguments in rotate transform");
                }
                break;

            case "scale":
                switch (len)
                {
                case 1:
                    SetScale(values[0], values[0]);
                    break;

                case 2:
                    SetScale(values[0], values[1]);
                    break;

                default:
                    throw new ApplicationException("Wrong number of arguments in scale transform");
                }
                break;

            case "skewX":
                if (len != 1)
                {
                    throw new ApplicationException("Wrong number of arguments in skewX transform");
                }
                SetSkewX(values[0]);
                break;

            case "skewY":
                if (len != 1)
                {
                    throw new ApplicationException("Wrong number of arguments in skewY transform");
                }
                SetSkewY(values[0]);
                break;

            case "matrix":
                if (len != 6)
                {
                    throw new ApplicationException("Wrong number of arguments in matrix transform");
                }
                SetMatrix(new SvgMatrix(values[0], values[1], values[2], values[3], values[4], values[5]));
                break;

            default:
                _type = SvgTransformType.Unknown;
                break;
            }
        }