Skip to content

Commit 69ca295

Browse files
committed
DVF-2855-word-wrap
1 parent 60e490f commit 69ca295

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/vector/Text.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ acgraph.vector.Text.TextWrap = {
240240
/**
241241
Wrap by symbol.
242242
*/
243-
BY_LETTER: 'byLetter'
243+
BY_LETTER: 'byLetter',
244+
/**
245+
Wrap by word.
246+
*/
247+
BY_WORD: 'byWord'
244248
};
245249

246250

@@ -1078,6 +1082,24 @@ acgraph.vector.Text.prototype.cutTextSegment_ = function(text, style, a, b, segm
10781082
bounds.width = cutTextWidth;
10791083
acgraph.getRenderer().textBounds(cutText, resultStatus, bounds);
10801084

1085+
if (this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_WORD) {
1086+
var anyWhiteSpace = /\s+/g;
1087+
var anyNonWhiteSpace = /\S+/g;
1088+
1089+
var left = subWrappedText[subWrappedText.length - 1];
1090+
var right = cutText[0];
1091+
1092+
if (!(anyWhiteSpace.test(left) || anyWhiteSpace.test(right))) {
1093+
if (anyWhiteSpace.test(subWrappedText)) {
1094+
var words = subWrappedText.match(anyNonWhiteSpace);
1095+
pos = subWrappedText.lastIndexOf(words[words.length - 1]);
1096+
} else {
1097+
var tt = anyNonWhiteSpace.exec(text)[0];
1098+
pos = tt.length;
1099+
}
1100+
}
1101+
}
1102+
10811103
return pos;
10821104
};
10831105

@@ -1281,13 +1303,12 @@ acgraph.vector.Text.prototype.addSegment = function(text, opt_style) {
12811303
// cut characters.
12821304

12831305
while ((this.currentLineWidth_ + segment_bounds.width + shift > this.width_) && !this.stopAddSegments_) {
1284-
12851306
// calculate the position where to cut.
12861307
var cutPos = this.cutTextSegment_(text, style, shift + this.currentLineWidth_, this.width_, segment_bounds);
12871308

12881309
if (cutPos < 1 && (this.currentLine_.length == 0)) cutPos = 1;
12891310
if (cutPos != 0) {
1290-
var cutText = goog.string.trimRight(text.substring(0, cutPos));
1311+
var cutText = text.substring(0, cutPos);
12911312
segment_bounds = this.getTextBounds(cutText, style);
12921313
this.createSegment_(cutText, style, segment_bounds);
12931314
}
@@ -1299,7 +1320,8 @@ acgraph.vector.Text.prototype.addSegment = function(text, opt_style) {
12991320

13001321
shift = 0;
13011322

1302-
if (this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_LETTER) {
1323+
if (this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_LETTER ||
1324+
this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_WORD) {
13031325
text = goog.string.trimLeft(text.substring(cutPos, text.length));
13041326
segment_bounds = this.getTextBounds(text, style);
13051327
} else {
@@ -1320,7 +1342,8 @@ acgraph.vector.Text.prototype.addSegment = function(text, opt_style) {
13201342
* Finalizes text line.
13211343
*/
13221344
acgraph.vector.Text.prototype.finalizeTextLine = function() {
1323-
if ((this.textWrap() == acgraph.vector.Text.TextWrap.NO_WRAP) &&
1345+
var textWrap = this.textWrap();
1346+
if ((textWrap == acgraph.vector.Text.TextWrap.NO_WRAP) &&
13241347
(this.textLines_.length == 1) &&
13251348
!this.htmlOn_) {
13261349
this.applyTextOverflow_();
@@ -1687,6 +1710,7 @@ acgraph.vector.Text.prototype.disposeInternal = function() {
16871710
proto['selectable'] = proto.selectable;
16881711
goog.exportSymbol('acgraph.vector.Text.TextWrap.NO_WRAP', acgraph.vector.Text.TextWrap.NO_WRAP);
16891712
goog.exportSymbol('acgraph.vector.Text.TextWrap.BY_LETTER', acgraph.vector.Text.TextWrap.BY_LETTER);
1713+
goog.exportSymbol('acgraph.vector.Text.TextWrap.BY_WORD', acgraph.vector.Text.TextWrap.BY_WORD);
16901714
goog.exportSymbol('acgraph.vector.Text.TextOverflow.CLIP', acgraph.vector.Text.TextOverflow.CLIP);
16911715
goog.exportSymbol('acgraph.vector.Text.TextOverflow.ELLIPSIS', acgraph.vector.Text.TextOverflow.ELLIPSIS);
16921716
goog.exportSymbol('acgraph.vector.Text.FontStyle.ITALIC', acgraph.vector.Text.FontStyle.ITALIC);

0 commit comments

Comments
 (0)