plugins_svg.js.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>plugins/svg.js - Documentation</title>
  6. <script src="scripts/prettify/prettify.js"></script>
  7. <script src="scripts/prettify/lang-css.js"></script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
  13. </head>
  14. <body>
  15. <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  16. <label for="nav-trigger" class="navicon-button x">
  17. <div class="navicon"></div>
  18. </label>
  19. <label for="nav-trigger" class="overlay"></label>
  20. <nav>
  21. <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>
  22. </nav>
  23. <div id="main">
  24. <h1 class="page-title">plugins/svg.js</h1>
  25. <section>
  26. <article>
  27. <pre class="prettyprint source linenums"><code>/** @preserve
  28. jsPDF SVG plugin
  29. Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
  30. */
  31. /**
  32. * Permission is hereby granted, free of charge, to any person obtaining
  33. * a copy of this software and associated documentation files (the
  34. * "Software"), to deal in the Software without restriction, including
  35. * without limitation the rights to use, copy, modify, merge, publish,
  36. * distribute, sublicense, and/or sell copies of the Software, and to
  37. * permit persons to whom the Software is furnished to do so, subject to
  38. * the following conditions:
  39. *
  40. * The above copyright notice and this permission notice shall be
  41. * included in all copies or substantial portions of the Software.
  42. *
  43. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  44. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  45. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  46. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  47. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  48. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  49. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  50. * ====================================================================
  51. */
  52. ;(function(jsPDFAPI) {
  53. 'use strict'
  54. /**
  55. Parses SVG XML and converts only some of the SVG elements into
  56. PDF elements.
  57. Supports:
  58. paths
  59. @public
  60. @function
  61. @param
  62. @returns {Type}
  63. */
  64. jsPDFAPI.addSVG = function(svgtext, x, y, w, h) {
  65. // 'this' is _jsPDF object returned when jsPDF is inited (new jsPDF())
  66. var undef
  67. if (x === undef || y === undef) {
  68. throw new Error("addSVG needs values for 'x' and 'y'");
  69. }
  70. function InjectCSS(cssbody, document) {
  71. var styletag = document.createElement('style');
  72. styletag.type = 'text/css';
  73. if (styletag.styleSheet) {
  74. // ie
  75. styletag.styleSheet.cssText = cssbody;
  76. } else {
  77. // others
  78. styletag.appendChild(document.createTextNode(cssbody));
  79. }
  80. document.getElementsByTagName("head")[0].appendChild(styletag);
  81. }
  82. function createWorkerNode(document){
  83. var frameID = 'childframe' // Date.now().toString() + '_' + (Math.random() * 100).toString()
  84. , frame = document.createElement('iframe')
  85. InjectCSS(
  86. '.jsPDF_sillysvg_iframe {display:none;position:absolute;}'
  87. , document
  88. )
  89. frame.name = frameID
  90. frame.setAttribute("width", 0)
  91. frame.setAttribute("height", 0)
  92. frame.setAttribute("frameborder", "0")
  93. frame.setAttribute("scrolling", "no")
  94. frame.setAttribute("seamless", "seamless")
  95. frame.setAttribute("class", "jsPDF_sillysvg_iframe")
  96. document.body.appendChild(frame)
  97. return frame
  98. }
  99. function attachSVGToWorkerNode(svgtext, frame){
  100. var framedoc = ( frame.contentWindow || frame.contentDocument ).document
  101. framedoc.write(svgtext)
  102. framedoc.close()
  103. return framedoc.getElementsByTagName('svg')[0]
  104. }
  105. function convertPathToPDFLinesArgs(path){
  106. 'use strict'
  107. // we will use 'lines' method call. it needs:
  108. // - starting coordinate pair
  109. // - array of arrays of vector shifts (2-len for line, 6 len for bezier)
  110. // - scale array [horizontal, vertical] ratios
  111. // - style (stroke, fill, both)
  112. var x = parseFloat(path[1])
  113. , y = parseFloat(path[2])
  114. , vectors = []
  115. , position = 3
  116. , len = path.length
  117. while (position &lt; len){
  118. if (path[position] === 'c'){
  119. vectors.push([
  120. parseFloat(path[position + 1])
  121. , parseFloat(path[position + 2])
  122. , parseFloat(path[position + 3])
  123. , parseFloat(path[position + 4])
  124. , parseFloat(path[position + 5])
  125. , parseFloat(path[position + 6])
  126. ])
  127. position += 7
  128. } else if (path[position] === 'l') {
  129. vectors.push([
  130. parseFloat(path[position + 1])
  131. , parseFloat(path[position + 2])
  132. ])
  133. position += 3
  134. } else {
  135. position += 1
  136. }
  137. }
  138. return [x,y,vectors]
  139. }
  140. var workernode = createWorkerNode(document)
  141. , svgnode = attachSVGToWorkerNode(svgtext, workernode)
  142. , scale = [1,1]
  143. , svgw = parseFloat(svgnode.getAttribute('width'))
  144. , svgh = parseFloat(svgnode.getAttribute('height'))
  145. if (svgw &amp;&amp; svgh) {
  146. // setting both w and h makes image stretch to size.
  147. // this may distort the image, but fits your demanded size
  148. if (w &amp;&amp; h) {
  149. scale = [w / svgw, h / svgh]
  150. }
  151. // if only one is set, that value is set as max and SVG
  152. // is scaled proportionately.
  153. else if (w) {
  154. scale = [w / svgw, w / svgw]
  155. } else if (h) {
  156. scale = [h / svgh, h / svgh]
  157. }
  158. }
  159. var i, l, tmp
  160. , linesargs
  161. , items = svgnode.childNodes
  162. for (i = 0, l = items.length; i &lt; l; i++) {
  163. tmp = items[i]
  164. if (tmp.tagName &amp;&amp; tmp.tagName.toUpperCase() === 'PATH') {
  165. linesargs = convertPathToPDFLinesArgs( tmp.getAttribute("d").split(' ') )
  166. // path start x coordinate
  167. linesargs[0] = linesargs[0] * scale[0] + x // where x is upper left X of image
  168. // path start y coordinate
  169. linesargs[1] = linesargs[1] * scale[1] + y // where y is upper left Y of image
  170. // the rest of lines are vectors. these will adjust with scale value auto.
  171. this.lines.call(
  172. this
  173. , linesargs[2] // lines
  174. , linesargs[0] // starting x
  175. , linesargs[1] // starting y
  176. , scale
  177. )
  178. }
  179. }
  180. // clean up
  181. // workernode.parentNode.removeChild(workernode)
  182. return this
  183. }
  184. })(jsPDF.API);
  185. </code></pre>
  186. </article>
  187. </section>
  188. </div>
  189. <br class="clear">
  190. <footer>
  191. 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.
  192. </footer>
  193. <script>prettyPrint();</script>
  194. <script src="scripts/linenumber.js"></script>
  195. </body>
  196. </html>