Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion examples/js/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,43 @@ doc.setLineWidth(1);
doc.line(110, 20, 110, 60);

doc.setLineWidth(1.5);
doc.line(115, 20, 115, 60);
doc.line(115, 20, 115, 60);

doc.line(20, 20, 60, 20); // horizontal line

doc.setLineWidth(0.5);
doc.line(20, 25, 60, 25);

doc.setLineWidth(1);
doc.line(20, 30, 60, 30);

doc.setLineWidth(1.5);
doc.line(20, 35, 60, 35);

doc.setDrawColor(255,0,0); // draw red lines

doc.setLineWidth(0.1);
doc.line(100, 20, 100, 60); // vertical line

doc.setLineWidth(0.5);
doc.line(105, 20, 105, 60);

doc.setLineWidth(1);
doc.line(110, 20, 110, 60);

doc.setLineWidth(1.5);
doc.line(115, 20, 115, 60);


doc.text(10,110, "setLineDash");
doc.setLineWidth(0.1)
doc.setDrawColor(0,0,0);

doc.setLineDash([2.5])
doc.line(10,120,200,120)

doc.setLineDash([1, 1.5, 1, 1.5, 1, 1.5, 3, 2, 3, 2, 3, 2 ])
doc.line(10,125,200,125)

doc.setLineDash([1, 1.5, 1, 1.5, 1, 1.5, 3, 2, 3, 2, 3, 2 ], 7.5)
doc.line(10,130,200,130)
29 changes: 29 additions & 0 deletions src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2738,6 +2738,35 @@ 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.
* @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) {

if (Array.isArray(dashArray)) {
dashArray = dashArray.map(x => (x * k).toFixed(2)).join(' ');
} else {
dashArray = '';
}

if (typeof dashPhase === "number") {
dashPhase = (dashPhase * k).toFixed(2);
}

dashPhase = dashPhase || 0;
out('[' + dashArray + '] ' + dashPhase + ' d');
return this;
};

var lineHeightFactor;

var getLineHeight = API.__private__.getLineHeight = API.getLineHeight = function () {
Expand Down
23 changes: 22 additions & 1 deletion tests/init/jspdf.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,28 @@ describe('jsPDF unit tests', () => {

expect(writeArray).toEqual(['1687.41 w']);
});


it('jsPDF private function setLineDash', () => {
const doc = jsPDF()
var writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);
doc.__private__.setLineDash('');

expect(writeArray).toEqual(['[] 0 d']);

var writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);
doc.__private__.setLineDash('1 1', 1);

expect(writeArray).toEqual(['[1] 2.83 d']);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is again wrong. Must be

expect(writeArray).toEqual(['[] 2.83 d']);


var writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);
doc.__private__.setLineDash([1,1], 1);

expect(writeArray).toEqual(['[2.83 2.83] 2.83 d']);
});

it('jsPDF private function getLineHeight', () => {
const doc = jsPDF()

Expand Down