PDFRenderer.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. http://www.cgsoso.com/forum-211-1.html
  3. CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
  4. CGSOSO 主打游戏开发,影视设计等CG资源素材。
  5. 插件如若商用,请务必官网购买!
  6. daily assets update for try.
  7. U should buy the asset from home store if u use it in your project!
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Runtime.InteropServices;
  12. using Paroxe.PdfRenderer.Internal;
  13. using UnityEngine;
  14. using System.Threading;
  15. using System.Collections;
  16. using Paroxe.PdfRenderer.WebGL;
  17. namespace Paroxe.PdfRenderer
  18. {
  19. /// <summary>
  20. /// This class allow the application to render pages into textures.
  21. /// </summary>
  22. public class PDFRenderer : IDisposable
  23. {
  24. private bool m_Disposed;
  25. #if !UNITY_WEBGL || UNITY_EDITOR
  26. private PDFBitmap m_Bitmap;
  27. private byte[] m_IntermediateBuffer;
  28. #endif
  29. public PDFRenderer()
  30. {
  31. PDFLibrary.AddRef("PDFRenderer");
  32. }
  33. ~PDFRenderer()
  34. {
  35. Dispose(false);
  36. }
  37. public void Dispose()
  38. {
  39. Dispose(true);
  40. GC.SuppressFinalize(this);
  41. }
  42. protected virtual void Dispose(bool disposing)
  43. {
  44. if (!m_Disposed)
  45. {
  46. if (disposing)
  47. {
  48. #if !UNITY_WEBGL || UNITY_EDITOR
  49. m_Bitmap.Dispose();
  50. m_Bitmap = null;
  51. #endif
  52. }
  53. PDFLibrary.RemoveRef("PDFRenderer");
  54. m_Disposed = true;
  55. }
  56. }
  57. #if UNITY_WEBGL
  58. public class RenderPageParameters
  59. {
  60. public IntPtr pageHandle;
  61. public Texture2D existingTexture;
  62. public Vector2 newTextureSize;
  63. public RenderPageParameters(IntPtr pageHandle, Texture2D existingTexture, Vector2 newTextureSize)
  64. {
  65. this.pageHandle = pageHandle;
  66. this.existingTexture = existingTexture;
  67. this.newTextureSize = newTextureSize;
  68. }
  69. }
  70. #endif
  71. public static PDFJS_Promise<Texture2D> RenderPageToExistingTextureAsync(PDFPage page, Texture2D tex)
  72. {
  73. PDFJS_Promise<Texture2D> renderPromise = new PDFJS_Promise<Texture2D>();
  74. #if !UNITY_WEBGL || UNITY_EDITOR
  75. using (PDFRenderer renderer = new PDFRenderer())
  76. {
  77. renderPromise.HasFinished = true;
  78. renderPromise.HasSucceeded = true;
  79. renderPromise.HasReceivedJSResponse = true;
  80. renderer.RenderPageToExistingTexture(page, tex);
  81. renderPromise.Result = tex;
  82. }
  83. #else
  84. RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, tex, new Vector2(tex.width, tex.height));
  85. PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
  86. #endif
  87. return renderPromise;
  88. }
  89. public static PDFJS_Promise<Texture2D> RenderPageToTextureAsync(PDFPage page, int width, int height)
  90. {
  91. return RenderPageToTextureAsync(page, new Vector2(width, height));
  92. }
  93. public static PDFJS_Promise<Texture2D> RenderPageToTextureAsync(PDFPage page, Vector2 size)
  94. {
  95. PDFJS_Promise<Texture2D> renderPromise = new PDFJS_Promise<Texture2D>();
  96. #if !UNITY_WEBGL || UNITY_EDITOR
  97. using (PDFRenderer renderer = new PDFRenderer())
  98. {
  99. renderPromise.HasFinished = true;
  100. renderPromise.HasSucceeded = true;
  101. renderPromise.HasReceivedJSResponse = true;
  102. renderPromise.Result = renderer.RenderPageToTexture(page, (int)size.x, (int)size.y);
  103. }
  104. #else
  105. RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, null, size);
  106. PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
  107. #endif
  108. return renderPromise;
  109. }
  110. public static PDFJS_Promise<Texture2D> RenderPageToTextureAsync(PDFPage page, float scale = 1.0f)
  111. {
  112. PDFJS_Promise<Texture2D> renderPromise = new PDFJS_Promise<Texture2D>();
  113. #if !UNITY_WEBGL || UNITY_EDITOR
  114. using (PDFRenderer renderer = new PDFRenderer())
  115. {
  116. renderPromise.HasFinished = true;
  117. renderPromise.HasSucceeded = true;
  118. renderPromise.HasReceivedJSResponse = true;
  119. Vector2 size = page.GetPageSize(scale);
  120. renderPromise.Result = renderer.RenderPageToTexture(page, (int)size.x, (int)size.y);
  121. }
  122. #else
  123. RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, null, page.GetPageSize(scale));
  124. PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
  125. #endif
  126. return renderPromise;
  127. }
  128. #if UNITY_WEBGL && !UNITY_EDITOR
  129. private static IEnumerator RenderPageCoroutine(PDFJS_PromiseCoroutine promiseCoroutine, IPDFJS_Promise promise, object parameters)
  130. {
  131. PDFJS_Promise<PDFJS_WebGLCanvas> renderToCanvasPromise = new PDFJS_Promise<PDFJS_WebGLCanvas>();
  132. PDFJS_Library.Instance.PreparePromiseCoroutine(null, renderToCanvasPromise, null);
  133. IntPtr pageHandle = ((RenderPageParameters)parameters).pageHandle;
  134. Texture2D texture = ((RenderPageParameters)parameters).existingTexture;
  135. Vector2 newtextureSize = ((RenderPageParameters)parameters).newTextureSize;
  136. Vector2 pageSize = PDFPage.GetPageSize(pageHandle, 1.0f);
  137. float scale = 1.0f;
  138. if (texture != null)
  139. {
  140. float wf = pageSize.x / texture.width;
  141. float hf = pageSize.y / texture.height;
  142. scale = 1.0f / Mathf.Max(wf, hf);
  143. }
  144. else
  145. {
  146. float wf = pageSize.x / ((int)newtextureSize.x);
  147. float hf = pageSize.y / ((int)newtextureSize.y);
  148. scale = 1.0f / Mathf.Max(wf, hf);
  149. }
  150. PDFJS_RenderPageIntoCanvas(renderToCanvasPromise.PromiseHandle, pageHandle.ToInt32(), scale);
  151. while (!renderToCanvasPromise.HasReceivedJSResponse)
  152. yield return null;
  153. if (renderToCanvasPromise.HasSucceeded)
  154. {
  155. int canvasHandle = int.Parse(renderToCanvasPromise.JSObjectHandle);
  156. using (PDFJS_WebGLCanvas canvas = new PDFJS_WebGLCanvas(new IntPtr(canvasHandle)))
  157. {
  158. PDFJS_Promise<Texture2D> renderToTexturePromise = promise as PDFJS_Promise<Texture2D>;
  159. if (texture == null)
  160. {
  161. texture = new Texture2D((int)newtextureSize.x, (int)newtextureSize.y, TextureFormat.ARGB32, false);
  162. texture.filterMode = FilterMode.Bilinear;
  163. texture.Apply();
  164. }
  165. PDFJS_RenderCanvasIntoTexture(canvasHandle, texture.GetNativeTexturePtr().ToInt32());
  166. renderToTexturePromise.Result = texture;
  167. renderToTexturePromise.HasSucceeded = true;
  168. renderToTexturePromise.HasFinished = true;
  169. promiseCoroutine.ExecuteThenAction(true, texture);
  170. }
  171. }
  172. else
  173. {
  174. PDFJS_Promise<Texture2D> renderToTexturePromise = promise as PDFJS_Promise<Texture2D>;
  175. renderToTexturePromise.Result = null;
  176. renderToTexturePromise.HasSucceeded = false;
  177. renderToTexturePromise.HasFinished = true;
  178. promiseCoroutine.ExecuteThenAction(false, null);
  179. }
  180. }
  181. #endif
  182. #if !UNITY_WEBGL || UNITY_EDITOR
  183. /// <summary>
  184. /// Render page into a new byte array.
  185. /// </summary>
  186. /// <param name="page"></param>
  187. /// <returns></returns>
  188. #if !UNITY_WEBGL
  189. public
  190. #else
  191. private
  192. #endif
  193. byte[] RenderPageToByteArray(PDFPage page)
  194. {
  195. return RenderPageToByteArray(page, (int) page.GetPageSize().x, (int) page.GetPageSize().y, null,
  196. RenderSettings.defaultRenderSettings);
  197. }
  198. /// <summary>
  199. /// Render page into a new byte array.
  200. /// </summary>
  201. /// <param name="page"></param>
  202. /// <param name="width"></param>
  203. /// <param name="height"></param>
  204. /// <returns></returns>
  205. #if !UNITY_WEBGL
  206. public
  207. #else
  208. private
  209. #endif
  210. byte[] RenderPageToByteArray(PDFPage page, int width, int height)
  211. {
  212. return RenderPageToByteArray(page, width, height, null, RenderSettings.defaultRenderSettings);
  213. }
  214. /// <summary>
  215. /// Render page into a new byte array.
  216. /// </summary>
  217. /// <param name="page"></param>
  218. /// <param name="width"></param>
  219. /// <param name="height"></param>
  220. /// <param name="rectsProvider"></param>
  221. /// <returns></returns>
  222. #if !UNITY_WEBGL
  223. public
  224. #else
  225. private
  226. #endif
  227. byte[] RenderPageToByteArray(PDFPage page, int width, int height,
  228. IPDFColoredRectListProvider rectsProvider)
  229. {
  230. return RenderPageToByteArray(page, width, height, rectsProvider, RenderSettings.defaultRenderSettings);
  231. }
  232. /// <summary>
  233. /// Render page into a new byte array.
  234. /// </summary>
  235. /// <param name="page"></param>
  236. /// <param name="width"></param>
  237. /// <param name="height"></param>
  238. /// <param name="rectsProvider"></param>
  239. /// <param name="settings"></param>
  240. /// <returns></returns>
  241. #if !UNITY_WEBGL
  242. public
  243. #else
  244. private
  245. #endif
  246. byte[] RenderPageToByteArray(PDFPage page, int width, int height,
  247. IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
  248. {
  249. if (m_Bitmap == null || !m_Bitmap.HasSameSize(width, height))
  250. {
  251. if (m_Bitmap != null)
  252. m_Bitmap.Dispose();
  253. m_Bitmap = new PDFBitmap(width, height, false);
  254. }
  255. m_Bitmap.FillRect(0, 0, width, height, int.MaxValue);
  256. int flags = settings == null
  257. ? RenderSettings.defaultRenderSettings.ComputeRenderingFlags()
  258. : settings.ComputeRenderingFlags();
  259. FPDF_RenderPageBitmap(m_Bitmap.NativePointer, page.NativePointer, 0, 0, width, height, 0, flags);
  260. IntPtr bufferPtr = m_Bitmap.GetBuffer();
  261. if (bufferPtr == IntPtr.Zero)
  262. return null;
  263. int length = width * height * 4;
  264. if (m_IntermediateBuffer == null || m_IntermediateBuffer.Length < length)
  265. m_IntermediateBuffer = new byte[width * height * 4];
  266. Marshal.Copy(bufferPtr, m_IntermediateBuffer, 0, width * height * 4);
  267. #if !UNITY_WEBGL
  268. IList<PDFColoredRect> coloredRects = rectsProvider != null
  269. ? rectsProvider.GetBackgroundColoredRectList(page)
  270. : null;
  271. if (coloredRects != null && coloredRects.Count > 0)
  272. {
  273. foreach (PDFColoredRect coloredRect in coloredRects)
  274. {
  275. var r = (int)(coloredRect.color.r * 255) & 0xFF;
  276. var g = (int)(coloredRect.color.g * 255) & 0xFF;
  277. var b = (int)(coloredRect.color.b * 255) & 0xFF;
  278. var a = (int)(coloredRect.color.a * 255) & 0xFF;
  279. float alpha = (a / (float)255);
  280. float reverseAlpha = 1.0f - alpha;
  281. Rect deviceRect = page.ConvertPageRectToDeviceRect(coloredRect.pageRect, new Vector2(width, height));
  282. for (int y = 0; y < -(int)deviceRect.height; ++y)
  283. {
  284. for (int x = 0; x < (int)deviceRect.width; ++x)
  285. {
  286. int s = (((int)deviceRect.y + y + (int)deviceRect.height) * width + (int)deviceRect.x + x) * 4;
  287. var sr = m_IntermediateBuffer[s];
  288. var sg = m_IntermediateBuffer[s + 1];
  289. var sb = m_IntermediateBuffer[s + 2];
  290. m_IntermediateBuffer[s] = (byte)Mathf.Clamp(alpha * r + (reverseAlpha * sr), 0, 255);
  291. m_IntermediateBuffer[s + 1] = (byte)Mathf.Clamp(alpha * g + (reverseAlpha * sg), 0, 255);
  292. m_IntermediateBuffer[s + 2] = (byte)Mathf.Clamp(alpha * b + (reverseAlpha * sb), 0, 255);
  293. m_IntermediateBuffer[s + 3] = 0xFF;
  294. }
  295. }
  296. }
  297. }
  298. #endif
  299. return m_IntermediateBuffer;
  300. }
  301. /// <summary>
  302. /// Render page into a new Texture2D.
  303. /// </summary>
  304. /// <param name="page"></param>
  305. /// <returns></returns>
  306. #if !UNITY_WEBGL
  307. public
  308. #else
  309. private
  310. #endif
  311. Texture2D RenderPageToTexture(PDFPage page)
  312. {
  313. return RenderPageToTexture(page, (int) page.GetPageSize().x, (int) page.GetPageSize().y, null,
  314. RenderSettings.defaultRenderSettings);
  315. }
  316. /// <summary>
  317. /// Render page into a new Texture2D.
  318. /// </summary>
  319. /// <param name="page"></param>
  320. /// <param name="width"></param>
  321. /// <param name="height"></param>
  322. /// <returns></returns>
  323. #if !UNITY_WEBGL
  324. public
  325. #else
  326. private
  327. #endif
  328. Texture2D RenderPageToTexture(PDFPage page, int width, int height)
  329. {
  330. return RenderPageToTexture(page, width, height, null, RenderSettings.defaultRenderSettings);
  331. }
  332. /// <summary>
  333. /// Render page into a new Texture2D.
  334. /// </summary>
  335. /// <param name="page"></param>
  336. /// <param name="width"></param>
  337. /// <param name="height"></param>
  338. /// <param name="rectsProvider"></param>
  339. /// <returns></returns>
  340. #if !UNITY_WEBGL
  341. public
  342. #else
  343. private
  344. #endif
  345. Texture2D RenderPageToTexture(PDFPage page, int width, int height,
  346. IPDFColoredRectListProvider rectsProvider)
  347. {
  348. return RenderPageToTexture(page, width, height, rectsProvider, RenderSettings.defaultRenderSettings);
  349. }
  350. /// <summary>
  351. /// Render page into a new Texture2D.
  352. /// </summary>
  353. /// <param name="page"></param>
  354. /// <param name="width"></param>
  355. /// <param name="height"></param>
  356. /// <param name="rectsProvider"></param>
  357. /// <param name="settings"></param>
  358. /// <returns></returns>
  359. #if !UNITY_WEBGL
  360. public
  361. #else
  362. private
  363. #endif
  364. Texture2D RenderPageToTexture(PDFPage page, int width, int height,
  365. IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
  366. {
  367. Texture2D newTex = new Texture2D(width, height, TextureFormat.RGBA32, false);
  368. RenderPageToExistingTexture(page, newTex, rectsProvider, settings);
  369. return newTex;
  370. }
  371. /// <summary>
  372. /// Render page into an existing Texture2D.
  373. /// </summary>
  374. /// <param name="page"></param>
  375. /// <param name="texture"></param>
  376. #if !UNITY_WEBGL
  377. public
  378. #else
  379. private
  380. #endif
  381. void RenderPageToExistingTexture(PDFPage page, Texture2D texture)
  382. {
  383. RenderPageToExistingTexture(page, texture, null, RenderSettings.defaultRenderSettings);
  384. }
  385. /// <summary>
  386. /// Render page into an existing Texture2D.
  387. /// </summary>
  388. /// <param name="page"></param>
  389. /// <param name="texture"></param>
  390. /// <param name="rectsProvider"></param>
  391. #if !UNITY_WEBGL
  392. public
  393. #else
  394. private
  395. #endif
  396. void RenderPageToExistingTexture(PDFPage page, Texture2D texture,
  397. IPDFColoredRectListProvider rectsProvider)
  398. {
  399. RenderPageToExistingTexture(page, texture, rectsProvider, RenderSettings.defaultRenderSettings);
  400. }
  401. /// <summary>
  402. /// Render page into an existing Texture2D.
  403. /// </summary>
  404. /// <param name="page"></param>
  405. /// <param name="texture"></param>
  406. /// <param name="rectsProvider"></param>
  407. /// <param name="settings"></param>
  408. #if !UNITY_WEBGL
  409. public
  410. #else
  411. private
  412. #endif
  413. void RenderPageToExistingTexture(PDFPage page, Texture2D texture,
  414. IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
  415. {
  416. byte[] byteArray = RenderPageToByteArray(page, texture.width, texture.height, rectsProvider, settings);
  417. if (byteArray != null)
  418. {
  419. if ((texture.format != TextureFormat.RGBA32
  420. && texture.format != TextureFormat.ARGB32
  421. && texture.format != TextureFormat.BGRA32
  422. && texture.format != (TextureFormat)37) || texture.mipmapCount > 1)
  423. {
  424. Color32[] pixels = new Color32[texture.width * texture.height];
  425. for (int i = 0; i < pixels.Length; ++i)
  426. pixels[i] = new Color32(
  427. byteArray[i * 4],
  428. byteArray[i * 4 + 1],
  429. byteArray[i * 4 + 2],
  430. byteArray[i * 4 + 3]);
  431. texture.SetPixels32(pixels);
  432. texture.Apply();
  433. }
  434. else
  435. {
  436. texture.LoadRawTextureData(byteArray);
  437. texture.Apply();
  438. }
  439. }
  440. }
  441. #endif
  442. /// <summary>
  443. /// Allows the application to specify render settings.
  444. /// </summary>
  445. [Serializable]
  446. public class RenderSettings
  447. {
  448. public bool disableSmoothPath = false;
  449. public bool disableSmoothText = false;
  450. public bool grayscale = false;
  451. public bool optimizeTextForLCDDisplay = false;
  452. public bool renderAnnotations = false;
  453. public bool renderForPrinting = false;
  454. public static RenderSettings defaultRenderSettings
  455. {
  456. get { return new RenderSettings(); }
  457. }
  458. public int ComputeRenderingFlags()
  459. {
  460. int flags = 0x10;
  461. if (renderAnnotations)
  462. flags |= 0x01;
  463. if (optimizeTextForLCDDisplay)
  464. flags |= 0x02;
  465. if (grayscale)
  466. flags |= 0x08;
  467. if (renderForPrinting)
  468. flags |= 0x800;
  469. if (disableSmoothText)
  470. flags |= 0x1000;
  471. if (disableSmoothPath)
  472. flags |= 0x1000;
  473. return flags;
  474. }
  475. }
  476. #region NATIVE
  477. #if UNITY_WEBGL && !UNITY_EDITOR
  478. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  479. private static extern void PDFJS_RenderPageIntoCanvas(string promiseHandle, int pageHandle, float scale);
  480. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  481. private static extern void PDFJS_RenderCanvasIntoTexture(int canvasHandle, int textureHandle);
  482. #else
  483. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  484. private static extern void FPDF_RenderPageBitmap(IntPtr bitmap, IntPtr page, int start_x, int start_y, int size_x, int size_y, int rotate, int flags);
  485. #endif
  486. #endregion
  487. }
  488. }