SelectedRect.protected.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. namespace ComPDFKit.Tool.DrawTool
  6. {
  7. public partial class SelectedRect
  8. {
  9. #region Properties
  10. /// <summary>
  11. /// Current control point drawing style.
  12. /// </summary>
  13. protected DrawPointType currentDrawPointType
  14. {
  15. get;
  16. set;
  17. }
  18. /// <summary>
  19. /// Current drag drawing style.
  20. /// </summary>
  21. protected DrawMoveType currentDrawMoveType { get; set; }
  22. /// <summary>
  23. /// Current click hit control point.
  24. /// </summary>
  25. protected PointControlType hitControlType { get; set; }
  26. /// <summary>
  27. /// Mouse down position information.
  28. /// </summary>
  29. protected Point mouseDownPoint { get; set; }
  30. /// <summary>
  31. /// Whether the mouse is pressed.
  32. /// </summary>
  33. protected bool isMouseDown { get; set; }
  34. /// <summary>
  35. /// Whether proportional scaling is required.
  36. /// </summary>
  37. protected bool isProportionalScaling { get; set; } = false;
  38. /// <summary>
  39. /// Current control point size.
  40. /// </summary>
  41. protected int pointSize { get; set; } = 4;
  42. /// <summary>
  43. /// Rectangular minimum width.
  44. /// </summary>
  45. protected int rectMinWidth { get; set; } = 10;
  46. /// <summary>
  47. /// Rectangular minimum height.
  48. /// </summary>
  49. protected int RectMinHeight { get; set; } = 10;
  50. /// <summary>
  51. /// Current set of ignore points.
  52. /// </summary>
  53. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  54. /// <summary>
  55. /// Current set of drawing rectangles (original data).
  56. /// </summary>
  57. protected Rect SetDrawRect { get; set; } = new Rect(0, 0, 0, 0);
  58. /// <summary>
  59. /// Current drawing rectangle (calculated during operation).
  60. /// </summary>
  61. protected Rect drawRect { get; set; } = new Rect(0, 0, 0, 0);
  62. /// <summary>
  63. /// Maximum range that can be drawn.
  64. /// </summary>
  65. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  66. /// <summary>
  67. /// Current center point of the drawing rectangle.
  68. /// </summary>
  69. protected Point drawCenterPoint { get; private set; } = new Point(0, 0);
  70. /// <summary>
  71. /// When the mouse is pressed, the cached rectangle.
  72. /// </summary>
  73. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  74. /// <summary>
  75. /// Current control point coordinates.
  76. /// </summary>
  77. protected List<Point> controlPoints { get; set; } = new List<Point>();
  78. /// <summary>
  79. /// Move offset during movement.
  80. /// </summary>
  81. protected Point moveOffset { get; set; } = new Point(0, 0);
  82. /// <summary>
  83. /// Current drawing rectangle (calculated during operation).
  84. /// </summary>
  85. protected Thickness clipThickness = new Thickness(0, 0, 0, 0);
  86. private Pen editPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
  87. private Pen editHoverPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
  88. private bool showCreatTextRect = false;
  89. protected bool isOutSideScaling = false;
  90. /// <summary>
  91. /// Current actual display width and height of PDFVIewer.
  92. /// </summary>
  93. protected double PDFViewerActualWidth { get; set; } = 0;
  94. protected double PDFViewerActualHeight { get; set; } = 0;
  95. protected double rectPadding = 6;
  96. protected double currentZoom = 1;
  97. protected SelectedAnnotData selectedRectData = new SelectedAnnotData();
  98. protected bool disable = false;
  99. #endregion
  100. #region Functions
  101. /// <summary>
  102. /// Calcuate the control points
  103. /// </summary>
  104. /// <param name="currentRect">
  105. /// Control points in the target rectangle
  106. /// </param>
  107. protected void CalcControlPoint(Rect currentRect)
  108. {
  109. controlPoints.Clear();
  110. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  111. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  112. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  113. controlPoints.Add(new Point(currentRect.Left, centerY));
  114. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  115. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  116. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  117. controlPoints.Add(new Point(currentRect.Right, centerY));
  118. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  119. controlPoints.Add(new Point(centerX, currentRect.Top));
  120. }
  121. protected List<Point> GetControlPoint(Rect currentRect)
  122. {
  123. List<Point> controlCurrentPoints = new List<Point>();
  124. controlCurrentPoints.Clear();
  125. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  126. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  127. controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Top));
  128. controlCurrentPoints.Add(new Point(currentRect.Left, centerY));
  129. controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  130. controlCurrentPoints.Add(new Point(centerX, currentRect.Bottom));
  131. controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  132. controlCurrentPoints.Add(new Point(currentRect.Right, centerY));
  133. controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Top));
  134. controlCurrentPoints.Add(new Point(centerX, currentRect.Top));
  135. return controlCurrentPoints;
  136. }
  137. /// <summary>
  138. /// Calcuate the offset of the current rectangle in the maximum rectangle range
  139. /// </summary>
  140. /// <param name="currentRect">
  141. /// The rectangle cached when pressed
  142. /// </param>
  143. /// <param name="offsetPoint">
  144. /// Equivalent to the offset value when pressed
  145. /// </param>
  146. /// <param name="maxRect">
  147. /// The maximum rectangle range.
  148. /// </param>
  149. /// <returns></returns>
  150. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  151. {
  152. double cLeft = currentRect.Left;
  153. double cRight = currentRect.Right;
  154. double cUp = currentRect.Top;
  155. double cDown = currentRect.Bottom;
  156. double TmpLeft = cLeft + offsetPoint.X;
  157. double TmpRight = cRight + offsetPoint.X;
  158. double TmpUp = cUp + offsetPoint.Y;
  159. double TmpDown = cDown + offsetPoint.Y;
  160. if (TmpLeft < maxRect.Left)
  161. {
  162. TmpRight = (cRight - cLeft) + maxRect.Left;
  163. TmpLeft = maxRect.Left;
  164. }
  165. if (TmpUp < maxRect.Top)
  166. {
  167. TmpDown = (cDown - cUp) + maxRect.Top;
  168. TmpUp = maxRect.Top;
  169. }
  170. if (TmpRight > maxRect.Right)
  171. {
  172. TmpLeft = maxRect.Right - (cRight - cLeft);
  173. TmpRight = maxRect.Right;
  174. }
  175. if (TmpDown > maxRect.Bottom)
  176. {
  177. TmpUp = maxRect.Bottom - (cDown - cUp);
  178. TmpDown = maxRect.Bottom;
  179. }
  180. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  181. return offsetPoint;
  182. }
  183. /// <summary>
  184. /// Calculate the movement of the hit point
  185. /// </summary>
  186. /// <param name="mousePoint">
  187. /// Current mouse position
  188. /// </param>
  189. /// <returns>
  190. /// Whether the movement is successful
  191. /// </returns>
  192. protected bool CalcHitPointMove(Point mousePoint)
  193. {
  194. if (isMouseDown == false || hitControlType == PointControlType.None)
  195. {
  196. return false;
  197. }
  198. if (!isOutSideScaling)
  199. {
  200. return NormalScaling(mousePoint);
  201. }
  202. else
  203. {
  204. return OutSideScaling(mousePoint);
  205. }
  206. }
  207. public void SetOutSideScaling(bool IsOutSideScaling)
  208. {
  209. isOutSideScaling = IsOutSideScaling;
  210. }
  211. private Size GetProportionalScalingSize(double width, double height)
  212. {
  213. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  214. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  215. if (minWidth > width || minHeight > height)
  216. {
  217. if (cacheRect.Width >= cacheRect.Height)
  218. {
  219. width = cacheRect.Width * minHeight / cacheRect.Height;
  220. height = minHeight;
  221. }
  222. else
  223. {
  224. height = cacheRect.Height * minWidth / cacheRect.Width;
  225. width = minWidth;
  226. }
  227. }
  228. return new Size(width, height);
  229. }
  230. /// <summary>
  231. /// Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
  232. /// </summary>
  233. /// <param name="mousePoint">Current mouse position.</param>
  234. /// <returns></returns>
  235. protected bool NormalScaling(Point mousePoint)
  236. {
  237. try
  238. {
  239. double left = 0, right = 0, top = 0, bottom = 0;
  240. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  241. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  242. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  243. Point moveVector = (Point)(mousePoint - centerPoint);
  244. moveVector = ProportionalScalingOffsetPos(moveVector);
  245. switch (hitControlType)
  246. {
  247. case PointControlType.LeftTop:
  248. {
  249. left = centerPoint.X + moveVector.X;
  250. right = cacheRect.Right;
  251. top = centerPoint.Y + moveVector.Y;
  252. bottom = cacheRect.Bottom;
  253. if (isProportionalScaling)
  254. {
  255. Size size = GetProportionalScalingSize(right - left, bottom - top);
  256. left = right - size.Width;
  257. top = bottom - size.Height;
  258. if (left < maxRect.Left)
  259. {
  260. double tmpWidth = right - left;
  261. left = maxRect.Left;
  262. double width = right - left;
  263. double height = (bottom - top) * width / tmpWidth;
  264. top = bottom - height;
  265. }
  266. if (top < maxRect.Top)
  267. {
  268. double tmpHeight = bottom - top;
  269. top = maxRect.Top;
  270. double height = bottom - top;
  271. double width = (right - left) * height / tmpHeight;
  272. left = right - width;
  273. }
  274. }
  275. else
  276. {
  277. if (left + minWidth > right)
  278. {
  279. left = right - minWidth;
  280. }
  281. if (top + minHeight > bottom)
  282. {
  283. top = bottom - minHeight;
  284. }
  285. }
  286. }
  287. break;
  288. case PointControlType.LeftMiddle:
  289. {
  290. left = centerPoint.X + moveVector.X;
  291. right = cacheRect.Right;
  292. top = cacheRect.Top;
  293. bottom = cacheRect.Bottom;
  294. if (left + minWidth > right)
  295. {
  296. left = right - minWidth;
  297. }
  298. }
  299. break;
  300. case PointControlType.LeftBottom:
  301. {
  302. left = centerPoint.X + moveVector.X;
  303. right = cacheRect.Right;
  304. top = cacheRect.Top;
  305. bottom = centerPoint.Y + moveVector.Y;
  306. if (isProportionalScaling)
  307. {
  308. Size size = GetProportionalScalingSize(right - left, bottom - top);
  309. left = right - size.Width;
  310. bottom = top + size.Height;
  311. if (left < maxRect.Left)
  312. {
  313. double tmpWidth = right - left;
  314. left = maxRect.Left;
  315. double width = right - left;
  316. double height = (bottom - top) * width / tmpWidth;
  317. bottom = top + height;
  318. }
  319. if (bottom > maxRect.Bottom)
  320. {
  321. double tmpHeight = bottom - top;
  322. bottom = maxRect.Bottom;
  323. double height = bottom - top;
  324. double width = (right - left) * height / tmpHeight;
  325. left = right - width;
  326. }
  327. }
  328. else
  329. {
  330. if (left + minWidth > right)
  331. {
  332. left = right - minWidth;
  333. }
  334. if (top + minHeight > bottom)
  335. {
  336. bottom = top + minHeight;
  337. }
  338. }
  339. }
  340. break;
  341. case PointControlType.MiddlBottom:
  342. {
  343. left = cacheRect.Left;
  344. right = cacheRect.Right;
  345. top = cacheRect.Top;
  346. bottom = centerPoint.Y + moveVector.Y;
  347. if (top + minHeight > bottom)
  348. {
  349. bottom = top + minHeight;
  350. }
  351. }
  352. break;
  353. case PointControlType.RightBottom:
  354. {
  355. left = cacheRect.Left;
  356. right = centerPoint.X + moveVector.X;
  357. top = cacheRect.Top;
  358. bottom = centerPoint.Y + moveVector.Y;
  359. if (isProportionalScaling)
  360. {
  361. Size size = GetProportionalScalingSize(right - left, bottom - top);
  362. right = left + size.Width;
  363. bottom = top + size.Height;
  364. if (right > maxRect.Right)
  365. {
  366. double tmpWidth = right - left;
  367. right = maxRect.Right;
  368. double width = right - left;
  369. double height = (bottom - top) * width / tmpWidth;
  370. bottom = top + height;
  371. }
  372. if (bottom > maxRect.Bottom)
  373. {
  374. double tmpHeight = bottom - top;
  375. bottom = maxRect.Bottom;
  376. double height = bottom - top;
  377. double width = (right - left) * height / tmpHeight;
  378. right = left + width;
  379. }
  380. }
  381. else
  382. {
  383. if (left + minWidth > right)
  384. {
  385. right = left + minWidth;
  386. }
  387. if (top + minHeight > bottom)
  388. {
  389. bottom = top + minHeight;
  390. }
  391. }
  392. }
  393. break;
  394. case PointControlType.RightMiddle:
  395. {
  396. left = cacheRect.Left;
  397. right = centerPoint.X + moveVector.X;
  398. top = cacheRect.Top;
  399. bottom = cacheRect.Bottom;
  400. if (left + minWidth > right)
  401. {
  402. right = left + minWidth;
  403. }
  404. }
  405. break;
  406. case PointControlType.RightTop:
  407. {
  408. left = cacheRect.Left;
  409. right = centerPoint.X + moveVector.X;
  410. top = centerPoint.Y + moveVector.Y;
  411. bottom = cacheRect.Bottom;
  412. if (isProportionalScaling)
  413. {
  414. Size size = GetProportionalScalingSize(right - left, bottom - top);
  415. right = left + size.Width;
  416. top = bottom - size.Height;
  417. if (right > maxRect.Right)
  418. {
  419. double tmpWidth = right - left;
  420. right = maxRect.Right;
  421. double width = right - left;
  422. double height = (bottom - top) * width / tmpWidth;
  423. top = bottom - height;
  424. }
  425. if (top < maxRect.Top)
  426. {
  427. double tmpHeight = bottom - top;
  428. top = maxRect.Top;
  429. double height = bottom - top;
  430. double width = (right - left) * height / tmpHeight;
  431. right = left + width;
  432. }
  433. }
  434. else
  435. {
  436. if (left + minWidth > right)
  437. {
  438. right = left + minWidth;
  439. }
  440. if (top + minHeight > bottom)
  441. {
  442. top = bottom - minHeight;
  443. }
  444. }
  445. }
  446. break;
  447. case PointControlType.MiddleTop:
  448. {
  449. left = cacheRect.Left;
  450. right = cacheRect.Right;
  451. top = centerPoint.Y + moveVector.Y;
  452. bottom = cacheRect.Bottom;
  453. if (top + minHeight > bottom)
  454. {
  455. top = bottom - minHeight;
  456. }
  457. }
  458. break;
  459. case PointControlType.Body:
  460. case PointControlType.Line:
  461. {
  462. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  463. left = cacheRect.Left + OffsetPos.X;
  464. right = cacheRect.Right + OffsetPos.X;
  465. top = cacheRect.Top + OffsetPos.Y;
  466. bottom = cacheRect.Bottom + OffsetPos.Y;
  467. }
  468. break;
  469. default:
  470. break;
  471. }
  472. if (left < maxRect.Left)
  473. {
  474. left = maxRect.Left;
  475. }
  476. if (top < maxRect.Top)
  477. {
  478. top = maxRect.Top;
  479. }
  480. if (right > maxRect.Right)
  481. {
  482. right = maxRect.Right;
  483. }
  484. if (bottom > maxRect.Bottom)
  485. {
  486. bottom = maxRect.Bottom;
  487. }
  488. drawRect = new Rect(left, top, right - left, bottom - top);
  489. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  490. return true;
  491. }
  492. catch (Exception ex)
  493. {
  494. }
  495. return false;
  496. }
  497. /// <summary>
  498. /// Provisional logic, to be further improved, not yet used: Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
  499. /// </summary>
  500. /// <param name="mousePoint">Current mouse position.</param>
  501. /// <returns></returns>
  502. protected bool OutSideScaling(Point mousePoint)
  503. {
  504. try
  505. {
  506. double left = 0, right = 0, top = 0, bottom = 0;
  507. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  508. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  509. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  510. Point moveVector = (Point)(mousePoint - centerPoint);
  511. moveVector = ProportionalScalingOffsetPos(moveVector);
  512. switch (hitControlType)
  513. {
  514. case PointControlType.LeftTop:
  515. {
  516. left = centerPoint.X + moveVector.X;
  517. right = cacheRect.Right;
  518. top = centerPoint.Y + moveVector.Y;
  519. bottom = cacheRect.Bottom;
  520. if (isProportionalScaling)
  521. {
  522. Size size = GetProportionalScalingSize(right - left, bottom - top);
  523. left = right - size.Width;
  524. top = bottom - size.Height;
  525. if (left < maxRect.Left)
  526. {
  527. double tmpWidth = right - left;
  528. left = maxRect.Left;
  529. double width = right - left;
  530. double height = (bottom - top) * width / tmpWidth;
  531. top = bottom - height;
  532. }
  533. if (top < maxRect.Top)
  534. {
  535. double tmpHeight = bottom - top;
  536. top = maxRect.Top;
  537. double height = bottom - top;
  538. double width = (right - left) * height / tmpHeight;
  539. left = right - width;
  540. }
  541. }
  542. else
  543. {
  544. if (left + minWidth > right)
  545. {
  546. left = right - minWidth;
  547. }
  548. if (top + minHeight > bottom)
  549. {
  550. top = bottom - minHeight;
  551. }
  552. }
  553. }
  554. break;
  555. case PointControlType.LeftMiddle:
  556. {
  557. left = centerPoint.X + moveVector.X;
  558. right = cacheRect.Right;
  559. top = cacheRect.Top;
  560. bottom = cacheRect.Bottom;
  561. if (left + minWidth > right)
  562. {
  563. left = right - minWidth;
  564. }
  565. }
  566. break;
  567. case PointControlType.LeftBottom:
  568. {
  569. left = centerPoint.X + moveVector.X;
  570. right = cacheRect.Right;
  571. top = cacheRect.Top;
  572. bottom = centerPoint.Y + moveVector.Y;
  573. if (isProportionalScaling)
  574. {
  575. Size size = GetProportionalScalingSize(right - left, bottom - top);
  576. left = right - size.Width;
  577. bottom = top + size.Height;
  578. if (left < maxRect.Left)
  579. {
  580. double tmpWidth = right - left;
  581. left = maxRect.Left;
  582. double width = right - left;
  583. double height = (bottom - top) * width / tmpWidth;
  584. bottom = top + height;
  585. }
  586. if (bottom > maxRect.Bottom)
  587. {
  588. double tmpHeight = bottom - top;
  589. bottom = maxRect.Bottom;
  590. double height = bottom - top;
  591. double width = (right - left) * height / tmpHeight;
  592. left = right - width;
  593. }
  594. }
  595. else
  596. {
  597. if (left + minWidth > right)
  598. {
  599. left = right - minWidth;
  600. }
  601. if (top + minHeight > bottom)
  602. {
  603. bottom = top + minHeight;
  604. }
  605. }
  606. }
  607. break;
  608. case PointControlType.MiddlBottom:
  609. {
  610. left = cacheRect.Left;
  611. right = cacheRect.Right;
  612. top = cacheRect.Top;
  613. bottom = centerPoint.Y + moveVector.Y;
  614. if (top + minHeight > bottom)
  615. {
  616. bottom = top + minHeight;
  617. }
  618. }
  619. break;
  620. case PointControlType.RightBottom:
  621. {
  622. left = cacheRect.Left;
  623. right = centerPoint.X + moveVector.X;
  624. top = cacheRect.Top;
  625. bottom = centerPoint.Y + moveVector.Y;
  626. if (isProportionalScaling)
  627. {
  628. Size size = GetProportionalScalingSize(right - left, bottom - top);
  629. right = left + size.Width;
  630. bottom = top + size.Height;
  631. if (right > maxRect.Right)
  632. {
  633. double tmpWidth = right - left;
  634. right = maxRect.Right;
  635. double width = right - left;
  636. double height = (bottom - top) * width / tmpWidth;
  637. bottom = top + height;
  638. }
  639. if (bottom > maxRect.Bottom)
  640. {
  641. double tmpHeight = bottom - top;
  642. bottom = maxRect.Bottom;
  643. double height = bottom - top;
  644. double width = (right - left) * height / tmpHeight;
  645. right = left + width;
  646. }
  647. }
  648. else
  649. {
  650. if (left + minWidth > right)
  651. {
  652. right = left + minWidth;
  653. }
  654. if (top + minHeight > bottom)
  655. {
  656. bottom = top + minHeight;
  657. }
  658. }
  659. }
  660. break;
  661. case PointControlType.RightMiddle:
  662. {
  663. left = cacheRect.Left;
  664. right = centerPoint.X + moveVector.X;
  665. top = cacheRect.Top;
  666. bottom = cacheRect.Bottom;
  667. if (left + minWidth > right)
  668. {
  669. right = left + minWidth;
  670. }
  671. }
  672. break;
  673. case PointControlType.RightTop:
  674. {
  675. left = cacheRect.Left;
  676. right = centerPoint.X + moveVector.X;
  677. top = centerPoint.Y + moveVector.Y;
  678. bottom = cacheRect.Bottom;
  679. if (isProportionalScaling)
  680. {
  681. Size size = GetProportionalScalingSize(right - left, bottom - top);
  682. right = left + size.Width;
  683. top = bottom - size.Height;
  684. if (right > maxRect.Right)
  685. {
  686. double tmpWidth = right - left;
  687. right = maxRect.Right;
  688. double width = right - left;
  689. double height = (bottom - top) * width / tmpWidth;
  690. top = bottom - height;
  691. }
  692. if (top < maxRect.Top)
  693. {
  694. double tmpHeight = bottom - top;
  695. top = maxRect.Top;
  696. double height = bottom - top;
  697. double width = (right - left) * height / tmpHeight;
  698. right = left + width;
  699. }
  700. }
  701. else
  702. {
  703. if (left + minWidth > right)
  704. {
  705. right = left + minWidth;
  706. }
  707. if (top + minHeight > bottom)
  708. {
  709. top = bottom - minHeight;
  710. }
  711. }
  712. }
  713. break;
  714. case PointControlType.MiddleTop:
  715. {
  716. left = cacheRect.Left;
  717. right = cacheRect.Right;
  718. top = centerPoint.Y + moveVector.Y;
  719. bottom = cacheRect.Bottom;
  720. if (top + minHeight > bottom)
  721. {
  722. top = bottom - minHeight;
  723. }
  724. }
  725. break;
  726. case PointControlType.Body:
  727. case PointControlType.Line:
  728. {
  729. double newleft = maxRect.Left - SetDrawRect.Width + 10;
  730. double newright = maxRect.Right + SetDrawRect.Width - 10;
  731. double newtop = maxRect.Top - SetDrawRect.Height + 10;
  732. double newbottom = maxRect.Bottom + SetDrawRect.Height - 10;
  733. if (newleft < 0)
  734. {
  735. newleft = 0;
  736. }
  737. Rect newMaxRect = new Rect(newleft, newtop, newright - newleft, newbottom - newtop);
  738. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), newMaxRect);
  739. left = cacheRect.Left + OffsetPos.X;
  740. right = cacheRect.Right + OffsetPos.X;
  741. top = cacheRect.Top + OffsetPos.Y;
  742. bottom = cacheRect.Bottom + OffsetPos.Y;
  743. }
  744. break;
  745. default:
  746. break;
  747. }
  748. //if (left < maxRect.Left)
  749. //{
  750. // left = maxRect.Left;
  751. //}
  752. //if (top < maxRect.Top)
  753. //{
  754. // top = maxRect.Top;
  755. //}
  756. if (right > maxRect.Right + SetDrawRect.Width - 10)
  757. {
  758. if (left > maxRect.Right - 10)
  759. {
  760. left = maxRect.Right - 10;
  761. }
  762. right = maxRect.Right + SetDrawRect.Width - 10;
  763. }
  764. if (bottom > maxRect.Bottom + SetDrawRect.Height - 10)
  765. {
  766. if (top > maxRect.Bottom - 10)
  767. {
  768. top = maxRect.Bottom - 10;
  769. }
  770. bottom = maxRect.Bottom + SetDrawRect.Height - 10;
  771. }
  772. drawRect = new Rect(left, top, right - left, bottom - top);
  773. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  774. return true;
  775. }
  776. catch (Exception ex)
  777. {
  778. }
  779. return false;
  780. }
  781. /// <summary>
  782. /// Proportional scaling offset calibration
  783. /// </summary>
  784. /// <param name="movePoint">
  785. /// The current movement point
  786. /// </param>
  787. /// <returns>
  788. /// The offset point after the proportional scaling
  789. /// </returns>
  790. protected Point ProportionalScalingOffsetPos(Point movePoint)
  791. {
  792. if (isProportionalScaling)
  793. {
  794. Point offsetPos = movePoint;
  795. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  796. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  797. switch (hitControlType)
  798. {
  799. case PointControlType.LeftTop:
  800. case PointControlType.RightBottom:
  801. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  802. break;
  803. case PointControlType.LeftBottom:
  804. case PointControlType.RightTop:
  805. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  806. break;
  807. case PointControlType.LeftMiddle:
  808. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  809. break;
  810. case PointControlType.RightMiddle:
  811. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  812. break;
  813. case PointControlType.MiddlBottom:
  814. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  815. break;
  816. case PointControlType.MiddleTop:
  817. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  818. break;
  819. default:
  820. break;
  821. }
  822. return offsetPos;
  823. }
  824. else
  825. {
  826. return movePoint;
  827. }
  828. }
  829. /// <summary>
  830. /// Inner drawing circle point
  831. /// </summary>
  832. /// <param name="drawingContext">
  833. /// Drawing context
  834. /// </param>
  835. /// <param name="ignoreList">
  836. /// Collection of positions that need to be drawn
  837. /// </param>
  838. /// <param name="PointSize">
  839. /// Size of the point
  840. /// </param>
  841. /// <param name="PointPen">
  842. /// Brush for drawing points
  843. /// </param>
  844. /// <param name="BorderBrush">
  845. /// Border brush for drawing points
  846. /// </param>
  847. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  848. {
  849. GeometryGroup controlGroup = new GeometryGroup();
  850. controlGroup.FillRule = FillRule.Nonzero;
  851. List<Point> ignorePointsList = new List<Point>();
  852. // Get specific points
  853. foreach (PointControlType type in ignoreList)
  854. {
  855. if ((int)type < controlPoints.Count)
  856. {
  857. ignorePointsList.Add(controlPoints[(int)type]);
  858. }
  859. }
  860. for (int i = 0; i < controlPoints.Count; i++)
  861. {
  862. Point controlPoint = controlPoints[i];
  863. if (ignorePointsList.Contains(controlPoint))
  864. {
  865. continue;
  866. }
  867. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  868. controlGroup.Children.Add(circlPoint);
  869. }
  870. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  871. }
  872. /// <summary>
  873. /// Inner drawing square
  874. /// </summary>
  875. /// <param name="drawingContext">
  876. /// Drawing context
  877. /// </param>
  878. /// <param name="ControlPoints">
  879. /// Collection of positions that need to be drawn
  880. /// </param>
  881. /// <param name="PointSize">
  882. /// Size of the point
  883. /// </param>
  884. /// <param name="PointPen">
  885. /// Brush for drawing points
  886. /// </param>
  887. /// <param name="BorderBrush">
  888. /// Border brush for drawing points
  889. /// </param>
  890. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  891. {
  892. GeometryGroup controlGroup = new GeometryGroup();
  893. controlGroup.FillRule = FillRule.Nonzero;
  894. List<Point> ignorePointsList = new List<Point>();
  895. // Get specific points
  896. foreach (PointControlType type in ignoreList)
  897. {
  898. if ((int)type < controlPoints.Count)
  899. {
  900. ignorePointsList.Add(controlPoints[(int)type]);
  901. }
  902. }
  903. for (int i = 0; i < controlPoints.Count; i++)
  904. {
  905. Point controlPoint = controlPoints[i];
  906. if (ignorePointsList.Contains(controlPoint))
  907. {
  908. continue;
  909. }
  910. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  911. PointSize * 2, PointSize * 2), 1, 1);
  912. controlGroup.Children.Add(rectPoint);
  913. }
  914. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  915. }
  916. protected void DrawCropPoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  917. {
  918. //GeometryGroup controlGroup = new GeometryGroup();
  919. //controlGroup.FillRule = FillRule.Nonzero;
  920. clipThickness.Left = (SetDrawRect.Left - drawRect.Left)/currentZoom;
  921. clipThickness.Top = (SetDrawRect.Top - drawRect.Top) / currentZoom;
  922. clipThickness.Right = (SetDrawRect.Right - drawRect.Right) / currentZoom;
  923. clipThickness.Bottom = (SetDrawRect.Bottom - drawRect.Bottom) / currentZoom;
  924. List<Point> controlCurrentPoints = GetControlPoint(drawRect);
  925. CombinedGeometry controlGroup = new CombinedGeometry();
  926. RectangleGeometry paintGeometry = new RectangleGeometry();
  927. paintGeometry.Rect = SetDrawRect;
  928. controlGroup.Geometry1 = paintGeometry;
  929. RectangleGeometry moveGeometry = new RectangleGeometry();
  930. Rect clippedBorder = drawRect;
  931. if (clippedBorder.IsEmpty == false)
  932. {
  933. moveGeometry.Rect = drawRect;
  934. }
  935. controlGroup.Geometry2 = moveGeometry;
  936. controlGroup.GeometryCombineMode = GeometryCombineMode.Exclude;
  937. //Left Top Corner
  938. if (!ignoreList.Contains(PointControlType.LeftTop))
  939. {
  940. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize, PointSize * 4));
  941. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize * 4, PointSize));
  942. }
  943. //Left Center
  944. if (!ignoreList.Contains(PointControlType.LeftMiddle))
  945. {
  946. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[1].X - PointSize, (controlCurrentPoints[1].Y + controlCurrentPoints[1].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  947. }
  948. //Left Bottom Corner
  949. if (!ignoreList.Contains(PointControlType.LeftBottom))
  950. {
  951. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y - PointSize * 3, PointSize, PointSize * 4));
  952. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y, PointSize * 4, PointSize));
  953. }
  954. //Bottom Center
  955. if (!ignoreList.Contains(PointControlType.MiddlBottom))
  956. {
  957. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[3].X + controlCurrentPoints[3].X - PointSize * 5) / 2, controlCurrentPoints[3].Y, PointSize * 5, PointSize));
  958. }
  959. //Bottom Right Corner
  960. if (!ignoreList.Contains(PointControlType.RightBottom))
  961. {
  962. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X, controlCurrentPoints[4].Y - PointSize * 3, PointSize, PointSize * 4));
  963. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X - PointSize * 3, controlCurrentPoints[4].Y, PointSize * 4, PointSize));
  964. }
  965. //Right Center
  966. if (!ignoreList.Contains(PointControlType.RightMiddle))
  967. {
  968. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[5].X, (controlCurrentPoints[5].Y + controlCurrentPoints[5].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  969. }
  970. //Right Top Corner
  971. if (!ignoreList.Contains(PointControlType.RightTop))
  972. {
  973. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X, controlCurrentPoints[6].Y - PointSize, PointSize, PointSize * 4));
  974. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X - PointSize * 4, controlCurrentPoints[6].Y - PointSize, PointSize * 4, PointSize));
  975. }
  976. //Top Center
  977. if (!ignoreList.Contains(PointControlType.MiddleTop))
  978. {
  979. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[7].X + controlCurrentPoints[7].X - PointSize * 5) / 2, controlCurrentPoints[7].Y - PointSize, PointSize * 5, PointSize));
  980. }
  981. BorderBrush = new SolidColorBrush(Color.FromArgb(0x3F, 0x00, 0x00, 0x00));
  982. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  983. }
  984. /// <summary>
  985. /// Draw the reference line in the moving state
  986. /// </summary>
  987. /// <param name="drawDc">
  988. /// Draw context handle
  989. /// </param>
  990. /// <param name="controltype">
  991. /// Current selected control point type
  992. /// </param>
  993. /// <param name="activePen">
  994. /// Brush for drawing lines
  995. /// </param>
  996. /// <param name="moveBrush">
  997. /// Brush for drawing rectangles
  998. /// </param>
  999. /// <param name="moveRect">
  1000. /// Current rectangle to draw
  1001. /// </param>
  1002. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect, Pen RectPen = null)
  1003. {
  1004. switch (controltype)
  1005. {
  1006. case PointControlType.LeftTop:
  1007. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1008. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1009. break;
  1010. case PointControlType.LeftMiddle:
  1011. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1012. break;
  1013. case PointControlType.LeftBottom:
  1014. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1015. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  1016. break;
  1017. case PointControlType.MiddlBottom:
  1018. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1019. break;
  1020. case PointControlType.RightBottom:
  1021. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1022. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1023. break;
  1024. case PointControlType.RightMiddle:
  1025. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1026. break;
  1027. case PointControlType.RightTop:
  1028. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1029. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  1030. break;
  1031. case PointControlType.MiddleTop:
  1032. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1033. break;
  1034. case PointControlType.Rotate:
  1035. break;
  1036. case PointControlType.Body:
  1037. case PointControlType.Line:
  1038. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(moveRect.Left, moveRect.Top));
  1039. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  1040. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Top), new Point(moveRect.Left, 0));
  1041. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Top), new Point(moveRect.Right, 0));
  1042. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(moveRect.Left, moveRect.Bottom));
  1043. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  1044. drawDc?.DrawLine(activePen, new Point(moveRect.Left, moveRect.Bottom), new Point(moveRect.Left, PDFViewerActualHeight));
  1045. drawDc?.DrawLine(activePen, new Point(moveRect.Right, moveRect.Bottom), new Point(moveRect.Right, PDFViewerActualHeight));
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. drawDc?.DrawRectangle(moveBrush, RectPen, moveRect);
  1051. }
  1052. /// <summary>
  1053. /// Notify the event during/after the drawing data
  1054. /// </summary>
  1055. /// <param name="isFinish">
  1056. /// Identifies whether the data change is complete
  1057. /// </param>
  1058. protected void InvokeDataChangEvent(bool isFinish)
  1059. {
  1060. selectedRectData.Square = GetRect();
  1061. if (isFinish)
  1062. {
  1063. DataChanged?.Invoke(this, selectedRectData);
  1064. }
  1065. else
  1066. {
  1067. DataChanging?.Invoke(this, selectedRectData);
  1068. }
  1069. }
  1070. /// <summary>
  1071. /// Align the rectangle drawing
  1072. /// </summary>
  1073. /// <param name="RectMovePoint">
  1074. /// Move distance required for the aligned algorithm to obtain the rectangle
  1075. /// </param>
  1076. private void DrawAlignRect(Point RectMovePoint)
  1077. {
  1078. double TmpLeft, TmpRight, TmpUp, TmpDown;
  1079. Point OffsetPos = CalcMoveBound(drawRect, RectMovePoint, maxRect);
  1080. TmpLeft = drawRect.Left + OffsetPos.X;
  1081. TmpRight = drawRect.Right + OffsetPos.X;
  1082. TmpUp = drawRect.Top + OffsetPos.Y;
  1083. TmpDown = drawRect.Bottom + OffsetPos.Y;
  1084. SetDrawRect = drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  1085. Draw();
  1086. }
  1087. /// <summary>
  1088. /// Get the current set of ignore points
  1089. /// </summary>
  1090. /// <returns>
  1091. /// Data set of ignored points
  1092. /// </returns>
  1093. private List<PointControlType> GetIgnorePoints()
  1094. {
  1095. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  1096. foreach (PointControlType type in ignorePoints)
  1097. {
  1098. IgnorePointsList.Add(type);
  1099. }
  1100. return IgnorePointsList;
  1101. }
  1102. #endregion
  1103. }
  1104. }