1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

[api-minor] Let LinkAnnotation/PDFLinkService_getDestinationHash return a stringified version of the destination array for explicit destinations

Currently for explicit destinations, compared to named destinations, we manually try to build a hash that often times is a quite poor representation of the *actual* destination. (Currently this only, kind of, works for `\XYZ` destinations.)
For PDF files using explicit destinations, this can make it difficult/impossible to obtain a link to a specific section of the document through the URL.

Note that in practice most PDF files, especially newer ones, use named destinations and these are thus unnaffected by this patch.
This patch also fixes an existing issue in `PDFLinkService_getDestinationHash`, where a named destination consisting of only a number would not be handled correctly.

With the added, and already existing, type checks in place for destinations, I really don't think that this patch exposes any "sensitive" internal destination code not already accessible through normal hash parameters.

*Please note:* Just trying to improve the algorithm that generates the hash is unfortunately not possible in general, since there are a number of cases where it will simply never work well.

 - First of all, note that `getDestinationHash` currently relies on the `_pagesRefCache`, hence it's possible that the hash returned is empty during e.g. ranged/streamed loading of a PDF file.

 - Second of all, the currently computed hash is actually dependent on the document rotation. With named destinations, the fetched internal destination array is rotational invariant (as it should be), but this will not hold in general for the hash. We can easily avoid this issue by using a stringified destination array.

 - Third of all, note that according to the PDF specification[1], `GoToR` destinations may actually contain explicit destination arrays. Since we cannot really construct a hash in `annotation.js`, we currently have no good way to support those. Even though this case seems *very* rare in practice (I've not actually seen such a PDF file), it's in the specification, and this patch allows us to support that for "free".

---
[1] http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf#G11.1951685
This commit is contained in:
Jonas Jenwald 2016-05-15 12:12:47 +02:00
parent 01ab15a6f1
commit b354682dd6
5 changed files with 155 additions and 39 deletions

View file

@ -268,8 +268,9 @@ describe('Annotation layer', function() {
var actionDict = new Dict();
actionDict.set('Type', Name.get('Action'));
actionDict.set('S', Name.get('GoToR'));
actionDict.set('F', '../../0021/002156/215675E.pdf');
actionDict.set('D', '15');
actionDict.set('F', '../../0013/001346/134685E.pdf');
actionDict.set('D', '4.3');
actionDict.set('NewWindow', true);
var annotationDict = new Dict();
annotationDict.set('Type', Name.get('Annot'));
@ -283,7 +284,58 @@ describe('Annotation layer', function() {
var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK);
expect(data.url).toBeUndefined();
expect(data.url).toBeUndefined(); // ../../0013/001346/134685E.pdf#4.3
expect(data.dest).toBeUndefined();
expect(data.newWindow).toEqual(true);
});
it('should correctly parse a GoToR action, with named destination',
function() {
var actionDict = new Dict();
actionDict.set('Type', Name.get('Action'));
actionDict.set('S', Name.get('GoToR'));
actionDict.set('F', 'http://www.example.com/test.pdf');
actionDict.set('D', '15');
var annotationDict = new Dict();
annotationDict.set('Type', Name.get('Annot'));
annotationDict.set('Subtype', Name.get('Link'));
annotationDict.set('A', actionDict);
var xrefMock = new XrefMock([annotationDict]);
var annotationRef = new Ref(495, 0);
var annotation = annotationFactory.create(xrefMock, annotationRef);
var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK);
expect(data.url).toEqual('http://www.example.com/test.pdf#nameddest=15');
expect(data.dest).toBeUndefined();
expect(data.newWindow).toBeFalsy();
});
it('should correctly parse a GoToR action, with explicit destination array',
function() {
var actionDict = new Dict();
actionDict.set('Type', Name.get('Action'));
actionDict.set('S', Name.get('GoToR'));
actionDict.set('F', 'http://www.example.com/test.pdf');
actionDict.set('D', [14, Name.get('XYZ'), null, 298.043, null]);
var annotationDict = new Dict();
annotationDict.set('Type', Name.get('Annot'));
annotationDict.set('Subtype', Name.get('Link'));
annotationDict.set('A', actionDict);
var xrefMock = new XrefMock([annotationDict]);
var annotationRef = new Ref(489, 0);
var annotation = annotationFactory.create(xrefMock, annotationRef);
var data = annotation.data;
expect(data.annotationType).toEqual(AnnotationType.LINK);
expect(data.url).toEqual('http://www.example.com/test.pdf#' +
'[14,{"name":"XYZ"},null,298.043,null]');
expect(data.dest).toBeUndefined();
expect(data.newWindow).toBeFalsy();
});