FrameSelectTool.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using ComPDFKit.Import;
  2. using ComPDFKit.Measure;
  3. using ComPDFKit.PDFAnnotation;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFPage;
  6. using ComPDFKit.Tool.Help;
  7. using ComPDFKit.Tool.SettingParam;
  8. using ComPDFKit.Tool.UndoManger;
  9. using ComPDFKit.Viewer.Helper;
  10. using ComPDFKit.Viewer.Layer;
  11. using ComPDFKitViewer;
  12. using ComPDFKitViewer.Annot;
  13. using ComPDFKitViewer.BaseObject;
  14. using ComPDFKitViewer.Helper;
  15. using ComPDFKitViewer.Layer;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Globalization;
  20. using System.Linq;
  21. using System.Net;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. using System.Windows;
  25. using System.Windows.Annotations;
  26. using System.Windows.Controls;
  27. using System.Windows.Input;
  28. using System.Windows.Media;
  29. using System.Windows.Media.Animation;
  30. using System.Windows.Media.Media3D;
  31. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  32. using static ComPDFKit.Tool.Help.ImportWin32;
  33. namespace ComPDFKit.Tool.DrawTool
  34. {
  35. internal class FrameSelectTool : CustomizeLayer
  36. {
  37. #region Attributes
  38. /// <summary>
  39. /// Indicates whether proportional scaling is required
  40. /// </summary>
  41. protected bool isProportionalScaling { get; set; } = false;
  42. /// <summary>
  43. /// Mouse start point
  44. /// </summary>
  45. protected Point mouseStartPoint { get; set; }
  46. /// <summary>
  47. /// Mouse end point
  48. /// </summary>
  49. protected Point mouseEndPoint { get; set; }
  50. /// <summary>
  51. /// Crop point
  52. /// </summary>
  53. protected Point cropPoint { get; set; }
  54. /// <summary>
  55. ///Is drawing annotation
  56. /// </summary>
  57. protected bool isFrameSelect { get; set; }
  58. /// <summary>
  59. /// Current zoom factor
  60. /// </summary>
  61. private double zoomFactor { get; set; } = 1;
  62. /// <summary>
  63. /// Draw rectangle
  64. /// </summary>
  65. protected Rect drawRect { get; set; } = new Rect();
  66. /// <summary>
  67. /// The rectangle representing the maximum drawing area
  68. /// </summary>
  69. protected Rect maxRect { get; set; }
  70. /// <summary>
  71. /// The rectangle representing the original page range (calculated offset in continuous mode)
  72. /// </summary>
  73. protected Rect pageBound { get; set; }
  74. /// <summary>
  75. /// The rectangle at standard DPI (without subtracting half of the pen thickness)
  76. /// </summary>
  77. protected Rect DPIRect { get; set; }
  78. /// <summary>
  79. /// The offset value during movement
  80. /// </summary>
  81. protected Point moveOffset { get; set; } = new Point(0, 0);
  82. /// <summary>
  83. /// The offset value during movement
  84. /// </summary>
  85. protected int pageIndex { get; set; } = -1;
  86. protected DrawingContext drawDC { get; set; }
  87. /// <summary>
  88. /// The collection of points measured for annotation drawing
  89. /// </summary>
  90. protected PointCollection drawPoints { get; set; } = new PointCollection();
  91. protected double textPadding { get; set; } = 10;
  92. protected Border lastTextBorder;
  93. #endregion
  94. public FrameSelectTool()
  95. {
  96. }
  97. public Point GetStartPoint()
  98. {
  99. return DpiHelper.StandardPointToPDFPoint(new Point((mouseStartPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseStartPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
  100. }
  101. public Point GetEndPoint()
  102. {
  103. if (moveOffset == new Point())
  104. {
  105. return new Point(-1, -1);
  106. }
  107. else
  108. {
  109. return DpiHelper.StandardPointToPDFPoint(new Point((mouseEndPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseEndPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
  110. }
  111. }
  112. public double GetMoveLength()
  113. {
  114. if (mouseEndPoint == new Point())
  115. {
  116. return 0;
  117. }
  118. Point checkPoint = mouseEndPoint;
  119. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  120. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  121. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  122. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  123. Vector moveOffset = checkPoint - mouseStartPoint;
  124. return moveOffset.Length;
  125. }
  126. public void SetIsProportionalScaling(bool isOpen)
  127. {
  128. isProportionalScaling = isOpen;
  129. }
  130. #region Draw
  131. public void StartDraw(Point downPoint, Rect maxRect, Rect pageBound, double zoom, int pageindex)
  132. {
  133. this.pageBound = pageBound;
  134. pageIndex = pageindex;
  135. isFrameSelect = true;
  136. drawRect = new Rect();
  137. DPIRect = new Rect();
  138. mouseStartPoint = downPoint;
  139. isFrameSelect = true;
  140. this.maxRect = maxRect;
  141. zoomFactor = zoom;
  142. moveOffset = new Point();
  143. DPIRect = new Rect();
  144. }
  145. bool noDraw=false;
  146. public void MoveDraw(Point downPoint, double zoom)
  147. {
  148. if (isFrameSelect)
  149. {
  150. noDraw = true;
  151. moveOffset = new Point(
  152. mouseEndPoint.X - downPoint.X,
  153. mouseEndPoint.Y - downPoint.Y
  154. );
  155. mouseEndPoint = downPoint;
  156. zoomFactor = zoom;
  157. Draw();
  158. }
  159. noDraw = false;
  160. }
  161. public Rect EndDraw(ref int index)
  162. {
  163. if (noDraw && isFrameSelect)
  164. {
  165. new Rect();
  166. }
  167. if (!DPIRect.Equals(new Rect()))
  168. {
  169. Rect StandardRect = new Rect(
  170. (DPIRect.Left - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (DPIRect.Top - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor,
  171. DPIRect.Width / zoomFactor, DPIRect.Height / zoomFactor);
  172. isFrameSelect = false;
  173. noDraw = false;
  174. mouseStartPoint = new Point();
  175. mouseEndPoint = new Point();
  176. moveOffset = new Point();
  177. pageBound = new Rect();
  178. DPIRect = new Rect();
  179. drawPoints.Clear();
  180. ClearDraw();
  181. index = pageIndex;
  182. return DpiHelper.StandardRectToPDFRect(StandardRect);
  183. }
  184. return new Rect();
  185. }
  186. public override void Draw()
  187. {
  188. if (noDraw&& isFrameSelect)
  189. {
  190. Dispatcher.Invoke(() =>
  191. {
  192. drawDC = Open();
  193. DrawSquare(drawDC);
  194. Present();
  195. });
  196. }
  197. }
  198. public virtual void ClearDraw()
  199. {
  200. Open();
  201. Present();
  202. }
  203. private void DrawSquare(DrawingContext drawingContext)
  204. {
  205. try
  206. {
  207. Pen DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2);
  208. SolidColorBrush FillBrush = new SolidColorBrush(Colors.Transparent);
  209. FillBrush = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
  210. if (isProportionalScaling)
  211. {
  212. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  213. if (mouseOffset.X < 0)
  214. {
  215. if (mouseOffset.Y > 0)
  216. {
  217. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  218. }
  219. else
  220. {
  221. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  222. }
  223. }
  224. else
  225. {
  226. if (mouseOffset.Y > 0)
  227. {
  228. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  229. }
  230. else
  231. {
  232. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  233. }
  234. }
  235. }
  236. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  237. double mLeft = rect.Left;
  238. double mRight = rect.Right;
  239. double mUp = rect.Top;
  240. double mDown = rect.Bottom;
  241. if (rect.Left < maxRect.Left)
  242. {
  243. mLeft = maxRect.Left;
  244. }
  245. if (rect.Right > maxRect.Right)
  246. {
  247. mRight = maxRect.Right;
  248. }
  249. if (rect.Top < maxRect.Top)
  250. {
  251. mUp = maxRect.Top;
  252. }
  253. if (rect.Bottom > maxRect.Bottom)
  254. {
  255. mDown = maxRect.Bottom;
  256. }
  257. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  258. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  259. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  260. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  261. if (drawWidth > 0 && drawHeight > 0)
  262. {
  263. drawRect = new Rect(
  264. (int)DPIRect.Left + halfPenWidth,
  265. (int)DPIRect.Top + halfPenWidth,
  266. (int)DPIRect.Width - halfPenWidth * 2,
  267. (int)DPIRect.Height - halfPenWidth * 2);
  268. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  269. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  270. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  271. {
  272. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  273. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  274. }
  275. }
  276. else
  277. {
  278. drawRect = new Rect();
  279. }
  280. }
  281. catch { }
  282. }
  283. public Rect GetMaxRect()
  284. {
  285. return maxRect;
  286. }
  287. /// <summary>
  288. /// Use to calculate the point drawn at a fixed angle
  289. /// </summary>
  290. /// <param name="currentPoint">
  291. /// Current point
  292. /// </param>
  293. /// <param name="startPoint">
  294. /// Start point
  295. /// </param>
  296. /// <param name="pageBound">
  297. /// Maximum drawing area
  298. /// </param>
  299. /// <returns>
  300. /// Return the calculated point
  301. /// </returns>
  302. internal Point CalcAnglePoint(Point currentPoint, Point startPoint, Rect pageBound)
  303. {
  304. Vector angleVector = currentPoint - startPoint;
  305. Point originPoint = new Point(startPoint.X, startPoint.Y - angleVector.Length);
  306. Vector orignVector = originPoint - startPoint;
  307. Rect checkRect = pageBound;
  308. int angle = (int)Vector.AngleBetween(orignVector, angleVector);
  309. if (angle < 0)
  310. {
  311. angle += 360;
  312. }
  313. int mod = angle % 45;
  314. int quot = angle / 45;
  315. Point anglePoint = currentPoint;
  316. int rotateAngle = 0;
  317. if (mod < 22)
  318. {
  319. Matrix rotateMatrix = new Matrix();
  320. rotateAngle = quot * 45;
  321. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  322. anglePoint = rotateMatrix.Transform(originPoint);
  323. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  324. }
  325. else
  326. {
  327. Matrix rotateMatrix = new Matrix();
  328. rotateAngle = (quot + 1) * 45;
  329. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  330. anglePoint = rotateMatrix.Transform(originPoint);
  331. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  332. }
  333. if (checkRect.Contains(anglePoint) == false)
  334. {
  335. switch (rotateAngle)
  336. {
  337. case 0:
  338. {
  339. anglePoint.X = startPoint.X;
  340. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  341. }
  342. break;
  343. case 45:
  344. {
  345. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  346. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  347. anglePoint.X = startPoint.X + addValue;
  348. anglePoint.Y = startPoint.Y - addValue;
  349. }
  350. break;
  351. case 90:
  352. {
  353. anglePoint.X = startPoint.X + Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  354. anglePoint.Y = startPoint.Y;
  355. }
  356. break;
  357. case 135:
  358. {
  359. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  360. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  361. anglePoint.X = startPoint.X + addValue;
  362. anglePoint.Y = startPoint.Y + addValue;
  363. }
  364. break;
  365. case 180:
  366. {
  367. anglePoint.X = startPoint.X;
  368. anglePoint.Y = Math.Min(anglePoint.Y, checkRect.Bottom);
  369. }
  370. break;
  371. case 225:
  372. {
  373. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  374. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  375. anglePoint.X = startPoint.X - addValue;
  376. anglePoint.Y = startPoint.Y + addValue;
  377. }
  378. break;
  379. case 270:
  380. {
  381. anglePoint.X = startPoint.X - Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  382. anglePoint.Y = startPoint.Y;
  383. }
  384. break;
  385. case 315:
  386. {
  387. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  388. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  389. anglePoint.X = startPoint.X - addValue;
  390. anglePoint.Y = startPoint.Y - addValue;
  391. }
  392. break;
  393. case 360:
  394. {
  395. anglePoint.X = startPoint.X;
  396. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  397. }
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. return anglePoint;
  404. }
  405. }
  406. #endregion
  407. }