@@ -37,70 +37,63 @@ function notEmpty(obj) {
3737 'use strict' ;
3838
3939 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 = [ ] ;
5040
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 : [ ] ,
5445
55- this . f2 = function ( number ) {
46+ f2 : function ( number ) {
5647 return number . toFixed ( 2 ) ;
57- } ;
58-
59- pdf . internal . events . subscribe ( 'addPage' , function ( info ) {
60- this . annotationPlugin . annotations [ info . pageNumber ] = [ ] ;
61- } ) ;
48+ }
49+ } ;
6250
63- pdf . internal . events . subscribe ( 'render/page' , function ( info ) {
64- var pageAnnos = this . annotationPlugin . annotations [ info . pageNumber ] ;
51+ jsPDF . API . annotationPlugin = annotationPlugin ;
6552
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+ }
7371 }
7472 }
75- }
76- if ( found == false ) {
77- return ;
78- }
73+ if ( found == false ) {
74+ return ;
75+ }
7976
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+ }
9492 }
93+ this . internal . write ( "]" ) ;
9594 }
96- this . internal . write ( "]" ) ;
97- } ) ;
98- } ;
95+ ] ) ;
9996
100- /**
101- * An array of arrays, indexed by <em>pageNumber</em>.
102- */
103- // this.internal.annotations = [];
10497 /**
10598 * valid options
10699 * <li> pageNumber or url [required]
@@ -132,16 +125,19 @@ function notEmpty(obj) {
132125 return this ;
133126 } ;
134127
128+ //TODO move into external library
135129 jsPDFAPI . getTextWidth = function ( text ) {
136130 'use strict' ;
137131 var fontSize = this . internal . getFontSize ( ) ;
138132 var txtWidth = this . getStringUnitWidth ( text ) * fontSize / this . internal . scaleFactor ;
139133 return txtWidth ;
140134 } ;
141135
136+ //TODO move into external library
142137 jsPDFAPI . getLineHeight = function ( ) {
143138 return this . internal . getLineHeight ( ) ;
144139 } ;
145140
146141 return this ;
142+
147143} ) ( jsPDF . API ) ;
0 commit comments