Skip to content

Commit f15a154

Browse files
committed
text wrap improvements - Handle new line chars
1 parent f4cf96c commit f15a154

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

dist/node-i2d.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* node-i2djs v1.1.0
2+
* node-i2djs v1.1.1
33
* (c) 2022 Narayana swamy ([email protected])
44
* @license BSD-3-Clause
55
*/
@@ -5092,25 +5092,35 @@ RenderText.prototype.fitWidth = function () {
50925092
this.ctx.font = this.style.font;
50935093
}
50945094
var width = this.attr.width;
5095-
var textLits = this.attr.text.split(" ");
5095+
var textListByLine = this.attr.text.split("\n");
50965096
var textSubStrs = [];
50975097
var strLit = "";
50985098
var i = 0;
5099-
while(i < textLits.length) {
5099+
var textList = textListByLine.reduce(function (p, c) {
5100+
p.push("\n");
5101+
p = p.concat(c.split(" "));
5102+
return p;
5103+
}, []);
5104+
while(i < textList.length) {
51005105
if (i !== 0) {
51015106
strLit += " ";
51025107
}
5103-
if (this.ctx.measureText(strLit + textLits[i]).width < width) {
5104-
strLit = strLit + textLits[i];
5105-
} else {
5108+
if (textList[i] === "\n") {
51065109
textSubStrs.push(strLit);
5107-
strLit = textLits[i];
5110+
strLit = " ";
5111+
} else {
5112+
if (this.ctx.measureText(strLit + textList[i]).width < width) {
5113+
strLit = strLit + textList[i];
5114+
} else {
5115+
textSubStrs.push(strLit);
5116+
strLit = textList[i];
5117+
}
51085118
}
51095119
i++;
51105120
}
51115121
textSubStrs.push(strLit);
51125122

5113-
this.textLits = textSubStrs;
5123+
this.textList = textSubStrs;
51145124
};
51155125

51165126
RenderText.prototype.text = function RTtext(value) {
@@ -5139,9 +5149,9 @@ RenderText.prototype.updateBBox = function RTupdateBBox() {
51395149
height = parseInt(this.style.font.replace(/[^\d.]/g, ""), 10) || 1;
51405150
self.textHeight = height + 3;
51415151
}
5142-
if (this.attr.width && this.textLits && this.textLits.length > 0) {
5152+
if (this.attr.width && this.textList && this.textList.length > 0) {
51435153
width = this.attr.width;
5144-
height = height * this.textLits.length;
5154+
height = height * this.textList.length;
51455155
} else {
51465156
width = this.ctx.measureText(this.attr.text).width;
51475157
}
@@ -5173,14 +5183,14 @@ RenderText.prototype.updateBBox = function RTupdateBBox() {
51735183

51745184
RenderText.prototype.execute = function RTexecute() {
51755185
if (this.attr.text !== undefined && this.attr.text !== null) {
5176-
if (this.textLits && this.textLits.length > 0) {
5177-
for (var i = 0; i < this.textLits.length; i++) {
5186+
if (this.textList && this.textList.length > 0) {
5187+
for (var i = 0; i < this.textList.length; i++) {
51785188
if (this.ctx.fillStyle !== "#000000") {
5179-
this.ctx.fillText(this.textLits[i], this.attr.x, this.attr.y + this.textHeight * (i + 1) );
5189+
this.ctx.fillText(this.textList[i], this.attr.x, this.attr.y + this.textHeight * (i + 1) );
51805190
}
51815191

51825192
if (this.ctx.strokeStyle !== "#000000") {
5183-
this.ctx.strokeText(this.textLits[i], this.attr.x, this.attr.y + this.textHeight * (i + 1));
5193+
this.ctx.strokeText(this.textList[i], this.attr.x, this.attr.y + this.textHeight * (i + 1));
51845194
}
51855195
}
51865196
} else {

examples/Example3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
y: 30,
110110
width: 600 * 0.5, // wraps text if width specified
111111
text:
112-
"Ohtani is a baseball savant doing what has never been seen in Major League Baseball history. The last player to both pitch and hit at an elite level was Babe Ruth, a century ago. But the Bambino stopped pitching relatively early in his career to concentrate on hitting. And no one ever called Ruth fast. Ohtani, the unanimous American League MVP, stole 26 bases last year.Ohtani is a baseball savant doing what has never been seen in Major League Baseball history. The last player to both pitch and hit at an elite level was Babe Ruth, a century ago. But the Bambino stopped pitching relatively early in his career to concentrate on hitting. And no one ever called Ruth fast. Ohtani, the unanimous American League MVP, stole 26 bases last year.Ohtani is a baseball savant doing what has never been seen in Major League Baseball history."
112+
"Ohtani is a baseball savant doing what has never been seen in Major League Baseball history. The last player to both pitch and hit at an elite level was Babe Ruth, a century ago. But the Bambino stopped pitching relatively early in his career to concentrate on hitting. And no one ever called Ruth fast. Ohtani, the unanimous American League MVP, stole 26 bases last year.\n\nOhtani is a baseball savant doing what has never been seen in Major League Baseball history. The last player to both pitch and hit at an elite level was Babe Ruth, a century ago. But the Bambino stopped pitching relatively early in his career to concentrate on hitting. And no one ever called Ruth fast. Ohtani, the unanimous American League MVP, stole 26 bases last year.Ohtani is a baseball savant doing what has never been seen in Major League Baseball history."
113113
},
114114
style: {
115115
strokeStyle: "#a3d0ff",

examples/example3.pdf

122 Bytes
Binary file not shown.

src/modules/canvas.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -539,25 +539,35 @@ RenderText.prototype.fitWidth = function () {
539539
this.ctx.font = this.style.font;
540540
}
541541
let width = this.attr.width;
542-
let textLits = this.attr.text.split(" ");
542+
let textListByLine = this.attr.text.split("\n");
543543
let textSubStrs = [];
544544
let strLit = "";
545545
let i = 0;
546-
while(i < textLits.length) {
546+
let textList = textListByLine.reduce(function (p, c) {
547+
p.push("\n");
548+
p = p.concat(c.split(" "));
549+
return p;
550+
}, [])
551+
while(i < textList.length) {
547552
if (i !== 0) {
548553
strLit += " "
549554
}
550-
if (this.ctx.measureText(strLit + textLits[i]).width < width) {
551-
strLit = strLit + textLits[i];
552-
} else {
555+
if (textList[i] === "\n") {
553556
textSubStrs.push(strLit);
554-
strLit = textLits[i];
557+
strLit = " ";
558+
} else {
559+
if (this.ctx.measureText(strLit + textList[i]).width < width) {
560+
strLit = strLit + textList[i];
561+
} else {
562+
textSubStrs.push(strLit);
563+
strLit = textList[i];
564+
}
555565
}
556566
i++;
557567
}
558568
textSubStrs.push(strLit);
559569

560-
this.textLits = textSubStrs;
570+
this.textList = textSubStrs;
561571
}
562572

563573
RenderText.prototype.text = function RTtext(value) {
@@ -579,9 +589,9 @@ RenderText.prototype.updateBBox = function RTupdateBBox() {
579589
height = parseInt(this.style.font.replace(/[^\d.]/g, ""), 10) || 1;
580590
self.textHeight = height + 3;
581591
}
582-
if (this.attr.width && this.textLits && this.textLits.length > 0) {
592+
if (this.attr.width && this.textList && this.textList.length > 0) {
583593
width = this.attr.width;
584-
height = height * this.textLits.length;
594+
height = height * this.textList.length;
585595
} else {
586596
width = this.ctx.measureText(this.attr.text).width;
587597
}
@@ -613,14 +623,14 @@ RenderText.prototype.updateBBox = function RTupdateBBox() {
613623

614624
RenderText.prototype.execute = function RTexecute() {
615625
if (this.attr.text !== undefined && this.attr.text !== null) {
616-
if (this.textLits && this.textLits.length > 0) {
617-
for (var i = 0; i < this.textLits.length; i++) {
626+
if (this.textList && this.textList.length > 0) {
627+
for (var i = 0; i < this.textList.length; i++) {
618628
if (this.ctx.fillStyle !== "#000000") {
619-
this.ctx.fillText(this.textLits[i], this.attr.x, this.attr.y + this.textHeight * (i + 1) );
629+
this.ctx.fillText(this.textList[i], this.attr.x, this.attr.y + this.textHeight * (i + 1) );
620630
}
621631

622632
if (this.ctx.strokeStyle !== "#000000") {
623-
this.ctx.strokeText(this.textLits[i], this.attr.x, this.attr.y + this.textHeight * (i + 1));
633+
this.ctx.strokeText(this.textList[i], this.attr.x, this.attr.y + this.textHeight * (i + 1));
624634
}
625635
}
626636
} else {

0 commit comments

Comments
 (0)