Example #1
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
            {
                return new SvgAspectRatio();
            }

            if (!(value is string))
            {
                throw new ArgumentOutOfRangeException("value must be a string.");
            }

            SvgPreserveAspectRatio eAlign = SvgPreserveAspectRatio.none;
            bool bDefer = false;
            bool bSlice = false;

            string[] sParts = (value as string).Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            int nAlignIndex = 0;
            if (sParts[0].Equals("defer"))
            {
                bDefer = true;
                nAlignIndex++;
                if(sParts.Length < 2)
                    throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
            }

#if Net4
            if (!Enum.TryParse<SvgPreserveAspectRatio>(sParts[nAlignIndex], out eAlign))
                throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
#else
            eAlign = (SvgPreserveAspectRatio)Enum.Parse(typeof(SvgPreserveAspectRatio), sParts[nAlignIndex]);
#endif

            nAlignIndex++;

            if (sParts.Length > nAlignIndex)
            {
                switch (sParts[nAlignIndex])
                {
                    case "meet":
                        break;
                    case "slice":
                        bSlice = true;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
                }
            }
            nAlignIndex++;
            if(sParts.Length > nAlignIndex)
                throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");

            SvgAspectRatio pRet = new SvgAspectRatio(eAlign);
            pRet.Slice = bSlice;
            pRet.Defer = bDefer;
            return (pRet);
        }
        public SvgDocument GetDocument(string fileLocation, SvgAspectRatio aspectRatio)
        {
            SvgDocument svgDocument = GetDocument(fileLocation);

            if (aspectRatio != null)
            {
                svgDocument.AspectRatio = aspectRatio;
            }
            return(svgDocument);
        }
Example #3
0
        public static SKMatrix ToSKMatrix(this SvgViewBox svgViewBox, SvgAspectRatio svgAspectRatio, float x, float y, float width, float height)
        {
            if (svgViewBox.Equals(SvgViewBox.Empty))
            {
                return(SKMatrix.MakeTranslation(x, y));
            }

            float fScaleX = width / svgViewBox.Width;
            float fScaleY = height / svgViewBox.Height;
            float fMinX   = -svgViewBox.MinX * fScaleX;
            float fMinY   = -svgViewBox.MinY * fScaleY;

            if (svgAspectRatio == null)
            {
                svgAspectRatio = new SvgAspectRatio(SvgPreserveAspectRatio.xMidYMid);
            }

            if (svgAspectRatio.Align != SvgPreserveAspectRatio.none)
            {
                if (svgAspectRatio.Slice)
                {
                    fScaleX = Math.Max(fScaleX, fScaleY);
                    fScaleY = Math.Max(fScaleX, fScaleY);
                }
                else
                {
                    fScaleX = Math.Min(fScaleX, fScaleY);
                    fScaleY = Math.Min(fScaleX, fScaleY);
                }
                float fViewMidX = (svgViewBox.Width / 2) * fScaleX;
                float fViewMidY = (svgViewBox.Height / 2) * fScaleY;
                float fMidX     = width / 2;
                float fMidY     = height / 2;
                fMinX = -svgViewBox.MinX * fScaleX;
                fMinY = -svgViewBox.MinY * fScaleY;

                switch (svgAspectRatio.Align)
                {
                case SvgPreserveAspectRatio.xMinYMin:
                    break;

                case SvgPreserveAspectRatio.xMidYMin:
                    fMinX += fMidX - fViewMidX;
                    break;

                case SvgPreserveAspectRatio.xMaxYMin:
                    fMinX += width - svgViewBox.Width * fScaleX;
                    break;

                case SvgPreserveAspectRatio.xMinYMid:
                    fMinY += fMidY - fViewMidY;
                    break;

                case SvgPreserveAspectRatio.xMidYMid:
                    fMinX += fMidX - fViewMidX;
                    fMinY += fMidY - fViewMidY;
                    break;

                case SvgPreserveAspectRatio.xMaxYMid:
                    fMinX += width - svgViewBox.Width * fScaleX;
                    fMinY += fMidY - fViewMidY;
                    break;

                case SvgPreserveAspectRatio.xMinYMax:
                    fMinY += height - svgViewBox.Height * fScaleY;
                    break;

                case SvgPreserveAspectRatio.xMidYMax:
                    fMinX += fMidX - fViewMidX;
                    fMinY += height - svgViewBox.Height * fScaleY;
                    break;

                case SvgPreserveAspectRatio.xMaxYMax:
                    fMinX += width - svgViewBox.Width * fScaleX;
                    fMinY += height - svgViewBox.Height * fScaleY;
                    break;

                default:
                    break;
                }
            }

            var skMatrixTotal = SKMatrix.MakeIdentity();

            var skMatrixXY = SKMatrix.MakeTranslation(x, y);

            SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixXY);

            var skMatrixMinXY = SKMatrix.MakeTranslation(fMinX, fMinY);

            SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixMinXY);

            var skMatrixScale = SKMatrix.MakeScale(fScaleX, fScaleY);

            SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixScale);

            return(skMatrixTotal);
        }
Example #4
0
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
            {
                return(new SvgAspectRatio());
            }

            if (!(value is string))
            {
                throw new ArgumentOutOfRangeException("value must be a string.");
            }

            SvgPreserveAspectRatio eAlign = SvgPreserveAspectRatio.none;
            bool bDefer = false;
            bool bSlice = false;

            string[] sParts      = (value as string).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int      nAlignIndex = 0;

            if (sParts[0].Equals("defer"))
            {
                bDefer = true;
                nAlignIndex++;
                if (sParts.Length < 2)
                {
                    throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
                }
            }

#if Net4
            if (!Enum.TryParse <SvgPreserveAspectRatio>(sParts[nAlignIndex], out eAlign))
            {
                throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
            }
#else
            eAlign = (SvgPreserveAspectRatio)Enum.Parse(typeof(SvgPreserveAspectRatio), sParts[nAlignIndex]);
#endif

            nAlignIndex++;

            if (sParts.Length > nAlignIndex)
            {
                switch (sParts[nAlignIndex])
                {
                case "meet":
                    break;

                case "slice":
                    bSlice = true;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
                }
            }
            nAlignIndex++;
            if (sParts.Length > nAlignIndex)
            {
                throw new ArgumentOutOfRangeException("value is not a member of SvgPreserveAspectRatio");
            }

            SvgAspectRatio pRet = new SvgAspectRatio(eAlign);
            pRet.Slice = bSlice;
            pRet.Defer = bDefer;
            return(pRet);
        }