PDFLibrary.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 System;
  10. using System.Collections.Generic;
  11. using System.Runtime.InteropServices;
  12. using UnityEngine;
  13. namespace Paroxe.PdfRenderer
  14. {
  15. public class PDFLibrary : IDisposable
  16. {
  17. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  18. public const string PLUGIN_ASSEMBLY = "__Internal";
  19. #else
  20. public const string PLUGIN_ASSEMBLY = "pdfrenderer";
  21. #endif
  22. public const string CurrentVersion = "3.0";
  23. public static System.Object nativeLock = new System.Object();
  24. public enum ErrorCode
  25. {
  26. ErrSuccess = 0, // No error.
  27. ErrUnknown = 1, // Unknown error.
  28. ErrFile = 2, // File not found or could not be opened.
  29. ErrFormat = 3, // File not in PDF format or corrupted.
  30. ErrPassword = 4, // Password required or incorrect password.
  31. ErrSecurity = 5, // Unsupported security scheme.
  32. ErrPage = 6 // Page not found or content error.
  33. }
  34. private static bool m_Disposed;
  35. private static PDFLibrary m_Instance;
  36. private static int m_RefCount;
  37. private bool m_IsInitialized;
  38. private static bool m_AlreadyDestroyed;
  39. #if UNITY_WEBGL || !UNITY_EDITOR
  40. private static bool m_AlreadyInitialized;
  41. #endif
  42. private PDFLibrary()
  43. {
  44. #if UNITY_WEBGL && !UNITY_EDITOR
  45. if (!m_AlreadyInitialized)
  46. {
  47. PDFJS_InitLibrary();
  48. m_AlreadyInitialized = true;
  49. }
  50. #else
  51. #if !UNITY_EDITOR
  52. if (!m_AlreadyInitialized)
  53. #endif
  54. {
  55. m_IsInitialized = true;
  56. m_AlreadyDestroyed = false;
  57. FPDF_InitLibrary(
  58. "These binary file is intended to be used only by PDFRenderer plugin for Unity3D. http://paroxe.com/products/pdf-renderer/");
  59. #if !UNITY_EDITOR
  60. m_AlreadyInitialized = true;
  61. #endif
  62. }
  63. #endif
  64. }
  65. ~PDFLibrary()
  66. {
  67. Dispose(false);
  68. }
  69. /// <summary>
  70. /// Return the last error code.
  71. /// </summary>
  72. /// <returns></returns>
  73. public static ErrorCode GetLastError()
  74. {
  75. #if UNITY_WEBGL && !UNITY_EDITOR
  76. return ErrorCode.ErrSuccess;
  77. #else
  78. Instance.EnsureInitialized();
  79. return (ErrorCode)FPDF_GetLastError();
  80. #endif
  81. }
  82. public void Dispose()
  83. {
  84. Dispose(true);
  85. GC.SuppressFinalize(this);
  86. }
  87. protected virtual void Dispose(bool disposing)
  88. {
  89. lock (nativeLock)
  90. {
  91. if (!m_Disposed)
  92. {
  93. if (m_RefCount == 0)
  94. {
  95. #if PDFRENDERER_DEBUG
  96. PrintInstanceMap();
  97. #endif
  98. #if !UNITY_WEBGL || UNITY_EDITOR
  99. if (!m_AlreadyDestroyed)
  100. {
  101. m_AlreadyDestroyed = true;
  102. #if UNITY_EDITOR
  103. FPDF_DestroyLibrary();
  104. #endif
  105. }
  106. #endif
  107. m_Disposed = true;
  108. m_Instance = null;
  109. }
  110. m_Disposed = true;
  111. }
  112. }
  113. }
  114. #if PDFRENDERER_DEBUG
  115. private static Dictionary<string, int> s_InstanceMap = new Dictionary<string, int>();
  116. private static void PrintInstanceMap()
  117. {
  118. foreach (string key in s_InstanceMap.Keys)
  119. {
  120. Debug.Log(key + " Count: " + s_InstanceMap[key]);
  121. }
  122. }
  123. #endif
  124. internal static void AddRef(string token)
  125. {
  126. lock (nativeLock)
  127. {
  128. #if PDFRENDERER_DEBUG
  129. if (s_InstanceMap.ContainsKey(token))
  130. s_InstanceMap[token] = s_InstanceMap[token] + 1;
  131. else
  132. s_InstanceMap.Add(token, 1);
  133. #endif
  134. ++m_RefCount;
  135. Instance.EnsureInitialized();
  136. }
  137. }
  138. internal static void RemoveRef(string token)
  139. {
  140. lock (nativeLock)
  141. {
  142. --m_RefCount;
  143. #if PDFRENDERER_DEBUG
  144. s_InstanceMap[token] = s_InstanceMap[token] - 1;
  145. #endif
  146. if (m_RefCount == 0)
  147. {
  148. if (m_Disposed)
  149. {
  150. #if PDFRENDERER_DEBUG
  151. PrintInstanceMap();
  152. #endif
  153. #if !UNITY_WEBGL || UNITY_EDITOR
  154. if (!m_AlreadyDestroyed)
  155. {
  156. m_AlreadyDestroyed = true;
  157. FPDF_DestroyLibrary();
  158. }
  159. #endif
  160. m_Instance = null;
  161. }
  162. else
  163. m_Instance.Dispose();
  164. }
  165. }
  166. }
  167. public static PDFLibrary Instance
  168. {
  169. get
  170. {
  171. if (m_Instance == null)
  172. m_Instance = new PDFLibrary();
  173. return m_Instance;
  174. }
  175. }
  176. public void EnsureInitialized()
  177. {
  178. }
  179. public bool IsInitialized
  180. {
  181. get
  182. {
  183. return m_IsInitialized;
  184. }
  185. set
  186. {
  187. m_IsInitialized = value;
  188. }
  189. }
  190. #region NATIVE
  191. #if UNITY_WEBGL && !UNITY_EDITOR
  192. [DllImport(PLUGIN_ASSEMBLY)]
  193. private static extern void PDFJS_InitLibrary();
  194. #else
  195. [DllImport(PLUGIN_ASSEMBLY)]
  196. private static extern uint FPDF_GetLastError();
  197. [DllImport(PLUGIN_ASSEMBLY)]
  198. private static extern void FPDF_DestroyLibrary();
  199. [DllImport(PLUGIN_ASSEMBLY, CharSet = CharSet.Ansi)]
  200. private static extern void FPDF_InitLibrary(string libSignature);
  201. #endif
  202. #endregion
  203. }
  204. }