Пример #1
0
        // Enumerate the drawings in the DrawingGroup.
        private void EnumDrawingGroup(DrawingGroup drawingGroup)
        {
            // Enumerate the drawings in the DrawingCollection.
            foreach (Drawing drawing in drawingGroup.Children)
            {
                string objectId = SvgObject.GetId(drawing);
                if (!string.IsNullOrWhiteSpace(objectId))
                {
                    _idMap[objectId] = drawing;
                }
                string objectName = (string)drawing.GetValue(FrameworkElement.NameProperty);
                if (!string.IsNullOrWhiteSpace(objectName))
                {
                    _idMap[objectName] = drawing;
                }
                string uniqueId = SvgObject.GetUniqueId(drawing);
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    _guidMap[uniqueId] = drawing;
                }

                DrawingGroup groupDrawing = null;

                // If the drawing is a DrawingGroup, call the function recursively.
                if (TryCast.Cast(drawing, out groupDrawing))
                {
                    SvgObjectType objectType = SvgObject.GetType(groupDrawing);

                    if (objectType != SvgObjectType.Text)
                    {
                        EnumDrawingGroup(groupDrawing);
                    }
                }
            }
        }
Пример #2
0
        public WpfHitTestResult HitTest(Rect rect, IntersectionDetail detail)
        {
            var svgDrawing = this.PerformHitTest(rect, detail);

            if (svgDrawing == null)
            {
                return(WpfHitTestResult.Empty);
            }
            string uniqueId = SvgObject.GetUniqueId(svgDrawing);

            if (string.IsNullOrWhiteSpace(uniqueId))
            {
                return(new WpfHitTestResult(rect, null, svgDrawing));
//                return WpfHitTestResult.Empty;
            }
            var svgElement = this.GetSvgByUniqueId(uniqueId);

            if (svgElement == null)
            {
                return(new WpfHitTestResult(rect, null, svgDrawing));
//                return WpfHitTestResult.Empty;
            }

            return(new WpfHitTestResult(rect, svgElement, svgDrawing));
        }
Пример #3
0
        public WpfHitTestResult HitTest(Point point)
        {
            var svgDrawing = this.PerformHitTest(point);

            if (svgDrawing == null)
            {
                return(WpfHitTestResult.Empty);
            }
            string uniqueId = SvgObject.GetUniqueId(svgDrawing);

            if (string.IsNullOrWhiteSpace(uniqueId))
            {
                if (_hitGroup != null && _hitGroup.Children.Contains(svgDrawing))
                {
                    var groupElement = this.GetSvgByUniqueId(uniqueId);
                    if (groupElement != null)
                    {
                        return(new WpfHitTestResult(point, groupElement, svgDrawing));
                    }
                }

                return(new WpfHitTestResult(point, null, svgDrawing));
//                return WpfHitTestResult.Empty;
            }
            var svgElement = this.GetSvgByUniqueId(uniqueId);

            if (svgElement == null)
            {
                return(new WpfHitTestResult(point, null, svgDrawing));
//                return WpfHitTestResult.Empty;
            }

            return(new WpfHitTestResult(point, svgElement, svgDrawing));
        }
Пример #4
0
        public Transform GetTransform(WpfDrawingDocument document, Drawing hitDrawing)
        {
            var displayTransform = (Transform)document.DisplayTransform.Inverse;

            if (_parent == null)
            {
                return(displayTransform);
            }
            var splitPaths = this.SplitPath;

            if (splitPaths == null || splitPaths.Length == 0)
            {
                return(displayTransform);
            }
            foreach (var splitPath in splitPaths)
            {
                System.Diagnostics.Trace.WriteLine(splitPath);
            }

            var uniqueId = SvgObject.GetUniqueId(hitDrawing);

            TransformGroup transforms = new TransformGroup();

            transforms.Children.Add(displayTransform);

            var drawingLayer = document.GetDrawingLayer();

            if (drawingLayer != null && drawingLayer.Transform != null)
            {
                var transform = drawingLayer.Transform;
                if (!transform.Value.IsIdentity)
                {
                    transforms.Children.Add(transform);
                }
            }

            for (int i = 0; i < splitPaths.Length; i++)
            {
                var splitPath = splitPaths[i];
                if (!string.IsNullOrWhiteSpace(uniqueId) &&
                    string.Equals(splitPath, uniqueId, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                var drawing = document.GetByUniqueId(splitPath) as DrawingGroup;
                if (drawing != null && drawing.Transform != null)
                {
                    if (drawing.Transform != drawingLayer.Transform)
                    {
                        transforms.Children.Add(drawing.Transform);
                    }
                }
            }

            return(transforms);
        }
Пример #5
0
        private bool HitTestDrawing(DrawingGroup group, Point pt, out Drawing hitDrawing)
        {
            hitDrawing = null;

            if (group.Bounds.Contains(pt))
            {
                DrawingGroup    groupDrawing    = null;
                GlyphRunDrawing glyRunDrawing   = null;
                GeometryDrawing geometryDrawing = null;

                DrawingCollection drawings = group.Children;
                for (int i = drawings.Count - 1; i >= 0; i--)
                {
                    Drawing drawing = drawings[i];
                    if (TryCast.Cast(drawing, out geometryDrawing))
                    {
                        if (HitTestDrawing(geometryDrawing, pt))
                        {
                            hitDrawing = drawing;
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out groupDrawing))
                    {
                        SvgObjectType objectType = SvgObject.GetType(groupDrawing);
                        //if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
                        if (objectType == SvgObjectType.Text)
                        {
                            hitDrawing = drawing;
                            return(true);
                        }
                        if (HitTestDrawing(groupDrawing, pt, out hitDrawing))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(drawing, out glyRunDrawing))
                    {
                        if (HitTestDrawing(glyRunDrawing, pt))
                        {
                            hitDrawing = glyRunDrawing;
                            return(true);
                        }
                    }
                }
                string uniqueId = SvgObject.GetUniqueId(group);
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    _hitGroup = group;
                }
            }

            return(false);
        }
Пример #6
0
        private Drawing PerformHitTest(Rect rect, IntersectionDetail detail)
        {
            if (_svgDrawing == null)
            {
                return(null);
            }

            var rectDisplay = _displayTransform.TransformBounds(rect);
            var geomDisplay = new RectangleGeometry(rectDisplay);

            DrawingGroup    groupDrawing    = null;
            GlyphRunDrawing glyRunDrawing   = null;
            GeometryDrawing geometryDrawing = null;

            Drawing foundDrawing = null;

            DrawingCollection drawings = _svgDrawing.Children;

            for (int i = drawings.Count - 1; i >= 0; i--)
            {
                Drawing drawing = drawings[i];
                if (TryCast.Cast(drawing, out geometryDrawing))
                {
                    if (HitTestDrawing(geometryDrawing, geomDisplay, detail))
                    {
                        string uniqueId = SvgObject.GetUniqueId(drawing);
                        if (!string.IsNullOrWhiteSpace(uniqueId))
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out groupDrawing))
                {
                    if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text)
                    {
                        var textBounds = new RectangleGeometry(groupDrawing.Bounds);
                        if (textBounds.FillContainsWithDetail(geomDisplay) == detail)
                        {
                            string uniqueId = SvgObject.GetUniqueId(drawing);
                            if (!string.IsNullOrWhiteSpace(uniqueId))
                            {
                                foundDrawing = drawing;
                                break;
                            }
                        }
                    }
                    if (HitTestDrawing(groupDrawing, geomDisplay, out foundDrawing, detail))
                    {
                        string uniqueId = SvgObject.GetUniqueId(drawing);
                        if (!string.IsNullOrWhiteSpace(uniqueId))
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out glyRunDrawing))
                {
                    if (HitTestDrawing(glyRunDrawing, geomDisplay, detail))
                    {
                        foundDrawing = drawing;
                        break;
                    }
                }
            }

            return(foundDrawing);
        }
Пример #7
0
        private bool HitTestDrawing(DrawingGroup group, Point pt, out Drawing hitDrawing)
        {
            hitDrawing = null;
            bool isHit = false;

            if (group.Bounds.Contains(pt))
            {
                var transform = group.Transform;
                if (transform != null)
                {
                    var matrix = transform.Value;
                    if (matrix != null && matrix.IsIdentity == false)
                    {
                        pt = transform.Inverse.Transform(pt);
                    }
                }

                DrawingGroup    groupDrawing    = null;
                GlyphRunDrawing glyRunDrawing   = null;
                GeometryDrawing geometryDrawing = null;

                DrawingCollection drawings = group.Children;
                for (int i = drawings.Count - 1; i >= 0; i--)
                {
                    Drawing drawing = drawings[i];
                    if (TryCast.Cast(drawing, out geometryDrawing))
                    {
                        if (HitTestDrawing(geometryDrawing, pt))
                        {
                            hitDrawing = drawing;
                            int orderNumber = SvgObject.GetOrder(drawing);
                            if (orderNumber >= 0)
                            {
                                _hitList[orderNumber] = drawing;
                                isHit = false;
                            }
                            else
                            {
                                return(true);
                            }
                        }
                    }
                    else if (TryCast.Cast(drawing, out groupDrawing))
                    {
                        SvgObjectType objectType = SvgObject.GetType(groupDrawing);
                        //if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
                        if (objectType == SvgObjectType.Text)
                        {
                            hitDrawing = drawing;
                            int orderNumber = SvgObject.GetOrder(drawing);
                            if (orderNumber >= 0)
                            {
                                _hitList[orderNumber] = drawing;
                                isHit = false;
                            }
                            else
                            {
                                return(true);
                            }
                        }
                        if (HitTestDrawing(groupDrawing, pt, out hitDrawing))
                        {
                            int orderNumber = SvgObject.GetOrder(drawing);
                            if (orderNumber >= 0)
                            {
                                _hitList[orderNumber] = drawing;
                                isHit = false;
                            }
                            else
                            {
                                return(true);
                            }
                        }
                    }
                    else if (TryCast.Cast(drawing, out glyRunDrawing))
                    {
                        if (HitTestDrawing(glyRunDrawing, pt))
                        {
                            hitDrawing = glyRunDrawing;
                            int orderNumber = SvgObject.GetOrder(drawing);
                            if (orderNumber >= 0)
                            {
                                _hitList[orderNumber] = drawing;
                                isHit = false;
                            }
                            else
                            {
                                return(true);
                            }
                        }
                    }
                }
                string uniqueId = SvgObject.GetUniqueId(group);
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    _hitGroup = group;
                }
            }

            return(isHit);
        }
Пример #8
0
        private Drawing PerformHitTest(Point pt)
        {
            _hitGroup = null;
            if (_hitList == null)
            {
                _hitList = new SortedList <int, Drawing>();
            }
            else if (_hitList.Count != 0)
            {
                _hitList.Clear();
            }

            if (_svgDrawing == null)
            {
                return(null);
            }

            Point ptDisplay = _displayTransform.Transform(pt);

            DrawingGroup    groupDrawing    = null;
            GlyphRunDrawing glyRunDrawing   = null;
            GeometryDrawing geometryDrawing = null;

            Drawing foundDrawing = null;

            DrawingCollection drawings = _svgDrawing.Children;

            for (int i = drawings.Count - 1; i >= 0; i--)
            {
                Drawing drawing = drawings[i];
                if (TryCast.Cast(drawing, out geometryDrawing))
                {
                    if (HitTestDrawing(geometryDrawing, ptDisplay))
                    {
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber] = drawing;
                        }
                        else
                        {
                            string uniqueId = SvgObject.GetUniqueId(geometryDrawing);
                            if (!string.IsNullOrWhiteSpace(uniqueId))
                            {
                                foundDrawing = drawing;
                                break;
                            }
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out groupDrawing))
                {
                    int orderNumber = SvgObject.GetOrder(drawing);
                    if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text &&
                        groupDrawing.Bounds.Contains(ptDisplay))
                    {
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber] = drawing;
                        }
                        else
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                    if (HitTestDrawing(groupDrawing, ptDisplay, out foundDrawing))
                    {
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber] = drawing;
                        }
                        else
                        {
                            string uniqueId = SvgObject.GetUniqueId(foundDrawing);
                            if (!string.IsNullOrWhiteSpace(uniqueId))
                            {
                                //foundDrawing = drawing;
                                break;
                            }
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out glyRunDrawing))
                {
                    if (HitTestDrawing(glyRunDrawing, ptDisplay))
                    {
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber] = drawing;
                        }
                        else
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                }
            }

            if (_hitList.Count != 0)
            {
                return(_hitList.LastOrDefault().Value);
            }

            if (foundDrawing == null)
            {
                return(_hitGroup);
            }

            return(foundDrawing);
        }
Пример #9
0
        private bool HitTestDrawing(DrawingGroup group, Point pt, out Drawing hitDrawing, bool isText = false)
        {
            hitDrawing = null;
            bool isHit = false;

            if (!group.Bounds.Contains(pt))
            {
                return(isHit);
            }
            var transform = group.Transform;

            if (transform != null && !transform.Value.IsIdentity)
            {
                pt = transform.Inverse.Transform(pt);
            }

            var groupHitPath = _hitPath;

            DrawingGroup    groupDrawing    = null;
            GlyphRunDrawing glyRunDrawing   = null;
            GeometryDrawing geometryDrawing = null;

            DrawingCollection drawings = group.Children;

            for (int i = drawings.Count - 1; i >= 0; i--)
            {
                Drawing drawing = drawings[i];

//                _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(drawing));

                if (TryCast.Cast(drawing, out geometryDrawing))
                {
                    if (HitTestDrawing(geometryDrawing, pt))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));

                        hitDrawing = drawing;
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));
                            isHit = false;
                        }
                        else
                        {
                            orderNumber = SvgObject.GetOrder(group);
                            if (orderNumber >= 0)
                            {
                                _hitList[orderNumber]  = group;
                                _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(group));
                            }
                            if (!string.IsNullOrWhiteSpace(SvgObject.GetUniqueId(group)))
                            {
                                _hitGroup = group;
                            }
                            return(true);
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out groupDrawing))
                {
                    _hitPath = groupHitPath.AddChild(SvgObject.GetUniqueId(groupDrawing));

                    SvgObjectType objectType = SvgObject.GetType(groupDrawing);
//                    if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
                    if (objectType == SvgObjectType.Text && this.HitTestText(groupDrawing, pt, out hitDrawing))
//                    if (objectType == SvgObjectType.Text)
                    {
                        hitDrawing = drawing;
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath;
                            isHit = false;
                        }
                        else
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        //                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(hitDrawing));

                        var currentPath = _hitPath;
                        try
                        {
                            if (HitTestDrawing(groupDrawing, pt, out hitDrawing))
                            {
//                                int orderNumber = SvgObject.GetOrder(drawing);
                                int orderNumber = SvgObject.GetOrder(hitDrawing);
                                if (orderNumber >= 0)
                                {
                                    _hitList[orderNumber]  = drawing;
                                    _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(hitDrawing));
                                    isHit = false;
                                }
                                else
                                {
                                    orderNumber = SvgObject.GetOrder(groupDrawing);
                                    if (orderNumber >= 0)
                                    {
                                        _hitList[orderNumber]  = groupDrawing;
                                        _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(groupDrawing));
                                    }
                                    return(true);
                                }
                            }
                        }
                        finally
                        {
                            _hitPath = currentPath;
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out glyRunDrawing))
                {
                    if (HitTestDrawing(glyRunDrawing, pt))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));

                        hitDrawing = glyRunDrawing;
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));
                            isHit = false;
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }
            }
            string uniqueId = SvgObject.GetUniqueId(group);

            if (!string.IsNullOrWhiteSpace(uniqueId))
            {
                _hitGroup = group;
            }

            return(isHit);
        }
Пример #10
0
        private Drawing PerformHitTest(Point pt)
        {
            if (_svgDrawing == null)
            {
                return(null);
            }

            _hitGroup = null;
            if (_hitList == null)
            {
                _hitList  = new SortedList <int, Drawing>();
                _hitPaths = new SortedList <int, WpfHitPath>();
            }
            else if (_hitList.Count != 0)
            {
                _hitList.Clear();
                _hitPaths.Clear();
            }

            var hitRootPath = new WpfHitPath();

            _hitPath = hitRootPath;

            Point ptDisplay = _displayTransform.Transform(pt);

            DrawingGroup    groupDrawing    = null;
            GlyphRunDrawing glyRunDrawing   = null;
            GeometryDrawing geometryDrawing = null;

            Drawing foundDrawing = null;

            //var isFound = false;
            //var drawingLayer = this.GetDrawingLayer(_svgDrawing, ref isFound);
            //System.Diagnostics.Trace.WriteLine("GetId: " + SvgObject.GetId(drawingLayer));
            //System.Diagnostics.Trace.WriteLine("GetName: " + SvgObject.GetName(drawingLayer));
            //System.Diagnostics.Trace.WriteLine("GetUniqueId: " + SvgObject.GetUniqueId(drawingLayer));

            //DrawingCollection drawings = drawingLayer.Children;
            DrawingCollection drawings = _svgDrawing.Children;

            for (int i = drawings.Count - 1; i >= 0; i--)
            {
                Drawing drawing = drawings[i];
                System.Diagnostics.Trace.WriteLine("GetId: " + SvgObject.GetId(drawing));
                System.Diagnostics.Trace.WriteLine("GetName: " + SvgObject.GetName(drawing));
                System.Diagnostics.Trace.WriteLine("GetUniqueId: " + SvgObject.GetUniqueId(drawing));

                if (TryCast.Cast(drawing, out geometryDrawing))
                {
                    if (HitTestDrawing(geometryDrawing, ptDisplay))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));

                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));
                        }
                        else
                        {
                            string uniqueId = SvgObject.GetUniqueId(geometryDrawing);
                            if (!string.IsNullOrWhiteSpace(uniqueId))
                            {
                                foundDrawing = drawing;
                                break;
                            }
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out groupDrawing))
                {
                    //var ptSaved = ptDisplay;
                    //var transform = groupDrawing.Transform;
                    //if (transform != null && !transform.Value.IsIdentity)
                    //{
                    //    ptDisplay = transform.Inverse.Transform(ptDisplay);
                    //}

                    _hitPath = hitRootPath.AddChild(SvgObject.GetUniqueId(groupDrawing));

                    int orderNumber = SvgObject.GetOrder(groupDrawing);
                    //if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text &&
                    //    groupDrawing.Bounds.Contains(ptDisplay))
                    if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text &&
                        this.HitTestText(groupDrawing, ptDisplay, out foundDrawing))
                    {
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath;
                        }
                        else
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                    else
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(groupDrawing));

                        var currentPath = _hitPath;
                        try
                        {
                            if (HitTestDrawing(groupDrawing, ptDisplay, out foundDrawing))
                            {
                                if (orderNumber >= 0)
                                {
                                    _hitList[orderNumber]  = drawing;
                                    _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(foundDrawing));
                                }
                                else
                                {
                                    string uniqueId = SvgObject.GetUniqueId(foundDrawing);
                                    if (!string.IsNullOrWhiteSpace(uniqueId))
                                    {
                                        //foundDrawing = drawing;
                                        break;
                                    }
                                }
                            }
                        }
                        finally
                        {
                            _hitPath = currentPath;
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out glyRunDrawing))
                {
                    if (HitTestDrawing(glyRunDrawing, ptDisplay))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));

                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));
                        }
                        else
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                }
            }

            if (_hitList.Count != 0)
            {
                var key = _hitList.LastOrDefault().Key;
                if (_hitPaths.ContainsKey(key))
                {
                    System.Diagnostics.Trace.WriteLine("Key Found: " + _hitPaths[key].Path);
                }
                return(_hitList.LastOrDefault().Value);
            }

            if (foundDrawing == null)
            {
                return(_hitGroup);
            }

            return(foundDrawing);
        }