Browse Source

ComPDFKit.Tolol(win)-水平平分和垂直平分逻辑优化

liyijie 8 months ago
parent
commit
2d85901cd6

+ 139 - 1
Demo/Examples/ComPDFKit.Tool/AlignmentsHelp.cs

@@ -2,6 +2,7 @@
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
@@ -44,7 +45,7 @@ namespace ComPDFKit.Tool
         /// <returns>
         /// X Y direction distance required for alignment of the source rectangle
         /// </returns>
-        public static Point SetAlignHorizonCenter (Rect src, Rect dst)
+        public static Point SetAlignHorizonCenter(Rect src, Rect dst)
         {
             Point movePoint = new Point((dst.Left + dst.Right - src.Left - src.Right) / 2, 0);
             return movePoint;
@@ -214,6 +215,143 @@ namespace ComPDFKit.Tool
             return dictionary;
         }
 
+        /// <summary>
+        /// Set the source rectangle to a horizontal distribution and align it within the target rectangle to maintain consistent gaps
+        /// </summary>
+        /// <param name="src">
+        /// Array of source rectangles needed
+        /// </param>
+        /// <param name="dst">
+        /// Target rectangle
+        /// </param>
+        /// <returns>
+        /// Dictionary of XY direction distance required for alignment of each source rectangle
+        /// </returns>
+        public static Dictionary<Rect, Point> SetGapDistributeHorizontal(List<Rect> src, Rect dst)
+        {
+            Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
+            List<double> Leftlist = new List<double>();
+
+            // Sort the data according to the leftmost position of each rectangle, not the array order
+            double weight = 0;
+            foreach (Rect srcRect in src)
+            {
+                double left = srcRect.Left;
+                if (Leftlist.Contains(left))
+                {
+                    left += src.IndexOf(srcRect) * 0.01;
+                }
+                Leftlist.Add(left);
+                weight += srcRect.Width;
+            }
+            double[] datalist = Leftlist.ToArray();
+            Sort(datalist, 0, Leftlist.Count - 1);
+
+            double startX = dst.Left;
+            double endX = dst.Right;
+            double interval = ((endX - startX) - weight) / (Leftlist.Count - 1);
+            for (int i = 0; i < datalist.Count(); i++)
+            {
+                int index = Leftlist.IndexOf(datalist[i]);
+                Point movePoint = new Point();
+                if (i == 0 || i == datalist.Count() - 1)
+                {
+                    movePoint = new Point(0, 0);
+                }
+                else
+                {
+                    double width = 0;
+                    for (int f = 0; f < i; f++)
+                    {
+                        int index2 = 0;
+                        if (f != 0)
+                        {
+                            index2 = Leftlist.IndexOf(datalist[i - 1]);
+                            width += src[index2].Width;
+                        }
+                        int index0 = Leftlist.IndexOf(datalist[0]);
+                        if (f == 0)
+                        {
+                            width += src[index0].Right;
+                        }
+                        width += interval;
+
+                    }
+                    movePoint = new Point(width - src[index].X , 0);
+                }
+                dictionary.Add(src[index], movePoint);
+            }
+            return dictionary;
+        }
+
+        /// <summary>
+        /// Vertically distribute source rectangles within the target rectangle to maintain consistent gaps (sorted by the leftmost position of each rectangle, rather than array order)
+        /// </summary>
+        /// <param name="src">
+        /// Array of source rectangles needed
+        /// </param>
+        /// <param name="dst">
+        /// Target rectangle
+        /// </param>
+        /// <returns>
+        /// Dictionary of XY direction distance required for alignment of each source rectangle
+        /// </returns>
+        public static Dictionary<Rect, Point> SetGapDistributeVertical(List<Rect> src, Rect dst)
+        {
+            Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
+            List<double> Leftlist = new List<double>();
+
+            // Sort the data according to the leftmost position of each rectangle, not the array order
+            double tall = 0;
+            foreach (Rect srcRect in src)
+            {
+                double top = srcRect.Top;
+                if (Leftlist.Contains(top)) {
+                    top += src.IndexOf(srcRect)*0.01;
+                }
+                Leftlist.Add(top);
+                tall += srcRect.Height;
+            }
+            double[] datalist = Leftlist.ToArray();
+            Sort(datalist, 0, Leftlist.Count - 1);
+
+            double startY = dst.Top;
+            double endY = dst.Bottom;
+            double interval = ((endY - startY) - tall) / (Leftlist.Count - 1);
+            for (int i = 0; i < datalist.Count(); i++)
+            {
+                int index = Leftlist.IndexOf(datalist[i]);
+                Point movePoint = new Point();
+                if (i == 0 || i == datalist.Count() - 1)
+                {
+                    movePoint = new Point(0, 0);
+                }
+                else
+                {
+                    double height = 0;
+                    for (int f = 0; f < i; f++)
+                    {
+                        int index2 = 0;
+                        if (f != 0)
+                        {
+                            index2 = Leftlist.IndexOf(datalist[i - 1]);
+                            height += src[index2].Height;
+                        }
+                        int index0 = Leftlist.IndexOf(datalist[0]);
+                        if (f == 0)
+                        {
+                            height += src[index0].Bottom;
+                        }
+                        height +=interval;
+
+                    }
+                    movePoint = new Point(0, height - src[index].Y);
+                }
+                dictionary.Add(src[index], movePoint);
+            }
+            return dictionary;
+        }
+
         #region Quick sort
 
         private static int SortUnit(double[] array, int low, int high)

+ 2 - 14
Demo/Examples/ComPDFKit.Tool/CPDFViewerTool.TextEdit.cs

@@ -2533,11 +2533,9 @@ namespace ComPDFKit.Tool
             foreach (SelectedRect item in MultiSelectEditList.Children)
             {
                 Rect rect = item.GetRect();
-                rect.X = rect.X + editPadding;
-                rect.Y = rect.Y - editPadding;
                 rects.Add(rect);
             }
-            Dictionary<Rect, Point> rectandpoint = AlignmentsHelp.SetDistributeHorizontal(rects, drawRect);
+            Dictionary<Rect, Point> rectandpoint = AlignmentsHelp.SetGapDistributeHorizontal(rects, drawRect);
             foreach (SelectedRect item in MultiSelectEditList.Children)
             {
                 PDFEditHistory pDFEditHistory = new PDFEditHistory();
@@ -2546,12 +2544,8 @@ namespace ComPDFKit.Tool
                 EditAreaObject editAreaObject = GetEditAreaObjectListForRect(item);
                 EditAreaObject newEditAreaObject = GetSelectedForIndex(multiPage, editAreaObject.EditAreaIndex);
                 Rect rect = item.GetRect();
-                rect.X = rect.X + editPadding;
-                rect.Y = rect.Y - editPadding;
                 item.SetRect(GetDrawAlignRect(rectandpoint[rect], rect, drawRect), currentZoom);
                 Rect rect2 = item.GetRect();
-                rect2.X = rect2.X + editPadding;
-                rect2.Y = rect2.Y - editPadding;
                 Rect pageBound = newEditAreaObject.PageBound;
                 Rect PDFRect = DpiHelper.StandardRectToPDFRect(new Rect((rect2.Left - pageBound.Left) / currentZoom, (rect2.Top - pageBound.Top) / currentZoom, rect2.Width / currentZoom, rect2.Height / currentZoom));
                 editAreaObject.cPDFEditArea.SetFrame(DataConversionForWPF.RectConversionForCRect(PDFRect));
@@ -2589,11 +2583,9 @@ namespace ComPDFKit.Tool
             foreach (SelectedRect item in MultiSelectEditList.Children)
             {
                 Rect rect = item.GetRect();
-                rect.Y = rect.Y + editPadding;
-                rect.X = rect.X - editPadding;
                 rects.Add(rect);
             }
-            Dictionary<Rect, Point> rectandpoint = AlignmentsHelp.SetDistributeVertical(rects, drawRect);
+            Dictionary<Rect, Point> rectandpoint = AlignmentsHelp.SetGapDistributeVertical(rects, drawRect);
             foreach (SelectedRect item in MultiSelectEditList.Children)
             {
                 PDFEditHistory pDFEditHistory = new PDFEditHistory();
@@ -2602,12 +2594,8 @@ namespace ComPDFKit.Tool
                 EditAreaObject editAreaObject = GetEditAreaObjectListForRect(item);
                 EditAreaObject newEditAreaObject = GetSelectedForIndex(multiPage, editAreaObject.EditAreaIndex);
                 Rect rect = item.GetRect();
-                rect.Y = rect.Y + editPadding;
-                rect.X = rect.X - editPadding;
                 item.SetRect(GetDrawAlignRect(rectandpoint[rect], rect, drawRect), currentZoom);
                 Rect rect2 = item.GetRect();
-                rect2.Y = rect2.Y + editPadding;
-                rect2.X = rect2.X - editPadding;
                 Rect pageBound = newEditAreaObject.PageBound;
                 Rect PDFRect = DpiHelper.StandardRectToPDFRect(new Rect((rect2.Left - pageBound.Left) / currentZoom, (rect2.Top - pageBound.Top) / currentZoom, rect2.Width / currentZoom, rect2.Height / currentZoom));
                 editAreaObject.cPDFEditArea.SetFrame(DataConversionForWPF.RectConversionForCRect(PDFRect));