Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add horizontal scale option for text #3294

Merged
merged 4 commits into from
Oct 13, 2021
Merged
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
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