Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / policyApp / libs / jspdf / jspdf.js
1 /**
2  * jsPDF
3  * (c) 2009 James Hall
4  * 
5  * Some parts based on FPDF.
6  */
7
8 var jsPDF = function(){
9         
10         // Private properties
11         var version = '20090504';
12         var buffer = '';
13         
14         var pdfVersion = '1.3'; // PDF Version
15         var defaultPageFormat = 'a4';
16         var pageFormats = { // Size in mm of various paper formats
17                 'a3': [841.89, 1190.55],
18                 'a4': [595.28, 841.89],
19                 'a5': [420.94, 595.28],
20                 'letter': [612, 792],
21                 'legal': [612, 1008]
22         };
23         var textColor = '0 g';
24         var page = 0;
25         var objectNumber = 2; // 'n' Current object number
26         var state = 0; // Current document state
27         var pages = new Array();
28         var offsets = new Array(); // List of offsets
29         var lineWidth = 0.200025; // 2mm
30         var pageHeight;
31         var k; // Scale factor
32         var unit = 'mm'; // Default to mm for units
33         var fontNumber; // TODO: This is temp, replace with real font handling
34         var documentProperties = {};
35         var fontSize = 16; // Default font size
36         var pageFontSize = 16;
37
38         // Initilisation 
39         if (unit == 'pt') {
40                 k = 1;
41         } else if(unit == 'mm') {
42                 k = 72/25.4;
43         } else if(unit == 'cm') {
44                 k = 72/2.54;
45         } else if(unit == 'in') {
46                 k = 72;
47         }
48         
49         // Private functions
50         var newObject = function() {
51                 //Begin a new object
52                 objectNumber ++;
53                 offsets[objectNumber] = buffer.length;
54                 out(objectNumber + ' 0 obj');           
55         }
56         
57         
58         var putHeader = function() {
59                 out('%PDF-' + pdfVersion);
60         }
61         
62         var putPages = function() {
63                 
64                 // TODO: Fix, hardcoded to a4 portrait
65                 var wPt = pageWidth * k;
66                 var hPt = pageHeight * k;
67
68                 for(n=1; n <= page; n++) {
69                         newObject();
70                         out('<</Type /Page');
71                         out('/Parent 1 0 R');   
72                         out('/Resources 2 0 R');
73                         out('/Contents ' + (objectNumber + 1) + ' 0 R>>');
74                         out('endobj');
75                         
76                         //Page content
77                         p = pages[n];
78                         newObject();
79                         out('<</Length ' + p.length  + '>>');
80                         putStream(p);
81                         out('endobj');                                  
82                 }
83                 offsets[1] = buffer.length;
84                 out('1 0 obj');
85                 out('<</Type /Pages');
86                 var kids='/Kids [';
87                 for (i = 0; i < page; i++) {
88                         kids += (3 + 2 * i) + ' 0 R ';
89                 }
90                 out(kids + ']');
91                 out('/Count ' + page);
92                 out(sprintf('/MediaBox [0 0 %.2f %.2f]', wPt, hPt));
93                 out('>>');
94                 out('endobj');          
95         }
96         
97         var putStream = function(str) {
98                 out('stream');
99                 out(str);
100                 out('endstream');
101         }
102         
103         var putResources = function() {
104                 putFonts();
105                 putImages();
106                 
107                 //Resource dictionary
108                 offsets[2] = buffer.length;
109                 out('2 0 obj');
110                 out('<<');
111                 putResourceDictionary();
112                 out('>>');
113                 out('endobj');
114         }       
115         
116         var putFonts = function() {
117                 // TODO: Only supports core font hardcoded to Helvetica
118                 newObject();
119                 fontNumber = objectNumber;
120                 name = 'Helvetica';
121                 out('<</Type /Font');
122                 out('/BaseFont /' + name);
123                 out('/Subtype /Type1');
124                 out('/Encoding /WinAnsiEncoding');
125                 out('>>');
126                 out('endobj');
127         }
128         
129         var putImages = function() {
130                 // TODO
131         }
132         
133         var putResourceDictionary = function() {
134                 out('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
135                 out('/Font <<');
136                 // Do this for each font, the '1' bit is the index of the font
137         // fontNumber is currently the object number related to 'putFonts'
138                 out('/F1 ' + fontNumber + ' 0 R');
139                 out('>>');
140                 out('/XObject <<');
141                 putXobjectDict();
142                 out('>>');
143         }
144         
145         var putXobjectDict = function() {
146                 // TODO
147                 // Loop through images
148         }
149         
150         
151         var putInfo = function() {
152                 out('/Producer (jsPDF ' + version + ')');
153                 if(documentProperties.title != undefined) {
154                         out('/Title (' + pdfEscape(documentProperties.title) + ')');
155                 }
156                 if(documentProperties.subject != undefined) {
157                         out('/Subject (' + pdfEscape(documentProperties.subject) + ')');
158                 }
159                 if(documentProperties.author != undefined) {
160                         out('/Author (' + pdfEscape(documentProperties.author) + ')');
161                 }
162                 if(documentProperties.keywords != undefined) {
163                         out('/Keywords (' + pdfEscape(documentProperties.keywords) + ')');
164                 }
165                 if(documentProperties.creator != undefined) {
166                         out('/Creator (' + pdfEscape(documentProperties.creator) + ')');
167                 }               
168                 var created = new Date();
169                 var year = created.getFullYear();
170                 var month = (created.getMonth() + 1);
171                 var day = created.getDate();
172                 var hour = created.getHours();
173                 var minute = created.getMinutes();
174                 var second = created.getSeconds();
175                 out('/CreationDate (D:' + sprintf('%02d%02d%02d%02d%02d%02d', year, month, day, hour, minute, second) + ')');
176         }
177         
178         var putCatalog = function () {
179                 out('/Type /Catalog');
180                 out('/Pages 1 0 R');
181                 // TODO: Add zoom and layout modes
182                 out('/OpenAction [3 0 R /FitH null]');
183                 out('/PageLayout /OneColumn');
184         }       
185         
186         function putTrailer() {
187                 out('/Size ' + (objectNumber + 1));
188                 out('/Root ' + objectNumber + ' 0 R');
189                 out('/Info ' + (objectNumber - 1) + ' 0 R');
190         }       
191         
192         var endDocument = function() {
193                 state = 1;
194                 putHeader();
195                 putPages();
196                 
197                 putResources();
198                 //Info
199                 newObject();
200                 out('<<');
201                 putInfo();
202                 out('>>');
203                 out('endobj');
204                 
205                 //Catalog
206                 newObject();
207                 out('<<');
208                 putCatalog();
209                 out('>>');
210                 out('endobj');
211                 
212                 //Cross-ref
213                 var o = buffer.length;
214                 out('xref');
215                 out('0 ' + (objectNumber + 1));
216                 out('0000000000 65535 f ');
217                 for (var i=1; i <= objectNumber; i++) {
218                         out(sprintf('%010d 00000 n ', offsets[i]));
219                 }
220                 //Trailer
221                 out('trailer');
222                 out('<<');
223                 putTrailer();
224                 out('>>');
225                 out('startxref');
226                 out(o);
227                 out('%%EOF');
228                 state = 3;              
229         }
230         
231         var beginPage = function() {
232                 page ++;
233                 // Do dimension stuff
234                 state = 2;
235                 pages[page] = '';
236                 
237                 // TODO: Hardcoded at A4 and portrait
238                 pageHeight = pageFormats['a4'][1] / k;
239                 pageWidth = pageFormats['a4'][0] / k;
240         }
241         
242         var out = function(string) {
243                 if(state == 2) {
244                         pages[page] += string + '\n';
245                 } else {
246                         buffer += string + '\n';
247                 }
248         }
249         
250         var _addPage = function() {
251                 beginPage();
252                 // Set line width
253                 out(sprintf('%.2f w', (lineWidth * k)));
254                 
255                 // Set font - TODO
256                 // 16 is the font size
257                 pageFontSize = fontSize;
258                 out('BT /F1 ' + parseInt(fontSize) + '.00 Tf ET');              
259         }
260         
261         // Add the first page automatically
262         _addPage();     
263
264         // Escape text
265         var pdfEscape = function(text) {
266                 return text.replace(/\\/g, '\\\\').replace(/\(/g, '\\(').replace(/\)/g, '\\)');
267         }
268         
269         return {
270                 addPage: function() {
271                         _addPage();
272                 },
273                 text: function(x, y, text) {
274                         // need page height
275                         if(pageFontSize != fontSize) {
276                                 out('BT /F1 ' + parseInt(fontSize) + '.00 Tf ET');
277                                 pageFontSize = fontSize;
278                         }
279                         var str = sprintf('BT %.2f %.2f Td (%s) Tj ET', x * k, (pageHeight - y) * k, pdfEscape(text));
280                         out(str);
281                 },
282                 setProperties: function(properties) {
283                         documentProperties = properties;
284                 },
285                 addImage: function(imageData, format, x, y, w, h) {
286                 
287                 },
288                 output: function(type, options) {
289                         endDocument();
290                         if(type == undefined) {
291                                 return buffer;
292                         }
293                         if(type == 'datauri') {
294                                 document.location.href = 'data:application/pdf;base64,' + Base64.encode(buffer);
295                         }
296                         // @TODO: Add different output options
297                 },
298                 setFontSize: function(size) {
299                         fontSize = size;
300                 }
301         }
302
303 };