Skip to content

Commit 981d249

Browse files
committed
removed plugin manager
changed event name to match conventions
1 parent 0675144 commit 981d249

File tree

3 files changed

+186
-207
lines changed

3 files changed

+186
-207
lines changed

jspdf.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ var jsPDF = (function(global) {
265265
out('/MediaBox [0 0 ' + f2(wPt) + ' ' + f2(hPt) + ']');
266266
out('/Contents ' + (objectNumber + 1) + ' 0 R');
267267
// Added for annotation plugin
268-
events.publish('render/page', {pageNumber:n,page:pages[n]});
268+
events.publish('putPage', {pageNumber:n,page:pages[n]});
269269
out('>>');
270270
out('endobj');
271271

@@ -1768,7 +1768,6 @@ var jsPDF = (function(global) {
17681768
// Add the first page automatically
17691769
addFonts();
17701770
activeFontKey = 'F1';
1771-
jsPDF.plugins.internal.onInitialize(API);
17721771
_addPage(format, orientation);
17731772

17741773
events.publish('initialized');
@@ -1803,28 +1802,13 @@ var jsPDF = (function(global) {
18031802
*/
18041803
jsPDF.API = {events:[]};
18051804
jsPDF.version = "1.0.0-trunk";
1805+
18061806
if (typeof define === 'function' && define.amd) {
18071807
define('jsPDF', function() {
18081808
return jsPDF;
18091809
});
18101810
} else {
18111811
global.jsPDF = jsPDF;
18121812
}
1813-
jsPDF.plugins = {
1814-
register:function(plugin){
1815-
this.internal.plugins.push(plugin);
1816-
},
1817-
internal:{
1818-
plugins:[],
1819-
onInitialize: function(pdf){
1820-
for (var i = 0; i < this.plugins.length; i++) {
1821-
var plugin = this.plugins[i];
1822-
if (typeof plugin.onInitialize === 'function'){
1823-
plugin.onInitialize.call(plugin, pdf);
1824-
}
1825-
}
1826-
},
1827-
}
1828-
};
18291813
return jsPDF;
18301814
}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this));

jspdf.plugin.annotations.js

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)