AlignmentsHelp.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Markup;
  10. namespace ComPDFKit.Tool
  11. {
  12. /// <summary>
  13. /// Help class for calculating the alignment position of the rectangle
  14. /// </summary>
  15. public class AlignmentsHelp
  16. {
  17. /// <summary>
  18. /// Set the source rectangle to be left-aligned in the target rectangle
  19. /// </summary>
  20. /// <param name="src">
  21. /// Origin rectangle
  22. /// </param>
  23. /// <param name="dst">
  24. /// Target rectangle
  25. /// </param>
  26. /// <returns>
  27. /// X Y direction distance required for alignment of the source rectangle
  28. /// </returns>
  29. public static Point SetAlignLeft(Rect src, Rect dst)
  30. {
  31. Point movePoint = new Point(dst.Left - src.Left, 0);
  32. return movePoint;
  33. }
  34. /// <summary>
  35. /// Set the source rectangle to be horizontally centered in the target rectangle
  36. /// </summary>
  37. /// <param name="src">
  38. /// Origin rectangle
  39. /// </param>
  40. /// <param name="dst">
  41. /// Target rectangle
  42. /// </param>
  43. /// <returns>
  44. /// X Y direction distance required for alignment of the source rectangle
  45. /// </returns>
  46. public static Point SetAlignHorizonCenter(Rect src, Rect dst)
  47. {
  48. Point movePoint = new Point((dst.Left + dst.Right - src.Left - src.Right) / 2, 0);
  49. return movePoint;
  50. }
  51. /// <summary>
  52. /// Right-align the source rectangle in the target rectangle
  53. /// </summary>
  54. /// <param name="src">
  55. /// Origin rectangle
  56. /// </param>
  57. /// <param name="dst">
  58. /// Target rectangle
  59. /// </param>
  60. /// <returns>
  61. /// X Y direction distance required for alignment of the source rectangle
  62. /// </returns>
  63. public static Point SetAlignRight(Rect src, Rect dst)
  64. {
  65. Point movePoint = new Point(dst.Right - src.Width - src.Left, 0);
  66. return movePoint;
  67. }
  68. /// <summary>
  69. /// Set the source rectangle to be top-aligned in the target rectangle
  70. /// </summary>
  71. /// <param name="src">
  72. /// Origin rectangle
  73. /// </param>
  74. /// <param name="dst">
  75. /// Target rectangle
  76. /// </param>
  77. /// <returns>
  78. /// X Y direction distance required for alignment of the source rectangle
  79. /// </returns>
  80. public static Point SetAlignTop(Rect src, Rect dst)
  81. {
  82. Point movePoint = new Point(0, dst.Top - src.Top);
  83. return movePoint;
  84. }
  85. /// <summary>
  86. /// Set the source rectangle to be vertically centered in the target rectangle
  87. /// </summary>
  88. /// <param name="src">
  89. /// Origin rectangle
  90. /// </param>
  91. /// <param name="dst">
  92. /// Target rectangle
  93. /// </param>
  94. /// <returns>
  95. /// X Y direction distance required for alignment of the source rectangle
  96. /// </returns>
  97. public static Point SetAlignVerticalCenter(Rect src, Rect dst)
  98. {
  99. Point movePoint = new Point(0, (dst.Bottom + dst.Top - src.Top - src.Bottom) / 2);
  100. return movePoint;
  101. }
  102. /// <summary>
  103. /// Set the source rectangle to be horizontally and vertically centered in the target rectangle
  104. /// </summary>
  105. /// <param name="src">
  106. /// Origin rectangle
  107. /// </param>
  108. /// <param name="dst">
  109. /// Target rectangle
  110. /// </param>
  111. /// <returns>
  112. /// X Y direction distance required for alignment of the source rectangle
  113. /// </returns>
  114. public static Point SetAlignHorizonVerticalCenter(Rect src, Rect dst)
  115. {
  116. Point movePoint = new Point((dst.Left + dst.Right - src.Left - src.Right) / 2, (dst.Bottom + dst.Top - src.Top - src.Bottom) / 2);
  117. return movePoint;
  118. }
  119. /// <summary>
  120. /// Set the source rectangle to be bottom-aligned in the target rectangle
  121. /// </summary>
  122. /// <param name="src">
  123. /// Origin rectangle
  124. /// </param>
  125. /// <param name="dst">
  126. /// Target rectangle
  127. /// </param>
  128. /// <returns>
  129. /// X Y direction distance required for alignment of the source rectangle
  130. /// </returns>
  131. public static Point SetAlignBottom(Rect src, Rect dst)
  132. {
  133. Point movePoint = new Point(0, dst.Bottom - src.Height - src.Top);
  134. return movePoint;
  135. }
  136. /// <summary>
  137. /// Set the source rectangle to be horizontally distributed and aligned in the target rectangle
  138. /// </summary>
  139. /// <param name="src">
  140. /// Array of source rectangles needed
  141. /// </param>
  142. /// <param name="dst">
  143. /// Target rectangle
  144. /// </param>
  145. /// <returns>
  146. /// Dictionary of XY direction distance required for alignment of each source rectangle
  147. /// </returns>
  148. public static Dictionary<Rect, Point> SetDistributeHorizontal(List<Rect> src, Rect dst)
  149. {
  150. Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
  151. List<double> Leftlist = new List<double>();
  152. // Sort the data according to the leftmost position of each rectangle, not the array order
  153. foreach (Rect srcRect in src)
  154. {
  155. Leftlist.Add(srcRect.Left + srcRect.Width / 2);
  156. }
  157. double[] datalist = Leftlist.ToArray();
  158. Sort(datalist, 0, Leftlist.Count - 1);
  159. double startX = dst.Left;
  160. double endX = dst.Right;
  161. double interval = (endX - startX) / Leftlist.Count;
  162. for (int i = 0; i < datalist.Count(); i++)
  163. {
  164. int index = Leftlist.IndexOf(datalist[i]);
  165. Point movePoint = new Point(startX + i * interval - src[index].Left - src[index].Width / 2, 0);
  166. dictionary.Add(src[index], movePoint);
  167. }
  168. return dictionary;
  169. }
  170. /// <summary>
  171. /// Vertically distribute the source rectangles within the target rectangle (sorting based on the leftmost position of each rectangle, not the array order)
  172. /// </summary>
  173. /// <param name="src">
  174. /// Array of source rectangles needed
  175. /// </param>
  176. /// <param name="dst">
  177. /// Target rectangle
  178. /// </param>
  179. /// <returns>
  180. /// Dictionary of XY direction distance required for alignment of each source rectangle
  181. /// </returns>
  182. public static Dictionary<Rect, Point> SetDistributeVertical(List<Rect> src, Rect dst)
  183. {
  184. Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
  185. List<double> Leftlist = new List<double>();
  186. // Sort the data according to the leftmost position of each rectangle, not the array order
  187. foreach (Rect srcRect in src)
  188. {
  189. Leftlist.Add(srcRect.Left + srcRect.Width / 2);
  190. }
  191. double[] datalist = Leftlist.ToArray();
  192. Sort(datalist, 0, Leftlist.Count - 1);
  193. double startY = dst.Top;
  194. double endY = dst.Bottom;
  195. double interval = (endY - startY) / Leftlist.Count;
  196. for (int i = 0; i < datalist.Count(); i++)
  197. {
  198. int index = Leftlist.IndexOf(datalist[i]);
  199. Point movePoint = new Point(0, startY + i * interval - src[index].Top - src[index].Height / 2);
  200. dictionary.Add(src[index], movePoint);
  201. }
  202. return dictionary;
  203. }
  204. /// <summary>
  205. /// Set the source rectangle to a horizontal distribution and align it within the target rectangle to maintain consistent gaps
  206. /// </summary>
  207. /// <param name="src">
  208. /// Array of source rectangles needed
  209. /// </param>
  210. /// <param name="dst">
  211. /// Target rectangle
  212. /// </param>
  213. /// <returns>
  214. /// Dictionary of XY direction distance required for alignment of each source rectangle
  215. /// </returns>
  216. public static Dictionary<Rect, Point> SetGapDistributeHorizontal(List<Rect> src, Rect dst)
  217. {
  218. Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
  219. List<double> Leftlist = new List<double>();
  220. // Sort the data according to the leftmost position of each rectangle, not the array order
  221. double weight = 0;
  222. foreach (Rect srcRect in src)
  223. {
  224. double left = srcRect.Left;
  225. if (Leftlist.Contains(left))
  226. {
  227. left += src.IndexOf(srcRect) * 0.01;
  228. }
  229. Leftlist.Add(left);
  230. weight += srcRect.Width;
  231. }
  232. double[] datalist = Leftlist.ToArray();
  233. Sort(datalist, 0, Leftlist.Count - 1);
  234. double startX = dst.Left;
  235. double endX = dst.Right;
  236. double interval = ((endX - startX) - weight) / (Leftlist.Count - 1);
  237. for (int i = 0; i < datalist.Count(); i++)
  238. {
  239. int index = Leftlist.IndexOf(datalist[i]);
  240. Point movePoint = new Point();
  241. if (i == 0 || i == datalist.Count() - 1)
  242. {
  243. movePoint = new Point(0, 0);
  244. }
  245. else
  246. {
  247. double width = 0;
  248. for (int f = 0; f < i; f++)
  249. {
  250. int index2 = 0;
  251. if (f != 0)
  252. {
  253. index2 = Leftlist.IndexOf(datalist[i - 1]);
  254. width += src[index2].Width;
  255. }
  256. int index0 = Leftlist.IndexOf(datalist[0]);
  257. if (f == 0)
  258. {
  259. width += src[index0].Right;
  260. }
  261. width += interval;
  262. }
  263. movePoint = new Point(width - src[index].X , 0);
  264. }
  265. dictionary.Add(src[index], movePoint);
  266. }
  267. return dictionary;
  268. }
  269. /// <summary>
  270. /// Vertically distribute source rectangles within the target rectangle to maintain consistent gaps (sorted by the leftmost position of each rectangle, rather than array order)
  271. /// </summary>
  272. /// <param name="src">
  273. /// Array of source rectangles needed
  274. /// </param>
  275. /// <param name="dst">
  276. /// Target rectangle
  277. /// </param>
  278. /// <returns>
  279. /// Dictionary of XY direction distance required for alignment of each source rectangle
  280. /// </returns>
  281. public static Dictionary<Rect, Point> SetGapDistributeVertical(List<Rect> src, Rect dst)
  282. {
  283. Dictionary<Rect, Point> dictionary = new Dictionary<Rect, Point>();
  284. List<double> Leftlist = new List<double>();
  285. // Sort the data according to the leftmost position of each rectangle, not the array order
  286. double tall = 0;
  287. foreach (Rect srcRect in src)
  288. {
  289. double top = srcRect.Top;
  290. if (Leftlist.Contains(top)) {
  291. top += src.IndexOf(srcRect)*0.01;
  292. }
  293. Leftlist.Add(top);
  294. tall += srcRect.Height;
  295. }
  296. double[] datalist = Leftlist.ToArray();
  297. Sort(datalist, 0, Leftlist.Count - 1);
  298. double startY = dst.Top;
  299. double endY = dst.Bottom;
  300. double interval = ((endY - startY) - tall) / (Leftlist.Count - 1);
  301. for (int i = 0; i < datalist.Count(); i++)
  302. {
  303. int index = Leftlist.IndexOf(datalist[i]);
  304. Point movePoint = new Point();
  305. if (i == 0 || i == datalist.Count() - 1)
  306. {
  307. movePoint = new Point(0, 0);
  308. }
  309. else
  310. {
  311. double height = 0;
  312. for (int f = 0; f < i; f++)
  313. {
  314. int index2 = 0;
  315. if (f != 0)
  316. {
  317. index2 = Leftlist.IndexOf(datalist[i - 1]);
  318. height += src[index2].Height;
  319. }
  320. int index0 = Leftlist.IndexOf(datalist[0]);
  321. if (f == 0)
  322. {
  323. height += src[index0].Bottom;
  324. }
  325. height +=interval;
  326. }
  327. movePoint = new Point(0, height - src[index].Y);
  328. }
  329. dictionary.Add(src[index], movePoint);
  330. }
  331. return dictionary;
  332. }
  333. #region Quick sort
  334. private static int SortUnit(double[] array, int low, int high)
  335. {
  336. double key = array[low];
  337. while (low < high)
  338. {
  339. // Search backward for values smaller than the key
  340. while (array[high] >= key && high > low)
  341. --high;
  342. // Put the value smaller than key on the left
  343. array[low] = array[high];
  344. // Search forward for values larger than the key
  345. while (array[low] <= key && high > low)
  346. ++low;
  347. // Put the value larger than key on the right
  348. array[high] = array[low];
  349. }
  350. // Left is smaller than key, right is larger than key.
  351. // Put the key in the current position of the cursor
  352. // At this point, 'low' equals 'high
  353. array[low] = key;
  354. return high;
  355. }
  356. private static void Sort(double[] array, int low, int high)
  357. {
  358. if (low >= high)
  359. return;
  360. // Finish a single unit sort
  361. int index = SortUnit(array, low, high);
  362. // Sort the left unit
  363. Sort(array, low, index - 1);
  364. // Sort the right unit
  365. Sort(array, index + 1, high);
  366. }
  367. #endregion
  368. }
  369. }