123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*
- http://www.cgsoso.com/forum-211-1.html
- CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
- CGSOSO 主打游戏开发,影视设计等CG资源素材。
- 插件如若商用,请务必官网购买!
- daily assets update for try.
- U should buy the asset from home store if u use it in your project!
- */
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- namespace Paroxe.PdfRenderer
- {
- #if !UNITY_WEBGL
- /// <summary>
- /// Represent a search session within a specific page. To search in entire document use PDFTextPage.Search
- /// </summary>
- public class PDFSearchHandle : IDisposable
- {
- public enum MatchOption
- {
- NONE = 0x00000000,
- MATCH_CASE = 0x00000001,
- MATCH_WHOLE_WORD = 0x00000002,
- MATCH_CASE_AND_WHOLE_WORD = 0x00000003
- }
- private bool m_Disposed;
- private IntPtr m_NativePointer;
- private PDFTextPage m_TextPage;
- public PDFSearchHandle(PDFTextPage textPage, byte[] findWhatUnicode, int startIndex,
- MatchOption flags = MatchOption.NONE)
- {
- if (textPage == null)
- throw new NullReferenceException();
- if (startIndex < 0)
- throw new ArgumentOutOfRangeException();
- PDFLibrary.AddRef("PDFSearchHandle");
- m_TextPage = textPage;
- IntPtr unmanagedPointer = Marshal.AllocHGlobal(findWhatUnicode.Length);
- Marshal.Copy(findWhatUnicode, 0, unmanagedPointer, findWhatUnicode.Length);
- m_NativePointer = FPDFText_FindStart(textPage.NativePointer, unmanagedPointer, (int) flags, startIndex);
- Marshal.FreeHGlobal(unmanagedPointer);
- }
- ~PDFSearchHandle()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!m_Disposed)
- {
- lock (PDFLibrary.nativeLock)
- {
- if (m_NativePointer != IntPtr.Zero)
- {
- FPDFText_FindClose(m_NativePointer);
- m_NativePointer = IntPtr.Zero;
- }
- }
- PDFLibrary.RemoveRef("PDFSearchHandle");
- m_Disposed = true;
- }
- }
- public IntPtr NativePointer
- {
- get { return m_NativePointer; }
- }
- /// <summary>
- /// Return an array containing all the searchResults. If there is no result, this function return null.
- /// </summary>
- /// <returns></returns>
- public IList<PDFSearchResult> GetResults()
- {
- List<PDFSearchResult> results = new List<PDFSearchResult>();
- foreach (PDFSearchResult result in EnumerateSearchResults())
- {
- results.Add(result);
- }
- return results;
- }
- /// <summary>
- /// Enumerate search results.
- /// </summary>
- /// <returns></returns>
- public IEnumerable<PDFSearchResult> EnumerateSearchResults()
- {
- if (m_NativePointer != IntPtr.Zero)
- {
- while (FPDFText_FindNext(m_NativePointer))
- yield return new PDFSearchResult(
- m_TextPage.PageIndex,
- FPDFText_GetSchResultIndex(m_NativePointer),
- FPDFText_GetSchCount(m_NativePointer));
- }
- }
- /// <summary>
- /// Get the next search result. If there is no other result, the function returns an invalid searchResult (validate it with PDFSearchResult.IsValid)
- /// </summary>
- /// <returns></returns>
- public PDFSearchResult FindNext()
- {
- if (FPDFText_FindNext(m_NativePointer))
- return new PDFSearchResult(
- m_TextPage.PageIndex,
- FPDFText_GetSchResultIndex(m_NativePointer),
- FPDFText_GetSchCount(m_NativePointer));
- return new PDFSearchResult(-1, -1, -1);
- }
- /// <summary>
- /// Get the previous search result. If there is no other result, the function returns an invalid searchResult (validate it with PDFSearchResult.IsValid)
- /// </summary>
- /// <returns></returns>
- public PDFSearchResult FindPrevious()
- {
- if (FPDFText_FindPrev(m_NativePointer))
- return new PDFSearchResult(
- m_TextPage.PageIndex,
- FPDFText_GetSchResultIndex(m_NativePointer),
- FPDFText_GetSchCount(m_NativePointer));
- return new PDFSearchResult(-1, -1, -1);
- }
- #region NATIVE
- [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
- private static extern IntPtr FPDFText_FindStart(IntPtr text_page, IntPtr buffer, int flags, int start_index);
- [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
- private static extern void FPDFText_FindClose(IntPtr handle);
- [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
- private static extern bool FPDFText_FindNext(IntPtr handle);
- [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
- private static extern bool FPDFText_FindPrev(IntPtr handle);
- [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
- private static extern int FPDFText_GetSchCount(IntPtr handle);
- [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
- private static extern int FPDFText_GetSchResultIndex(IntPtr handle);
- #endregion
- }
- #endif
- }
|