plugins_context2d.js.html 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>plugins/context2d.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/context2d.js</h1>
  25. <section>
  26. <article>
  27. <pre class="prettyprint source linenums"><code>/**
  28. * jsPDF Context2D PlugIn Copyright (c) 2014 Steven Spungin (TwelveTone LLC) steven@twelvetone.tv
  29. *
  30. * Licensed under the MIT License. http://opensource.org/licenses/mit-license
  31. */
  32. /**
  33. * This plugin mimics the HTML5 Canvas's context2d.
  34. *
  35. * The goal is to provide a way for current canvas implementations to print directly to a PDF.
  36. */
  37. /**
  38. * TODO implement stroke opacity (refactor from fill() method )
  39. * TODO transform angle and radii parameters
  40. */
  41. /**
  42. * require('jspdf.js'); require('lib/css_colors.js');
  43. */
  44. (function (jsPDFAPI) {
  45. 'use strict';
  46. jsPDFAPI.events.push([
  47. 'initialized', function () {
  48. this.context2d.pdf = this;
  49. this.context2d.internal.pdf = this;
  50. this.context2d.ctx = new context();
  51. this.context2d.ctxStack = [];
  52. this.context2d.path = [];
  53. }
  54. ]);
  55. jsPDFAPI.context2d = {
  56. pageWrapXEnabled: false,
  57. pageWrapYEnabled: false,
  58. pageWrapX: 9999999,
  59. pageWrapY: 9999999,
  60. ctx: new context(),
  61. f2: function (number) {
  62. return number.toFixed(2);
  63. },
  64. fillRect: function (x, y, w, h) {
  65. if (this._isFillTransparent()) {
  66. return;
  67. }
  68. x = this._wrapX(x);
  69. y = this._wrapY(y);
  70. var xRect = this._matrix_map_rect(this.ctx._transform, {x: x, y: y, w: w, h: h});
  71. this.pdf.rect(xRect.x, xRect.y, xRect.w, xRect.h, "f");
  72. },
  73. strokeRect: function (x, y, w, h) {
  74. if (this._isStrokeTransparent()) {
  75. return;
  76. }
  77. x = this._wrapX(x);
  78. y = this._wrapY(y);
  79. var xRect = this._matrix_map_rect(this.ctx._transform, {x: x, y: y, w: w, h: h});
  80. this.pdf.rect(xRect.x, xRect.y, xRect.w, xRect.h, "s");
  81. },
  82. /**
  83. * We cannot clear PDF commands that were already written to PDF, so we use white instead. &lt;br />
  84. * As a special case, read a special flag (ignoreClearRect) and do nothing if it is set.
  85. * This results in all calls to clearRect() to do nothing, and keep the canvas transparent.
  86. * This flag is stored in the save/restore context and is managed the same way as other drawing states.
  87. * @param x
  88. * @param y
  89. * @param w
  90. * @param h
  91. */
  92. clearRect: function (x, y, w, h) {
  93. if (this.ctx.ignoreClearRect) {
  94. return;
  95. }
  96. x = this._wrapX(x);
  97. y = this._wrapY(y);
  98. var xRect = this._matrix_map_rect(this.ctx._transform, {x: x, y: y, w: w, h: h});
  99. this.save();
  100. this.setFillStyle('#ffffff');
  101. //TODO This is hack to fill with white.
  102. this.pdf.rect(xRect.x, xRect.y, xRect.w, xRect.h, "f");
  103. this.restore();
  104. },
  105. save: function () {
  106. this.ctx._fontSize = this.pdf.internal.getFontSize();
  107. var ctx = new context();
  108. ctx.copy(this.ctx);
  109. this.ctxStack.push(this.ctx);
  110. this.ctx = ctx;
  111. },
  112. restore: function () {
  113. this.ctx = this.ctxStack.pop();
  114. this.setFillStyle(this.ctx.fillStyle);
  115. this.setStrokeStyle(this.ctx.strokeStyle);
  116. this.setFont(this.ctx.font);
  117. this.pdf.setFontSize(this.ctx._fontSize);
  118. this.setLineCap(this.ctx.lineCap);
  119. this.setLineWidth(this.ctx.lineWidth);
  120. this.setLineJoin(this.ctx.lineJoin);
  121. },
  122. rect: function (x, y, w, h) {
  123. this.moveTo(x, y);
  124. this.lineTo(x + w, y);
  125. this.lineTo(x + w, y + h);
  126. this.lineTo(x, y + h);
  127. this.lineTo(x, y); //TODO not needed
  128. this.closePath();
  129. },
  130. beginPath: function () {
  131. this.path = [];
  132. },
  133. closePath: function () {
  134. this.path.push({
  135. type: 'close'
  136. });
  137. },
  138. _getRgba: function (style) {
  139. // get the decimal values of r, g, and b;
  140. var rgba = {};
  141. if (this.internal.rxTransparent.test(style)) {
  142. rgba.r = 0;
  143. rgba.g = 0;
  144. rgba.b = 0;
  145. rgba.a = 0;
  146. }
  147. else {
  148. var m = this.internal.rxRgb.exec(style);
  149. if (m != null) {
  150. rgba.r = parseInt(m[1]);
  151. rgba.g = parseInt(m[2]);
  152. rgba.b = parseInt(m[3]);
  153. rgba.a = 1;
  154. } else {
  155. m = this.internal.rxRgba.exec(style);
  156. if (m != null) {
  157. rgba.r = parseInt(m[1]);
  158. rgba.g = parseInt(m[2]);
  159. rgba.b = parseInt(m[3]);
  160. rgba.a = parseFloat(m[4]);
  161. } else {
  162. rgba.a = 1;
  163. if (style.charAt(0) != '#') {
  164. style = CssColors.colorNameToHex(style);
  165. if (!style) {
  166. style = '#000000';
  167. }
  168. } else {
  169. }
  170. if (style.length === 4) {
  171. rgba.r = style.substring(1, 2);
  172. rgba.r += r;
  173. rgba.g = style.substring(2, 3);
  174. rgba.g += g;
  175. rgba.b = style.substring(3, 4);
  176. rgba.b += b;
  177. } else {
  178. rgba.r = style.substring(1, 3);
  179. rgba.g = style.substring(3, 5);
  180. rgba.b = style.substring(5, 7);
  181. }
  182. rgba.r = parseInt(rgba.r, 16);
  183. rgba.g = parseInt(rgba.g, 16);
  184. rgba.b = parseInt(rgba.b, 16);
  185. }
  186. }
  187. }
  188. rgba.style = style;
  189. return rgba;
  190. },
  191. setFillStyle: function (style) {
  192. // get the decimal values of r, g, and b;
  193. var r, g, b, a;
  194. if (this.internal.rxTransparent.test(style)) {
  195. r = 0;
  196. g = 0;
  197. b = 0;
  198. a = 0;
  199. }
  200. else {
  201. var m = this.internal.rxRgb.exec(style);
  202. if (m != null) {
  203. r = parseInt(m[1]);
  204. g = parseInt(m[2]);
  205. b = parseInt(m[3]);
  206. a = 1;
  207. } else {
  208. m = this.internal.rxRgba.exec(style);
  209. if (m != null) {
  210. r = parseInt(m[1]);
  211. g = parseInt(m[2]);
  212. b = parseInt(m[3]);
  213. a = parseFloat(m[4]);
  214. } else {
  215. a = 1;
  216. if (style.charAt(0) != '#') {
  217. style = CssColors.colorNameToHex(style);
  218. if (!style) {
  219. style = '#000000';
  220. }
  221. } else {
  222. }
  223. if (style.length === 4) {
  224. r = style.substring(1, 2);
  225. r += r;
  226. g = style.substring(2, 3);
  227. g += g;
  228. b = style.substring(3, 4);
  229. b += b;
  230. } else {
  231. r = style.substring(1, 3);
  232. g = style.substring(3, 5);
  233. b = style.substring(5, 7);
  234. }
  235. r = parseInt(r, 16);
  236. g = parseInt(g, 16);
  237. b = parseInt(b, 16);
  238. }
  239. }
  240. }
  241. this.ctx.fillStyle = style;
  242. this.ctx._isFillTransparent = (a == 0);
  243. this.ctx._fillOpacity = a;
  244. this.pdf.setFillColor(r, g, b, {
  245. a: a
  246. });
  247. this.pdf.setTextColor(r, g, b, {
  248. a: a
  249. });
  250. },
  251. setStrokeStyle: function (style) {
  252. var rgba = this._getRgba(style);
  253. this.ctx.strokeStyle = rgba.style;
  254. this.ctx._isStrokeTransparent = (rgba.a == 0);
  255. this.ctx._strokeOpacity = rgba.a;
  256. //TODO jsPDF to handle rgba
  257. if (rgba.a === 0) {
  258. this.pdf.setDrawColor(255, 255, 255);
  259. }
  260. else if (rgba.a === 1) {
  261. this.pdf.setDrawColor(rgba.r, rgba.g, rgba.b);
  262. } else {
  263. //this.pdf.setDrawColor(rgba.r, rgba.g, rgba.b, {a: rgba.a});
  264. this.pdf.setDrawColor(rgba.r, rgba.g, rgba.b);
  265. }
  266. },
  267. fillText: function (text, x, y, maxWidth) {
  268. if (this._isFillTransparent()) {
  269. return;
  270. }
  271. x = this._wrapX(x);
  272. y = this._wrapY(y);
  273. var xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  274. x = xpt[0];
  275. y = xpt[1];
  276. var rads = this._matrix_rotation(this.ctx._transform);
  277. var degs = rads * 57.2958;
  278. //TODO only push the clip if it has not been applied to the current PDF context
  279. if (this.ctx._clip_path.length > 0) {
  280. var lines;
  281. if (window.outIntercept) {
  282. lines = window.outIntercept.type === 'group' ? window.outIntercept.stream : window.outIntercept;
  283. } else {
  284. lines = this.pdf.internal.pages[1];
  285. }
  286. lines.push("q");
  287. var origPath = this.path;
  288. this.path = this.ctx._clip_path;
  289. this.ctx._clip_path = [];
  290. this._fill(null, true);
  291. this.ctx._clip_path = this.path;
  292. this.path = origPath;
  293. }
  294. this.pdf.text(text, x, this._getBaseline(y), null, degs);
  295. if (this.ctx._clip_path.length > 0) {
  296. lines.push('Q');
  297. }
  298. },
  299. strokeText: function (text, x, y, maxWidth) {
  300. if (this._isStrokeTransparent()) {
  301. return;
  302. }
  303. x = this._wrapX(x);
  304. y = this._wrapY(y);
  305. var xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  306. x = xpt[0];
  307. y = xpt[1];
  308. var rads = this._matrix_rotation(this.ctx._transform);
  309. var degs = rads * 57.2958;
  310. //TODO only push the clip if it has not been applied to the current PDF context
  311. if (this.ctx._clip_path.length > 0) {
  312. var lines;
  313. if (window.outIntercept) {
  314. lines = window.outIntercept.type === 'group' ? window.outIntercept.stream : window.outIntercept;
  315. } else {
  316. lines = this.pdf.internal.pages[1];
  317. }
  318. lines.push("q");
  319. var origPath = this.path;
  320. this.path = this.ctx._clip_path;
  321. this.ctx._clip_path = [];
  322. this._fill(null, true);
  323. this.ctx._clip_path = this.path;
  324. this.path = origPath;
  325. }
  326. this.pdf.text(text, x, this._getBaseline(y), {
  327. stroke: true
  328. }, degs);
  329. if (this.ctx._clip_path.length > 0) {
  330. lines.push('Q');
  331. }
  332. },
  333. setFont: function (font) {
  334. this.ctx.font = font;
  335. //var rx = /\s*(\w+)\s+(\w+)\s+(\w+)\s+([\d\.]+)(px|pt|em)\s+["']?(\w+)['"]?/;
  336. var rx = /\s*(\w+)\s+(\w+)\s+(\w+)\s+([\d\.]+)(px|pt|em)\s+(.*)?/;
  337. m = rx.exec(font);
  338. if (m != null) {
  339. var fontStyle = m[1];
  340. var fontVariant = m[2];
  341. var fontWeight = m[3];
  342. var fontSize = m[4];
  343. var fontSizeUnit = m[5];
  344. var fontFamily = m[6];
  345. if ('px' === fontSizeUnit) {
  346. fontSize = Math.floor(parseFloat(fontSize));
  347. // fontSize = fontSize * 1.25;
  348. } else if ('em' === fontSizeUnit) {
  349. fontSize = Math.floor(parseFloat(fontSize) * this.pdf.getFontSize());
  350. } else {
  351. fontSize = Math.floor(parseFloat(fontSize));
  352. }
  353. this.pdf.setFontSize(fontSize);
  354. if (fontWeight === 'bold' || fontWeight === '700') {
  355. this.pdf.setFontStyle('bold');
  356. } else {
  357. if (fontStyle === 'italic') {
  358. this.pdf.setFontStyle('italic');
  359. } else {
  360. this.pdf.setFontStyle('normal');
  361. }
  362. }
  363. var name = fontFamily;
  364. var parts = name.toLowerCase().split(/\s*,\s*/);
  365. var jsPdfFontName;
  366. if (parts.indexOf('arial') != -1) {
  367. jsPdfFontName = 'Arial';
  368. }
  369. else if (parts.indexOf('verdana') != -1) {
  370. jsPdfFontName = 'Verdana';
  371. }
  372. else if (parts.indexOf('helvetica') != -1) {
  373. jsPdfFontName = 'Helvetica';
  374. }
  375. else if (parts.indexOf('sans-serif') != -1) {
  376. jsPdfFontName = 'sans-serif';
  377. }
  378. else if (parts.indexOf('fixed') != -1) {
  379. jsPdfFontName = 'Fixed';
  380. }
  381. else if (parts.indexOf('monospace') != -1) {
  382. jsPdfFontName = 'Monospace';
  383. }
  384. else if (parts.indexOf('terminal') != -1) {
  385. jsPdfFontName = 'Terminal';
  386. }
  387. else if (parts.indexOf('courier') != -1) {
  388. jsPdfFontName = 'Courier';
  389. }
  390. else if (parts.indexOf('times') != -1) {
  391. jsPdfFontName = 'Times';
  392. }
  393. else if (parts.indexOf('cursive') != -1) {
  394. jsPdfFontName = 'Cursive';
  395. }
  396. else if (parts.indexOf('fantasy') != -1) {
  397. jsPdfFontName = 'Fantasy';
  398. }
  399. else if (parts.indexOf('serif') != -1) {
  400. jsPdfFontName = 'Serif';
  401. }
  402. else {
  403. jsPdfFontName = 'Serif';
  404. }
  405. //TODO check more cases
  406. var style;
  407. if ('bold' === fontWeight) {
  408. style = 'bold';
  409. } else {
  410. style = 'normal';
  411. }
  412. this.pdf.setFont(jsPdfFontName, style);
  413. } else {
  414. var rx = /(\d+)(pt|px|em)\s+(\w+)\s*(\w+)?/;
  415. var m = rx.exec(font);
  416. if (m != null) {
  417. var size = m[1];
  418. var unit = m[2];
  419. var name = m[3];
  420. var style = m[4];
  421. if (!style) {
  422. style = 'normal';
  423. }
  424. if ('em' === fontSizeUnit) {
  425. size = Math.floor(parseFloat(fontSize) * this.pdf.getFontSize());
  426. } else {
  427. size = Math.floor(parseFloat(size));
  428. }
  429. this.pdf.setFontSize(size);
  430. this.pdf.setFont(name, style);
  431. }
  432. }
  433. },
  434. setTextBaseline: function (baseline) {
  435. this.ctx.textBaseline = baseline;
  436. },
  437. getTextBaseline: function () {
  438. return this.ctx.textBaseline;
  439. },
  440. //TODO implement textAlign
  441. setTextAlign: function (align) {
  442. this.ctx.textAlign = align;
  443. },
  444. getTextAlign: function () {
  445. return this.ctx.textAlign;
  446. },
  447. setLineWidth: function (width) {
  448. this.ctx.lineWidth = width;
  449. this.pdf.setLineWidth(width);
  450. },
  451. setLineCap: function (style) {
  452. this.ctx.lineCap = style;
  453. this.pdf.setLineCap(style);
  454. },
  455. setLineJoin: function (style) {
  456. this.ctx.lineJoin = style;
  457. this.pdf.setLineJoin(style);
  458. },
  459. moveTo: function (x, y) {
  460. x = this._wrapX(x);
  461. y = this._wrapY(y);
  462. var xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  463. x = xpt[0];
  464. y = xpt[1];
  465. var obj = {
  466. type: 'mt',
  467. x: x,
  468. y: y
  469. };
  470. this.path.push(obj);
  471. },
  472. _wrapX: function (x) {
  473. if (this.pageWrapXEnabled) {
  474. return x % this.pageWrapX;
  475. } else {
  476. return x;
  477. }
  478. },
  479. _wrapY: function (y) {
  480. if (this.pageWrapYEnabled) {
  481. this._gotoPage(this._page(y));
  482. return (y - this.lastBreak) % this.pageWrapY;
  483. } else {
  484. return y;
  485. }
  486. },
  487. transform: function (a, b, c, d, e, f) {
  488. //TODO apply to current transformation instead of replacing
  489. this.ctx._transform = [a, b, c, d, e, f];
  490. },
  491. setTransform: function (a, b, c, d, e, f) {
  492. this.ctx._transform = [a, b, c, d, e, f];
  493. },
  494. _getTransform: function () {
  495. return this.ctx._transform;
  496. },
  497. lastBreak: 0,
  498. // Y Position of page breaks.
  499. pageBreaks: [],
  500. // returns: One-based Page Number
  501. // Should only be used if pageWrapYEnabled is true
  502. _page: function (y) {
  503. if (this.pageWrapYEnabled) {
  504. this.lastBreak = 0;
  505. var manualBreaks = 0;
  506. var autoBreaks = 0;
  507. for (var i = 0; i &lt; this.pageBreaks.length; i++) {
  508. if (y >= this.pageBreaks[i]) {
  509. manualBreaks++;
  510. if (this.lastBreak === 0) {
  511. autoBreaks++;
  512. }
  513. var spaceBetweenLastBreak = this.pageBreaks[i] - this.lastBreak;
  514. this.lastBreak = this.pageBreaks[i];
  515. var pagesSinceLastBreak = Math.floor(spaceBetweenLastBreak / this.pageWrapY);
  516. autoBreaks += pagesSinceLastBreak;
  517. }
  518. }
  519. if (this.lastBreak === 0) {
  520. var pagesSinceLastBreak = Math.floor(y / this.pageWrapY) + 1;
  521. autoBreaks += pagesSinceLastBreak;
  522. }
  523. return autoBreaks + manualBreaks;
  524. } else {
  525. return this.pdf.internal.getCurrentPageInfo().pageNumber;
  526. }
  527. },
  528. _gotoPage: function (pageOneBased) {
  529. // This is a stub to be overriden if needed
  530. },
  531. lineTo: function (x, y) {
  532. x = this._wrapX(x);
  533. y = this._wrapY(y);
  534. var xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  535. x = xpt[0];
  536. y = xpt[1];
  537. var obj = {
  538. type: 'lt',
  539. x: x,
  540. y: y
  541. };
  542. this.path.push(obj);
  543. },
  544. bezierCurveTo: function (x1, y1, x2, y2, x, y) {
  545. x1 = this._wrapX(x1);
  546. y1 = this._wrapY(y1);
  547. x2 = this._wrapX(x2);
  548. y2 = this._wrapY(y2);
  549. x = this._wrapX(x);
  550. y = this._wrapY(y);
  551. var xpt;
  552. xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  553. x = xpt[0];
  554. y = xpt[1];
  555. xpt = this._matrix_map_point(this.ctx._transform, [x1, y1]);
  556. x1 = xpt[0];
  557. y1 = xpt[1];
  558. xpt = this._matrix_map_point(this.ctx._transform, [x2, y2]);
  559. x2 = xpt[0];
  560. y2 = xpt[1];
  561. var obj = {
  562. type: 'bct',
  563. x1: x1,
  564. y1: y1,
  565. x2: x2,
  566. y2: y2,
  567. x: x,
  568. y: y
  569. };
  570. this.path.push(obj);
  571. },
  572. quadraticCurveTo: function (x1, y1, x, y) {
  573. x1 = this._wrapX(x1);
  574. y1 = this._wrapY(y1);
  575. x = this._wrapX(x);
  576. y = this._wrapY(y);
  577. var xpt;
  578. xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  579. x = xpt[0];
  580. y = xpt[1];
  581. xpt = this._matrix_map_point(this.ctx._transform, [x1, y1]);
  582. x1 = xpt[0];
  583. y1 = xpt[1];
  584. var obj = {
  585. type: 'qct',
  586. x1: x1,
  587. y1: y1,
  588. x: x,
  589. y: y
  590. };
  591. this.path.push(obj);
  592. },
  593. arc: function (x, y, radius, startAngle, endAngle, anticlockwise) {
  594. x = this._wrapX(x);
  595. y = this._wrapY(y);
  596. var xpt = this._matrix_map_point(this.ctx._transform, [x, y]);
  597. x = xpt[0];
  598. y = xpt[1];
  599. var obj = {
  600. type: 'arc',
  601. x: x,
  602. y: y,
  603. radius: radius,
  604. startAngle: startAngle,
  605. endAngle: endAngle,
  606. anticlockwise: anticlockwise
  607. };
  608. this.path.push(obj);
  609. },
  610. drawImage: function (img, x, y, w, h, x2, y2, w2, h2) {
  611. if (x2 !== undefined) {
  612. x = x2;
  613. y = y2;
  614. w = w2;
  615. h = h2;
  616. }
  617. x = this._wrapX(x);
  618. y = this._wrapY(y);
  619. var xRect = this._matrix_map_rect(this.ctx._transform, {x: x, y: y, w: w, h: h});
  620. var xRect2 = this._matrix_map_rect(this.ctx._transform, {x: x2, y: y2, w: w2, h: h2});
  621. // TODO implement source clipping and image scaling
  622. var format;
  623. var rx = /data:image\/(\w+).*/i;
  624. var m = rx.exec(img);
  625. if (m != null) {
  626. format = m[1];
  627. } else {
  628. // format = "jpeg";
  629. format = "png";
  630. }
  631. this.pdf.addImage(img, format, xRect.x, xRect.y, xRect.w, xRect.h);
  632. },
  633. /**
  634. * Multiply the first matrix by the second
  635. * @param m1
  636. * @param m2
  637. * @returns {*[]}
  638. * @private
  639. */
  640. _matrix_multiply: function (m2, m1) {
  641. var sx = m1[0];
  642. var shy = m1[1];
  643. var shx = m1[2];
  644. var sy = m1[3];
  645. var tx = m1[4];
  646. var ty = m1[5];
  647. var t0 = sx * m2[0] + shy * m2[2];
  648. var t2 = shx * m2[0] + sy * m2[2];
  649. var t4 = tx * m2[0] + ty * m2[2] + m2[4];
  650. shy = sx * m2[1] + shy * m2[3];
  651. sy = shx * m2[1] + sy * m2[3];
  652. ty = tx * m2[1] + ty * m2[3] + m2[5];
  653. sx = t0;
  654. shx = t2;
  655. tx = t4;
  656. return [sx, shy, shx, sy, tx, ty];
  657. },
  658. _matrix_rotation: function (m) {
  659. return Math.atan2(m[2], m[0]);
  660. },
  661. _matrix_decompose: function (matrix) {
  662. var a = matrix[0];
  663. var b = matrix[1];
  664. var c = matrix[2];
  665. var d = matrix[3];
  666. var scaleX = Math.sqrt(a * a + b * b);
  667. a /= scaleX;
  668. b /= scaleX;
  669. var shear = a * c + b * d;
  670. c -= a * shear;
  671. d -= b * shear;
  672. var scaleY = Math.sqrt(c * c + d * d);
  673. c /= scaleY;
  674. d /= scaleY;
  675. shear /= scaleY;
  676. if (a * d &lt; b * c) {
  677. a = -a;
  678. b = -b;
  679. shear = -shear;
  680. scaleX = -scaleX;
  681. }
  682. return {
  683. scale: [scaleX, 0, 0, scaleY, 0, 0],
  684. translate: [1, 0, 0, 1, matrix[4], matrix[5]],
  685. rotate: [a, b, -b, a, 0, 0],
  686. skew: [1, 0, shear, 1, 0, 0]
  687. };
  688. },
  689. _matrix_map_point: function (m1, pt) {
  690. var sx = m1[0];
  691. var shy = m1[1];
  692. var shx = m1[2];
  693. var sy = m1[3];
  694. var tx = m1[4];
  695. var ty = m1[5];
  696. var px = pt[0];
  697. var py = pt[1];
  698. var x = px * sx + py * shx + tx;
  699. var y = px * shy + py * sy + ty;
  700. return [x, y];
  701. },
  702. _matrix_map_point_obj: function (m1, pt) {
  703. var xpt = this._matrix_map_point(m1, [pt.x, pt.y]);
  704. return {x: xpt[0], y: xpt[1]};
  705. },
  706. _matrix_map_rect: function (m1, rect) {
  707. var p1 = this._matrix_map_point(m1, [rect.x, rect.y]);
  708. var p2 = this._matrix_map_point(m1, [rect.x + rect.w, rect.y + rect.h]);
  709. return {x: p1[0], y: p1[1], w: p2[0] - p1[0], h: p2[1] - p1[1]};
  710. },
  711. _matrix_is_identity: function (m1) {
  712. if (m1[0] != 1) {
  713. return false;
  714. }
  715. if (m1[1] != 0) {
  716. return false;
  717. }
  718. if (m1[2] != 0) {
  719. return false;
  720. }
  721. if (m1[3] != 1) {
  722. return false;
  723. }
  724. if (m1[4] != 0) {
  725. return false;
  726. }
  727. if (m1[5] != 0) {
  728. return false;
  729. }
  730. return true;
  731. },
  732. rotate: function (angle) {
  733. var matrix = [Math.cos(angle), Math.sin(angle), -Math.sin(angle), Math.cos(angle), 0.0, 0.0];
  734. this.ctx._transform = this._matrix_multiply(this.ctx._transform, matrix);
  735. },
  736. scale: function (sx, sy) {
  737. var matrix = [sx, 0.0, 0.0, sy, 0.0, 0.0];
  738. this.ctx._transform = this._matrix_multiply(this.ctx._transform, matrix);
  739. },
  740. translate: function (x, y) {
  741. var matrix = [1.0, 0.0, 0.0, 1.0, x, y];
  742. this.ctx._transform = this._matrix_multiply(this.ctx._transform, matrix);
  743. },
  744. stroke: function () {
  745. if (this.ctx._clip_path.length > 0) {
  746. var lines;
  747. if (window.outIntercept) {
  748. lines = window.outIntercept.type === 'group' ? window.outIntercept.stream : window.outIntercept;
  749. } else {
  750. lines = this.pdf.internal.pages[1];
  751. }
  752. lines.push("q");
  753. var origPath = this.path;
  754. this.path = this.ctx._clip_path;
  755. this.ctx._clip_path = [];
  756. this._stroke(true);
  757. this.ctx._clip_path = this.path;
  758. this.path = origPath;
  759. this._stroke(false);
  760. lines.push("Q");
  761. } else {
  762. this._stroke(false);
  763. }
  764. },
  765. _stroke: function (isClip) {
  766. if (!isClip &amp;&amp; this._isStrokeTransparent()) {
  767. return;
  768. }
  769. //TODO opacity
  770. var moves = [];
  771. var closed = false;
  772. var xPath = this.path;
  773. for (var i = 0; i &lt; xPath.length; i++) {
  774. var pt = xPath[i];
  775. switch (pt.type) {
  776. case 'mt':
  777. moves.push({start: pt, deltas: [], abs: []});
  778. break;
  779. case 'lt':
  780. var delta = [
  781. pt.x - xPath[i - 1].x, pt.y - xPath[i - 1].y
  782. ];
  783. moves[moves.length - 1].deltas.push(delta);
  784. moves[moves.length - 1].abs.push(pt);
  785. break;
  786. case 'bct':
  787. var delta = [
  788. pt.x1 - xPath[i - 1].x, pt.y1 - xPath[i - 1].y,
  789. pt.x2 - xPath[i - 1].x, pt.y2 - xPath[i - 1].y,
  790. pt.x - xPath[i - 1].x, pt.y - xPath[i - 1].y
  791. ];
  792. moves[moves.length - 1].deltas.push(delta);
  793. break;
  794. case 'qct':
  795. // convert to bezier
  796. var x1 = xPath[i - 1].x + 2.0 / 3.0 * (pt.x1 - xPath[i - 1].x);
  797. var y1 = xPath[i - 1].y + 2.0 / 3.0 * (pt.y1 - xPath[i - 1].y);
  798. var x2 = pt.x + 2.0 / 3.0 * (pt.x1 - pt.x);
  799. var y2 = pt.y + 2.0 / 3.0 * (pt.y1 - pt.y);
  800. var x3 = pt.x;
  801. var y3 = pt.y;
  802. var delta = [
  803. x1 - xPath[i - 1].x, y1 - xPath[i - 1].y,
  804. x2 - xPath[i - 1].x, y2 - xPath[i - 1].y,
  805. x3 - xPath[i - 1].x, y3 - xPath[i - 1].y
  806. ];
  807. moves[moves.length - 1].deltas.push(delta);
  808. break;
  809. case 'arc':
  810. moves[moves.length - 1].arc = true;
  811. moves[moves.length - 1].abs.push(pt);
  812. break;
  813. case 'close':
  814. closed = true;
  815. break;
  816. }
  817. }
  818. for (var i = 0; i &lt; moves.length; i++) {
  819. var style;
  820. if (i == moves.length - 1) {
  821. style = 's';
  822. } else {
  823. style = null;
  824. }
  825. if (moves[i].arc) {
  826. var arcs = moves[i].abs;
  827. for (var ii = 0; ii &lt; arcs.length; ii++) {
  828. var arc = arcs[ii];
  829. var start = arc.startAngle * 360 / (2 * Math.PI);
  830. var end = arc.endAngle * 360 / (2 * Math.PI);
  831. var x = arc.x;
  832. var y = arc.y;
  833. this.internal.arc2(this, x, y, arc.radius, start, end, arc.anticlockwise, style, isClip);
  834. }
  835. } else {
  836. var x = moves[i].start.x;
  837. var y = moves[i].start.y;
  838. if (!isClip) {
  839. this.pdf.lines(moves[i].deltas, x, y, null, style);
  840. } else {
  841. this.pdf.lines(moves[i].deltas, x, y, null, null);
  842. this.pdf.clip_fixed();
  843. }
  844. }
  845. }
  846. },
  847. _isFillTransparent: function () {
  848. return this.ctx._isFillTransparent || this.globalAlpha == 0;
  849. },
  850. _isStrokeTransparent: function () {
  851. return this.ctx._isStrokeTransparent || this.globalAlpha == 0;
  852. },
  853. fill: function (fillRule) { //evenodd or nonzero (default)
  854. if (this.ctx._clip_path.length > 0) {
  855. var lines;
  856. if (window.outIntercept) {
  857. lines = window.outIntercept.type === 'group' ? window.outIntercept.stream : window.outIntercept;
  858. } else {
  859. lines = this.pdf.internal.pages[1];
  860. }
  861. lines.push("q");
  862. var origPath = this.path;
  863. this.path = this.ctx._clip_path;
  864. this.ctx._clip_path = [];
  865. this._fill(fillRule, true);
  866. this.ctx._clip_path = this.path;
  867. this.path = origPath;
  868. this._fill(fillRule, false);
  869. lines.push('Q');
  870. } else {
  871. this._fill(fillRule, false);
  872. }
  873. },
  874. _fill: function (fillRule, isClip) {
  875. if (this._isFillTransparent()) {
  876. return;
  877. }
  878. var v2Support = typeof this.pdf.internal.newObject2 === 'function';
  879. var lines;
  880. if (window.outIntercept) {
  881. lines = window.outIntercept.type === 'group' ? window.outIntercept.stream : window.outIntercept;
  882. } else {
  883. lines = this.pdf.internal.pages[1];
  884. }
  885. // if (this.ctx._clip_path.length > 0) {
  886. // lines.push('q');
  887. // var oldPath = this.path;
  888. // this.path = this.ctx._clip_path;
  889. // this.ctx._clip_path = [];
  890. // this._fill(fillRule, true);
  891. // this.ctx._clip_path = this.path;
  892. // this.path = oldPath;
  893. // }
  894. var moves = [];
  895. var outInterceptOld = window.outIntercept;
  896. if (v2Support) {
  897. // Blend and Mask
  898. switch (this.ctx.globalCompositeOperation) {
  899. case 'normal':
  900. case 'source-over':
  901. break;
  902. case 'destination-in':
  903. case 'destination-out':
  904. //TODO this need to be added to the current group or page
  905. // define a mask stream
  906. var obj = this.pdf.internal.newStreamObject();
  907. // define a mask state
  908. var obj2 = this.pdf.internal.newObject2();
  909. obj2.push('&lt;&lt;/Type /ExtGState');
  910. obj2.push('/SMask &lt;&lt;/S /Alpha /G ' + obj.objId + ' 0 R>>'); // /S /Luminosity will need to define color space
  911. obj2.push('>>');
  912. // add mask to page resources
  913. var gsName = 'MASK' + obj2.objId;
  914. this.pdf.internal.addGraphicsState(gsName, obj2.objId);
  915. var instruction = '/' + gsName + ' gs';
  916. // add mask to page, group, or stream
  917. lines.splice(0, 0, 'q');
  918. lines.splice(1, 0, instruction);
  919. lines.push('Q');
  920. window.outIntercept = obj;
  921. break;
  922. default:
  923. var dictionaryEntry = '/' + this.pdf.internal.blendModeMap[this.ctx.globalCompositeOperation.toUpperCase()];
  924. if (dictionaryEntry) {
  925. this.pdf.internal.out(dictionaryEntry + ' gs');
  926. }
  927. break;
  928. }
  929. }
  930. var alpha = this.ctx.globalAlpha;
  931. if (this.ctx._fillOpacity &lt; 1) {
  932. // TODO combine this with global opacity
  933. alpha = this.ctx._fillOpacity;
  934. }
  935. //TODO check for an opacity graphics state that was already created
  936. //TODO do not set opacity if current value is already active
  937. if (v2Support) {
  938. var objOpac = this.pdf.internal.newObject2();
  939. objOpac.push('&lt;&lt;/Type /ExtGState');
  940. //objOpac.push(this.ctx.globalAlpha + " CA"); // Stroke
  941. //objOpac.push(this.ctx.globalAlpha + " ca"); // Not Stroke
  942. objOpac.push('/CA ' + alpha); // Stroke
  943. objOpac.push('/ca ' + alpha); // Not Stroke
  944. objOpac.push('>>');
  945. var gsName = 'GS_O_' + objOpac.objId;
  946. this.pdf.internal.addGraphicsState(gsName, objOpac.objId);
  947. this.pdf.internal.out('/' + gsName + ' gs');
  948. }
  949. var xPath = this.path;
  950. for (var i = 0; i &lt; xPath.length; i++) {
  951. var pt = xPath[i];
  952. switch (pt.type) {
  953. case 'mt':
  954. moves.push({start: pt, deltas: [], abs: []});
  955. break;
  956. case 'lt':
  957. var delta = [
  958. pt.x - xPath[i - 1].x, pt.y - xPath[i - 1].y
  959. ];
  960. moves[moves.length - 1].deltas.push(delta);
  961. moves[moves.length - 1].abs.push(pt);
  962. break;
  963. case 'bct':
  964. var delta = [
  965. pt.x1 - xPath[i - 1].x, pt.y1 - xPath[i - 1].y,
  966. pt.x2 - xPath[i - 1].x, pt.y2 - xPath[i - 1].y,
  967. pt.x - xPath[i - 1].x, pt.y - xPath[i - 1].y
  968. ];
  969. moves[moves.length - 1].deltas.push(delta);
  970. break;
  971. case 'qct':
  972. // convert to bezier
  973. var x1 = xPath[i - 1].x + 2.0 / 3.0 * (pt.x1 - xPath[i - 1].x);
  974. var y1 = xPath[i - 1].y + 2.0 / 3.0 * (pt.y1 - xPath[i - 1].y);
  975. var x2 = pt.x + 2.0 / 3.0 * (pt.x1 - pt.x);
  976. var y2 = pt.y + 2.0 / 3.0 * (pt.y1 - pt.y);
  977. var x3 = pt.x;
  978. var y3 = pt.y;
  979. var delta = [
  980. x1 - xPath[i - 1].x, y1 - xPath[i - 1].y,
  981. x2 - xPath[i - 1].x, y2 - xPath[i - 1].y,
  982. x3 - xPath[i - 1].x, y3 - xPath[i - 1].y
  983. ];
  984. moves[moves.length - 1].deltas.push(delta);
  985. break;
  986. case 'arc':
  987. //TODO this was hack to avoid out of bounds issue
  988. if (moves.length == 0) {
  989. moves.push({start: {x: 0, y: 0}, deltas: [], abs: []});
  990. }
  991. moves[moves.length - 1].arc = true;
  992. moves[moves.length - 1].abs.push(pt);
  993. break;
  994. case 'close':
  995. //moves[moves.length - 1].deltas.push('close');
  996. break;
  997. }
  998. }
  999. for (var i = 0; i &lt; moves.length; i++) {
  1000. var style;
  1001. if (i == moves.length - 1) {
  1002. style = 'f';
  1003. if (fillRule === 'evenodd') {
  1004. style += '*';
  1005. }
  1006. } else {
  1007. style = null;
  1008. }
  1009. if (moves[i].arc) {
  1010. var arcs = moves[i].abs;
  1011. for (var ii = 0; ii &lt; arcs.length; ii++) {
  1012. var arc = arcs[ii];
  1013. //TODO lines deltas were getting in here
  1014. if (typeof arc.startAngle !== 'undefined') {
  1015. var start = arc.startAngle * 360 / (2 * Math.PI);
  1016. var end = arc.endAngle * 360 / (2 * Math.PI);
  1017. // Add the current position (last move to)
  1018. //var x = moves[i].start.x + arc.x;
  1019. //var y = moves[i].start.y + arc.y;
  1020. var x = arc.x;
  1021. var y = arc.y;
  1022. if (ii == 0) {
  1023. this.internal.move2(this, x, y);
  1024. }
  1025. this.internal.arc2(this, x, y, arc.radius, start, end, arc.anticlockwise, null, isClip);
  1026. } else {
  1027. this.internal.line2(c2d, arc.x, arc.y);
  1028. }
  1029. }
  1030. // extra move bug causing close to resolve to wrong point
  1031. var x = moves[i].start.x;
  1032. var y = moves[i].start.y;
  1033. this.internal.line2(c2d, x, y);
  1034. this.pdf.internal.out('h');
  1035. this.pdf.internal.out('f');
  1036. }
  1037. else {
  1038. var x = moves[i].start.x;
  1039. var y = moves[i].start.y;
  1040. if (!isClip) {
  1041. this.pdf.lines(moves[i].deltas, x, y, null, style);
  1042. } else {
  1043. this.pdf.lines(moves[i].deltas, x, y, null, null);
  1044. this.pdf.clip_fixed();
  1045. }
  1046. }
  1047. }
  1048. window.outIntercept = outInterceptOld;
  1049. // if (this.ctx._clip_path.length > 0) {
  1050. // lines.push('Q');
  1051. // }
  1052. },
  1053. pushMask: function () {
  1054. var v2Support = typeof this.pdf.internal.newObject2 === 'function';
  1055. if (!v2Support) {
  1056. console.log('jsPDF v2 not enabled')
  1057. return;
  1058. }
  1059. // define a mask stream
  1060. var obj = this.pdf.internal.newStreamObject();
  1061. // define a mask state
  1062. var obj2 = this.pdf.internal.newObject2();
  1063. obj2.push('&lt;&lt;/Type /ExtGState');
  1064. obj2.push('/SMask &lt;&lt;/S /Alpha /G ' + obj.objId + ' 0 R>>'); // /S /Luminosity will need to define color space
  1065. obj2.push('>>');
  1066. // add mask to page resources
  1067. var gsName = 'MASK' + obj2.objId;
  1068. this.pdf.internal.addGraphicsState(gsName, obj2.objId);
  1069. var instruction = '/' + gsName + ' gs';
  1070. this.pdf.internal.out(instruction);
  1071. },
  1072. clip: function () {
  1073. //TODO do we reset the path, or just copy it?
  1074. if (this.ctx._clip_path.length > 0) {
  1075. for (var i = 0; i &lt; this.path.length; i++) {
  1076. this.ctx._clip_path.push(this.path[i]);
  1077. }
  1078. } else {
  1079. this.ctx._clip_path = this.path;
  1080. }
  1081. this.path = [];
  1082. },
  1083. measureText: function (text) {
  1084. var pdf = this.pdf;
  1085. return {
  1086. getWidth: function () {
  1087. var fontSize = pdf.internal.getFontSize();
  1088. var txtWidth = pdf.getStringUnitWidth(text) * fontSize / pdf.internal.scaleFactor;
  1089. return txtWidth;
  1090. },
  1091. get width() {
  1092. return this.getWidth(text);
  1093. }
  1094. }
  1095. },
  1096. _getBaseline: function (y) {
  1097. var height = parseInt(this.pdf.internal.getFontSize());
  1098. // TODO Get descent from font descriptor
  1099. var descent = height * .25;
  1100. switch (this.ctx.textBaseline) {
  1101. case 'bottom':
  1102. return y - descent;
  1103. case 'top':
  1104. return y + height;
  1105. case 'hanging':
  1106. return y + height - descent;
  1107. case 'middle':
  1108. return y + height / 2 - descent;
  1109. case 'ideographic':
  1110. // TODO not implemented
  1111. return y;
  1112. case 'alphabetic':
  1113. default:
  1114. return y;
  1115. }
  1116. }
  1117. }
  1118. ;
  1119. var c2d = jsPDFAPI.context2d;
  1120. // accessor methods
  1121. Object.defineProperty(c2d, 'fillStyle', {
  1122. set: function (value) {
  1123. this.setFillStyle(value);
  1124. },
  1125. get: function () {
  1126. return this.ctx.fillStyle;
  1127. }
  1128. });
  1129. Object.defineProperty(c2d, 'strokeStyle', {
  1130. set: function (value) {
  1131. this.setStrokeStyle(value);
  1132. },
  1133. get: function () {
  1134. return this.ctx.strokeStyle;
  1135. }
  1136. });
  1137. Object.defineProperty(c2d, 'lineWidth', {
  1138. set: function (value) {
  1139. this.setLineWidth(value);
  1140. },
  1141. get: function () {
  1142. return this.ctx.lineWidth;
  1143. }
  1144. });
  1145. Object.defineProperty(c2d, 'lineCap', {
  1146. set: function (val) {
  1147. this.setLineCap(val);
  1148. },
  1149. get: function () {
  1150. return this.ctx.lineCap;
  1151. }
  1152. });
  1153. Object.defineProperty(c2d, 'lineJoin', {
  1154. set: function (val) {
  1155. this.setLineJoin(val);
  1156. },
  1157. get: function () {
  1158. return this.ctx.lineJoin;
  1159. }
  1160. });
  1161. Object.defineProperty(c2d, 'miterLimit', {
  1162. set: function (val) {
  1163. this.ctx.miterLimit = val;
  1164. },
  1165. get: function () {
  1166. return this.ctx.miterLimit;
  1167. }
  1168. });
  1169. Object.defineProperty(c2d, 'textBaseline', {
  1170. set: function (value) {
  1171. this.setTextBaseline(value);
  1172. },
  1173. get: function () {
  1174. return this.getTextBaseline();
  1175. }
  1176. });
  1177. Object.defineProperty(c2d, 'textAlign', {
  1178. set: function (value) {
  1179. this.setTextAlign(value);
  1180. },
  1181. get: function () {
  1182. return this.getTextAlign();
  1183. }
  1184. });
  1185. Object.defineProperty(c2d, 'font', {
  1186. set: function (value) {
  1187. this.setFont(value);
  1188. },
  1189. get: function () {
  1190. return this.ctx.font;
  1191. }
  1192. });
  1193. Object.defineProperty(c2d, 'globalCompositeOperation', {
  1194. set: function (value) {
  1195. this.ctx.globalCompositeOperation = value;
  1196. },
  1197. get: function () {
  1198. return this.ctx.globalCompositeOperation;
  1199. }
  1200. });
  1201. Object.defineProperty(c2d, 'globalAlpha', {
  1202. set: function (value) {
  1203. this.ctx.globalAlpha = value;
  1204. },
  1205. get: function () {
  1206. return this.ctx.globalAlpha;
  1207. }
  1208. });
  1209. // Not HTML API
  1210. Object.defineProperty(c2d, 'ignoreClearRect', {
  1211. set: function (value) {
  1212. this.ctx.ignoreClearRect = value;
  1213. },
  1214. get: function () {
  1215. return this.ctx.ignoreClearRect;
  1216. }
  1217. });
  1218. // End Not HTML API
  1219. c2d.internal = {};
  1220. c2d.internal.rxRgb = /rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/;
  1221. c2d.internal.rxRgba = /rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d\.]+)\s*\)/;
  1222. c2d.internal.rxTransparent = /transparent|rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*0+\s*\)/;
  1223. // http://hansmuller-flex.blogspot.com/2011/10/more-about-approximating-circular-arcs.html
  1224. c2d.internal.arc = function (c2d, xc, yc, r, a1, a2, anticlockwise, style) {
  1225. var includeMove = true;
  1226. var k = this.pdf.internal.scaleFactor;
  1227. var pageHeight = this.pdf.internal.pageSize.height;
  1228. var f2 = this.pdf.internal.f2;
  1229. var a1r = a1 * (Math.PI / 180);
  1230. var a2r = a2 * (Math.PI / 180);
  1231. var curves = this.createArc(r, a1r, a2r, anticlockwise);
  1232. var pathData = null;
  1233. for (var i = 0; i &lt; curves.length; i++) {
  1234. var curve = curves[i];
  1235. if (includeMove &amp;&amp; i == 0) {
  1236. this.pdf.internal.out([
  1237. f2((curve.x1 + xc) * k), f2((pageHeight - (curve.y1 + yc)) * k), 'm', f2((curve.x2 + xc) * k), f2((pageHeight - (curve.y2 + yc)) * k), f2((curve.x3 + xc) * k), f2((pageHeight - (curve.y3 + yc)) * k), f2((curve.x4 + xc) * k), f2((pageHeight - (curve.y4 + yc)) * k), 'c'
  1238. ].join(' '));
  1239. } else {
  1240. this.pdf.internal.out([
  1241. f2((curve.x2 + xc) * k), f2((pageHeight - (curve.y2 + yc)) * k), f2((curve.x3 + xc) * k), f2((pageHeight - (curve.y3 + yc)) * k), f2((curve.x4 + xc) * k), f2((pageHeight - (curve.y4 + yc)) * k), 'c'
  1242. ].join(' '));
  1243. }
  1244. //c2d._lastPoint = {x: curve.x1 + xc, y: curve.y1 + yc};
  1245. c2d._lastPoint = {x: xc, y: yc};
  1246. // f2((curve.x1 + xc) * k), f2((pageHeight - (curve.y1 + yc)) * k), 'm', f2((curve.x2 + xc) * k), f2((pageHeight - (curve.y2 + yc)) * k), f2((curve.x3 + xc) * k), f2((pageHeight - (curve.y3 + yc)) * k), f2((curve.x4 + xc) * k), f2((pageHeight - (curve.y4 + yc)) * k), 'c'
  1247. }
  1248. if (style !== null) {
  1249. this.pdf.internal.out(this.pdf.internal.getStyle(style));
  1250. }
  1251. };
  1252. /**
  1253. *
  1254. * @param x Edge point X
  1255. * @param y Edge point Y
  1256. * @param r Radius
  1257. * @param a1 start angle
  1258. * @param a2 end angle
  1259. * @param anticlockwise
  1260. * @param style
  1261. * @param isClip
  1262. */
  1263. c2d.internal.arc2 = function (c2d, x, y, r, a1, a2, anticlockwise, style, isClip) {
  1264. // we need to convert from cartesian to polar here methinks.
  1265. var centerX = x;// + r;
  1266. var centerY = y;
  1267. if (false) {
  1268. var phi = (a2 - a1);
  1269. var start = {
  1270. x: r,
  1271. y: 0
  1272. };
  1273. var pt1 = {
  1274. x: r,
  1275. y: r * 4 / 3 * Math.tan(phi / 4)
  1276. };
  1277. var pt2 = {
  1278. x: r * ( Math.cos(phi) + 4 / 3 * Math.tan(phi / 4) * Math.sin(phi) ),
  1279. y: r * ( Math.sin(phi) - 4 / 3 * Math.tan(phi / 4) * Math.cos(phi) )
  1280. };
  1281. var end = {
  1282. x: r * Math.cos(phi),
  1283. y: r * Math.sin(phi)
  1284. };
  1285. var matrix = [Math.cos(a1), Math.sin(a1), -Math.sin(a1), Math.cos(a1), x, y];
  1286. start = c2d._matrix_map_point_obj(matrix, start);
  1287. pt1 = c2d._matrix_map_point_obj(matrix, pt1);
  1288. pt2 = c2d._matrix_map_point_obj(matrix, pt2);
  1289. end = c2d._matrix_map_point_obj(matrix, end);
  1290. var k = this.pdf.internal.scaleFactor;
  1291. var pageHeight = this.pdf.internal.pageSize.height;
  1292. var f2 = this.pdf.internal.f2;
  1293. this.pdf.internal.out([
  1294. f2((start.x) * k), f2((pageHeight - (start.y)) * k), 'm', f2((pt1.x) * k), f2((pageHeight - (pt1.y)) * k), f2((pt2.x) * k), f2((pageHeight - (pt2.y)) * k), f2((end.x) * k), f2((pageHeight - (end.y)) * k), 'c'
  1295. ].join(' '));
  1296. //this.pdf.internal.out('f');
  1297. c2d._lastPoint = end;
  1298. return;
  1299. }
  1300. if (!isClip) {
  1301. this.arc(c2d, centerX, centerY, r, a1, a2, anticlockwise, style);
  1302. } else {
  1303. this.arc(c2d, centerX, centerY, r, a1, a2, anticlockwise, null);
  1304. this.pdf.clip_fixed();
  1305. }
  1306. };
  1307. c2d.internal.move2 = function (c2d, x, y) {
  1308. var k = this.pdf.internal.scaleFactor;
  1309. var pageHeight = this.pdf.internal.pageSize.height;
  1310. var f2 = this.pdf.internal.f2;
  1311. this.pdf.internal.out([
  1312. f2((x) * k), f2((pageHeight - (y)) * k), 'm'
  1313. ].join(' '));
  1314. c2d._lastPoint = {x: x, y: y};
  1315. };
  1316. c2d.internal.line2 = function (c2d, dx, dy) {
  1317. var k = this.pdf.internal.scaleFactor;
  1318. var pageHeight = this.pdf.internal.pageSize.height;
  1319. var f2 = this.pdf.internal.f2;
  1320. //var pt = {x: c2d._lastPoint.x + dx, y: c2d._lastPoint.y + dy};
  1321. var pt = {x: dx, y: dy};
  1322. this.pdf.internal.out([
  1323. f2((pt.x) * k), f2((pageHeight - (pt.y)) * k), 'l'
  1324. ].join(' '));
  1325. //this.pdf.internal.out('f');
  1326. c2d._lastPoint = pt;
  1327. };
  1328. /**
  1329. * Return a array of objects that represent bezier curves which approximate the circular arc centered at the origin, from startAngle to endAngle (radians) with the specified radius.
  1330. *
  1331. * Each bezier curve is an object with four points, where x1,y1 and x4,y4 are the arc's end points and x2,y2 and x3,y3 are the cubic bezier's control points.
  1332. */
  1333. c2d.internal.createArc = function (radius, startAngle, endAngle, anticlockwise) {
  1334. var EPSILON = 0.00001; // Roughly 1/1000th of a degree, see below
  1335. // normalize startAngle, endAngle to [-2PI, 2PI]
  1336. var twoPI = Math.PI * 2;
  1337. var startAngleN = startAngle;
  1338. if (startAngleN &lt; twoPI || startAngleN > twoPI) {
  1339. startAngleN = startAngleN % twoPI;
  1340. }
  1341. var endAngleN = endAngle;
  1342. if (endAngleN &lt; twoPI || endAngleN > twoPI) {
  1343. endAngleN = endAngleN % twoPI;
  1344. }
  1345. // Compute the sequence of arc curves, up to PI/2 at a time.
  1346. // Total arc angle is less than 2PI.
  1347. var curves = [];
  1348. var piOverTwo = Math.PI / 2.0;
  1349. // var sgn = (startAngle &lt; endAngle) ? +1 : -1; // clockwise or counterclockwise
  1350. var sgn = anticlockwise ? -1 : +1;
  1351. var a1 = startAngle;
  1352. for (var totalAngle = Math.min(twoPI, Math.abs(endAngleN - startAngleN)); totalAngle > EPSILON;) {
  1353. var a2 = a1 + sgn * Math.min(totalAngle, piOverTwo);
  1354. curves.push(this.createSmallArc(radius, a1, a2));
  1355. totalAngle -= Math.abs(a2 - a1);
  1356. a1 = a2;
  1357. }
  1358. return curves;
  1359. };
  1360. /**
  1361. * Cubic bezier approximation of a circular arc centered at the origin, from (radians) a1 to a2, where a2-a1 &lt; pi/2. The arc's radius is r.
  1362. *
  1363. * Returns an object with four points, where x1,y1 and x4,y4 are the arc's end points and x2,y2 and x3,y3 are the cubic bezier's control points.
  1364. *
  1365. * This algorithm is based on the approach described in: A. Riškus, "Approximation of a Cubic Bezier Curve by Circular Arcs and Vice Versa," Information Technology and Control, 35(4), 2006 pp. 371-378.
  1366. */
  1367. c2d.internal.createSmallArc = function (r, a1, a2) {
  1368. // Compute all four points for an arc that subtends the same total angle
  1369. // but is centered on the X-axis
  1370. var a = (a2 - a1) / 2.0;
  1371. var x4 = r * Math.cos(a);
  1372. var y4 = r * Math.sin(a);
  1373. var x1 = x4;
  1374. var y1 = -y4;
  1375. var q1 = x1 * x1 + y1 * y1;
  1376. var q2 = q1 + x1 * x4 + y1 * y4;
  1377. var k2 = 4 / 3 * (Math.sqrt(2 * q1 * q2) - q2) / (x1 * y4 - y1 * x4);
  1378. var x2 = x1 - k2 * y1;
  1379. var y2 = y1 + k2 * x1;
  1380. var x3 = x2;
  1381. var y3 = -y2;
  1382. // Find the arc points' actual locations by computing x1,y1 and x4,y4
  1383. // and rotating the control points by a + a1
  1384. var ar = a + a1;
  1385. var cos_ar = Math.cos(ar);
  1386. var sin_ar = Math.sin(ar);
  1387. return {
  1388. x1: r * Math.cos(a1),
  1389. y1: r * Math.sin(a1),
  1390. x2: x2 * cos_ar - y2 * sin_ar,
  1391. y2: x2 * sin_ar + y2 * cos_ar,
  1392. x3: x3 * cos_ar - y3 * sin_ar,
  1393. y3: x3 * sin_ar + y3 * cos_ar,
  1394. x4: r * Math.cos(a2),
  1395. y4: r * Math.sin(a2)
  1396. };
  1397. }
  1398. function context() {
  1399. this._isStrokeTransparent = false;
  1400. this._strokeOpacity = 1;
  1401. this.strokeStyle = '#000000';
  1402. this.fillStyle = '#000000';
  1403. this._isFillTransparent = false;
  1404. this._fillOpacity = 1;
  1405. this.font = "12pt times";
  1406. this.textBaseline = 'alphabetic'; // top,bottom,middle,ideographic,alphabetic,hanging
  1407. this.textAlign = 'start';
  1408. this.lineWidth = 1;
  1409. this.lineJoin = 'miter'; // round, bevel, miter
  1410. this.lineCap = 'butt'; // butt, round, square
  1411. this._transform = [1, 0, 0, 1, 0, 0]; // sx, shy, shx, sy, tx, ty
  1412. this.globalCompositeOperation = 'normal';
  1413. this.globalAlpha = 1.0;
  1414. this._clip_path = [];
  1415. // TODO miter limit //default 10
  1416. // Not HTML API
  1417. this.ignoreClearRect = false;
  1418. this.copy = function (ctx) {
  1419. this._isStrokeTransparent = ctx._isStrokeTransparent;
  1420. this._strokeOpacity = ctx._strokeOpacity;
  1421. this.strokeStyle = ctx.strokeStyle;
  1422. this._isFillTransparent = ctx._isFillTransparent;
  1423. this._fillOpacity = ctx._fillOpacity;
  1424. this.fillStyle = ctx.fillStyle;
  1425. this.font = ctx.font;
  1426. this.lineWidth = ctx.lineWidth;
  1427. this.lineJoin = ctx.lineJoin;
  1428. this.lineCap = ctx.lineCap;
  1429. this.textBaseline = ctx.textBaseline;
  1430. this.textAlign = ctx.textAlign;
  1431. this._fontSize = ctx._fontSize;
  1432. this._transform = ctx._transform.slice(0);
  1433. this.globalCompositeOperation = ctx.globalCompositeOperation;
  1434. this.globalAlpha = ctx.globalAlpha;
  1435. this._clip_path = ctx._clip_path.slice(0); //TODO deep copy?
  1436. // Not HTML API
  1437. this.ignoreClearRect = ctx.ignoreClearRect;
  1438. };
  1439. }
  1440. return this;
  1441. })(jsPDF.API);
  1442. </code></pre>
  1443. </article>
  1444. </section>
  1445. </div>
  1446. <br class="clear">
  1447. <footer>
  1448. 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.
  1449. </footer>
  1450. <script>prettyPrint();</script>
  1451. <script src="scripts/linenumber.js"></script>
  1452. </body>
  1453. </html>