SelectedRect.protected.cs 49 KB

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