From 1680e7ac6d46015ed26bdaed5452b332d39950fc Mon Sep 17 00:00:00 2001 From: Owen Leong Date: Sun, 10 Oct 2021 21:38:00 +0800 Subject: [PATCH 1/2] Add horizontal scale option for text --- src/jspdf.js | 8 +++++++- test/reference/text-horizontal-scaling.pdf | Bin 0 -> 3265 bytes test/specs/text.spec.js | 9 +++++++++ types/index.d.ts | 1 + 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/reference/text-horizontal-scaling.pdf diff --git a/src/jspdf.js b/src/jspdf.js index 4a973423e..817c793e0 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -3400,6 +3400,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=100] - Horizontal scaling as a percentage of the normal 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. @@ -3436,7 +3437,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 @@ -3702,6 +3703,11 @@ function jsPDF(options) { this.setCharSpace(this.getCharSpace() || 0); } + horizontalScale = options.horizontalScale; + if (typeof horizontalScale !== "undefined") { + xtra += hpf(horizontalScale) + " Tz\n"; + } + //lang var lang = options.lang; diff --git a/test/reference/text-horizontal-scaling.pdf b/test/reference/text-horizontal-scaling.pdf new file mode 100644 index 0000000000000000000000000000000000000000..18cf105b2dbc1d0a19662a5454b4671fb7d7cd02 GIT binary patch literal 3265 zcmcImU2fVy5PsiN%uCcnRcx=#`lmvX0)z%N6%kl!)hG`J7UIZwQ+sKXoT(@1W!j^3 z#+b%2Ng-7*$R3Ypc4p^qzrFKL_mCS7+xz+J$8Y9<1!{E7noTpjyOCg?M>EOrp2<`L z$FBos4rHzt*;wXaFBu=@B#v6@7A`Dgh(H*&4}OCiesIamwn{Z>=g=smkB*p3CuD@) zm&TZRCexX|0$cbD1#+FqXwED{c<`NB@Se3oW_CGn7s4syzR`g23_r}6unY+Ane7{n z0G~UCz)uslejT=#GXPiSMP%3zd1 zqa3VdC}-w}c#^{f=yd%u1Z)x_uR8;MkbQsC==7s2F#*H-6Ys$>jAt@Lu3S zf0V>u7g+bN9R~jePEZ(j=18#7kv6t?EVI{-0jC+UYWwAosTX2tdqpH&=!aiKm(L07m9gCqzA=h zA<~DU!HEoj7l$&ED9M)?2x-JJD_bawxMsN?3iFjc6fS}`po7%>+#g-bu?BP$UmwnO zUY+H*@yTK=GuXe*Y3r7OzXQZzh^KU{(oUo$?04Sr!1uU~0H(O!TGkuPg*Qazl z!X{8NLW48JekB;}-^lcMOlz;u2OkS<79JlhuN;bWdpr3DHP+YjJuHI9 z>+vV-a_+3l*)FEGrt1WlzZ#ErU+1~*S`4_iz7HNyJv?5|wgfgujmHru*LjU#&1VGU z6*-+nae|G literal 0 HcmV?d00001 diff --git a/test/specs/text.spec.js b/test/specs/text.spec.js index f5044db01..cd55d6b93 100644 --- a/test/specs/text.spec.js +++ b/test/specs/text.spec.js @@ -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: 50 }); + doc.text("hello", 10, 20, { horizontalScale: 75 }); + doc.text("hello", 10, 30, { horizontalScale: 100 }); + doc.text("hello", 10, 40, { horizontalScale: 150 }); + comparePdf(doc.output(), "text-horizontal-scaling.pdf", "text"); + }); + it("should respect autoencode and noBOM flags", () => { const doc = jsPDF({ floatPrecision: 2 }); diff --git a/types/index.d.ts b/types/index.d.ts index 8b8c6f70b..bda690638 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -549,6 +549,7 @@ declare module "jspdf" { }; rotationDirection?: 0 | 1; charSpace?: number; + horizontalScale?: number; lineHeightFactor?: number; maxWidth?: number; renderingMode?: From adcd89f8b570fd8e00d2fa279b4716c945abf582 Mon Sep 17 00:00:00 2001 From: Owen Leong Date: Tue, 12 Oct 2021 17:22:01 +0800 Subject: [PATCH 2/2] Change from using percentage to just a multiple of normal size --- src/jspdf.js | 4 ++-- test/specs/text.spec.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 817c793e0..ca331d5f6 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -3400,7 +3400,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=100] - Horizontal scaling as a percentage of the normal size. + * @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. @@ -3705,7 +3705,7 @@ function jsPDF(options) { horizontalScale = options.horizontalScale; if (typeof horizontalScale !== "undefined") { - xtra += hpf(horizontalScale) + " Tz\n"; + xtra += hpf(horizontalScale * 100) + " Tz\n"; } //lang diff --git a/test/specs/text.spec.js b/test/specs/text.spec.js index cd55d6b93..168eb31ff 100644 --- a/test/specs/text.spec.js +++ b/test/specs/text.spec.js @@ -213,10 +213,10 @@ break` it("should render horizontally scaled text", () => { const doc = jsPDF({ floatPrecision: 2 }); - doc.text("hello", 10, 10, { horizontalScale: 50 }); - doc.text("hello", 10, 20, { horizontalScale: 75 }); - doc.text("hello", 10, 30, { horizontalScale: 100 }); - doc.text("hello", 10, 40, { horizontalScale: 150 }); + 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"); });