@@ -173,6 +173,7 @@ function renderAnnotation(annotationId) {
173
173
}
174
174
}
175
175
176
+ // `getContentTree` retrieves the page text and accessibility tree for a given page index.
176
177
function getContentTree ( pageIndex ) {
177
178
const result = Module . dispatchCommand (
178
179
JSON . stringify ( {
@@ -184,6 +185,41 @@ function getContentTree(pageIndex) {
184
185
if ( result && result . hasError ( ) ) {
185
186
throw new Error ( "Failed to get content tree: " + result . getErrorMessage ( ) ) ;
186
187
}
188
+
189
+ return getSingleJsonReplyFromResult ( result ) ;
190
+ }
191
+
192
+ function verifyContentTree ( contentTree ) {
193
+ if (
194
+ ! contentTree ||
195
+ ! Array . isArray ( contentTree . nodes ) ||
196
+ contentTree . nodes . length === 0
197
+ ) {
198
+ throw new Error ( "Content tree is empty or not an array" ) ;
199
+ }
200
+ }
201
+
202
+ function getAnnotations ( ) {
203
+ const result = Module . dispatchCommand (
204
+ JSON . stringify ( {
205
+ type : "get_annotations" ,
206
+ skip_attachments : true ,
207
+ } )
208
+ ) ;
209
+
210
+ if ( result && result . hasError ( ) ) {
211
+ throw new Error ( "Failed to get annotations: " + result . getErrorMessage ( ) ) ;
212
+ }
213
+
214
+ const annotations = [ ] ;
215
+ for ( let i = 0 ; i < result . getRepliesCount ( ) ; i ++ ) {
216
+ if ( result . hasJSONReply ( i ) ) {
217
+ annotations . push ( JSON . parse ( result . getJSONReply ( i ) ) ) ;
218
+ } else {
219
+ throw new Error ( `Expected JSON reply for annotation ${ i } ` ) ;
220
+ }
221
+ }
222
+ return annotations ;
187
223
}
188
224
189
225
// Page width and height for the assets/example.pdf document.
@@ -209,12 +245,18 @@ class Benchmark {
209
245
openDocument ( "/document.pdf" ) ;
210
246
211
247
importInstantJSON ( Module . annotations ) ;
248
+ const annotations = getAnnotations ( ) ;
249
+ if ( annotations . length !== 9 ) {
250
+ throw new Error ( `Expected 9 annotations, but got ${ annotations . length } ` ) ;
251
+ }
212
252
213
253
var renderScale = 0.3 ;
214
254
renderPage ( 0 , PAGE_WIDTH * renderScale , PAGE_HEIGHT * renderScale ) ;
215
255
renderPage ( 1 , PAGE_WIDTH * renderScale , PAGE_HEIGHT * renderScale ) ;
216
256
217
- getContentTree ( 0 ) ;
218
- getContentTree ( 1 ) ;
257
+ const contentTree0 = getContentTree ( 0 ) ;
258
+ verifyContentTree ( contentTree0 ) ;
259
+ const contentTree1 = getContentTree ( 1 ) ;
260
+ verifyContentTree ( contentTree1 ) ;
219
261
}
220
262
}
0 commit comments