Exemple #1
0
        private void ReadStyle(Dictionary <string, string> _dictionary)
        {
            if (_dictionary == null || _dictionary.Count == 0)
            {
                return;
            }

            if (_dictionary.ContainsKey("visibility"))
            {
                SetVisibility(_dictionary ["visibility"]);
            }
            if (_dictionary.ContainsKey("display"))
            {
                SetDisplay(_dictionary ["display"]);
            }
            if (_dictionary.ContainsKey("overflow"))
            {
                SetOverflow(_dictionary ["overflow"]);
            }
            if (_dictionary.ContainsKey("clip-rule"))
            {
                SetClipRule(_dictionary ["clip-rule"]);
            }
            if (_dictionary.ContainsKey("clip-path"))
            {
                ReadClipPath(_dictionary ["clip-path"]);
            }
            if (_dictionary.ContainsKey("fill"))
            {
                string fillValue = _dictionary ["fill"];
                if (fillValue.IndexOf("url") >= 0)
                {
                    _gradientID = SVGStringExtractor.ExtractUrl(fillValue);
                }
                else
                {
                    _fillColor = new SVGColor(_dictionary ["fill"]);
                }
            }
            if (_dictionary.ContainsKey("opacity"))
            {
                _opacity *= new SVGLength(_dictionary ["opacity"]).value;
            }
            if (_dictionary.ContainsKey("fill-opacity"))
            {
                _fillOpacity *= new SVGLength(_dictionary ["fill-opacity"]).value;
            }
            if (_dictionary.ContainsKey("stroke-opacity"))
            {
                _strokeOpacity *= new SVGLength(_dictionary ["stroke-opacity"]).value;
            }
            if (_dictionary.ContainsKey("fill-rule"))
            {
                SetFillRule(_dictionary["fill-rule"]);
            }
            if (_dictionary.ContainsKey("stroke"))
            {
                _strokeColor = new SVGColor(_dictionary ["stroke"]);
            }
            if (_dictionary.ContainsKey("stroke-width"))
            {
                this.isStrokeWidth = true;
                _strokeWidth       = new SVGLength(_dictionary ["stroke-width"]);
            }
            if (_dictionary.ContainsKey("stroke-linecap"))
            {
                SetStrokeLineCap(_dictionary ["stroke-linecap"]);
            }
            if (_dictionary.ContainsKey("stroke-linejoin"))
            {
                SetStrokeLineJoin(_dictionary ["stroke-linejoin"]);
            }
            if (_dictionary.ContainsKey("stroke-miterlimit"))
            {
                _miterLimit = new SVGLength(_dictionary["stroke-miterlimit"]);
            }
            if (_dictionary.ContainsKey("stroke-dasharray"))
            {
                SetDashArray(_dictionary["stroke-dasharray"].Split(','));
            }
            if (_dictionary.ContainsKey("stroke-dashoffset"))
            {
                _dashOfset = new SVGLength(_dictionary["stroke-dashoffset"]);
            }
        }
 public SVGTransformList(string listString)
 {
     _listTransform = SVGStringExtractor.ExtractTransformList(listString);
 }
Exemple #3
0
        public static SVGMatrix GetViewBoxTransform(AttributeList attributeList, ref Rect viewport, bool negotiate = false)
        {
            SVGMatrix matrix = new SVGMatrix();

            float x = 0.0f;
            float y = 0.0f;
            float w = 0.0f;
            float h = 0.0f;

            string preserveAspectRatio = attributeList.GetValue("preserveAspectRatio");
            string viewBox             = attributeList.GetValue("viewBox");

            if (!string.IsNullOrEmpty(viewBox))
            {
                string[] viewBoxValues = SVGStringExtractor.ExtractTransformValue(viewBox);
                if (viewBoxValues.Length == 4)
                {
                    Rect contentRect = new Rect(
                        new SVGLength(viewBoxValues[0]).value,
                        new SVGLength(viewBoxValues[1]).value,
                        new SVGLength(viewBoxValues[2]).value,
                        new SVGLength(viewBoxValues[3]).value
                        );

                    SVGViewport.Align       align       = SVGViewport.Align.xMidYMid;
                    SVGViewport.MeetOrSlice meetOrSlice = SVGViewport.MeetOrSlice.Meet;

                    if (!string.IsNullOrEmpty(preserveAspectRatio))
                    {
                        string[] aspectRatioValues = SVGStringExtractor.ExtractStringArray(preserveAspectRatio);
                        align       = SVGViewport.GetAlignFromStrings(aspectRatioValues);
                        meetOrSlice = SVGViewport.GetMeetOrSliceFromStrings(aspectRatioValues);
                    }

                    Rect oldViewport = viewport;
                    viewport = SVGViewport.GetViewport(viewport, contentRect, align, meetOrSlice);

                    float sizeX = 0f, sizeY = 0f;
                    if (oldViewport.size.x != 0f)
                    {
                        sizeX = viewport.size.x / oldViewport.size.x;
                    }
                    if (oldViewport.size.y != 0f)
                    {
                        sizeY = viewport.size.y / oldViewport.size.y;
                    }

                    matrix.ScaleNonUniform(sizeX, sizeY);
                    matrix = matrix.Translate(viewport.x - oldViewport.x, viewport.y - oldViewport.y);
                }
            }
            else
            {
                if (negotiate)
                {
                    string attrXString      = attributeList.GetValue("x");
                    string attrYString      = attributeList.GetValue("y");
                    string attrWidthString  = attributeList.GetValue("width");
                    string attrHeightString = attributeList.GetValue("height");

                    SVGLength attrX = new SVGLength(SVGLengthType.PX, 0f), attrY = new SVGLength(SVGLengthType.PX, 0f),
                              attrWidth = new SVGLength(SVGLengthType.PX, 1f), attrHeight = new SVGLength(SVGLengthType.PX, 1f);

                    if (!string.IsNullOrEmpty(attrXString))
                    {
                        attrX = new SVGLength(attrXString);
                    }
                    if (!string.IsNullOrEmpty(attrYString))
                    {
                        attrY = new SVGLength(attrYString);
                    }
                    if (!string.IsNullOrEmpty(attrWidthString))
                    {
                        attrWidth = new SVGLength(attrWidthString);
                    }
                    if (!string.IsNullOrEmpty(attrHeightString))
                    {
                        attrHeight = new SVGLength(attrHeightString);
                    }


                    x = attrX.value;
                    y = attrY.value;
                    w = attrWidth.value;
                    h = attrHeight.value;

                    float x_ratio = 1f;
                    if (w != 0f)
                    {
                        x_ratio = attrWidth.value / w;
                    }

                    float y_ratio = 1f;
                    if (h != 0f)
                    {
                        y_ratio = attrHeight.value / h;
                    }

                    matrix   = matrix.ScaleNonUniform(x_ratio, y_ratio);
                    matrix   = matrix.Translate(x, y);
                    viewport = new Rect(x, y, w, h);

                    //                Debug.Log(string.Format("x: {0}, y: {1}, width: {2}, height: {3}, attrWidth: {4}, attrHeight: {5}", x, y, w, h, attrWidth, attrHeight));
                }
                //                Debug.Log(string.Format("x: {0}, y: {1}, width: {2}, height: {3}, attrWidth: {4}, attrHeight: {5}", x, y, w, h, attrWidth, attrHeight));
            }

            return(matrix);
        }
Exemple #4
0
        public static SVGMatrix GetRootViewBoxTransform(AttributeList attributeList, ref Rect viewport)
        {
            SVGMatrix matrix = new SVGMatrix();

            string attrXString      = attributeList.GetValue("x");
            string attrYString      = attributeList.GetValue("y");
            string attrWidthString  = attributeList.GetValue("width");
            string attrHeightString = attributeList.GetValue("height");

            SVGLength attrX = new SVGLength(SVGLengthType.PX, 0f), attrY = new SVGLength(SVGLengthType.PX, 0f),
                      attrWidth = new SVGLength(SVGLengthType.PX, 1f), attrHeight = new SVGLength(SVGLengthType.PX, 1f);

            if (!string.IsNullOrEmpty(attrXString))
            {
                attrX = new SVGLength(attrXString);
            }
            if (!string.IsNullOrEmpty(attrYString))
            {
                attrY = new SVGLength(attrYString);
            }
            if (!string.IsNullOrEmpty(attrWidthString))
            {
                attrWidth = new SVGLength(attrWidthString);
            }
            if (!string.IsNullOrEmpty(attrHeightString))
            {
                attrHeight = new SVGLength(attrHeightString);
            }

            string viewBox = attributeList.GetValue("viewBox");

            if (!string.IsNullOrEmpty(viewBox))
            {
                string[] _temp = SVGStringExtractor.ExtractTransformValue(viewBox);
                if (_temp.Length > 0)
                {
                    if (string.IsNullOrEmpty(attrXString))
                    {
                        attrX = new SVGLength(_temp [0]);
                    }
                }
                if (_temp.Length > 1)
                {
                    if (string.IsNullOrEmpty(attrYString))
                    {
                        attrY = new SVGLength(_temp [1]);
                    }
                }
                if (_temp.Length > 2)
                {
                    if (string.IsNullOrEmpty(attrWidthString))
                    {
                        attrWidth = new SVGLength(_temp [2]);
                    }
                }
                if (_temp.Length > 3)
                {
                    if (string.IsNullOrEmpty(attrHeightString))
                    {
                        attrHeight = new SVGLength(_temp [3]);
                    }
                }

                viewport = new Rect(attrX.value, attrY.value, attrWidth.value, attrHeight.value);

                if (string.IsNullOrEmpty(attrXString))
                {
                    viewport.x = attrX.value;
                }

                if (string.IsNullOrEmpty(attrYString))
                {
                    viewport.y = attrY.value;
                }

                if (string.IsNullOrEmpty(attrWidthString))
                {
                    viewport.width = attrWidth.value;
                }

                if (string.IsNullOrEmpty(attrHeightString))
                {
                    viewport.height = attrHeight.value;
                }
            }
            else
            {
                viewport = new Rect(attrX.value, attrY.value, attrWidth.value, attrHeight.value);
            }

            return(matrix);
        }
        private void Initial()
        {
            string     _d               = _attrList.GetValue("d");
            SVGPathSeg lastSegment      = null;
            SVGPathSeg firstPathSegment = null;

            List <char>   _charList  = new List <char>();
            List <string> _valueList = new List <string>();

            SVGStringExtractor.ExtractPathSegList(_d, ref _charList, ref _valueList);
            _segList = new SVGPathSegList(_charList.Count);
            int i, j, paramsLength;

            for (i = 0; i < _charList.Count; i++)
            {
                //lastSegment = _segList.GetLastItem();
                char    _char  = _charList [i];
                string  _value = _valueList [i];
                float[] parms  = SVGStringExtractor.ExtractTransformValueAsPX(_value);
                paramsLength = parms.Length;

                //Debug.Log(_char+" "+_value);

                switch (_char)
                {
                case 'Z':
                case 'z':
                    if (_segList.Count > 0 && firstPathSegment != null)
                    {
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoAbs(firstPathSegment.currentPoint.x, firstPathSegment.currentPoint.y, lastSegment));
                    }
                    _segList.AppendItem(CreateSVGPathSegClosePath());
                    firstPathSegment = null;
                    break;

                case 'M':
                    if (lastSegment != null && lastSegment.type != SVGPathSegTypes.Close && lastSegment.type != SVGPathSegTypes.MoveTo_Abs && lastSegment.type != SVGPathSegTypes.MoveTo_Rel)
                    {
                        firstPathSegment = null;
                    }
                    if (paramsLength < 2)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 2)
                    {
                        if (paramsLength - j < 2)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegMovetoAbs(parms [j], parms [j + 1], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'm':
                    if (lastSegment != null && lastSegment.type != SVGPathSegTypes.Close && lastSegment.type != SVGPathSegTypes.MoveTo_Abs && lastSegment.type != SVGPathSegTypes.MoveTo_Rel)
                    {
                        firstPathSegment = null;
                    }
                    if (paramsLength < 2)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 2)
                    {
                        if (paramsLength - j < 2)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegMovetoRel(parms [j], parms [j + 1], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'L':
                    if (paramsLength < 2)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 2)
                    {
                        if (paramsLength - j < 2)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoAbs(parms [j], parms [j + 1], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'l':
                    if (paramsLength < 2)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 2)
                    {
                        if (paramsLength - j < 2)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoRel(parms [j], parms [j + 1], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'C':
                    if (paramsLength < 6)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 6)
                    {
                        if (paramsLength - j < 6)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'c':
                    if (paramsLength < 6)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 6)
                    {
                        if (paramsLength - j < 6)
                        {
                            continue;
                        }
//                            Debug.Log(string.Format("CubicCurveRel: {0}, {1}, {2}, {3}, {4}, {5}", parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5]));
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'S':
                    if (paramsLength < 4)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 4)
                    {
                        if (paramsLength - j < 4)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicSmoothAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 's':
                    if (paramsLength < 4)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 4)
                    {
                        if (paramsLength - j < 4)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicSmoothRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'Q':
                    if (paramsLength < 4)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 4)
                    {
                        if (paramsLength - j < 4)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'q':
                    if (paramsLength < 4)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 4)
                    {
                        if (paramsLength - j < 4)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'T':
                    if (paramsLength < 2)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 2)
                    {
                        if (paramsLength - j < 2)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticSmoothAbs(parms [j], parms [j + 1], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 't':
                    if (paramsLength < 2)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 2)
                    {
                        if (paramsLength - j < 2)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticSmoothRel(parms [j], parms [j + 1], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'A':
                    if (paramsLength < 7)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 7)
                    {
                        if (paramsLength - j < 7)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegArcAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3] == 1f, parms [j + 4] == 1f, parms [j + 5], parms [j + 6], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'a':
                    if (paramsLength < 7)
                    {
                        continue;
                    }
                    for (j = 0; j < paramsLength; j += 7)
                    {
                        if (paramsLength - j < 7)
                        {
                            continue;
                        }
                        lastSegment = _segList.AppendItem(new SVGPathSegArcRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3] == 1f, parms [j + 4] == 1f, parms [j + 5], parms [j + 6], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'H':
                    for (j = 0; j < paramsLength; j++)
                    {
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoHorizontalAbs(parms [j], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'h':
                    for (j = 0; j < paramsLength; j++)
                    {
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoHorizontalRel(parms [j], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'V':
                    for (j = 0; j < paramsLength; j++)
                    {
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoVerticalAbs(parms [j], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;

                case 'v':
                    for (j = 0; j < paramsLength; j++)
                    {
                        lastSegment = _segList.AppendItem(new SVGPathSegLinetoVerticalRel(parms [j], lastSegment));
                        if (firstPathSegment == null)
                        {
                            firstPathSegment = lastSegment;
                        }
                    }
                    break;
                }
            }
        }
Exemple #6
0
    public SVGTransform(string strKey, string strValue)
    {
        string[] valuesStr = SVGStringExtractor.ExtractTransformValue(strValue);
        int      len       = valuesStr.Length;

        float[] values = new float[len];

        for (int i = 0; i < len; i++)
        {
            values.SetValue(float.Parse(valuesStr[i], CultureInfo.InvariantCulture), i);
        }
        switch (strKey)
        {
        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 Matrix2x3(
                    values[0],
                    values[1],
                    values[2],
                    values[3],
                    values[4],
                    values[5]
                    ));
            break;

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