PDFRenderer.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using Paroxe.PdfRenderer.Internal;
  5. using UnityEngine;
  6. using Paroxe.PdfRenderer.WebGL;
  7. using System.Collections; // For WebGL
  8. namespace Paroxe.PdfRenderer
  9. {
  10. /// <summary>
  11. /// This class allow the application to render pages into textures.
  12. /// </summary>
  13. public class PDFRenderer : IDisposable
  14. {
  15. private bool m_Disposed;
  16. #if !UNITY_WEBGL || UNITY_EDITOR
  17. private PDFBitmap m_Bitmap;
  18. private byte[] m_IntermediateBuffer;
  19. #endif
  20. public PDFRenderer()
  21. {
  22. PDFLibrary.AddRef("PDFRenderer");
  23. }
  24. ~PDFRenderer()
  25. {
  26. Dispose(false);
  27. }
  28. public void Dispose()
  29. {
  30. Dispose(true);
  31. GC.SuppressFinalize(this);
  32. }
  33. protected virtual void Dispose(bool disposing)
  34. {
  35. if (!m_Disposed)
  36. {
  37. if (disposing)
  38. {
  39. #if !UNITY_WEBGL || UNITY_EDITOR
  40. m_Bitmap.Dispose();
  41. m_Bitmap = null;
  42. #endif
  43. }
  44. PDFLibrary.RemoveRef("PDFRenderer");
  45. m_Disposed = true;
  46. }
  47. }
  48. #if UNITY_WEBGL
  49. public class RenderPageParameters
  50. {
  51. public IntPtr pageHandle;
  52. public Texture2D existingTexture;
  53. public Vector2 newTextureSize;
  54. public RenderPageParameters(IntPtr pageHandle, Texture2D existingTexture, Vector2 newTextureSize)
  55. {
  56. this.pageHandle = pageHandle;
  57. this.existingTexture = existingTexture;
  58. this.newTextureSize = newTextureSize;
  59. }
  60. }
  61. #endif
  62. public static PDFJS_Promise<Texture2D> RenderPageToExistingTextureAsync(PDFPage page, Texture2D tex)
  63. {
  64. PDFJS_Promise<Texture2D> renderPromise = new PDFJS_Promise<Texture2D>();
  65. #if !UNITY_WEBGL || UNITY_EDITOR
  66. using (PDFRenderer renderer = new PDFRenderer())
  67. {
  68. renderPromise.HasFinished = true;
  69. renderPromise.HasSucceeded = true;
  70. renderPromise.HasReceivedJSResponse = true;
  71. renderer.RenderPageToExistingTexture(page, tex);
  72. renderPromise.Result = tex;
  73. }
  74. #else
  75. RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, tex, new Vector2(tex.width, tex.height));
  76. PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
  77. #endif
  78. return renderPromise;
  79. }
  80. public static PDFJS_Promise<Texture2D> RenderPageToTextureAsync(PDFPage page, int width, int height)
  81. {
  82. return RenderPageToTextureAsync(page, new Vector2(width, height));
  83. }
  84. public static PDFJS_Promise<Texture2D> RenderPageToTextureAsync(PDFPage page, Vector2 size)
  85. {
  86. PDFJS_Promise<Texture2D> renderPromise = new PDFJS_Promise<Texture2D>();
  87. #if !UNITY_WEBGL || UNITY_EDITOR
  88. using (PDFRenderer renderer = new PDFRenderer())
  89. {
  90. renderPromise.HasFinished = true;
  91. renderPromise.HasSucceeded = true;
  92. renderPromise.HasReceivedJSResponse = true;
  93. renderPromise.Result = renderer.RenderPageToTexture(page, (int)size.x, (int)size.y);
  94. }
  95. #else
  96. RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, null, size);
  97. PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
  98. #endif
  99. return renderPromise;
  100. }
  101. public static PDFJS_Promise<Texture2D> RenderPageToTextureAsync(PDFPage page, float scale = 1.0f)
  102. {
  103. PDFJS_Promise<Texture2D> renderPromise = new PDFJS_Promise<Texture2D>();
  104. #if !UNITY_WEBGL || UNITY_EDITOR
  105. using (PDFRenderer renderer = new PDFRenderer())
  106. {
  107. renderPromise.HasFinished = true;
  108. renderPromise.HasSucceeded = true;
  109. renderPromise.HasReceivedJSResponse = true;
  110. Vector2 size = page.GetPageSize(scale);
  111. renderPromise.Result = renderer.RenderPageToTexture(page, (int)size.x, (int)size.y);
  112. }
  113. #else
  114. RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, null, page.GetPageSize(scale));
  115. PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
  116. #endif
  117. return renderPromise;
  118. }
  119. #if UNITY_WEBGL && !UNITY_EDITOR
  120. private static IEnumerator RenderPageCoroutine(PDFJS_PromiseCoroutine promiseCoroutine, IPDFJS_Promise promise, object parameters)
  121. {
  122. PDFJS_Promise<PDFJS_WebGLCanvas> renderToCanvasPromise = new PDFJS_Promise<PDFJS_WebGLCanvas>();
  123. PDFJS_Library.Instance.PreparePromiseCoroutine(null, renderToCanvasPromise, null);
  124. IntPtr pageHandle = ((RenderPageParameters)parameters).pageHandle;
  125. Texture2D texture = ((RenderPageParameters)parameters).existingTexture;
  126. Vector2 newtextureSize = ((RenderPageParameters)parameters).newTextureSize;
  127. Vector2 pageSize = PDFPage.GetPageSize(pageHandle, 1.0f);
  128. float scale = 1.0f;
  129. float width = 0.0f;
  130. float height = 0.0f;
  131. if (texture != null)
  132. {
  133. float wf = pageSize.x / texture.width;
  134. float hf = pageSize.y / texture.height;
  135. width = texture.width;
  136. height = texture.height;
  137. scale = 1.0f / Mathf.Max(wf, hf);
  138. }
  139. else
  140. {
  141. float wf = pageSize.x / newtextureSize.x;
  142. float hf = pageSize.y / newtextureSize.y;
  143. width = newtextureSize.x;
  144. height = newtextureSize.y;
  145. scale = 1.0f / Mathf.Max(wf, hf);
  146. }
  147. NativeMethods.PDFJS_RenderPageIntoCanvas(renderToCanvasPromise.PromiseHandle, pageHandle.ToInt32(), scale, width, height);
  148. while (!renderToCanvasPromise.HasReceivedJSResponse)
  149. yield return null;
  150. if (renderToCanvasPromise.HasSucceeded)
  151. {
  152. int canvasHandle = int.Parse(renderToCanvasPromise.JSObjectHandle);
  153. using (PDFJS_WebGLCanvas canvas = new PDFJS_WebGLCanvas(new IntPtr(canvasHandle)))
  154. {
  155. PDFJS_Promise<Texture2D> renderToTexturePromise = promise as PDFJS_Promise<Texture2D>;
  156. if (texture == null)
  157. {
  158. texture = new Texture2D((int)newtextureSize.x, (int)newtextureSize.y, TextureFormat.ARGB32, false);
  159. texture.filterMode = FilterMode.Bilinear;
  160. texture.Apply();
  161. }
  162. NativeMethods.PDFJS_RenderCanvasIntoTexture(canvasHandle, texture.GetNativeTexturePtr().ToInt32());
  163. renderToTexturePromise.Result = texture;
  164. renderToTexturePromise.HasSucceeded = true;
  165. renderToTexturePromise.HasFinished = true;
  166. promiseCoroutine.ExecuteThenAction(true, texture);
  167. }
  168. }
  169. else
  170. {
  171. PDFJS_Promise<Texture2D> renderToTexturePromise = promise as PDFJS_Promise<Texture2D>;
  172. renderToTexturePromise.Result = null;
  173. renderToTexturePromise.HasSucceeded = false;
  174. renderToTexturePromise.HasFinished = true;
  175. promiseCoroutine.ExecuteThenAction(false, null);
  176. }
  177. }
  178. #endif
  179. #if !UNITY_WEBGL || UNITY_EDITOR
  180. /// <summary>
  181. /// Render page into a new byte array.
  182. /// </summary>
  183. /// <param name="page"></param>
  184. /// <returns></returns>
  185. #if !UNITY_WEBGL
  186. public
  187. #else
  188. private
  189. #endif
  190. byte[] RenderPageToByteArray(PDFPage page)
  191. {
  192. return RenderPageToByteArray(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y, null,
  193. RenderSettings.defaultRenderSettings);
  194. }
  195. /// <summary>
  196. /// Render page into a new byte array.
  197. /// </summary>
  198. /// <param name="page"></param>
  199. /// <param name="width"></param>
  200. /// <param name="height"></param>
  201. /// <returns></returns>
  202. #if !UNITY_WEBGL
  203. public
  204. #else
  205. private
  206. #endif
  207. byte[] RenderPageToByteArray(PDFPage page, int width, int height)
  208. {
  209. return RenderPageToByteArray(page, width, height, null, RenderSettings.defaultRenderSettings);
  210. }
  211. /// <summary>
  212. /// Render page into a new byte array.
  213. /// </summary>
  214. /// <param name="page"></param>
  215. /// <param name="width"></param>
  216. /// <param name="height"></param>
  217. /// <param name="rectsProvider"></param>
  218. /// <returns></returns>
  219. #if !UNITY_WEBGL
  220. public
  221. #else
  222. private
  223. #endif
  224. byte[] RenderPageToByteArray(PDFPage page, int width, int height,
  225. IPDFColoredRectListProvider rectsProvider)
  226. {
  227. return RenderPageToByteArray(page, width, height, rectsProvider, RenderSettings.defaultRenderSettings);
  228. }
  229. /// <summary>
  230. /// Render page into a new byte array.
  231. /// </summary>
  232. /// <param name="page"></param>
  233. /// <param name="width"></param>
  234. /// <param name="height"></param>
  235. /// <param name="rectsProvider"></param>
  236. /// <param name="settings"></param>
  237. /// <returns></returns>
  238. #if !UNITY_WEBGL
  239. public
  240. #else
  241. private
  242. #endif
  243. byte[] RenderPageToByteArray(PDFPage page, int width, int height,
  244. IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
  245. {
  246. if (settings == null)
  247. settings = RenderSettings.defaultRenderSettings;
  248. if (m_Bitmap == null || m_Bitmap.UseAlphaChannel != settings.transparentBackground || !m_Bitmap.HasSameSize(width, height))
  249. {
  250. if (m_Bitmap != null)
  251. m_Bitmap.Dispose();
  252. m_Bitmap = new PDFBitmap(width, height, settings.transparentBackground);
  253. }
  254. m_Bitmap.FillRect(0, 0, width, height, settings.transparentBackground ? 0x00000000 : int.MaxValue);
  255. int flags = settings == null
  256. ? RenderSettings.defaultRenderSettings.ComputeRenderingFlags()
  257. : settings.ComputeRenderingFlags();
  258. float scale = width / page.GetPageSize(1.0f).x;
  259. PDFRect clipping = new PDFRect(width, height);
  260. PDFMatrix matrix = PDFMatrix.Identity;
  261. matrix.Scale(scale, -scale);
  262. matrix.Translate(0.0f, height);
  263. NativeMethods.FPDF_RenderPageBitmapWithMatrix(m_Bitmap.NativePointer, page.NativePointer, ref matrix, ref clipping, flags);
  264. IntPtr bufferPtr = m_Bitmap.GetBuffer();
  265. if (bufferPtr == IntPtr.Zero)
  266. return null;
  267. int length = width * height * 4;
  268. if (m_IntermediateBuffer == null || m_IntermediateBuffer.Length < length)
  269. m_IntermediateBuffer = new byte[width * height * 4];
  270. Marshal.Copy(bufferPtr, m_IntermediateBuffer, 0, width * height * 4);
  271. #if !UNITY_WEBGL
  272. IList<PDFColoredRect> coloredRects = rectsProvider != null
  273. ? rectsProvider.GetBackgroundColoredRectList(page)
  274. : null;
  275. if (coloredRects != null && coloredRects.Count > 0)
  276. {
  277. foreach (PDFColoredRect coloredRect in coloredRects)
  278. {
  279. var r = (int)(coloredRect.Color.r * 255) & 0xFF;
  280. var g = (int)(coloredRect.Color.g * 255) & 0xFF;
  281. var b = (int)(coloredRect.Color.b * 255) & 0xFF;
  282. var a = (int)(coloredRect.Color.a * 255) & 0xFF;
  283. float alpha = (a / (float)255);
  284. float reverseAlpha = 1.0f - alpha;
  285. Rect deviceRect = page.ConvertPageRectToDeviceRect(coloredRect.PageRect, new Vector2(width, height));
  286. if (deviceRect.x >= 0.0f
  287. && deviceRect.y >= 0.0f
  288. && deviceRect.x + deviceRect.width <= width
  289. && deviceRect.y + deviceRect.height <= height)
  290. {
  291. for (int y = 0; y < (int)deviceRect.height; ++y)
  292. {
  293. for (int x = 0; x < (int)deviceRect.width; ++x)
  294. {
  295. int s = (((height - (int)deviceRect.y) - y) * width + (int)deviceRect.x + x) * 4;
  296. var sr = m_IntermediateBuffer[s];
  297. var sg = m_IntermediateBuffer[s + 1];
  298. var sb = m_IntermediateBuffer[s + 2];
  299. m_IntermediateBuffer[s] = (byte)Mathf.Clamp(alpha * r + (reverseAlpha * sr), 0, 255);
  300. m_IntermediateBuffer[s + 1] = (byte)Mathf.Clamp(alpha * g + (reverseAlpha * sg), 0, 255);
  301. m_IntermediateBuffer[s + 2] = (byte)Mathf.Clamp(alpha * b + (reverseAlpha * sb), 0, 255);
  302. m_IntermediateBuffer[s + 3] = 0xFF;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. #endif
  309. return m_IntermediateBuffer;
  310. }
  311. [StructLayout(LayoutKind.Sequential)]
  312. public struct PDFMatrix
  313. {
  314. public static PDFMatrix Identity
  315. {
  316. get { return new PDFMatrix { a = 1, b = 0, c = 0, d = 1, e = 0, f = 0 }; }
  317. }
  318. public float a;
  319. public float b;
  320. public float c;
  321. public float d;
  322. public float e;
  323. public float f;
  324. public PDFMatrix(float[] n)
  325. {
  326. if (n == null)
  327. throw new ArgumentNullException("n");
  328. if (n.Length != 6)
  329. throw new ArgumentException("n must have 6 elements", "n");
  330. a = n[0];
  331. b = n[1];
  332. c = n[2];
  333. d = n[3];
  334. e = n[4];
  335. f = n[5];
  336. }
  337. public PDFMatrix(float a, float b, float c, float d, float e, float f)
  338. {
  339. this.a = a;
  340. this.b = b;
  341. this.c = c;
  342. this.d = d;
  343. this.e = e;
  344. this.f = f;
  345. }
  346. public void SetIdentity()
  347. {
  348. a = 1;
  349. b = 0;
  350. c = 0;
  351. d = 1;
  352. e = 0;
  353. f = 0;
  354. }
  355. public void Scale(float sx, float sy, bool prepended = false)
  356. {
  357. a *= sx;
  358. d *= sy;
  359. if (prepended)
  360. {
  361. b *= sx;
  362. c *= sy;
  363. return;
  364. }
  365. b *= sy;
  366. c *= sx;
  367. e *= sx;
  368. f *= sy;
  369. }
  370. public void Translate(float x, float y, bool prepended = false)
  371. {
  372. if (prepended)
  373. {
  374. e += x * a + y * c;
  375. f += y * d + x * b;
  376. return;
  377. }
  378. e += x;
  379. f += y;
  380. }
  381. public void Rotate(float radian, bool prepended = false)
  382. {
  383. float cosValue = Mathf.Cos(radian);
  384. float sinValue = Mathf.Sin(radian);
  385. ConcatInternal(new PDFMatrix(cosValue, sinValue, -sinValue, cosValue, 0, 0), prepended);
  386. }
  387. public void RotateAt(float fRadian, float dx, float dy, bool prepended = false)
  388. {
  389. Translate(dx, dy, prepended);
  390. Rotate(fRadian, prepended);
  391. Translate(-dx, -dy, prepended);
  392. }
  393. private void ConcatInternal(PDFMatrix other, bool prepend)
  394. {
  395. PDFMatrix left;
  396. PDFMatrix right;
  397. if (prepend)
  398. {
  399. left = other;
  400. right = this;
  401. }
  402. else
  403. {
  404. left = this;
  405. right = other;
  406. }
  407. a = left.a * right.a + left.b * right.c;
  408. b = left.a * right.b + left.b * right.d;
  409. c = left.c * right.a + left.d * right.c;
  410. d = left.c * right.b + left.d * right.d;
  411. e = left.e * right.a + left.f * right.c + right.e;
  412. f = left.e * right.b + left.f * right.d + right.f;
  413. }
  414. public float this[int index]
  415. {
  416. get
  417. {
  418. float num;
  419. switch (index)
  420. {
  421. case 0:
  422. {
  423. num = a;
  424. break;
  425. }
  426. case 1:
  427. {
  428. num = b;
  429. break;
  430. }
  431. case 2:
  432. {
  433. num = c;
  434. break;
  435. }
  436. case 3:
  437. {
  438. num = d;
  439. break;
  440. }
  441. case 4:
  442. {
  443. num = e;
  444. break;
  445. }
  446. case 5:
  447. {
  448. num = f;
  449. break;
  450. }
  451. default:
  452. {
  453. throw new IndexOutOfRangeException(string.Format("Invalid PDFMatrix index addressed: {0}!", new object[] { index }));
  454. }
  455. }
  456. return num;
  457. }
  458. set
  459. {
  460. switch (index)
  461. {
  462. case 0:
  463. {
  464. a = value;
  465. break;
  466. }
  467. case 1:
  468. {
  469. b = value;
  470. break;
  471. }
  472. case 2:
  473. {
  474. c = value;
  475. break;
  476. }
  477. case 3:
  478. {
  479. d = value;
  480. break;
  481. }
  482. case 4:
  483. {
  484. e = value;
  485. break;
  486. }
  487. case 5:
  488. {
  489. f = value;
  490. break;
  491. }
  492. default:
  493. {
  494. throw new IndexOutOfRangeException(string.Format("Invalid PDFMatrix index addressed: {0}!", new object[] { index }));
  495. }
  496. }
  497. }
  498. }
  499. public override bool Equals(object other)
  500. {
  501. return (other is PDFMatrix ? this == (PDFMatrix)other : false);
  502. }
  503. public static bool operator ==(PDFMatrix lhs, PDFMatrix rhs)
  504. {
  505. return (lhs.a != rhs.a || lhs.b != rhs.b || lhs.c != rhs.c || lhs.d != rhs.d || lhs.e != rhs.e ? false : lhs.f == rhs.f);
  506. }
  507. public static bool operator !=(PDFMatrix lhs, PDFMatrix rhs)
  508. {
  509. return !(lhs == rhs);
  510. }
  511. public override int GetHashCode()
  512. {
  513. unchecked
  514. {
  515. int hashCode = this.a.GetHashCode();
  516. hashCode = hashCode * 23 + b.GetHashCode();
  517. hashCode = hashCode * 23 + c.GetHashCode();
  518. hashCode = hashCode * 23 + d.GetHashCode();
  519. hashCode = hashCode * 23 + e.GetHashCode();
  520. hashCode = hashCode * 23 + f.GetHashCode();
  521. return hashCode;
  522. }
  523. }
  524. }
  525. [StructLayout(LayoutKind.Sequential)]
  526. public struct PDFRect
  527. {
  528. public float left;
  529. public float top;
  530. public float right;
  531. public float bottom;
  532. public PDFRect(float left, float top, float right, float bottom)
  533. {
  534. this.left = left;
  535. this.top = top;
  536. this.right = right;
  537. this.bottom = bottom;
  538. }
  539. public PDFRect(float width, float height)
  540. {
  541. left = 0;
  542. top = 0;
  543. right = width;
  544. bottom = height;
  545. }
  546. public override bool Equals(object other)
  547. {
  548. return (other is PDFRect ? this == (PDFRect)other : false);
  549. }
  550. public static bool operator ==(PDFRect lhs, PDFRect rhs)
  551. {
  552. return (lhs.left != rhs.left || lhs.top != rhs.top || lhs.right != rhs.right ? false : lhs.bottom == rhs.bottom);
  553. }
  554. public static bool operator !=(PDFRect lhs, PDFRect rhs)
  555. {
  556. return !(lhs == rhs);
  557. }
  558. public override int GetHashCode()
  559. {
  560. unchecked
  561. {
  562. int hashCode = this.left.GetHashCode();
  563. hashCode = hashCode * 23 + top.GetHashCode();
  564. hashCode = hashCode * 23 + right.GetHashCode();
  565. hashCode = hashCode * 23 + bottom.GetHashCode();
  566. return hashCode;
  567. }
  568. }
  569. }
  570. /// <summary>
  571. /// Render page into a new Texture2D.
  572. /// </summary>
  573. /// <param name="page"></param>
  574. /// <returns></returns>
  575. #if !UNITY_WEBGL
  576. public
  577. #else
  578. private
  579. #endif
  580. Texture2D RenderPageToTexture(PDFPage page)
  581. {
  582. return RenderPageToTexture(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y, null,
  583. RenderSettings.defaultRenderSettings);
  584. }
  585. /// <summary>
  586. /// Render page into a new Texture2D.
  587. /// </summary>
  588. /// <param name="page"></param>
  589. /// <param name="width"></param>
  590. /// <param name="height"></param>
  591. /// <returns></returns>
  592. #if !UNITY_WEBGL
  593. public
  594. #else
  595. private
  596. #endif
  597. Texture2D RenderPageToTexture(PDFPage page, int width, int height)
  598. {
  599. return RenderPageToTexture(page, width, height, null, RenderSettings.defaultRenderSettings);
  600. }
  601. /// <summary>
  602. /// Render page into a new Texture2D.
  603. /// </summary>
  604. /// <param name="page"></param>
  605. /// <param name="width"></param>
  606. /// <param name="height"></param>
  607. /// <param name="rectsProvider"></param>
  608. /// <returns></returns>
  609. #if !UNITY_WEBGL
  610. public
  611. #else
  612. private
  613. #endif
  614. Texture2D RenderPageToTexture(PDFPage page, int width, int height,
  615. IPDFColoredRectListProvider rectsProvider)
  616. {
  617. return RenderPageToTexture(page, width, height, rectsProvider, RenderSettings.defaultRenderSettings);
  618. }
  619. /// <summary>
  620. /// Render page into a new Texture2D.
  621. /// </summary>
  622. /// <param name="page"></param>
  623. /// <param name="width"></param>
  624. /// <param name="height"></param>
  625. /// <param name="rectsProvider"></param>
  626. /// <param name="settings"></param>
  627. /// <returns></returns>
  628. #if !UNITY_WEBGL
  629. public
  630. #else
  631. private
  632. #endif
  633. Texture2D RenderPageToTexture(PDFPage page, int width, int height,
  634. IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
  635. {
  636. Texture2D newTex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  637. RenderPageToExistingTexture(page, newTex, rectsProvider, settings);
  638. return newTex;
  639. }
  640. /// <summary>
  641. /// Render page into an existing Texture2D.
  642. /// </summary>
  643. /// <param name="page"></param>
  644. /// <param name="texture"></param>
  645. #if !UNITY_WEBGL
  646. public
  647. #else
  648. private
  649. #endif
  650. void RenderPageToExistingTexture(PDFPage page, Texture2D texture)
  651. {
  652. RenderPageToExistingTexture(page, texture, null, RenderSettings.defaultRenderSettings);
  653. }
  654. /// <summary>
  655. /// Render page into an existing Texture2D.
  656. /// </summary>
  657. /// <param name="page"></param>
  658. /// <param name="texture"></param>
  659. /// <param name="rectsProvider"></param>
  660. #if !UNITY_WEBGL
  661. public
  662. #else
  663. private
  664. #endif
  665. void RenderPageToExistingTexture(PDFPage page, Texture2D texture,
  666. IPDFColoredRectListProvider rectsProvider)
  667. {
  668. RenderPageToExistingTexture(page, texture, rectsProvider, RenderSettings.defaultRenderSettings);
  669. }
  670. /// <summary>
  671. /// Render page into an existing Texture2D.
  672. /// </summary>
  673. /// <param name="page"></param>
  674. /// <param name="texture"></param>
  675. /// <param name="rectsProvider"></param>
  676. /// <param name="settings"></param>
  677. #if !UNITY_WEBGL
  678. public
  679. #else
  680. private
  681. #endif
  682. void RenderPageToExistingTexture(PDFPage page, Texture2D texture,
  683. IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
  684. {
  685. byte[] byteArray = RenderPageToByteArray(page, texture.width, texture.height, rectsProvider, settings);
  686. if (byteArray != null)
  687. {
  688. texture.wrapMode = TextureWrapMode.Clamp;
  689. if ((texture.format != TextureFormat.RGBA32
  690. && texture.format != TextureFormat.ARGB32
  691. && texture.format != TextureFormat.BGRA32
  692. && texture.format != (TextureFormat)37) || texture.mipmapCount > 1)
  693. {
  694. Color32[] pixels = new Color32[texture.width * texture.height];
  695. for (int i = 0; i < pixels.Length; ++i)
  696. pixels[i] = new Color32(
  697. byteArray[i * 4],
  698. byteArray[i * 4 + 1],
  699. byteArray[i * 4 + 2],
  700. byteArray[i * 4 + 3]);
  701. texture.SetPixels32(pixels);
  702. texture.Apply();
  703. }
  704. else
  705. {
  706. texture.LoadRawTextureData(byteArray);
  707. texture.Apply();
  708. }
  709. }
  710. }
  711. #endif
  712. /// <summary>
  713. /// Allows the application to specify render settings.
  714. /// </summary>
  715. [Serializable]
  716. public class RenderSettings
  717. {
  718. public bool disableSmoothPath = false;
  719. public bool disableSmoothText = false;
  720. public bool disableSmoothImage = false;
  721. public bool grayscale = false;
  722. public bool optimizeTextForLCDDisplay = false;
  723. public bool renderAnnotations = false;
  724. public bool renderForPrinting = false;
  725. public bool transparentBackground = false;
  726. public static RenderSettings defaultRenderSettings
  727. {
  728. get { return new RenderSettings(); }
  729. }
  730. public int ComputeRenderingFlags()
  731. {
  732. int flags = 0x10;
  733. if (renderAnnotations)
  734. flags |= 0x01;
  735. if (optimizeTextForLCDDisplay)
  736. flags |= 0x02;
  737. if (grayscale)
  738. flags |= 0x08;
  739. if (renderForPrinting)
  740. flags |= 0x800;
  741. if (disableSmoothText)
  742. flags |= 0x1000;
  743. if (disableSmoothImage)
  744. flags |= 0x2000;
  745. if (disableSmoothPath)
  746. flags |= 0x4000;
  747. return flags;
  748. }
  749. }
  750. }
  751. }