PDFProgressiveSearch.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.Text;
  12. using UnityEngine;
  13. using UnityEngine.EventSystems;
  14. namespace Paroxe.PdfRenderer
  15. {
  16. #if !UNITY_WEBGL
  17. /// <summary>
  18. /// Don't instantiate this class directly. Use the static method instead PDFProgressiveSearch.CreateSearch
  19. /// </summary>
  20. public class PDFProgressiveSearch : UIBehaviour
  21. {
  22. private bool m_AbortRequested;
  23. private int m_CurrentPage;
  24. private PDFDocument m_Document;
  25. private PDFSearchHandle.MatchOption m_Flags;
  26. private bool m_NewSearchRequested;
  27. private int m_PageCount;
  28. private byte[] m_Search;
  29. private IList<PDFSearchResult>[] m_SearchResults;
  30. private bool m_SearchStarted;
  31. private float m_TimeBudget = 0.50f;
  32. private int m_Total;
  33. public delegate void ProgressEventHandler(PDFProgressiveSearch sender, int total);
  34. public delegate void SearchFinishedEventHandler(PDFProgressiveSearch sender, IList<PDFSearchResult>[] searchResults);
  35. public event ProgressEventHandler OnProgressChanged;
  36. public event SearchFinishedEventHandler OnSearchFinished;
  37. /// <summary>
  38. /// Create a progressive search object within the scene.
  39. /// </summary>
  40. /// <param name="document"></param>
  41. /// <param name="timeBudget">Time budget per frame. The value must be metween 0.0f and 1.0f</param>
  42. /// <returns></returns>
  43. public static PDFProgressiveSearch CreateSearch(PDFDocument document, float timeBudget)
  44. {
  45. GameObject searchObject = new GameObject();
  46. searchObject.name = "PDFProgressiveSearch";
  47. PDFProgressiveSearch progressiveSearch = searchObject.AddComponent<PDFProgressiveSearch>();
  48. progressiveSearch.m_Document = document;
  49. progressiveSearch.m_TimeBudget = Mathf.Clamp01(timeBudget);
  50. return progressiveSearch;
  51. }
  52. /// <summary>
  53. /// Stop the current search request.
  54. /// </summary>
  55. public void Abort()
  56. {
  57. m_AbortRequested = true;
  58. }
  59. /// <summary>
  60. /// Start a new search and if a search is already started this method will stop it.
  61. /// </summary>
  62. /// <param name="search"></param>
  63. /// <param name="flags">PDFSearchHandle.MATCH_CASE, (PDFSearchHandle.MATCH_WHOLE_WORD or PDFSearchHandle.MATCH_CASE | PDFSearchHandle.MATCH_WHOLE_WORD)</param>
  64. public void StartSearch(string search, PDFSearchHandle.MatchOption flags)
  65. {
  66. if (string.IsNullOrEmpty(search.Trim()))
  67. m_Search = null;
  68. else
  69. m_Search = Encoding.Unicode.GetBytes(search.Trim() + "\0");
  70. m_Flags = flags;
  71. m_PageCount = m_Document.GetPageCount();
  72. m_NewSearchRequested = true;
  73. }
  74. void LateUpdate()
  75. {
  76. if (!m_SearchStarted && m_NewSearchRequested)
  77. {
  78. m_CurrentPage = 0;
  79. m_SearchResults = new IList<PDFSearchResult>[m_PageCount];
  80. m_NewSearchRequested = false;
  81. m_SearchStarted = true;
  82. m_AbortRequested = false;
  83. m_Total = 0;
  84. }
  85. if (m_SearchStarted)
  86. {
  87. if (m_AbortRequested)
  88. {
  89. m_SearchStarted = false;
  90. m_NewSearchRequested = false;
  91. m_AbortRequested = false;
  92. m_Total = 0;
  93. return;
  94. }
  95. if (m_NewSearchRequested)
  96. {
  97. m_CurrentPage = 0;
  98. m_SearchResults = new IList<PDFSearchResult>[m_PageCount];
  99. m_NewSearchRequested = false;
  100. m_Total = 0;
  101. }
  102. if (m_Search == null || m_Search.Length == 0)
  103. {
  104. m_SearchStarted = false;
  105. if (OnProgressChanged != null)
  106. {
  107. OnProgressChanged(this, 0);
  108. }
  109. var handler = OnSearchFinished;
  110. if (handler != null)
  111. {
  112. handler(this, null);
  113. }
  114. }
  115. else
  116. {
  117. Stopwatch timer = Stopwatch.StartNew();
  118. for (int i = m_CurrentPage; i < m_PageCount; ++i)
  119. {
  120. using (PDFTextPage textPage = m_Document.GetPage(i).GetTextPage())
  121. {
  122. IList<PDFSearchResult> searchResults = textPage.Search(m_Search, m_Flags);
  123. m_SearchResults[i] = searchResults;
  124. m_Total += searchResults.Count;
  125. ++m_CurrentPage;
  126. if (timer.ElapsedMilliseconds >=
  127. m_TimeBudget*1000.0f*(1.0f/Mathf.Max(Application.targetFrameRate, 1.0f/Time.deltaTime)))
  128. {
  129. if (OnProgressChanged != null)
  130. {
  131. OnProgressChanged(this, m_Total);
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. if (m_CurrentPage + 1 > m_PageCount)
  138. {
  139. m_SearchStarted = false;
  140. if (OnProgressChanged != null)
  141. {
  142. OnProgressChanged(this, m_Total);
  143. }
  144. var handler = OnSearchFinished;
  145. if (handler != null)
  146. {
  147. handler(this, m_SearchResults);
  148. }
  149. if (OnProgressChanged != null)
  150. {
  151. OnProgressChanged(this, m_Total);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. #endif
  159. }