Skip to content

Commit

Permalink
Add horizontal scale option for text (#3294)
Browse files Browse the repository at this point in the history
  • Loading branch information
owenl131 committed Oct 13, 2021
1 parent 57120de commit babeb35
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3404,6 +3404,7 @@ function jsPDF(options) {
* @param {number|Matrix} [options.angle=0] - Rotate the text clockwise or counterclockwise. Expects the angle in degree.
* @param {number} [options.rotationDirection=1] - Direction of the rotation. 0 = clockwise, 1 = counterclockwise.
* @param {number} [options.charSpace=0] - The space between each letter.
* @param {number} [options.horizontalScale=1] - Horizontal scale of the text as a factor of the regular size.
* @param {number} [options.lineHeightFactor=1.15] - The lineheight of each line.
* @param {Object} [options.flags] - Flags for to8bitStream.
* @param {boolean} [options.flags.noBOM=true] - Don't add BOM to Unicode-text.
Expand Down Expand Up @@ -3440,7 +3441,7 @@ function jsPDF(options) {
*/
options = options || {};
var scope = options.scope || this;
var payload, da, angle, align, charSpace, maxWidth, flags;
var payload, da, angle, align, charSpace, maxWidth, flags, horizontalScale;

// Pre-August-2012 the order of arguments was function(x, y, text, flags)
// in effort to make all calls have similar signature like
Expand Down Expand Up @@ -3707,6 +3708,11 @@ function jsPDF(options) {
this.setCharSpace(this.getCharSpace() || 0);
}

horizontalScale = options.horizontalScale;
if (typeof horizontalScale !== "undefined") {
xtra += hpf(horizontalScale * 100) + " Tz\n";
}

//lang

var lang = options.lang;
Expand Down
Binary file added test/reference/text-horizontal-scaling.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions test/specs/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ break`
comparePdf(doc.output(), "letter-spacing.pdf", "text");
});

it("should render horizontally scaled text", () => {
const doc = jsPDF({ floatPrecision: 2 });
doc.text("hello", 10, 10, { horizontalScale: 0.5 });
doc.text("hello", 10, 20, { horizontalScale: 0.75 });
doc.text("hello", 10, 30, { horizontalScale: 1 });
doc.text("hello", 10, 40, { horizontalScale: 1.5 });
comparePdf(doc.output(), "text-horizontal-scaling.pdf", "text");
});

it("should respect autoencode and noBOM flags", () => {
const doc = jsPDF({ floatPrecision: 2 });

Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ declare module "jspdf" {
};
rotationDirection?: 0 | 1;
charSpace?: number;
horizontalScale?: number;
lineHeightFactor?: number;
maxWidth?: number;
renderingMode?:
Expand Down

0 comments on commit babeb35

Please sign in to comment.