PDFViewer.cs 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using Paroxe.PdfRenderer.Internal;
  8. using Paroxe.PdfRenderer.Internal.Viewer;
  9. using UnityEngine;
  10. using UnityEngine.EventSystems;
  11. using UnityEngine.UI;
  12. using Paroxe.PdfRenderer.WebGL;
  13. #if NETFX_CORE && !UNITY_WSA_10_0
  14. using WinRTLegacy;
  15. #endif
  16. namespace Paroxe.PdfRenderer
  17. {
  18. /// <summary>
  19. /// PDFViewer is an Unity UI component that allow you to visualize PDF Document.
  20. /// </summary>
  21. public class PDFViewer : UIBehaviour, IPDFDevice, IPDFColoredRectListProvider
  22. {
  23. [SerializeField] public PDFViewerInternal m_Internal;
  24. private IPDFDeviceActionHandler m_BookmarksActionHandler;
  25. private IPDFDeviceActionHandler m_LinksActionHandler;
  26. private PDFPageRange m_CurrentPageRange;
  27. private PDFSearchResult m_CurrentSearchResult;
  28. private PDFDocument m_Document;
  29. private PDFDocument m_SuppliedDocument;
  30. private PDFPageTextureHolder[] m_PageTextureHolders;
  31. private int m_CurrentSearchResultIndex;
  32. private int m_CurrentSearchResultIndexWithinCurrentPage;
  33. private bool m_DelayedOnEnable;
  34. private float m_InvalidPasswordMessageDelay;
  35. #if !UNITY_WEBGL || UNITY_EDITOR
  36. private float m_InvalidPasswordMessageDelayBeforeFade = 0.5f;
  37. #endif
  38. private bool m_InvalidPasswordMessageVisisble;
  39. private bool m_IsLoaded;
  40. private int m_LoadAtPageIndex;
  41. private float m_OverlayAlpha = 0.50f;
  42. private bool m_OverlayVisible;
  43. private int m_PageCount;
  44. private float[] m_PageOffsets;
  45. private Vector2[] m_PageSizes;
  46. private Vector2[] m_NormalPageSizes;
  47. private byte[] m_PendingDocumentBuffer;
  48. private int m_PreviousMostVisiblePage = -1;
  49. private PageFittingType m_PreviousPageFitting;
  50. private Vector2 m_PreviousViewportSize = Vector2.zero;
  51. private float m_PreviousZoom;
  52. private float m_PreviousZoomToGo;
  53. private IList<PDFSearchResult>[] m_SearchResults;
  54. private float m_StartZoom;
  55. private float m_UpdateChangeDelay;
  56. private Vector2 m_ViewportSize = Vector2.zero;
  57. #if !UNITY_WEBGL || UNITY_EDITOR
  58. private WWW m_WWW;
  59. #endif
  60. private Vector2 m_ZoomPosition = Vector2.zero;
  61. private PDFRenderer m_Renderer;
  62. private Canvas m_Canvas;
  63. /// ...
  64. private PDFThumbnailsViewer m_ThumbnailsViewer;
  65. private PDFBookmarksViewer m_BookmarksViewer;
  66. [SerializeField] private bool m_AllowOpenURL = true;
  67. [SerializeField] private Component m_BytesSupplierComponent;
  68. [SerializeField] private string m_BytesSupplierFunctionName;
  69. [SerializeField] private GameObject m_BytesSupplierObject;
  70. [SerializeField] private string m_FileName = "";
  71. [SerializeField] private string m_FilePath = "";
  72. [SerializeField] private FileSourceType m_FileSource = FileSourceType.Resources;
  73. [SerializeField] private string m_FileURL = "";
  74. [SerializeField] private string m_Folder = "";
  75. [SerializeField] private bool m_LoadOnEnable = true;
  76. [SerializeField] private float m_MaxZoomFactor = 8.0f;
  77. [SerializeField] private float m_MaxZoomFactorTextureQuality = 4.0f;
  78. [SerializeField] private float m_MinZoomFactor = 0.25f;
  79. [SerializeField] private PageFittingType m_PageFitting = PageFittingType.Zoom;
  80. [SerializeField] private string m_Password = "";
  81. [SerializeField] private PDFAsset m_PDFAsset = null;
  82. [SerializeField] private float m_ZoomFactor = 1.0f;
  83. [SerializeField] private float m_ZoomStep = 0.25f;
  84. [SerializeField] private float m_ZoomToGo;
  85. [SerializeField] private float m_VerticalMarginBetweenPages = 20.0f;
  86. [SerializeField] private bool m_UnloadOnDisable;
  87. [SerializeField] private bool m_ShowVerticalScrollBar = false;
  88. [SerializeField] private bool m_ShowBookmarksViewer = true;
  89. [SerializeField] private bool m_ShowHorizontalScrollBar = true;
  90. [SerializeField] private bool m_ShowThumbnailsViewer = true;
  91. [SerializeField] private bool m_ShowTopBar = false;
  92. [SerializeField] private float m_ScrollSensitivity = 75.0f;
  93. [SerializeField] private Color m_SearchResultColor = new Color(0.0f, 115 / 255.0f, 230 / 255.0f, 125 / 255.0f);
  94. [SerializeField] private Vector2 m_SearchResultPadding = new Vector2(2.0f, 4.0f);
  95. [SerializeField] private float m_SearchTimeBudgetPerFrame = 0.60f;
  96. [SerializeField] private PDFRenderer.RenderSettings m_RenderSettings = new PDFRenderer.RenderSettings();
  97. [SerializeField] private float m_DelayAfterZoomingBeforeUpdate = 0.005f;
  98. [SerializeField] private float m_ParagraphZoomFactor = 2.0f;
  99. [SerializeField] private bool m_ParagraphZoomingEnable = true;
  100. [SerializeField] private float m_ParagraphDetectionThreshold = 12.0f;
  101. [SerializeField] private Texture2D m_PageTileTexture;
  102. [SerializeField] private Color m_PageColor = Color.white;
  103. public delegate void CancelEventHandler(PDFViewer sender);
  104. public delegate void CurrentPageChangedEventHandler(PDFViewer sender, int oldPageIndex, int newPageIndex);
  105. public delegate void DocumentChangedEventHandler(PDFViewer sender, PDFDocument document);
  106. public delegate void LoadFailEventHandler(PDFViewer sender);
  107. public delegate void PDFViewerEventHandler(PDFViewer sender);
  108. public delegate void ZoomChangedEventHandler(PDFViewer sender, float oldZoom, float newZoom);
  109. public event CurrentPageChangedEventHandler OnCurrentPageChanged;
  110. public event PDFViewerEventHandler OnDisabled;
  111. public event DocumentChangedEventHandler OnDocumentLoaded;
  112. public event LoadFailEventHandler OnDocumentLoadFailed;
  113. public event DocumentChangedEventHandler OnDocumentUnloaded;
  114. public event CancelEventHandler OnDownloadCancelled;
  115. public event CancelEventHandler OnPasswordCancelled;
  116. public event ZoomChangedEventHandler OnZoomChanged;
  117. public enum FileSourceType
  118. {
  119. None,
  120. Web,
  121. StreamingAssets,
  122. Resources,
  123. FilePath,
  124. Bytes,
  125. Asset,
  126. DocumentObject
  127. }
  128. public enum PageFittingType
  129. {
  130. ViewerWidth,
  131. ViewerHeight,
  132. WholePage,
  133. Zoom
  134. }
  135. public enum ViewerModeType
  136. {
  137. Move,
  138. ZoomOut,
  139. ZoomIn
  140. }
  141. /// <summary>
  142. /// Specify if the PDFViewer can open url link with external browser.
  143. /// </summary>
  144. public bool AllowOpenURL
  145. {
  146. get { return m_AllowOpenURL; }
  147. set { m_AllowOpenURL = value; }
  148. }
  149. /// <summary>
  150. /// Specify the viewport background color.
  151. /// </summary>
  152. public Color BackgroundColor
  153. {
  154. get { return m_Internal.m_Viewport.GetComponent<Image>().color; }
  155. set
  156. {
  157. if (m_Internal.m_Viewport.GetComponent<Image>().color != value)
  158. {
  159. m_Internal.m_Viewport.GetComponent<Image>().color = value;
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// Specify the relative amount of time is allowed for text search per frame (0.0f to 1.0f).
  165. /// </summary>
  166. public float SearchTimeBudgetPerFrame
  167. {
  168. get { return m_SearchTimeBudgetPerFrame; }
  169. set { m_SearchTimeBudgetPerFrame = Mathf.Clamp01(value); }
  170. }
  171. /// <summary>
  172. /// Specify the action handler for bookmarks.
  173. /// </summary>
  174. public IPDFDeviceActionHandler BookmarksActionHandler
  175. {
  176. get { return m_BookmarksActionHandler; }
  177. set { m_BookmarksActionHandler = value; }
  178. }
  179. /// <summary>
  180. /// Specify the action handler for links.
  181. /// </summary>
  182. public IPDFDeviceActionHandler LinksActionHandler
  183. {
  184. get { return m_LinksActionHandler; }
  185. set { m_LinksActionHandler = value; }
  186. }
  187. /// <summary>
  188. /// This property is intended to be used along side with the bytes file source. (FileSource.Bytes)
  189. /// Specify from which component the byte suplier function resides.
  190. /// </summary>
  191. public Component BytesSupplierComponent
  192. {
  193. get { return m_BytesSupplierComponent; }
  194. set { m_BytesSupplierComponent = value; }
  195. }
  196. /// <summary>
  197. /// This property is intended to be used along side with the bytes file source. (FileSource.Bytes)
  198. /// Specify the function name whithin the byte supplier component.
  199. /// </summary>
  200. public string BytesSupplierFunctionName
  201. {
  202. get { return m_BytesSupplierFunctionName; }
  203. set { m_BytesSupplierFunctionName = value; }
  204. }
  205. /// <summary>
  206. /// This property is intended to be used along side with the bytes file source. (FileSource.Bytes)
  207. /// Specify the object in which the bytes suppliers component resides.
  208. /// </summary>
  209. public GameObject BytesSupplierObject
  210. {
  211. get { return m_BytesSupplierObject; }
  212. set { m_BytesSupplierObject = value; }
  213. }
  214. public int CurrentPageIndex
  215. {
  216. get { return GetMostVisiblePageIndex(); }
  217. set
  218. {
  219. int mostVisible = GetMostVisiblePageIndex();
  220. if (value != mostVisible)
  221. {
  222. GoToPage(value);
  223. }
  224. }
  225. }
  226. public int CurrentSearchResultIndex
  227. {
  228. get { return m_CurrentSearchResultIndex; }
  229. }
  230. /// <summary>
  231. /// Return the byte array of the current loaded pdf document.
  232. /// </summary>
  233. public byte[] DataBuffer
  234. {
  235. get
  236. {
  237. if (Document != null)
  238. {
  239. return m_Document.DocumentBuffer;
  240. }
  241. return null;
  242. }
  243. }
  244. public PDFDocument Document
  245. {
  246. get { return m_Document; }
  247. }
  248. public string FileName
  249. {
  250. get { return m_FileName; }
  251. set { m_FileName = value; }
  252. }
  253. public string FilePath
  254. {
  255. get { return m_FilePath; }
  256. set { m_FilePath = value; }
  257. }
  258. public FileSourceType FileSource
  259. {
  260. get { return m_FileSource; }
  261. set { m_FileSource = value; }
  262. }
  263. public string FileURL
  264. {
  265. get { return m_FileURL; }
  266. set { m_FileURL = value; }
  267. }
  268. public string Folder
  269. {
  270. get { return m_Folder; }
  271. set { m_Folder = value; }
  272. }
  273. public bool IsLoaded
  274. {
  275. get { return m_IsLoaded; }
  276. }
  277. public bool LoadOnEnable
  278. {
  279. get { return m_LoadOnEnable; }
  280. set { m_LoadOnEnable = value; }
  281. }
  282. public float MaxZoomFactor
  283. {
  284. get { return m_MaxZoomFactor; }
  285. set { m_MaxZoomFactor = value; }
  286. }
  287. public float MaxZoomFactorTextureQuality
  288. {
  289. get { return m_MaxZoomFactorTextureQuality; }
  290. set
  291. {
  292. if (Math.Abs(Mathf.Clamp(value, MinZoomFactor, MaxZoomFactor) - m_MaxZoomFactorTextureQuality) >
  293. float.Epsilon)
  294. {
  295. m_MaxZoomFactorTextureQuality = Mathf.Clamp(value, MinZoomFactor, MaxZoomFactor);
  296. m_UpdateChangeDelay = 1.0f;
  297. }
  298. }
  299. }
  300. public float MinZoomFactor
  301. {
  302. get { return m_MinZoomFactor; }
  303. set { m_MinZoomFactor = value; }
  304. }
  305. public PageFittingType PageFitting
  306. {
  307. get { return m_PageFitting; }
  308. set { m_PageFitting = value; }
  309. }
  310. public string Password
  311. {
  312. get { return m_Password; }
  313. set { m_Password = value; }
  314. }
  315. /// <summary>
  316. /// Intended to be used along side the Asset file source (FileSource.Asset)
  317. /// </summary>
  318. public PDFAsset PDFAsset
  319. {
  320. get { return m_PDFAsset; }
  321. set { m_PDFAsset = value; }
  322. }
  323. public bool RenderAnnotations
  324. {
  325. get { return m_RenderSettings.renderAnnotations; }
  326. set
  327. {
  328. if (m_RenderSettings.renderAnnotations != value)
  329. {
  330. m_RenderSettings.renderAnnotations = value;
  331. m_UpdateChangeDelay = 0.1f;
  332. }
  333. }
  334. }
  335. public bool RenderGrayscale
  336. {
  337. get { return m_RenderSettings.grayscale; }
  338. set
  339. {
  340. if (m_RenderSettings.grayscale != value)
  341. {
  342. m_RenderSettings.grayscale = value;
  343. m_UpdateChangeDelay = 0.1f;
  344. }
  345. }
  346. }
  347. public float ScrollSensitivity
  348. {
  349. get { return m_ScrollSensitivity; }
  350. set { m_ScrollSensitivity = value; }
  351. }
  352. public Color SearchResultColor
  353. {
  354. get { return m_SearchResultColor; }
  355. set
  356. {
  357. if (m_SearchResultColor != value)
  358. {
  359. m_SearchResultColor = value;
  360. m_UpdateChangeDelay = 0.25f;
  361. }
  362. }
  363. }
  364. public Vector2 SearchResultPadding
  365. {
  366. get { return m_SearchResultPadding; }
  367. set
  368. {
  369. if (m_SearchResultPadding != value)
  370. {
  371. m_SearchResultPadding = value;
  372. m_UpdateChangeDelay = 0.25f;
  373. }
  374. }
  375. }
  376. public bool ShowBookmarksViewer
  377. {
  378. get { return m_ShowBookmarksViewer; }
  379. set
  380. {
  381. m_ShowBookmarksViewer = value;
  382. bool visible = m_ShowBookmarksViewer;
  383. if (m_IsLoaded)
  384. {
  385. #if !UNITY_WEBGL
  386. if (m_Document.GetRootBookmark().ChildCount == 0)
  387. #endif
  388. visible = false;
  389. }
  390. if (m_Internal.m_LeftPanel != null)
  391. {
  392. m_Internal.m_LeftPanel.m_Bookmarks.gameObject.SetActive(visible);
  393. m_Internal.m_LeftPanel.m_BookmarksTab.gameObject.SetActive(visible);
  394. m_Internal.m_LeftPanel.SetActive(m_ShowThumbnailsViewer || visible);
  395. if (!visible && m_ShowThumbnailsViewer)
  396. m_Internal.m_LeftPanel.OnThumbnailsTabClicked();
  397. else if (visible && !m_ShowThumbnailsViewer)
  398. m_Internal.m_LeftPanel.OnBookmarksTabClicked();
  399. else
  400. m_Internal.m_LeftPanel.OnBookmarksTabClicked();
  401. }
  402. }
  403. }
  404. public bool ShowHorizontalScrollBar
  405. {
  406. get { return m_ShowHorizontalScrollBar; }
  407. set
  408. {
  409. if (m_ShowHorizontalScrollBar != value)
  410. {
  411. m_ShowHorizontalScrollBar = value;
  412. UpdateScrollBarVisibility();
  413. }
  414. }
  415. }
  416. public bool ShowThumbnailsViewer
  417. {
  418. get { return m_ShowThumbnailsViewer; }
  419. set
  420. {
  421. if (m_ShowThumbnailsViewer != value)
  422. {
  423. m_ShowThumbnailsViewer = value;
  424. if (m_Internal.m_LeftPanel != null)
  425. {
  426. m_Internal.m_LeftPanel.m_ThumbnailsViewer.gameObject.SetActive(m_ShowThumbnailsViewer);
  427. m_Internal.m_LeftPanel.m_ThumbnailsTab.gameObject.SetActive(m_ShowThumbnailsViewer);
  428. m_Internal.m_LeftPanel.SetActive(m_ShowThumbnailsViewer || m_ShowBookmarksViewer);
  429. if (!m_ShowBookmarksViewer && m_ShowThumbnailsViewer)
  430. m_Internal.m_LeftPanel.OnThumbnailsTabClicked();
  431. else if (m_ShowBookmarksViewer && !m_ShowThumbnailsViewer)
  432. m_Internal.m_LeftPanel.OnBookmarksTabClicked();
  433. else
  434. m_Internal.m_LeftPanel.OnBookmarksTabClicked();
  435. }
  436. }
  437. }
  438. }
  439. public bool ShowTopBar
  440. {
  441. get { return m_ShowTopBar; }
  442. set
  443. {
  444. if (m_ShowTopBar != value)
  445. {
  446. m_ShowTopBar = value;
  447. if (!m_ShowTopBar)
  448. {
  449. m_Internal.m_TopPanel.gameObject.SetActive(false);
  450. m_Internal.m_TopPanel.sizeDelta = new Vector2(0.0f, 0.0f);
  451. m_Internal.m_Viewport.offsetMax = new Vector2(m_Internal.m_Viewport.offsetMax.x, 0.0f);
  452. m_Internal.m_VerticalScrollBar.offsetMax =
  453. new Vector2(m_Internal.m_VerticalScrollBar.offsetMax.x, 0.0f);
  454. if (m_Internal.m_LeftPanel != null)
  455. {
  456. (m_Internal.m_LeftPanel.transform as RectTransform).sizeDelta =
  457. new Vector2((m_Internal.m_LeftPanel.transform as RectTransform).sizeDelta.x, 0.0f);
  458. }
  459. }
  460. else
  461. {
  462. m_Internal.m_TopPanel.gameObject.SetActive(true);
  463. m_Internal.m_TopPanel.sizeDelta = new Vector2(0.0f, 60.0f);
  464. m_Internal.m_Viewport.offsetMax = new Vector2(m_Internal.m_Viewport.offsetMax.x, -60.0f);
  465. m_Internal.m_VerticalScrollBar.offsetMax =
  466. new Vector2(m_Internal.m_VerticalScrollBar.offsetMax.x, -59.0f);
  467. if (m_Internal.m_LeftPanel != null)
  468. {
  469. (m_Internal.m_LeftPanel.transform as RectTransform).sizeDelta =
  470. new Vector2((m_Internal.m_LeftPanel.transform as RectTransform).sizeDelta.x, -59.0f);
  471. }
  472. }
  473. }
  474. }
  475. }
  476. public bool ShowVerticalScrollBar
  477. {
  478. get { return m_ShowVerticalScrollBar; }
  479. set
  480. {
  481. if (m_ShowVerticalScrollBar != value)
  482. {
  483. m_ShowVerticalScrollBar = value;
  484. UpdateScrollBarVisibility();
  485. }
  486. }
  487. }
  488. public bool UnloadOnDisable
  489. {
  490. get { return m_UnloadOnDisable; }
  491. set { m_UnloadOnDisable = value; }
  492. }
  493. public float VerticalMarginBetweenPages
  494. {
  495. get { return m_VerticalMarginBetweenPages; }
  496. set
  497. {
  498. if (m_VerticalMarginBetweenPages != value)
  499. {
  500. if (value < 0.0f)
  501. {
  502. m_VerticalMarginBetweenPages = 0.0f;
  503. }
  504. else
  505. {
  506. m_VerticalMarginBetweenPages = value;
  507. }
  508. ComputePageOffsets();
  509. UpdatePagesPlacement();
  510. m_Internal.m_PageContainer.sizeDelta = GetDocumentSize();
  511. EnsureValidPageContainerPosition();
  512. }
  513. }
  514. }
  515. public float ZoomFactor
  516. {
  517. get { return m_ZoomToGo; }
  518. set
  519. {
  520. if (Math.Abs(m_ZoomToGo - Mathf.Clamp(value, MinZoomFactor, MaxZoomFactor)) > float.Epsilon)
  521. {
  522. m_ZoomToGo = Mathf.Clamp(value, MinZoomFactor, MaxZoomFactor);
  523. m_ZoomPosition = new Vector2(0.0f, m_ViewportSize.y*0.5f);
  524. NotifyZoomChanged(m_PreviousZoomToGo, m_ZoomToGo);
  525. m_PageFitting = PageFittingType.Zoom;
  526. }
  527. }
  528. }
  529. public float ZoomStep
  530. {
  531. get { return m_ZoomStep; }
  532. set { m_ZoomStep = value; }
  533. }
  534. public bool ParagraphZoomingEnable
  535. {
  536. get
  537. {
  538. return m_ParagraphZoomingEnable;
  539. }
  540. set
  541. {
  542. m_ParagraphZoomingEnable = value;
  543. }
  544. }
  545. public float ParagraphZoomFactor
  546. {
  547. get { return m_ParagraphZoomFactor; }
  548. set { m_ParagraphZoomFactor = value; }
  549. }
  550. public float ParagraphDetectionThreshold
  551. {
  552. get { return m_ParagraphDetectionThreshold; }
  553. set { m_ParagraphDetectionThreshold = value; }
  554. }
  555. public Texture2D PageTileTexture
  556. {
  557. get
  558. {
  559. return m_PageTileTexture;
  560. }
  561. set
  562. {
  563. m_PageTileTexture = value;
  564. }
  565. }
  566. public Color PageColor
  567. {
  568. get
  569. {
  570. return m_PageColor;
  571. }
  572. set
  573. {
  574. m_PageColor = value;
  575. }
  576. }
  577. public void AdjustZoomToPageFitting(PageFittingType pageFitting, Vector2 referencePageSize)
  578. {
  579. if (m_ViewportSize == Vector2.zero)
  580. {
  581. m_ViewportSize = ComputeRectTransformFinalSize(m_Internal.m_Viewport.transform as RectTransform);
  582. if (m_Canvas != null)
  583. {
  584. m_ViewportSize.x /= m_Canvas.scaleFactor;
  585. m_ViewportSize.y /= m_Canvas.scaleFactor;
  586. }
  587. }
  588. switch (pageFitting)
  589. {
  590. case PageFittingType.ViewerWidth:
  591. {
  592. float firstPageWidth = referencePageSize.x;
  593. float viewportWidth = m_ViewportSize.x;
  594. m_ZoomToGo = viewportWidth/firstPageWidth;
  595. break;
  596. }
  597. case PageFittingType.ViewerHeight:
  598. {
  599. float firstPageHeight = referencePageSize.y;
  600. float viewportHeight = m_ViewportSize.y;
  601. m_ZoomToGo = viewportHeight/firstPageHeight;
  602. break;
  603. }
  604. case PageFittingType.WholePage:
  605. {
  606. float firstPageWidth = referencePageSize.x;
  607. float firstPageHeight = referencePageSize.y + 2.0f*m_VerticalMarginBetweenPages;
  608. float viewportWidth = m_ViewportSize.x;
  609. float viewportHeight = m_ViewportSize.y;
  610. m_ZoomToGo = Mathf.Min(viewportWidth/firstPageWidth, viewportHeight/firstPageHeight);
  611. break;
  612. }
  613. case PageFittingType.Zoom:
  614. {
  615. break;
  616. }
  617. }
  618. }
  619. public void CloseDocument()
  620. {
  621. #if !UNITY_WEBPLAYER
  622. if (m_IsLoaded)
  623. {
  624. CleanUp();
  625. }
  626. #endif
  627. }
  628. public string GetFileLocation()
  629. {
  630. switch (m_FileSource)
  631. {
  632. case FileSourceType.FilePath:
  633. return m_FilePath;
  634. case FileSourceType.Resources:
  635. string folder = m_Folder + "/";
  636. if (string.IsNullOrEmpty(m_Folder))
  637. {
  638. folder = "";
  639. }
  640. return (folder + m_FileName).Replace("//", "/").Replace(@"\\", @"/").Replace(@"\", @"/");
  641. case FileSourceType.StreamingAssets:
  642. folder = m_Folder + "/";
  643. if (string.IsNullOrEmpty(m_Folder))
  644. {
  645. folder = "";
  646. }
  647. string location = ("/" + folder + m_FileName).Replace("//", "/")
  648. .Replace(@"\\", @"/")
  649. .Replace(@"\", @"/");
  650. return Application.streamingAssetsPath + location;
  651. case FileSourceType.Web:
  652. return m_FileURL;
  653. default:
  654. return "";
  655. }
  656. }
  657. public void GoToNextPage()
  658. {
  659. if (m_Document == null || !m_Document.IsValid)
  660. {
  661. return;
  662. }
  663. int mostVisiblePage = GetMostVisiblePageIndex();
  664. if (mostVisiblePage + 1 < m_PageCount)
  665. {
  666. GoToPage(mostVisiblePage + 1);
  667. }
  668. else
  669. {
  670. m_Internal.m_PageContainer.anchoredPosition = new Vector2(
  671. m_Internal.m_PageContainer.anchoredPosition.x,
  672. m_Internal.m_PageContainer.sizeDelta.y - m_ViewportSize.y);
  673. }
  674. }
  675. public void GoToNextSearchResult()
  676. {
  677. if (m_SearchResults != null && m_SearchResults.Length > 0)
  678. {
  679. ++m_CurrentSearchResultIndex;
  680. ++m_CurrentSearchResultIndexWithinCurrentPage;
  681. int oldPageIndex = m_CurrentSearchResult.PageIndex;
  682. if (m_CurrentSearchResultIndexWithinCurrentPage >= m_SearchResults[m_CurrentSearchResult.PageIndex].Count)
  683. {
  684. int nextPage = m_CurrentSearchResult.PageIndex + 1;
  685. while (nextPage < m_PageCount - 1 && m_SearchResults[nextPage].Count == 0)
  686. {
  687. ++nextPage;
  688. }
  689. if (nextPage <= m_PageCount - 1 && m_SearchResults[nextPage].Count > 0)
  690. {
  691. m_CurrentSearchResultIndexWithinCurrentPage = 0;
  692. m_CurrentSearchResult = m_SearchResults[nextPage][0];
  693. if (oldPageIndex != nextPage)
  694. {
  695. GoToPage(nextPage);
  696. }
  697. }
  698. else
  699. {
  700. --m_CurrentSearchResultIndexWithinCurrentPage;
  701. --m_CurrentSearchResultIndex;
  702. }
  703. }
  704. else
  705. {
  706. m_CurrentSearchResult =
  707. m_SearchResults[m_CurrentSearchResult.PageIndex][m_CurrentSearchResultIndexWithinCurrentPage];
  708. if (!m_CurrentPageRange.ContainsPage(m_CurrentSearchResult.PageIndex))
  709. GoToPage(m_CurrentSearchResult.PageIndex);
  710. }
  711. }
  712. }
  713. public void GoToPage(int pageIndex)
  714. {
  715. if (pageIndex < 0)
  716. {
  717. pageIndex = 0;
  718. }
  719. else if (pageIndex > m_PageCount - 1)
  720. {
  721. pageIndex = m_PageCount - 1;
  722. }
  723. m_Internal.m_PageInputField.text = (pageIndex + 1).ToString();
  724. m_Internal.m_PageContainer.anchoredPosition = new Vector2(m_Internal.m_PageContainer.anchoredPosition.x,
  725. m_PageOffsets[pageIndex] - m_PageSizes[pageIndex].y*0.5f);
  726. m_Internal.m_PageContainer.anchoredPosition -= m_VerticalMarginBetweenPages*Vector2.up;
  727. SetPageCountLabel(pageIndex, m_PageCount);
  728. EnsureValidPageContainerPosition();
  729. }
  730. public void GoToPreviousPage()
  731. {
  732. if (m_Document == null || !m_Document.IsValid)
  733. {
  734. return;
  735. }
  736. int mostVisiblePage = GetMostVisiblePageIndex();
  737. if (mostVisiblePage - 1 >= 0)
  738. {
  739. GoToPage(mostVisiblePage - 1);
  740. }
  741. else
  742. {
  743. m_Internal.m_PageContainer.anchoredPosition = Vector2.zero;
  744. }
  745. }
  746. public void GoToPreviousSearchResult()
  747. {
  748. if (m_SearchResults != null && m_SearchResults.Length > 0 && m_CurrentSearchResultIndex > 0)
  749. {
  750. --m_CurrentSearchResultIndex;
  751. --m_CurrentSearchResultIndexWithinCurrentPage;
  752. int oldPageIndex = m_CurrentSearchResult.PageIndex;
  753. if (m_CurrentSearchResultIndexWithinCurrentPage < 0)
  754. {
  755. int prevPage = m_CurrentSearchResult.PageIndex - 1;
  756. while (prevPage >= 0 && m_SearchResults[prevPage].Count == 0)
  757. {
  758. --prevPage;
  759. }
  760. if (prevPage >= 0 && m_SearchResults[prevPage].Count > 0)
  761. {
  762. m_CurrentSearchResultIndexWithinCurrentPage = m_SearchResults[prevPage].Count - 1;
  763. m_CurrentSearchResult = m_SearchResults[prevPage][m_SearchResults[prevPage].Count - 1];
  764. if (oldPageIndex != prevPage)
  765. {
  766. GoToPage(prevPage);
  767. }
  768. }
  769. else
  770. {
  771. ++m_CurrentSearchResultIndexWithinCurrentPage;
  772. ++m_CurrentSearchResultIndex;
  773. }
  774. }
  775. else
  776. {
  777. m_CurrentSearchResult =
  778. m_SearchResults[m_CurrentSearchResult.PageIndex][m_CurrentSearchResultIndexWithinCurrentPage];
  779. if (!m_CurrentPageRange.ContainsPage(m_CurrentSearchResult.PageIndex))
  780. GoToPage(m_CurrentSearchResult.PageIndex);
  781. }
  782. }
  783. }
  784. public void LoadDocument(int pageIndex = 0)
  785. {
  786. #if !UNITY_WEBPLAYER
  787. if (m_IsLoaded)
  788. CleanUp();
  789. CommonLoad();
  790. #endif
  791. }
  792. public void LoadDocument(PDFDocument document, string password, int pageIndex)
  793. {
  794. #if !UNITY_WEBPLAYER
  795. m_LoadAtPageIndex = pageIndex;
  796. if (m_IsLoaded)
  797. CleanUp();
  798. m_FileSource = FileSourceType.DocumentObject;
  799. m_SuppliedDocument = document;
  800. m_Password = password;
  801. //if (m_SuppliedDocument != null)
  802. CommonLoad();
  803. #endif
  804. }
  805. public void LoadDocument(PDFDocument document, int pageIndex = 0)
  806. {
  807. LoadDocument(document, "", pageIndex);
  808. }
  809. public void LoadDocumentFromAsset(PDFAsset pdfAsset, int pageIndex = 0)
  810. {
  811. #if !UNITY_WEBPLAYER
  812. m_LoadAtPageIndex = pageIndex;
  813. if (m_IsLoaded)
  814. {
  815. CleanUp();
  816. }
  817. m_FileSource = FileSourceType.Asset;
  818. m_PDFAsset = pdfAsset;
  819. m_Password = pdfAsset.Password;
  820. CommonLoad(pdfAsset.m_FileContent);
  821. #endif
  822. }
  823. public void LoadDocumentFromAsset(PDFAsset pdfAsset, string password, int pageIndex = 0)
  824. {
  825. #if !UNITY_WEBPLAYER
  826. m_LoadAtPageIndex = pageIndex;
  827. if (m_IsLoaded)
  828. {
  829. CleanUp();
  830. }
  831. m_FileSource = FileSourceType.Asset;
  832. m_PDFAsset = pdfAsset;
  833. m_Password = password;
  834. CommonLoad(pdfAsset.m_FileContent);
  835. #endif
  836. }
  837. public void LoadDocumentFromResources(string folder, string fileName, string password, int pageIndex = 0)
  838. {
  839. #if !UNITY_WEBPLAYER
  840. m_LoadAtPageIndex = pageIndex;
  841. if (m_IsLoaded)
  842. CleanUp();
  843. m_FileSource = FileSourceType.Resources;
  844. m_Folder = folder;
  845. m_FileName = fileName;
  846. m_FilePath = GetFileLocation();
  847. m_Password = password;
  848. CommonLoad();
  849. #endif
  850. }
  851. public void LoadDocumentFromStreamingAssets(string folder, string fileName, string password, int pageIndex = 0)
  852. {
  853. #if !UNITY_WEBPLAYER
  854. m_LoadAtPageIndex = pageIndex;
  855. if (m_IsLoaded)
  856. CleanUp();
  857. m_FileSource = FileSourceType.StreamingAssets;
  858. m_Folder = folder;
  859. m_FileName = fileName;
  860. m_FilePath = GetFileLocation();
  861. m_Password = password;
  862. CommonLoad();
  863. #endif
  864. }
  865. public void LoadDocumentFromWeb(string url, string password, int pageIndex = 0)
  866. {
  867. #if !UNITY_WEBPLAYER
  868. m_LoadAtPageIndex = pageIndex;
  869. if (m_IsLoaded)
  870. CleanUp();
  871. m_FileSource = FileSourceType.Web;
  872. m_FileURL = url;
  873. m_FilePath = GetFileLocation();
  874. m_Password = password;
  875. CommonLoad();
  876. #endif
  877. }
  878. public void LoadDocumentWithBuffer(byte[] buffer, string password, int pageIndex = 0)
  879. {
  880. #if !UNITY_WEBPLAYER
  881. m_LoadAtPageIndex = pageIndex;
  882. if (m_IsLoaded)
  883. CleanUp();
  884. m_FileSource = FileSourceType.Bytes;
  885. m_Password = password;
  886. CommonLoad(buffer);
  887. #endif
  888. }
  889. public void LoadDocumentWithFile(string filePath, string password, int pageIndex = 0)
  890. {
  891. #if !UNITY_WEBPLAYER
  892. m_LoadAtPageIndex = pageIndex;
  893. if (m_IsLoaded)
  894. CleanUp();
  895. m_FileSource = FileSourceType.FilePath;
  896. m_FilePath = filePath;
  897. m_Password = password;
  898. m_FilePath = GetFileLocation();
  899. m_Password = password;
  900. CommonLoad();
  901. #endif
  902. }
  903. public void OnDownloadCancelButtonClicked()
  904. {
  905. #if !UNITY_WEBGL || UNITY_EDITOR
  906. StopCoroutine(DownloadFileFromWWW());
  907. m_Internal.m_DownloadDialog.gameObject.SetActive(false);
  908. m_WWW = null;
  909. NotifyDownloadCancelled();
  910. CleanUp();
  911. #endif
  912. }
  913. public void OnPageEditEnd()
  914. {
  915. if (m_Document == null || !m_Document.IsValid)
  916. {
  917. return;
  918. }
  919. if (string.IsNullOrEmpty(m_Internal.m_PageInputField.text))
  920. {
  921. return;
  922. }
  923. int pageIndex = int.Parse(m_Internal.m_PageInputField.text) - 1;
  924. GoToPage(pageIndex);
  925. }
  926. public void OnPasswordDialogCancelButtonClicked()
  927. {
  928. m_InvalidPasswordMessageVisisble = false;
  929. m_Internal.m_InvalidPasswordImage.gameObject.SetActive(false);
  930. m_Internal.m_InvalidPasswordImage.GetComponent<CanvasGroup>().alpha = 1.0f;
  931. NotifyPasswordCancelled();
  932. CleanUp();
  933. }
  934. public void OnPasswordDialogOkButtonClicked()
  935. {
  936. #if !UNITY_WEBGL || UNITY_EDITOR
  937. m_Password = m_Internal.m_PasswordInputField.text;
  938. if (TryLoadDocumentWithBuffer(m_PendingDocumentBuffer, m_Password))
  939. {
  940. m_Internal.m_PasswordDialog.gameObject.SetActive(false);
  941. m_InvalidPasswordMessageVisisble = false;
  942. m_Internal.m_InvalidPasswordImage.gameObject.SetActive(false);
  943. m_Internal.m_InvalidPasswordImage.GetComponent<CanvasGroup>().alpha = 1.0f;
  944. m_Internal.m_PasswordInputField.text = "";
  945. }
  946. else
  947. {
  948. m_InvalidPasswordMessageVisisble = true;
  949. m_Internal.m_InvalidPasswordImage.gameObject.SetActive(true);
  950. m_Internal.m_InvalidPasswordImage.GetComponent<CanvasGroup>().alpha = 1.0f;
  951. m_InvalidPasswordMessageDelay = m_InvalidPasswordMessageDelayBeforeFade;
  952. m_Internal.m_PasswordInputField.Select();
  953. }
  954. #endif
  955. }
  956. public void ReloadDocument(int pageIndex = 0)
  957. {
  958. #if !UNITY_WEBPLAYER
  959. LoadDocument(pageIndex);
  960. #endif
  961. }
  962. #if (!UNITY_WINRT && !UNITY_WEBGL) || UNITY_EDITOR
  963. public bool SaveDocumentAsFile(string path)
  964. {
  965. if (m_Document == null || m_Document.DocumentBuffer == null)
  966. {
  967. Debug.LogError("Error while saving document: there is no document loaded.");
  968. return false;
  969. }
  970. if (!new Uri(path).IsWellFormedOriginalString())
  971. {
  972. Debug.LogError("Error while saving document: the path is not well formed => " + path);
  973. return false;
  974. }
  975. try
  976. {
  977. FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
  978. stream.Write(m_Document.DocumentBuffer, 0, m_Document.DocumentBuffer.Length);
  979. stream.Close();
  980. return true;
  981. }
  982. catch (Exception ex)
  983. {
  984. Debug.LogError("Exception while saving document: " + ex);
  985. }
  986. return false;
  987. }
  988. #endif
  989. public void SetSearchResults(IList<PDFSearchResult>[] searchResults)
  990. {
  991. m_SearchResults = searchResults;
  992. if (m_SearchResults != null && m_SearchResults.Length > 0)
  993. {
  994. m_CurrentSearchResultIndex = 0;
  995. m_CurrentSearchResultIndexWithinCurrentPage = 0;
  996. for (int i = 0; i < m_PageCount; ++i)
  997. {
  998. if (m_SearchResults[i] != null && m_SearchResults[i].Count > 0)
  999. {
  1000. m_CurrentSearchResult = m_SearchResults[i][0];
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. else
  1006. {
  1007. m_CurrentSearchResult = new PDFSearchResult(-1, 0, 0);
  1008. m_CurrentSearchResultIndex = 0;
  1009. }
  1010. AdjustCurrentSearchResultDisplayed();
  1011. m_UpdateChangeDelay = 0.25f;
  1012. }
  1013. public void UnloadDocument()
  1014. {
  1015. #if !UNITY_WEBPLAYER
  1016. if (m_IsLoaded)
  1017. {
  1018. CleanUp();
  1019. }
  1020. #endif
  1021. }
  1022. public void ZoomIn()
  1023. {
  1024. ZoomCommon(new Vector2(0.0f, m_ViewportSize.y*0.5f), true);
  1025. }
  1026. public void ZoomOut()
  1027. {
  1028. ZoomCommon(new Vector2(0.0f, m_ViewportSize.y*0.5f), false);
  1029. }
  1030. private void AdjustCurrentSearchResultDisplayed()
  1031. {
  1032. if (m_SearchResults != null && m_SearchResults.Length > 0)
  1033. {
  1034. if (!m_CurrentPageRange.ContainsPage(m_CurrentSearchResult.PageIndex))
  1035. {
  1036. int minPage = m_CurrentPageRange.m_From;
  1037. int maxPage = m_CurrentPageRange.m_To;
  1038. bool minFound = false;
  1039. bool maxFound = false;
  1040. for (int i = minPage; i >= 0; --i)
  1041. {
  1042. if (m_SearchResults[i] != null && m_SearchResults[i].Count > 0)
  1043. {
  1044. minFound = true;
  1045. minPage = i;
  1046. break;
  1047. }
  1048. }
  1049. for (int i = maxPage; i < m_PageCount; ++i)
  1050. {
  1051. if (m_SearchResults[i] != null && m_SearchResults[i].Count > 0)
  1052. {
  1053. maxFound = true;
  1054. maxPage = i;
  1055. break;
  1056. }
  1057. }
  1058. int disMinPage = Math.Abs(m_CurrentPageRange.m_From - minPage);
  1059. int disMaxPage = Math.Abs(maxPage - m_CurrentPageRange.m_To);
  1060. int nearestPage = -1;
  1061. if (disMinPage <= disMaxPage)
  1062. {
  1063. if (minFound)
  1064. {
  1065. nearestPage = minPage;
  1066. }
  1067. else if (maxFound)
  1068. {
  1069. nearestPage = maxPage;
  1070. }
  1071. }
  1072. else
  1073. {
  1074. if (maxFound)
  1075. {
  1076. nearestPage = maxPage;
  1077. }
  1078. else if (minFound)
  1079. {
  1080. nearestPage = minPage;
  1081. }
  1082. }
  1083. int count = 0;
  1084. for (int i = 0; i < nearestPage; ++i)
  1085. {
  1086. count += m_SearchResults[i].Count;
  1087. }
  1088. if (minFound || maxFound)
  1089. {
  1090. if (m_CurrentPageRange.ContainsPage(nearestPage)
  1091. || nearestPage >= m_CurrentPageRange.m_To)
  1092. {
  1093. m_CurrentSearchResult = m_SearchResults[nearestPage][0];
  1094. m_CurrentSearchResultIndex = count;
  1095. m_CurrentSearchResultIndexWithinCurrentPage = 0;
  1096. }
  1097. else
  1098. {
  1099. m_CurrentSearchResult = m_SearchResults[nearestPage][m_SearchResults[nearestPage].Count - 1];
  1100. m_CurrentSearchResultIndex = count + m_SearchResults[nearestPage].Count - 1;
  1101. m_CurrentSearchResultIndexWithinCurrentPage = m_SearchResults[nearestPage].Count - 1;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. private void CleanUp()
  1108. {
  1109. if (m_Document != null)
  1110. NotifyDocumentUnloaded(m_Document);
  1111. m_Document = null;
  1112. if (m_PageTextureHolders != null)
  1113. {
  1114. foreach (PDFPageTextureHolder holder in m_PageTextureHolders)
  1115. {
  1116. if (holder.Texture != null)
  1117. {
  1118. Texture2D tex = holder.Texture;
  1119. holder.Texture = null;
  1120. Texture2D.Destroy(tex);
  1121. Resources.UnloadUnusedAssets();
  1122. }
  1123. if (holder.m_Page.name != "Page")
  1124. {
  1125. Destroy(holder.m_Page);
  1126. }
  1127. }
  1128. }
  1129. m_IsLoaded = false;
  1130. m_Internal.m_PageContainer.anchoredPosition = Vector2.zero;
  1131. m_Internal.m_PageContainer.sizeDelta = Vector2.zero;
  1132. UpdateScrollBarVisibility();
  1133. EnsureValidPageContainerPosition();
  1134. m_ZoomToGo = m_StartZoom;
  1135. m_PageSizes = null;
  1136. m_NormalPageSizes = null;
  1137. m_PageOffsets = null;
  1138. m_PageCount = 0;
  1139. m_PreviousZoom = 0.0f;
  1140. m_PreviousZoomToGo = 0.0f;
  1141. m_PreviousViewportSize = Vector2.zero;
  1142. m_PageTextureHolders = null;
  1143. m_CurrentPageRange = null;
  1144. m_PreviousMostVisiblePage = -1;
  1145. m_OverlayVisible = false;
  1146. m_InvalidPasswordMessageVisisble = false;
  1147. m_Internal.m_Overlay.gameObject.SetActive(false);
  1148. m_Internal.m_PasswordDialog.gameObject.SetActive(false);
  1149. m_Internal.m_DownloadDialog.gameObject.SetActive(false);
  1150. m_Internal.m_PageCountLabel.text = "";
  1151. m_Internal.m_PageZoomLabel.text = "";
  1152. m_Internal.m_PageInputField.text = "";
  1153. GC.Collect();
  1154. GC.WaitForPendingFinalizers();
  1155. }
  1156. private void CommonLoad(byte[] specifiedBuffer = null)
  1157. {
  1158. #if !UNITY_WEBPLAYER
  1159. UpdateScrollBarVisibility();
  1160. EnsureValidPageContainerPosition();
  1161. m_IsLoaded = false;
  1162. if (m_FileSource == FileSourceType.None)
  1163. {
  1164. m_OverlayVisible = true;
  1165. m_Internal.m_Overlay.gameObject.SetActive(true);
  1166. m_Internal.m_Overlay.GetComponent<CanvasGroup>().alpha = 1.0f;
  1167. return;
  1168. }
  1169. #if UNITY_WEBGL && !UNITY_EDITOR
  1170. StartCoroutine(LoadDocument_WebGL(specifiedBuffer));
  1171. return;
  1172. #else
  1173. byte[] buffer = specifiedBuffer;
  1174. if (m_FileSource != FileSourceType.DocumentObject)
  1175. m_SuppliedDocument = null;
  1176. if (m_FileSource == FileSourceType.DocumentObject)
  1177. {
  1178. TryLoadWithSpecifiedDocument(m_SuppliedDocument);
  1179. }
  1180. else if (m_FileSource == FileSourceType.FilePath)
  1181. {
  1182. #if !((UNITY_4_6 || UNITY_4_7) && UNITY_WINRT)
  1183. buffer = File.ReadAllBytes(GetFileLocation());
  1184. OnLoadingBufferFinished(buffer);
  1185. #endif
  1186. }
  1187. else if (m_FileSource == FileSourceType.Resources)
  1188. {
  1189. buffer = LoadAssetBytesFromResources(GetFileLocation());
  1190. OnLoadingBufferFinished(buffer);
  1191. }
  1192. else if (m_FileSource == FileSourceType.StreamingAssets)
  1193. {
  1194. #if (UNITY_ANDROID || UNITY_WINRT) && !UNITY_EDITOR
  1195. StartCoroutine(DownloadFileFromWWW());
  1196. #else
  1197. string location = GetFileLocation();
  1198. if (File.Exists(location))
  1199. buffer = File.ReadAllBytes(location);
  1200. OnLoadingBufferFinished(buffer);
  1201. #endif
  1202. }
  1203. else if (m_FileSource == FileSourceType.Web)
  1204. {
  1205. StartCoroutine(DownloadFileFromWWW());
  1206. }
  1207. else if (m_FileSource == FileSourceType.Bytes)
  1208. {
  1209. #if !((UNITY_4_6 || UNITY_4_7) && UNITY_WINRT)
  1210. if (buffer != null)
  1211. {
  1212. OnLoadingBufferFinished(buffer);
  1213. }
  1214. else if (BytesSupplierComponent != null)
  1215. {
  1216. #if UNITY_WINRT
  1217. MethodInfo methodInfo = BytesSupplierComponent.GetType().GetMethod(BytesSupplierFunctionName, BindingFlags.Public);
  1218. #else
  1219. MethodInfo methodInfo = BytesSupplierComponent.GetType().GetMethod(BytesSupplierFunctionName);
  1220. #endif
  1221. if (methodInfo != null)
  1222. {
  1223. buffer = (byte[]) methodInfo.Invoke(BytesSupplierComponent, null);
  1224. }
  1225. if (buffer != null)
  1226. {
  1227. OnLoadingBufferFinished(buffer);
  1228. }
  1229. }
  1230. #endif
  1231. if (buffer == null)
  1232. {
  1233. NotifyDocumentLoadFailed();
  1234. }
  1235. }
  1236. else if (m_FileSource == FileSourceType.Asset)
  1237. {
  1238. if (m_PDFAsset != null && m_PDFAsset.m_FileContent != null && m_PDFAsset.m_FileContent.Length > 0)
  1239. {
  1240. OnLoadingBufferFinished(m_PDFAsset.m_FileContent);
  1241. }
  1242. else
  1243. {
  1244. NotifyDocumentLoadFailed();
  1245. }
  1246. }
  1247. #endif
  1248. #endif
  1249. }
  1250. private void ComputePageOffsets()
  1251. {
  1252. float totalOffset = m_VerticalMarginBetweenPages;
  1253. m_PageOffsets = new float[m_PageCount];
  1254. for (int i = 0; i < m_PageCount; ++i)
  1255. {
  1256. m_PageOffsets[i] = totalOffset + m_PageSizes[i].y*0.5f;
  1257. totalOffset += m_VerticalMarginBetweenPages + m_PageSizes[i].y;
  1258. }
  1259. }
  1260. private void ComputePageSizes()
  1261. {
  1262. m_PageCount = m_Document.GetPageCount();
  1263. m_PageSizes = new Vector2[m_PageCount];
  1264. for (int i = 0; i < m_PageCount; ++i)
  1265. {
  1266. #if UNITY_WEBGL && !UNITY_EDITOR
  1267. m_PageSizes[i] = m_NormalPageSizes[i] * m_ZoomFactor;
  1268. #else
  1269. float w = (m_Document.GetPageWidth(i) * m_ZoomFactor);
  1270. float h = (m_Document.GetPageHeight(i) * m_ZoomFactor);
  1271. m_PageSizes[i] = new Vector2(w, h);
  1272. #endif
  1273. }
  1274. }
  1275. private Vector2 ComputeRectTransformFinalSize(RectTransform rectTransform)
  1276. {
  1277. if (rectTransform == null || rectTransform.parent == null)
  1278. {
  1279. return new Vector2(Screen.width, Screen.height);
  1280. }
  1281. Vector2 offsetDelta = rectTransform.anchorMax - rectTransform.anchorMin;
  1282. Vector2 parentSize = ComputeRectTransformFinalSize(rectTransform.parent as RectTransform);
  1283. parentSize.Scale(offsetDelta);
  1284. return rectTransform.sizeDelta + parentSize;
  1285. }
  1286. #if UNITY_WEBGL && !UNITY_EDITOR
  1287. IEnumerator LoadDocument_WebGL(byte[] specifiedBuffer = null)
  1288. {
  1289. PDFJS_Promise<PDFDocument> documentPromise = null;
  1290. byte[] buffer = specifiedBuffer;
  1291. switch (m_FileSource)
  1292. {
  1293. case FileSourceType.Asset:
  1294. if (m_PDFAsset.m_FileContent == null || m_PDFAsset.m_FileContent.Length == 0)
  1295. yield break;
  1296. documentPromise = PDFDocument.LoadDocumentFromBytesAsync(m_PDFAsset.m_FileContent);
  1297. break;
  1298. case FileSourceType.Bytes:
  1299. if (buffer != null)
  1300. {
  1301. documentPromise = PDFDocument.LoadDocumentFromBytesAsync(buffer);
  1302. }
  1303. else if (BytesSupplierComponent != null)
  1304. {
  1305. MethodInfo methodInfo = BytesSupplierComponent.GetType().GetMethod(BytesSupplierFunctionName);
  1306. if (methodInfo != null)
  1307. buffer = (byte[])methodInfo.Invoke(BytesSupplierComponent, null);
  1308. if (buffer != null)
  1309. documentPromise = PDFDocument.LoadDocumentFromBytesAsync(buffer);
  1310. }
  1311. if (buffer == null)
  1312. yield break;
  1313. break;
  1314. case FileSourceType.Resources:
  1315. buffer = LoadAssetBytesFromResources(GetFileLocation());
  1316. if (buffer != null)
  1317. documentPromise = PDFDocument.LoadDocumentFromBytesAsync(buffer);
  1318. else
  1319. yield break;
  1320. break;
  1321. case FileSourceType.Web:
  1322. case FileSourceType.FilePath:
  1323. case FileSourceType.StreamingAssets:
  1324. documentPromise = PDFDocument.LoadDocumentFromUrlAsync(GetFileLocation());
  1325. break;
  1326. }
  1327. while (!documentPromise.HasFinished)
  1328. yield return null;
  1329. if (documentPromise.HasSucceeded)
  1330. {
  1331. m_Document = documentPromise.Result;
  1332. m_NormalPageSizes = new Vector2[m_Document.GetPageCount()];
  1333. for (int i = 0; i < m_NormalPageSizes.Length; ++i)
  1334. {
  1335. PDFJS_Promise<PDFPage> pagePromise = m_Document.GetPageAsync(i);
  1336. while (!pagePromise.HasFinished)
  1337. yield return null;
  1338. if (pagePromise.HasSucceeded)
  1339. {
  1340. PDFPage page = pagePromise.Result;
  1341. m_NormalPageSizes[i] = page.GetPageSize(1.0f);
  1342. }
  1343. else
  1344. {
  1345. NotifyDocumentLoadFailed();
  1346. yield break;
  1347. }
  1348. }
  1349. TryLoadWithSpecifiedDocument(m_Document);
  1350. }
  1351. else
  1352. {
  1353. NotifyDocumentLoadFailed();
  1354. yield break;
  1355. }
  1356. }
  1357. #endif
  1358. #if !UNITY_WEBGL || UNITY_EDITOR
  1359. IEnumerator DownloadFileFromWWW()
  1360. {
  1361. m_OverlayVisible = true;
  1362. m_Internal.m_Overlay.gameObject.SetActive(true);
  1363. m_Internal.m_Overlay.GetComponent<CanvasGroup>().alpha = 0.0f;
  1364. m_Internal.m_DownloadDialog.gameObject.SetActive(true);
  1365. if (m_FileSource == FileSourceType.Web)
  1366. {
  1367. m_Internal.m_DownloadSourceLabel.text = GetFileLocation();
  1368. }
  1369. else
  1370. {
  1371. m_Internal.m_DownloadSourceLabel.text = "";
  1372. }
  1373. m_Internal.m_ProgressRect.sizeDelta = new Vector2(0.0f, m_Internal.m_ProgressRect.sizeDelta.y);
  1374. m_Internal.m_ProgressLabel.text = "0%";
  1375. m_WWW = new WWW(GetFileLocation());
  1376. yield return m_WWW;
  1377. if (m_WWW != null && m_WWW.error == null && m_WWW.isDone)
  1378. {
  1379. SetProgress(1.0f);
  1380. OnLoadingBufferFinished(m_WWW.bytes);
  1381. }
  1382. else if (m_WWW == null || m_WWW.error != null)
  1383. {
  1384. NotifyDocumentLoadFailed();
  1385. }
  1386. m_Internal.m_DownloadDialog.gameObject.SetActive(false);
  1387. }
  1388. #endif
  1389. private void EnsureValidPageContainerPosition()
  1390. {
  1391. if (GetDocumentSize().x <= m_ViewportSize.x)
  1392. {
  1393. m_Internal.m_PageContainer.anchoredPosition = new Vector2(0.0f,
  1394. m_Internal.m_PageContainer.anchoredPosition.y);
  1395. }
  1396. if (m_Internal.m_PageContainer.anchoredPosition.y >
  1397. m_Internal.m_PageContainer.sizeDelta.y - m_ViewportSize.y)
  1398. {
  1399. m_Internal.m_PageContainer.anchoredPosition = new Vector2(
  1400. m_Internal.m_PageContainer.anchoredPosition.x,
  1401. m_Internal.m_PageContainer.sizeDelta.y - m_ViewportSize.y);
  1402. }
  1403. if (m_Internal.m_PageContainer.anchoredPosition.y < 0.0f)
  1404. {
  1405. m_Internal.m_PageContainer.anchoredPosition = new Vector2(
  1406. m_Internal.m_PageContainer.anchoredPosition.x, 0.0f);
  1407. }
  1408. }
  1409. private Vector2 GetDocumentSize()
  1410. {
  1411. Vector2 size = new Vector2(0.0f, 0.0f);
  1412. if (m_PageSizes == null)
  1413. {
  1414. return size;
  1415. }
  1416. foreach (Vector2 s in m_PageSizes)
  1417. {
  1418. if (s.x > size.x)
  1419. {
  1420. size.x = s.x;
  1421. }
  1422. }
  1423. size.y = m_PageOffsets[m_PageCount - 1] + m_PageSizes[m_PageCount - 1].y * 0.5f;
  1424. size.x += 0.0f * m_VerticalMarginBetweenPages;
  1425. size.y += 1.0f * m_VerticalMarginBetweenPages;
  1426. return size;
  1427. }
  1428. private int GetMostVisiblePageIndex()
  1429. {
  1430. int mostVisibleIndex = -1;
  1431. float mostVisibleArea = 0.0f;
  1432. for (int i = m_CurrentPageRange.m_From; i < m_CurrentPageRange.m_To; ++i)
  1433. {
  1434. RectTransform page = m_Internal.m_PageContainer.GetChild(i) as RectTransform;
  1435. float area = PDFInternalUtils.CalculateRectTransformIntersectArea(page, m_Internal.m_Viewport);
  1436. if (area > page.sizeDelta.x*page.sizeDelta.y*0.4f)
  1437. {
  1438. return i;
  1439. }
  1440. if (area > mostVisibleArea)
  1441. {
  1442. mostVisibleIndex = i;
  1443. mostVisibleArea = area;
  1444. }
  1445. }
  1446. return mostVisibleIndex;
  1447. }
  1448. private PDFPageRange GetVisiblePageRange()
  1449. {
  1450. PDFPageRange pageRange = new PDFPageRange();
  1451. for (int i = 0; i < m_PageCount; ++i)
  1452. {
  1453. if (
  1454. PDFInternalUtils.CalculateRectTransformIntersectArea(
  1455. m_Internal.m_PageContainer.GetChild(i) as RectTransform, m_Internal.m_Viewport) > 0.0f)
  1456. {
  1457. if (pageRange.m_From == -1)
  1458. {
  1459. pageRange.m_From = i;
  1460. }
  1461. else
  1462. {
  1463. pageRange.m_To = i + 1;
  1464. }
  1465. }
  1466. else if (pageRange.m_From != -1)
  1467. {
  1468. break;
  1469. }
  1470. }
  1471. if (pageRange.m_From != -1 && pageRange.m_To == -1)
  1472. {
  1473. pageRange.m_To = pageRange.m_From + 1;
  1474. }
  1475. return pageRange;
  1476. }
  1477. private void InstantiatePageTextureHolders()
  1478. {
  1479. if (m_PageTextureHolders == null)
  1480. {
  1481. m_PageTextureHolders = new PDFPageTextureHolder[m_PageCount];
  1482. for (int i = 0; i < m_PageCount; ++i)
  1483. {
  1484. m_PageTextureHolders[i] = new PDFPageTextureHolder();
  1485. GameObject page = null;
  1486. if (i == 0)
  1487. page = m_Internal.m_PageSample.gameObject;
  1488. else
  1489. page = (GameObject)Instantiate(m_Internal.m_PageSample.gameObject);
  1490. page.transform.SetParent(m_Internal.m_PageSample.transform.parent);
  1491. page.transform.localScale = Vector3.one;
  1492. page.transform.localRotation = Quaternion.identity;
  1493. m_PageTextureHolders[i].m_PageIndex = i;
  1494. m_PageTextureHolders[i].m_Page = page;
  1495. m_PageTextureHolders[i].m_PDFViewer = this;
  1496. m_PageTextureHolders[i].Texture = null;
  1497. }
  1498. }
  1499. }
  1500. void Update()
  1501. {
  1502. #if !UNITY_WEBGL || UNITY_EDITOR
  1503. if (m_WWW != null && !m_WWW.isDone)
  1504. {
  1505. SetProgress(m_WWW.progress);
  1506. }
  1507. #endif
  1508. if (m_DelayedOnEnable)
  1509. {
  1510. m_DelayedOnEnable = false;
  1511. if (m_LoadOnEnable && !m_IsLoaded)
  1512. {
  1513. LoadDocument();
  1514. }
  1515. else
  1516. {
  1517. m_OverlayVisible = true;
  1518. m_Internal.m_Overlay.gameObject.SetActive(true);
  1519. m_Internal.m_Overlay.GetComponent<CanvasGroup>().alpha = 1.0f;
  1520. }
  1521. }
  1522. if (Input.touchCount >= 2)
  1523. {
  1524. if (m_PreviousTouchCount < 2)
  1525. {
  1526. m_PinchZoomStartZoomFactor = ZoomFactor;
  1527. ScrollRect scrollRect = m_Internal.m_Viewport.GetComponent<ScrollRect>();
  1528. scrollRect.inertia = false;
  1529. scrollRect.horizontal = false;
  1530. scrollRect.vertical = false;
  1531. StopCoroutine(DelayedUnlockScrollRect());
  1532. }
  1533. Touch touchZero = Input.GetTouch(0);
  1534. Touch touchOne = Input.GetTouch(1);
  1535. float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
  1536. if (m_PreviousTouchCount < 2)
  1537. m_PinchZoomStartDeltaMag = touchDeltaMag;
  1538. else
  1539. ZoomFactor = m_PinchZoomStartZoomFactor / (m_PinchZoomStartDeltaMag / touchDeltaMag);
  1540. }
  1541. else if (m_PreviousTouchCount >= 2)
  1542. StartCoroutine(DelayedUnlockScrollRect());
  1543. m_PreviousTouchCount = Input.touchCount;
  1544. }
  1545. private int m_PreviousTouchCount;
  1546. private float m_PinchZoomStartZoomFactor;
  1547. private float m_PinchZoomStartDeltaMag;
  1548. IEnumerator DelayedUnlockScrollRect()
  1549. {
  1550. while (Input.touchCount != 0)
  1551. yield return null;
  1552. ScrollRect scrollRect = m_Internal.m_Viewport.GetComponent<ScrollRect>();
  1553. scrollRect.inertia = true;
  1554. scrollRect.horizontal = true;
  1555. scrollRect.vertical = true;
  1556. }
  1557. private byte[] LoadAssetBytesFromResources(string path)
  1558. {
  1559. string fixedPath = path.Replace(".bytes", "");
  1560. if (fixedPath.StartsWith("./"))
  1561. {
  1562. fixedPath = fixedPath.Substring(2);
  1563. }
  1564. var pdfAsset = Resources.Load(fixedPath, typeof (TextAsset)) as TextAsset;
  1565. if (pdfAsset != null && pdfAsset.bytes != null && pdfAsset.bytes.Length > 0)
  1566. {
  1567. return pdfAsset.bytes;
  1568. }
  1569. return null;
  1570. }
  1571. private void NotifyCurrentPageChanged(int oldPageIndex, int newPageIndex)
  1572. {
  1573. if (OnCurrentPageChanged != null)
  1574. {
  1575. OnCurrentPageChanged(this, oldPageIndex, newPageIndex);
  1576. }
  1577. m_ThumbnailsViewer.OnCurrentPageChanged(newPageIndex);
  1578. }
  1579. private void NotifyDisabled()
  1580. {
  1581. if (OnDisabled != null)
  1582. {
  1583. OnDisabled(this);
  1584. }
  1585. }
  1586. private void NotifyDocumentLoaded(PDFDocument document)
  1587. {
  1588. if (OnDocumentLoaded != null)
  1589. {
  1590. OnDocumentLoaded(this, document);
  1591. }
  1592. m_ThumbnailsViewer.OnDocumentLoaded(document);
  1593. m_BookmarksViewer.OnDocumentLoaded(document);
  1594. }
  1595. private void NotifyDocumentLoadFailed()
  1596. {
  1597. if (OnDocumentLoadFailed != null)
  1598. {
  1599. OnDocumentLoadFailed(this);
  1600. }
  1601. }
  1602. private void NotifyDocumentUnloaded(PDFDocument document)
  1603. {
  1604. if (OnDocumentUnloaded != null)
  1605. {
  1606. OnDocumentUnloaded(this, document);
  1607. }
  1608. m_ThumbnailsViewer.OnDocumentUnloaded();
  1609. m_BookmarksViewer.OnDocumentUnloaded();
  1610. }
  1611. private void NotifyDownloadCancelled()
  1612. {
  1613. if (OnDownloadCancelled != null)
  1614. {
  1615. OnDownloadCancelled(this);
  1616. }
  1617. }
  1618. private void NotifyPasswordCancelled()
  1619. {
  1620. if (OnPasswordCancelled != null)
  1621. {
  1622. OnPasswordCancelled(this);
  1623. }
  1624. }
  1625. private void NotifyZoomChanged(float oldZoom, float newZoom)
  1626. {
  1627. if (OnZoomChanged != null)
  1628. {
  1629. OnZoomChanged(this, oldZoom, newZoom);
  1630. }
  1631. }
  1632. protected override void OnDisable()
  1633. {
  1634. base.OnDisable();
  1635. if (m_UnloadOnDisable && m_IsLoaded)
  1636. {
  1637. if (m_Renderer != null)
  1638. m_Renderer.Dispose();
  1639. m_Renderer = null;
  1640. CleanUp();
  1641. }
  1642. NotifyDisabled();
  1643. GC.Collect();
  1644. GC.WaitForPendingFinalizers();
  1645. }
  1646. protected override void OnEnable()
  1647. {
  1648. base.OnEnable();
  1649. m_Canvas = GetComponentInParent<Canvas>();
  1650. m_DelayedOnEnable = true;
  1651. if (m_ThumbnailsViewer == null)
  1652. m_ThumbnailsViewer = m_Internal.m_LeftPanel.m_ThumbnailsViewer;
  1653. if (m_BookmarksViewer == null)
  1654. m_BookmarksViewer = m_Internal.m_LeftPanel.m_Bookmarks.GetComponent<PDFBookmarksViewer>();
  1655. if (!m_ShowBookmarksViewer && m_ShowThumbnailsViewer)
  1656. m_Internal.m_LeftPanel.OnThumbnailsTabClicked();
  1657. else if (m_ShowBookmarksViewer && !m_ShowThumbnailsViewer)
  1658. m_Internal.m_LeftPanel.OnBookmarksTabClicked();
  1659. else
  1660. m_Internal.m_LeftPanel.OnBookmarksTabClicked();
  1661. m_ThumbnailsViewer.DoOnEnable();
  1662. m_BookmarksViewer.DoOnEnable();
  1663. #if UNITY_WEBGL
  1664. m_Internal.m_SearchPanel.gameObject.SetActive(false);
  1665. int c = m_Internal.m_TopPanel.childCount;
  1666. for (int i = 0; i < c; ++i)
  1667. {
  1668. Transform t = m_Internal.m_TopPanel.GetChild(i);
  1669. if (t.name == "_SearchButton")
  1670. t.gameObject.SetActive(false);
  1671. }
  1672. #endif
  1673. }
  1674. #if !UNITY_WEBGL || UNITY_EDITOR
  1675. private void OnLoadingBufferFinished(byte[] buffer)
  1676. {
  1677. m_PendingDocumentBuffer = buffer;
  1678. if (m_PendingDocumentBuffer != null && m_PendingDocumentBuffer.Length > 0)
  1679. {
  1680. if (!TryLoadDocumentWithBuffer(m_PendingDocumentBuffer, m_Password))
  1681. {
  1682. if (m_FileSource == FileSourceType.Asset)
  1683. {
  1684. if (!TryLoadDocumentWithBuffer(m_PendingDocumentBuffer, m_PDFAsset.m_Password))
  1685. {
  1686. ShowPasswordDialog();
  1687. }
  1688. }
  1689. else
  1690. ShowPasswordDialog();
  1691. }
  1692. }
  1693. else
  1694. {
  1695. m_OverlayVisible = true;
  1696. m_Internal.m_Overlay.gameObject.SetActive(true);
  1697. m_Internal.m_Overlay.GetComponent<CanvasGroup>().alpha = 1.0f;
  1698. }
  1699. }
  1700. #endif
  1701. private void SetPageCountLabel(int pageIndex, int pageCount)
  1702. {
  1703. m_Internal.m_PageCountLabel.text = "(" + pageIndex + " of " + pageCount + ")";
  1704. }
  1705. private void SetProgress(float progress)
  1706. {
  1707. var rectTransform = m_Internal.m_ProgressRect.parent.transform as RectTransform;
  1708. if (rectTransform != null)
  1709. {
  1710. m_Internal.m_ProgressRect.sizeDelta = new Vector2(progress*rectTransform.sizeDelta.x,
  1711. m_Internal.m_ProgressRect.sizeDelta.y);
  1712. }
  1713. m_Internal.m_ProgressLabel.text = ((int) (progress*100)) + "%";
  1714. }
  1715. private void SetZoomLabel()
  1716. {
  1717. m_Internal.m_PageZoomLabel.text = "(" + (int) (Mathf.Round(m_ZoomFactor*100.0f)) + "%)";
  1718. }
  1719. private void ShowPasswordDialog()
  1720. {
  1721. m_OverlayVisible = true;
  1722. m_Internal.m_Overlay.gameObject.SetActive(true);
  1723. m_Internal.m_Overlay.GetComponent<CanvasGroup>().alpha = 0.0f;
  1724. m_Internal.m_PasswordDialog.gameObject.SetActive(true);
  1725. }
  1726. protected override void Start()
  1727. {
  1728. m_StartZoom = m_ZoomToGo;
  1729. m_LinksActionHandler = new PDFViewerDefaultActionHandler();
  1730. m_BookmarksActionHandler = new PDFViewerDefaultActionHandler();
  1731. }
  1732. #if !UNITY_WEBGL || UNITY_EDITOR
  1733. private bool TryLoadDocumentWithBuffer(byte[] buffer, string password)
  1734. {
  1735. m_Document = new PDFDocument(buffer, password);
  1736. return TryLoadWithSpecifiedDocument(m_Document);
  1737. }
  1738. #endif
  1739. private bool TryLoadWithSpecifiedDocument(PDFDocument document)
  1740. {
  1741. m_Document = document;
  1742. if (m_Document != null && m_Document.IsValid)
  1743. {
  1744. m_CurrentPageRange = new PDFPageRange();
  1745. m_PageCount = m_Document.GetPageCount();
  1746. #if !UNITY_WEBGL || UNITY_EDITOR
  1747. m_NormalPageSizes = new Vector2[m_PageCount];
  1748. for (int i = 0; i < m_NormalPageSizes.Length; ++i)
  1749. {
  1750. m_NormalPageSizes[i] = m_Document.GetPageSize(i);
  1751. }
  1752. #endif
  1753. m_Internal.m_ScrollRect.scrollSensitivity = m_ScrollSensitivity;
  1754. m_PreviousPageFitting = m_PageFitting;
  1755. AdjustZoomToPageFitting(m_PageFitting, m_NormalPageSizes[0]);
  1756. m_ZoomFactor = m_ZoomToGo;
  1757. ComputePageSizes();
  1758. ComputePageOffsets();
  1759. InstantiatePageTextureHolders();
  1760. m_Internal.m_PageContainer.sizeDelta = GetDocumentSize();
  1761. SetPageCountLabel(1, m_PageCount);
  1762. SetZoomLabel();
  1763. m_Internal.m_PageContainer.anchoredPosition = Vector2.zero;
  1764. m_IsLoaded = true;
  1765. UpdatePagesPlacement();
  1766. UpdateScrollBarVisibility();
  1767. EnsureValidPageContainerPosition();
  1768. GoToPage(m_LoadAtPageIndex);
  1769. m_LoadAtPageIndex = 0;
  1770. NotifyDocumentLoaded(m_Document);
  1771. if (m_ShowBookmarksViewer)
  1772. ShowBookmarksViewer = true;
  1773. return true;
  1774. }
  1775. else
  1776. {
  1777. m_OverlayVisible = true;
  1778. m_Internal.m_Overlay.gameObject.SetActive(true);
  1779. m_Internal.m_Overlay.GetComponent<CanvasGroup>().alpha = 1.0f;
  1780. }
  1781. m_IsLoaded = false;
  1782. NotifyDocumentLoadFailed();
  1783. return false;
  1784. }
  1785. void LateUpdate()
  1786. {
  1787. if (m_InvalidPasswordMessageVisisble && m_InvalidPasswordMessageDelay >= 0.0f)
  1788. {
  1789. m_InvalidPasswordMessageDelay = m_InvalidPasswordMessageDelay - Time.deltaTime;
  1790. if (m_InvalidPasswordMessageDelay < 0.0f)
  1791. {
  1792. m_InvalidPasswordMessageDelay = 0.0f;
  1793. }
  1794. CanvasGroup messageCanvas = m_Internal.m_InvalidPasswordImage.GetComponent<CanvasGroup>();
  1795. if (m_InvalidPasswordMessageDelay <= 0.0f)
  1796. {
  1797. messageCanvas.alpha = Mathf.Clamp01(messageCanvas.alpha - Time.deltaTime);
  1798. }
  1799. if (messageCanvas.alpha <= 0.0f)
  1800. {
  1801. m_InvalidPasswordMessageVisisble = false;
  1802. messageCanvas.alpha = 1.0f;
  1803. m_Internal.m_InvalidPasswordImage.gameObject.SetActive(false);
  1804. }
  1805. }
  1806. if (m_OverlayVisible && !m_IsLoaded)
  1807. {
  1808. CanvasGroup overlayCanvas = m_Internal.m_Overlay.GetComponent<CanvasGroup>();
  1809. overlayCanvas.alpha = Mathf.Clamp01(overlayCanvas.alpha + Time.deltaTime*2.0f);
  1810. if (overlayCanvas.alpha > m_OverlayAlpha)
  1811. {
  1812. overlayCanvas.alpha = m_OverlayAlpha;
  1813. }
  1814. }
  1815. else if (m_OverlayVisible && m_IsLoaded)
  1816. {
  1817. CanvasGroup overlayCanvas = m_Internal.m_Overlay.GetComponent<CanvasGroup>();
  1818. overlayCanvas.alpha = Mathf.Clamp01(overlayCanvas.alpha - Time.deltaTime*2.0f);
  1819. if (overlayCanvas.alpha == 0.0f)
  1820. {
  1821. m_OverlayVisible = false;
  1822. m_Internal.m_Overlay.gameObject.SetActive(false);
  1823. }
  1824. }
  1825. if (m_Internal.m_PasswordDialog.gameObject.activeInHierarchy && m_Internal.m_PasswordInputField.text != "" &&
  1826. Input.GetKeyDown("enter"))
  1827. {
  1828. OnPasswordDialogOkButtonClicked();
  1829. }
  1830. if (m_Document == null || !m_Document.IsValid || !m_IsLoaded)
  1831. return;
  1832. m_ViewportSize = ComputeRectTransformFinalSize(m_Internal.m_Viewport);
  1833. if (m_Canvas != null)
  1834. {
  1835. m_ViewportSize.x /= m_Canvas.scaleFactor;
  1836. m_ViewportSize.y /= m_Canvas.scaleFactor;
  1837. }
  1838. if (m_ViewportSize != m_PreviousViewportSize)
  1839. {
  1840. UpdateScrollBarVisibility();
  1841. EnsureValidPageContainerPosition();
  1842. }
  1843. if (m_PageFitting != m_PreviousPageFitting)
  1844. {
  1845. AdjustZoomToPageFitting(m_PageFitting, m_NormalPageSizes[0]);
  1846. }
  1847. if (!Mathf.Approximately(m_ZoomFactor, m_ZoomToGo))
  1848. {
  1849. m_ZoomFactor = Mathf.Lerp(m_ZoomFactor, m_ZoomToGo, Time.deltaTime*15.0f);
  1850. m_ZoomFactor = Mathf.Clamp(m_ZoomFactor, m_MinZoomFactor, m_MaxZoomFactor);
  1851. m_UpdateChangeDelay = m_DelayAfterZoomingBeforeUpdate;
  1852. }
  1853. else
  1854. m_ZoomFactor = m_ZoomToGo;
  1855. bool zoomHasChanged = (m_PreviousZoom != m_ZoomFactor && m_PreviousZoom != 0.0f);
  1856. if (m_PreviousZoom == 0.0f)
  1857. {
  1858. UpdateScrollBarVisibility();
  1859. EnsureValidPageContainerPosition();
  1860. }
  1861. if (zoomHasChanged)
  1862. {
  1863. Vector2 oldDocumentSize = GetDocumentSize();
  1864. ComputePageSizes();
  1865. ComputePageOffsets();
  1866. UpdatePagesPlacement();
  1867. m_Internal.m_PageContainer.sizeDelta = GetDocumentSize();
  1868. float newDocumentWidthRatio = m_Internal.m_PageContainer.sizeDelta.x / oldDocumentSize.x;
  1869. float newDocumentHeightRatio = m_Internal.m_PageContainer.sizeDelta.y / oldDocumentSize.y;
  1870. float deltaOffsetY = (m_Internal.m_PageContainer.anchoredPosition.y + m_ZoomPosition.y) *
  1871. newDocumentHeightRatio - m_Internal.m_PageContainer.anchoredPosition.y - m_ZoomPosition.y;
  1872. float deltaOffsetX = (m_Internal.m_PageContainer.anchoredPosition.x + m_ZoomPosition.x) *
  1873. newDocumentWidthRatio - m_Internal.m_PageContainer.anchoredPosition.x - m_ZoomPosition.x;
  1874. m_Internal.m_PageContainer.anchoredPosition += Vector2.up * deltaOffsetY + Vector2.right * deltaOffsetX;
  1875. UpdateScrollBarVisibility();
  1876. SetZoomLabel();
  1877. }
  1878. else if (Input.touchCount < 2)
  1879. {
  1880. EnsureValidPageContainerPosition();
  1881. }
  1882. PDFPageRange oldPageRange = m_CurrentPageRange;
  1883. m_CurrentPageRange = GetVisiblePageRange();
  1884. if (!m_Internal.m_PageInputField.isFocused)
  1885. {
  1886. int p = GetMostVisiblePageIndex() + 1;
  1887. m_Internal.m_PageInputField.text = p.ToString();
  1888. SetPageCountLabel(p, m_PageCount);
  1889. }
  1890. if ((!oldPageRange.Equals(m_CurrentPageRange) && m_CurrentPageRange.IsValid)
  1891. || (zoomHasChanged && m_CurrentPageRange.IsValid))
  1892. {
  1893. float scale = Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality);
  1894. PDFPageRange.UpdatePageAgainstRanges(oldPageRange, m_CurrentPageRange, m_Document, m_PageTextureHolders, m_RenderSettings, scale, this, m_NormalPageSizes);
  1895. }
  1896. int mostVisible = GetMostVisiblePageIndex();
  1897. if (m_PreviousMostVisiblePage != mostVisible)
  1898. {
  1899. NotifyCurrentPageChanged(m_PreviousMostVisiblePage, mostVisible);
  1900. m_PreviousMostVisiblePage = GetMostVisiblePageIndex();
  1901. }
  1902. if (!oldPageRange.Equals(m_CurrentPageRange))
  1903. {
  1904. AdjustCurrentSearchResultDisplayed();
  1905. }
  1906. if (m_PreviousZoomToGo != m_ZoomToGo)
  1907. {
  1908. NotifyZoomChanged(m_PreviousZoomToGo, m_ZoomToGo);
  1909. }
  1910. if (m_UpdateChangeDelay != 0.0f && !zoomHasChanged)
  1911. {
  1912. m_UpdateChangeDelay -= Time.deltaTime;
  1913. if (m_UpdateChangeDelay <= 0.0f)
  1914. {
  1915. m_UpdateChangeDelay = 0.0f;
  1916. for (int i = m_CurrentPageRange.m_From; i < m_CurrentPageRange.m_To; ++i)
  1917. {
  1918. #if UNITY_WEBGL
  1919. m_PageTextureHolders[i].m_Visible = true;
  1920. if (m_PageTextureHolders[i].m_RenderingStarted)
  1921. continue;
  1922. int w = (int)(m_NormalPageSizes[i].x * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1923. int h = (int)(m_NormalPageSizes[i].y * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1924. m_PageTextureHolders[i].m_RenderingStarted = true;
  1925. StartCoroutine(UpdatePageRangeTextures(i, w, h));
  1926. #else
  1927. if (m_PageTextureHolders[i].Texture != null)
  1928. {
  1929. Texture2D tex = m_PageTextureHolders[i].Texture;
  1930. m_PageTextureHolders[i].Texture = null;
  1931. Destroy(tex);
  1932. Resources.UnloadUnusedAssets();
  1933. }
  1934. int w = (int)(m_Document.GetPageWidth(i) * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1935. int h = (int)(m_Document.GetPageHeight(i) * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1936. if (m_Renderer == null)
  1937. m_Renderer = new PDFRenderer();
  1938. Texture2D newTex = m_Renderer.RenderPageToTexture(m_Document.GetPage(i), w, h, this, m_RenderSettings);
  1939. m_PageTextureHolders[i].Texture = newTex;
  1940. #endif
  1941. }
  1942. }
  1943. }
  1944. m_PreviousViewportSize = m_ViewportSize;
  1945. m_PreviousZoom = m_ZoomFactor;
  1946. m_PreviousZoomToGo = m_ZoomToGo;
  1947. m_PreviousPageFitting = m_PageFitting;
  1948. if (m_ThumbnailsViewer.gameObject.activeInHierarchy)
  1949. m_ThumbnailsViewer.DoUpdate();
  1950. if (m_BookmarksViewer.gameObject.activeInHierarchy)
  1951. m_BookmarksViewer.DoUpdate();
  1952. }
  1953. #if UNITY_WEBGL
  1954. IEnumerator UpdatePageRangeTextures(int pageIndex, int w, int h)
  1955. {
  1956. PDFJS_Promise<PDFPage> pagePromise = m_Document.GetPageAsync(pageIndex);
  1957. while (!pagePromise.HasFinished)
  1958. yield return null;
  1959. if (pagePromise.HasSucceeded)
  1960. {
  1961. PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToTextureAsync(pagePromise.Result, w, h);
  1962. m_PageTextureHolders[pageIndex].m_RenderingPromise = renderPromise;
  1963. while (!renderPromise.HasFinished)
  1964. yield return null;
  1965. m_PageTextureHolders[pageIndex].m_RenderingPromise = null;
  1966. m_PageTextureHolders[pageIndex].m_RenderingStarted = false;
  1967. if (renderPromise.HasSucceeded)
  1968. {
  1969. if (m_PageTextureHolders[pageIndex].Texture != null && m_PageTextureHolders[pageIndex].Texture != renderPromise.Result)
  1970. {
  1971. DestroyImmediate(m_PageTextureHolders[pageIndex].Texture);
  1972. Resources.UnloadAsset(m_PageTextureHolders[pageIndex].Texture);
  1973. m_PageTextureHolders[pageIndex].Texture = null;
  1974. }
  1975. if (m_PageTextureHolders[pageIndex].m_Visible)
  1976. {
  1977. m_PageTextureHolders[pageIndex].Texture = renderPromise.Result;
  1978. }
  1979. else
  1980. {
  1981. DestroyImmediate(renderPromise.Result);
  1982. Resources.UnloadAsset(renderPromise.Result);
  1983. renderPromise.Result = null;
  1984. }
  1985. }
  1986. }
  1987. else
  1988. {
  1989. m_PageTextureHolders[pageIndex].m_RenderingPromise = null;
  1990. m_PageTextureHolders[pageIndex].m_RenderingStarted = false;
  1991. }
  1992. }
  1993. #endif
  1994. public Vector2[] GetCachedNormalPageSizes()
  1995. {
  1996. return m_NormalPageSizes;
  1997. }
  1998. private void UpdatePagesPlacement()
  1999. {
  2000. if (m_PageTextureHolders == null || m_PageSizes == null)
  2001. return;
  2002. foreach (PDFPageTextureHolder holder in m_PageTextureHolders)
  2003. {
  2004. (holder.m_Page.transform as RectTransform).sizeDelta = m_PageSizes[holder.m_PageIndex];
  2005. holder.RefreshTexture();
  2006. Vector3 newPosition = (holder.m_Page.transform as RectTransform).anchoredPosition3D;
  2007. newPosition.x = 0;
  2008. newPosition.y = -m_PageOffsets[holder.m_PageIndex];
  2009. newPosition.z = 0;
  2010. (holder.m_Page.transform as RectTransform).anchoredPosition3D = newPosition;
  2011. }
  2012. }
  2013. private void UpdateScrollBarVisibility()
  2014. {
  2015. bool vScrollVisible = true;
  2016. bool hScrollVisible = true;
  2017. if (Application.isPlaying)
  2018. {
  2019. vScrollVisible = (m_Internal.m_PageContainer.sizeDelta.y > m_ViewportSize.y);
  2020. hScrollVisible = (m_Internal.m_PageContainer.sizeDelta.x > m_ViewportSize.x);
  2021. }
  2022. vScrollVisible = vScrollVisible && m_ShowVerticalScrollBar;
  2023. hScrollVisible = hScrollVisible && m_ShowHorizontalScrollBar;
  2024. if (!hScrollVisible)
  2025. {
  2026. m_Internal.m_Viewport.offsetMin = new Vector2(m_Internal.m_Viewport.offsetMin.x, 0.0f);
  2027. if (m_Internal.m_HorizontalScrollBar.gameObject.activeInHierarchy)
  2028. {
  2029. m_Internal.m_HorizontalScrollBar.gameObject.SetActive(false);
  2030. }
  2031. }
  2032. else
  2033. {
  2034. m_Internal.m_Viewport.offsetMin = new Vector2(m_Internal.m_Viewport.offsetMin.x, 20.0f);
  2035. if (!m_Internal.m_HorizontalScrollBar.gameObject.activeInHierarchy)
  2036. {
  2037. m_Internal.m_HorizontalScrollBar.gameObject.SetActive(true);
  2038. }
  2039. }
  2040. if (!vScrollVisible)
  2041. {
  2042. m_Internal.m_Viewport.offsetMax = new Vector2(0.0f, m_Internal.m_Viewport.offsetMax.y);
  2043. if (m_Internal.m_VerticalScrollBar.gameObject.activeInHierarchy)
  2044. {
  2045. m_Internal.m_VerticalScrollBar.gameObject.SetActive(false);
  2046. }
  2047. }
  2048. else
  2049. {
  2050. m_Internal.m_Viewport.offsetMax = new Vector2(-20.0f, m_Internal.m_Viewport.offsetMax.y);
  2051. if (!m_Internal.m_VerticalScrollBar.gameObject.activeInHierarchy)
  2052. {
  2053. m_Internal.m_VerticalScrollBar.gameObject.SetActive(true);
  2054. }
  2055. }
  2056. if (hScrollVisible && vScrollVisible)
  2057. {
  2058. m_Internal.m_VerticalScrollBar.offsetMin = new Vector2(m_Internal.m_VerticalScrollBar.offsetMin.x, 19.0f);
  2059. m_Internal.m_HorizontalScrollBar.offsetMax = new Vector2(-19.0f,
  2060. m_Internal.m_HorizontalScrollBar.offsetMax.y);
  2061. if (!m_Internal.m_ScrollCorner.gameObject.activeInHierarchy)
  2062. {
  2063. m_Internal.m_ScrollCorner.gameObject.SetActive(true);
  2064. }
  2065. }
  2066. else if (!hScrollVisible)
  2067. {
  2068. m_Internal.m_VerticalScrollBar.offsetMin = new Vector2(m_Internal.m_VerticalScrollBar.offsetMin.x, 0.0f);
  2069. if (m_Internal.m_ScrollCorner.gameObject.activeInHierarchy)
  2070. {
  2071. m_Internal.m_ScrollCorner.gameObject.SetActive(false);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. m_Internal.m_HorizontalScrollBar.offsetMax = new Vector2(0.0f,
  2077. m_Internal.m_HorizontalScrollBar.offsetMax.y);
  2078. if (m_Internal.m_ScrollCorner.gameObject.activeInHierarchy)
  2079. {
  2080. m_Internal.m_ScrollCorner.gameObject.SetActive(false);
  2081. }
  2082. }
  2083. }
  2084. private void ZoomCommon(Vector2 zoomPosition, bool zoomIn, bool useSpecificZoom = false, float specificZoom = 1.0f)
  2085. {
  2086. if (m_Document == null || !m_Document.IsValid)
  2087. return;
  2088. m_ZoomPosition = zoomPosition;
  2089. if (useSpecificZoom)
  2090. {
  2091. m_ZoomToGo = specificZoom;
  2092. }
  2093. else
  2094. {
  2095. float step = m_ZoomStep;
  2096. if (m_ZoomToGo >= 2.0f)
  2097. {
  2098. step *= 2.0f;
  2099. }
  2100. if (m_ZoomToGo >= 4.0f)
  2101. {
  2102. step *= 2.0f;
  2103. }
  2104. if (zoomIn)
  2105. {
  2106. if (!Mathf.Approximately(Mathf.Floor(m_ZoomToGo * (1 / step)), m_ZoomToGo * (1 / step))
  2107. && m_ZoomToGo * (1 / step) < Mathf.Floor(m_ZoomToGo * (1 / step)))
  2108. {
  2109. m_ZoomToGo = Mathf.Floor(m_ZoomToGo * (1 / step)) * step;
  2110. }
  2111. else
  2112. {
  2113. m_ZoomToGo = Mathf.Clamp(Mathf.Floor((m_ZoomToGo + step) * (1 / step)) * step, m_MinZoomFactor,
  2114. m_MaxZoomFactor);
  2115. }
  2116. }
  2117. else
  2118. {
  2119. if (!Mathf.Approximately(Mathf.Floor(m_ZoomToGo * (1 / step)), m_ZoomToGo * (1 / step))
  2120. && m_ZoomToGo * (1 / step) > Mathf.Floor(m_ZoomToGo * (1 / step)))
  2121. {
  2122. m_ZoomToGo = Mathf.Floor(m_ZoomToGo * (1 / step)) * step;
  2123. }
  2124. else
  2125. {
  2126. m_ZoomToGo = Mathf.Clamp(Mathf.Floor((m_ZoomToGo - step) * (1 / step)) * step, m_MinZoomFactor,
  2127. m_MaxZoomFactor);
  2128. }
  2129. }
  2130. }
  2131. m_PageFitting = PageFittingType.Zoom;
  2132. }
  2133. public Vector2 GetDevicePageSize(int pageIndex)
  2134. {
  2135. return m_PageSizes[pageIndex];
  2136. }
  2137. public IPDFDeviceActionHandler GetBookmarksActionHandler()
  2138. {
  2139. #if UNITY_WEBGL && !UNITY_EDITOR
  2140. return null;
  2141. #else
  2142. return m_BookmarksActionHandler;
  2143. #endif
  2144. }
  2145. public IPDFDeviceActionHandler GetLinksActionHandler()
  2146. {
  2147. #if UNITY_WEBGL && !UNITY_EDITOR
  2148. return null;
  2149. #else
  2150. return m_LinksActionHandler;
  2151. #endif
  2152. }
  2153. public PDFDocument GetLoadedDocument()
  2154. {
  2155. return m_Document;
  2156. }
  2157. public IList<PDFColoredRect> GetBackgroundColoredRectList(PDFPage page)
  2158. {
  2159. #if !UNITY_WEBGL
  2160. if (m_SearchResults != null && m_SearchResults[page.PageIndex] != null &&
  2161. m_SearchResults[page.PageIndex].Count > 0)
  2162. {
  2163. using (PDFTextPage textPage = page.GetTextPage())
  2164. {
  2165. List<PDFColoredRect> coloredRectList = new List<PDFColoredRect>();
  2166. foreach (PDFSearchResult result in m_SearchResults[page.PageIndex])
  2167. {
  2168. int pageRectCount = textPage.CountRects(result.StartIndex, result.Count);
  2169. for (int j = 0; j < pageRectCount; ++j)
  2170. {
  2171. Rect rect = textPage.GetRect(j);
  2172. rect = new Rect(
  2173. rect.xMin - m_SearchResultPadding.x,
  2174. rect.yMin + m_SearchResultPadding.y,
  2175. rect.width + 2*m_SearchResultPadding.x,
  2176. rect.height + 2*m_SearchResultPadding.y);
  2177. coloredRectList.Add(new PDFColoredRect(rect, m_SearchResultColor));
  2178. }
  2179. }
  2180. return coloredRectList;
  2181. }
  2182. }
  2183. #endif
  2184. return null;
  2185. }
  2186. public void ZoomOnParagraph(PDFViewerPage viewerPage, Rect pageRect)
  2187. {
  2188. #if !UNITY_WEBGL
  2189. Vector3[] pageCorners = new Vector3[4];
  2190. (viewerPage.transform as RectTransform).GetWorldCorners(pageCorners);
  2191. Vector2 min = pageCorners[0];
  2192. Vector2 max = pageCorners[0];
  2193. for (int i = 1; i < 4; ++i)
  2194. {
  2195. if (pageCorners[i].x < min.x)
  2196. min.x = pageCorners[i].x;
  2197. if (pageCorners[i].y < min.y)
  2198. min.y = pageCorners[i].y;
  2199. if (pageCorners[i].x > max.x)
  2200. max.x = pageCorners[i].x;
  2201. if (pageCorners[i].y > max.y)
  2202. max.y = pageCorners[i].y;
  2203. }
  2204. Vector2 devicePageSize = (viewerPage.transform as RectTransform).sizeDelta;
  2205. Rect deviceRect = viewerPage.m_Page.ConvertPageRectToDeviceRect(pageRect, devicePageSize);
  2206. float deviceRectCenterPosition = deviceRect.max.y + (deviceRect.min - deviceRect.max).y * 0.5f;
  2207. m_Internal.m_PageContainer.anchoredPosition = new Vector2(
  2208. m_Internal.m_PageContainer.anchoredPosition.x,
  2209. (m_PageOffsets[viewerPage.m_Page.PageIndex]
  2210. - m_PageSizes[viewerPage.m_Page.PageIndex].y * 0.5f)
  2211. + (m_PageSizes[viewerPage.m_Page.PageIndex].y - deviceRectCenterPosition)
  2212. - m_ViewportSize.y * 0.5f);
  2213. if (m_ZoomToGo < m_ParagraphZoomFactor)
  2214. ZoomCommon(new Vector2(0.0f, m_ViewportSize.y * 0.5f), true, true, m_ParagraphZoomFactor);
  2215. #endif
  2216. }
  2217. }
  2218. }