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

ComPDFKit.Tool(Win) - 修复直线测量按住控制点旋转时效果异常问题;修复直线测量注释从右往左创建后第一次点击中间点拖拽时效果异常问题

liyuxuan пре 2 месеци
родитељ
комит
4f2ea5e136
1 измењених фајлова са 162 додато и 96 уклоњено
  1. 162 96
      Demo/Examples/ComPDFKit.Tool/DrawTool/AnnotEdit.cs

+ 162 - 96
Demo/Examples/ComPDFKit.Tool/DrawTool/AnnotEdit.cs

@@ -79,6 +79,7 @@ namespace ComPDFKit.Tool.DrawTool
         /// Move offset during movement
         /// </summary>
         protected Point moveOffset { get; set; } = new Point(0, 0);
+        private Point LineMeasureOffset { get; set; }= new Point(0, 0);
 
         /// <summary>
         /// Point size
@@ -160,6 +161,7 @@ namespace ComPDFKit.Tool.DrawTool
                 case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
                     break;
                 case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
+                    List<Point> sortPointList= new List<Point>();
                     for (int i = 0; i < (annotData.Annot as CPDFLineAnnotation).Points.Length; i++)
                     {
                         Point point = DpiHelper.PDFPointToStandardPoint(new Point((annotData.Annot as CPDFLineAnnotation).Points[i].x, (annotData.Annot as CPDFLineAnnotation).Points[i].y));
@@ -167,8 +169,14 @@ namespace ComPDFKit.Tool.DrawTool
                             point.X * annotData.CurrentZoom + annotData.PaintOffset.X - annotData.CropLeft * annotData.CurrentZoom,
                             point.Y * annotData.CurrentZoom + annotData.PaintOffset.Y - annotData.CropTop * annotData.CurrentZoom
                             );
-                        activePoints.Add(point);
+                        sortPointList.Add(point);
+                    }
+                    sortPointList.Sort((a, b) => { if (a.X > b.X) return 1; else return 0; });
+                    foreach (Point checkPoint in sortPointList)
+                    {
+                        activePoints.Add(checkPoint);
                     }
+                   
                     if ((annotData.Annot as CPDFLineAnnotation).IsMeasured())
                     {
                         CRect rawRect = annotData.Annot.GetRect();
@@ -327,12 +335,12 @@ namespace ComPDFKit.Tool.DrawTool
             Point leftCrossPoint = rotateMatrix.Transform(leadCrossVector) + activePoints[0];
 
             leftLine.Add(leftStartPoint);
-            leftLine.Add(leftCrossPoint);
+            leftLine.Add(leftEndPoint);
             crossLine.Add(leftCrossPoint);
 
             lineVector = activePoints[1] - activePoints[0];
             rightLine.Add(leftStartPoint + lineVector);
-            rightLine.Add(leftCrossPoint + lineVector);
+            rightLine.Add(leftEndPoint + lineVector);
             crossLine.Add(leftCrossPoint + lineVector);
             activePoints.Insert(1, new Point(
                 (crossLine[0].X + crossLine[1].X) / 2,
@@ -348,6 +356,7 @@ namespace ComPDFKit.Tool.DrawTool
             hitIndex = -1;
             mouseDownPoint = downPoint;
             moveOffset = new Point(0, 0);
+            LineMeasureOffset=new Point(0, 0);
             HitTestResult hitResult = VisualTreeHelper.HitTest(this, downPoint);
             if (hitResult != null && hitResult.VisualHit is DrawingVisual)
             {
@@ -369,6 +378,7 @@ namespace ComPDFKit.Tool.DrawTool
                     );
 
                 Point movePoint = CheckMoveOffSet(activePoints.ToList(), maxRect, newOffset);
+                LineMeasureOffset=newOffset;
                 if(movePoint.X==0)
                 {
                     newOffset.X=moveOffset.X;
@@ -625,19 +635,31 @@ namespace ComPDFKit.Tool.DrawTool
             }
         }
 
-        private void DrawLineMeasure(DrawingContext drawingContext)
+        private Point CalcRotateBound(Point rawPoint, Point moveOffset)
+        {
+            Point newPoint = rawPoint;
+            Rect checkRect = annotData.PaintOffset;
+            newPoint.X += moveOffset.X;
+            newPoint.Y += moveOffset.Y;
+            newPoint.X = Math.Max(checkRect.Left, newPoint.X);
+            newPoint.X = Math.Min(checkRect.Right, newPoint.X);
+            newPoint.Y = Math.Max(checkRect.Top, newPoint.Y);
+            newPoint.Y = Math.Min(checkRect.Bottom, newPoint.Y);
+
+            return newPoint;
+        }
+
+        private void DrawLineMeasure(DrawingContext drawDC)
         {
             foreach (Point controlPoint in activePoints)
             {
-                Point drawPoint = new Point(
-                    controlPoint.X,
-                    controlPoint.Y);
+                Point drawPoint = new Point(controlPoint.X,controlPoint.Y);
                 if (hitIndex == -1)
                 {
                     drawPoint.X += moveOffset.X;
                     drawPoint.Y += moveOffset.Y;
                 }
-                drawingContext?.DrawEllipse(drawParam.EditControlLineBrush, drawParam.EditControlLinePen, (drawPoint), pointSize, pointSize);
+                drawDC?.DrawEllipse(drawParam.EditControlLineBrush, drawParam.EditControlLinePen, (drawPoint), pointSize, pointSize);
             }
             Rect drawRect = activeRect;
             drawRect.X += moveOffset.X;
@@ -650,44 +672,52 @@ namespace ComPDFKit.Tool.DrawTool
                 moveLeftLine = leftLine.ToArray();
                 moveRightLine = rightLine.ToArray();
                 moveCrossLine = crossLine.ToArray();
-                switch (hitIndex)
-                { 
-                    case 0://Left
-                        {
-                            moveLeftLine[0].X += moveOffset.X;
-                            moveLeftLine[0].Y += moveOffset.Y;
+                foreach(Point checkPoint in moveLeftLine)
+                {
+                    drawLeftPoints.Add(checkPoint);
+                }
+
+                foreach (Point checkPoint in moveRightLine)
+                {
+                    drawRightPoints.Add(checkPoint);
+                }
 
-                            moveLeftLine[0].X = Math.Max(maxRect.Left, moveLeftLine[0].X);
-                            moveLeftLine[0].X = Math.Min(maxRect.Right, moveLeftLine[0].X);
-                            moveLeftLine[0].Y = Math.Max(maxRect.Top, moveLeftLine[0].Y);
-                            moveLeftLine[0].Y = Math.Min(maxRect.Bottom, moveLeftLine[0].Y);
+                foreach (Point checkPoint in moveCrossLine)
+                {
+                    drawCrossPoints.Add(checkPoint);
+                }
 
-                            Vector newVector = moveLeftLine[0] - rightLine[0];
-                            Vector leftVector = leftLine[1] - leftLine[0];
+                switch (hitIndex)
+                {
+                    case 0:
+                        {
+                            moveLeftLine[0] = CalcRotateBound(drawLeftPoints[0], LineMeasureOffset);
 
-                            double angle = leftLine[0].Y < crossLine[0].Y ? -90 : 90;
+                            Vector newVector = moveLeftLine[0] - drawRightPoints[0];
+                            Vector leftVector = drawLeftPoints[1] - drawLeftPoints[0];
+                            double angle = drawLeftPoints[0].Y < drawCrossPoints[0].Y ? -90 : 90;
                             newVector.Normalize();
                             newVector = newVector * leftVector.Length;
                             Matrix rotateMatrix = new Matrix();
                             rotateMatrix.Rotate(angle);
+                            moveRightLine[0] = drawRightPoints[0];
                             moveLeftLine[1] = moveLeftLine[0] + newVector * rotateMatrix;
-                            moveRightLine[0] = rightLine[0];
                             moveRightLine[1] = moveRightLine[0] + newVector * rotateMatrix;
+
                             moveCrossLine[0] = moveLeftLine[1];
                             moveCrossLine[1] = moveRightLine[1];
+
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveLeftLine[0], moveLeftLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveRightLine[0], moveRightLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveLeftLine[1], moveRightLine[1]);
                         }
                         break;
-                    case 1:// Center
+                    case 1:
                         {
-                            Point centerPoint = new Point(
-                            (crossLine[0].X + crossLine[1].X) / 2,
-                            (crossLine[0].Y + crossLine[1].Y) / 2
-                            );
-                            Point movePoint = new Point(centerPoint.X, centerPoint.Y);
-                            movePoint.X += moveOffset.X;
-                            movePoint.Y += moveOffset.Y;
-                            Vector ruleVector = crossLine[1] - crossLine[0];
+                            Point centerPoint = new Point((drawCrossPoints[0].X + drawCrossPoints[1].X) / 2,(drawCrossPoints[0].Y + drawCrossPoints[1].Y) / 2);
 
+                            Point movePoint = CalcRotateBound(centerPoint, LineMeasureOffset);
+                            Vector ruleVector = drawCrossPoints[1] - drawCrossPoints[0];
                             bool rateMove = true;
                             if (ruleVector.X == 0)
                             {
@@ -699,6 +729,7 @@ namespace ComPDFKit.Tool.DrawTool
                                 movePoint.X = centerPoint.X;
                                 rateMove = false;
                             }
+
                             if (rateMove)
                             {
                                 Vector moveVector = movePoint - centerPoint;
@@ -722,98 +753,133 @@ namespace ComPDFKit.Tool.DrawTool
                                 moveCrossLine[1] = SaveRight;
                                 moveLeftLine[1] = saveLeft;
                                 moveRightLine[1] = SaveRight;
-                                moveLeftLine[0] = leftLine[0];
-                                moveRightLine[0] = rightLine[0];
+                                moveLeftLine[0] = drawLeftPoints[0];
+                                moveRightLine[0] = drawRightPoints[0];
                             }
                             else
                             {
                                 Point moveOffset = new Point(
                                     movePoint.X - centerPoint.X,
                                     movePoint.Y - centerPoint.Y);
-                                moveCrossLine[0].X += moveOffset.X;
-                                moveCrossLine[0].Y += moveOffset.Y;
-                                moveCrossLine[1].X += moveOffset.X;
-                                moveCrossLine[1].Y += moveOffset.Y;
 
-                                moveLeftLine[1].X += moveOffset.X;
-                                moveLeftLine[1].Y += moveOffset.Y;
+                                moveCrossLine[0] = new Point(
+                                    drawCrossPoints[0].X + moveOffset.X,
+                                    drawCrossPoints[0].Y + moveOffset.Y);
+                                moveCrossLine[1] = new Point(
+                                    drawCrossPoints[1].X + moveOffset.X,
+                                    drawCrossPoints[1].Y + moveOffset.Y);
+
+                                moveLeftLine[0] = drawLeftPoints[0];
+                                moveRightLine[0] = drawRightPoints[0];
+
+                                moveLeftLine[1] = new Point(
+                                    drawLeftPoints[1].X + moveOffset.X,
+                                    drawLeftPoints[1].Y + moveOffset.Y);
 
-                                moveRightLine[1].X += moveOffset.X;
-                                moveRightLine[1].Y += moveOffset.Y;
+                                moveRightLine[1] = new Point(
+                                   drawRightPoints[1].X + moveOffset.X,
+                                   drawRightPoints[1].Y + moveOffset.Y);
                             }
 
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveCrossLine[0], moveCrossLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveCrossLine[0], moveLeftLine[0]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveCrossLine[1], moveRightLine[0]);
                         }
                         break;
-                    case 2://Right
+                    case 2:
                         {
-                            moveRightLine[0].X += moveOffset.X;
-                            moveRightLine[0].Y += moveOffset.Y;
-
-                            Vector newVector = moveRightLine[0] - leftLine[0];
-                            Vector leftVector = rightLine[1] - rightLine[0];
-
-                            double angle = (rightLine[0].Y + leftLine[0].Y) / 2 > (crossLine[0].Y + crossLine[1].Y) / 2 ? -90 : 90;
+                            moveRightLine[0] = CalcRotateBound(drawRightPoints[0], LineMeasureOffset);
+                            Vector newVector = moveRightLine[0] - drawLeftPoints[0];
+                            Vector rightVector = drawRightPoints[1] - drawRightPoints[0];
+                            double angle = (drawRightPoints[0].Y + drawLeftPoints[0].Y) / 2 > (drawCrossPoints[0].Y + drawCrossPoints[1].Y) / 2 ? -90 : 90;
                             newVector.Normalize();
-                            newVector = newVector * leftVector.Length;
+                            newVector = newVector * rightVector.Length;
                             Matrix rotateMatrix = new Matrix();
                             rotateMatrix.Rotate(angle);
+                            moveLeftLine[0] = drawLeftPoints[0];
                             moveLeftLine[1] = moveLeftLine[0] + newVector * rotateMatrix;
-                            moveLeftLine[0] = leftLine[0];
                             moveRightLine[1] = moveRightLine[0] + newVector * rotateMatrix;
+
                             moveCrossLine[0] = moveLeftLine[1];
                             moveCrossLine[1] = moveRightLine[1];
+
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveLeftLine[0], moveLeftLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveRightLine[0], moveRightLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveLeftLine[1], moveRightLine[1]);
                         }
                         break;
                     case -1:
-                        moveLeftLine[0].X += moveOffset.X;
-                        moveLeftLine[0].Y += moveOffset.Y;
-                        moveLeftLine[1].X += moveOffset.X;
-                        moveLeftLine[1].Y += moveOffset.Y;
-
-                        moveRightLine[0].X += moveOffset.X;
-                        moveRightLine[0].Y += moveOffset.Y;
-                        moveRightLine[1].X += moveOffset.X;
-                        moveRightLine[1].Y += moveOffset.Y;
-
-                        moveCrossLine[0] = moveLeftLine[1];
-                        moveCrossLine[1] = moveRightLine[1];
+                        {
+                            Rect moveRect = new Rect(moveLeftLine[0], moveRightLine[1]);
+                            moveRect.Union(moveLeftLine[1]);
+                            moveRect.Union(moveRightLine[0]);
+                            Point moveOffset = LineMeasureOffset;
+                            Rect pageBound = annotData.PaintOffset;
+                            if (moveRect.Left + LineMeasureOffset.X < pageBound.Left)
+                            {
+                                moveOffset.X = pageBound.Left - moveRect.Left;
+                            }
+                            if (moveRect.Right + moveOffset.X > pageBound.Right)
+                            {
+                                moveOffset.X = pageBound.Right - moveRect.Right;
+                            }
+                            if (moveRect.Top + moveOffset.Y < pageBound.Top)
+                            {
+                                moveOffset.Y = pageBound.Top - moveRect.Top;
+                            }
+                            if (moveRect.Bottom + moveOffset.Y > pageBound.Bottom)
+                            {
+                                moveOffset.X = pageBound.Bottom - moveRect.Bottom;
+                            }
+
+                            moveLeftLine[0].X = moveLeftLine[0].X + moveOffset.X;
+                            moveLeftLine[0].Y = moveLeftLine[0].Y + moveOffset.Y;
+
+                            moveLeftLine[1].X = moveLeftLine[1].X + moveOffset.X;
+                            moveLeftLine[1].Y = moveLeftLine[1].Y + moveOffset.Y;
+
+                            moveRightLine[0].X = moveRightLine[0].X + moveOffset.X;
+                            moveRightLine[0].Y = moveRightLine[0].Y + moveOffset.Y;
+
+                            moveRightLine[1].X = moveRightLine[1].X + moveOffset.X;
+                            moveRightLine[1].Y = moveRightLine[1].Y + moveOffset.Y;
+
+                            moveCrossLine[0].X = moveCrossLine[0].X + moveOffset.X;
+                            moveCrossLine[0].Y = moveCrossLine[0].Y + moveOffset.Y;
+
+                            moveCrossLine[1].X = moveCrossLine[1].X + moveOffset.X;
+                            moveCrossLine[1].Y = moveCrossLine[1].Y + moveOffset.Y;
+
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveLeftLine[0], moveLeftLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveRightLine[0], moveRightLine[1]);
+                            drawDC?.DrawLine(drawParam.EditLinePen, moveCrossLine[0], moveCrossLine[1]);
+                        }
                         break;
                     default:
                         break;
                 }
-                //Left
-                drawLeftPoints.Add(new Point(
-                    moveLeftLine[0].X,
-                    moveLeftLine[0].Y));
-                drawLeftPoints.Add(new Point(
-                   moveLeftLine[1].X,
-                   moveLeftLine[1].Y));
-
-                //Right
-                drawRightPoints.Add(new Point(
-                    moveRightLine[0].X,
-                    moveRightLine[0].Y));
-                drawRightPoints.Add(new Point(
-                   moveRightLine[1].X,
-                   moveRightLine[1].Y));
-
-                //Middle
-                drawCrossPoints.Add(new Point(
-                    moveCrossLine[0].X,
-                    moveCrossLine[0].Y));
-
-                drawCrossPoints.Add(new Point(
-                    moveCrossLine[1].X,
-                    moveCrossLine[1].Y));
-
-
-                drawingContext?.DrawLine(drawParam.EditLinePen, drawLeftPoints[0], drawLeftPoints[1]);
-                drawingContext?.DrawLine(drawParam.EditLinePen, drawRightPoints[0], drawRightPoints[1]);
-                drawingContext?.DrawLine(drawParam.EditLinePen, drawCrossPoints[0], drawCrossPoints[1]);
-            }
-            else
-            {
-                drawingContext?.DrawRectangle(null, drawParam.EditLinePen, drawRect);
+
+
+                if (moveLeftLine[0].X > moveRightLine[0].X)
+                {
+                    Point tmpPoint = moveRightLine[0];
+                    moveRightLine[0] = moveLeftLine[0];
+                    moveLeftLine[0] = tmpPoint;
+                }
+
+                if (moveLeftLine[1].X > moveRightLine[1].X)
+                {
+                    Point tmpPoint = moveRightLine[1];
+                    moveRightLine[1] = moveLeftLine[1];
+                    moveLeftLine[1] = tmpPoint;
+                }
+
+                if (crossLine[0].X > crossLine[1].X)
+                {
+                    Point tmpPoint = crossLine[1];
+                    crossLine[1] = crossLine[0];
+                    crossLine[0] = tmpPoint;
+                }
             }
         }