Пример #1
0
        public static PDFJS_Promise <Texture2D> RenderPageToTextureAsync(PDFPage page, float scale = 1.0f)
        {
            PDFJS_Promise <Texture2D> renderPromise = new PDFJS_Promise <Texture2D>();

#if !UNITY_WEBGL || UNITY_EDITOR
            using (PDFRenderer renderer = new PDFRenderer())
            {
                renderPromise.HasFinished           = true;
                renderPromise.HasSucceeded          = true;
                renderPromise.HasReceivedJSResponse = true;
                Vector2 size = page.GetPageSize(scale);
                renderPromise.Result = renderer.RenderPageToTexture(page, (int)size.x, (int)size.y);
            }
#else
            RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, null, page.GetPageSize(scale));

            PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
#endif

            return(renderPromise);
        }
Пример #2
0
 Texture2D RenderPageToTexture(PDFPage page)
 {
     return(RenderPageToTexture(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y, null,
                                RenderSettings.defaultRenderSettings));
 }
Пример #3
0
 byte[] RenderPageToByteArray(PDFPage page)
 {
     return(RenderPageToByteArray(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y, null,
                                  RenderSettings.defaultRenderSettings));
 }
Пример #4
0
        private static IEnumerator RenderPageCoroutine(PDFJS_PromiseCoroutine promiseCoroutine, IPDFJS_Promise promise, object parameters)
        {
            PDFJS_Promise <PDFJS_WebGLCanvas> renderToCanvasPromise = new PDFJS_Promise <PDFJS_WebGLCanvas>();

            PDFJS_Library.Instance.PreparePromiseCoroutine(null, renderToCanvasPromise, null);

            IntPtr    pageHandle     = ((RenderPageParameters)parameters).pageHandle;
            Texture2D texture        = ((RenderPageParameters)parameters).existingTexture;
            Vector2   newtextureSize = ((RenderPageParameters)parameters).newTextureSize;

            Vector2 pageSize = PDFPage.GetPageSize(pageHandle, 1.0f);

            float scale = 1.0f;

            if (texture != null)
            {
                float wf = pageSize.x / texture.width;
                float hf = pageSize.y / texture.height;

                scale = 1.0f / Mathf.Max(wf, hf);
            }
            else
            {
                float wf = pageSize.x / ((int)newtextureSize.x);
                float hf = pageSize.y / ((int)newtextureSize.y);

                scale = 1.0f / Mathf.Max(wf, hf);
            }

            PDFJS_RenderPageIntoCanvas(renderToCanvasPromise.PromiseHandle, pageHandle.ToInt32(), scale);

            while (!renderToCanvasPromise.HasReceivedJSResponse)
            {
                yield return(null);
            }

            if (renderToCanvasPromise.HasSucceeded)
            {
                int canvasHandle = int.Parse(renderToCanvasPromise.JSObjectHandle);

                using (PDFJS_WebGLCanvas canvas = new PDFJS_WebGLCanvas(new IntPtr(canvasHandle)))
                {
                    PDFJS_Promise <Texture2D> renderToTexturePromise = promise as PDFJS_Promise <Texture2D>;

                    if (texture == null)
                    {
                        texture            = new Texture2D((int)newtextureSize.x, (int)newtextureSize.y, TextureFormat.ARGB32, false);
                        texture.filterMode = FilterMode.Bilinear;
                        texture.Apply();
                    }


                    PDFJS_RenderCanvasIntoTexture(canvasHandle, texture.GetNativeTexturePtr().ToInt32());

                    renderToTexturePromise.Result       = texture;
                    renderToTexturePromise.HasSucceeded = true;
                    renderToTexturePromise.HasFinished  = true;

                    promiseCoroutine.ExecuteThenAction(true, texture);
                }
            }
            else
            {
                PDFJS_Promise <Texture2D> renderToTexturePromise = promise as PDFJS_Promise <Texture2D>;

                renderToTexturePromise.Result       = null;
                renderToTexturePromise.HasSucceeded = false;
                renderToTexturePromise.HasFinished  = true;

                promiseCoroutine.ExecuteThenAction(false, null);
            }
        }