PDFViewer.cs 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  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. m_Internal.OnZoomInButtonClicked();
  1843. m_Internal.OnZoomInButtonClicked();
  1844. m_Internal.OnZoomInButtonClicked();
  1845. m_Internal.OnZoomInButtonClicked();
  1846. return true;
  1847. }
  1848. else
  1849. {
  1850. OverlayVisible = true;
  1851. m_Internal.Overlay.gameObject.SetActive(true);
  1852. m_Internal.Overlay.GetComponent<CanvasGroup>().alpha = 1.0f;
  1853. }
  1854. m_IsLoaded = false;
  1855. NotifyDocumentLoadFailed();
  1856. return false;
  1857. }
  1858. private void LateUpdate()
  1859. {
  1860. if (m_InvalidPasswordMessageVisisble && m_InvalidPasswordMessageDelay >= 0.0f)
  1861. {
  1862. m_InvalidPasswordMessageDelay = m_InvalidPasswordMessageDelay - Time.deltaTime;
  1863. if (m_InvalidPasswordMessageDelay < 0.0f)
  1864. m_InvalidPasswordMessageDelay = 0.0f;
  1865. CanvasGroup messageCanvas = m_Internal.InvalidPasswordImage.GetComponent<CanvasGroup>();
  1866. if (m_InvalidPasswordMessageDelay <= 0.0f)
  1867. messageCanvas.alpha = Mathf.Clamp01(messageCanvas.alpha - Time.deltaTime);
  1868. if (messageCanvas.alpha <= 0.0f)
  1869. {
  1870. m_InvalidPasswordMessageVisisble = false;
  1871. messageCanvas.alpha = 1.0f;
  1872. m_Internal.InvalidPasswordImage.gameObject.SetActive(false);
  1873. }
  1874. }
  1875. if (OverlayVisible && !m_IsLoaded)
  1876. {
  1877. CanvasGroup overlayCanvas = m_Internal.Overlay.GetComponent<CanvasGroup>();
  1878. overlayCanvas.alpha = Mathf.Clamp01(overlayCanvas.alpha + Time.deltaTime * 2.0f);
  1879. if (overlayCanvas.alpha > OverlayAlpha)
  1880. overlayCanvas.alpha = OverlayAlpha;
  1881. }
  1882. else if (OverlayVisible && m_IsLoaded)
  1883. {
  1884. CanvasGroup overlayCanvas = m_Internal.Overlay.GetComponent<CanvasGroup>();
  1885. overlayCanvas.alpha = Mathf.Clamp01(overlayCanvas.alpha - Time.deltaTime * 2.0f);
  1886. if (overlayCanvas.alpha == 0.0f)
  1887. {
  1888. OverlayVisible = false;
  1889. m_Internal.Overlay.gameObject.SetActive(false);
  1890. }
  1891. }
  1892. if (m_Internal.PasswordDialog.gameObject.activeInHierarchy
  1893. && m_Internal.PasswordInputField.text != ""
  1894. && Input.GetKeyDown("enter"))
  1895. {
  1896. OnPasswordDialogOkButtonClicked();
  1897. }
  1898. if (m_Document == null || !m_Document.IsValid || !m_IsLoaded)
  1899. return;
  1900. UpdateScrollBarVisibility();
  1901. EnsureValidPageContainerPosition();
  1902. if (m_PageFitting != m_PreviousPageFitting)
  1903. AdjustZoomToPageFitting(m_PageFitting, m_NormalPageSizes[0]);
  1904. if (Math.Abs(m_ZoomFactor - m_ZoomToGo) > 0.001f)
  1905. {
  1906. m_ZoomToGo = Mathf.Clamp(m_ZoomToGo, m_MinZoomFactor, m_MaxZoomFactor);
  1907. m_ZoomFactor = Mathf.Lerp(m_ZoomFactor, m_ZoomToGo, Time.deltaTime * 15.0f);
  1908. m_UpdateChangeDelay = m_DelayAfterZoomingBeforeUpdate;
  1909. }
  1910. else
  1911. {
  1912. m_ZoomFactor = m_ZoomToGo;
  1913. }
  1914. bool zoomHasChanged = m_PreviousZoom != 0.0f && Math.Abs(m_PreviousZoom - m_ZoomFactor) > float.Epsilon;
  1915. if (m_PreviousZoom == 0.0f)
  1916. {
  1917. UpdateScrollBarVisibility();
  1918. EnsureValidPageContainerPosition();
  1919. }
  1920. if (zoomHasChanged)
  1921. {
  1922. Vector2 oldDocumentSize = GetDocumentSize();
  1923. ComputePageSizes();
  1924. ComputePageOffsets();
  1925. UpdatePagesPlacement();
  1926. m_Internal.PageContainer.sizeDelta = GetDocumentSize();
  1927. float newDocumentWidthRatio = m_Internal.PageContainer.sizeDelta.x / oldDocumentSize.x;
  1928. float newDocumentHeightRatio = m_Internal.PageContainer.sizeDelta.y / oldDocumentSize.y;
  1929. float deltaOffsetY = (m_Internal.PageContainer.anchoredPosition.y + m_ZoomPosition.y) *
  1930. newDocumentHeightRatio - m_Internal.PageContainer.anchoredPosition.y - m_ZoomPosition.y;
  1931. float deltaOffsetX = (m_Internal.PageContainer.anchoredPosition.x + m_ZoomPosition.x) *
  1932. newDocumentWidthRatio - m_Internal.PageContainer.anchoredPosition.x - m_ZoomPosition.x;
  1933. m_Internal.PageContainer.anchoredPosition += Vector2.up * deltaOffsetY + Vector2.right * deltaOffsetX;
  1934. UpdateScrollBarVisibility();
  1935. SetZoomLabel();
  1936. }
  1937. else if (Input.touchCount < 2)
  1938. {
  1939. EnsureValidPageContainerPosition();
  1940. }
  1941. PDFPageRange oldPageRange = m_CurrentPageRange;
  1942. m_CurrentPageRange = GetVisiblePageRange();
  1943. if (!m_Internal.PageInputField.isFocused)
  1944. {
  1945. int p = GetMostVisiblePageIndex() + 1;
  1946. m_Internal.PageInputField.text = p.ToString();
  1947. SetPageCountLabel(p, m_PageCount);
  1948. }
  1949. if (!oldPageRange.Equals(m_CurrentPageRange) && m_CurrentPageRange.IsValid
  1950. || zoomHasChanged && m_CurrentPageRange.IsValid)
  1951. {
  1952. float scale = Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality);
  1953. PDFPageRange.UpdatePageAgainstRanges(oldPageRange, m_CurrentPageRange, m_Document, m_PageTextureHolders, m_RenderSettings, scale, this, m_NormalPageSizes);
  1954. }
  1955. int mostVisible = GetMostVisiblePageIndex();
  1956. if (m_PreviousMostVisiblePage != mostVisible)
  1957. {
  1958. NotifyCurrentPageChanged(m_PreviousMostVisiblePage, mostVisible);
  1959. m_PreviousMostVisiblePage = GetMostVisiblePageIndex();
  1960. }
  1961. if (!oldPageRange.Equals(m_CurrentPageRange))
  1962. {
  1963. AdjustCurrentSearchResultDisplayed();
  1964. }
  1965. if (Math.Abs(m_PreviousZoomToGo - m_ZoomToGo) > float.Epsilon)
  1966. {
  1967. NotifyZoomChanged(m_PreviousZoomToGo, m_ZoomToGo);
  1968. }
  1969. if (m_UpdateChangeDelay != 0.0f && !zoomHasChanged)
  1970. {
  1971. m_UpdateChangeDelay -= Time.deltaTime;
  1972. if (m_UpdateChangeDelay <= 0.0f)
  1973. {
  1974. m_UpdateChangeDelay = 0.0f;
  1975. for (int i = m_CurrentPageRange.m_From; i < m_CurrentPageRange.m_To; ++i)
  1976. {
  1977. #if UNITY_WEBGL
  1978. m_PageTextureHolders[i].m_Visible = true;
  1979. if (m_PageTextureHolders[i].m_RenderingStarted)
  1980. continue;
  1981. int w = (int)(m_NormalPageSizes[i].x * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1982. int h = (int)(m_NormalPageSizes[i].y * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1983. m_PageTextureHolders[i].m_RenderingStarted = true;
  1984. StartCoroutine(UpdatePageRangeTextures(i, w, h));
  1985. #else
  1986. if (m_PageTextureHolders[i].Texture != null)
  1987. {
  1988. Texture2D tex = m_PageTextureHolders[i].Texture;
  1989. m_PageTextureHolders[i].Texture = null;
  1990. Destroy(tex);
  1991. }
  1992. int w = (int)(m_Document.GetPageWidth(i) * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1993. int h = (int)(m_Document.GetPageHeight(i) * Mathf.Min(m_ZoomToGo, m_MaxZoomFactorTextureQuality));
  1994. if (m_Renderer == null)
  1995. m_Renderer = new PDFRenderer();
  1996. Texture2D newTex = m_Renderer.RenderPageToTexture(m_Document.GetPage(i), w, h, this, m_RenderSettings);
  1997. m_PageTextureHolders[i].Texture = newTex;
  1998. #endif
  1999. }
  2000. }
  2001. }
  2002. m_PreviousZoom = m_ZoomFactor;
  2003. m_PreviousZoomToGo = m_ZoomToGo;
  2004. m_PreviousPageFitting = m_PageFitting;
  2005. if (m_ThumbnailsViewer.gameObject.activeInHierarchy)
  2006. m_ThumbnailsViewer.DoUpdate();
  2007. if (m_BookmarksViewer.gameObject.activeInHierarchy)
  2008. m_BookmarksViewer.DoUpdate();
  2009. }
  2010. #if UNITY_WEBGL
  2011. private IEnumerator UpdatePageRangeTextures(int pageIndex, int w, int h)
  2012. {
  2013. PDFJS_Promise<PDFPage> pagePromise = m_Document.GetPageAsync(pageIndex);
  2014. while (!pagePromise.HasFinished)
  2015. yield return null;
  2016. if (pagePromise.HasSucceeded)
  2017. {
  2018. PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToTextureAsync(pagePromise.Result, w, h);
  2019. m_PageTextureHolders[pageIndex].m_RenderingPromise = renderPromise;
  2020. while (!renderPromise.HasFinished)
  2021. yield return null;
  2022. m_PageTextureHolders[pageIndex].m_RenderingPromise = null;
  2023. m_PageTextureHolders[pageIndex].m_RenderingStarted = false;
  2024. if (renderPromise.HasSucceeded)
  2025. {
  2026. if (m_PageTextureHolders[pageIndex].Texture != null && m_PageTextureHolders[pageIndex].Texture != renderPromise.Result)
  2027. {
  2028. Destroy(m_PageTextureHolders[pageIndex].Texture);
  2029. m_PageTextureHolders[pageIndex].Texture = null;
  2030. }
  2031. if (m_PageTextureHolders[pageIndex].m_Visible)
  2032. {
  2033. m_PageTextureHolders[pageIndex].Texture = renderPromise.Result;
  2034. }
  2035. else
  2036. {
  2037. Destroy(renderPromise.Result);
  2038. renderPromise.Result = null;
  2039. }
  2040. }
  2041. }
  2042. else
  2043. {
  2044. m_PageTextureHolders[pageIndex].m_RenderingPromise = null;
  2045. m_PageTextureHolders[pageIndex].m_RenderingStarted = false;
  2046. }
  2047. }
  2048. #endif
  2049. public Vector2[] GetCachedNormalPageSizes()
  2050. {
  2051. return m_NormalPageSizes;
  2052. }
  2053. private void UpdatePagesPlacement()
  2054. {
  2055. if (m_PageTextureHolders == null || m_PageSizes == null)
  2056. return;
  2057. foreach (PDFPageTextureHolder holder in m_PageTextureHolders)
  2058. {
  2059. RectTransform holderRectTransform = (RectTransform)holder.m_Page.transform;
  2060. holderRectTransform.sizeDelta = m_PageSizes[holder.m_PageIndex];
  2061. holder.RefreshTexture();
  2062. Vector3 newPosition = holderRectTransform.anchoredPosition3D;
  2063. newPosition.x = 0;
  2064. newPosition.y = -m_PageOffsets[holder.m_PageIndex];
  2065. newPosition.z = 0;
  2066. holderRectTransform.anchoredPosition3D = newPosition;
  2067. }
  2068. }
  2069. private void UpdateScrollBarVisibility()
  2070. {
  2071. bool vScrollVisible = true;
  2072. bool hScrollVisible = true;
  2073. if (Application.isPlaying)
  2074. {
  2075. vScrollVisible = m_Internal.PageContainer.sizeDelta.y > m_Internal.Viewport.rect.size.y;
  2076. hScrollVisible = m_Internal.PageContainer.sizeDelta.x > m_Internal.Viewport.rect.size.x;
  2077. }
  2078. vScrollVisible = vScrollVisible && m_ShowVerticalScrollBar;
  2079. hScrollVisible = hScrollVisible && m_ShowHorizontalScrollBar;
  2080. if (!hScrollVisible)
  2081. {
  2082. m_Internal.Viewport.offsetMin = new Vector2(m_Internal.Viewport.offsetMin.x, 0.0f);
  2083. m_Internal.Viewport.GetComponent<ScrollRect>().horizontalScrollbar = null;
  2084. m_Internal.HorizontalScrollBar.gameObject.SetActive(false);
  2085. }
  2086. else
  2087. {
  2088. m_Internal.Viewport.offsetMin = new Vector2(m_Internal.Viewport.offsetMin.x, 20.0f);
  2089. m_Internal.Viewport.GetComponent<ScrollRect>().horizontalScrollbar = m_Internal.HorizontalScrollBar.GetComponent<Scrollbar>();
  2090. m_Internal.HorizontalScrollBar.gameObject.SetActive(true);
  2091. }
  2092. if (!vScrollVisible)
  2093. {
  2094. m_Internal.Viewport.offsetMax = new Vector2(0.0f, m_Internal.Viewport.offsetMax.y);
  2095. m_Internal.Viewport.GetComponent<ScrollRect>().verticalScrollbar = null;
  2096. m_Internal.VerticalScrollBar.gameObject.SetActive(false);
  2097. }
  2098. else
  2099. {
  2100. m_Internal.Viewport.offsetMax = new Vector2(-20.0f, m_Internal.Viewport.offsetMax.y);
  2101. m_Internal.Viewport.GetComponent<ScrollRect>().verticalScrollbar = m_Internal.VerticalScrollBar.GetComponent<Scrollbar>();
  2102. m_Internal.VerticalScrollBar.gameObject.SetActive(true);
  2103. }
  2104. if (hScrollVisible && vScrollVisible)
  2105. {
  2106. m_Internal.VerticalScrollBar.offsetMin = new Vector2(m_Internal.VerticalScrollBar.offsetMin.x, 19.0f);
  2107. m_Internal.HorizontalScrollBar.offsetMax = new Vector2(-19.0f,
  2108. m_Internal.HorizontalScrollBar.offsetMax.y);
  2109. if (!m_Internal.ScrollCorner.gameObject.activeInHierarchy)
  2110. m_Internal.ScrollCorner.gameObject.SetActive(true);
  2111. }
  2112. else if (!hScrollVisible)
  2113. {
  2114. m_Internal.VerticalScrollBar.offsetMin = new Vector2(m_Internal.VerticalScrollBar.offsetMin.x, 0.0f);
  2115. if (m_Internal.ScrollCorner.gameObject.activeInHierarchy)
  2116. m_Internal.ScrollCorner.gameObject.SetActive(false);
  2117. }
  2118. else
  2119. {
  2120. m_Internal.HorizontalScrollBar.offsetMax = new Vector2(0.0f,
  2121. m_Internal.HorizontalScrollBar.offsetMax.y);
  2122. if (m_Internal.ScrollCorner.gameObject.activeInHierarchy)
  2123. m_Internal.ScrollCorner.gameObject.SetActive(false);
  2124. }
  2125. }
  2126. private void ZoomCommon(Vector2 zoomPosition, bool zoomIn, bool useSpecificZoom = false, float specificZoom = 1.0f)
  2127. {
  2128. if (m_Document == null || !m_Document.IsValid)
  2129. return;
  2130. m_ZoomPosition = zoomPosition;
  2131. if (useSpecificZoom)
  2132. {
  2133. m_ZoomToGo = specificZoom;
  2134. }
  2135. else
  2136. {
  2137. float step = m_ZoomStep;
  2138. if (m_ZoomToGo >= 2.0f)
  2139. step *= 2.0f;
  2140. if (m_ZoomToGo >= 4.0f)
  2141. step *= 2.0f;
  2142. float epsilon = m_ZoomStep * 0.125f;
  2143. if (zoomIn)
  2144. {
  2145. if (!Mathf.Approximately(Mathf.Floor(m_ZoomToGo * (1 / step)), m_ZoomToGo * (1 / step))
  2146. && m_ZoomToGo * (1 / step) <= Mathf.Floor(m_ZoomToGo * (1 / step)))
  2147. {
  2148. m_ZoomToGo = Mathf.Floor(m_ZoomToGo * (1 / step)) * step;
  2149. }
  2150. m_ZoomToGo = Mathf.Clamp(Mathf.Floor((m_ZoomToGo + step) * (1 / step) + epsilon) * step, m_MinZoomFactor, m_MaxZoomFactor);
  2151. }
  2152. else
  2153. {
  2154. if (!Mathf.Approximately(Mathf.Floor(m_ZoomToGo * (1 / step)), m_ZoomToGo * (1 / step))
  2155. && m_ZoomToGo * (1 / step) >= Mathf.Floor(m_ZoomToGo * (1 / step)))
  2156. {
  2157. m_ZoomToGo = Mathf.Floor(m_ZoomToGo * (1 / step)) * step;
  2158. }
  2159. m_ZoomToGo = Mathf.Clamp(Mathf.Floor((m_ZoomToGo - step) * (1 / step) + epsilon) * step, m_MinZoomFactor, m_MaxZoomFactor);
  2160. }
  2161. }
  2162. m_PageFitting = PageFittingType.Zoom;
  2163. }
  2164. public Vector2 GetDevicePageSize(int pageIndex)
  2165. {
  2166. return m_PageSizes[pageIndex];
  2167. }
  2168. public IPDFDeviceActionHandler GetBookmarksActionHandler()
  2169. {
  2170. #if UNITY_WEBGL && !UNITY_EDITOR
  2171. return null;
  2172. #else
  2173. return m_BookmarksActionHandler;
  2174. #endif
  2175. }
  2176. public IPDFDeviceActionHandler GetLinksActionHandler()
  2177. {
  2178. #if UNITY_WEBGL && !UNITY_EDITOR
  2179. return null;
  2180. #else
  2181. return m_LinksActionHandler;
  2182. #endif
  2183. }
  2184. public IList<PDFColoredRect> GetBackgroundColoredRectList(PDFPage page)
  2185. {
  2186. #if !UNITY_WEBGL
  2187. if (m_SearchResults != null && m_SearchResults[page.PageIndex] != null &&
  2188. m_SearchResults[page.PageIndex].Count > 0)
  2189. {
  2190. using (PDFTextPage textPage = page.GetTextPage())
  2191. {
  2192. List<PDFColoredRect> coloredRectList = new List<PDFColoredRect>();
  2193. foreach (PDFSearchResult result in m_SearchResults[page.PageIndex])
  2194. {
  2195. int pageRectCount = textPage.CountRects(result.StartIndex, result.Count);
  2196. for (int j = 0; j < pageRectCount; ++j)
  2197. {
  2198. Rect rect = textPage.GetRect(j);
  2199. rect = new Rect(
  2200. rect.xMin - m_SearchResultPadding.x,
  2201. rect.yMin + m_SearchResultPadding.y,
  2202. rect.width + 2 * m_SearchResultPadding.x,
  2203. rect.height + 2 * m_SearchResultPadding.y);
  2204. coloredRectList.Add(new PDFColoredRect(rect, m_SearchResultColor));
  2205. }
  2206. }
  2207. return coloredRectList;
  2208. }
  2209. }
  2210. #endif
  2211. return null;
  2212. }
  2213. public void ZoomOnParagraph(PDFViewerPage viewerPage, Rect pageRect)
  2214. {
  2215. if (m_Document == null || !m_Document.IsValid)
  2216. return;
  2217. #if !UNITY_WEBGL
  2218. Vector3[] pageCorners = new Vector3[4];
  2219. (viewerPage.transform as RectTransform).GetWorldCorners(pageCorners);
  2220. Vector2 min = pageCorners[0];
  2221. Vector2 max = pageCorners[0];
  2222. for (int i = 1; i < 4; ++i)
  2223. {
  2224. if (pageCorners[i].x < min.x)
  2225. min.x = pageCorners[i].x;
  2226. if (pageCorners[i].y < min.y)
  2227. min.y = pageCorners[i].y;
  2228. if (pageCorners[i].x > max.x)
  2229. max.x = pageCorners[i].x;
  2230. if (pageCorners[i].y > max.y)
  2231. max.y = pageCorners[i].y;
  2232. }
  2233. Vector2 devicePageSize = (viewerPage.transform as RectTransform).sizeDelta;
  2234. Rect deviceRect = viewerPage.m_Page.ConvertPageRectToDeviceRect(pageRect, devicePageSize);
  2235. float deviceRectCenterPosition = deviceRect.max.y + (deviceRect.min - deviceRect.max).y * 0.5f;
  2236. m_Internal.PageContainer.anchoredPosition = new Vector2(
  2237. m_Internal.PageContainer.anchoredPosition.x,
  2238. m_PageOffsets[viewerPage.m_Page.PageIndex]
  2239. - m_PageSizes[viewerPage.m_Page.PageIndex].y * 0.5f
  2240. + (m_PageSizes[viewerPage.m_Page.PageIndex].y - deviceRectCenterPosition)
  2241. - m_Internal.Viewport.rect.size.y * 0.5f);
  2242. if (m_ZoomToGo < m_ParagraphZoomFactor)
  2243. ZoomCommon(new Vector2(0.0f, m_Internal.Viewport.rect.size.y * 0.5f), true, true, m_ParagraphZoomFactor);
  2244. #endif
  2245. }
  2246. protected override void OnTransformParentChanged()
  2247. {
  2248. base.OnTransformParentChanged();
  2249. if (gameObject != null)
  2250. m_Canvas = null;
  2251. }
  2252. private void CacheCanvas()
  2253. {
  2254. if (gameObject == null)
  2255. return;
  2256. gameObject.GetComponentsInParent(false, m_CanvasList);
  2257. if (m_CanvasList.Count > 0)
  2258. {
  2259. // Find the first active and enabled canvas.
  2260. for (int i = 0; i < m_CanvasList.Count; ++i)
  2261. {
  2262. if (m_CanvasList[i].isActiveAndEnabled)
  2263. {
  2264. m_Canvas = m_CanvasList[i];
  2265. m_GraphicRaycaster = m_Canvas.GetComponent<GraphicRaycaster>();
  2266. break;
  2267. }
  2268. }
  2269. }
  2270. else
  2271. {
  2272. m_Canvas = null;
  2273. m_GraphicRaycaster = null;
  2274. }
  2275. m_CanvasList.Clear();
  2276. }
  2277. }
  2278. }