PDFViewer.cs 92 KB

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