@@ -59,20 +59,22 @@ function loadStyles(styles) {
59
59
return Promise . all ( styles . map ( style => style . promise ) ) ;
60
60
}
61
61
62
- function writeSVG ( svgElement , ctx , resolve , reject ) {
62
+ function writeSVG ( svgElement , ctx ) {
63
63
// We need to have UTF-8 encoded XML.
64
64
const svg_xml = unescape (
65
65
encodeURIComponent ( new XMLSerializer ( ) . serializeToString ( svgElement ) )
66
66
) ;
67
- const img = new Image ( ) ;
68
- img . src = "data:image/svg+xml;base64," + btoa ( svg_xml ) ;
69
- img . onload = function ( ) {
70
- ctx . drawImage ( img , 0 , 0 ) ;
71
- resolve ( ) ;
72
- } ;
73
- img . onerror = function ( e ) {
74
- reject ( new Error ( "Error rasterizing text layer " + e ) ) ;
75
- } ;
67
+ return new Promise ( ( resolve , reject ) => {
68
+ const img = new Image ( ) ;
69
+ img . src = "data:image/svg+xml;base64," + btoa ( svg_xml ) ;
70
+ img . onload = function ( ) {
71
+ ctx . drawImage ( img , 0 , 0 ) ;
72
+ resolve ( ) ;
73
+ } ;
74
+ img . onerror = function ( e ) {
75
+ reject ( new Error ( `Error rasterizing SVG: ${ e } ` ) ) ;
76
+ } ;
77
+ } ) ;
76
78
}
77
79
78
80
function inlineImages ( images ) {
@@ -142,15 +144,20 @@ async function resolveImages(node, silentErrors = false) {
142
144
await Promise . all ( loadedPromises ) ;
143
145
}
144
146
145
- function rasterizeTextLayer ( ctx , viewport , textContent , enhanceTextSelection ) {
147
+ async function rasterizeTextLayer (
148
+ ctx ,
149
+ viewport ,
150
+ textContent ,
151
+ enhanceTextSelection
152
+ ) {
146
153
const styles = {
147
154
common : {
148
155
file : "./text_layer_test.css" ,
149
156
promise : null ,
150
157
} ,
151
158
} ;
152
159
153
- return new Promise ( function ( resolve , reject ) {
160
+ try {
154
161
// Building SVG with size of the viewport.
155
162
const svg = document . createElementNS ( SVG_NS , "svg:svg" ) ;
156
163
svg . setAttribute ( "width" , viewport . width + "px" ) ;
@@ -171,28 +178,25 @@ function rasterizeTextLayer(ctx, viewport, textContent, enhanceTextSelection) {
171
178
div . className = "textLayer" ;
172
179
foreignObject . appendChild ( div ) ;
173
180
174
- stylePromise
175
- . then ( async ( [ cssRules ] ) => {
176
- style . textContent = cssRules ;
181
+ const [ cssRules ] = await stylePromise ;
182
+ style . textContent = cssRules ;
177
183
178
- // Rendering text layer as HTML.
179
- const task = renderTextLayer ( {
180
- textContent,
181
- container : div ,
182
- viewport,
183
- enhanceTextSelection,
184
- } ) ;
185
- await task . promise ;
184
+ // Rendering text layer as HTML.
185
+ const task = renderTextLayer ( {
186
+ textContent,
187
+ container : div ,
188
+ viewport,
189
+ enhanceTextSelection,
190
+ } ) ;
191
+ await task . promise ;
186
192
187
- task . expandTextDivs ( true ) ;
188
- svg . appendChild ( foreignObject ) ;
193
+ task . expandTextDivs ( true ) ;
194
+ svg . appendChild ( foreignObject ) ;
189
195
190
- writeSVG ( svg , ctx , resolve , reject ) ;
191
- } )
192
- . catch ( reason => {
193
- reject ( new Error ( `rasterizeTextLayer: "${ reason ?. message } ".` ) ) ;
194
- } ) ;
195
- } ) ;
196
+ await writeSVG ( svg , ctx ) ;
197
+ } catch ( reason ) {
198
+ throw new Error ( `rasterizeTextLayer: "${ reason ?. message } ".` ) ;
199
+ }
196
200
}
197
201
198
202
/**
@@ -204,7 +208,7 @@ function rasterizeTextLayer(ctx, viewport, textContent, enhanceTextSelection) {
204
208
* the overrides file because the browser does not resolve that when the
205
209
* styles are inserted via XHR. Therefore, we load and combine them here.
206
210
*/
207
- function rasterizeAnnotationLayer (
211
+ async function rasterizeAnnotationLayer (
208
212
ctx ,
209
213
viewport ,
210
214
annotations ,
@@ -224,7 +228,7 @@ function rasterizeAnnotationLayer(
224
228
} ,
225
229
} ;
226
230
227
- return new Promise ( function ( resolve , reject ) {
231
+ try {
228
232
// Building SVG with size of the viewport.
229
233
const svg = document . createElementNS ( SVG_NS , "svg:svg" ) ;
230
234
svg . setAttribute ( "width" , viewport . width + "px" ) ;
@@ -243,41 +247,38 @@ function rasterizeAnnotationLayer(
243
247
div . className = "annotationLayer" ;
244
248
245
249
// Rendering annotation layer as HTML.
246
- stylePromise
247
- . then ( async ( [ common , overrides ] ) => {
248
- style . textContent = common + "\n" + overrides ;
250
+ const [ common , overrides ] = await stylePromise ;
251
+ style . textContent = common + "\n" + overrides ;
249
252
250
- const annotation_viewport = viewport . clone ( { dontFlip : true } ) ;
251
- const annotationImageMap = await convertCanvasesToImages (
252
- annotationCanvasMap
253
- ) ;
253
+ const annotation_viewport = viewport . clone ( { dontFlip : true } ) ;
254
+ const annotationImageMap = await convertCanvasesToImages (
255
+ annotationCanvasMap
256
+ ) ;
254
257
255
- const parameters = {
256
- viewport : annotation_viewport ,
257
- div,
258
- annotations,
259
- page,
260
- linkService : new SimpleLinkService ( ) ,
261
- imageResourcesPath,
262
- renderForms,
263
- annotationCanvasMap : annotationImageMap ,
264
- } ;
265
- AnnotationLayer . render ( parameters ) ;
258
+ const parameters = {
259
+ viewport : annotation_viewport ,
260
+ div,
261
+ annotations,
262
+ page,
263
+ linkService : new SimpleLinkService ( ) ,
264
+ imageResourcesPath,
265
+ renderForms,
266
+ annotationCanvasMap : annotationImageMap ,
267
+ } ;
268
+ AnnotationLayer . render ( parameters ) ;
266
269
267
- // Inline SVG images from text annotations.
268
- await resolveImages ( div ) ;
269
- foreignObject . appendChild ( div ) ;
270
- svg . appendChild ( foreignObject ) ;
270
+ // Inline SVG images from text annotations.
271
+ await resolveImages ( div ) ;
272
+ foreignObject . appendChild ( div ) ;
273
+ svg . appendChild ( foreignObject ) ;
271
274
272
- writeSVG ( svg , ctx , resolve , reject ) ;
273
- } )
274
- . catch ( reason => {
275
- reject ( new Error ( `rasterizeAnnotationLayer: "${ reason ?. message } ".` ) ) ;
276
- } ) ;
277
- } ) ;
275
+ await writeSVG ( svg , ctx ) ;
276
+ } catch ( reason ) {
277
+ throw new Error ( `rasterizeAnnotationLayer: "${ reason ?. message } ".` ) ;
278
+ }
278
279
}
279
280
280
- function rasterizeXfaLayer (
281
+ async function rasterizeXfaLayer (
281
282
ctx ,
282
283
viewport ,
283
284
xfa ,
@@ -296,7 +297,7 @@ function rasterizeXfaLayer(
296
297
} ,
297
298
} ;
298
299
299
- return new Promise ( function ( resolve , reject ) {
300
+ try {
300
301
// Building SVG with size of the viewport.
301
302
const svg = document . createElementNS ( SVG_NS , "svg:svg" ) ;
302
303
svg . setAttribute ( "width" , viewport . width + "px" ) ;
@@ -312,30 +313,27 @@ function rasterizeXfaLayer(
312
313
const div = document . createElement ( "div" ) ;
313
314
foreignObject . appendChild ( div ) ;
314
315
315
- stylePromise
316
- . then ( async ( [ common , overrides ] ) => {
317
- style . textContent = fontRules + "\n" + common + "\n" + overrides ;
318
-
319
- XfaLayer . render ( {
320
- xfa,
321
- div,
322
- viewport : viewport . clone ( { dontFlip : true } ) ,
323
- annotationStorage,
324
- linkService : new SimpleLinkService ( ) ,
325
- intent : isPrint ? "print" : "display" ,
326
- } ) ;
316
+ const [ common , overrides ] = await stylePromise ;
317
+ style . textContent = fontRules + "\n" + common + "\n" + overrides ;
327
318
328
- // Some unsupported type of images (e.g. tiff)
329
- // lead to errors.
330
- await resolveImages ( div , /* silentErrors = */ true ) ;
331
- svg . appendChild ( foreignObject ) ;
319
+ XfaLayer . render ( {
320
+ xfa,
321
+ div,
322
+ viewport : viewport . clone ( { dontFlip : true } ) ,
323
+ annotationStorage,
324
+ linkService : new SimpleLinkService ( ) ,
325
+ intent : isPrint ? "print" : "display" ,
326
+ } ) ;
332
327
333
- writeSVG ( svg , ctx , resolve , reject ) ;
334
- } )
335
- . catch ( reason => {
336
- reject ( new Error ( `rasterizeXfaLayer: "${ reason ?. message } ".` ) ) ;
337
- } ) ;
338
- } ) ;
328
+ // Some unsupported type of images (e.g. tiff)
329
+ // lead to errors.
330
+ await resolveImages ( div , /* silentErrors = */ true ) ;
331
+ svg . appendChild ( foreignObject ) ;
332
+
333
+ await writeSVG ( svg , ctx ) ;
334
+ } catch ( reason ) {
335
+ throw new Error ( `rasterizeXfaLayer: "${ reason ?. message } ".` ) ;
336
+ }
339
337
}
340
338
341
339
/**
0 commit comments