コード例 #1
0
ファイル: SvgUseElement.cs プロジェクト: lallous/SvgForXaml
		internal SvgUseElement(INode parent, XmlElement element)
			: base(parent, element)
		{
			this._stylableHelper = new SvgStylableHelper(this, element);
			this._transformableHelper = new SvgTransformableHelper(element);

			var nan = new SvgLength(SvgLength.SvgLengthType.Unknown, float.NaN);
			this.X = element.ParseCoordinate("x", nan);
			this.Y = element.ParseCoordinate("y", nan);
			this.Width = element.ParseLength("width", nan);
			this.Height = element.ParseLength("height", nan);
			this.Href = element.GetAttribute("xlink:href").Substring(1);
			
			var child = (SvgElement)this.OwnerDocument.GetElementById(this.Href).CloneNode(true);
			if (child.GetType() == typeof(SvgSymbolElement))
			{
				throw new NotImplementedException();
			}
			else if (child.GetType() == typeof(SvgRectElement))
			{
				var casted = (SvgRectElement)child;
				if (this.Width.UnitType != SvgLength.SvgLengthType.Unknown) casted.Width = this.Width;
				if (this.Height.UnitType != SvgLength.SvgLengthType.Unknown) casted.Height = this.Height;
			}
			this.InstanceRoot = child;

			if (this.X.UnitType != SvgLength.SvgLengthType.Unknown && this.Y.UnitType != SvgLength.SvgLengthType.Unknown)
			{
				this.Transform.Add(SvgTransform.CreateTranslate(
					this.X.UnitType != SvgLength.SvgLengthType.Unknown ? this.X.ValueAsPixel : 0.0F,
					this.Y.UnitType != SvgLength.SvgLengthType.Unknown ? this.Y.ValueAsPixel : 0.0F));
			}
		}
コード例 #2
0
ファイル: SvgHelper.cs プロジェクト: lallous/SvgForXaml
		public static SvgLength ParseLength(this XmlElement element, string attributeName, SvgLength defaultValue)
		{
			var attribute = element.GetAttribute(attributeName);
			if (string.IsNullOrEmpty(attribute)) return defaultValue;

			return element.ParseLength(attributeName);
		}
コード例 #3
0
		public float ConvertX(SvgLength length)
		{
			if (length.UnitType == SvgLength.SvgLengthType.Percentage)
			{
				return (float)this.CanvasSize.X * length.Value / 100.0F;
			}
			return this.Convert(length);
		}
コード例 #4
0
ファイル: SvgHelper.cs プロジェクト: lallous/SvgForXaml
		public static bool TryParseLength(this XmlElement element, string attributeName, out SvgLength result)
		{
			result = 0.0F;

			var attribute = element.GetAttribute(attributeName);
			if (string.IsNullOrEmpty(attribute)) return false;

			var lengthText = element.GetAttribute(attributeName);
			return SvgLength.TryParse(lengthText, true, out result);
		}
コード例 #5
0
		public float Convert(SvgLength length)
		{
			switch (length.UnitType)
			{
				case SvgLength.SvgLengthType.Percentage:
				case SvgLength.SvgLengthType.Ems:
				case SvgLength.SvgLengthType.Exs:
					throw new NotSupportedException();
			}
			return length.ValueAsPixel;
		}
コード例 #6
0
ファイル: SvgLengthTests.cs プロジェクト: nagyist/savagesvg
        public void TestPercentValues()
        {
            length = new SvgLength(elm, "test", SvgLengthDirection.Horizontal, "10%");
            Assert.AreEqual(20, length.Value);
            Assert.AreEqual(SvgLengthType.Percentage, length.UnitType);
            Assert.AreEqual(10, length.ValueInSpecifiedUnits);
            Assert.AreEqual("10%", length.ValueAsString);

            length = new SvgLength(elm, "test", SvgLengthDirection.Vertical, "20%");
            Assert.AreEqual(60, length.Value);

            length = new SvgLength(elm, "test", SvgLengthDirection.Viewport, "10%");
            Assert.AreEqual(Math.Sqrt(200 * 200 + 300 * 300) / Math.Sqrt(2) * 0.1D, length.Value);
        }
コード例 #7
0
        private double GetStrokeWidth()
        {
            string strokeWidth = _element.GetPropertyValue("stroke-width");

            if (strokeWidth.Length == 0)
            {
                strokeWidth = "1px";
            }

            SvgLength strokeWidthLength = new SvgLength(_element, "stroke-width",
                                                        SvgLengthDirection.Viewport, strokeWidth);

            return(strokeWidthLength.Value);
        }
コード例 #8
0
        private float GetComputedFontSize(SvgTextContentElement element)
        {
            string str      = element.GetPropertyValue("font-size");
            float  fontSize = 12;

            if (_decimalNumber.IsMatch(str))
            {
                // svg length
                var fontLength = new SvgLength(element, "font-size", SvgLengthDirection.Viewport, str, "10px");
                fontSize = (float)fontLength.Value;
            }

            return(fontSize);
        }
コード例 #9
0
ファイル: WpfSvgPaint.cs プロジェクト: zellus/SharpVectors
        private double GetDashOffset(double strokeWidth)
        {
            string dashOffset = _element.GetPropertyValue("stroke-dashoffset");

            if (dashOffset.Length > 0)
            {
                //divide by strokeWidth to take care of the difference between Svg and GDI+
                SvgLength dashOffsetLength = new SvgLength(_element, "stroke-dashoffset",
                                                           SvgLengthDirection.Viewport, dashOffset);
                return(dashOffsetLength.Value);
            }

            return(0);
        }
コード例 #10
0
        public static double GetComputedFontSize(SvgTextContentElement element)
        {
            string str      = element.GetPropertyValue(CssConstants.PropFontSize);
            double fontSize = 12;

            if (_decimalNumber.IsMatch(str))
            {
                // svg length
                var fontLength = new SvgLength(element, CssConstants.PropFontSize, SvgLengthDirection.Viewport, str, "10px");
                fontSize = fontLength.Value;
            }

            return(fontSize);
        }
コード例 #11
0
        public static double GetComputedFontSize(SvgTextContentElement element)
        {
            string str      = element.GetPropertyValue("font-size");
            double fontSize = 12;

            if (_decimalNumber.IsMatch(str))
            {
                // svg length
                fontSize = new SvgLength(element, "font-size",
                                         SvgLengthDirection.Viewport, str, "10px").Value;
            }

            return(fontSize);
        }
コード例 #12
0
ファイル: WpfShapeHelper.cs プロジェクト: zellus/SharpVectors
        public static bool TryGetDashOffset(SvgStyleableElement element, out double offset)
        {
            string dashOffset = element.GetPropertyValue("stroke-dashoffset");

            if (dashOffset.Length > 0)
            {
                //divide by strokeWidth to take care of the difference between Svg and GDI+
                SvgLength dashOffsetLength = new SvgLength(element, "stroke-dashoffset",
                                                           SvgLengthDirection.Viewport, dashOffset);
                offset = dashOffsetLength.Value;
                return(true);
            }
            offset = 0;
            return(false);
        }
コード例 #13
0
ファイル: SvgHelper.cs プロジェクト: p69/SvgForXaml
        public static bool TryParseLength(this XmlElement element, string attributeName, out SvgLength result)
        {
            result = 0.0F;

            var attribute = element.GetAttribute(attributeName);

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

            var lengthText = element.GetAttribute(attributeName);

            return(SvgLength.TryParse(lengthText, true, out result));
        }
コード例 #14
0
ファイル: SvgLengthTests.cs プロジェクト: nagyist/savagesvg
        public void TestScientificValues()
        {
            length = new SvgLength(elm, "test", SvgLengthDirection.Horizontal, "2E3px");
            Assert.AreEqual(2000, length.Value);
            Assert.AreEqual(SvgLengthType.Px, length.UnitType);
            Assert.AreEqual(2000, length.ValueInSpecifiedUnits);
            Assert.AreEqual("2000px", length.ValueAsString);


            length = new SvgLength(elm, "test", SvgLengthDirection.Horizontal, "1E-1px");
            Assert.IsTrue(Math.Abs(0.1D - length.Value) < 0.0001, "Value");
            Assert.AreEqual(SvgLengthType.Px, length.UnitType);
            Assert.IsTrue(Math.Abs(0.1D - length.ValueInSpecifiedUnits) < 0.0001, "ValueInSpecifiedUnits");
            //Assert.AreEqual("0.1px", length.ValueAsString);
        }
コード例 #15
0
ファイル: WpfShapeHelper.cs プロジェクト: zellus/SharpVectors
        public static bool TryGetStrokeWidth(SvgStyleableElement element, out double strokeWidth)
        {
            string propValue = element.GetPropertyValue("stroke-width");

            if (string.IsNullOrWhiteSpace(propValue))
            {
                strokeWidth = 1d;
                return(false);
            }

            SvgLength strokeWidthLength = new SvgLength(element, "stroke-width", SvgLengthDirection.Viewport, propValue);

            strokeWidth = strokeWidthLength.Value;
            return(true);
        }
コード例 #16
0
ファイル: WpfShapeHelper.cs プロジェクト: zellus/SharpVectors
        private static double GetComputedLineHeight(SvgTextContentElement element)
        {
            string str        = element.GetPropertyValue("line-height");
            double lineHeight = 13;

            if (str.EndsWith("%", StringComparison.Ordinal))
            {
                // percentage of inherited value
            }
            else if (_decimalNumber.IsMatch(str))
            {
                // svg length
                lineHeight = new SvgLength(element, "line-height",
                                           SvgLengthDirection.Viewport, str, "13px").Value;
            }

            return(lineHeight);
        }
コード例 #17
0
        protected double CalcLengthUnit(SvgLength length, SvgLengthDirection dir, Rect bounds)
        {
            double calcValue = length.ValueInSpecifiedUnits;

            if (dir == SvgLengthDirection.Horizontal)
            {
                calcValue *= bounds.Width;
            }
            else
            {
                calcValue *= bounds.Height;
            }
            if (length.UnitType == SvgLengthType.Percentage)
            {
                calcValue /= 100F;
            }

            return(calcValue);
        }
コード例 #18
0
ファイル: LoaderTests.cs プロジェクト: dswisher/swish-svg
        public void CanLoadCircleAttributes()
        {
            // Arrange
            var cx = new SvgLength(6.2f, SvgUnit.Inch);
            var cy = new SvgLength(316, SvgUnit.User);
            var r  = new SvgLength(54, SvgUnit.Inch);

            var xml = ElementBuilder.Create("circle")
                      .AddAttribute("cx", cx.ToString())
                      .AddAttribute("cy", cy.ToString())
                      .AddAttribute("r", r.ToString());

            // Act
            var circle = Svg.FromString <SvgCircleElement>(xml.ToString());

            // Assert
            circle.CX.Should().Be(cx);
            circle.CY.Should().Be(cy);
            circle.R.Should().Be(r);
        }
コード例 #19
0
ファイル: SvgUseElement.cs プロジェクト: p69/SvgForXaml
        internal SvgUseElement(INode parent, XmlElement element)
            : base(parent, element)
        {
            this._stylableHelper      = new SvgStylableHelper(this, element);
            this._transformableHelper = new SvgTransformableHelper(element);

            var nan = new SvgLength(SvgLength.SvgLengthType.Unknown, float.NaN);

            this.X      = element.ParseCoordinate("x", nan);
            this.Y      = element.ParseCoordinate("y", nan);
            this.Width  = element.ParseLength("width", nan);
            this.Height = element.ParseLength("height", nan);
            this.Href   = element.GetAttribute("xlink:href").Substring(1);

            var child = (SvgElement)this.OwnerDocument.GetElementById(this.Href).CloneNode(true);

            if (child.GetType() == typeof(SvgSymbolElement))
            {
                throw new NotImplementedException();
            }
            else if (child.GetType() == typeof(SvgRectElement))
            {
                var casted = (SvgRectElement)child;
                if (this.Width.UnitType != SvgLength.SvgLengthType.Unknown)
                {
                    casted.Width = this.Width;
                }
                if (this.Height.UnitType != SvgLength.SvgLengthType.Unknown)
                {
                    casted.Height = this.Height;
                }
            }
            this.InstanceRoot = child;

            if (this.X.UnitType != SvgLength.SvgLengthType.Unknown && this.Y.UnitType != SvgLength.SvgLengthType.Unknown)
            {
                this.Transform.Add(SvgTransform.CreateTranslate(
                                       this.X.UnitType != SvgLength.SvgLengthType.Unknown ? this.X.ValueAsPixel : 0.0F,
                                       this.Y.UnitType != SvgLength.SvgLengthType.Unknown ? this.Y.ValueAsPixel : 0.0F));
            }
        }
コード例 #20
0
        internal SvgUseElement(INode parent, XmlElement element)
            : base(parent, element)
        {
            this._stylableHelper      = new SvgStylableHelper(this, element);
            this._transformableHelper = new SvgTransformableHelper(element);

            var nan = new SvgLength(SvgLength.SvgLengthType.Unknown, float.NaN);

            this.X             = element.ParseCoordinate("x", nan);
            this.Y             = element.ParseCoordinate("y", nan);
            this.Width         = element.ParseLength("width", nan);
            this.Height        = element.ParseLength("height", nan);
            this.Href          = element.GetAttributeNS("http://www.w3.org/1999/xlink", "href").Substring(1);
            this._instanceRoot = new Lazy <SvgElement>(this.CreateInstanceRoot, true);

            if (this.X.UnitType != SvgLength.SvgLengthType.Unknown && this.Y.UnitType != SvgLength.SvgLengthType.Unknown)
            {
                this.Transform.Add(SvgTransform.CreateTranslate(
                                       this.X.UnitType != SvgLength.SvgLengthType.Unknown ? this.X.ValueAsPixel : 0.0F,
                                       this.Y.UnitType != SvgLength.SvgLengthType.Unknown ? this.Y.ValueAsPixel : 0.0F));
            }
        }
コード例 #21
0
ファイル: LoaderTests.cs プロジェクト: dswisher/swish-svg
        public void CanLoadLineAttributes()
        {
            // Arrange
            var x1 = new SvgLength(6.2f, SvgUnit.Inch);
            var y1 = new SvgLength(316, SvgUnit.User);
            var x2 = new SvgLength(54, SvgUnit.Inch);
            var y2 = new SvgLength(45, SvgUnit.Inch);

            var xml = ElementBuilder.Create("line")
                      .AddAttribute("x1", x1.ToString())
                      .AddAttribute("y1", y1.ToString())
                      .AddAttribute("x2", x2.ToString())
                      .AddAttribute("y2", y2.ToString());

            // Act
            var line = Svg.FromString <SvgLineElement>(xml.ToString());

            // Assert
            line.X1.Should().Be(x1);
            line.Y1.Should().Be(y1);
            line.X2.Should().Be(x2);
            line.Y2.Should().Be(y2);
        }
コード例 #22
0
ファイル: LoaderTests.cs プロジェクト: dswisher/swish-svg
        public void CanLoadEllipseAttributes()
        {
            // Arrange
            var cx = new SvgLength(6.2f, SvgUnit.Inch);
            var cy = new SvgLength(316, SvgUnit.User);
            var rx = new SvgLength(54, SvgUnit.Inch);
            var ry = new SvgLength(45, SvgUnit.Inch);

            var xml = ElementBuilder.Create("ellipse")
                      .AddAttribute("cx", cx.ToString())
                      .AddAttribute("cy", cy.ToString())
                      .AddAttribute("rx", rx.ToString())
                      .AddAttribute("ry", ry.ToString());

            // Act
            var ellipse = Svg.FromString <SvgEllipseElement>(xml.ToString());

            // Assert
            ellipse.CX.Should().Be(cx);
            ellipse.CY.Should().Be(cy);
            ellipse.RX.Should().Be(rx);
            ellipse.RY.Should().Be(ry);
        }
コード例 #23
0
        private float CalcPatternUnit(SvgLength length, SvgLengthDirection dir, RectangleF bounds)
        {
            if (_patternElement.PatternUnits.AnimVal.Equals((ushort)SvgUnitType.UserSpaceOnUse))
            {
                return((float)length.Value);
            }
            float calcValue = (float)length.ValueInSpecifiedUnits;

            if (dir == SvgLengthDirection.Horizontal)
            {
                calcValue *= bounds.Width;
            }
            else
            {
                calcValue *= bounds.Height;
            }
            if (length.UnitType == SvgLengthType.Percentage)
            {
                calcValue /= 100F;
            }

            return(calcValue);
        }
コード例 #24
0
        private double CalcPatternUnit(SvgLength length, SvgLengthDirection dir, Rect bounds)
        {
            if (_patternElement.PatternUnits.AnimVal.Equals((ushort)SvgUnitType.UserSpaceOnUse))
            {
                _isUserSpace = true;
                return(length.Value);
            }
            double calcValue = length.ValueInSpecifiedUnits;

            if (dir == SvgLengthDirection.Horizontal)
            {
                calcValue *= bounds.Width;
            }
            else
            {
                calcValue *= bounds.Height;
            }
            if (length.UnitType == SvgLengthType.Percentage)
            {
                calcValue /= 100F;
            }

            return(calcValue);
        }
コード例 #25
0
        public void RenderMarker0(WpfDrawingRenderer renderer, WpfDrawingContext gr,
                                  SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            //PathGeometry g;
            //g.GetPointAtFractionLength(

            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
            SvgMarkerElement markerElm     = (SvgMarkerElement)_svgElement;

            SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
            int         start;
            int         len;

            // Choose which part of the position array to use
            switch (markerPos)
            {
            case SvgMarkerPosition.Start:
                start = 0;
                len   = 1;
                break;

            case SvgMarkerPosition.Mid:
                start = 1;
                len   = vertexPositions.Length - 2;
                break;

            default:
                // == MarkerPosition.End
                start = vertexPositions.Length - 1;
                len   = 1;
                break;
            }

            for (int i = start; i < start + len; i++)
            {
                SvgPointF point = vertexPositions[i];

                Matrix m = GetTransformMatrix(_svgElement);

                //GraphicsContainer gc = gr.BeginContainer();

                this.BeforeRender(renderer);

                //gr.TranslateTransform(point.X, point.Y);

                //PAUL:
                //m.Translate(point.X, point.Y);

                if (markerElm.OrientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle))
                {
                    m.Rotate(markerElm.OrientAngle.AnimVal.Value);
                    //gr.RotateTransform((double)markerElm.OrientAngle.AnimVal.Value);
                }
                else
                {
                    double angle;

                    switch (markerPos)
                    {
                    case SvgMarkerPosition.Start:
                        angle = markerHostElm.GetStartAngle(i + 1);
                        break;

                    case SvgMarkerPosition.Mid:
                        //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
                        angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
                        break;

                    default:
                        angle = markerHostElm.GetEndAngle(i);
                        break;
                    }
                    //gr.RotateTransform(angle);
                    m.Rotate(angle);
                }

                if (markerElm.MarkerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                {
                    string propValue = refElement.GetPropertyValue("stroke-width");
                    if (propValue.Length == 0)
                    {
                        propValue = "1";
                    }

                    SvgLength strokeWidthLength = new SvgLength("stroke-width", propValue, refElement, SvgLengthDirection.Viewport);
                    double    strokeWidth       = strokeWidthLength.Value;
                    //gr.ScaleTransform(strokeWidth, strokeWidth);
                    m.Scale(strokeWidth, strokeWidth);
                }

                SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
                double[] translateAndScale  = spar.FitToViewBox(
                    (SvgRect)markerElm.ViewBox.AnimVal, new SvgRect(0, 0,
                                                                    markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value));


                //PAUL:
                //m.Translate(-(double)markerElm.RefX.AnimVal.Value * translateAndScale[2], -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]);

                //PAUL:
                m.Scale(translateAndScale[2], translateAndScale[3]);
                m.Translate(point.X, point.Y);

                //Matrix oldTransform = TransformMatrix;
                //TransformMatrix = m;
                //try
                //{
                //newTransform.Append(m);
                //TransformGroup tg = new TransformGroup();

                //renderer.Canvas.re

                //gr.TranslateTransform(
                //    -(double)markerElm.RefX.AnimVal.Value * translateAndScale[2],
                //    -(double)markerElm.RefY.AnimVal.Value * translateAndScale[3]
                //    );

                //gr.ScaleTransform(translateAndScale[2], translateAndScale[3]);

                renderer.RenderChildren(markerElm);
                //                markerElm.RenderChildren(renderer);
                //}
                //finally
                //{
                //    TransformMatrix = oldTransform;
                //}
                //    //gr.EndContainer(gc);

                _matrix = m;
                this.Render(renderer);

                //gr.EndContainer(gc);

                this.AfterRender(renderer);
            }
        }
コード例 #26
0
        public void RenderMarker2(WpfDrawingRenderer renderer, WpfDrawingContext gr,
                                  SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
            SvgMarkerElement markerElm     = (SvgMarkerElement)_svgElement;

            SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
            int         start;
            int         len;

            // Choose which part of the position array to use
            switch (markerPos)
            {
            case SvgMarkerPosition.Start:
                start = 0;
                len   = 1;
                break;

            case SvgMarkerPosition.Mid:
                start = 1;
                len   = vertexPositions.Length - 2;
                break;

            default:
                // == MarkerPosition.End
                start = vertexPositions.Length - 1;
                len   = 1;
                break;
            }

            for (int i = start; i < start + len; i++)
            {
                SvgPointF point = vertexPositions[i];

                //GdiGraphicsContainer gc = gr.BeginContainer();

                this.BeforeRender(renderer);

                //Matrix matrix = Matrix.Identity;

                Matrix matrix = GetTransformMatrix(_svgElement);

                if (markerElm.OrientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle))
                {
                    matrix.Rotate(markerElm.OrientAngle.AnimVal.Value);
                }
                else
                {
                    double angle = 0;

                    switch (markerPos)
                    {
                    case SvgMarkerPosition.Start:
                        angle = markerHostElm.GetStartAngle(i + 1);
                        break;

                    case SvgMarkerPosition.Mid:
                        //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
                        angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
                        break;

                    default:
                        angle = markerHostElm.GetEndAngle(i);
                        break;
                    }
                    matrix.Rotate(angle);
                }

                if (markerElm.MarkerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                {
                    SvgLength strokeWidthLength = new SvgLength(refElement,
                                                                "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
                    double strokeWidth = strokeWidthLength.Value;
                    matrix.Scale(strokeWidth, strokeWidth);
                }

                SvgPreserveAspectRatio spar =
                    (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
                double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal,
                                                               new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value,
                                                                           markerElm.MarkerHeight.AnimVal.Value));


                matrix.Translate(-markerElm.RefX.AnimVal.Value * translateAndScale[2],
                                 -markerElm.RefY.AnimVal.Value * translateAndScale[3]);

                matrix.Scale(translateAndScale[2], translateAndScale[3]);

                matrix.Translate(point.X, point.Y);

                _matrix = matrix;
                this.Render(renderer);

                //Clip(gr);

                renderer.RenderChildren(markerElm);

                //gr.EndContainer(gc);

                this.AfterRender(renderer);
            }
        }
コード例 #27
0
        public SvgStopElement(SvgLength num, SvgColor col)
        {
            Offset = num;

            Style.Set("stop-color", col);
        }
コード例 #28
0
        public void RenderMarker(WpfDrawingRenderer renderer, WpfDrawingContext gr,
                                 SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;

            SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
            int         start           = 0;
            int         len             = 0;

            // Choose which part of the position array to use
            switch (markerPos)
            {
            case SvgMarkerPosition.Start:
                start = 0;
                len   = 1;
                break;

            case SvgMarkerPosition.Mid:
                start = 1;
                len   = vertexPositions.Length - 2;
                break;

            default:
                // == MarkerPosition.End
                start = vertexPositions.Length - 1;
                len   = 1;
                break;
            }
            int end = start + len;

            TransformGroup transform = new TransformGroup();

            for (int i = start; i < end; i++)
            {
                SvgPointF point = vertexPositions[i];

                //GdiGraphicsContainer gc = gr.BeginContainer();

                this.BeforeRender(renderer);

                //Matrix matrix = Matrix.Identity;

                Matrix matrix = GetTransformMatrix(_svgElement, transform);

                ISvgAnimatedEnumeration orientType = _markerElement.OrientType;

                if (orientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle))
                {
                    double scaleValue = _markerElement.OrientAngle.AnimVal.Value;
                    if (!scaleValue.Equals(0))
                    {
                        matrix.Rotate(scaleValue);
                        transform.Children.Add(new RotateTransform(scaleValue));
                    }
                }
                else
                {
                    bool isAutoReverse = orientType.AnimVal.Equals((ushort)SvgMarkerOrient.AutoStartReverse);

                    double angle = 0;
                    switch (markerPos)
                    {
                    case SvgMarkerPosition.Start:
                        angle = markerHostElm.GetStartAngle(i);
                        //angle = markerHostElm.GetStartAngle(i + 1);
                        if (vertexPositions.Length >= 2)
                        {
                            SvgPointF pMarkerPoint1 = vertexPositions[start];
                            SvgPointF pMarkerPoint2 = vertexPositions[end];
                            float     xDiff         = pMarkerPoint2.X - pMarkerPoint1.X;
                            float     yDiff         = pMarkerPoint2.Y - pMarkerPoint1.Y;
                            double    angleMarker   = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
                            if (!angleMarker.Equals(angle))
                            {
                                angle = angleMarker;
                            }
                        }
                        // A value of 'auto-start-reverse' means the same as 'auto' except that for a
                        // marker placed by 'marker-start', the orientation is 180° different from
                        // the orientation as determined by 'auto'.
                        if (isAutoReverse)
                        {
                            angle += 180;
                        }
                        break;

                    case SvgMarkerPosition.Mid:
                        //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
                        angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
                        break;

                    default:
                        angle = markerHostElm.GetEndAngle(i - 1);
                        //angle = markerHostElm.GetEndAngle(i);
                        if (vertexPositions.Length >= 2)
                        {
                            SvgPointF pMarkerPoint1 = vertexPositions[start - 1];
                            SvgPointF pMarkerPoint2 = vertexPositions[start];
                            float     xDiff         = pMarkerPoint2.X - pMarkerPoint1.X;
                            float     yDiff         = pMarkerPoint2.Y - pMarkerPoint1.Y;
                            double    angleMarker   = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
                            if (!angleMarker.Equals(angle))
                            {
                                angle = angleMarker;
                            }
                        }
                        break;
                    }
                    matrix.Rotate(angle);
                    transform.Children.Add(new RotateTransform(angle));
                }

                // 'viewBox' and 'preserveAspectRatio' attributes
                // viewBox -> viewport(0, 0, markerWidth, markerHeight)
                SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)_markerElement.PreserveAspectRatio.AnimVal;
                double[] translateAndScale  = spar.FitToViewBox((SvgRect)_markerElement.ViewBox.AnimVal,
                                                                new SvgRect(0, 0, _markerElement.MarkerWidth.AnimVal.Value, _markerElement.MarkerHeight.AnimVal.Value));

                // Warning at this time, refX and refY are relative to the painted element's coordinate system.
                // We need to move the reference point to the marker's coordinate system
                double refX = _markerElement.RefX.AnimVal.Value;
                double refY = _markerElement.RefY.AnimVal.Value;

                if (!(refX.Equals(0) && refY.Equals(0)))
                {
                    var ptRef = matrix.Transform(new Point(refX, refY));

                    refX = ptRef.X;
                    refY = ptRef.Y;

                    matrix.Translate(-refX, -refY);
                    transform.Children.Add(new TranslateTransform(-refX, -refY));
                }

                //matrix.Translate(-markerElm.RefX.AnimVal.Value * translateAndScale[2],
                //    -markerElm.RefY.AnimVal.Value * translateAndScale[3]);
                //transform.Children.Add(new TranslateTransform(-markerElm.RefX.AnimVal.Value * translateAndScale[2],
                //    -markerElm.RefY.AnimVal.Value * translateAndScale[3]));

                // compute an additional transform for 'strokeWidth' coordinate system
                ISvgAnimatedEnumeration markerUnits = _markerElement.MarkerUnits;
                if (markerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                {
                    SvgLength strokeWidthLength = new SvgLength(refElement,
                                                                "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
                    double strokeWidth = strokeWidthLength.Value;
                    if (!strokeWidth.Equals(1))
                    {
                        matrix.Scale(strokeWidth, strokeWidth);
                        transform.Children.Add(new ScaleTransform(strokeWidth, strokeWidth));
                    }
                }

                if (!(translateAndScale[2].Equals(1) && translateAndScale[3].Equals(1)))
                {
                    matrix.Scale(translateAndScale[2], translateAndScale[3]);
                    transform.Children.Add(new ScaleTransform(translateAndScale[2], translateAndScale[3]));
                }

                matrix.Translate(point.X, point.Y);
                transform.Children.Add(new TranslateTransform(point.X, point.Y));

                _matrix = matrix;

                this.Transform = transform;

                this.Render(renderer);

                //Clip(gr);

                renderer.RenderChildren(_markerElement);

                //gr.EndContainer(gc);

                this.AfterRender(renderer);
            }
        }
コード例 #29
0
ファイル: SvgHelper.cs プロジェクト: lallous/SvgForXaml
		public static SvgLength ParseCoordinate(this XmlElement element, string attributeName, SvgLength defaultValue) => element.ParseLength(attributeName, defaultValue);
コード例 #30
0
 public SvgUseElement(SvgLength x, SvgLength y, SvgXRef xref)
 {
     XRef = xref;
     X    = x;
     Y    = y;
 }
コード例 #31
0
 public SvgImageElement(SvgLength x, SvgLength y, string href)
 {
     this.Href = href;
     this.X    = x;
     this.Y    = y;
 }
コード例 #32
0
 public SvgPatternElement(SvgLength width, SvgLength height, SvgNumList vport)
 {
     Width   = width;
     Height  = height;
     ViewBox = vport;
 }
コード例 #33
0
 public SvgSvgElement(SvgLength width, SvgLength height)
 {
     Width  = width;
     Height = height;
 }
コード例 #34
0
 public SvgUseElement(SvgLength x, SvgLength y, string href)
 {
     Href = href;
     X    = x;
     Y    = y;
 }
コード例 #35
0
        public void PaintMarker(GdiGraphicsRenderer renderer, GdiGraphics gr,
                                SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
            SvgMarkerElement markerElm     = (SvgMarkerElement)_svgElement;

            SvgPointF[] vertexPositions = markerHostElm.MarkerPositions;
            if (vertexPositions == null)
            {
                return;
            }
            var comparer = StringComparison.OrdinalIgnoreCase;

            bool mayHaveCurves = markerHostElm.MayHaveCurves;
            int  start;
            int  len;

            // Choose which part of the position array to use
            switch (markerPos)
            {
            case SvgMarkerPosition.Start:
                start = 0;
                len   = 1;
                break;

            case SvgMarkerPosition.Mid:
                start = 1;
                len   = vertexPositions.Length - 2;
                break;

            default:
                // == MarkerPosition.End
                start = vertexPositions.Length - 1;
                len   = 1;
                break;
            }
            int end = start + len;

            for (int i = start; i < end; i++)
            {
                SvgPointF point = vertexPositions[i];

                GdiGraphicsContainer gc = gr.BeginContainer();

                gr.TranslateTransform(point.X, point.Y);

                if (markerElm.OrientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle))
                {
                    double scaleValue = markerElm.OrientAngle.AnimVal.Value;
                    if (!scaleValue.Equals(0))
                    {
                        gr.RotateTransform((float)scaleValue);
                    }
                }
                else
                {
                    double angle;

                    switch (markerPos)
                    {
                    case SvgMarkerPosition.Start:
                        angle = markerHostElm.GetStartAngle(i);
                        //angle = markerHostElm.GetStartAngle(i + 1);
                        if (vertexPositions.Length >= 2)
                        {
                            SvgPointF pMarkerPoint1 = vertexPositions[start];
                            SvgPointF pMarkerPoint2 = vertexPositions[end];
                            float     xDiff         = pMarkerPoint2.X - pMarkerPoint1.X;
                            float     yDiff         = pMarkerPoint2.Y - pMarkerPoint1.Y;
                            double    angleMarker   = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
                            if (!angleMarker.Equals(angle))
                            {
                                angle = angleMarker;
                            }
                        }
                        break;

                    case SvgMarkerPosition.Mid:
                        //angle = (markerHostElm.GetEndAngle(i) + markerHostElm.GetStartAngle(i + 1)) / 2;
                        angle = SvgNumber.CalcAngleBisection(markerHostElm.GetEndAngle(i), markerHostElm.GetStartAngle(i + 1));
                        break;

                    default:
                        angle = markerHostElm.GetEndAngle(i - 1);
                        //double angle2 = markerHostElm.GetEndAngle(i);
                        if (vertexPositions.Length >= 2)
                        {
                            SvgPointF pMarkerPoint1 = vertexPositions[start - 1];
                            SvgPointF pMarkerPoint2 = vertexPositions[start];
                            float     xDiff         = pMarkerPoint2.X - pMarkerPoint1.X;
                            float     yDiff         = pMarkerPoint2.Y - pMarkerPoint1.Y;
                            double    angleMarker   = (float)(Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI);
                            if (!angleMarker.Equals(angle))
                            {
                                angle = angleMarker;
                            }
                        }
                        //if (mayHaveCurves)
                        //{
                        //	angle = this.GetAngleAt(start - 1, angle, markerPos, markerHostElm);
                        //}
                        break;
                    }
                    gr.RotateTransform((float)angle);
                }

                // 'viewBox' and 'preserveAspectRatio' attributes
                // viewBox -> viewport(0, 0, markerWidth, markerHeight)
                var      spar = (SvgPreserveAspectRatio)markerElm.PreserveAspectRatio.AnimVal;
                double[] translateAndScale = spar.FitToViewBox((SvgRect)markerElm.ViewBox.AnimVal,
                                                               new SvgRect(0, 0, markerElm.MarkerWidth.AnimVal.Value, markerElm.MarkerHeight.AnimVal.Value));

                //// Warning at this time, refX and refY are relative to the painted element's coordinate system.
                //// We need to move the reference point to the marker's coordinate system
                //float refX = (float)markerElm.RefX.AnimVal.Value;
                //float refY = (float)markerElm.RefY.AnimVal.Value;

                ////if (!(refX.Equals(0) && refY.Equals(0)))
                ////{
                ////	var points = new PointF[] { new PointF(refX, refY) };
                ////	gr.Transform.TransformPoints(points);

                ////	refX = points[0].X;
                ////	refY = points[0].Y;

                ////	gr.TranslateTransform(-refX, -refY);
                ////}

                //if (markerElm.MarkerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                //{
                //	SvgLength strokeWidthLength = new SvgLength(refElement,
                //                    "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
                //	float strokeWidth = (float)strokeWidthLength.Value;
                //	gr.ScaleTransform(strokeWidth, strokeWidth);
                //}

                //gr.TranslateTransform(-(float)(markerElm.RefX.AnimVal.Value * translateAndScale[2]),
                //	-(float)(markerElm.RefY.AnimVal.Value * translateAndScale[3]));

                //gr.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]);

                // compute an additional transform for 'strokeWidth' coordinate system
                ISvgAnimatedEnumeration markerUnits = markerElm.MarkerUnits;
                if (markerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                {
                    SvgLength strokeWidthLength = new SvgLength(refElement,
                                                                "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, SvgConstants.ValOne);
                    double strokeWidth = strokeWidthLength.Value;
                    if (!strokeWidth.Equals(1))
                    {
                        gr.ScaleTransform((float)strokeWidth, (float)strokeWidth);
                    }
                }

                gr.TranslateTransform(-(float)(markerElm.RefX.AnimVal.Value * translateAndScale[2]),
                                      -(float)(markerElm.RefY.AnimVal.Value * translateAndScale[3]));

                if (!(translateAndScale[2].Equals(1) && translateAndScale[3].Equals(1)))
                {
                    gr.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]);
                }

                //				gr.TranslateTransform(point.X, point.Y);

                RectangleF rectClip = RectangleF.Empty;

                if (markerUnits.AnimVal.Equals((ushort)SvgMarkerUnit.StrokeWidth))
                {
                    string overflowAttr = markerElm.GetAttribute("overflow");
                    if (string.IsNullOrWhiteSpace(overflowAttr) ||
                        overflowAttr.Equals("scroll", comparer) || overflowAttr.Equals(CssConstants.ValHidden, comparer))
                    {
                        var     markerClip = RectangleF.Empty;
                        SvgRect clipRect   = (SvgRect)markerElm.ViewBox.AnimVal;
                        if (clipRect != null && !clipRect.IsEmpty)
                        {
                            rectClip = new RectangleF((float)clipRect.X, (float)clipRect.Y,
                                                      (float)clipRect.Width, (float)clipRect.Height);
                        }
                        else if (markerElm.IsSizeDefined)
                        {
                            rectClip = new RectangleF(0, 0,
                                                      (float)markerElm.MarkerWidth.AnimVal.Value, (float)markerElm.MarkerHeight.AnimVal.Value);
                        }
                    }
                }

                if (rectClip.IsEmpty)
                {
                    SetClip(gr);
                }
                else
                {
                    gr.SetClip(rectClip);
                }

                renderer.RenderChildren(markerElm);

                gr.EndContainer(gc);
            }
        }
コード例 #36
0
 public SvgImageElement(SvgLength x, SvgLength y, SvgXRef xref)
 {
     this.XRef = xref;
     this.X    = x;
     this.Y    = y;
 }
コード例 #37
0
ファイル: SvgHelper.cs プロジェクト: lallous/SvgForXaml
		public static bool TryParseCoordinate(this XmlElement element, string attributeName, out SvgLength result) => element.TryParseLength(attributeName, out result);