Преглед изворни кода

compdfkit(win)-编辑单选框样式调整为(文字两原点)(图片六原点),裁剪获取边框点范围,获取以及设置当前裁剪框,调整编辑初步显示样式,绘制单选选中辅助线

liyijie пре 8 месеци
родитељ
комит
4a5c929d0c

+ 175 - 16
Demo/Examples/ComPDFKit.Tool/DrawTool/SelectedRect.cs

@@ -76,7 +76,6 @@ namespace ComPDFKit.Tool.DrawTool
 
     public partial class SelectedRect : DrawingVisual
     {
-
         /// <summary>
         /// Re-layout child elements
         /// </summary>
@@ -140,7 +139,7 @@ namespace ComPDFKit.Tool.DrawTool
 
         public bool GetIsHover()
         {
-           return isHover;
+            return isHover;
         }
 
         public void SetIsSelected(bool selected)
@@ -150,7 +149,7 @@ namespace ComPDFKit.Tool.DrawTool
 
         public bool GetIsSelected()
         {
-           return isSelected;
+            return isSelected;
         }
 
         public void SetCurrentDrawPointType(DrawPointType type)
@@ -172,7 +171,15 @@ namespace ComPDFKit.Tool.DrawTool
             HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
             if (hitResult != null && hitResult.VisualHit is DrawingVisual)
             {
-                hitControlType = GetHitControlIndex(downPoint);
+                //Crop judgment point
+                if (currentDrawPointType == DrawPointType.Crop)
+                {
+                    hitControlType = GetHitCropControlIndex(downPoint);
+                }
+                else 
+                {
+                    hitControlType = GetHitControlIndex(downPoint);
+                }
                 if (hitControlType != PointControlType.None)
                 {
                     cacheRect = drawRect;
@@ -292,7 +299,16 @@ namespace ComPDFKit.Tool.DrawTool
                 switch (currentDrawPointType)
                 {
                     case DrawPointType.Circle:
-                        DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
+                        if (selectedType == SelectedType.PDFEdit)
+                        {
+                            DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, new SolidColorBrush(Color.FromRgb(71, 126, 222)));
+
+                            DrawEditSelectionBox(drawDC, PointPen);
+                        }
+                        else
+                        {
+                            DrawCirclePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
+                        }
                         break;
                     case DrawPointType.Square:
                         DrawSquarePoint(drawDC, GetIgnorePoints(), pointSize, PointPen, PointBrush);
@@ -306,6 +322,24 @@ namespace ComPDFKit.Tool.DrawTool
             });
         }
 
+        /// <summary>
+        /// Edit Selection Box,The timing logic needs to obtain the current page size in the future
+        /// </summary>
+        /// <param name="drawingContext"></param>
+        /// <param name="PointPen"></param>
+        private void DrawEditSelectionBox(DrawingContext drawingContext, Pen PointPen) {
+            drawingContext?.DrawLine(PointPen, new Point(0, SetDrawRect.Top), new Point(SetDrawRect.Left, SetDrawRect.Top));
+            drawingContext?.DrawLine(PointPen, new Point(0, SetDrawRect.Bottom), new Point(SetDrawRect.Left, SetDrawRect.Bottom));
+            drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Left, 0), new Point(SetDrawRect.Left, SetDrawRect.Top));
+            drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, 0), new Point(SetDrawRect.Right, SetDrawRect.Top));
+
+            drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Left, SetDrawRect.Bottom), new Point(SetDrawRect.Left, SetDrawRect.Bottom * 3));
+            drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, SetDrawRect.Bottom), new Point(SetDrawRect.Right, SetDrawRect.Bottom * 3));
+
+            drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, SetDrawRect.Bottom), new Point(SetDrawRect.Right * 3, SetDrawRect.Bottom));
+            drawingContext?.DrawLine(PointPen, new Point(SetDrawRect.Right, SetDrawRect.Top), new Point(SetDrawRect.Right * 3, SetDrawRect.Top));
+        }
+
         private void GetMoveBrushAndPen(ref SolidColorBrush colorBrush, ref Pen pen)
         {
             switch (selectedType)
@@ -400,7 +434,8 @@ namespace ComPDFKit.Tool.DrawTool
                         else
                         {
                             colorBrush = DrawParam.PDFEditRectFillBrush;
-                            pen = DrawParam.PDFEditRectLinePen;
+                            //init Color
+                            pen = new Pen(new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)), 2) { DashStyle = DashStyles.Dash };// DrawParam.PDFEditRectLinePen; 
                         }
                     }
                     break;
@@ -425,13 +460,13 @@ namespace ComPDFKit.Tool.DrawTool
             drawDC?.Close();
         }
 
-        public void SetRect(Rect newRect,double zoom)
+        public void SetRect(Rect newRect, double zoom)
         {
-            if(newRect == Rect.Empty || newRect == null)
+            if (newRect == Rect.Empty || newRect == null)
             {
                 return;
             }
-            newRect = new Rect((int)(newRect.X - rectPadding* zoom), (int)(newRect.Y - rectPadding* zoom),(int)( newRect.Width + 2 * rectPadding* zoom), (int)(newRect.Height + 2 * rectPadding* zoom));
+            newRect = new Rect((int)(newRect.X - rectPadding * zoom), (int)(newRect.Y - rectPadding * zoom), (int)(newRect.Width + 2 * rectPadding * zoom), (int)(newRect.Height + 2 * rectPadding * zoom));
             currentZoom = zoom;
             SetDrawRect = drawRect = newRect;
             drawCenterPoint = new Point(drawRect.Left + drawRect.Width / 2, drawRect.Top + drawRect.Height / 2);
@@ -464,6 +499,25 @@ namespace ComPDFKit.Tool.DrawTool
             return drawRect;
         }
 
+        /// <summary>
+        /// Get ClipRect
+        /// </summary>
+        /// <returns></returns>
+        public Rect GetClipRect()
+        {
+            return clipRect;
+        }
+
+        /// <summary>
+        /// Set ClipRect And DrawRect
+        /// </summary>
+        /// <returns></returns>
+        public void SetClipRect(Rect rect)
+        {
+            drawRect = rect;
+            clipRect= rect;
+        }
+
         public void SetMaxRect(Rect rect)
         {
             maxRect = rect;
@@ -551,6 +605,31 @@ namespace ComPDFKit.Tool.DrawTool
             }
         }
 
+        /// <summary>
+        /// Set Edit that need to be ignored
+        /// </summary>
+        /// <param name="types">
+        /// The collection of point types that need to be ignored
+        /// </param>
+        public void SetEditIgnorePoints(bool IsText = true)
+        {
+            SetCurrentDrawPointType(DrawPointType.Circle);
+            if (IsText)
+            {
+                ignorePoints.Clear();
+                ignorePoints.Add(PointControlType.LeftTop);
+                ignorePoints.Add(PointControlType.LeftBottom);
+                ignorePoints.Add(PointControlType.MiddlBottom);
+                ignorePoints.Add(PointControlType.RightBottom);
+                ignorePoints.Add(PointControlType.RightTop);
+                ignorePoints.Add(PointControlType.MiddleTop);
+            }
+            else
+            {
+                ignorePoints.Clear();
+            }
+        }
+
         /// <summary>
         /// Ignore all points
         /// </summary>
@@ -652,7 +731,7 @@ namespace ComPDFKit.Tool.DrawTool
         /// <returns>
         /// The control point type
         /// </returns>
-        public PointControlType GetHitControlIndex(Point point, bool isIgnore=true)
+        public PointControlType GetHitControlIndex(Point point, bool isIgnore = true)
         {
             HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
             if (hitResult != null && hitResult.VisualHit is DrawingVisual)
@@ -671,7 +750,7 @@ namespace ComPDFKit.Tool.DrawTool
                 {
                     Point checkPoint = controlPoints[i];
 
-                    if (isIgnore&&IgnorePointsList.Contains(checkPoint))
+                    if (isIgnore && IgnorePointsList.Contains(checkPoint))
                     {
                         continue;
                     }
@@ -686,13 +765,93 @@ namespace ComPDFKit.Tool.DrawTool
                             break;
                         case DrawPointType.Square:
 
-                            Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize,0), Math.Max(checkPoint.Y - pointSize,0), pointSize * 2, pointSize * 2);
+                            Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
                             if (checkRect.Contains(point))
                             {
                                 return (PointControlType)i;
                             }
                             break;
-                        
+
+                        case DrawPointType.Crop:
+                            Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
+                            if (cropRect.Contains(point))
+                            {
+                                return (PointControlType)i;
+                            }
+                            break;
+                        default:
+                            break;
+                    }
+                }
+                if (drawRect.Contains(point))
+                {
+                    double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
+                    double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
+                    Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
+                    if (rect.Contains(point))
+                    {
+                        if (!ignoreList.Contains(PointControlType.Body))
+                        {
+                            return PointControlType.Body;
+                        }
+                    }
+                    if (!ignoreList.Contains(PointControlType.Body))
+                    {
+                        return PointControlType.Line;
+                    }
+                }
+            }
+            return PointControlType.None;
+        }
+
+        /// <summary>
+        /// The position of the points in the cropping box
+        /// </summary>
+        /// <param name="point"></param>
+        /// <param name="isIgnore"></param>
+        /// <returns></returns>
+        public PointControlType GetHitCropControlIndex(Point point, bool isIgnore = true)
+        {
+            List<Point> controlCurrentPoints = GetControlPoint(drawRect);
+            HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
+            if (hitResult != null && hitResult.VisualHit is DrawingVisual)
+            {
+                List<PointControlType> ignoreList = GetIgnorePoints();
+
+                List<Point> IgnorePointsList = new List<Point>();
+                foreach (PointControlType type in ignoreList)
+                {
+                    if ((int)type < controlCurrentPoints.Count)
+                    {
+                        IgnorePointsList.Add(controlCurrentPoints[(int)type]);
+                    }
+                }
+                for (int i = 0; i < controlCurrentPoints.Count; i++)
+                {
+                    Point checkPoint = controlCurrentPoints[i];
+
+                    if (isIgnore && IgnorePointsList.Contains(checkPoint))
+                    {
+                        continue;
+                    }
+                    switch (currentDrawPointType)
+                    {
+                        case DrawPointType.Circle:
+                            Vector checkVector = checkPoint - point;
+                            if (checkVector.Length < pointSize)
+                            {
+                                return (PointControlType)i;
+                            }
+                            break;
+                        case DrawPointType.Square:
+
+                            Rect checkRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
+                            if (checkRect.Contains(point))
+                            {
+                                return (PointControlType)i;
+                            }
+                            break;
+
                         case DrawPointType.Crop:
                             Rect cropRect = new Rect(Math.Max(checkPoint.X - pointSize, 0), Math.Max(checkPoint.Y - pointSize, 0), pointSize * 2, pointSize * 2);
                             if (cropRect.Contains(point))
@@ -706,9 +865,9 @@ namespace ComPDFKit.Tool.DrawTool
                 }
                 if (drawRect.Contains(point))
                 {
-                    double rectWidth = (drawRect.Width - 2 * rectPadding > 0)? drawRect.Width - 2 * rectPadding: 0;
-                    double rectHeight = (drawRect.Height - 2 * rectPadding > 0)? drawRect.Height - 2 * rectPadding: 0;
-                    Rect rect = new Rect(Math.Max(drawRect.X + rectPadding,0),Math.Max( drawRect.Y + rectPadding,0), rectWidth, rectHeight);
+                    double rectWidth = (drawRect.Width - 2 * rectPadding > 0) ? drawRect.Width - 2 * rectPadding : 0;
+                    double rectHeight = (drawRect.Height - 2 * rectPadding > 0) ? drawRect.Height - 2 * rectPadding : 0;
+                    Rect rect = new Rect(Math.Max(drawRect.X + rectPadding, 0), Math.Max(drawRect.Y + rectPadding, 0), rectWidth, rectHeight);
                     if (rect.Contains(point))
                     {
                         if (!ignoreList.Contains(PointControlType.Body))

+ 54 - 16
Demo/Examples/ComPDFKit.Tool/DrawTool/SelectedRect.protected.cs

@@ -12,7 +12,9 @@ namespace ComPDFKit.Tool.DrawTool
         /// <summary>
         /// Current control point drawing style.
         /// </summary>
-        protected DrawPointType currentDrawPointType { get; set; }
+        protected DrawPointType currentDrawPointType { get; 
+            set; }
+
 
         /// <summary>
         /// Current drag drawing style.
@@ -94,6 +96,11 @@ namespace ComPDFKit.Tool.DrawTool
         /// </summary>
         protected Point moveOffset { get; set; } = new Point(0, 0);
 
+        /// <summary>
+        /// Current drawing rectangle (calculated during operation).
+        /// </summary>
+        protected Rect clipRect { get; set; } = new Rect(0, 0, 0, 0);
+
         /// <summary>
         /// Current actual display width and height of PDFVIewer.
         /// </summary>
@@ -135,6 +142,23 @@ namespace ComPDFKit.Tool.DrawTool
             controlPoints.Add(new Point(centerX, currentRect.Top));
         }
 
+        protected List<Point> GetControlPoint(Rect currentRect)
+        { List<Point> controlCurrentPoints = new List<Point>();
+            controlCurrentPoints.Clear();
+            int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
+            int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
+
+            controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Top));
+            controlCurrentPoints.Add(new Point(currentRect.Left, centerY));
+            controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
+            controlCurrentPoints.Add(new Point(centerX, currentRect.Bottom));
+            controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
+            controlCurrentPoints.Add(new Point(currentRect.Right, centerY));
+            controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Top));
+            controlCurrentPoints.Add(new Point(centerX, currentRect.Top));
+            return controlCurrentPoints;
+        }
+
         /// <summary>
         /// Calcuate the offset of the current rectangle in the maximum rectangle range
         /// </summary>
@@ -656,60 +680,74 @@ namespace ComPDFKit.Tool.DrawTool
 
         protected void DrawCropPoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
         {
-            GeometryGroup controlGroup = new GeometryGroup();
-            controlGroup.FillRule = FillRule.Nonzero;
-
+            //GeometryGroup controlGroup = new GeometryGroup();
+            //controlGroup.FillRule = FillRule.Nonzero;
+            clipRect = drawRect;
+            List<Point> controlCurrentPoints=GetControlPoint(drawRect);
+            CombinedGeometry controlGroup = new CombinedGeometry();
+            RectangleGeometry paintGeometry = new RectangleGeometry();
+            paintGeometry.Rect = SetDrawRect;
+            controlGroup.Geometry1 = paintGeometry;
+            RectangleGeometry moveGeometry = new RectangleGeometry();
+            Rect clippedBorder = drawRect;
+            if (clippedBorder.IsEmpty == false)
+            {
+                moveGeometry.Rect = drawRect;
+            }
+            controlGroup.Geometry2 = moveGeometry;
+            controlGroup.GeometryCombineMode = GeometryCombineMode.Exclude;
             //Left Top Corner
             if (!ignoreList.Contains(PointControlType.LeftTop))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[0].X - PointSize, controlPoints[0].Y - PointSize, PointSize, PointSize * 4));
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[0].X - PointSize, controlPoints[0].Y - PointSize, PointSize * 4, PointSize));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize, PointSize * 4));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize * 4, PointSize));
             }
 
             //Left Center
             if (!ignoreList.Contains(PointControlType.LeftMiddle))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[1].X - PointSize, (controlPoints[1].Y + controlPoints[1].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[1].X - PointSize, (controlCurrentPoints[1].Y + controlCurrentPoints[1].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
             }
 
             //Left Bottom Corner
             if (!ignoreList.Contains(PointControlType.LeftBottom))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[2].X - PointSize, controlPoints[2].Y - PointSize * 3, PointSize, PointSize * 4));
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[2].X - PointSize, controlPoints[2].Y, PointSize * 4, PointSize));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y - PointSize * 3, PointSize, PointSize * 4));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y, PointSize * 4, PointSize));
             }
 
             //Bottom Center
             if (!ignoreList.Contains(PointControlType.MiddlBottom))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlPoints[3].X + controlPoints[3].X - PointSize * 5) / 2, controlPoints[3].Y, PointSize * 5, PointSize));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[3].X + controlCurrentPoints[3].X - PointSize * 5) / 2, controlCurrentPoints[3].Y, PointSize * 5, PointSize));
             }
 
             //Bottom Right Corner
             if (!ignoreList.Contains(PointControlType.RightBottom))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[4].X, controlPoints[4].Y - PointSize * 3, PointSize, PointSize * 4));
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[4].X - PointSize * 3, controlPoints[4].Y, PointSize * 4, PointSize));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X, controlCurrentPoints[4].Y - PointSize * 3, PointSize, PointSize * 4));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X - PointSize * 3, controlCurrentPoints[4].Y, PointSize * 4, PointSize));
             }
 
             //Right Center
             if (!ignoreList.Contains(PointControlType.RightMiddle))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[5].X, (controlPoints[5].Y + controlPoints[5].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[5].X, (controlCurrentPoints[5].Y + controlCurrentPoints[5].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
             }
 
             //Right Top Corner
             if (!ignoreList.Contains(PointControlType.RightTop))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[6].X, controlPoints[6].Y - PointSize, PointSize, PointSize * 4));
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlPoints[6].X - PointSize * 4, controlPoints[6].Y - PointSize, PointSize * 4, PointSize));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X, controlCurrentPoints[6].Y - PointSize, PointSize, PointSize * 4));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X - PointSize * 4, controlCurrentPoints[6].Y - PointSize, PointSize * 4, PointSize));
             }
 
             //Top Center
             if (!ignoreList.Contains(PointControlType.MiddleTop))
             {
-                drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlPoints[7].X + controlPoints[7].X - PointSize * 5) / 2, controlPoints[7].Y - PointSize, PointSize * 5, PointSize));
+                drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[7].X + controlCurrentPoints[7].X - PointSize * 5) / 2, controlCurrentPoints[7].Y - PointSize, PointSize * 5, PointSize));
             }
+            BorderBrush = new SolidColorBrush(Color.FromArgb(0x3F, 0x00, 0x00, 0x00));
             drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
         }