FrameSelectTool.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. isFrameSelect = false;
  185. noDraw = false;
  186. return new Rect();
  187. }
  188. public override void Draw()
  189. {
  190. if (noDraw&& isFrameSelect)
  191. {
  192. Dispatcher.Invoke(() =>
  193. {
  194. drawDC = Open();
  195. DrawSquare(drawDC);
  196. Present();
  197. });
  198. }
  199. }
  200. public virtual void ClearDraw()
  201. {
  202. Open();
  203. Present();
  204. }
  205. private void DrawSquare(DrawingContext drawingContext)
  206. {
  207. try
  208. {
  209. Pen DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2);
  210. SolidColorBrush FillBrush = new SolidColorBrush(Colors.Transparent);
  211. FillBrush = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255));
  212. if (isProportionalScaling)
  213. {
  214. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  215. if (mouseOffset.X < 0)
  216. {
  217. if (mouseOffset.Y > 0)
  218. {
  219. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  220. }
  221. else
  222. {
  223. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  224. }
  225. }
  226. else
  227. {
  228. if (mouseOffset.Y > 0)
  229. {
  230. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  231. }
  232. else
  233. {
  234. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  235. }
  236. }
  237. }
  238. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  239. double mLeft = rect.Left;
  240. double mRight = rect.Right;
  241. double mUp = rect.Top;
  242. double mDown = rect.Bottom;
  243. if (rect.Left < maxRect.Left)
  244. {
  245. mLeft = maxRect.Left;
  246. }
  247. if (rect.Right > maxRect.Right)
  248. {
  249. mRight = maxRect.Right;
  250. }
  251. if (rect.Top < maxRect.Top)
  252. {
  253. mUp = maxRect.Top;
  254. }
  255. if (rect.Bottom > maxRect.Bottom)
  256. {
  257. mDown = maxRect.Bottom;
  258. }
  259. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  260. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  261. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  262. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  263. if (drawWidth > 0 && drawHeight > 0)
  264. {
  265. drawRect = new Rect(
  266. (int)DPIRect.Left + halfPenWidth,
  267. (int)DPIRect.Top + halfPenWidth,
  268. (int)DPIRect.Width - halfPenWidth * 2,
  269. (int)DPIRect.Height - halfPenWidth * 2);
  270. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  271. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  272. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  273. {
  274. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  275. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  276. }
  277. }
  278. else
  279. {
  280. drawRect = new Rect();
  281. }
  282. }
  283. catch { }
  284. }
  285. public Rect GetMaxRect()
  286. {
  287. return maxRect;
  288. }
  289. /// <summary>
  290. /// Use to calculate the point drawn at a fixed angle
  291. /// </summary>
  292. /// <param name="currentPoint">
  293. /// Current point
  294. /// </param>
  295. /// <param name="startPoint">
  296. /// Start point
  297. /// </param>
  298. /// <param name="pageBound">
  299. /// Maximum drawing area
  300. /// </param>
  301. /// <returns>
  302. /// Return the calculated point
  303. /// </returns>
  304. internal Point CalcAnglePoint(Point currentPoint, Point startPoint, Rect pageBound)
  305. {
  306. Vector angleVector = currentPoint - startPoint;
  307. Point originPoint = new Point(startPoint.X, startPoint.Y - angleVector.Length);
  308. Vector orignVector = originPoint - startPoint;
  309. Rect checkRect = pageBound;
  310. int angle = (int)Vector.AngleBetween(orignVector, angleVector);
  311. if (angle < 0)
  312. {
  313. angle += 360;
  314. }
  315. int mod = angle % 45;
  316. int quot = angle / 45;
  317. Point anglePoint = currentPoint;
  318. int rotateAngle = 0;
  319. if (mod < 22)
  320. {
  321. Matrix rotateMatrix = new Matrix();
  322. rotateAngle = quot * 45;
  323. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  324. anglePoint = rotateMatrix.Transform(originPoint);
  325. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  326. }
  327. else
  328. {
  329. Matrix rotateMatrix = new Matrix();
  330. rotateAngle = (quot + 1) * 45;
  331. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  332. anglePoint = rotateMatrix.Transform(originPoint);
  333. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  334. }
  335. if (checkRect.Contains(anglePoint) == false)
  336. {
  337. switch (rotateAngle)
  338. {
  339. case 0:
  340. {
  341. anglePoint.X = startPoint.X;
  342. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  343. }
  344. break;
  345. case 45:
  346. {
  347. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  348. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  349. anglePoint.X = startPoint.X + addValue;
  350. anglePoint.Y = startPoint.Y - addValue;
  351. }
  352. break;
  353. case 90:
  354. {
  355. anglePoint.X = startPoint.X + Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  356. anglePoint.Y = startPoint.Y;
  357. }
  358. break;
  359. case 135:
  360. {
  361. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  362. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  363. anglePoint.X = startPoint.X + addValue;
  364. anglePoint.Y = startPoint.Y + addValue;
  365. }
  366. break;
  367. case 180:
  368. {
  369. anglePoint.X = startPoint.X;
  370. anglePoint.Y = Math.Min(anglePoint.Y, checkRect.Bottom);
  371. }
  372. break;
  373. case 225:
  374. {
  375. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  376. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  377. anglePoint.X = startPoint.X - addValue;
  378. anglePoint.Y = startPoint.Y + addValue;
  379. }
  380. break;
  381. case 270:
  382. {
  383. anglePoint.X = startPoint.X - Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  384. anglePoint.Y = startPoint.Y;
  385. }
  386. break;
  387. case 315:
  388. {
  389. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  390. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  391. anglePoint.X = startPoint.X - addValue;
  392. anglePoint.Y = startPoint.Y - addValue;
  393. }
  394. break;
  395. case 360:
  396. {
  397. anglePoint.X = startPoint.X;
  398. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  399. }
  400. break;
  401. default:
  402. break;
  403. }
  404. }
  405. return anglePoint;
  406. }
  407. }
  408. #endregion
  409. }