Skip to content

Commit a55312d

Browse files
committed
New: PDF rendering
1 parent 897fcaf commit a55312d

File tree

7 files changed

+373
-82
lines changed

7 files changed

+373
-82
lines changed

scripts/dom.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,19 @@ function addImageToDom(querySelector, path, animation = true)
157157
{
158158
if(animation)
159159
{
160-
var src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);
160+
let src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);
161161

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

167167
$('.ri-sha-'+querySelector).attr('src', path);
168-
169168
$('.continue-reading-sha-'+querySelector).css({'background-image': 'url('+path+')'}).addClass('a');
170169
}
171170
else
172171
{
173-
var src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);
172+
let src = $('.fi-sha-'+querySelector+' img, .sha-'+querySelector+' img, img.fi-sha-'+querySelector);
174173

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

1277+
let isCanvas = false;
12781278
let compressedFile = fileManager.lastCompressedFile(path);
12791279

12801280
if(compressedFile)
12811281
{
12821282
let features = fileManager.fileCompressed(compressedFile);
12831283
features = features.getFeatures();
12841284

1285-
if(features.canvas && false) // Not work yet
1285+
if(features.canvas)
12861286
{
12871287
await file.makeAvailable([{path: compressedFile}]);
1288+
isCanvas = true;
12881289
}
12891290
else
12901291
{
@@ -1353,6 +1354,8 @@ async function openComic(animation = true, path = true, mainPath = true, end = f
13531354
path: file.path,
13541355
mainPath: mainPath,
13551356
thumbnail: (thumbnail.cache) ? thumbnail.path : '',
1357+
size: file.size || false,
1358+
canvas: isCanvas,
13561359
folder: false,
13571360
});
13581361
}
@@ -1382,7 +1385,7 @@ async function openComic(animation = true, path = true, mainPath = true, end = f
13821385

13831386
events.events();
13841387

1385-
reading.read(path, indexStart, end);
1388+
reading.read(path, indexStart, end, isCanvas);
13861389
reading.hideContent(electronRemote.getCurrentWindow().isFullScreen(), true);
13871390

13881391
generateAppMenu();

scripts/file-manager.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -712,13 +712,13 @@ var fileCompressed = function(path, _realPath = false) {
712712

713713
}
714714

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

717717
this.updateConfig(config);
718718
this.getFeatures();
719719

720720
if(this.features.pdf)
721-
this.renderCanvasPdf(file, canvas, callback);
721+
return this.renderCanvasPdf(file, canvas);
722722

723723
}
724724

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

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

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

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

15291529
}
15301530

1531-
this.renderCanvasPdf = async function(file, canvas, callback = false, callbackError = false) {
1531+
this.renderCanvasPdf = async function(file, canvas) {
15321532

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

15361536
let status = this.getFileStatus(file);
15371537

1538-
console.log(file, status);
1538+
console.log('renderCanvasPdf', file/*, status*/);
15391539

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

1552-
var transform = this.config.scale && this.config.scale !== 1 ? [this.config.scale, 0, 0, this.config.scale, 0, 0] : null;
1553-
1554-
await page.render({canvasContext: context, viewport: viewport, transform: transform}).promise;
1552+
await page.render({canvasContext: context, viewport: viewport}).promise;
15551553

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

1557+
return;
15591558
}
15601559

15611560
}

scripts/queue.js

+63-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var processingTheQueue = {}, queued = {};
1+
var queued = {}, threads = {}, onEnd = {};
22

3-
async function processTheQueue(key)
3+
async function processTheQueue(key, thread = 0)
44
{
55
if(queued[key])
66
{
@@ -20,11 +20,13 @@ async function processTheQueue(key)
2020
console.error(error);
2121
}
2222

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

27-
processTheQueue(key);
27+
process.nextTick(function(){
28+
29+
processTheQueue(key, thread);
2830

2931
});
3032

@@ -33,11 +35,27 @@ async function processTheQueue(key)
3335
}
3436
}
3537

36-
processingTheQueue[key] = false;
38+
threads[key][thread] = null;
39+
40+
checkEnd(key);
3741

3842
return;
3943
}
4044

45+
function processTheQueueThreads(key)
46+
{
47+
for(let i = 0, len = threads[key].length; i < len; i++)
48+
{
49+
if(threads[key][i] === null)
50+
{
51+
threads[key][i] = 1;
52+
processTheQueue(key, i);
53+
54+
break;
55+
}
56+
}
57+
}
58+
4159
async function addToQueue(key, callback)
4260
{
4361
_arguments = [];
@@ -47,36 +65,63 @@ async function addToQueue(key, callback)
4765
_arguments.push(arguments[i]);
4866
}
4967

68+
if(!threads[key]) threads[key] = [null];
69+
5070
if(!queued[key]) queued[key] = [];
5171
queued[key].push({key: key, callback: callback, arguments: _arguments});
5272

53-
if(!processingTheQueue[key])
54-
{
55-
processingTheQueue[key] = true;
73+
processTheQueueThreads(key);
74+
}
5675

57-
process.nextTick(function() {
76+
function cleanQueue(key = false)
77+
{
78+
queued[key] = [];
79+
}
80+
81+
function setThreads(key, num = 1)
82+
{
83+
if(!threads[key]) threads[key] = [null];
5884

59-
processTheQueue(key).catch(function(error){
85+
let _threads = [];
6086

61-
//if(key == 'folderThumbnails')
62-
// dom.compressedError(error);
87+
for(let i = 0; i < num; i++)
88+
{
89+
_threads.push(threads[key][i] || null);
90+
}
6391

64-
//console.error(error);
92+
threads[key] = _threads;
93+
}
6594

66-
});
95+
function checkEnd(key)
96+
{
97+
if(onEnd[key])
98+
{
99+
let allNull = true;
67100

68-
});
101+
for(let i = 0, len = threads[key].length; i < len; i++)
102+
{
103+
if(threads[key][i] !== null)
104+
allNull = false;
105+
}
106+
107+
if(allNull)
108+
{
109+
onEnd[key]();
110+
onEnd[key] = false;
111+
}
69112
}
70113
}
71114

72-
function cleanQueue(key = false)
115+
function end(key, callback)
73116
{
74-
queued[key] = [];
117+
onEnd[key] = callback;
75118
}
76119

77120
module.exports = {
78121
add: addToQueue,
79122
queued: function(){return queued},
80123
clean: cleanQueue,
124+
threads: setThreads,
125+
end: end,
81126
process: processTheQueue,
82127
};

0 commit comments

Comments
 (0)