PDFPage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using Paroxe.PdfRenderer.WebGL;
  2. using System;
  3. using System.Collections.Generic;
  4. using Paroxe.PdfRenderer.Internal;
  5. using UnityEngine;
  6. using System.Collections; // For WebGL
  7. namespace Paroxe.PdfRenderer
  8. {
  9. /// <summary>
  10. /// Represents a PDF page inside document.
  11. /// </summary>
  12. public class PDFPage : IIPDFPageInternal, IDisposable, IEquatable<PDFPage>
  13. {
  14. private bool m_Disposed;
  15. private IntPtr m_NativePointer;
  16. private PDFDocument m_Document;
  17. private int m_PageIndex;
  18. #if !UNITY_WEBGL
  19. private bool m_Disposing;
  20. private HashSet<PDFTextPage> m_LoadedTextPages;
  21. #endif
  22. #if !UNITY_WEBGL || UNITY_EDITOR
  23. private static Dictionary<IntPtr, int> s_InstanceMap = new Dictionary<IntPtr, int>();
  24. #endif
  25. /// <summary>
  26. /// Rotations are clockwise
  27. /// </summary>
  28. public enum PageRotation
  29. {
  30. Normal = 0,
  31. Rotate90 = 1,
  32. Rotate180 = 2,
  33. Rotate270 = 3
  34. };
  35. public static PDFJS_Promise<PDFPage> LoadPageAsync(PDFDocument document, int pageIndex)
  36. {
  37. PDFJS_Promise<PDFPage> pagePromise = new PDFJS_Promise<PDFPage>();
  38. #if UNITY_WEBGL && !UNITY_EDITOR
  39. LoadPageParameters parameters = new LoadPageParameters(document, pageIndex);
  40. PDFJS_Library.Instance.PreparePromiseCoroutine(LoadPageCoroutine, pagePromise, parameters).Start();
  41. #else
  42. pagePromise.HasFinished = true;
  43. pagePromise.HasSucceeded = true;
  44. pagePromise.HasReceivedJSResponse = true;
  45. pagePromise.Result = document.GetPage(pageIndex);
  46. #endif
  47. return pagePromise;
  48. }
  49. #if UNITY_WEBGL && !UNITY_EDITOR
  50. public PDFPage(PDFDocument document, IntPtr pageHandle, int pageIndex)
  51. {
  52. PDFLibrary.AddRef("PDFPage");
  53. m_Document = document;
  54. m_PageIndex = pageIndex;
  55. m_NativePointer = pageHandle;
  56. }
  57. #endif
  58. #if !UNITY_WEBGL || UNITY_EDITOR
  59. public PDFPage(PDFDocument document, int pageIndex)
  60. {
  61. if (document == null)
  62. throw new NullReferenceException();
  63. if (pageIndex < 0)
  64. throw new ArgumentOutOfRangeException();
  65. PDFLibrary.AddRef("PDFPage");
  66. m_Document = document;
  67. m_PageIndex = pageIndex;
  68. m_NativePointer = NativeMethods.FPDF_LoadPage(document.NativePointer, m_PageIndex);
  69. if (m_NativePointer != IntPtr.Zero)
  70. {
  71. if (s_InstanceMap.ContainsKey(m_NativePointer))
  72. {
  73. s_InstanceMap[m_NativePointer] = s_InstanceMap[m_NativePointer] + 1;
  74. }
  75. else
  76. s_InstanceMap[m_NativePointer] = 1;
  77. }
  78. }
  79. #endif
  80. ~PDFPage()
  81. {
  82. Dispose(false);
  83. }
  84. public void Dispose()
  85. {
  86. Dispose(true);
  87. GC.SuppressFinalize(this);
  88. }
  89. protected virtual void Dispose(bool _)
  90. {
  91. if (!m_Disposed)
  92. {
  93. #if !UNITY_WEBGL
  94. m_Disposing = true;
  95. #endif
  96. if (m_NativePointer != IntPtr.Zero)
  97. {
  98. #if !UNITY_WEBGL || UNITY_EDITOR
  99. lock (PDFLibrary.nativeLock)
  100. {
  101. s_InstanceMap[m_NativePointer] = s_InstanceMap[m_NativePointer] - 1;
  102. if (s_InstanceMap[m_NativePointer] == 0)
  103. {
  104. #if !UNITY_WEBGL
  105. if (m_LoadedTextPages != null)
  106. {
  107. foreach (PDFTextPage textPage in m_LoadedTextPages)
  108. textPage.Dispose();
  109. m_LoadedTextPages = null;
  110. }
  111. #endif
  112. ((IPDFDocumentInternal)m_Document).OnPageClose(this);
  113. NativeMethods.FPDF_ClosePage(m_NativePointer);
  114. s_InstanceMap.Remove(m_NativePointer);
  115. m_NativePointer = IntPtr.Zero;
  116. }
  117. }
  118. #else
  119. NativeMethods.PDFJS_ClosePage(m_NativePointer.ToInt32());
  120. m_NativePointer = IntPtr.Zero;
  121. #endif
  122. }
  123. PDFLibrary.RemoveRef("PDFPage");
  124. #if !UNITY_WEBGL
  125. m_Disposing = false;
  126. #endif
  127. m_Disposed = true;
  128. }
  129. }
  130. #if !UNITY_WEBGL
  131. void IIPDFPageInternal.OnTextPageClose(PDFTextPage textPage)
  132. {
  133. if (m_LoadedTextPages != null && !m_Disposing)
  134. m_LoadedTextPages.Remove(textPage);
  135. }
  136. #endif
  137. public IntPtr NativePointer
  138. {
  139. get { return m_NativePointer; }
  140. }
  141. public PDFDocument Document
  142. {
  143. get { return m_Document; }
  144. }
  145. public int PageIndex
  146. {
  147. get { return m_PageIndex; }
  148. }
  149. public Vector2 GetPageSize(float scale = 1.0f)
  150. {
  151. #if !UNITY_WEBGL || UNITY_EDITOR
  152. return m_Document.GetPageSize(m_PageIndex) * scale;
  153. #else
  154. return new Vector2(
  155. NativeMethods.PDFJS_GetPageWidth(m_NativePointer.ToInt32(), scale),
  156. NativeMethods.PDFJS_GetPageHeight(m_NativePointer.ToInt32(), scale));
  157. #endif
  158. }
  159. #if UNITY_WEBGL && !UNITY_EDITOR
  160. internal static Vector2 GetPageSize(IntPtr pageHandle, float scale = 1.0f)
  161. {
  162. return new Vector2(
  163. NativeMethods.PDFJS_GetPageWidth(pageHandle.ToInt32(), scale),
  164. NativeMethods.PDFJS_GetPageHeight(pageHandle.ToInt32(), scale));
  165. }
  166. #endif
  167. #if !UNITY_WEBGL
  168. /// <summary>
  169. /// Return an instance of PDFTextPage that give access the the current page text content
  170. /// </summary>
  171. /// <returns></returns>
  172. public PDFTextPage GetTextPage()
  173. {
  174. PDFTextPage textPage = new PDFTextPage(this);
  175. if (m_LoadedTextPages == null)
  176. m_LoadedTextPages = new HashSet<PDFTextPage>();
  177. m_LoadedTextPages.Add(textPage);
  178. return textPage;
  179. }
  180. public PDFLink GetLinkAtPoint(Vector2 pagePoint)
  181. {
  182. IntPtr linkPtr = NativeMethods.FPDFLink_GetLinkAtPoint(m_NativePointer, pagePoint.x, pagePoint.y);
  183. if (linkPtr != IntPtr.Zero)
  184. return new PDFLink(this, linkPtr);
  185. return null;
  186. }
  187. public PDFLink GetLinkAtPoint(double pageX, double pageY)
  188. {
  189. IntPtr linkPtr = NativeMethods.FPDFLink_GetLinkAtPoint(m_NativePointer, pageX, pageY);
  190. if (linkPtr != IntPtr.Zero)
  191. return new PDFLink(this, linkPtr);
  192. return null;
  193. }
  194. public Vector2 DeviceToPage(int startX, int startY, int sizeX, int sizeY, PageRotation rotation, int deviceX, int deviceY)
  195. {
  196. double pageX;
  197. double pageY;
  198. NativeMethods.FPDF_DeviceToPage(m_NativePointer, startX, startY, sizeX, sizeY, (int)rotation, deviceX, deviceY, out pageX,
  199. out pageY);
  200. return new Vector2((float)pageX, (float)pageY);
  201. }
  202. public Vector2 PageToDevice(int startX, int startY, int sizeX, int sizeY, PageRotation rotation, int pageX, int pageY)
  203. {
  204. int deviceX;
  205. int deviceY;
  206. NativeMethods.FPDF_PageToDevice(m_NativePointer, startX, startY, sizeX, sizeY, (int)rotation, pageX, pageY, out deviceX,
  207. out deviceY);
  208. return new Vector2(deviceX, deviceY);
  209. }
  210. public Vector2 ConvertPagePositionToUnityUIDevicePosition(Vector2 pagePoint, Vector2 devicePageSize)
  211. {
  212. pagePoint = pagePoint.x / (devicePageSize.y / devicePageSize.x) * Vector2.right + pagePoint.y * Vector2.up;
  213. int device_x;
  214. int device_y;
  215. NativeMethods.FPDF_PageToDevice(m_NativePointer, 0, 0, (int)devicePageSize.y, (int)devicePageSize.y, 0, pagePoint.x,
  216. pagePoint.y, out device_x, out device_y);
  217. return new Vector2(device_x, device_y);
  218. }
  219. public Rect ConvertPageRectToDeviceRect(Rect pageRect, Vector2 devicePageSize)
  220. {
  221. Vector2 min = ConvertPagePositionToUnityUIDevicePosition(pageRect.min, devicePageSize);
  222. float mx = pageRect.max.x;
  223. float my = (pageRect.min - (pageRect.max - pageRect.min)).y;
  224. Vector2 max = ConvertPagePositionToUnityUIDevicePosition(new Vector2(mx, my), devicePageSize);
  225. Rect rect = new Rect();
  226. rect.min = min;
  227. rect.max = max;
  228. return rect;
  229. }
  230. public Vector2 ConvertUnityUIDevicePositionToPagePosition(Vector2 devicePoint, Vector2 devicePageSize)
  231. {
  232. devicePoint = devicePoint.x * (devicePageSize.y / devicePageSize.x) * Vector2.right + devicePoint.y * Vector2.up;
  233. double page_x;
  234. double page_y;
  235. NativeMethods.FPDF_DeviceToPage(m_NativePointer, 0, 0, (int)devicePageSize.y, (int)devicePageSize.y, 0,
  236. (int)devicePoint.x, (int)devicePoint.y, out page_x, out page_y);
  237. return new Vector2((float)page_x, (float)page_y);
  238. }
  239. #endif
  240. public bool Equals(PDFPage other)
  241. {
  242. return (m_NativePointer != IntPtr.Zero && m_NativePointer == other.m_NativePointer);
  243. }
  244. #if UNITY_WEBGL && !UNITY_EDITOR
  245. private class LoadPageParameters
  246. {
  247. public PDFDocument document;
  248. public int pageIndex;
  249. public LoadPageParameters(PDFDocument document, int pageIndex)
  250. {
  251. this.document = document;
  252. this.pageIndex = pageIndex;
  253. }
  254. }
  255. private static IEnumerator LoadPageCoroutine(PDFJS_PromiseCoroutine promiseCoroutine, IPDFJS_Promise promise, object par)
  256. {
  257. PDFLibrary.Instance.EnsureInitialized();
  258. while (!PDFLibrary.Instance.IsInitialized)
  259. yield return null;
  260. PDFJS_Promise<PDFPage> pagePromise = promise as PDFJS_Promise<PDFPage>;
  261. LoadPageParameters parameters = par as LoadPageParameters;
  262. NativeMethods.PDFJS_LoadPage(promise.PromiseHandle, parameters.document.NativePointer.ToInt32(), parameters.pageIndex + 1);
  263. while (!pagePromise.HasReceivedJSResponse)
  264. yield return null;
  265. if (pagePromise.HasSucceeded)
  266. {
  267. int pageHandle = int.Parse(pagePromise.JSObjectHandle);
  268. PDFPage page = new PDFPage(parameters.document, new IntPtr(pageHandle), parameters.pageIndex);
  269. pagePromise.Result = page;
  270. pagePromise.HasFinished = true;
  271. promiseCoroutine.ExecuteThenAction(true, page);
  272. }
  273. else
  274. {
  275. pagePromise.Result = null;
  276. pagePromise.HasFinished = true;
  277. promiseCoroutine.ExecuteThenAction(false, null);
  278. }
  279. }
  280. #endif
  281. }
  282. public interface IIPDFPageInternal
  283. {
  284. #if !UNITY_WEBGL
  285. void OnTextPageClose(PDFTextPage textPage);
  286. #endif
  287. }
  288. }