From 58548b9adb3d1b05fc942f3b562b9049e8087a0c Mon Sep 17 00:00:00 2001 From: Paulo Coutinho Date: Fri, 11 Mar 2022 21:05:58 -0300 Subject: [PATCH] updated to version 4938 --- extras/wasm/template/index.html | 203 +++++++++++++++++--------------- modules/android.py | 8 ++ 2 files changed, 117 insertions(+), 94 deletions(-) diff --git a/extras/wasm/template/index.html b/extras/wasm/template/index.html index 230e2ca..011ddeb 100644 --- a/extras/wasm/template/index.html +++ b/extras/wasm/template/index.html @@ -82,15 +82,16 @@ // classes class Page { - constructor(index, o) { + constructor(index, processor) { this.index = index; this.src = null; - this.o = o; + this.processor = processor; } render() { let canvas = document.createElement('canvas'); - this.o.render(this.index, canvas, 3.0, 90); + this.processor.render(this.index, canvas, 3.0, 90); + let dataUri = canvas.toDataURL(); this.updateImage(dataUri); } @@ -98,9 +99,10 @@ createImage() { let image = document.createElement('img'); let pageIndex = this.index; + image.id = "pageImage" + (pageIndex + 1); image.src = ''; - image.className = "page-list-item"; + image.className = "is-invisible page-list-item"; return image; } @@ -112,6 +114,7 @@ let pageIndex = this.index; let image = document.getElementById("pageImage" + (pageIndex + 1)); image.src = dataUri; + image.classList.remove("is-invisible"); image.onclick = function () { openModal(); changeCurrentPageItem(pageIndex + 1); @@ -119,6 +122,7 @@ const container = image.closest('.column'); const loaderWrapper = container.querySelector('.page-loader-wrapper'); + if (loaderWrapper) { container.removeChild(loaderWrapper); } @@ -129,17 +133,103 @@ } } + class Processor { + constructor(wasmData) { + this.wasmData = wasmData; + } + + getPageSize(i = 0, s = 2) { + return H(F64, 2, [-1, -1])((w, h) => FPDF.GetPageSizeByIndex(this.wasmData.wasm, i, w, h)).map(v => parseInt(v) * s); + } + + getRender(i = 0, w, h) { + const flag = FPDF.REVERSE_BYTE_ORDER | FPDF.ANNOT; + const heap = Module._malloc(w * h * C); + + for (let i = 0; i < w * h * C; i++) { + Module.HEAPU8[heap + i] = 0; + } + + const bmap = FPDF.Bitmap_CreateEx(w, h, F, heap, w * C); + const page = FPDF.LoadPage(this.wasmData.wasm, i); + + FPDF.Bitmap_FillRect(bmap, 0, 0, w, h, 0xFFFFFFFF); + FPDF.RenderPageBitmap(bmap, page, 0, 0, w, h, 0, flag); + FPDF.Bitmap_Destroy(bmap); + FPDF.ClosePage(page); + + return heap; + } + + getPageRender(n = 0, w, h) { + let pageRenderPtr = this.getRender(n, w, h); + let pageRenderData = []; + + for (let v = 0; v < w * h * C; v++) { + pageRenderData.push(Module.HEAPU8[pageRenderPtr + v]); + } + + Module._free(pageRenderPtr); + + return pageRenderData; + } + + render(n = 0, canvas, scale, rotation) { + const [w, h] = this.getPageSize(n, scale, rotation); + const data = this.getPageRender(n, w, h, rotation); + + canvas.width = w; + canvas.height = h; + + const x = canvas.getContext('2d'); + const i = x.createImageData(w, h); + i.data.set(data); + x.putImageData(i, 0, 0); + } + + getLastError() { + let lastError = FPDF.GetLastError(); + + switch (lastError) { + case FPDF.LAST_ERROR.SUCCESS: + return "success"; + break; + case FPDF.LAST_ERROR.UNKNOWN: + return "unknown error"; + break; + case FPDF.LAST_ERROR.FILE: + return "file not found or could not be opened"; + break; + case FPDF.LAST_ERROR.FORMAT: + return "file not in PDF format or corrupted"; + break; + case FPDF.LAST_ERROR.PASSWORD: + return "password required or incorrect password"; + break; + case FPDF.LAST_ERROR.SECURITY: + return "unsupported security scheme"; + break; + case FPDF.LAST_ERROR.PAGE: + return "page not found or content error"; + break; + default: + return "unknown error"; + } + } + } + class Doc { - constructor(pagesCount, o) { - this.pages = Array(pagesCount).fill(null) - this.o = o; + constructor(wasmData) { + this.processor = new Processor(wasmData); + } - this.onCreate(); + setPages(pagesCount) { + this.pages = Array(pagesCount).fill(null); } - onCreate() { + createAllPages() { for (let i = 0; i < this.pages.length; i++) { - this.pages[i] = new Page(i, this.o); + this.pages[i] = new Page(i, this.processor); } } @@ -155,13 +245,14 @@ } renderPages() { - const wasmBuffer = this.o.wasmBuffer; - const wasm = this.o.wasm; + const wasmBuffer = this.processor.wasmData.wasmBuffer; + const wasm = this.processor.wasmData.wasm; doc.pages.forEach(async function (page, index) { if (!page.loaded) { setTimeout(function () { page.render(); + if (index === doc.pages.length - 1) { Module._free(wasmBuffer); @@ -287,103 +378,27 @@ // create document console.log('Loading document...'); - let o = { + doc = new Doc({ wasm: FPDF.LoadMemDocument(wasmBuffer, fileSize, ""), wasmBuffer: wasmBuffer, - }; - - o.getPageSize = (i = 0, s = 2) => H(F64, 2, [-1, -1])((w, h) => FPDF.GetPageSizeByIndex(o.wasm, i, w, h)).map(v => parseInt(v) * s); - - o.getRender = function (i = 0, w, h) { - const flag = FPDF.REVERSE_BYTE_ORDER | FPDF.ANNOT; - const heap = Module._malloc(w * h * C); - - for (let i = 0; i < w * h * C; i++) { - Module.HEAPU8[heap + i] = 0; - } - - const bmap = FPDF.Bitmap_CreateEx(w, h, F, heap, w * C); - const page = FPDF.LoadPage(this.wasm, i); - - FPDF.Bitmap_FillRect(bmap, 0, 0, w, h, 0xFFFFFFFF); - FPDF.RenderPageBitmap(bmap, page, 0, 0, w, h, 0, flag); - FPDF.Bitmap_Destroy(bmap); - FPDF.ClosePage(page); - - return heap; - }; - - o.getPageRender = function (n = 0, w, h) { - let pageRenderPtr = this.getRender(n, w, h); - let pageRenderData = []; - - for (let v = 0; v < w * h * C; v++) { - pageRenderData.push(Module.HEAPU8[pageRenderPtr + v]); - } - - Module._free(pageRenderPtr); - - return pageRenderData; - }; - - o.render = function (n = 0, canvas, scale, rotation) { - const [w, h] = this.getPageSize(n, scale, rotation); - const data = this.getPageRender(n, w, h, rotation); - - canvas.width = w; - canvas.height = h; - - const x = canvas.getContext('2d'); - const i = x.createImageData(w, h); - i.data.set(data); - x.putImageData(i, 0, 0); - }; - - o.getLastError = function () { - let lastError = FPDF.GetLastError(); - - switch (lastError) { - case FPDF.LAST_ERROR.SUCCESS: - return "success"; - break; - case FPDF.LAST_ERROR.UNKNOWN: - return "unknown error"; - break; - case FPDF.LAST_ERROR.FILE: - return "file not found or could not be opened"; - break; - case FPDF.LAST_ERROR.FORMAT: - return "file not in PDF format or corrupted"; - break; - case FPDF.LAST_ERROR.PASSWORD: - return "password required or incorrect password"; - break; - case FPDF.LAST_ERROR.SECURITY: - return "unsupported security scheme"; - break; - case FPDF.LAST_ERROR.PAGE: - return "page not found or content error"; - break; - default: - return "unknown error"; - } - } + }); // check last error - let lastError = o.getLastError(); + let lastError = doc.processor.getLastError(); console.log("Load document state: " + lastError); // count page console.log('Couting pages...'); - let pages = FPDF.GetPageCount(o.wasm); + let pages = FPDF.GetPageCount(doc.processor.wasmData.wasm); console.log('Pages: ' + pages); // list all pages if (pages > 0) { console.log('Rendering ' + pages + ' PDF pages...'); - doc = new Doc(pages, o); + doc.setPages(pages); + doc.createAllPages(); for (let x = 0; x < pages; x++) { console.log('Rendering page ' + (x + 1) + '...'); diff --git a/modules/android.py b/modules/android.py index 8ed8cc6..665b4c8 100644 --- a/modules/android.py +++ b/modules/android.py @@ -62,6 +62,14 @@ def run_task_patch(): f.set_file_line_content(source_file, line_number, content, new_line=True) + # more one + line_number = f.get_file_line_number_with_content( + source_file, line_content, strip=True + ) + + f.set_file_line_content(source_file, line_number, content, new_line=True) + + # more one line_number = f.get_file_line_number_with_content( source_file, line_content, strip=True )