Require destinations when they are needed and do not fetch all of them in advance
This commit is contained in:
		
							parent
							
								
									fb6d87c77b
								
							
						
					
					
						commit
						b215af30d3
					
				@ -457,6 +457,46 @@ var Catalog = (function CatalogClosure() {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
      return shadow(this, 'destinations', dests);
 | 
					      return shadow(this, 'destinations', dests);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    getDestination: function Catalog_getDestination(destinationId) {
 | 
				
			||||||
 | 
					      function fetchDestination(dest) {
 | 
				
			||||||
 | 
					        return isDict(dest) ? dest.get('D') : dest;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      var xref = this.xref;
 | 
				
			||||||
 | 
					      var dest, nameTreeRef, nameDictionaryRef;
 | 
				
			||||||
 | 
					      var obj = this.catDict.get('Names');
 | 
				
			||||||
 | 
					      if (obj && obj.has('Dests')) {
 | 
				
			||||||
 | 
					        nameTreeRef = obj.getRaw('Dests');
 | 
				
			||||||
 | 
					      } else if (this.catDict.has('Dests')) {
 | 
				
			||||||
 | 
					        nameDictionaryRef = this.catDict.get('Dests');
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (nameDictionaryRef) {
 | 
				
			||||||
 | 
					        // reading simple destination dictionary
 | 
				
			||||||
 | 
					        obj = nameDictionaryRef;
 | 
				
			||||||
 | 
					        obj.forEach(function catalogForEach(key, value) {
 | 
				
			||||||
 | 
					          if (!value) {
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          if (key === destinationId) {
 | 
				
			||||||
 | 
					            dest = fetchDestination(value);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      if (nameTreeRef) {
 | 
				
			||||||
 | 
					        var nameTree = new NameTree(nameTreeRef, xref);
 | 
				
			||||||
 | 
					        var names = nameTree.getAll();
 | 
				
			||||||
 | 
					        for (var name in names) {
 | 
				
			||||||
 | 
					          if (!names.hasOwnProperty(name)) {
 | 
				
			||||||
 | 
					            continue;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					          if (name === destinationId) {
 | 
				
			||||||
 | 
					            dest = fetchDestination(names[name]);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return dest;
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    get attachments() {
 | 
					    get attachments() {
 | 
				
			||||||
      var xref = this.xref;
 | 
					      var xref = this.xref;
 | 
				
			||||||
      var attachments = null, nameTreeRef;
 | 
					      var attachments = null, nameTreeRef;
 | 
				
			||||||
 | 
				
			|||||||
@ -325,6 +325,12 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    handler.on('GetDestination',
 | 
				
			||||||
 | 
					      function wphSetupGetDestination(data) {
 | 
				
			||||||
 | 
					        return pdfManager.ensureCatalog('getDestination', [ data.id ]);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    handler.on('GetAttachments',
 | 
					    handler.on('GetAttachments',
 | 
				
			||||||
      function wphSetupGetAttachments(data) {
 | 
					      function wphSetupGetAttachments(data) {
 | 
				
			||||||
        return pdfManager.ensureCatalog('attachments');
 | 
					        return pdfManager.ensureCatalog('attachments');
 | 
				
			||||||
 | 
				
			|||||||
@ -293,10 +293,20 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
 | 
				
			|||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @return {Promise} A promise that is resolved with a lookup table for
 | 
					     * @return {Promise} A promise that is resolved with a lookup table for
 | 
				
			||||||
     * mapping named destinations to reference numbers.
 | 
					     * mapping named destinations to reference numbers.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * This can be slow for large documents: use getDestination instead
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    getDestinations: function PDFDocumentProxy_getDestinations() {
 | 
					    getDestinations: function PDFDocumentProxy_getDestinations() {
 | 
				
			||||||
      return this.transport.getDestinations();
 | 
					      return this.transport.getDestinations();
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @param {string} id The named destination to get.
 | 
				
			||||||
 | 
					     * @return {Promise} A promise that is resolved with all information
 | 
				
			||||||
 | 
					     * of the given named destination.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    getDestination: function PDFDocumentProxy_getDestination(id) {
 | 
				
			||||||
 | 
					      return this.transport.getDestination(id);
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @return {Promise} A promise that is resolved with a lookup table for
 | 
					     * @return {Promise} A promise that is resolved with a lookup table for
 | 
				
			||||||
     * mapping named attachments to their content.
 | 
					     * mapping named attachments to their content.
 | 
				
			||||||
@ -1128,6 +1138,10 @@ var WorkerTransport = (function WorkerTransportClosure() {
 | 
				
			|||||||
      return this.messageHandler.sendWithPromise('GetDestinations', null);
 | 
					      return this.messageHandler.sendWithPromise('GetDestinations', null);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getDestination: function WorkerTransport_getDestination(id) {
 | 
				
			||||||
 | 
					      return this.messageHandler.sendWithPromise('GetDestination', { id: id } );
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    getAttachments: function WorkerTransport_getAttachments() {
 | 
					    getAttachments: function WorkerTransport_getAttachments() {
 | 
				
			||||||
      return this.messageHandler.sendWithPromise('GetAttachments', null);
 | 
					      return this.messageHandler.sendWithPromise('GetAttachments', null);
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
				
			|||||||
@ -123,6 +123,13 @@ describe('api', function() {
 | 
				
			|||||||
                                          0, 841.89, null] });
 | 
					                                          0, 841.89, null] });
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					    it('gets a destination', function() {
 | 
				
			||||||
 | 
					      var promise = doc.getDestination('chapter1');
 | 
				
			||||||
 | 
					      waitsForPromiseResolved(promise, function(data) {
 | 
				
			||||||
 | 
					        expect(data).toEqual([{ gen: 0, num: 17 }, { name: 'XYZ' },
 | 
				
			||||||
 | 
					                              0, 841.89, null]);
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
    it('gets attachments', function() {
 | 
					    it('gets attachments', function() {
 | 
				
			||||||
      var promise = doc.getAttachments();
 | 
					      var promise = doc.getAttachments();
 | 
				
			||||||
      waitsForPromiseResolved(promise, function (data) {
 | 
					      waitsForPromiseResolved(promise, function (data) {
 | 
				
			||||||
 | 
				
			|||||||
@ -675,15 +675,19 @@ var PDFViewerApplication = {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.destinationsPromise.then(function() {
 | 
					    var destinationPromise;
 | 
				
			||||||
    if (typeof dest === 'string') {
 | 
					    if (typeof dest === 'string') {
 | 
				
			||||||
      destString = dest;
 | 
					      destString = dest;
 | 
				
			||||||
        dest = self.destinations[dest];
 | 
					      destinationPromise = this.pdfDocument.getDestination(dest);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      destinationPromise = Promise.resolve(dest);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
      if (!(dest instanceof Array)) {
 | 
					    destinationPromise.then(function(destination) {
 | 
				
			||||||
 | 
					      dest = destination;
 | 
				
			||||||
 | 
					      if (!(destination instanceof Array)) {
 | 
				
			||||||
        return; // invalid destination
 | 
					        return; // invalid destination
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      goToDestination(dest[0]);
 | 
					      goToDestination(destination[0]);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -984,15 +988,8 @@ var PDFViewerApplication = {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var destinationsPromise =
 | 
					    // outline depends on pagesRefMap
 | 
				
			||||||
      this.destinationsPromise = pdfDocument.getDestinations();
 | 
					    var promises = [pagesPromise, this.animationStartedPromise];
 | 
				
			||||||
    destinationsPromise.then(function(destinations) {
 | 
					 | 
				
			||||||
      self.destinations = destinations;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // outline depends on destinations and pagesRefMap
 | 
					 | 
				
			||||||
    var promises = [pagesPromise, destinationsPromise,
 | 
					 | 
				
			||||||
                    this.animationStartedPromise];
 | 
					 | 
				
			||||||
    Promise.all(promises).then(function() {
 | 
					    Promise.all(promises).then(function() {
 | 
				
			||||||
      pdfDocument.getOutline().then(function(outline) {
 | 
					      pdfDocument.getOutline().then(function(outline) {
 | 
				
			||||||
        var outlineView = document.getElementById('outlineView');
 | 
					        var outlineView = document.getElementById('outlineView');
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user