Skip to content

Commit

Permalink
New: PDF rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ollm committed Jul 31, 2023
1 parent 897fcaf commit a55312d
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 82 deletions.
13 changes: 8 additions & 5 deletions scripts/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,19 @@ function addImageToDom(querySelector, path, animation = true)
{
if(animation)
{
var src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);
let src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);

if(src.length > 0)
src.attr('src', path).addClass('a');
else
$('.fi-sha-'+querySelector+', .sha-'+querySelector+' .item-image').css({'background-image': 'url('+path+')'}).addClass('a');

$('.ri-sha-'+querySelector).attr('src', path);

$('.continue-reading-sha-'+querySelector).css({'background-image': 'url('+path+')'}).addClass('a');
}
else
{
var src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);
let src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);

if(src.length > 0)
src.attr('src', path);
Expand Down Expand Up @@ -1275,16 +1274,18 @@ async function openComic(animation = true, path = true, mainPath = true, end = f
let file = fileManager.file(path);
let files = await file.read();

let isCanvas = false;
let compressedFile = fileManager.lastCompressedFile(path);

if(compressedFile)
{
let features = fileManager.fileCompressed(compressedFile);
features = features.getFeatures();

if(features.canvas && false) // Not work yet
if(features.canvas)
{
await file.makeAvailable([{path: compressedFile}]);
isCanvas = true;
}
else
{
Expand Down Expand Up @@ -1353,6 +1354,8 @@ async function openComic(animation = true, path = true, mainPath = true, end = f
path: file.path,
mainPath: mainPath,
thumbnail: (thumbnail.cache) ? thumbnail.path : '',
size: file.size || false,
canvas: isCanvas,
folder: false,
});
}
Expand Down Expand Up @@ -1382,7 +1385,7 @@ async function openComic(animation = true, path = true, mainPath = true, end = f

events.events();

reading.read(path, indexStart, end);
reading.read(path, indexStart, end, isCanvas);
reading.hideContent(electronRemote.getCurrentWindow().isFullScreen(), true);

generateAppMenu();
Expand Down
15 changes: 7 additions & 8 deletions scripts/file-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,13 @@ var fileCompressed = function(path, _realPath = false) {

}

this.renderCanvas = async function(file, canvas, config = {}, callback = false, callbackError = false) {
this.renderCanvas = async function(file, canvas, config = {}) {

this.updateConfig(config);
this.getFeatures();

if(this.features.pdf)
this.renderCanvasPdf(file, canvas, callback);
return this.renderCanvasPdf(file, canvas);

}

Expand Down Expand Up @@ -1465,7 +1465,7 @@ var fileCompressed = function(path, _realPath = false) {

let size = {width: viewport.width, height: viewport.height};

files.push({name: file, path: p.join(this.path, file), folder: false, compressed: false, size: size});
files.push({name: file, path: p.join(this.path, file), folder: false, compressed: false, size: size, page: i});
this.setFileStatus(file, {page: i, extracted: false, size: size});
}

Expand Down Expand Up @@ -1528,14 +1528,14 @@ var fileCompressed = function(path, _realPath = false) {

}

this.renderCanvasPdf = async function(file, canvas, callback = false, callbackError = false) {
this.renderCanvasPdf = async function(file, canvas) {

let pdf = await this.openPdf();
let pages = pdf.numPages;

let status = this.getFileStatus(file);

console.log(file, status);
console.log('renderCanvasPdf', file/*, status*/);

if((status && status.widthRendered !== this.config.width) || this.config.force)
{
Expand All @@ -1549,13 +1549,12 @@ var fileCompressed = function(path, _realPath = false) {
canvas.height = viewport.height;
let context = canvas.getContext('2d');

var transform = this.config.scale && this.config.scale !== 1 ? [this.config.scale, 0, 0, this.config.scale, 0, 0] : null;

await page.render({canvasContext: context, viewport: viewport, transform: transform}).promise;
await page.render({canvasContext: context, viewport: viewport}).promise;

this.setFileStatus(file, {rendered: true, widthRendered: this.config.width});
}

return;
}

}
Expand Down
81 changes: 63 additions & 18 deletions scripts/queue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var processingTheQueue = {}, queued = {};
var queued = {}, threads = {}, onEnd = {};

async function processTheQueue(key)
async function processTheQueue(key, thread = 0)
{
if(queued[key])
{
Expand All @@ -20,11 +20,13 @@ async function processTheQueue(key)
console.error(error);
}

if(queued[key].length > 0)
if(queued[key].length > 0 && threads[key][thread])
{
process.nextTick(function() {
threads[key][thread]++;

processTheQueue(key);
process.nextTick(function(){

processTheQueue(key, thread);

});

Expand All @@ -33,11 +35,27 @@ async function processTheQueue(key)
}
}

processingTheQueue[key] = false;
threads[key][thread] = null;

checkEnd(key);

return;
}

function processTheQueueThreads(key)
{
for(let i = 0, len = threads[key].length; i < len; i++)
{
if(threads[key][i] === null)
{
threads[key][i] = 1;
processTheQueue(key, i);

break;
}
}
}

async function addToQueue(key, callback)
{
_arguments = [];
Expand All @@ -47,36 +65,63 @@ async function addToQueue(key, callback)
_arguments.push(arguments[i]);
}

if(!threads[key]) threads[key] = [null];

if(!queued[key]) queued[key] = [];
queued[key].push({key: key, callback: callback, arguments: _arguments});

if(!processingTheQueue[key])
{
processingTheQueue[key] = true;
processTheQueueThreads(key);
}

process.nextTick(function() {
function cleanQueue(key = false)
{
queued[key] = [];
}

function setThreads(key, num = 1)
{
if(!threads[key]) threads[key] = [null];

processTheQueue(key).catch(function(error){
let _threads = [];

//if(key == 'folderThumbnails')
// dom.compressedError(error);
for(let i = 0; i < num; i++)
{
_threads.push(threads[key][i] || null);
}

//console.error(error);
threads[key] = _threads;
}

});
function checkEnd(key)
{
if(onEnd[key])
{
let allNull = true;

});
for(let i = 0, len = threads[key].length; i < len; i++)
{
if(threads[key][i] !== null)
allNull = false;
}

if(allNull)
{
onEnd[key]();
onEnd[key] = false;
}
}
}

function cleanQueue(key = false)
function end(key, callback)
{
queued[key] = [];
onEnd[key] = callback;
}

module.exports = {
add: addToQueue,
queued: function(){return queued},
clean: cleanQueue,
threads: setThreads,
end: end,
process: processTheQueue,
};

0 comments on commit a55312d

Please sign in to comment.