PDFPageRange.cs 10 KB

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