diff --git a/src/jspdf.js b/src/jspdf.js index 8f1ac5337..9aa613a39 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -2738,6 +2738,39 @@ var jsPDF = (function (global) { return this; }; + /** + * Sets the dash pattern for upcoming lines. + * + * To reset the settings simply call the method without any parameters. + * @param {array} dashArray The pattern of the line, expects numbers. + * @param {number} dashPhase The phase at which the dash pattern starts. + * @function + * @instance + * @returns {jsPDF} + * @memberOf jsPDF + * @name setLineDash + */ + var setLineDash = API.__private__.setLineDash = jsPDF.API.setLineDash = function (dashArray, dashPhase) { + dashArray = dashArray || []; + dashPhase = dashPhase || 0; + + if (isNaN(dashPhase) || !Array.isArray(dashArray)) { + throw new Error('Invalid arguments passed to jsPDF.setLineDash'); + } + + dashArray = dashArray.map(function (x) {return (x * k).toFixed(3)}).join(' '); + dashPhase = parseFloat((dashPhase * k).toFixed(3)); + + out('[' + dashArray + '] ' + dashPhase + ' d'); + return this; + }; + + var lineHeightFactor; + + var getLineHeight = API.__private__.getLineHeight = API.getLineHeight = function () { + return activeFontSize * lineHeightFactor; + }; + var lineHeightFactor; var getLineHeight = API.__private__.getLineHeight = API.getLineHeight = function () { diff --git a/tests/init/jspdf.unit.spec.js b/tests/init/jspdf.unit.spec.js index a9bc33167..cf4721dd7 100644 --- a/tests/init/jspdf.unit.spec.js +++ b/tests/init/jspdf.unit.spec.js @@ -442,7 +442,35 @@ describe('jsPDF unit tests', () => { expect(writeArray).toEqual(['1687.41 w']); }); - + + it('jsPDF private function setLineDash', () => { + const doc = jsPDF() + var writeArray = []; + doc.__private__.setCustomOutputDestination(writeArray); + + expect(function () {doc.__private__.setLineDash('');} ).not.toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash')); + + var writeArray = []; + doc.__private__.setCustomOutputDestination(writeArray); + expect(function () {doc.__private__.setLineDash();} ).not.toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash')); + + expect(writeArray).toEqual(['[] 0 d']); + + var writeArray = []; + doc.__private__.setCustomOutputDestination(writeArray); + expect(function () {doc.__private__.setLineDash('1 1', '1');} ).toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash')); + + var writeArray = []; + doc.__private__.setCustomOutputDestination(writeArray); + expect(function () {doc.__private__.setLineDash('1 1', 1);} ).toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash')); + + var writeArray = []; + doc.__private__.setCustomOutputDestination(writeArray); + expect(function () {doc.__private__.setLineDash([1,1], 1)} ).not.toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash')); + + expect(writeArray).toEqual(['[2.835 2.835] 2.835 d']); + }); + it('jsPDF private function getLineHeight', () => { const doc = jsPDF()