@@ -21,26 +21,23 @@ import {
21
21
isString ,
22
22
removeNullCharacters ,
23
23
stringToBytes ,
24
+ unreachable ,
24
25
Util ,
25
26
warn ,
26
27
} from "../shared/util.js" ;
27
28
28
29
const DEFAULT_LINK_REL = "noopener noreferrer nofollow" ;
29
30
const SVG_NS = "http://www.w3.org/2000/svg" ;
30
31
31
- class DOMCanvasFactory {
32
- create ( width , height ) {
33
- if ( width <= 0 || height <= 0 ) {
34
- throw new Error ( "Invalid canvas size ") ;
32
+ class BaseCanvasFactory {
33
+ constructor ( ) {
34
+ if ( this . constructor === BaseCanvasFactory ) {
35
+ unreachable ( "Cannot initialize BaseCanvasFactory. ") ;
35
36
}
36
- const canvas = document . createElement ( "canvas" ) ;
37
- const context = canvas . getContext ( "2d" ) ;
38
- canvas . width = width ;
39
- canvas . height = height ;
40
- return {
41
- canvas,
42
- context,
43
- } ;
37
+ }
38
+
39
+ create ( width , height ) {
40
+ unreachable ( "Abstract method `create` called." ) ;
44
41
}
45
42
46
43
reset ( canvasAndContext , width , height ) {
@@ -67,8 +64,27 @@ class DOMCanvasFactory {
67
64
}
68
65
}
69
66
70
- class DOMCMapReaderFactory {
67
+ class DOMCanvasFactory extends BaseCanvasFactory {
68
+ create ( width , height ) {
69
+ if ( width <= 0 || height <= 0 ) {
70
+ throw new Error ( "Invalid canvas size" ) ;
71
+ }
72
+ const canvas = document . createElement ( "canvas" ) ;
73
+ const context = canvas . getContext ( "2d" ) ;
74
+ canvas . width = width ;
75
+ canvas . height = height ;
76
+ return {
77
+ canvas,
78
+ context,
79
+ } ;
80
+ }
81
+ }
82
+
83
+ class BaseCMapReaderFactory {
71
84
constructor ( { baseUrl = null , isCompressed = false } ) {
85
+ if ( this . constructor === BaseCMapReaderFactory ) {
86
+ unreachable ( "Cannot initialize BaseCMapReaderFactory." ) ;
87
+ }
72
88
this . baseUrl = baseUrl ;
73
89
this . isCompressed = isCompressed ;
74
90
}
@@ -88,29 +104,39 @@ class DOMCMapReaderFactory {
88
104
? CMapCompressionType . BINARY
89
105
: CMapCompressionType . NONE ;
90
106
107
+ return this . _fetchData ( url , compressionType ) . catch ( reason => {
108
+ throw new Error (
109
+ `Unable to load ${ this . isCompressed ? "binary " : "" } CMap at: ${ url } `
110
+ ) ;
111
+ } ) ;
112
+ }
113
+
114
+ /**
115
+ * @private
116
+ */
117
+ _fetchData ( url , compressionType ) {
118
+ unreachable ( "Abstract method `_fetchData` called." ) ;
119
+ }
120
+ }
121
+
122
+ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
123
+ _fetchData ( url , compressionType ) {
91
124
if (
92
125
( typeof PDFJSDev !== "undefined" && PDFJSDev . test ( "MOZCENTRAL" ) ) ||
93
126
( isFetchSupported ( ) && isValidFetchUrl ( url , document . baseURI ) )
94
127
) {
95
- return fetch ( url )
96
- . then ( async response => {
97
- if ( ! response . ok ) {
98
- throw new Error ( response . statusText ) ;
99
- }
100
- let cMapData ;
101
- if ( this . isCompressed ) {
102
- cMapData = new Uint8Array ( await response . arrayBuffer ( ) ) ;
103
- } else {
104
- cMapData = stringToBytes ( await response . text ( ) ) ;
105
- }
106
- return { cMapData, compressionType } ;
107
- } )
108
- . catch ( reason => {
109
- throw new Error (
110
- `Unable to load ${ this . isCompressed ? "binary " : "" } ` +
111
- `CMap at: ${ url } `
112
- ) ;
113
- } ) ;
128
+ return fetch ( url ) . then ( async response => {
129
+ if ( ! response . ok ) {
130
+ throw new Error ( response . statusText ) ;
131
+ }
132
+ let cMapData ;
133
+ if ( this . isCompressed ) {
134
+ cMapData = new Uint8Array ( await response . arrayBuffer ( ) ) ;
135
+ } else {
136
+ cMapData = stringToBytes ( await response . text ( ) ) ;
137
+ }
138
+ return { cMapData, compressionType } ;
139
+ } ) ;
114
140
}
115
141
116
142
// The Fetch API is not supported.
@@ -141,11 +167,6 @@ class DOMCMapReaderFactory {
141
167
} ;
142
168
143
169
request . send ( null ) ;
144
- } ) . catch ( reason => {
145
- throw new Error (
146
- `Unable to load ${ this . isCompressed ? "binary " : "" } ` +
147
- `CMap at: ${ url } `
148
- ) ;
149
170
} ) ;
150
171
}
151
172
}
@@ -609,7 +630,9 @@ export {
609
630
getFilenameFromUrl ,
610
631
LinkTarget ,
611
632
DEFAULT_LINK_REL ,
633
+ BaseCanvasFactory ,
612
634
DOMCanvasFactory ,
635
+ BaseCMapReaderFactory ,
613
636
DOMCMapReaderFactory ,
614
637
DOMSVGFactory ,
615
638
StatTimer ,
0 commit comments