Merge pull request #10709 from Snuffleupagus/pageLayout
[api-minor] Add basic support for PageLayout in the API and the viewer
This commit is contained in:
		
						commit
						b161050df4
					
				@ -376,6 +376,27 @@ class Catalog {
 | 
				
			|||||||
    return pageLabels;
 | 
					    return pageLabels;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  get pageLayout() {
 | 
				
			||||||
 | 
					    const obj = this.catDict.get('PageLayout');
 | 
				
			||||||
 | 
					    // Purposely use a non-standard default value, rather than 'SinglePage', to
 | 
				
			||||||
 | 
					    // allow differentiating between `undefined` and /SinglePage since that does
 | 
				
			||||||
 | 
					    // affect the Scroll mode (continuous/non-continuous) used in Adobe Reader.
 | 
				
			||||||
 | 
					    let pageLayout = '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (isName(obj)) {
 | 
				
			||||||
 | 
					      switch (obj.name) {
 | 
				
			||||||
 | 
					        case 'SinglePage':
 | 
				
			||||||
 | 
					        case 'OneColumn':
 | 
				
			||||||
 | 
					        case 'TwoColumnLeft':
 | 
				
			||||||
 | 
					        case 'TwoColumnRight':
 | 
				
			||||||
 | 
					        case 'TwoPageLeft':
 | 
				
			||||||
 | 
					        case 'TwoPageRight':
 | 
				
			||||||
 | 
					          pageLayout = obj.name;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return shadow(this, 'pageLayout', pageLayout);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  get pageMode() {
 | 
					  get pageMode() {
 | 
				
			||||||
    const obj = this.catDict.get('PageMode');
 | 
					    const obj = this.catDict.get('PageMode');
 | 
				
			||||||
    let pageMode = 'UseNone'; // Default value.
 | 
					    let pageMode = 'UseNone'; // Default value.
 | 
				
			||||||
 | 
				
			|||||||
@ -517,6 +517,10 @@ var WorkerMessageHandler = {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    handler.on('GetPageLayout', function wphSetupGetPageLayout(data) {
 | 
				
			||||||
 | 
					      return pdfManager.ensureCatalog('pageLayout');
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    handler.on('GetPageMode', function wphSetupGetPageMode(data) {
 | 
					    handler.on('GetPageMode', function wphSetupGetPageMode(data) {
 | 
				
			||||||
      return pdfManager.ensureCatalog('pageMode');
 | 
					      return pdfManager.ensureCatalog('pageMode');
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
				
			|||||||
@ -657,6 +657,14 @@ class PDFDocumentProxy {
 | 
				
			|||||||
    return this._transport.getPageLabels();
 | 
					    return this._transport.getPageLabels();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * @return {Promise} A promise that is resolved with a {string} containing
 | 
				
			||||||
 | 
					   *   the page layout name.
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
 | 
					  getPageLayout() {
 | 
				
			||||||
 | 
					    return this._transport.getPageLayout();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * @return {Promise} A promise that is resolved with a {string} containing
 | 
					   * @return {Promise} A promise that is resolved with a {string} containing
 | 
				
			||||||
   *   the page mode name.
 | 
					   *   the page mode name.
 | 
				
			||||||
@ -2214,6 +2222,10 @@ class WorkerTransport {
 | 
				
			|||||||
    return this.messageHandler.sendWithPromise('GetPageLabels', null);
 | 
					    return this.messageHandler.sendWithPromise('GetPageLabels', null);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  getPageLayout() {
 | 
				
			||||||
 | 
					    return this.messageHandler.sendWithPromise('GetPageLayout', null);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getPageMode() {
 | 
					  getPageMode() {
 | 
				
			||||||
    return this.messageHandler.sendWithPromise('GetPageMode', null);
 | 
					    return this.messageHandler.sendWithPromise('GetPageMode', null);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -610,6 +610,24 @@ describe('api', function() {
 | 
				
			|||||||
      }).catch(done.fail);
 | 
					      }).catch(done.fail);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('gets default page layout', function(done) {
 | 
				
			||||||
 | 
					      var loadingTask = getDocument(buildGetDocumentParams('tracemonkey.pdf'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      loadingTask.promise.then(function(pdfDocument) {
 | 
				
			||||||
 | 
					        return pdfDocument.getPageLayout();
 | 
				
			||||||
 | 
					      }).then(function(mode) {
 | 
				
			||||||
 | 
					        expect(mode).toEqual('');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        loadingTask.destroy().then(done);
 | 
				
			||||||
 | 
					      }).catch(done.fail);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    it('gets non-default page layout', function(done) {
 | 
				
			||||||
 | 
					      doc.getPageLayout().then(function(mode) {
 | 
				
			||||||
 | 
					        expect(mode).toEqual('SinglePage');
 | 
				
			||||||
 | 
					        done();
 | 
				
			||||||
 | 
					      }).catch(done.fail);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    it('gets default page mode', function(done) {
 | 
					    it('gets default page mode', function(done) {
 | 
				
			||||||
      var loadingTask = getDocument(buildGetDocumentParams('tracemonkey.pdf'));
 | 
					      var loadingTask = getDocument(buildGetDocumentParams('tracemonkey.pdf'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										32
									
								
								web/app.js
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								web/app.js
									
									
									
									
									
								
							@ -889,6 +889,8 @@ let PDFViewerApplication = {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Since the `setInitialView` call below depends on this being resolved,
 | 
					    // Since the `setInitialView` call below depends on this being resolved,
 | 
				
			||||||
    // fetch it early to avoid delaying initial rendering of the PDF document.
 | 
					    // fetch it early to avoid delaying initial rendering of the PDF document.
 | 
				
			||||||
 | 
					    const pageLayoutPromise = pdfDocument.getPageLayout().catch(
 | 
				
			||||||
 | 
					      function() { /* Avoid breaking initial rendering; ignoring errors. */ });
 | 
				
			||||||
    const pageModePromise = pdfDocument.getPageMode().catch(
 | 
					    const pageModePromise = pdfDocument.getPageMode().catch(
 | 
				
			||||||
      function() { /* Avoid breaking initial rendering; ignoring errors. */ });
 | 
					      function() { /* Avoid breaking initial rendering; ignoring errors. */ });
 | 
				
			||||||
    const openActionDestPromise = pdfDocument.getOpenActionDestination().catch(
 | 
					    const openActionDestPromise = pdfDocument.getOpenActionDestination().catch(
 | 
				
			||||||
@ -934,8 +936,8 @@ let PDFViewerApplication = {
 | 
				
			|||||||
      }).catch(() => { /* Unable to read from storage; ignoring errors. */ });
 | 
					      }).catch(() => { /* Unable to read from storage; ignoring errors. */ });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      Promise.all([
 | 
					      Promise.all([
 | 
				
			||||||
        storePromise, pageModePromise, openActionDestPromise,
 | 
					        storePromise, pageLayoutPromise, pageModePromise, openActionDestPromise,
 | 
				
			||||||
      ]).then(async ([values = {}, pageMode, openActionDest]) => {
 | 
					      ]).then(async ([values = {}, pageLayout, pageMode, openActionDest]) => {
 | 
				
			||||||
        const viewOnLoad = AppOptions.get('viewOnLoad');
 | 
					        const viewOnLoad = AppOptions.get('viewOnLoad');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this._initializePdfHistory({
 | 
					        this._initializePdfHistory({
 | 
				
			||||||
@ -974,6 +976,9 @@ let PDFViewerApplication = {
 | 
				
			|||||||
        if (pageMode && sidebarView === SidebarView.UNKNOWN) {
 | 
					        if (pageMode && sidebarView === SidebarView.UNKNOWN) {
 | 
				
			||||||
          sidebarView = apiPageModeToSidebarView(pageMode);
 | 
					          sidebarView = apiPageModeToSidebarView(pageMode);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        if (pageLayout && spreadMode === SpreadMode.UNKNOWN) {
 | 
				
			||||||
 | 
					          spreadMode = apiPageLayoutToSpreadMode(pageLayout);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.setInitialView(hash, {
 | 
					        this.setInitialView(hash, {
 | 
				
			||||||
          rotation, sidebarView, scrollMode, spreadMode,
 | 
					          rotation, sidebarView, scrollMode, spreadMode,
 | 
				
			||||||
@ -2434,6 +2439,29 @@ function webViewerKeyDown(evt) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Converts API PageLayout values to the format used by `PDFViewer`.
 | 
				
			||||||
 | 
					 * NOTE: This is supported to the extent that the viewer implements the
 | 
				
			||||||
 | 
					 *       necessary Scroll/Spread modes (since SinglePage, TwoPageLeft,
 | 
				
			||||||
 | 
					 *       and TwoPageRight all suggests using non-continuous scrolling).
 | 
				
			||||||
 | 
					 * @param {string} mode - The API PageLayout value.
 | 
				
			||||||
 | 
					 * @returns {number} A value from {SpreadMode}.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function apiPageLayoutToSpreadMode(layout) {
 | 
				
			||||||
 | 
					  switch (layout) {
 | 
				
			||||||
 | 
					    case 'SinglePage':
 | 
				
			||||||
 | 
					    case 'OneColumn':
 | 
				
			||||||
 | 
					      return SpreadMode.NONE;
 | 
				
			||||||
 | 
					    case 'TwoColumnLeft':
 | 
				
			||||||
 | 
					    case 'TwoPageLeft':
 | 
				
			||||||
 | 
					      return SpreadMode.ODD;
 | 
				
			||||||
 | 
					    case 'TwoColumnRight':
 | 
				
			||||||
 | 
					    case 'TwoPageRight':
 | 
				
			||||||
 | 
					      return SpreadMode.EVEN;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return SpreadMode.NONE; // Default value.
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Converts API PageMode values to the format used by `PDFSidebar`.
 | 
					 * Converts API PageMode values to the format used by `PDFSidebar`.
 | 
				
			||||||
 * NOTE: There's also a "FullScreen" parameter which is not possible to support,
 | 
					 * NOTE: There's also a "FullScreen" parameter which is not possible to support,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user