@@ -76,34 +76,6 @@ class InkEditor extends AnnotationEditor {
76
76
this . #boundCanvasMousedown = this . canvasMousedown . bind ( this ) ;
77
77
}
78
78
79
- /** @inheritdoc */
80
- copy ( ) {
81
- const editor = new InkEditor ( {
82
- parent : this . parent ,
83
- id : this . parent . getNextId ( ) ,
84
- } ) ;
85
-
86
- editor . x = this . x ;
87
- editor . y = this . y ;
88
- editor . width = this . width ;
89
- editor . height = this . height ;
90
- editor . color = this . color ;
91
- editor . thickness = this . thickness ;
92
- editor . paths = this . paths . slice ( ) ;
93
- editor . bezierPath2D = this . bezierPath2D . slice ( ) ;
94
- editor . scaleFactor = this . scaleFactor ;
95
- editor . translationX = this . translationX ;
96
- editor . translationY = this . translationY ;
97
- editor . #aspectRatio = this . #aspectRatio;
98
- editor . #baseWidth = this . #baseWidth;
99
- editor . #baseHeight = this . #baseHeight;
100
- editor . #disableEditing = this . #disableEditing;
101
- editor . #realWidth = this . #realWidth;
102
- editor . #realHeight = this . #realHeight;
103
-
104
- return editor ;
105
- }
106
-
107
79
static updateDefaultParams ( type , value ) {
108
80
switch ( type ) {
109
81
case AnnotationEditorParamsType . INK_THICKNESS :
@@ -351,7 +323,7 @@ class InkEditor extends AnnotationEditor {
351
323
const xy = [ x , y ] ;
352
324
bezier = [ [ xy , xy . slice ( ) , xy . slice ( ) , xy ] ] ;
353
325
}
354
- const path2D = this . #buildPath2D( bezier ) ;
326
+ const path2D = InkEditor . #buildPath2D( bezier ) ;
355
327
this . currentPath . length = 0 ;
356
328
357
329
const cmd = ( ) => {
@@ -543,17 +515,18 @@ class InkEditor extends AnnotationEditor {
543
515
544
516
if ( this . width ) {
545
517
// This editor was created in using copy (ctrl+c).
546
- this . #isCanvasInitialized = true ;
547
518
const [ parentWidth , parentHeight ] = this . parent . viewportBaseDimensions ;
548
519
this . setAt (
549
520
baseX * parentWidth ,
550
521
baseY * parentHeight ,
551
522
this . width * parentWidth ,
552
523
this . height * parentHeight
553
524
) ;
554
- this . setDims ( this . width * parentWidth , this . height * parentHeight ) ;
525
+ this . #isCanvasInitialized = true ;
555
526
this . #setCanvasDims( ) ;
527
+ this . setDims ( this . width * parentWidth , this . height * parentHeight ) ;
556
528
this . #redraw( ) ;
529
+ this . #setMinDims( ) ;
557
530
this . div . classList . add ( "disabled" ) ;
558
531
} else {
559
532
this . div . classList . add ( "editing" ) ;
@@ -570,8 +543,8 @@ class InkEditor extends AnnotationEditor {
570
543
return ;
571
544
}
572
545
const [ parentWidth , parentHeight ] = this . parent . viewportBaseDimensions ;
573
- this . canvas . width = this . width * parentWidth ;
574
- this . canvas . height = this . height * parentHeight ;
546
+ this . canvas . width = Math . ceil ( this . width * parentWidth ) ;
547
+ this . canvas . height = Math . ceil ( this . height * parentHeight ) ;
575
548
this . #updateTransform( ) ;
576
549
}
577
550
@@ -610,10 +583,7 @@ class InkEditor extends AnnotationEditor {
610
583
this . height = height / parentHeight ;
611
584
612
585
if ( this . #disableEditing) {
613
- const padding = this . #getPadding( ) ;
614
- const scaleFactorW = ( width - padding ) / this . #baseWidth;
615
- const scaleFactorH = ( height - padding ) / this . #baseHeight;
616
- this . scaleFactor = Math . min ( scaleFactorW , scaleFactorH ) ;
586
+ this . #setScaleFactor( width , height ) ;
617
587
}
618
588
619
589
this . #setCanvasDims( ) ;
@@ -622,6 +592,13 @@ class InkEditor extends AnnotationEditor {
622
592
this . canvas . style . visibility = "visible" ;
623
593
}
624
594
595
+ #setScaleFactor( width , height ) {
596
+ const padding = this . #getPadding( ) ;
597
+ const scaleFactorW = ( width - padding ) / this . #baseWidth;
598
+ const scaleFactorH = ( height - padding ) / this . #baseHeight;
599
+ this . scaleFactor = Math . min ( scaleFactorW , scaleFactorH ) ;
600
+ }
601
+
625
602
/**
626
603
* Update the canvas transform.
627
604
*/
@@ -642,7 +619,7 @@ class InkEditor extends AnnotationEditor {
642
619
* @param {Arra<Array<number> } bezier
643
620
* @returns {Path2D }
644
621
*/
645
- #buildPath2D( bezier ) {
622
+ static #buildPath2D( bezier ) {
646
623
const path2D = new Path2D ( ) ;
647
624
for ( let i = 0 , ii = bezier . length ; i < ii ; i ++ ) {
648
625
const [ first , control1 , control2 , second ] = bezier [ i ] ;
@@ -859,14 +836,7 @@ class InkEditor extends AnnotationEditor {
859
836
this . height = height / parentHeight ;
860
837
861
838
this . #aspectRatio = width / height ;
862
- const { style } = this . div ;
863
- if ( this . #aspectRatio >= 1 ) {
864
- style . minHeight = `${ RESIZER_SIZE } px` ;
865
- style . minWidth = `${ Math . round ( this . #aspectRatio * RESIZER_SIZE ) } px` ;
866
- } else {
867
- style . minWidth = `${ RESIZER_SIZE } px` ;
868
- style . minHeight = `${ Math . round ( RESIZER_SIZE / this . #aspectRatio) } px` ;
869
- }
839
+ this . #setMinDims( ) ;
870
840
871
841
const prevTranslationX = this . translationX ;
872
842
const prevTranslationY = this . translationY ;
@@ -886,6 +856,68 @@ class InkEditor extends AnnotationEditor {
886
856
) ;
887
857
}
888
858
859
+ #setMinDims( ) {
860
+ const { style } = this . div ;
861
+ if ( this . #aspectRatio >= 1 ) {
862
+ style . minHeight = `${ RESIZER_SIZE } px` ;
863
+ style . minWidth = `${ Math . round ( this . #aspectRatio * RESIZER_SIZE ) } px` ;
864
+ } else {
865
+ style . minWidth = `${ RESIZER_SIZE } px` ;
866
+ style . minHeight = `${ Math . round ( RESIZER_SIZE / this . #aspectRatio) } px` ;
867
+ }
868
+ }
869
+
870
+ /** @inheritdoc */
871
+ static deserialize ( data , parent ) {
872
+ const editor = super . deserialize ( data , parent ) ;
873
+
874
+ editor . thickness = data . thickness ;
875
+ editor . color = Util . makeHexColor ( ...data . color ) ;
876
+
877
+ const [ pageWidth , pageHeight ] = parent . pageDimensions ;
878
+ const width = editor . width * pageWidth ;
879
+ const height = editor . height * pageHeight ;
880
+ const scaleFactor = parent . scaleFactor ;
881
+ const padding = data . thickness / 2 ;
882
+
883
+ editor . #aspectRatio = width / height ;
884
+ editor . #disableEditing = true ;
885
+ editor . #realWidth = Math . round ( width ) ;
886
+ editor . #realHeight = Math . round ( height ) ;
887
+
888
+ for ( const { bezier } of data . paths ) {
889
+ const path = [ ] ;
890
+ editor . paths . push ( path ) ;
891
+ let p0 = scaleFactor * ( bezier [ 0 ] - padding ) ;
892
+ let p1 = scaleFactor * ( height - bezier [ 1 ] - padding ) ;
893
+ for ( let i = 2 , ii = bezier . length ; i < ii ; i += 6 ) {
894
+ const p10 = scaleFactor * ( bezier [ i ] - padding ) ;
895
+ const p11 = scaleFactor * ( height - bezier [ i + 1 ] - padding ) ;
896
+ const p20 = scaleFactor * ( bezier [ i + 2 ] - padding ) ;
897
+ const p21 = scaleFactor * ( height - bezier [ i + 3 ] - padding ) ;
898
+ const p30 = scaleFactor * ( bezier [ i + 4 ] - padding ) ;
899
+ const p31 = scaleFactor * ( height - bezier [ i + 5 ] - padding ) ;
900
+ path . push ( [
901
+ [ p0 , p1 ] ,
902
+ [ p10 , p11 ] ,
903
+ [ p20 , p21 ] ,
904
+ [ p30 , p31 ] ,
905
+ ] ) ;
906
+ p0 = p30 ;
907
+ p1 = p31 ;
908
+ }
909
+ const path2D = this . #buildPath2D( path ) ;
910
+ editor . bezierPath2D . push ( path2D ) ;
911
+ }
912
+
913
+ const bbox = editor . #getBbox( ) ;
914
+ editor . #baseWidth = bbox [ 2 ] - bbox [ 0 ] ;
915
+ editor . #baseHeight = bbox [ 3 ] - bbox [ 1 ] ;
916
+ editor . #setScaleFactor( width , height ) ;
917
+
918
+ return editor ;
919
+ }
920
+
889
921
/** @inheritdoc */
890
922
serialize ( ) {
891
923
if ( this . isEmpty ( ) ) {
0 commit comments