Skip to content

Commit c83782f

Browse files
authored
Merge pull request #1042 from Flamenco/feature_scale_text_2
Feature scale text
2 parents 8c0ce0c + 6233c7a commit c83782f

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

HOTFIX_README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,18 @@ Filling paths
2525
In certain cases, closing a fill would result in a path resolving to an incorrect point.
2626
The was most likely fixed when we refactored matrix logic. Enabling this hotfix will ignore a most-likely unneeded workaround.
2727

28+
29+
##scale_text
30+
31+
###Applies To
32+
context2d plugin
33+
34+
### Affects
35+
Drawing and Filling Text when a scale transformation is active.
36+
37+
### Description
38+
jsPDF currently has no way to draw scaled text.
39+
This hotfix scales the current font size by the x-axis scale factor.
40+
2841
#Accepted Hotfixes
2942
There a currently no accepted hotfixes.

plugins/context2d.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,22 @@
299299
this.path = origPath;
300300
}
301301

302-
this.pdf.text(text, x, this._getBaseline(y), null, degs);
302+
var scale;
303+
if (this.pdf.hotfix && this.pdf.hotfix.scale_text) {
304+
scale = this._getTransform()[0];
305+
}
306+
else {
307+
scale = 1;
308+
}
309+
if (scale === 1) {
310+
this.pdf.text(text, x, this._getBaseline(y), null, degs);
311+
}
312+
else {
313+
var oldSize = this.pdf.internal.getFontSize();
314+
this.pdf.setFontSize(oldSize * scale);
315+
this.pdf.text(text, x, this._getBaseline(y), null, degs);
316+
this.pdf.setFontSize(oldSize);
317+
}
303318

304319
if (this.ctx._clip_path.length > 0) {
305320
lines.push('Q');
@@ -337,9 +352,26 @@
337352
this.path = origPath;
338353
}
339354

340-
this.pdf.text(text, x, this._getBaseline(y), {
355+
var scale;
356+
if (this.pdf.hotfix && this.pdf.hotfix.scale_text) {
357+
scale = this._getTransform()[0];
358+
}
359+
else {
360+
scale = 1;
361+
}
362+
if (scale === 1) {
363+
this.pdf.text(text, x, this._getBaseline(y), {
341364
stroke: true
342365
}, degs);
366+
}
367+
else {
368+
var oldSize = this.pdf.internal.getFontSize();
369+
this.pdf.setFontSize(oldSize * scale);
370+
this.pdf.text(text, x, this._getBaseline(y), {
371+
stroke: true
372+
}, degs);
373+
this.pdf.setFontSize(oldSize);
374+
}
343375

344376
if (this.ctx._clip_path.length > 0) {
345377
lines.push('Q');
@@ -1219,6 +1251,8 @@
12191251
getWidth: function () {
12201252
var fontSize = pdf.internal.getFontSize();
12211253
var txtWidth = pdf.getStringUnitWidth(text) * fontSize / pdf.internal.scaleFactor;
1254+
// Convert points to pixels
1255+
txtWidth *= 1.3333;
12221256
return txtWidth;
12231257
},
12241258

0 commit comments

Comments
 (0)