-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp5.pdf.js
233 lines (206 loc) · 8.08 KB
/
p5.pdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* p5.pdf - Simple PDF module for p5.js using p5.svg and browser's print API
* Copyright (c) 2015 Zeno Zeng<zenoofzeng@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('p5.pdf', ['p5', 'p5.svg'], function (p5) {
factory(p5);
});
}
else if (typeof exports === 'object') {
module.exports = factory;
}
else {
factory(root['p5']);
}
}(this, function (p5) {
"use strict";
/**
* Print given elements using iframe
*
* @private
* @param {String} filename
* @param {Array} elements Array of Elements
* @param {Array} styles Array of style string
*/
var print = function(filename, elements, styles) {
var iframe = document.createElement("iframe");
iframe.height = 0;
iframe.width = 0;
document.body.appendChild(iframe);
var doc = iframe.contentDocument || iframe.contentWindow.documen;
var win = iframe.contentWindow;
var style = doc.createElement('style');
styles = styles.join('\n');
style.innerHTML = styles;
doc.head.appendChild(style);
elements.forEach(function(el) {
doc.body.appendChild(el);
});
win.focus(); // required for IE
// change the filename for print
var _title = document.title;
document.title = filename;
doc.title = filename;
win.print(); // note that window.print might be overridden by p5.js
document.title = _title;
iframe.remove();
};
/**
* Create a new p5.PDF instance.
*
* @class p5.PDF
* @return {p5.PDF} a p5.PDF instance
*/
function PDF(p5Instance) {
this.elements = [];
this.width = p5Instance.width;
this.height = p5Instance.height;
this.p5Instance = p5Instance;
}
/**
* Will return a clone of current SVG element
*/
PDF.prototype.__snapshot = function() {
var renderer = this.p5Instance._renderer || this.p5Instance._graphics;
var elt = renderer.isSVG ? renderer.svg : renderer.elt;
var snapshot = elt.cloneNode(true);
if (elt.nodeName.toLowerCase() === 'canvas') {
// for canvas, also copy its content
snapshot.getContext('2d').drawImage(elt, 0, 0);
}
snapshot.style.display = 'inline-block';
return snapshot;
};
/**
* Push current or last frame to this.elements
*/
PDF.prototype.__push = function() {
if (this.isRecording) {
this.elements.push(this.__snapshot());
} else {
if (this.lastFrame) {
this.elements.push(this.lastFrame);
this.lastFrame = null;
} else {
var div = document.createElement('div');
div.className = "empty-page";
this.elements.push(div);
}
}
};
/**
* Save current frame and move to next page
*
* @instance
* @function nextPage
* @memberof p5.PDF
*/
PDF.prototype.nextPage = function() {
this.__push();
var div = document.createElement('div');
div.className = "page-break";
this.elements.push(div);
};
PDF.prototype.nextColumn = function() {
this.elements.push(this.__snapshot());
var div = document.createElement('div');
div.className = "column-gap";
this.elements.push(div);
};
PDF.prototype.nextRow = function() {
this.elements.push(this.__snapshot());
var div = document.createElement('div');
div.className = "row-gap";
this.elements.push(div);
};
PDF.prototype.beginRecord = function() {
this.isRecording = true;
};
PDF.prototype.endRecord = function() {
this.lastFrame = this.__snapshot();
this.isRecording = false;
};
PDF.styles = [
"body, html, canvas, svg {margin: 0; padding: 0}",
".page-break {page-break-before: always;}",
".column-gap {display: inline-block}",
".empty-page {width: 1px; height: 1px}"
];
/**
* Save current PDF using window.print.
*
* @function save
* @memberof p5.PDF
* @param {Object} options - The options for generating pdf
* @param {String} options.filename - Filename for your pdf file, defaults to untitled
* @param {String} options.width - Page width, defaults to canvas width
* @param {String} options.height - Page height, defaults to canvas height
* @param {Object} options.margin - Margins for PDF Page {top, right, bottom, left}
* @param {String} options.margin.top - marginTop (eg. '1mm', '10px'), defaults to 0
* @param {String} options.margin.right - marginRight in mm, defaults to 0
* @param {String} options.margin.bottom - marginBottom in mm, defaults to 0
* @param {String} options.margin.left - marginLeft in mm, defaults to 0
* @param {String} options.columnGap - Size of the gap between columns (eg. '1mm', '10px'), defaults to 0
* @param {String} options.rowGap - Size of the gap between rows, defaults to 0
*/
PDF.prototype.save = function(options) {
options = options || {};
var styles = PDF.styles.concat();
// page size
var width = options.width || this.width + 'px';
var height = options.height || this.height + 'px';
if (typeof width === 'number') {
width += 'px';
}
if (typeof height === 'number') {
height += 'px';
}
styles.push('@page { size: ' + width + ' ' + height + ';}');
// using page's margin sometimes not work
// disable it, and control by other css
styles.push('@page { margin: 0 }');
styles.push('body { width: ' + width + '; box-sizing: border-box; }');
// page margin
options.margin = options.margin || {};
var top = options.margin.top || 0;
var bottom = options.margin.bottom || 0;
var left = options.margin.left || 0;
var right = options.margin.right || 0;
styles.push(['body { padding: ', top, right, bottom, left, '; }'].join(' '));
styles.push(['.page-break { padding-top: ', top, '; }'].join(' '));
if (typeof options.columnGap !== "undefined") {
styles.push(".column-gap {padding-left: " + options.columnGap + "}");
}
if (typeof options.rowGap !== "undefined") {
styles.push(".row-gap {padding-top: " + options.rowGap + "}");
}
var lastFrame = this.isRecording ? this.__snapshot() : this.lastFrame;
var elements = this.elements.concat(lastFrame);
var filename = typeof options.filename == "undefined" ? "untitled" : options.filename;
print(filename, elements, styles);
};
p5.PDF = PDF;
p5.prototype.createPDF = function() {
return new PDF(this);
};
}));