13
13
* limitations under the License.
14
14
*/
15
15
16
- import { assert , unreachable } from "../shared/util.js" ;
16
+ import { assert , shadow , unreachable } from "../shared/util.js" ;
17
17
import { BaseStream } from "./base_stream.js" ;
18
18
19
19
const EOF = { } ;
@@ -22,21 +22,21 @@ const Name = (function NameClosure() {
22
22
let nameCache = Object . create ( null ) ;
23
23
24
24
// eslint-disable-next-line no-shadow
25
- function Name ( name ) {
26
- this . name = name ;
27
- }
28
-
29
- Name . prototype = { } ;
25
+ class Name {
26
+ constructor ( name ) {
27
+ this . name = name ;
28
+ }
30
29
31
- Name . get = function Name_get ( name ) {
32
- const nameValue = nameCache [ name ] ;
33
- // eslint-disable-next-line no-restricted-syntax
34
- return nameValue ? nameValue : ( nameCache [ name ] = new Name ( name ) ) ;
35
- } ;
30
+ static get ( name ) {
31
+ const nameValue = nameCache [ name ] ;
32
+ // eslint-disable-next-line no-restricted-syntax
33
+ return nameValue ? nameValue : ( nameCache [ name ] = new Name ( name ) ) ;
34
+ }
36
35
37
- Name . _clearCache = function ( ) {
38
- nameCache = Object . create ( null ) ;
39
- } ;
36
+ static _clearCache ( ) {
37
+ nameCache = Object . create ( null ) ;
38
+ }
39
+ }
40
40
41
41
return Name ;
42
42
} ) ( ) ;
@@ -45,142 +45,138 @@ const Cmd = (function CmdClosure() {
45
45
let cmdCache = Object . create ( null ) ;
46
46
47
47
// eslint-disable-next-line no-shadow
48
- function Cmd ( cmd ) {
49
- this . cmd = cmd ;
50
- }
51
-
52
- Cmd . prototype = { } ;
48
+ class Cmd {
49
+ constructor ( cmd ) {
50
+ this . cmd = cmd ;
51
+ }
53
52
54
- Cmd . get = function Cmd_get ( cmd ) {
55
- const cmdValue = cmdCache [ cmd ] ;
56
- // eslint-disable-next-line no-restricted-syntax
57
- return cmdValue ? cmdValue : ( cmdCache [ cmd ] = new Cmd ( cmd ) ) ;
58
- } ;
53
+ static get ( cmd ) {
54
+ const cmdValue = cmdCache [ cmd ] ;
55
+ // eslint-disable-next-line no-restricted-syntax
56
+ return cmdValue ? cmdValue : ( cmdCache [ cmd ] = new Cmd ( cmd ) ) ;
57
+ }
59
58
60
- Cmd . _clearCache = function ( ) {
61
- cmdCache = Object . create ( null ) ;
62
- } ;
59
+ static _clearCache ( ) {
60
+ cmdCache = Object . create ( null ) ;
61
+ }
62
+ }
63
63
64
64
return Cmd ;
65
65
} ) ( ) ;
66
66
67
- const Dict = ( function DictClosure ( ) {
68
- const nonSerializable = function nonSerializableClosure ( ) {
69
- return nonSerializable ; // creating closure on some variable
70
- } ;
67
+ const nonSerializable = function nonSerializableClosure ( ) {
68
+ return nonSerializable ; // Creating closure on some variable.
69
+ } ;
71
70
72
- // xref is optional
73
- // eslint-disable-next-line no-shadow
74
- function Dict ( xref ) {
71
+ class Dict {
72
+ constructor ( xref = null ) {
75
73
// Map should only be used internally, use functions below to access.
76
74
this . _map = Object . create ( null ) ;
77
75
this . xref = xref ;
78
76
this . objId = null ;
79
77
this . suppressEncryption = false ;
80
- this . __nonSerializable__ = nonSerializable ; // disable cloning of the Dict
78
+ this . __nonSerializable__ = nonSerializable ; // Disable cloning of the Dict.
81
79
}
82
80
83
- Dict . prototype = {
84
- assignXref : function Dict_assignXref ( newXref ) {
85
- this . xref = newXref ;
86
- } ,
81
+ assignXref ( newXref ) {
82
+ this . xref = newXref ;
83
+ }
87
84
88
- get size ( ) {
89
- return Object . keys ( this . _map ) . length ;
90
- } ,
85
+ get size ( ) {
86
+ return Object . keys ( this . _map ) . length ;
87
+ }
91
88
92
- // automatically dereferences Ref objects
93
- get ( key1 , key2 , key3 ) {
94
- let value = this . _map [ key1 ] ;
95
- if ( value === undefined && key2 !== undefined ) {
96
- value = this . _map [ key2 ] ;
97
- if ( value === undefined && key3 !== undefined ) {
98
- value = this . _map [ key3 ] ;
99
- }
89
+ // Automatically dereferences Ref objects.
90
+ get ( key1 , key2 , key3 ) {
91
+ let value = this . _map [ key1 ] ;
92
+ if ( value === undefined && key2 !== undefined ) {
93
+ value = this . _map [ key2 ] ;
94
+ if ( value === undefined && key3 !== undefined ) {
95
+ value = this . _map [ key3 ] ;
100
96
}
101
- if ( value instanceof Ref && this . xref ) {
102
- return this . xref . fetch ( value , this . suppressEncryption ) ;
103
- }
104
- return value ;
105
- } ,
106
-
107
- // Same as get(), but returns a promise and uses fetchIfRefAsync().
108
- async getAsync ( key1 , key2 , key3 ) {
109
- let value = this . _map [ key1 ] ;
110
- if ( value === undefined && key2 !== undefined ) {
111
- value = this . _map [ key2 ] ;
112
- if ( value === undefined && key3 !== undefined ) {
113
- value = this . _map [ key3 ] ;
114
- }
115
- }
116
- if ( value instanceof Ref && this . xref ) {
117
- return this . xref . fetchAsync ( value , this . suppressEncryption ) ;
118
- }
119
- return value ;
120
- } ,
97
+ }
98
+ if ( value instanceof Ref && this . xref ) {
99
+ return this . xref . fetch ( value , this . suppressEncryption ) ;
100
+ }
101
+ return value ;
102
+ }
121
103
122
- // Same as get(), but dereferences all elements if the result is an Array.
123
- getArray ( key1 , key2 , key3 ) {
124
- let value = this . get ( key1 , key2 , key3 ) ;
125
- if ( ! Array . isArray ( value ) || ! this . xref ) {
126
- return value ;
127
- }
128
- value = value . slice ( ) ; // Ensure that we don't modify the Dict data.
129
- for ( let i = 0 , ii = value . length ; i < ii ; i ++ ) {
130
- if ( ! ( value [ i ] instanceof Ref ) ) {
131
- continue ;
132
- }
133
- value [ i ] = this . xref . fetch ( value [ i ] , this . suppressEncryption ) ;
104
+ // Same as get(), but returns a promise and uses fetchIfRefAsync().
105
+ async getAsync ( key1 , key2 , key3 ) {
106
+ let value = this . _map [ key1 ] ;
107
+ if ( value === undefined && key2 !== undefined ) {
108
+ value = this . _map [ key2 ] ;
109
+ if ( value === undefined && key3 !== undefined ) {
110
+ value = this . _map [ key3 ] ;
134
111
}
112
+ }
113
+ if ( value instanceof Ref && this . xref ) {
114
+ return this . xref . fetchAsync ( value , this . suppressEncryption ) ;
115
+ }
116
+ return value ;
117
+ }
118
+
119
+ // Same as get(), but dereferences all elements if the result is an Array.
120
+ getArray ( key1 , key2 , key3 ) {
121
+ let value = this . get ( key1 , key2 , key3 ) ;
122
+ if ( ! Array . isArray ( value ) || ! this . xref ) {
135
123
return value ;
136
- } ,
137
-
138
- // no dereferencing
139
- getRaw : function Dict_getRaw ( key ) {
140
- return this . _map [ key ] ;
141
- } ,
142
-
143
- getKeys : function Dict_getKeys ( ) {
144
- return Object . keys ( this . _map ) ;
145
- } ,
146
-
147
- // no dereferencing
148
- getRawValues : function Dict_getRawValues ( ) {
149
- return Object . values ( this . _map ) ;
150
- } ,
151
-
152
- set : function Dict_set ( key , value ) {
153
- if (
154
- ( typeof PDFJSDev === "undefined" ||
155
- PDFJSDev . test ( "!PRODUCTION || TESTING" ) ) &&
156
- value === undefined
157
- ) {
158
- unreachable ( 'Dict.set: The "value" cannot be undefined.' ) ;
124
+ }
125
+ value = value . slice ( ) ; // Ensure that we don't modify the Dict data.
126
+ for ( let i = 0 , ii = value . length ; i < ii ; i ++ ) {
127
+ if ( ! ( value [ i ] instanceof Ref ) ) {
128
+ continue ;
159
129
}
160
- this . _map [ key ] = value ;
161
- } ,
130
+ value [ i ] = this . xref . fetch ( value [ i ] , this . suppressEncryption ) ;
131
+ }
132
+ return value ;
133
+ }
162
134
163
- has : function Dict_has ( key ) {
164
- return this . _map [ key ] !== undefined ;
165
- } ,
135
+ // No dereferencing.
136
+ getRaw ( key ) {
137
+ return this . _map [ key ] ;
138
+ }
166
139
167
- forEach : function Dict_forEach ( callback ) {
168
- for ( const key in this . _map ) {
169
- callback ( key , this . get ( key ) ) ;
170
- }
171
- } ,
172
- } ;
140
+ getKeys ( ) {
141
+ return Object . keys ( this . _map ) ;
142
+ }
143
+
144
+ // No dereferencing.
145
+ getRawValues ( ) {
146
+ return Object . values ( this . _map ) ;
147
+ }
148
+
149
+ set ( key , value ) {
150
+ if (
151
+ ( typeof PDFJSDev === "undefined" ||
152
+ PDFJSDev . test ( "!PRODUCTION || TESTING" ) ) &&
153
+ value === undefined
154
+ ) {
155
+ unreachable ( 'Dict.set: The "value" cannot be undefined.' ) ;
156
+ }
157
+ this . _map [ key ] = value ;
158
+ }
159
+
160
+ has ( key ) {
161
+ return this . _map [ key ] !== undefined ;
162
+ }
173
163
174
- Dict . empty = ( function ( ) {
164
+ forEach ( callback ) {
165
+ for ( const key in this . _map ) {
166
+ callback ( key , this . get ( key ) ) ;
167
+ }
168
+ }
169
+
170
+ static get empty ( ) {
175
171
const emptyDict = new Dict ( null ) ;
176
172
177
173
emptyDict . set = ( key , value ) => {
178
174
unreachable ( "Should not call `set` on the empty dictionary." ) ;
179
175
} ;
180
- return emptyDict ;
181
- } ) ( ) ;
176
+ return shadow ( this , "empty" , emptyDict ) ;
177
+ }
182
178
183
- Dict . merge = function ( { xref, dictArray, mergeSubDicts = false } ) {
179
+ static merge ( { xref, dictArray, mergeSubDicts = false } ) {
184
180
const mergedDict = new Dict ( xref ) ;
185
181
186
182
if ( ! mergeSubDicts ) {
@@ -235,41 +231,39 @@ const Dict = (function DictClosure() {
235
231
properties . clear ( ) ;
236
232
237
233
return mergedDict . size > 0 ? mergedDict : Dict . empty ;
238
- } ;
239
-
240
- return Dict ;
241
- } ) ( ) ;
234
+ }
235
+ }
242
236
243
237
const Ref = ( function RefClosure ( ) {
244
238
let refCache = Object . create ( null ) ;
245
239
246
240
// eslint-disable-next-line no-shadow
247
- function Ref ( num , gen ) {
248
- this . num = num ;
249
- this . gen = gen ;
250
- }
241
+ class Ref {
242
+ constructor ( num , gen ) {
243
+ this . num = num ;
244
+ this . gen = gen ;
245
+ }
251
246
252
- Ref . prototype = {
253
- toString : function Ref_toString ( ) {
247
+ toString ( ) {
254
248
// This function is hot, so we make the string as compact as possible.
255
249
// |this.gen| is almost always zero, so we treat that case specially.
256
250
if ( this . gen === 0 ) {
257
251
return `${ this . num } R` ;
258
252
}
259
253
return `${ this . num } R${ this . gen } ` ;
260
- } ,
261
- } ;
262
-
263
- Ref . get = function ( num , gen ) {
264
- const key = gen === 0 ? ` ${ num } R` : ` ${ num } R ${ gen } ` ;
265
- const refValue = refCache [ key ] ;
266
- // eslint-disable-next-line no-restricted-syntax
267
- return refValue ? refValue : ( refCache [ key ] = new Ref ( num , gen ) ) ;
268
- } ;
269
-
270
- Ref . _clearCache = function ( ) {
271
- refCache = Object . create ( null ) ;
272
- } ;
254
+ }
255
+
256
+ static get ( num , gen ) {
257
+ const key = gen === 0 ? ` ${ num } R` : ` ${ num } R ${ gen } ` ;
258
+ const refValue = refCache [ key ] ;
259
+ // eslint-disable-next-line no-restricted-syntax
260
+ return refValue ? refValue : ( refCache [ key ] = new Ref ( num , gen ) ) ;
261
+ }
262
+
263
+ static _clearCache ( ) {
264
+ refCache = Object . create ( null ) ;
265
+ }
266
+ }
273
267
274
268
return Ref ;
275
269
} ) ( ) ;
0 commit comments