@@ -31,6 +31,13 @@ const DRAW_UPSCALE_FACTOR = 2; // See comment in `PDFThumbnailView.draw` below.
31
31
const MAX_NUM_SCALING_STEPS = 3 ;
32
32
const THUMBNAIL_WIDTH = 98 ; // px
33
33
34
+ function zeroCanvas ( c ) {
35
+ // Zeroing the width and height causes Firefox to release graphics
36
+ // resources immediately, which can greatly reduce memory consumption.
37
+ c . width = 0 ;
38
+ c . height = 0 ;
39
+ }
40
+
34
41
/**
35
42
* @typedef {Object } PDFThumbnailViewOptions
36
43
* @property {HTMLDivElement } container - The viewer element.
@@ -74,12 +81,8 @@ class TempImageFactory {
74
81
}
75
82
76
83
static destroyCanvas ( ) {
77
- const tempCanvas = this . #tempCanvas;
78
- if ( tempCanvas ) {
79
- // Zeroing the width and height causes Firefox to release graphics
80
- // resources immediately, which can greatly reduce memory consumption.
81
- tempCanvas . width = 0 ;
82
- tempCanvas . height = 0 ;
84
+ if ( this . #tempCanvas) {
85
+ zeroCanvas ( this . #tempCanvas) ;
83
86
}
84
87
this . #tempCanvas = null ;
85
88
}
@@ -255,37 +258,15 @@ class PDFThumbnailView {
255
258
this . div . setAttribute ( "data-loaded" , true ) ;
256
259
this . _placeholderImg . replaceWith ( image ) ;
257
260
258
- // Zeroing the width and height causes Firefox to release graphics
259
- // resources immediately, which can greatly reduce memory consumption.
260
- reducedCanvas . width = 0 ;
261
- reducedCanvas . height = 0 ;
262
- }
263
-
264
- async #finishRenderTask( renderTask , canvas , error = null ) {
265
- // The renderTask may have been replaced by a new one, so only remove
266
- // the reference to the renderTask if it matches the one that is
267
- // triggering this callback.
268
- if ( renderTask === this . renderTask ) {
269
- this . renderTask = null ;
270
- }
271
-
272
- if ( error instanceof RenderingCancelledException ) {
273
- return ;
274
- }
275
- this . renderingState = RenderingStates . FINISHED ;
276
- this . #convertCanvasToImage( canvas ) ;
277
-
278
- if ( error ) {
279
- throw error ;
280
- }
261
+ zeroCanvas ( reducedCanvas ) ;
281
262
}
282
263
283
264
async draw ( ) {
284
265
if ( this . renderingState !== RenderingStates . INITIAL ) {
285
266
console . error ( "Must be in new state before drawing" ) ;
286
- return undefined ;
267
+ return ;
287
268
}
288
- const { pdfPage } = this ;
269
+ const { pageColors , pdfPage } = this ;
289
270
290
271
if ( ! pdfPage ) {
291
272
this . renderingState = RenderingStates . FINISHED ;
@@ -321,29 +302,42 @@ class PDFThumbnailView {
321
302
transform,
322
303
viewport : drawViewport ,
323
304
optionalContentConfigPromise : this . _optionalContentConfigPromise ,
324
- pageColors : this . pageColors ,
305
+ pageColors,
325
306
} ;
326
307
const renderTask = ( this . renderTask = pdfPage . render ( renderContext ) ) ;
327
308
renderTask . onContinue = renderContinueCallback ;
328
309
329
- const resultPromise = renderTask . promise . then (
330
- ( ) => this . #finishRenderTask( renderTask , canvas ) ,
331
- error => this . #finishRenderTask( renderTask , canvas , error )
332
- ) ;
333
- resultPromise . finally ( ( ) => {
334
- // Zeroing the width and height causes Firefox to release graphics
335
- // resources immediately, which can greatly reduce memory consumption.
336
- canvas . width = 0 ;
337
- canvas . height = 0 ;
338
-
339
- this . eventBus . dispatch ( "thumbnailrendered" , {
340
- source : this ,
341
- pageNumber : this . id ,
342
- pdfPage : this . pdfPage ,
343
- } ) ;
310
+ let error = null ;
311
+ try {
312
+ await renderTask . promise ;
313
+ } catch ( e ) {
314
+ if ( e instanceof RenderingCancelledException ) {
315
+ zeroCanvas ( canvas ) ;
316
+ return ;
317
+ }
318
+ error = e ;
319
+ } finally {
320
+ // The renderTask may have been replaced by a new one, so only remove
321
+ // the reference to the renderTask if it matches the one that is
322
+ // triggering this callback.
323
+ if ( renderTask === this . renderTask ) {
324
+ this . renderTask = null ;
325
+ }
326
+ }
327
+ this . renderingState = RenderingStates . FINISHED ;
328
+
329
+ this . #convertCanvasToImage( canvas ) ;
330
+ zeroCanvas ( canvas ) ;
331
+
332
+ this . eventBus . dispatch ( "thumbnailrendered" , {
333
+ source : this ,
334
+ pageNumber : this . id ,
335
+ pdfPage,
344
336
} ) ;
345
337
346
- return resultPromise ;
338
+ if ( error ) {
339
+ throw error ;
340
+ }
347
341
}
348
342
349
343
setImage ( pageView ) {
@@ -366,24 +360,21 @@ class PDFThumbnailView {
366
360
}
367
361
368
362
#getReducedImageDims( canvas ) {
369
- let reducedWidth = canvas . width << MAX_NUM_SCALING_STEPS ,
370
- reducedHeight = canvas . height << MAX_NUM_SCALING_STEPS ;
363
+ const width = canvas . width << MAX_NUM_SCALING_STEPS ,
364
+ height = canvas . height << MAX_NUM_SCALING_STEPS ;
371
365
372
366
const outputScale = new OutputScale ( ) ;
373
367
// Here we're not actually "rendering" to the canvas and the `OutputScale`
374
368
// is thus only used to limit the canvas size, hence the identity scale.
375
369
outputScale . sx = outputScale . sy = 1 ;
376
370
377
371
outputScale . limitCanvas (
378
- reducedWidth ,
379
- reducedHeight ,
372
+ width ,
373
+ height ,
380
374
this . maxCanvasPixels ,
381
375
this . maxCanvasDim
382
376
) ;
383
- reducedWidth = ( reducedWidth * outputScale . sx ) | 0 ;
384
- reducedHeight = ( reducedHeight * outputScale . sy ) | 0 ;
385
-
386
- return [ reducedWidth , reducedHeight ] ;
377
+ return [ ( width * outputScale . sx ) | 0 , ( height * outputScale . sy ) | 0 ] ;
387
378
}
388
379
389
380
#reduceImage( img ) {
0 commit comments