PDFPage.cs 12 KB

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