1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 * Creates a new OpenPDFParamsParser. This parses the open pdf parameters
9 * passed in the url to set initial viewport settings for opening the pdf.
10 * @param {Object} getNamedDestinationsFunction The function called to fetch
11 * the page number for a named destination.
13 function OpenPDFParamsParser(getNamedDestinationsFunction) {
14 this.outstandingRequests_ = [];
15 this.getNamedDestinationsFunction_ = getNamedDestinationsFunction;
18 OpenPDFParamsParser.prototype = {
21 * Parse zoom parameter of open PDF parameters. If this
22 * parameter is passed while opening PDF then PDF should be opened
23 * at the specified zoom level.
24 * @param {number} zoom value.
25 * @param {Object} viewportPosition to store zoom and position value.
27 parseZoomParam_: function(paramValue, viewportPosition) {
28 var paramValueSplit = paramValue.split(',');
29 if ((paramValueSplit.length != 1) && (paramValueSplit.length != 3))
32 // User scale of 100 means zoom value of 100% i.e. zoom factor of 1.0.
33 var zoomFactor = parseFloat(paramValueSplit[0]) / 100;
34 if (isNaN(zoomFactor))
37 // Handle #zoom=scale.
38 if (paramValueSplit.length == 1) {
39 viewportPosition['zoom'] = zoomFactor;
43 // Handle #zoom=scale,left,top.
44 var position = {x: parseFloat(paramValueSplit[1]),
45 y: parseFloat(paramValueSplit[2])};
46 viewportPosition['position'] = position;
47 viewportPosition['zoom'] = zoomFactor;
52 * Parse PDF url parameters. These parameters are mentioned in the url
53 * and specify actions to be performed when opening pdf files.
54 * See http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/
55 * pdfs/pdf_open_parameters.pdf for details.
56 * @param {string} url that needs to be parsed.
57 * @param {Function} callback function to be called with viewport info.
59 getViewportFromUrlParams: function(url, callback) {
60 var viewportPosition = {};
61 viewportPosition['url'] = url;
62 var paramIndex = url.search('#');
63 if (paramIndex == -1) {
64 callback(viewportPosition);
68 var paramTokens = url.substring(paramIndex + 1).split('&');
69 if ((paramTokens.length == 1) && (paramTokens[0].search('=') == -1)) {
70 // Handle the case of http://foo.com/bar#NAMEDDEST. This is not
71 // explicitly mentioned except by example in the Adobe
72 // "PDF Open Parameters" document.
73 this.outstandingRequests_.push({
75 viewportPosition: viewportPosition
77 this.getNamedDestinationsFunction_(paramTokens[0]);
81 var paramsDictionary = {};
82 for (var i = 0; i < paramTokens.length; ++i) {
83 var keyValueSplit = paramTokens[i].split('=');
84 if (keyValueSplit.length != 2)
86 paramsDictionary[keyValueSplit[0]] = keyValueSplit[1];
89 if ('page' in paramsDictionary) {
90 // |pageNumber| is 1-based, but goToPage() take a zero-based page number.
91 var pageNumber = parseInt(paramsDictionary['page']);
92 if (!isNaN(pageNumber) && pageNumber > 0)
93 viewportPosition['page'] = pageNumber - 1;
96 if ('zoom' in paramsDictionary)
97 this.parseZoomParam_(paramsDictionary['zoom'], viewportPosition);
99 if (viewportPosition.page === undefined &&
100 'nameddest' in paramsDictionary) {
101 this.outstandingRequests_.push({
103 viewportPosition: viewportPosition
105 this.getNamedDestinationsFunction_(paramsDictionary['nameddest']);
107 callback(viewportPosition);
112 * This is called when a named destination is received and the page number
113 * corresponding to the request for which a named destination is passed.
114 * @param {number} pageNumber The page corresponding to the named destination
117 onNamedDestinationReceived: function(pageNumber) {
118 var outstandingRequest = this.outstandingRequests_.shift();
119 if (pageNumber != -1)
120 outstandingRequest.viewportPosition.page = pageNumber;
121 outstandingRequest.callback(outstandingRequest.viewportPosition);