PDFViewerPage.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. namespace Paroxe.PdfRenderer.Internal.Viewer
  4. {
  5. #if UNITY_WEBGL
  6. public class PDFViewerPage : UIBehaviour
  7. {
  8. public Texture2D m_HandCursor;
  9. public void ClearCache() {}
  10. }
  11. #endif
  12. #if !UNITY_WEBGL
  13. public class PDFViewerPage : UIBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
  14. #if UNITY_EDITOR || UNITY_STANDALONE_WIN|| UNITY_STANDALONE_OSX
  15. , IPointerEnterHandler, IPointerExitHandler
  16. #endif
  17. {
  18. public Texture2D m_HandCursor;
  19. private PDFViewer m_PDFViewer;
  20. #if UNITY_EDITOR || UNITY_STANDALONE_WIN|| UNITY_STANDALONE_OSX
  21. private bool m_PointerInside = false;
  22. private bool m_HandCursorSettedByMe = false;
  23. #endif
  24. private Camera m_CanvasCamera;
  25. public PDFPage m_Page;
  26. public void ClearCache()
  27. {
  28. if (m_Page != null)
  29. m_Page.Dispose();
  30. m_Page = null;
  31. }
  32. public void OnPointerClick(PointerEventData eventData)
  33. {
  34. if (m_PDFViewer == null)
  35. m_PDFViewer = GetComponentInParent<PDFViewer>();
  36. if (m_PDFViewer.Document == null)
  37. return;
  38. if (m_PDFViewer.LinksActionHandler == null)
  39. return;
  40. if (m_Page == null)
  41. m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
  42. PDFLink link = GetLinkAtPoint(eventData.pressPosition, eventData.pressEventCamera);
  43. if (link != null)
  44. PDFActionHandlerHelper.ExecuteLinkAction(m_PDFViewer, link);
  45. else if (m_PDFViewer.ParagraphZoomingEnable && eventData.clickCount == 2)
  46. {
  47. using (PDFTextPage textPage = m_Page.GetTextPage())
  48. {
  49. Rect pageRect = new Rect();
  50. Vector2 pos = eventData.pressPosition;
  51. RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, pos, GetComponent<Camera>(), out pos);
  52. RectTransform rt = transform as RectTransform;
  53. pos += rt.sizeDelta.x * 0.5f * Vector2.right;
  54. pos.y = -pos.y + rt.sizeDelta.y * 0.5f;
  55. pos = pos.x * (rt.sizeDelta.y / rt.sizeDelta.x) * Vector2.right + pos.y * Vector2.up;
  56. Vector2 pagePoint = m_Page.DeviceToPage(0, 0, (int)rt.sizeDelta.y, (int)rt.sizeDelta.y, PDFPage.PageRotation.Normal, (int)pos.x, (int)pos.y);
  57. Vector2 pageSize = m_Page.GetPageSize();
  58. float threshold = m_PDFViewer.ParagraphDetectionThreshold;
  59. string text = GetBoundedText(textPage, 0.0f, pagePoint.y + 0.0f, pageSize.x, pagePoint.y - 1.0f);
  60. if (!string.IsNullOrEmpty(text.Trim()) && text.Trim().Length > 4)
  61. {
  62. string prevText = text;
  63. float bottomOffset = 0.0f;
  64. float topOffset = 0.0f;
  65. float t = 0.0f;
  66. while (true)
  67. {
  68. bottomOffset += 2.0f;
  69. text = GetBoundedText(textPage, 0.0f, pagePoint.y + 0.0f, pageSize.x, pagePoint.y - bottomOffset);
  70. if (text == prevText)
  71. t += 2.0f;
  72. else
  73. t = 0.0f;
  74. if (t >= threshold)
  75. break;
  76. prevText = text;
  77. }
  78. t = 0.0f;
  79. while (true)
  80. {
  81. topOffset += 2.0f;
  82. text = GetBoundedText(textPage, 0.0f, pagePoint.y + topOffset, pageSize.x, pagePoint.y - bottomOffset);
  83. if (text == prevText)
  84. t += 2.0f;
  85. else
  86. t = 0.0f;
  87. if (t >= threshold)
  88. break;
  89. prevText = text;
  90. }
  91. pageRect = new Rect(0.0f, pagePoint.y + topOffset, pageSize.x, (pagePoint.y + topOffset) - (pagePoint.y - bottomOffset));
  92. m_PDFViewer.ZoomOnParagraph(this, pageRect);
  93. }
  94. }
  95. }
  96. }
  97. private string GetBoundedText(PDFTextPage textPage, float left, float top, float right, float bottom)
  98. {
  99. return textPage.GetBoundedText(left, top, right, bottom, 4096);
  100. }
  101. #if UNITY_EDITOR || UNITY_STANDALONE_WIN|| UNITY_STANDALONE_OSX
  102. public void OnPointerEnter(PointerEventData eventData)
  103. {
  104. if (m_PDFViewer == null)
  105. {
  106. m_PDFViewer = GetComponentInParent<PDFViewer>();
  107. }
  108. if (m_PDFViewer.Document == null)
  109. {
  110. return;
  111. }
  112. m_PointerInside = true;
  113. if (m_Page == null)
  114. m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
  115. }
  116. public void OnPointerExit(PointerEventData eventData)
  117. {
  118. m_PointerInside = false;
  119. if (m_HandCursorSettedByMe)
  120. {
  121. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  122. }
  123. if (m_Page != null)
  124. m_Page.Dispose();
  125. m_Page = null;
  126. }
  127. #endif
  128. protected override void OnEnable()
  129. {
  130. if (m_PDFViewer == null)
  131. m_PDFViewer = GetComponentInParent<PDFViewer>();
  132. if (m_CanvasCamera == null)
  133. m_CanvasCamera = FindCanvasCamera(transform as RectTransform);
  134. #if UNITY_EDITOR || UNITY_STANDALONE_WIN|| UNITY_STANDALONE_OSX
  135. m_PointerInside = false;
  136. #endif
  137. }
  138. protected override void OnDisable()
  139. {
  140. if (m_Page != null)
  141. m_Page.Dispose();
  142. m_Page = null;
  143. }
  144. private Camera FindCanvasCamera(RectTransform rt)
  145. {
  146. RectTransform parent = rt.parent as RectTransform;
  147. if (parent != null)
  148. {
  149. Canvas canvas = parent.GetComponent<Canvas>();
  150. if (canvas != null && canvas.worldCamera != null)
  151. {
  152. return canvas.worldCamera;
  153. }
  154. return FindCanvasCamera(parent);
  155. }
  156. return null;
  157. }
  158. #if UNITY_EDITOR || UNITY_STANDALONE_WIN|| UNITY_STANDALONE_OSX
  159. private void Update()
  160. {
  161. if (m_PDFViewer == null)
  162. m_PDFViewer = GetComponentInParent<PDFViewer>();
  163. if (m_HandCursor == null || !m_PDFViewer.ChangeCursorWhenOverURL)
  164. return;
  165. if (m_CanvasCamera == null)
  166. m_CanvasCamera = FindCanvasCamera(transform as RectTransform);
  167. if (m_PointerInside)
  168. {
  169. PDFLink link = GetLinkAtPoint(Input.mousePosition, m_CanvasCamera);
  170. if (link != null)
  171. {
  172. Cursor.SetCursor(m_HandCursor, new Vector2(6.0f, 0.0f), CursorMode.Auto);
  173. m_HandCursorSettedByMe = true;
  174. }
  175. else
  176. {
  177. Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
  178. }
  179. }
  180. }
  181. #endif
  182. public void OnPointerDown(PointerEventData eventData) { }
  183. public void OnPointerUp(PointerEventData eventData) { }
  184. private PDFLink GetLinkAtPoint(Vector2 point, Camera camera)
  185. {
  186. if (m_PDFViewer == null)
  187. m_PDFViewer = GetComponentInParent<PDFViewer>();
  188. if (m_Page == null)
  189. m_Page = m_PDFViewer.Document.GetPage(transform.GetSiblingIndex());
  190. Vector2 localPointerPosition;
  191. if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, point, camera,
  192. out localPointerPosition))
  193. {
  194. RectTransform rt = transform as RectTransform;
  195. localPointerPosition += rt.sizeDelta.x * 0.5f * Vector2.right;
  196. localPointerPosition.y = -localPointerPosition.y + rt.sizeDelta.y * 0.5f;
  197. localPointerPosition = localPointerPosition.x * (rt.sizeDelta.y / rt.sizeDelta.x) * Vector2.right +
  198. localPointerPosition.y * Vector2.up;
  199. Vector2 pagePoint = m_Page.DeviceToPage(0, 0, (int)rt.sizeDelta.y, (int)rt.sizeDelta.y,
  200. PDFPage.PageRotation.Normal,
  201. (int)localPointerPosition.x, (int)localPointerPosition.y);
  202. return m_Page.GetLinkAtPoint(pagePoint);
  203. }
  204. return null;
  205. }
  206. }
  207. #endif
  208. }