Skip to content

Commit 2d9a919

Browse files
Implement justifying for unicode fonts (#3285)
Co-authored-by: Lukas Holländer <[email protected]>
1 parent 7315ccb commit 2d9a919

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

src/jspdf.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,23 +3806,23 @@ function jsPDF(options) {
38063806
flags = Object.assign({ autoencode: true, noBOM: true }, options.flags);
38073807

38083808
var wordSpacingPerLine = [];
3809-
3809+
var findWidth = function(v) {
3810+
return (
3811+
(scope.getStringUnitWidth(v, {
3812+
font: activeFont,
3813+
charSpace: charSpace,
3814+
fontSize: activeFontSize,
3815+
doKerning: false
3816+
}) *
3817+
activeFontSize) /
3818+
scaleFactor
3819+
);
3820+
};
38103821
if (Object.prototype.toString.call(text) === "[object Array]") {
38113822
da = transformTextToSpecialArray(text);
38123823
var newY;
38133824
if (align !== "left") {
3814-
lineWidths = da.map(function(v) {
3815-
return (
3816-
(scope.getStringUnitWidth(v, {
3817-
font: activeFont,
3818-
charSpace: charSpace,
3819-
fontSize: activeFontSize,
3820-
doKerning: false
3821-
}) *
3822-
activeFontSize) /
3823-
scaleFactor
3824-
);
3825-
});
3825+
lineWidths = da.map(findWidth);
38263826
}
38273827
//The first line uses the "main" Td setting,
38283828
//and the subsequent lines are offset by the
@@ -3869,11 +3869,41 @@ function jsPDF(options) {
38693869
for (var h = 0; h < len; h++) {
38703870
text.push(da[h]);
38713871
}
3872+
} else if (align === "justify" && activeFont.encoding === "Identity-H") {
3873+
// when using unicode fonts, wordSpacePerLine does not apply
3874+
text = [];
3875+
len = da.length;
3876+
maxWidth = maxWidth !== 0 ? maxWidth : pageWidth;
3877+
let backToStartX = 0;
3878+
for (var l = 0; l < len; l++) {
3879+
newY = l === 0 ? getVerticalCoordinate(y) : -leading;
3880+
newX = l === 0 ? getHorizontalCoordinate(x) : backToStartX;
3881+
if (l < len - 1) {
3882+
let spacing = scale(
3883+
(maxWidth - lineWidths[l]) / (da[l].split(" ").length - 1)
3884+
);
3885+
let words = da[l].split(" ");
3886+
text.push([words[0] + " ", newX, newY]);
3887+
backToStartX = 0; // distance to reset back to the left
3888+
for (let i = 1; i < words.length; i++) {
3889+
let shiftAmount =
3890+
(findWidth(words[i - 1] + " " + words[i]) -
3891+
findWidth(words[i])) *
3892+
scaleFactor +
3893+
spacing;
3894+
if (i == words.length - 1) text.push([words[i], shiftAmount, 0]);
3895+
else text.push([words[i] + " ", shiftAmount, 0]);
3896+
backToStartX -= shiftAmount;
3897+
}
3898+
} else {
3899+
text.push([da[l], newX, newY]);
3900+
}
3901+
}
3902+
text.push(["", backToStartX, 0]);
38723903
} else if (align === "justify") {
38733904
text = [];
38743905
len = da.length;
38753906
maxWidth = maxWidth !== 0 ? maxWidth : pageWidth;
3876-
38773907
for (var l = 0; l < len; l++) {
38783908
newY = l === 0 ? getVerticalCoordinate(y) : -leading;
38793909
newX = l === 0 ? getHorizontalCoordinate(x) : 0;
46.4 KB
Binary file not shown.

test/specs/text.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ break`
175175
comparePdf(doc.output(), "alignment.pdf", "text");
176176
});
177177

178+
it("should justify custom font", () => {
179+
const doc = jsPDF({ floatPrecision: 2 });
180+
var PTSans;
181+
if (typeof global === "object" && global.isNode === true) {
182+
PTSans = doc.loadFile("./test/reference/PTSans.ttf");
183+
} else {
184+
PTSans = doc.loadFile("base/test/reference/PTSans.ttf");
185+
}
186+
doc.addFileToVFS("PTSans.ttf", PTSans);
187+
doc.addFont("PTSans.ttf", "PTSans", "normal");
188+
doc.setFont("PTSans");
189+
doc.setFontSize(10);
190+
doc.text("А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! А ну чики брики и в дамки! ", 10, 10, {
191+
align: "justify",
192+
maxWidth: 100,
193+
});
194+
comparePdf(doc.output(), "justify-custom-font.pdf", "text");
195+
});
196+
178197
it("should throw an error if not a string", () => {
179198
expect(() => {
180199
const doc = jsPDF({ floatPrecision: 2 });

0 commit comments

Comments
 (0)