forked from parallax/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins_svg.js.html
More file actions
239 lines (196 loc) · 8.18 KB
/
Copy pathplugins_svg.js.html
File metadata and controls
239 lines (196 loc) · 8.18 KB
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
234
235
236
237
238
239
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>plugins/svg.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsPDF.html">jsPDF</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addHTML">addHTML</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#autoPrint">autoPrint</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#lstext">lstext</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDisplayMode">setDisplayMode</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setPage">setPage</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#triangle">triangle</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">plugins/svg.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/** @preserve
jsPDF SVG plugin
Copyright (c) 2012 Willow Systems Corporation, willow-systems.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(jsPDFAPI) {
'use strict'
/**
Parses SVG XML and converts only some of the SVG elements into
PDF elements.
Supports:
paths
@public
@function
@param
@returns {Type}
*/
jsPDFAPI.addSVG = function(svgtext, x, y, w, h) {
// 'this' is _jsPDF object returned when jsPDF is inited (new jsPDF())
var undef
if (x === undef || y === undef) {
throw new Error("addSVG needs values for 'x' and 'y'");
}
function InjectCSS(cssbody, document) {
var styletag = document.createElement('style');
styletag.type = 'text/css';
if (styletag.styleSheet) {
// ie
styletag.styleSheet.cssText = cssbody;
} else {
// others
styletag.appendChild(document.createTextNode(cssbody));
}
document.getElementsByTagName("head")[0].appendChild(styletag);
}
function createWorkerNode(document){
var frameID = 'childframe' // Date.now().toString() + '_' + (Math.random() * 100).toString()
, frame = document.createElement('iframe')
InjectCSS(
'.jsPDF_sillysvg_iframe {display:none;position:absolute;}'
, document
)
frame.name = frameID
frame.setAttribute("width", 0)
frame.setAttribute("height", 0)
frame.setAttribute("frameborder", "0")
frame.setAttribute("scrolling", "no")
frame.setAttribute("seamless", "seamless")
frame.setAttribute("class", "jsPDF_sillysvg_iframe")
document.body.appendChild(frame)
return frame
}
function attachSVGToWorkerNode(svgtext, frame){
var framedoc = ( frame.contentWindow || frame.contentDocument ).document
framedoc.write(svgtext)
framedoc.close()
return framedoc.getElementsByTagName('svg')[0]
}
function convertPathToPDFLinesArgs(path){
'use strict'
// we will use 'lines' method call. it needs:
// - starting coordinate pair
// - array of arrays of vector shifts (2-len for line, 6 len for bezier)
// - scale array [horizontal, vertical] ratios
// - style (stroke, fill, both)
var x = parseFloat(path[1])
, y = parseFloat(path[2])
, vectors = []
, position = 3
, len = path.length
while (position < len){
if (path[position] === 'c'){
vectors.push([
parseFloat(path[position + 1])
, parseFloat(path[position + 2])
, parseFloat(path[position + 3])
, parseFloat(path[position + 4])
, parseFloat(path[position + 5])
, parseFloat(path[position + 6])
])
position += 7
} else if (path[position] === 'l') {
vectors.push([
parseFloat(path[position + 1])
, parseFloat(path[position + 2])
])
position += 3
} else {
position += 1
}
}
return [x,y,vectors]
}
var workernode = createWorkerNode(document)
, svgnode = attachSVGToWorkerNode(svgtext, workernode)
, scale = [1,1]
, svgw = parseFloat(svgnode.getAttribute('width'))
, svgh = parseFloat(svgnode.getAttribute('height'))
if (svgw && svgh) {
// setting both w and h makes image stretch to size.
// this may distort the image, but fits your demanded size
if (w && h) {
scale = [w / svgw, h / svgh]
}
// if only one is set, that value is set as max and SVG
// is scaled proportionately.
else if (w) {
scale = [w / svgw, w / svgw]
} else if (h) {
scale = [h / svgh, h / svgh]
}
}
var i, l, tmp
, linesargs
, items = svgnode.childNodes
for (i = 0, l = items.length; i < l; i++) {
tmp = items[i]
if (tmp.tagName && tmp.tagName.toUpperCase() === 'PATH') {
linesargs = convertPathToPDFLinesArgs( tmp.getAttribute("d").split(' ') )
// path start x coordinate
linesargs[0] = linesargs[0] * scale[0] + x // where x is upper left X of image
// path start y coordinate
linesargs[1] = linesargs[1] * scale[1] + y // where y is upper left Y of image
// the rest of lines are vectors. these will adjust with scale value auto.
this.lines.call(
this
, linesargs[2] // lines
, linesargs[0] // starting x
, linesargs[1] // starting y
, scale
)
}
}
// clean up
// workernode.parentNode.removeChild(workernode)
return this
}
})(jsPDF.API);
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Sun Oct 09 2016 11:08:27 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>