Skip to content

Commit 52afcab

Browse files
authored
add method setLineDash (#2158)
* Update jspdf.js * Update jspdf.unit.spec.js * Update jspdf.unit.spec.js * Update jspdf.js * Update jspdf.unit.spec.js * Update jspdf.js * Update jspdf.js * Update jspdf.unit.spec.js * Update jspdf.unit.spec.js * Update jspdf.js
1 parent d3e1dba commit 52afcab

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/jspdf.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,39 @@ var jsPDF = (function (global) {
27642764
return this;
27652765
};
27662766

2767+
/**
2768+
* Sets the dash pattern for upcoming lines.
2769+
*
2770+
* To reset the settings simply call the method without any parameters.
2771+
* @param {array} dashArray The pattern of the line, expects numbers.
2772+
* @param {number} dashPhase The phase at which the dash pattern starts.
2773+
* @function
2774+
* @instance
2775+
* @returns {jsPDF}
2776+
* @memberOf jsPDF
2777+
* @name setLineDash
2778+
*/
2779+
var setLineDash = API.__private__.setLineDash = jsPDF.API.setLineDash = function (dashArray, dashPhase) {
2780+
dashArray = dashArray || [];
2781+
dashPhase = dashPhase || 0;
2782+
2783+
if (isNaN(dashPhase) || !Array.isArray(dashArray)) {
2784+
throw new Error('Invalid arguments passed to jsPDF.setLineDash');
2785+
}
2786+
2787+
dashArray = dashArray.map(function (x) {return (x * k).toFixed(3)}).join(' ');
2788+
dashPhase = parseFloat((dashPhase * k).toFixed(3));
2789+
2790+
out('[' + dashArray + '] ' + dashPhase + ' d');
2791+
return this;
2792+
};
2793+
2794+
var lineHeightFactor;
2795+
2796+
var getLineHeight = API.__private__.getLineHeight = API.getLineHeight = function () {
2797+
return activeFontSize * lineHeightFactor;
2798+
};
2799+
27672800
var lineHeightFactor;
27682801

27692802
var getLineHeight = API.__private__.getLineHeight = API.getLineHeight = function () {

tests/init/jspdf.unit.spec.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,35 @@ describe('jsPDF unit tests', () => {
429429

430430
expect(writeArray).toEqual(['1687.41 w']);
431431
});
432-
432+
433+
it('jsPDF private function setLineDash', () => {
434+
const doc = jsPDF()
435+
var writeArray = [];
436+
doc.__private__.setCustomOutputDestination(writeArray);
437+
438+
expect(function () {doc.__private__.setLineDash('');} ).not.toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash'));
439+
440+
var writeArray = [];
441+
doc.__private__.setCustomOutputDestination(writeArray);
442+
expect(function () {doc.__private__.setLineDash();} ).not.toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash'));
443+
444+
expect(writeArray).toEqual(['[] 0 d']);
445+
446+
var writeArray = [];
447+
doc.__private__.setCustomOutputDestination(writeArray);
448+
expect(function () {doc.__private__.setLineDash('1 1', '1');} ).toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash'));
449+
450+
var writeArray = [];
451+
doc.__private__.setCustomOutputDestination(writeArray);
452+
expect(function () {doc.__private__.setLineDash('1 1', 1);} ).toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash'));
453+
454+
var writeArray = [];
455+
doc.__private__.setCustomOutputDestination(writeArray);
456+
expect(function () {doc.__private__.setLineDash([1,1], 1)} ).not.toThrow(new Error('Invalid arguments passed to jsPDF.setLineDash'));
457+
458+
expect(writeArray).toEqual(['[2.835 2.835] 2.835 d']);
459+
});
460+
433461
it('jsPDF private function getLineHeight', () => {
434462
const doc = jsPDF()
435463

0 commit comments

Comments
 (0)