public void TestRemoteGZip()
        {
            SvgWindow   wnd = new SvgWindow(100, 100, null);
            SvgDocument doc = new SvgDocument(wnd);

            doc.Load("http://www.sharpvectors.org/tests/gzip-test.svgz");
        }
Пример #2
0
    public SvgImageWidget()
    {
        //scriptEngineByMimeType = new TypeDictionary();
        //SetMimeTypeEngineType("application/ecmascript",
        //typeof(JScriptEngine));


        this.Events = this.Events |
                      Gdk.EventMask.ButtonPressMask |
                      Gdk.EventMask.ButtonReleaseMask |
                      Gdk.EventMask.PointerMotionMask |
                      Gdk.EventMask.EnterNotifyMask |
                      Gdk.EventMask.LeaveNotifyMask |
                      Gdk.EventMask.ScrollMask;


        this.CanFocus = true;

        renderer           = new GdiRenderer();
        renderer.OnRender += OnRender;

        window = new SvgWindow(this.Allocation.Width,
                               this.Allocation.Height,
                               renderer);
    }
Пример #3
0
        private SvgPathElement getPathElement()
        {
            SvgWindow   wnd = new SvgWindow(100, 100, null);
            SvgDocument doc = new SvgDocument(wnd);

            return((SvgPathElement)doc.CreateElement("", "path", SvgDocument.SvgNamespace));
        }
Пример #4
0
        public void TestLength()
        {
            SvgWindow   wnd = new SvgWindow(100, 100, null);
            SvgDocument doc = new SvgDocument(wnd);

            Assert.AreEqual(61, doc.CssPropertyProfile.Length);
        }
        public void TestLocalGZip()
        {
            SvgWindow   wnd = new SvgWindow(100, 100, null);
            SvgDocument doc = new SvgDocument(wnd);

            doc.Load("gzip-test.svgz");
        }
Пример #6
0
        public void TestKnownDomain()
        {
            SvgWindow   wnd = new SvgWindow(75, 75, null);
            SvgDocument doc = new SvgDocument(wnd);

            doc.Load("http://www.shiny-donkey.com/shinyDonkey.svg");
            Assert.AreEqual("www.shiny-donkey.com", doc.Domain);
        }
Пример #7
0
        public FrameGeneratorSVG()
        {
            errorBitmap   = new Bitmap(640, 480);
            currentBitmap = new Bitmap(640, 480);

            renderer.BackColor = Color.Transparent;
            svgWindow          = new SvgWindow(100, 100, renderer);
        }
Пример #8
0
        public override void TestTypeFromCreateElement()
        {
            SvgWindow   wnd = new SvgWindow(100, 100, null);
            SvgDocument doc = new SvgDocument(wnd);
            XmlElement  elm = doc.CreateElement("", localName, "http://www.w3.org/2000/svg");

            Assert.AreEqual(elmType, elm.GetType());
        }
Пример #9
0
        public void SetUp()
        {
            SvgWindow   wnd = new SvgWindow(100, 100, new GdiRenderer());
            SvgDocument doc = wnd.CreateEmptySvgDocument();

            doc.LoadXml("<svg xmlns='" + SvgDocument.SvgNamespace + "'><rect /></svg>");
            elm = (SvgStyleableElement)doc.SelectSingleNode("//*[local-name()='rect']");
        }
        public override void Render(ISvgRenderer renderer)
        {
            GraphicsWrapper graphics = ((GdiRenderer)renderer).GraphicsWrapper;
            SvgImageElement iElement = (SvgImageElement)element;
            //HttpResource resource = iElement.ReferencedResource;

            /*if (resource != null )
             * {*/
            ImageAttributes imageAttributes = new ImageAttributes();

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

            if (sOpacity.Length > 0)
            {
                double      opacity       = SvgNumber.ParseToFloat(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();

            destRect.X      = Convert.ToInt32(iElement.X.AnimVal.Value);
            destRect.Y      = Convert.ToInt32(iElement.Y.AnimVal.Value);
            destRect.Width  = Convert.ToInt32(width);
            destRect.Height = Convert.ToInt32(height);

            Image image;

            if (iElement.IsSvgImage)
            {
                SvgWindow wnd = getSvgWindow();
                gdiRenderer.BackColor = Color.Empty;
                gdiRenderer.Render(wnd.Document as SvgDocument);

                //wnd.Render();
                image = gdiRenderer.RasterImage;
                image.Save(@"c:\inlinesvg.png", ImageFormat.Png);
            }
            else
            {
                image = iElement.Bitmap;
            }

            if (image != null)
            {
                graphics.DrawImage(this, image, destRect, 0f, 0f, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            }
            //}
        }
Пример #11
0
 public GdiGraphicsRenderer(GdiGraphics graphics, SvgWindow svgWindow)
     : this(graphics)
 {
     _svgWindow = svgWindow;
     if (_svgWindow != null)
     {
         _svgWindow.Renderer = this;
     }
 }
        private SvgWindow getSvgWindow()
        {
            SvgImageElement iElm = Element as SvgImageElement;
            SvgWindow       wnd  = iElm.SvgWindow;

            wnd.Renderer       = gdiRenderer;
            gdiRenderer.Window = wnd;
            return(wnd);
        }
Пример #13
0
        public void SetUp()
        {
            // create a BitmapComparator
            comparator = new BitmapComparator();

            // create an SVGWindow and an associated GdiRenderer
            renderer = new GdiRenderer();
            window   = new SvgWindow(75, 75, renderer);
        }
Пример #14
0
        public void SetUp()
        {
            if (doc == null)
            {
                SvgWindow wnd = new SvgWindow(75, 75, null);
                doc = new SvgDocument(wnd);

                doc.Load(@"Renderer.Gdi\cssSelectors.svg");
            }
        }
Пример #15
0
        public DrawingSVG(int _iWidth, int _iHeight, long _iTimestamp, long _iAverageTimeStampsPerFrame, string _filename)
        {
            // Init and import an SVG.
            m_Renderer.BackColor = Color.Transparent;

            // Rendering window. The width and height will be updated later.
            m_SvgWindow = new SvgWindow(100, 100, m_Renderer);

            // FIXME: some files have external DTD that will be attempted to be loaded.
            // See files created from Amaya for example.
            m_SvgWindow.Src = _filename;
            m_bLoaded       = true;

            if (m_SvgWindow.Document.RootElement.Width.BaseVal.UnitType == SvgLengthType.Percentage)
            {
                m_bSizeInPercentage = true;
                m_iOriginalWidth    = (int)(m_SvgWindow.Document.RootElement.ViewBox.BaseVal.Width * (m_SvgWindow.Document.RootElement.Width.BaseVal.Value / 100));
                m_iOriginalHeight   = (int)(m_SvgWindow.Document.RootElement.ViewBox.BaseVal.Height * (m_SvgWindow.Document.RootElement.Height.BaseVal.Value / 100));
            }
            else
            {
                m_bSizeInPercentage = false;
                m_iOriginalWidth    = (int)m_SvgWindow.Document.RootElement.Width.BaseVal.Value;
                m_iOriginalHeight   = (int)m_SvgWindow.Document.RootElement.Height.BaseVal.Value;
            }

            // Set the initial scale so that the drawing is some part of the image height, to make sure it fits well.
            m_fInitialScale   = (float)(((float)_iHeight * 0.75) / m_iOriginalHeight);
            m_iOriginalWidth  = (int)((float)m_iOriginalWidth * m_fInitialScale);
            m_iOriginalHeight = (int)((float)m_iOriginalHeight * m_fInitialScale);

            m_BoundingBox.Rectangle = new Rectangle((_iWidth - m_iOriginalWidth) / 2, (_iHeight - m_iOriginalHeight) / 2, m_iOriginalWidth, m_iOriginalHeight);

            // Render on first draw call.
            m_bFinishedResizing = true;

            // Fading
            m_InfosFading               = new InfosFading(_iTimestamp, _iAverageTimeStampsPerFrame);
            m_InfosFading.UseDefault    = false;
            m_InfosFading.AlwaysVisible = true;

            // This is used to set the opacity factor.
            m_FadingColorMatrix.Matrix00 = 1.0f;
            m_FadingColorMatrix.Matrix11 = 1.0f;
            m_FadingColorMatrix.Matrix22 = 1.0f;
            m_FadingColorMatrix.Matrix33 = 1.0f;                // Change alpha value here for fading. (i.e: 0.5f).
            m_FadingColorMatrix.Matrix44 = 1.0f;
            m_FadingImgAttr.SetColorMatrix(m_FadingColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            PreferencesManager pm = PreferencesManager.Instance();

            m_PenBoundingBox           = new Pen(Color.White, 1);
            m_PenBoundingBox.DashStyle = DashStyle.Dash;
            m_BrushBoundingBox         = new SolidBrush(m_PenBoundingBox.Color);
        }
        private SvgPictureBoxWindow(SvgPictureBox control, SvgWindow parentWindow, long innerWidth, long innerHeight)
            : base(parentWindow, innerWidth, innerHeight)
        {
            if (control == null)
            {
                throw new ArgumentNullException(nameof(control), "control cannot be null");
            }

            _preferUserSize = true;
            _svgPictureBox  = control;
        }
Пример #17
0
 public void SetUp()
 {
     if (doc == null)
     {
         SvgWindow wnd = new SvgWindow(100, 100, null);
         doc = new SvgDocument(wnd);
         doc.Load("events_01.svg");
         rect = (SvgRectElement)doc.SelectSingleNode("//*[local-name()='rect']");
         g    = (SvgGElement)doc.SelectSingleNode("//*[local-name()='g']");
     }
     eventStatus = 0;
 }
Пример #18
0
        private SvgDocument LoadXml(string content)
        {
            SvgWindow   wnd = new SvgWindow(200, 300, null);
            SvgDocument doc = new SvgDocument(wnd);
            string      xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"100%\" height=\"100%\">";

            xml += content;
            xml += "</svg>";

            doc.LoadXml(xml);
            return(doc);
        }
Пример #19
0
        public DrawingSVG(long timestamp, long averageTimeStampsPerFrame, string filename)
        {
            this.filename      = filename;
            renderer.BackColor = Color.Transparent;
            svgWindow          = new SvgWindow(100, 100, renderer);

            if (!string.IsNullOrEmpty(filename))
            {
                LoadSVG(filename);
            }

            Initialize(timestamp, averageTimeStampsPerFrame);
        }
Пример #20
0
        public DrawingSVG(int x, int y, long _iTimestamp, long _iAverageTimeStampsPerFrame, string _filename)
        {
            m_fStretchFactor    = 1.0;
            m_DirectZoomTopLeft = new Point(0, 0);

            //----------------------------
            // Init and import an SVG.
            // Testing code. Should load a file according to a parameter.
            //----------------------------
            m_Renderer.BackColor = Color.Transparent;

            // Rendering window. The width and height will be updated later.
            m_SvgWindow = new SvgWindow(100, 100, m_Renderer);

            // FIXME: some files have external DTD that will be attempted to be loaded.
            // See files created from Amaya for example.
            m_SvgWindow.Src = _filename;
            m_bLoaded       = true;

            m_iOriginalWidth          = (int)m_SvgWindow.Document.RootElement.Width.BaseVal.Value;
            m_iOriginalHeight         = (int)m_SvgWindow.Document.RootElement.Height.BaseVal.Value;
            m_fOriginalAspectRatio    = (double)m_iOriginalWidth / (double)m_iOriginalHeight;
            m_UnscaledRenderingWindow = new Rectangle(x - m_iOriginalWidth / 2, y - m_iOriginalHeight / 2, m_iOriginalWidth, m_iOriginalHeight);

            // Everything start unscaled.
            RescaleCoordinates(m_fStretchFactor, m_DirectZoomTopLeft);

            // Render on first draw call.
            m_bFinishedResizing = true;

            // Fading
            m_InfosFading               = new InfosFading(_iTimestamp, _iAverageTimeStampsPerFrame);
            m_InfosFading.UseDefault    = false;
            m_InfosFading.AlwaysVisible = true;

            // This is kept in place but in the current implementation the drawing never fade. (motion guide).
            m_FadingColorMatrix.Matrix00 = 1.0f;
            m_FadingColorMatrix.Matrix11 = 1.0f;
            m_FadingColorMatrix.Matrix22 = 1.0f;
            m_FadingColorMatrix.Matrix33 = 1.0f;                // Change alpha value here for fading. (i.e: 0.5f).
            m_FadingColorMatrix.Matrix44 = 1.0f;
            m_FadingImgAttr.SetColorMatrix(m_FadingColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            PreferencesManager pm = PreferencesManager.Instance();

            m_PenBoundingBox           = new Pen(Color.FromArgb(255, pm.GridColor.R, pm.GridColor.G, pm.GridColor.B), 1);
            m_PenBoundingBox.DashStyle = DashStyle.Dash;
            m_BrushBoundingBox         = new SolidBrush(m_PenBoundingBox.Color);
        }
Пример #21
0
        public SvgPictureBox()
        {
            InitializeComponent();

            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);

            scriptEngineByMimeType = new TypeDictionary();
            SetMimeTypeEngineType("application/ecmascript", typeof(JScriptEngine));

            renderer          = new GdiRenderer();
            renderer.OnRender = new RenderEvent(this.OnRender);
            window            = new SvgWindow(this, renderer);
        }
Пример #22
0
        public static SvgDocument GetXmlDoc(string content, string style)
        {
            SvgWindow   wnd = new SvgWindow(100, 100, null);
            SvgDocument doc = new SvgDocument(wnd);

            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";

            xml += "<svg xmlns='" + SvgDocument.SvgNamespace + "' xmlns:xlink='" + SvgDocument.XLinkNamespace + "'>";
            xml += "<style type='text/css'>" + style + "</style>";
            xml += content;
            xml += "</svg>";

            doc.LoadXml(xml);
            return(doc);
        }
Пример #23
0
        private SvgWindow GetSvgWindow()
        {
            if (_embeddedRenderer == null)
            {
                _embeddedRenderer = new WpfDrawingRenderer();
            }

            SvgImageElement iElm = (SvgImageElement)this.Element;
            SvgWindow       wnd  = iElm.SvgWindow;

            wnd.Renderer = _embeddedRenderer;

            _embeddedRenderer.Window = wnd;

            return(wnd);
        }
Пример #24
0
        private SvgWindow GetSvgWindow()
        {
            if (_embeddedRenderer == null)
            {
                _embeddedRenderer = new GdiGraphicsRenderer();
            }

            SvgImageElement iElm = this.Element as SvgImageElement;
            SvgWindow       wnd  = iElm.SvgWindow;

            wnd.Renderer = _embeddedRenderer;

            _embeddedRenderer.Window = wnd;

            return(wnd);
        }
        private SvgWindow GetSvgWindow(GdiGraphics graphics)
        {
            SvgImageElement iElm = this.Element as SvgImageElement;
            SvgWindow       wnd  = iElm.SvgWindow;

            if (_embeddedRenderer == null)
            {
                _embeddedRenderer = new GdiGraphicsRenderer(graphics, wnd);
            }
            else
            {
                wnd.Renderer             = _embeddedRenderer;
                _embeddedRenderer.Window = wnd;
            }

            return(wnd);
        }
Пример #26
0
        /// <summary>
        /// The following code will load the SVGDOM, render to a emf, and
        /// place the EMF on the clipboard. Kt seems more complicated than it should
        /// see http://www.dotnet247.com/247reference/msgs/23/118514.aspx
        /// for why the straight forward solution does not work.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miCopy_Click(
            object sender,
            System.EventArgs e)
        {
            if (svgUrlCombo.Text.Length > 0)
            {
                int width  = 500;
                int height = 500;

                // make SVG document with associated window so we can render it.
                SvgWindow   window   = new SvgWindow(width, height, null);
                GdiRenderer renderer = new GdiRenderer();
                renderer.Window = window;
                window.Renderer = renderer;
                window.Src      = svgUrlCombo.Text;

                // copy and paste code taken from
                // http://www.dotnet247.com/247reference/msgs/23/117611.aspx
                // putting a plain MetaFile on the clipboard does not work.
                // .Net's metafile format is not recognised by most programs.
                Graphics g   = CreateGraphics();
                IntPtr   hdc = g.GetHdc();
                Metafile m   = new Metafile(hdc, EmfType.EmfOnly);
                g.ReleaseHdc(hdc);
                g.Dispose();
                g = Graphics.FromImage(m);

                // draw the SVG here
                // NOTE: the graphics object is automatically Disposed by the
                // GdiRenderer.
                renderer.Graphics = g;
                renderer.Render(window.Document as SvgDocument);
                //window.Render();

                // put meta file on the clipboard.
                IntPtr hwnd = this.Handle;
                if (Win32.OpenClipboard(hwnd))
                {
                    Win32.EmptyClipboard();
                    IntPtr hemf = m.GetHenhmetafile();
                    Win32.SetClipboardData(14, hemf);                     //CF_ENHMETAFILE=14
                    Win32.CloseClipboard();
                }
            }
        }
Пример #27
0
    public void TestDataImage()
    {
        string      testData = "<?xml version='1.0'?>\n<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'><image id='theImage' width='50' height='50' xlink:href='data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7'/></svg>";
        SvgWindow   wnd      = new SvgWindow(50, 50, null);
        SvgDocument doc      = new SvgDocument(wnd);

        doc.LoadXml(testData);

        SvgImageElement imgElm = doc.GetElementById("theImage") as SvgImageElement;

        Assert.IsNotNull(imgElm);
        Assert.IsTrue(!imgElm.IsSvgImage, "IsSvgImage");

        Bitmap bmp = imgElm.Bitmap;

        Assert.AreEqual(48, bmp.Width);
        Assert.AreEqual(48, bmp.Height);
    }
Пример #28
0
        private int singleRun(string filePath, bool saveLocal, bool saveImage)
        {
            DateTime startTime = DateTime.Now;

            GdiRenderer gdiRenderer = new GdiRenderer();
            SvgWindow   wnd         = new SvgWindow(300, 300, gdiRenderer);

            SvgDocument doc = new SvgDocument(wnd);

            wnd.Document = doc;
            doc.Load(filePath);

            //wnd.Render();

            if (((GdiRenderer)wnd.Renderer).RasterImage == null)
            {
                Console.WriteLine("ERROR: no image rendered");
            }

            DateTime endTime = DateTime.Now;
            TimeSpan diff    = endTime - startTime;

            if (saveLocal)
            {
                doc.Save(localPath);
            }

            if (saveImage)
            {
                ((GdiRenderer)wnd.Renderer).RasterImage.Save(imgPath, System.Drawing.Imaging.ImageFormat.Png);
                Console.WriteLine("Reference image saved as :\n" + imgPath);
            }

            wnd.Renderer = null;
            wnd          = null;
            doc          = null;

            return((int)diff.TotalMilliseconds);
        }
        public override void Render(WpfDrawingRenderer renderer)
        {
            WpfDrawingContext context      = renderer.Context;
            SvgImageElement   imageElement = (SvgImageElement)_svgElement;

            double x      = imageElement.X.AnimVal.Value;
            double y      = imageElement.Y.AnimVal.Value;
            double width  = imageElement.Width.AnimVal.Value;
            double height = imageElement.Height.AnimVal.Value;

            Rect destRect = new Rect(x, y, width, height);
            Rect clipRect = new Rect(x, y, width, height);

            ImageSource imageSource = null;

            if (imageElement.IsSvgImage)
            {
                if (imageElement.IsRootReferenced(imageElement.OwnerDocument.BaseURI))
                {
                    return;
                }

                SvgWindow wnd = GetSvgWindow();
                if (wnd == null)
                {
                    return;
                }
                //_embeddedRenderer.BackColor = Color.Empty;
                _embeddedRenderer.Render(wnd.Document);

                DrawingGroup imageGroup = _embeddedRenderer.Drawing as DrawingGroup;
                if (imageGroup != null &&
                    (imageGroup.Children != null && imageGroup.Children.Count == 1))
                {
                    DrawingGroup imageDrawing = imageGroup.Children[0] as DrawingGroup;
                    if (imageDrawing != null)
                    {
                        imageDrawing.ClipGeometry = null;

                        imageSource = new DrawingImage(imageDrawing);
                    }
                    else
                    {
                        imageGroup.ClipGeometry = null;

                        imageSource = new DrawingImage(imageGroup);
                    }
                }
                else
                {
                    imageSource = new DrawingImage(_embeddedRenderer.Drawing);
                }

                if (_embeddedRenderer != null)
                {
                    _embeddedRenderer.Dispose();
                    _embeddedRenderer = null;
                }
            }
            else
            {
                imageSource = GetBitmapSource(imageElement, context);
            }

            if (imageSource == null)
            {
                return;
            }

            //TODO--PAUL: Set the DecodePixelWidth/DecodePixelHeight?

            // Freeze the DrawingImage for performance benefits.
            //imageSource.Freeze();

            DrawingGroup drawGroup = null;

            ISvgAnimatedPreserveAspectRatio animatedAspectRatio = imageElement.PreserveAspectRatio;

            if (animatedAspectRatio != null && animatedAspectRatio.AnimVal != null)
            {
                SvgPreserveAspectRatio     aspectRatio     = animatedAspectRatio.AnimVal as SvgPreserveAspectRatio;
                SvgPreserveAspectRatioType aspectRatioType =
                    (aspectRatio != null) ? aspectRatio.Align : SvgPreserveAspectRatioType.Unknown;
                if (aspectRatio != null && aspectRatioType != SvgPreserveAspectRatioType.None &&
                    aspectRatioType != SvgPreserveAspectRatioType.Unknown)
                {
                    double imageWidth  = imageSource.Width;
                    double imageHeight = imageSource.Height;

                    double viewWidth  = destRect.Width;
                    double viewHeight = destRect.Height;

                    SvgMeetOrSlice meetOrSlice = aspectRatio.MeetOrSlice;
                    if (meetOrSlice == SvgMeetOrSlice.Meet)
                    {
                        if (imageWidth <= viewWidth && imageHeight <= viewHeight)
                        {
                            if (this.Transform == null)
                            {
                                if (!aspectRatio.IsDefaultAlign) // Cacxa
                                {
                                    destRect = this.GetBounds(destRect, new Size(imageWidth, imageHeight), aspectRatioType);
                                }
                                else
                                {
                                    Transform viewTransform = this.GetAspectRatioTransform(aspectRatio,
                                                                                           new SvgRect(0, 0, imageWidth, imageHeight),
                                                                                           new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height));

                                    if (viewTransform != null)
                                    {
                                        drawGroup           = new DrawingGroup();
                                        drawGroup.Transform = viewTransform;

                                        DrawingGroup lastGroup = context.Peek();
                                        Debug.Assert(lastGroup != null);

                                        if (lastGroup != null)
                                        {
                                            lastGroup.Children.Add(drawGroup);
                                        }

                                        destRect = this.GetBounds(destRect,
                                                                  new Size(imageWidth, imageHeight), aspectRatioType);

                                        // The origin is already handled by the view transform...
                                        destRect.X = 0;
                                        destRect.Y = 0;
                                    }
                                }
                            }
                            else
                            {
                                destRect = new Rect(0, 0, viewWidth, viewHeight);
                            }
                        }
                        else
                        {
                            if (this.Transform == null)
                            {
                                Transform viewTransform = this.GetAspectRatioTransform(aspectRatio,
                                                                                       new SvgRect(0, 0, imageWidth, imageHeight),
                                                                                       new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height));

                                if (viewTransform != null)
                                {
                                    drawGroup           = new DrawingGroup();
                                    drawGroup.Transform = viewTransform;

                                    DrawingGroup lastGroup = context.Peek();
                                    Debug.Assert(lastGroup != null);

                                    if (lastGroup != null)
                                    {
                                        lastGroup.Children.Add(drawGroup);
                                    }

                                    destRect = this.GetBounds(destRect,
                                                              new Size(imageWidth, imageHeight), aspectRatioType);

                                    // The origin is already handled by the view transform...
                                    destRect.X = 0;
                                    destRect.Y = 0;
                                }
                            }
                        }
                    }
                    else if (meetOrSlice == SvgMeetOrSlice.Slice)
                    {
                        var       fScaleX       = viewWidth / imageWidth;
                        var       fScaleY       = viewHeight / imageHeight;
                        Transform viewTransform = this.GetAspectRatioTransform(aspectRatio,
                                                                               new SvgRect(0, 0, imageWidth, imageHeight),
                                                                               new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height));

                        DrawingGroup sliceGroup = new DrawingGroup();
                        sliceGroup.ClipGeometry = new RectangleGeometry(clipRect);

                        DrawingGroup lastGroup = context.Peek();
                        Debug.Assert(lastGroup != null);

                        if (lastGroup != null)
                        {
                            lastGroup.Children.Add(sliceGroup);
                        }

                        if (viewTransform != null)
                        {
                            drawGroup           = new DrawingGroup();
                            drawGroup.Transform = viewTransform;

                            sliceGroup.Children.Add(drawGroup);

                            destRect = this.GetBounds(destRect,
                                                      new Size(imageWidth, imageHeight), aspectRatioType);

                            // The origin is already handled by the view transform...
                            destRect.X = 0;
                            destRect.Y = 0;
                        }
                        else
                        {
                            drawGroup = sliceGroup;
                        }
                    }
                }
            }

            ImageDrawing drawing = new ImageDrawing(imageSource, destRect);

            float opacityValue = -1;

            string opacity = imageElement.GetAttribute("opacity");

            if (string.IsNullOrWhiteSpace(opacity))
            {
                opacity = imageElement.GetPropertyValue("opacity");
            }
            if (!string.IsNullOrWhiteSpace(opacity))
            {
                opacityValue = (float)SvgNumber.ParseNumber(opacity);
                opacityValue = Math.Min(opacityValue, 1);
                opacityValue = Math.Max(opacityValue, 0);
            }

            Geometry  clipGeom  = this.ClipGeometry;
            Transform transform = this.Transform;

            bool ownedGroup = true;

            if (drawGroup == null)
            {
                drawGroup  = context.Peek();
                ownedGroup = false;
            }

            Debug.Assert(drawGroup != null);
            if (drawGroup != null)
            {
                if ((opacityValue >= 0 && opacityValue < 1) || (clipGeom != null && !clipGeom.IsEmpty()) ||
                    (transform != null && !transform.Value.IsIdentity))
                {
                    DrawingGroup clipGroup = ownedGroup ? drawGroup : new DrawingGroup();
                    if (opacityValue >= 0 && opacityValue < 1)
                    {
                        clipGroup.Opacity = opacityValue;
                    }
                    if (clipGeom != null)
                    {
                        SvgUnitType clipUnits = this.ClipUnits;
                        if (clipUnits == SvgUnitType.ObjectBoundingBox)
                        {
                            Rect drawingBounds = drawing.Bounds;

                            TransformGroup transformGroup = new TransformGroup();

                            // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target.
                            transformGroup.Children.Add(
                                new ScaleTransform(drawingBounds.Width, drawingBounds.Height));
                            transformGroup.Children.Add(
                                new TranslateTransform(drawingBounds.X, drawingBounds.Y));

                            clipGeom.Transform = transformGroup;
                        }

                        clipGroup.ClipGeometry = clipGeom;
                    }
                    if (transform != null)
                    {
                        Transform curTransform = clipGroup.Transform;
                        if (curTransform != null && curTransform.Value.IsIdentity == false)
                        {
                            TransformGroup transformGroup = new TransformGroup();
                            transformGroup.Children.Add(curTransform);
                            transformGroup.Children.Add(transform);
                            clipGroup.Transform = transformGroup;
                        }
                        else
                        {
                            clipGroup.Transform = transform;
                        }
                    }

                    clipGroup.Children.Add(drawing);
                    if (!ownedGroup)
                    {
                        drawGroup.Children.Add(clipGroup);
                    }
                }
                else
                {
                    drawGroup.Children.Add(drawing);
                }

                string elementId = this.GetElementName();
                if (ownedGroup)
                {
                    string sVisibility = imageElement.GetPropertyValue("visibility");
                    string sDisplay    = imageElement.GetPropertyValue("display");
                    if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none"))
                    {
                        drawGroup.Opacity = 0;
                    }

                    if (!_idAssigned && !string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                    {
                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetName(drawGroup, elementId);

                            SvgObject.SetId(drawGroup, elementId);
                        }
                    }

                    // Register this drawing with the Drawing-Document...
                    this.Rendered(drawGroup);
                }
                else if (!_idAssigned)
                {
                    if (!_idAssigned && !string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId))
                    {
                        context.RegisterId(elementId);

                        if (context.IncludeRuntime)
                        {
                            SvgObject.SetName(imageSource, elementId);

                            SvgObject.SetId(imageSource, elementId);
                        }
                    }

                    // Register this drawing with the Drawing-Document...
                    this.Rendered(drawing);
                }
            }
        }
Пример #30
0
 public WpfSvgWindow(SvgWindow parentWindow, long innerWidth, long innerHeight)
     : base(parentWindow, innerWidth, innerHeight)
 {
 }
Пример #31
0
        public MainWindow(IServiceProvider serviceProvider)
        {
            InitializeComponent();

            bool isClosing = false;

            _serviceProvider = serviceProvider;
            _projectEditor = _serviceProvider.GetService<ProjectEditor>();

            _previewWindow = new SvgWindow();

            _previewWindow.svgButton.Click += (sender, e) =>
            {
                UpdateSvg();
            };

            _previewWindow.Closing += (sender, e) =>
            {
                if (!isClosing)
                {
                    _previewWindow.Hide();
                    e.Cancel = true;
                }
            };

            Loaded += (seender, e) =>
            {
                _projectEditor.CurrentTool = _projectEditor.Tools.FirstOrDefault(t => t.Name == "Selection");
                _projectEditor.CurrentPathTool = _projectEditor.PathTools.FirstOrDefault(t => t.Name == "Line");
                _projectEditor.OnNewProject();
                _projectEditor.Invalidate = () =>
                {
                    skiaView.InvalidateVisual();
                    if (_previewWindow.svgLive.IsChecked == true)
                    {
                        UpdateSvg();
                    }
                };

                skiaView.Renderer = _projectEditor.Renderers[0];
                skiaView.Container = _projectEditor.Project.CurrentContainer;
                skiaView.Presenter = new EditorPresenter();
                skiaView.Focusable = true;
                skiaView.Focus();
                skiaView.InvalidateVisual();

                _inputProcessor = new InputProcessor(
                    new WpfInputSource(
                        skiaView,
                        skiaView,
                        skiaView.FixPointOffset),
                    _projectEditor);
            };

            Closing += (sender, e) =>
            {
                _inputProcessor.Dispose();
                isClosing = true;
                _previewWindow.Close();
            };

            KeyDown += (sender, e) =>
            {
                bool isControl = Keyboard.Modifiers == ModifierKeys.Control;

                if (isControl)
                {
                    switch (e.Key)
                    {
                        case Key.P:
                            _previewWindow.Show();
                            break;
                        case Key.N:
                            NewProject();
                            break;
                        case Key.O:
                            OpenProject();
                            break;
                        case Key.S:
                            SaveProject();
                            break;
                        case Key.E:
                            Export(_projectEditor.Project.CurrentContainer);
                            break;
                        case Key.Z:
                            _projectEditor.OnUndo();
                            break;
                        case Key.Y:
                            _projectEditor.OnRedo();
                            break;
                        case Key.X:
                            _projectEditor.OnCut();
                            break;
                        case Key.C:
                            _projectEditor.OnCopy();
                            break;
                        case Key.V:
                            _projectEditor.OnPaste();
                            break;
                        case Key.A:
                            _projectEditor.OnSelectAll();
                            break;
                        case Key.G:
                            _projectEditor.OnGroupSelected();
                            break;
                        case Key.U:
                            _projectEditor.OnUngroupSelected();
                            break;
                    }
                }
                else
                {
                    switch (e.Key)
                    {
                        case Key.N:
                            _projectEditor.OnToolNone();
                            break;
                        case Key.S:
                            _projectEditor.OnToolSelection();
                            break;
                        case Key.P:
                            _projectEditor.OnToolPoint();
                            break;
                        case Key.L:
                            _projectEditor.OnToolLine();
                            break;
                        case Key.A:
                            _projectEditor.OnToolArc();
                            break;
                        case Key.B:
                            _projectEditor.OnToolCubicBezier();
                            break;
                        case Key.Q:
                            _projectEditor.OnToolQuadraticBezier();
                            break;
                        case Key.H:
                            _projectEditor.OnToolPath();
                            break;
                        case Key.M:
                            _projectEditor.OnToolMove();
                            break;
                        case Key.R:
                            _projectEditor.OnToolRectangle();
                            break;
                        case Key.E:
                            _projectEditor.OnToolEllipse();
                            break;
                        case Key.T:
                            _projectEditor.OnToolText();
                            break;
                        case Key.I:
                            _projectEditor.OnToolImage();
                            break;
                        case Key.K:
                            _projectEditor.OnToggleDefaultIsStroked();
                            break;
                        case Key.F:
                            _projectEditor.OnToggleDefaultIsFilled();
                            break;
                        case Key.D:
                            _projectEditor.OnToggleDefaultIsClosed();
                            break;
                        case Key.J:
                            _projectEditor.OnToggleDefaultIsSmoothJoin();
                            break;
                        case Key.G:
                            _projectEditor.OnToggleSnapToGrid();
                            break;
                        case Key.C:
                            _projectEditor.OnToggleTryToConnect();
                            break;
                        case Key.Y:
                            _projectEditor.OnToggleCloneStyle();
                            break;
                        case Key.Delete:
                            _projectEditor.OnDeleteSelected();
                            break;
                        case Key.Escape:
                            _projectEditor.OnDeselectAll();
                            break;
                    }
                }
            };
        }