@@ -37,70 +37,63 @@ function notEmpty(obj) {
37
37
'use strict' ;
38
38
39
39
var annotationPlugin = {
40
- onInitialize : function ( pdf ) {
41
- this . installAnnotationPlugin ( pdf ) ;
42
- }
43
- } ;
44
- jsPDF . API . annotationPlugin = annotationPlugin ;
45
- jsPDF . plugins . register ( annotationPlugin ) ;
46
-
47
- annotationPlugin . installAnnotationPlugin = function ( pdf ) {
48
-
49
- this . annotations = [ ] ;
50
40
51
- // TODO remove this after we find a way to subscribe before the
52
- // first page is created.
53
- //this.annotations[1] = [];
41
+ /**
42
+ * An array of arrays, indexed by <em>pageNumber</em>.
43
+ */
44
+ annotations : [ ] ,
54
45
55
- this . f2 = function ( number ) {
46
+ f2 : function ( number ) {
56
47
return number . toFixed ( 2 ) ;
57
- } ;
58
-
59
- pdf . internal . events . subscribe ( 'addPage' , function ( info ) {
60
- this . annotationPlugin . annotations [ info . pageNumber ] = [ ] ;
61
- } ) ;
48
+ }
49
+ } ;
62
50
63
- pdf . internal . events . subscribe ( 'render/page' , function ( info ) {
64
- var pageAnnos = this . annotationPlugin . annotations [ info . pageNumber ] ;
51
+ jsPDF . API . annotationPlugin = annotationPlugin ;
65
52
66
- var found = false ;
67
- for ( var a = 0 ; a < pageAnnos . length ; a ++ ) {
68
- var anno = pageAnnos [ a ] ;
69
- if ( anno . type === 'link' ) {
70
- if ( notEmpty ( anno . options . url ) || notEmpty ( anno . options . pageNumber ) ) {
71
- found = true ;
72
- break ;
53
+ jsPDF . API . events . push ( [
54
+ 'addPage' , function ( info ) {
55
+ this . annotationPlugin . annotations [ info . pageNumber ] = [ ] ;
56
+ }
57
+ ] ) ;
58
+
59
+ jsPDFAPI . events . push ( [
60
+ 'putPage' , function ( info ) {
61
+ var pageAnnos = this . annotationPlugin . annotations [ info . pageNumber ] ;
62
+
63
+ var found = false ;
64
+ for ( var a = 0 ; a < pageAnnos . length ; a ++ ) {
65
+ var anno = pageAnnos [ a ] ;
66
+ if ( anno . type === 'link' ) {
67
+ if ( notEmpty ( anno . options . url ) || notEmpty ( anno . options . pageNumber ) ) {
68
+ found = true ;
69
+ break ;
70
+ }
73
71
}
74
72
}
75
- }
76
- if ( found == false ) {
77
- return ;
78
- }
73
+ if ( found == false ) {
74
+ return ;
75
+ }
79
76
80
- this . internal . write ( "/Annots [" ) ;
81
- var f2 = this . annotationPlugin . f2 ;
82
- for ( var a = 0 ; a < pageAnnos . length ; a ++ ) {
83
- var anno = pageAnnos [ a ] ;
84
- var k = this . internal . scaleFactor ;
85
- var pageHeight = this . internal . pageSize . height ;
86
- var rect = "/Rect [" + f2 ( anno . x * k ) + " " + f2 ( ( pageHeight - anno . y ) * k ) + " " + f2 ( anno . x + anno . w * k ) + " " + f2 ( pageHeight - ( anno . y + anno . h ) * k ) + "] " ;
87
- if ( anno . options . url ) {
88
- this . internal . write ( '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno . options . url + ') >> >>' )
89
- } else if ( anno . options . pageNumber ) {
90
- // first page is 0
91
- this . internal . write ( '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + ( anno . options . pageNumber - 1 ) + ' /XYZ 0 ' + pageHeight + ' 0] >>' )
92
- } else {
93
- // TODO error - should not be here
77
+ this . internal . write ( "/Annots [" ) ;
78
+ var f2 = this . annotationPlugin . f2 ;
79
+ for ( var a = 0 ; a < pageAnnos . length ; a ++ ) {
80
+ var anno = pageAnnos [ a ] ;
81
+ var k = this . internal . scaleFactor ;
82
+ var pageHeight = this . internal . pageSize . height ;
83
+ var rect = "/Rect [" + f2 ( anno . x * k ) + " " + f2 ( ( pageHeight - anno . y ) * k ) + " " + f2 ( anno . x + anno . w * k ) + " " + f2 ( pageHeight - ( anno . y + anno . h ) * k ) + "] " ;
84
+ if ( anno . options . url ) {
85
+ this . internal . write ( '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /A <</S /URI /URI (' + anno . options . url + ') >> >>' )
86
+ } else if ( anno . options . pageNumber ) {
87
+ // first page is 0
88
+ this . internal . write ( '<</Type /Annot /Subtype /Link ' + rect + '/Border [0 0 0] /Dest [' + ( anno . options . pageNumber - 1 ) + ' /XYZ 0 ' + pageHeight + ' 0] >>' )
89
+ } else {
90
+ // TODO error - should not be here
91
+ }
94
92
}
93
+ this . internal . write ( "]" ) ;
95
94
}
96
- this . internal . write ( "]" ) ;
97
- } ) ;
98
- } ;
95
+ ] ) ;
99
96
100
- /**
101
- * An array of arrays, indexed by <em>pageNumber</em>.
102
- */
103
- // this.internal.annotations = [];
104
97
/**
105
98
* valid options
106
99
* <li> pageNumber or url [required]
@@ -132,16 +125,19 @@ function notEmpty(obj) {
132
125
return this ;
133
126
} ;
134
127
128
+ //TODO move into external library
135
129
jsPDFAPI . getTextWidth = function ( text ) {
136
130
'use strict' ;
137
131
var fontSize = this . internal . getFontSize ( ) ;
138
132
var txtWidth = this . getStringUnitWidth ( text ) * fontSize / this . internal . scaleFactor ;
139
133
return txtWidth ;
140
134
} ;
141
135
136
+ //TODO move into external library
142
137
jsPDFAPI . getLineHeight = function ( ) {
143
138
return this . internal . getLineHeight ( ) ;
144
139
} ;
145
140
146
141
return this ;
142
+
147
143
} ) ( jsPDF . API ) ;
0 commit comments