From 26fc79d51d991be6b441f5da704c229a0cae467a Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sun, 12 Feb 2017 23:05:10 +0100 Subject: [PATCH] Annotations: refactor setting the normal appearance stream Previously, we had a function called `getDefaultAppearance`. This name, however, is misleading as the method gets the normal appearance (in the `N` entry) and not the default appearance (in the `DA` entry). Moreover, it was not entirely clear how it works just from reading the code. It primarily lacks comments and explicit error case handling. This patch improves the situation by fixing the issues mentioned above and making this function a proper method of the `Annotation` class, just like e.g., `setColor` and `setBorderStyle`. --- src/core/annotation.js | 56 +++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/core/annotation.js b/src/core/annotation.js index 212485448..8253864a8 100644 --- a/src/core/annotation.js +++ b/src/core/annotation.js @@ -47,6 +47,7 @@ var Dict = corePrimitives.Dict; var isDict = corePrimitives.isDict; var isName = corePrimitives.isName; var isRef = corePrimitives.isRef; +var isStream = corePrimitives.isStream; var Stream = coreStream.Stream; var ColorSpace = coreColorSpace.ColorSpace; var Catalog = coreObj.Catalog; @@ -168,25 +169,6 @@ var Annotation = (function AnnotationClosure() { ]; } - function getDefaultAppearance(dict) { - var appearanceState = dict.get('AP'); - if (!isDict(appearanceState)) { - return; - } - - var appearance; - var appearances = appearanceState.get('N'); - if (isDict(appearances)) { - var as = dict.get('AS'); - if (as && appearances.has(as.name)) { - appearance = appearances.get(as.name); - } - } else { - appearance = appearances; - } - return appearance; - } - function Annotation(params) { var dict = params.dict; @@ -194,7 +176,7 @@ var Annotation = (function AnnotationClosure() { this.setRectangle(dict.getArray('Rect')); this.setColor(dict.getArray('C')); this.setBorderStyle(dict); - this.appearance = getDefaultAppearance(dict); + this.setAppearance(dict); // Expose public properties using a data object. this.data = {}; @@ -379,6 +361,40 @@ var Annotation = (function AnnotationClosure() { } }, + /** + * Set the (normal) appearance. + * + * @public + * @memberof Annotation + * @param {Dict} dict - The annotation's data dictionary + */ + setAppearance: function Annotation_setAppearance(dict) { + this.appearance = null; + + var appearanceStates = dict.get('AP'); + if (!isDict(appearanceStates)) { + return; + } + + // In case the normal appearance is a stream, then it is used directly. + var normalAppearanceState = appearanceStates.get('N'); + if (isStream(normalAppearanceState)) { + this.appearance = normalAppearanceState; + return; + } + if (!isDict(normalAppearanceState)) { + return; + } + + // In case the normal appearance is a dictionary, the `AS` entry provides + // the key of the stream in this dictionary. + var as = dict.get('AS'); + if (!isName(as) || !normalAppearanceState.has(as.name)) { + return; + } + this.appearance = normalAppearanceState.get(as.name); + }, + /** * Prepare the annotation for working with a popup in the display layer. *