21
21
import {
22
22
AnnotationBorderStyleType ,
23
23
AnnotationType ,
24
- assert ,
25
24
FeatureTest ,
26
25
LINE_FACTOR ,
27
26
shadow ,
@@ -181,7 +180,7 @@ class AnnotationElement {
181
180
this . container = this . _createContainer ( ignoreBorder ) ;
182
181
}
183
182
if ( createQuadrilaterals ) {
184
- this . quadrilaterals = this . _createQuadrilaterals ( ignoreBorder ) ;
183
+ this . _createQuadrilaterals ( ) ;
185
184
}
186
185
}
187
186
@@ -416,26 +415,41 @@ class AnnotationElement {
416
415
* @private
417
416
* @param {boolean } ignoreBorder
418
417
* @memberof AnnotationElement
419
- * @returns {Array<HTMLElement> } An array of section elements.
420
418
*/
421
- _createQuadrilaterals ( ignoreBorder = false ) {
422
- if ( ! this . data . quadPoints ) {
423
- return null ;
419
+ _createQuadrilaterals ( ) {
420
+ const { quadPoints } = this . data ;
421
+ if ( ! quadPoints || quadPoints . length === 1 ) {
422
+ return ;
424
423
}
425
424
426
- const quadrilaterals = [ ] ;
427
- const savedRect = this . data . rect ;
428
- for ( const quadPoint of this . data . quadPoints ) {
429
- this . data . rect = [
430
- quadPoint [ 2 ] . x ,
431
- quadPoint [ 2 ] . y ,
432
- quadPoint [ 1 ] . x ,
433
- quadPoint [ 1 ] . y ,
434
- ] ;
435
- quadrilaterals . push ( this . _createContainer ( ignoreBorder ) ) ;
425
+ const { svgFactory } = this ;
426
+ const svg = svgFactory . createElement ( "svg" ) ;
427
+ svg . classList . add ( "quadrilateralsContainer" ) ;
428
+
429
+ svg . setAttribute ( "width" , 0 ) ;
430
+ svg . setAttribute ( "height" , 0 ) ;
431
+ const defs = svgFactory . createElement ( "defs" ) ;
432
+ svg . append ( defs ) ;
433
+ const clipPath = svgFactory . createElement ( "clipPath" ) ;
434
+ const id = `clippath_${ this . data . id } ` ;
435
+ clipPath . setAttribute ( "id" , id ) ;
436
+ clipPath . setAttribute ( "clipPathUnits" , "objectBoundingBox" ) ;
437
+ defs . append ( clipPath ) ;
438
+
439
+ const [ rectBlX , rectBlY , rectTrX , rectTrY ] = this . data . rect ;
440
+ const width = rectTrX - rectBlX ;
441
+ const height = rectTrY - rectBlY ;
442
+ for ( const [ , { x : trX , y : trY } , { x : blX , y : blY } ] of quadPoints ) {
443
+ const rect = svgFactory . createElement ( "rect" ) ;
444
+ rect . setAttribute ( "x" , `${ ( blX - rectBlX ) / width } ` ) ;
445
+ rect . setAttribute ( "y" , `${ ( rectTrY - trY ) / height } ` ) ;
446
+ rect . setAttribute ( "width" , `${ ( trX - blX ) / width } ` ) ;
447
+ rect . setAttribute ( "height" , `${ ( trY - blY ) / height } ` ) ;
448
+ clipPath . append ( rect ) ;
436
449
}
437
- this . data . rect = savedRect ;
438
- return quadrilaterals ;
450
+
451
+ this . container . append ( svg ) ;
452
+ this . container . style . clipPath = `url(#${ id } )` ;
439
453
}
440
454
441
455
/**
@@ -449,18 +463,8 @@ class AnnotationElement {
449
463
* @memberof AnnotationElement
450
464
*/
451
465
_createPopup ( trigger , data ) {
452
- let container = this . container ;
453
- if ( this . quadrilaterals ) {
454
- trigger ||= this . quadrilaterals ;
455
- container = this . quadrilaterals [ 0 ] ;
456
- }
457
-
458
- // If no trigger element is specified, create it.
459
- if ( ! trigger ) {
460
- trigger = document . createElement ( "div" ) ;
461
- trigger . classList . add ( "popupTriggerArea" ) ;
462
- container . append ( trigger ) ;
463
- }
466
+ const container = this . container ;
467
+ trigger ||= this . container ;
464
468
465
469
const popupElement = new PopupElement ( {
466
470
container,
@@ -480,25 +484,6 @@ class AnnotationElement {
480
484
container . append ( popup ) ;
481
485
}
482
486
483
- /**
484
- * Render the quadrilaterals of the annotation.
485
- *
486
- * @private
487
- * @param {string } className
488
- * @memberof AnnotationElement
489
- * @returns {Array<HTMLElement> } An array of section elements.
490
- */
491
- _renderQuadrilaterals ( className ) {
492
- if ( typeof PDFJSDev === "undefined" || PDFJSDev . test ( "TESTING" ) ) {
493
- assert ( this . quadrilaterals , "Missing quadrilaterals during rendering" ) ;
494
- }
495
-
496
- for ( const quadrilateral of this . quadrilaterals ) {
497
- quadrilateral . classList . add ( className ) ;
498
- }
499
- return this . quadrilaterals ;
500
- }
501
-
502
487
/**
503
488
* Render the annotation's HTML element(s).
504
489
*
@@ -613,16 +598,6 @@ class LinkAnnotationElement extends AnnotationElement {
613
598
}
614
599
}
615
600
616
- if ( this . quadrilaterals ) {
617
- return this . _renderQuadrilaterals ( "linkAnnotation" ) . map (
618
- ( quadrilateral , index ) => {
619
- const linkElement = index === 0 ? link : link . cloneNode ( ) ;
620
- quadrilateral . append ( linkElement ) ;
621
- return quadrilateral ;
622
- }
623
- ) ;
624
- }
625
-
626
601
this . container . classList . add ( "linkAnnotation" ) ;
627
602
if ( isBound ) {
628
603
this . container . append ( link ) ;
@@ -2396,10 +2371,6 @@ class HighlightAnnotationElement extends AnnotationElement {
2396
2371
this . _createPopup ( null , this . data ) ;
2397
2372
}
2398
2373
2399
- if ( this . quadrilaterals ) {
2400
- return this . _renderQuadrilaterals ( "highlightAnnotation" ) ;
2401
- }
2402
-
2403
2374
this . container . classList . add ( "highlightAnnotation" ) ;
2404
2375
return this . container ;
2405
2376
}
@@ -2425,10 +2396,6 @@ class UnderlineAnnotationElement extends AnnotationElement {
2425
2396
this . _createPopup ( null , this . data ) ;
2426
2397
}
2427
2398
2428
- if ( this . quadrilaterals ) {
2429
- return this . _renderQuadrilaterals ( "underlineAnnotation" ) ;
2430
- }
2431
-
2432
2399
this . container . classList . add ( "underlineAnnotation" ) ;
2433
2400
return this . container ;
2434
2401
}
@@ -2454,10 +2421,6 @@ class SquigglyAnnotationElement extends AnnotationElement {
2454
2421
this . _createPopup ( null , this . data ) ;
2455
2422
}
2456
2423
2457
- if ( this . quadrilaterals ) {
2458
- return this . _renderQuadrilaterals ( "squigglyAnnotation" ) ;
2459
- }
2460
-
2461
2424
this . container . classList . add ( "squigglyAnnotation" ) ;
2462
2425
return this . container ;
2463
2426
}
@@ -2483,10 +2446,6 @@ class StrikeOutAnnotationElement extends AnnotationElement {
2483
2446
this . _createPopup ( null , this . data ) ;
2484
2447
}
2485
2448
2486
- if ( this . quadrilaterals ) {
2487
- return this . _renderQuadrilaterals ( "strikeoutAnnotation" ) ;
2488
- }
2489
-
2490
2449
this . container . classList . add ( "strikeoutAnnotation" ) ;
2491
2450
return this . container ;
2492
2451
}
0 commit comments