PDFPageRange.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 UnityEngine;
  14. namespace Paroxe.PdfRenderer.Internal.Viewer
  15. {
  16. public class PDFPageRange : IEquatable<PDFPageRange>
  17. {
  18. public int m_From = -1;
  19. public int m_To = -1;
  20. public bool IsValid
  21. {
  22. get { return (m_From != -1 && m_To != -1); }
  23. }
  24. public int RangeLength
  25. {
  26. get { return IsValid ? (m_To - m_From) : 0; }
  27. }
  28. public static int[] GetPagesToload(PDFPageRange from, PDFPageRange to)
  29. {
  30. List<int> pagesToLoad = new List<int>();
  31. if (!from.IsValid)
  32. {
  33. for (int i = to.m_From; i < to.m_To; ++i)
  34. {
  35. pagesToLoad.Add(i);
  36. }
  37. return pagesToLoad.ToArray();
  38. }
  39. bool[] toLoad = new bool[Math.Max(from.m_To, to.m_To)];
  40. for (int i = to.m_From; i < to.m_To; ++i)
  41. {
  42. toLoad[i] = true;
  43. }
  44. for (int i = from.m_From; i < from.m_To; ++i)
  45. {
  46. toLoad[i] = false;
  47. }
  48. for (int i = 0; i < toLoad.Length; ++i)
  49. {
  50. if (toLoad[i])
  51. {
  52. pagesToLoad.Add(i);
  53. }
  54. }
  55. return pagesToLoad.ToArray();
  56. }
  57. public static int[] GetPagesToUnload(PDFPageRange from, PDFPageRange to)
  58. {
  59. List<int> pagesToUnLoad = new List<int>();
  60. if (!from.IsValid)
  61. {
  62. return pagesToUnLoad.ToArray();
  63. }
  64. bool[] toUnload = new bool[Math.Max(from.m_To, to.m_To)];
  65. for (int i = from.m_From; i < from.m_To; ++i)
  66. {
  67. toUnload[i] = true;
  68. }
  69. for (int i = to.m_From; i < to.m_To; ++i)
  70. {
  71. toUnload[i] = false;
  72. }
  73. for (int i = 0; i < toUnload.Length; ++i)
  74. {
  75. if (toUnload[i])
  76. {
  77. pagesToUnLoad.Add(i);
  78. }
  79. }
  80. return pagesToUnLoad.ToArray();
  81. }
  82. public static void UpdatePageAgainstRanges(PDFPageRange fromRange, PDFPageRange toRange, PDFDocument pdfDocument,
  83. PDFPageTextureHolder[] pageTextureHolderList, PDFRenderer.RenderSettings renderSettings, float scale,
  84. IPDFColoredRectListProvider rectsProvider, Vector2[] pageSizes)
  85. {
  86. int[] pagesToLoad = GetPagesToload(fromRange, toRange);
  87. int[] pagesToUnLoad = GetPagesToUnload(fromRange, toRange);
  88. List<Texture2D> recyclableTextures = new List<Texture2D>();
  89. for (int i = 0; i < pagesToUnLoad.Length; ++i)
  90. {
  91. #if UNITY_WEBGL
  92. pageTextureHolderList[pagesToUnLoad[i]].m_Visible = false;
  93. if (pageTextureHolderList[pagesToUnLoad[i]].m_RenderingPromise != null)
  94. {
  95. PDFJS_Library.Instance.TryTerminateRenderingWorker(pageTextureHolderList[pagesToUnLoad[i]].m_RenderingPromise.PromiseHandle);
  96. pageTextureHolderList[pagesToUnLoad[i]].m_RenderingPromise = null;
  97. }
  98. Texture2D tex = pageTextureHolderList[pagesToUnLoad[i]].Texture;
  99. if (tex != null)
  100. {
  101. recyclableTextures.Add(tex);
  102. pageTextureHolderList[pagesToUnLoad[i]].Texture = null;
  103. }
  104. #else
  105. Texture2D tex = pageTextureHolderList[pagesToUnLoad[i]].Texture;
  106. if (tex != null)
  107. {
  108. recyclableTextures.Add(tex);
  109. pageTextureHolderList[pagesToUnLoad[i]].Texture = null;
  110. }
  111. #endif
  112. }
  113. for (int i = 0; i < pagesToLoad.Length; ++i)
  114. {
  115. #if UNITY_WEBGL
  116. pageTextureHolderList[pagesToLoad[i]].m_Visible = true;
  117. if (pageTextureHolderList[pagesToLoad[i]].m_RenderingStarted)
  118. continue;
  119. #endif
  120. int w = (int)(pageSizes[pagesToLoad[i]].x * scale);
  121. int h = (int)(pageSizes[pagesToLoad[i]].y * scale);
  122. Texture2D tex = null;
  123. foreach (Texture2D texture in recyclableTextures)
  124. {
  125. if (texture.width == w && texture.height == h)
  126. {
  127. tex = texture;
  128. break;
  129. }
  130. }
  131. #if UNITY_WEBGL
  132. if (tex != null)
  133. {
  134. recyclableTextures.Remove(tex);
  135. //pdfDocument.Renderer.RenderPageToExistingTexture(pdfDocument.GetPage(pagesToLoad[i]), tex, rectsProvider, renderSettings);
  136. pageTextureHolderList[pagesToLoad[i]].m_RenderingStarted = true;
  137. PDFJS_Library.Instance.StartCoroutine(UpdatePageWithExistingTexture(pdfDocument, pagesToLoad[i], tex, pageTextureHolderList));
  138. }
  139. else
  140. {
  141. //tex = pdfDocument.Renderer.RenderPageToTexture(pdfDocument.GetPage(pagesToLoad[i]), w, h, rectsProvider, renderSettings);
  142. pageTextureHolderList[pagesToLoad[i]].m_RenderingStarted = true;
  143. PDFJS_Library.Instance.StartCoroutine(UpdatePageWithNewTexture(pdfDocument, pagesToLoad[i], pageTextureHolderList, w, h));
  144. }
  145. #else
  146. if (tex != null)
  147. {
  148. recyclableTextures.Remove(tex);
  149. pdfDocument.Renderer.RenderPageToExistingTexture(pdfDocument.GetPage(pagesToLoad[i]), tex, rectsProvider, renderSettings);
  150. }
  151. else
  152. tex = pdfDocument.Renderer.RenderPageToTexture(pdfDocument.GetPage(pagesToLoad[i]), w, h, rectsProvider, renderSettings);
  153. pageTextureHolderList[pagesToLoad[i]].Texture = tex;
  154. #endif
  155. }
  156. foreach (Texture2D unusedTexture in recyclableTextures)
  157. {
  158. UnityEngine.Object.DestroyImmediate(unusedTexture);
  159. Resources.UnloadAsset(unusedTexture);
  160. }
  161. recyclableTextures.Clear();
  162. GC.Collect();
  163. GC.WaitForPendingFinalizers();
  164. }
  165. #if UNITY_WEBGL
  166. public static IEnumerator UpdatePageWithExistingTexture(PDFDocument document, int pageIndex, Texture2D existingTexture, PDFPageTextureHolder[] pageTextureHolderList)
  167. {
  168. PDFJS_Promise<PDFPage> pagePromise = document.GetPageAsync(pageIndex);
  169. while (!pagePromise.HasFinished)
  170. yield return null;
  171. if (pagePromise.HasSucceeded)
  172. {
  173. PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToExistingTextureAsync(pagePromise.Result, existingTexture);
  174. pageTextureHolderList[pageIndex].m_RenderingPromise = renderPromise;
  175. while (!renderPromise.HasFinished)
  176. yield return null;
  177. pageTextureHolderList[pageIndex].m_RenderingPromise = null;
  178. pageTextureHolderList[pageIndex].m_RenderingStarted = false;
  179. if (renderPromise.HasSucceeded)
  180. {
  181. if (pageTextureHolderList[pageIndex].Texture != null
  182. && pageTextureHolderList[pageIndex].Texture != renderPromise.Result)
  183. {
  184. UnityEngine.Object.DestroyImmediate(pageTextureHolderList[pageIndex].Texture);
  185. Resources.UnloadAsset(pageTextureHolderList[pageIndex].Texture);
  186. pageTextureHolderList[pageIndex].Texture = null;
  187. }
  188. if (pageTextureHolderList[pageIndex].m_Visible)
  189. pageTextureHolderList[pageIndex].Texture = renderPromise.Result;
  190. else
  191. {
  192. UnityEngine.Object.DestroyImmediate(renderPromise.Result);
  193. Resources.UnloadAsset(renderPromise.Result);
  194. renderPromise.Result = null;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. pageTextureHolderList[pageIndex].m_RenderingPromise = null;
  201. pageTextureHolderList[pageIndex].m_RenderingStarted = false;
  202. }
  203. }
  204. public static IEnumerator UpdatePageWithNewTexture(PDFDocument document, int pageIndex, PDFPageTextureHolder[] pageTextureHolderList, int width, int height)
  205. {
  206. PDFJS_Promise<PDFPage> pagePromise = document.GetPageAsync(pageIndex);
  207. while (!pagePromise.HasFinished)
  208. yield return null;
  209. if (pagePromise.HasSucceeded)
  210. {
  211. PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToTextureAsync(pagePromise.Result, width, height);
  212. pageTextureHolderList[pageIndex].m_RenderingPromise = renderPromise;
  213. while (!renderPromise.HasFinished)
  214. yield return null;
  215. pageTextureHolderList[pageIndex].m_RenderingPromise = null;
  216. pageTextureHolderList[pageIndex].m_RenderingStarted = false;
  217. if (renderPromise.HasSucceeded)
  218. {
  219. if (pageTextureHolderList[pageIndex].Texture != null)
  220. {
  221. UnityEngine.Object.DestroyImmediate(pageTextureHolderList[pageIndex].Texture);
  222. Resources.UnloadAsset(pageTextureHolderList[pageIndex].Texture);
  223. pageTextureHolderList[pageIndex].Texture = null;
  224. }
  225. if (pageTextureHolderList[pageIndex].m_Visible)
  226. pageTextureHolderList[pageIndex].Texture = renderPromise.Result;
  227. else
  228. {
  229. UnityEngine.Object.DestroyImmediate(renderPromise.Result);
  230. Resources.UnloadAsset(renderPromise.Result);
  231. renderPromise.Result = null;
  232. }
  233. }
  234. }
  235. else
  236. {
  237. pageTextureHolderList[pageIndex].m_RenderingPromise = null;
  238. pageTextureHolderList[pageIndex].m_RenderingStarted = false;
  239. }
  240. }
  241. #endif
  242. public bool ContainsPage(int pageIndex)
  243. {
  244. if (!IsValid)
  245. {
  246. return false;
  247. }
  248. return (pageIndex >= m_From && pageIndex < m_To);
  249. }
  250. public bool Equals(PDFPageRange other)
  251. {
  252. if (other == null || GetType() != other.GetType())
  253. {
  254. return false;
  255. }
  256. var objectToCompareWith = (PDFPageRange)other;
  257. return (objectToCompareWith.m_From == m_From && objectToCompareWith.m_To == m_To);
  258. }
  259. public override int GetHashCode()
  260. {
  261. var calculation = m_From + m_To;
  262. return calculation.GetHashCode();
  263. }
  264. }
  265. }