FrameSelectTool.cs 15 KB

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