16
16
import { AnnotationEditorType , shadow } from "../../shared/util.js" ;
17
17
import { DrawingEditor , DrawingOptions } from "./draw.js" ;
18
18
import { AnnotationEditor } from "./editor.js" ;
19
+ import { ContourDrawOutline } from "./drawers/contour.js" ;
20
+ import { InkDrawingOptions } from "./ink.js" ;
19
21
import { SignatureExtractor } from "./drawers/signaturedraw.js" ;
20
- import { SupportedImageMimeTypes } from "../display_utils.js" ;
21
22
22
23
class SignatureOptions extends DrawingOptions {
23
- #viewParameters;
24
-
25
- constructor ( viewerParameters ) {
24
+ constructor ( ) {
26
25
super ( ) ;
27
- this . #viewParameters = viewerParameters ;
28
26
29
27
super . updateProperties ( {
30
28
fill : "black" ,
@@ -33,7 +31,24 @@ class SignatureOptions extends DrawingOptions {
33
31
}
34
32
35
33
clone ( ) {
36
- const clone = new SignatureOptions ( this . #viewParameters) ;
34
+ const clone = new SignatureOptions ( ) ;
35
+ clone . updateAll ( this ) ;
36
+ return clone ;
37
+ }
38
+ }
39
+
40
+ class DrawnSignatureOptions extends InkDrawingOptions {
41
+ constructor ( viewerParameters ) {
42
+ super ( viewerParameters ) ;
43
+
44
+ super . updateProperties ( {
45
+ stroke : "black" ,
46
+ "stroke-width" : 1 ,
47
+ } ) ;
48
+ }
49
+
50
+ clone ( ) {
51
+ const clone = new DrawnSignatureOptions ( this . _viewParameters ) ;
37
52
clone . updateAll ( this ) ;
38
53
return clone ;
39
54
}
@@ -44,6 +59,8 @@ class SignatureOptions extends DrawingOptions {
44
59
* a signature drawing.
45
60
*/
46
61
class SignatureEditor extends DrawingEditor {
62
+ #isExtracted = false ;
63
+
47
64
static _type = "signature" ;
48
65
49
66
static _editorType = AnnotationEditorType . SIGNATURE ;
@@ -52,13 +69,15 @@ class SignatureEditor extends DrawingEditor {
52
69
53
70
constructor ( params ) {
54
71
super ( { ...params , mustBeCommitted : true , name : "signatureEditor" } ) ;
55
- this . _willKeepAspectRatio = false ;
72
+ this . _willKeepAspectRatio = true ;
56
73
}
57
74
58
75
/** @inheritdoc */
59
76
static initialize ( l10n , uiManager ) {
60
77
AnnotationEditor . initialize ( l10n , uiManager ) ;
61
- this . _defaultDrawingOptions = new SignatureOptions (
78
+
79
+ this . _defaultDrawingOptions = new SignatureOptions ( ) ;
80
+ this . _defaultDrawnSignatureOptions = new DrawnSignatureOptions (
62
81
uiManager . viewParameters
63
82
) ;
64
83
}
@@ -88,6 +107,14 @@ class SignatureEditor extends DrawingEditor {
88
107
return true ;
89
108
}
90
109
110
+ /** @inheritdoc */
111
+ onScaleChanging ( ) {
112
+ if ( this . _drawId === null ) {
113
+ return ;
114
+ }
115
+ super . onScaleChanging ( ) ;
116
+ }
117
+
91
118
/** @inheritdoc */
92
119
render ( ) {
93
120
if ( this . div ) {
@@ -98,70 +125,91 @@ class SignatureEditor extends DrawingEditor {
98
125
this . div . hidden = true ;
99
126
this . div . setAttribute ( "role" , "figure" ) ;
100
127
101
- this . #extractSignature ( ) ;
128
+ this . _uiManager . getSignature ( this ) ;
102
129
103
130
return this . div ;
104
131
}
105
132
106
- async #extractSignature( ) {
107
- const input = document . createElement ( "input" ) ;
108
- input . type = "file" ;
109
- input . accept = SupportedImageMimeTypes . join ( "," ) ;
110
- const signal = this . _uiManager . _signal ;
111
- const { promise, resolve } = Promise . withResolvers ( ) ;
112
-
113
- input . addEventListener (
114
- "change" ,
115
- async ( ) => {
116
- if ( ! input . files || input . files . length === 0 ) {
117
- resolve ( ) ;
118
- } else {
119
- this . _uiManager . enableWaiting ( true ) ;
120
- const data = await this . _uiManager . imageManager . getFromFile (
121
- input . files [ 0 ]
122
- ) ;
123
- this . _uiManager . enableWaiting ( false ) ;
124
- resolve ( data ) ;
125
- }
126
- resolve ( ) ;
127
- } ,
128
- { signal }
129
- ) ;
130
- input . addEventListener ( "cancel" , resolve , { signal } ) ;
131
- input . click ( ) ;
132
-
133
- const bitmap = await promise ;
134
- const {
135
- rawDims : { pageWidth, pageHeight } ,
136
- rotation,
137
- } = this . parent . viewport ;
138
- let drawOutlines ;
139
- if ( bitmap ?. bitmap ) {
140
- drawOutlines = SignatureExtractor . process (
141
- bitmap . bitmap ,
142
- pageWidth ,
143
- pageHeight ,
144
- rotation ,
145
- SignatureEditor . _INNER_MARGIN
146
- ) ;
133
+ addSignature ( outline , heightInPage ) {
134
+ const { x : savedX , y : savedY } = this ;
135
+ this . #isExtracted = outline instanceof ContourDrawOutline ;
136
+ let drawingOptions ;
137
+ if ( this . #isExtracted) {
138
+ drawingOptions = SignatureEditor . getDefaultDrawingOptions ( ) ;
147
139
} else {
148
- drawOutlines = SignatureExtractor . extractContoursFromText (
149
- "Hello PDF.js' World !!" ,
150
- { fontStyle : "italic" , fontWeight : "400" , fontFamily : "cursive" } ,
151
- pageWidth ,
152
- pageHeight ,
153
- rotation ,
154
- SignatureEditor . _INNER_MARGIN
155
- ) ;
140
+ drawingOptions = SignatureEditor . _defaultDrawnSignatureOptions . clone ( ) ;
141
+ drawingOptions . updateProperties ( { "stroke-width" : outline . thickness } ) ;
156
142
}
157
143
this . _addOutlines ( {
158
- drawOutlines,
159
- drawingOptions : SignatureEditor . getDefaultDrawingOptions ( ) ,
144
+ drawOutlines : outline ,
145
+ drawingOptions,
160
146
} ) ;
147
+ const [ parentWidth , parentHeight ] = this . parentDimensions ;
148
+ const [ , pageHeight ] = this . pageDimensions ;
149
+ let newHeight = heightInPage / pageHeight ;
150
+ // Ensure the signature doesn't exceed the page height.
151
+ // If the signature is too big, we scale it down to 50% of the page height.
152
+ newHeight = newHeight >= 1 ? 0.5 : newHeight ;
153
+
154
+ this . width *= newHeight / this . height ;
155
+ this . height = newHeight ;
156
+ this . setDims ( parentWidth * this . width , parentHeight * this . height ) ;
157
+ this . x = savedX ;
158
+ this . y = savedY ;
159
+ this . center ( ) ;
160
+
161
+ this . _onResized ( ) ;
161
162
this . onScaleChanging ( ) ;
162
163
this . rotate ( ) ;
164
+ this . _uiManager . addToAnnotationStorage ( this ) ;
165
+
163
166
this . div . hidden = false ;
164
167
}
168
+
169
+ getFromImage ( bitmap ) {
170
+ const {
171
+ rawDims : { pageWidth, pageHeight } ,
172
+ rotation,
173
+ } = this . parent . viewport ;
174
+ return SignatureExtractor . process (
175
+ bitmap ,
176
+ pageWidth ,
177
+ pageHeight ,
178
+ rotation ,
179
+ SignatureEditor . _INNER_MARGIN
180
+ ) ;
181
+ }
182
+
183
+ getFromText ( text , fontInfo ) {
184
+ const {
185
+ rawDims : { pageWidth, pageHeight } ,
186
+ rotation,
187
+ } = this . parent . viewport ;
188
+ return SignatureExtractor . extractContoursFromText (
189
+ text ,
190
+ fontInfo ,
191
+ pageWidth ,
192
+ pageHeight ,
193
+ rotation ,
194
+ SignatureEditor . _INNER_MARGIN
195
+ ) ;
196
+ }
197
+
198
+ getDrawnSignature ( curves ) {
199
+ const {
200
+ rawDims : { pageWidth, pageHeight } ,
201
+ rotation,
202
+ } = this . parent . viewport ;
203
+ return SignatureExtractor . processDrawnLines ( {
204
+ lines : curves ,
205
+ pageWidth,
206
+ pageHeight,
207
+ rotation,
208
+ innerMargin : SignatureEditor . _INNER_MARGIN ,
209
+ mustSmooth : false ,
210
+ areContours : false ,
211
+ } ) ;
212
+ }
165
213
}
166
214
167
215
export { SignatureEditor } ;
0 commit comments