Exemple #1
0
        protected void FitToViewbox(GdiGraphicsWrapper graphics, RectangleF elmRect)
        {
            if (element is ISvgFitToViewBox)
            {
                ISvgFitToViewBox       fitToVBElm = (ISvgFitToViewBox)element;
                SvgPreserveAspectRatio spar       = (SvgPreserveAspectRatio)fitToVBElm.PreserveAspectRatio.AnimVal;

                double[] translateAndScale = spar.FitToViewBox(
                    (SvgRect)fitToVBElm.ViewBox.AnimVal,
                    new SvgRect(elmRect.X, elmRect.Y, elmRect.Width, elmRect.Height));
                graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]);
                graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]);
            }
        }
        protected void FitToViewbox(GdiGraphicsWrapper graphics, RectangleF elmRect)
        {
            if (element is ISvgFitToViewBox)
            {
                ISvgFitToViewBox fitToVBElm = (ISvgFitToViewBox)element;
                SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)fitToVBElm.PreserveAspectRatio.AnimVal;

                double[] translateAndScale = spar.FitToViewBox(
                  (SvgRect)fitToVBElm.ViewBox.AnimVal,
                  new SvgRect(elmRect.X, elmRect.Y, elmRect.Width, elmRect.Height));
                graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]);
                graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]);
            }
        }
        public void PaintMarker(GdiGraphicsRenderer renderer, GdiGraphicsWrapper gr, 
            SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
            SvgMarkerElement markerElm     = (SvgMarkerElement) element;

            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();

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

                if (markerElm.OrientType.AnimVal.Equals(SvgMarkerOrient.Angle))
                {
                    gr.RotateTransform((float)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((float)angle);
                }

                if (markerElm.MarkerUnits.AnimVal.Equals(SvgMarkerUnit.StrokeWidth))
                {
                    SvgLength strokeWidthLength = new SvgLength(refElement,
                        "stroke-width", SvgLengthSource.Css, SvgLengthDirection.Viewport, "1");
                    float strokeWidth = (float)strokeWidthLength.Value;
                    gr.ScaleTransform(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));

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

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

                Clip(gr);

                renderer.RenderChildren(markerElm);

                gr.EndContainer(gc);
            }
        }
Exemple #4
0
        public override void Render(GdiGraphicsRenderer renderer)
        {
            GdiGraphicsWrapper graphics = renderer.GraphicsWrapper;
            SvgImageElement    iElement = (SvgImageElement)element;

            ImageAttributes imageAttributes = new ImageAttributes();

            string sOpacity = iElement.GetPropertyValue("opacity");

            if (sOpacity != null && sOpacity.Length > 0)
            {
                double      opacity       = SvgNumber.ParseNumber(sOpacity);
                ColorMatrix myColorMatrix = new ColorMatrix();
                myColorMatrix.Matrix00 = 1.00f;                 // Red
                myColorMatrix.Matrix11 = 1.00f;                 // Green
                myColorMatrix.Matrix22 = 1.00f;                 // Blue
                myColorMatrix.Matrix33 = (float)opacity;        // alpha
                myColorMatrix.Matrix44 = 1.00f;                 // w

                imageAttributes.SetColorMatrix(myColorMatrix,
                                               ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            }

            float width  = (float)iElement.Width.AnimVal.Value;
            float height = (float)iElement.Height.AnimVal.Value;

            //Rectangle destRect = new Rectangle(Convert.ToInt32(iElement.X.AnimVal.Value),
            //    Convert.ToInt32(iElement.Y.AnimVal.Value),
            //    Convert.ToInt32(width), Convert.ToInt32(height));
            RectangleF destRect = new RectangleF((float)iElement.X.AnimVal.Value,
                                                 (float)iElement.Y.AnimVal.Value, (float)iElement.Width.AnimVal.Value,
                                                 (float)iElement.Height.AnimVal.Value);

            Image image = null;

            if (iElement.IsSvgImage)
            {
                SvgWindow wnd = GetSvgWindow();
                _embeddedRenderer.BackColor = Color.Empty;
                _embeddedRenderer.Render(wnd.Document);

                image = _embeddedRenderer.RasterImage;
            }
            else
            {
                image = GetBitmap(iElement);
            }

            if (image != null)
            {
                //graphics.DrawImage(this, image, destRect, 0f, 0f,
                //    image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);

                // code extracted from FitToViewbox
                SvgPreserveAspectRatio spar = (SvgPreserveAspectRatio)iElement.PreserveAspectRatio.AnimVal ?? new SvgPreserveAspectRatio("none", iElement);

                double[] translateAndScale =
                    spar.FitToViewBox(new SvgRect(0, 0, image.Width, image.Height),
                                      new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height));
                graphics.TranslateTransform((float)translateAndScale[0], (float)translateAndScale[1]);
                graphics.ScaleTransform((float)translateAndScale[2], (float)translateAndScale[3]);
                graphics.DrawImage(this, image, new Rectangle(0, 0, image.Width, image.Height), 0f, 0f,
                                   image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);

                image.Dispose();
                image = null;
            }

            if (_embeddedRenderer != null)
            {
                _embeddedRenderer.Dispose();
                _embeddedRenderer = null;
            }

            if (imageAttributes != null)
            {
                imageAttributes.Dispose();
                imageAttributes = null;
            }
        }
Exemple #5
0
        public void PaintMarker(GdiGraphicsRenderer renderer, GdiGraphicsWrapper gr,
                                SvgMarkerPosition markerPos, SvgStyleableElement refElement)
        {
            ISharpMarkerHost markerHostElm = (ISharpMarkerHost)refElement;
            SvgMarkerElement markerElm     = (SvgMarkerElement)element;

            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();

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

                if (markerElm.OrientType.AnimVal.Equals((ushort)SvgMarkerOrient.Angle))
                {
                    gr.RotateTransform((float)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((float)angle);
                }

                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);
                }

                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));


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

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

                Clip(gr);

                renderer.RenderChildren(markerElm);

                gr.EndContainer(gc);
            }
        }