コード例 #1
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
        void RenderPageToExistingTexture(PDFPage page, Texture2D texture,
                                         IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
        {
            byte[] byteArray = RenderPageToByteArray(page, texture.width, texture.height, rectsProvider, settings);

            if (byteArray != null)
            {
                if ((texture.format != TextureFormat.RGBA32 &&
                     texture.format != TextureFormat.ARGB32 &&
                     texture.format != TextureFormat.BGRA32 &&
                     texture.format != (TextureFormat)37) || texture.mipmapCount > 1)
                {
                    Color32[] pixels = new Color32[texture.width * texture.height];

                    for (int i = 0; i < pixels.Length; ++i)
                    {
                        pixels[i] = new Color32(
                            byteArray[i * 4],
                            byteArray[i * 4 + 1],
                            byteArray[i * 4 + 2],
                            byteArray[i * 4 + 3]);
                    }

                    texture.SetPixels32(pixels);
                    texture.Apply();
                }
                else
                {
                    texture.LoadRawTextureData(byteArray);
                    texture.Apply();
                }
            }
        }
コード例 #2
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
        Texture2D RenderPageToTexture(PDFPage page, int width, int height,
                                      IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
        {
            Texture2D newTex = new Texture2D(width, height, TextureFormat.RGBA32, false);

            RenderPageToExistingTexture(page, newTex, rectsProvider, settings);

            return(newTex);
        }
コード例 #3
0
ファイル: PDFLink.cs プロジェクト: trishiadenisse/KampilanApp
        public PDFLink(PDFPage page, IntPtr nativePointer)
        {
            if (page == null)
            {
                throw new NullReferenceException();
            }
            if (nativePointer == IntPtr.Zero)
            {
                throw new NullReferenceException();
            }

            PDFLibrary.AddRef("PDFLink");

            m_Page = page;

            m_NativePointer = nativePointer;
        }
コード例 #4
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
        public static PDFJS_Promise <Texture2D> RenderPageToTextureAsync(PDFPage page, Vector2 size)
        {
            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;
                renderPromise.Result = renderer.RenderPageToTexture(page, (int)size.x, (int)size.y);
            }
#else
            RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, null, size);

            PDFJS_Library.Instance.PreparePromiseCoroutine(RenderPageCoroutine, renderPromise, parameters).Start();
#endif
            return(renderPromise);
        }
コード例 #5
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
        public static PDFJS_Promise <Texture2D> RenderPageToExistingTextureAsync(PDFPage page, Texture2D tex)
        {
            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;
                renderer.RenderPageToExistingTexture(page, tex);
                renderPromise.Result = tex;
            }
#else
            RenderPageParameters parameters = new RenderPageParameters(page.NativePointer, tex, new Vector2(tex.width, tex.height));

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

            return(renderPromise);
        }
コード例 #6
0
        public PDFTextPage(PDFPage page)
        {
            if (page == null)
            {
                throw new NullReferenceException();
            }

            PDFLibrary.AddRef("PDFTextPage");

            m_Page          = page;
            m_NativePointer = FPDFText_LoadPage(m_Page.NativePointer);

            if (m_NativePointer != IntPtr.Zero)
            {
                if (s_InstanceMap.ContainsKey(m_NativePointer))
                {
                    s_InstanceMap[m_NativePointer] = s_InstanceMap[m_NativePointer] + 1;
                }
                else
                {
                    s_InstanceMap[m_NativePointer] = 1;
                }
            }
        }
コード例 #7
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 void RenderPageToExistingTexture(PDFPage page, Texture2D texture,
                                  IPDFColoredRectListProvider rectsProvider)
 {
     RenderPageToExistingTexture(page, texture, rectsProvider, RenderSettings.defaultRenderSettings);
 }
コード例 #8
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 void RenderPageToExistingTexture(PDFPage page, Texture2D texture)
 {
     RenderPageToExistingTexture(page, texture, null, RenderSettings.defaultRenderSettings);
 }
コード例 #9
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 Texture2D RenderPageToTexture(PDFPage page, int width, int height,
                               IPDFColoredRectListProvider rectsProvider)
 {
     return(RenderPageToTexture(page, width, height, rectsProvider, RenderSettings.defaultRenderSettings));
 }
コード例 #10
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 Texture2D RenderPageToTexture(PDFPage page, int width, int height)
 {
     return(RenderPageToTexture(page, width, height, null, RenderSettings.defaultRenderSettings));
 }
コード例 #11
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 Texture2D RenderPageToTexture(PDFPage page)
 {
     return(RenderPageToTexture(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y, null,
                                RenderSettings.defaultRenderSettings));
 }
コード例 #12
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 byte[] RenderPageToByteArray(PDFPage page, int width, int height,
                              IPDFColoredRectListProvider rectsProvider)
 {
     return(RenderPageToByteArray(page, width, height, rectsProvider, RenderSettings.defaultRenderSettings));
 }
コード例 #13
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 byte[] RenderPageToByteArray(PDFPage page, int width, int height)
 {
     return(RenderPageToByteArray(page, width, height, null, RenderSettings.defaultRenderSettings));
 }
コード例 #14
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 byte[] RenderPageToByteArray(PDFPage page)
 {
     return(RenderPageToByteArray(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y, null,
                                  RenderSettings.defaultRenderSettings));
 }
コード例 #15
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
        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);
            }
        }
コード例 #16
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
 public static PDFJS_Promise <Texture2D> RenderPageToTextureAsync(PDFPage page, int width, int height)
 {
     return(RenderPageToTextureAsync(page, new Vector2(width, height)));
 }
コード例 #17
0
ファイル: PDFRenderer.cs プロジェクト: kimch2/Unity-1
        byte[] RenderPageToByteArray(PDFPage page, int width, int height,
                                     IPDFColoredRectListProvider rectsProvider, RenderSettings settings)
        {
            if (m_Bitmap == null || !m_Bitmap.HasSameSize(width, height))
            {
                if (m_Bitmap != null)
                {
                    m_Bitmap.Dispose();
                }

                m_Bitmap = new PDFBitmap(width, height, false);
            }

            m_Bitmap.FillRect(0, 0, width, height, int.MaxValue);

            int flags = settings == null
                ? RenderSettings.defaultRenderSettings.ComputeRenderingFlags()
                : settings.ComputeRenderingFlags();

            FPDF_RenderPageBitmap(m_Bitmap.NativePointer, page.NativePointer, 0, 0, width, height, 0, flags);

            IntPtr bufferPtr = m_Bitmap.GetBuffer();

            if (bufferPtr == IntPtr.Zero)
            {
                return(null);
            }

            int length = width * height * 4;

            if (m_IntermediateBuffer == null || m_IntermediateBuffer.Length < length)
            {
                m_IntermediateBuffer = new byte[width * height * 4];
            }

            Marshal.Copy(bufferPtr, m_IntermediateBuffer, 0, width * height * 4);

#if !UNITY_WEBGL
            IList <PDFColoredRect> coloredRects = rectsProvider != null
                ? rectsProvider.GetBackgroundColoredRectList(page)
                : null;

            if (coloredRects != null && coloredRects.Count > 0)
            {
                foreach (PDFColoredRect coloredRect in coloredRects)
                {
                    var r = (int)(coloredRect.color.r * 255) & 0xFF;
                    var g = (int)(coloredRect.color.g * 255) & 0xFF;
                    var b = (int)(coloredRect.color.b * 255) & 0xFF;
                    var a = (int)(coloredRect.color.a * 255) & 0xFF;

                    float alpha        = (a / (float)255);
                    float reverseAlpha = 1.0f - alpha;

                    Rect deviceRect = page.ConvertPageRectToDeviceRect(coloredRect.pageRect, new Vector2(width, height));

                    for (int y = 0; y < -(int)deviceRect.height; ++y)
                    {
                        for (int x = 0; x < (int)deviceRect.width; ++x)
                        {
                            int s = (((int)deviceRect.y + y + (int)deviceRect.height) * width + (int)deviceRect.x + x) * 4;

                            var sr = m_IntermediateBuffer[s];
                            var sg = m_IntermediateBuffer[s + 1];
                            var sb = m_IntermediateBuffer[s + 2];

                            m_IntermediateBuffer[s]     = (byte)Mathf.Clamp(alpha * r + (reverseAlpha * sr), 0, 255);
                            m_IntermediateBuffer[s + 1] = (byte)Mathf.Clamp(alpha * g + (reverseAlpha * sg), 0, 255);
                            m_IntermediateBuffer[s + 2] = (byte)Mathf.Clamp(alpha * b + (reverseAlpha * sb), 0, 255);
                            m_IntermediateBuffer[s + 3] = 0xFF;
                        }
                    }
                }
            }
#endif


            return(m_IntermediateBuffer);
        }
コード例 #18
0
 public PDFJS_Promise <PDFPage> GetPageAsync(int index)
 {
     return(PDFPage.LoadPageAsync(this, index));
 }