Skip to content

Commit 69cd736

Browse files
committed
DVF-3065 word wrap bug
1 parent e89285b commit 69cd736

File tree

1 file changed

+53
-19
lines changed

1 file changed

+53
-19
lines changed

src/vector/Text.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,11 @@ acgraph.vector.Text.prototype.getTextBounds = function(text, segmentStyle) {
10641064
* @param {number} a Value of low limit of range.
10651065
* @param {number} b Value of high limit of range.
10661066
* @param {goog.math.Rect} segmentBounds Segment bounds.
1067+
* @param {boolean=} opt_ignoreByWord Ignore by word mode.
10671068
* @return {number} Position in inbound text where it has been cut.
10681069
* @private
10691070
*/
1070-
acgraph.vector.Text.prototype.cutTextSegment_ = function(text, style, a, b, segmentBounds) {
1071+
acgraph.vector.Text.prototype.cutTextSegment_ = function(text, style, a, b, segmentBounds, opt_ignoreByWord) {
10711072
var subWrappedText;
10721073
var subSegmentBounds;
10731074

@@ -1098,7 +1099,7 @@ acgraph.vector.Text.prototype.cutTextSegment_ = function(text, style, a, b, segm
10981099
bounds.width = cutTextWidth;
10991100
acgraph.getRenderer().textBounds(cutText, resultStatus, bounds);
11001101

1101-
if (this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_WORD) {
1102+
if (this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_WORD && !opt_ignoreByWord) {
11021103
var anyWhiteSpace = /\s+/g;
11031104
var anyNonWhiteSpace = /\S+/g;
11041105

@@ -1165,19 +1166,20 @@ acgraph.vector.Text.prototype.createSegment_ = function(text, style, bounds, opt
11651166
* Applying text overflow properties.
11661167
* If set width, height and text Overflow properties of text, and text out of the height then it will
11671168
* be cropped and joined ellipsis.
1169+
* @param {!Array.<acgraph.vector.TextSegment>=} opt_line .
11681170
* @private
11691171
*/
1170-
acgraph.vector.Text.prototype.applyTextOverflow_ = function() {
1172+
acgraph.vector.Text.prototype.applyTextOverflow_ = function(opt_line) {
11711173
var segment, index, cutPos, textSegmentEllipsis;
11721174

1173-
var line = /** @type {!Array.<acgraph.vector.TextSegment>} */ (goog.array.peek(this.textLines_));
1175+
var line = /** @type {!Array.<acgraph.vector.TextSegment>} */ (opt_line || goog.array.peek(this.textLines_));
11741176
var peekSegment = /** @type {!acgraph.vector.TextSegment} */ (goog.array.peek(line));
11751177
var ellipsisBounds = this.getTextBounds(this.ellipsis_, peekSegment.getStyle());
11761178
// Copy ellipsis to avoid overwriting this.ellipsis_ because "..." can be ".." (cut) when resize happened.
11771179
var ellipsis = this.ellipsis_;
11781180

11791181
if (ellipsisBounds.width > this.width_) {
1180-
cutPos = this.cutTextSegment_(this.ellipsis_, peekSegment.getStyle(), 0, this.width_, ellipsisBounds);
1182+
cutPos = this.cutTextSegment_(this.ellipsis_, peekSegment.getStyle(), 0, this.width_, ellipsisBounds, true);
11811183
ellipsis = this.ellipsis_.substring(0, cutPos);
11821184
}
11831185

@@ -1232,7 +1234,7 @@ acgraph.vector.Text.prototype.applyTextOverflow_ = function() {
12321234
this.currentBaseLine_ = 0;
12331235

12341236
segmentBounds = this.getTextBounds(segment.text, segment.getStyle());
1235-
cutPos = this.cutTextSegment_(segment.text, segment.getStyle(), left, right, segmentBounds);
1237+
cutPos = this.cutTextSegment_(segment.text, segment.getStyle(), left, right, segmentBounds, true);
12361238

12371239
if (cutPos < 1) cutPos = 1;
12381240
var cutText = segment.text.substring(0, cutPos);
@@ -1243,7 +1245,7 @@ acgraph.vector.Text.prototype.applyTextOverflow_ = function() {
12431245
lastSegmentInline.y = segment.y;
12441246

12451247
if (segment_bounds.width + ellipsisBounds.width > this.width_) {
1246-
cutPos = this.cutTextSegment_(this.ellipsis_, peekSegment.getStyle(), segment_bounds.width, this.width_, ellipsisBounds);
1248+
cutPos = this.cutTextSegment_(this.ellipsis_, peekSegment.getStyle(), segment_bounds.width, this.width_, ellipsisBounds, true);
12471249
ellipsis = this.ellipsis_.substring(0, cutPos);
12481250
}
12491251
if (cutPos > 0) {
@@ -1271,22 +1273,21 @@ acgraph.vector.Text.prototype.applyTextOverflow_ = function() {
12711273
index = goog.array.indexOf(this.segments_, firstLineSegment);
12721274
goog.array.insertAt(this.segments_, textSegmentEllipsis, index);
12731275
}
1274-
this.stopAddSegments_ = true;
12751276
};
12761277

12771278

12781279
/**
12791280
* Added break line.
12801281
*/
12811282
acgraph.vector.Text.prototype.addBreak = function() {
1282-
if (this.currentLineEmpty_) {
1283-
this.addSegment('');
1284-
}
1283+
// if (this.currentLineEmpty_) {
1284+
// this.addSegment('', null, true);
1285+
// }
12851286
this.finalizeTextLine();
12861287

12871288
this.currentNumberSeqBreaks_++;
12881289

1289-
this.addSegment('');
1290+
this.addSegment('', null, true);
12901291

12911292
var height = this.currentLine_[0] ? this.currentLine_[0].height : 0;
12921293
this.accumulatedHeight_ += goog.isString(this.lineHeight_) ?
@@ -1299,8 +1300,9 @@ acgraph.vector.Text.prototype.addBreak = function() {
12991300
* Adding text segment. Only here text wrapping is check.
13001301
* @param {string} text Segment text without tags and EOLs.
13011302
* @param {?acgraph.vector.TextSegmentStyle=} opt_style Segment style.
1303+
* @param {boolean=} opt_break Whether is break.
13021304
*/
1303-
acgraph.vector.Text.prototype.addSegment = function(text, opt_style) {
1305+
acgraph.vector.Text.prototype.addSegment = function(text, opt_style, opt_break) {
13041306
// if "stop adding" segments flags is set - do nothing
13051307
// this can happen if text has height, width and textOverflow set and and doesn't fit.
13061308
if (this.stopAddSegments_) return;
@@ -1324,7 +1326,7 @@ acgraph.vector.Text.prototype.addSegment = function(text, opt_style) {
13241326

13251327
if (cutPos < 1 && (this.currentLine_.length == 0)) cutPos = 1;
13261328
if (cutPos != 0) {
1327-
var cutText = text.substring(0, cutPos);
1329+
var cutText = goog.string.trimLeft(text.substring(0, cutPos));
13281330
segment_bounds = this.getTextBounds(cutText, style);
13291331
this.createSegment_(cutText, style, segment_bounds);
13301332
}
@@ -1344,13 +1346,16 @@ acgraph.vector.Text.prototype.addSegment = function(text, opt_style) {
13441346
if (this.htmlOn_) {
13451347
text = '';
13461348
segment_bounds = this.getTextBounds(text, style);
1347-
} else
1349+
} else {
1350+
this.applyTextOverflow_();
13481351
this.stopAddSegments_ = true;
1352+
}
13491353
}
13501354
}
13511355
}
13521356

1353-
if (!this.stopAddSegments_) this.createSegment_(text, style, segment_bounds);
1357+
if (!this.stopAddSegments_ && (text.length || opt_break))
1358+
this.createSegment_(text, style, segment_bounds);
13541359
};
13551360

13561361

@@ -1361,8 +1366,9 @@ acgraph.vector.Text.prototype.finalizeTextLine = function() {
13611366
var textWrap = this.textWrap();
13621367
if ((textWrap == acgraph.vector.Text.TextWrap.NO_WRAP) &&
13631368
(this.textLines_.length == 1) &&
1364-
!this.htmlOn_) {
1369+
!this.htmlOn_ && !this.stopAddSegments_) {
13651370
this.applyTextOverflow_();
1371+
this.stopAddSegments_ = true;
13661372
}
13671373
// if there is a flag to stop adding segements - stop it.
13681374
// this can happen if width, heigth and textOverflow are set and text doesn't fit.
@@ -1376,9 +1382,9 @@ acgraph.vector.Text.prototype.finalizeTextLine = function() {
13761382
(this.realHeigth + this.currentLineHeight_ > this.height_) &&
13771383
this.textLines_.length != 0;
13781384

1379-
13801385
if (endOfText) {
13811386
this.applyTextOverflow_();
1387+
this.stopAddSegments_ = true;
13821388
} else {
13831389
// calculate line height.
13841390
this.currentLineHeight_ = goog.isString(this.lineHeight_) ?
@@ -1460,6 +1466,34 @@ acgraph.vector.Text.prototype.finalizeTextLine = function() {
14601466
}
14611467
}
14621468

1469+
if (goog.isDefAndNotNull(this.style_['width']) && this.style_['textWrap'] == acgraph.vector.Text.TextWrap.BY_WORD &&
1470+
this.currentLineWidth_ > this.width_) {
1471+
if (this.currentLine_.length > 1 && !this.currentLine_[0].text.length) {
1472+
goog.array.removeAt(this.currentLine_, 0);
1473+
var index = goog.array.indexOf(this.segments_, this.currentLine_[0]);
1474+
goog.array.removeAt(this.segments_, index);
1475+
}
1476+
1477+
segment = goog.array.peek(this.currentLine_);
1478+
var segment_bounds = this.getTextBounds(segment.text, segment.getStyle());
1479+
1480+
var cutPos = this.cutTextSegment_(segment.text, segment.getStyle(), 0, this.width_, segment_bounds, true);
1481+
var cutText = segment.text.substring(0, cutPos);
1482+
segment_bounds = this.getTextBounds(cutText, segment.getStyle());
1483+
segment.text = cutText;
1484+
segment.width = segment_bounds.width;
1485+
1486+
this.currentLineWidth_ = segment_bounds.width;
1487+
this.prevLineWidth_ = this.currentLineWidth_;
1488+
1489+
this.applyTextOverflow_(this.currentLine_);
1490+
1491+
if (this.style_['hAlign'] == acgraph.vector.Text.HAlign.CENTER) {
1492+
segment = this.currentLine_[0];
1493+
segment.dx = -this.width_ / 2 + this.currentLineWidth_ / 2;
1494+
}
1495+
}
1496+
14631497
// calculate real width and height of the text.
14641498
this.realHeigth += this.currentLineHeight_;
14651499
this.realWidth = Math.max(this.realWidth, this.currentLineWidth_);
@@ -1567,7 +1601,7 @@ acgraph.vector.Text.prototype.textDefragmentation = function() {
15671601

15681602
} else {
15691603
for (i = 0; i < textArr.length; i++) {
1570-
text = textArr[i];
1604+
text = goog.string.trimLeft(textArr[i]);
15711605
if (goog.isDefAndNotNull(text)) {
15721606
if (text == '') {
15731607
this.addBreak();

0 commit comments

Comments
 (0)