FrameSelectTool.cs 16 KB

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