Skip to content

Commit

Permalink
updated to version 4938
Browse files Browse the repository at this point in the history
  • Loading branch information
paulocoutinhox committed Mar 12, 2022
1 parent ed8a32f commit 58548b9
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 94 deletions.
203 changes: 109 additions & 94 deletions extras/wasm/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,27 @@

// 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);
}

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;
}
Expand All @@ -112,13 +114,15 @@
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);
};

const container = image.closest('.column');
const loaderWrapper = container.querySelector('.page-loader-wrapper');

if (loaderWrapper) {
container.removeChild(loaderWrapper);
}
Expand All @@ -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);
}
}

Expand All @@ -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);

Expand Down Expand Up @@ -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) + '...');
Expand Down
8 changes: 8 additions & 0 deletions modules/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 58548b9

Please sign in to comment.