Skip to content

Commit

Permalink
fix for lineheight and text selection (fabricjs#3094)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Jul 10, 2016
1 parent 026c3e6 commit b101990
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
18 changes: 12 additions & 6 deletions fabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -3542,7 +3542,8 @@ if (typeof console !== 'undefined') {

function hasAncestorWithNodeName(element, nodeName) {
while (element && (element = element.parentNode)) {
if (element.nodeName && nodeName.test(element.nodeName.replace('svg:', '')) && !element.getAttribute('instantiated_by_use')) {
if (element.nodeName && nodeName.test(element.nodeName.replace('svg:', ''))
&& !element.getAttribute('instantiated_by_use')) {
return true;
}
}
Expand Down Expand Up @@ -22006,7 +22007,7 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass(/** @lends fabric.Imag
for (var i = startLine; i <= endLine; i++) {
var lineOffset = this._getLineLeftOffset(this._getLineWidth(ctx, i)) || 0,
lineHeight = this._getHeightOfLine(this.ctx, i),
boxWidth = 0, line = this._textLines[i];
realLineHeight = 0, boxWidth = 0, line = this._textLines[i];

if (i === startLine) {
for (var j = 0, len = line.length; j < len; j++) {
Expand All @@ -22026,13 +22027,17 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass(/** @lends fabric.Imag
boxWidth += this._getWidthOfChar(ctx, line[j2], i, j2);
}
}
realLineHeight = lineHeight;
if (this.lineHeight < 1 || (i === endLine && this.lineHeight > 1)) {
lineHeight /= this.lineHeight;
}
ctx.fillRect(
boundaries.left + lineOffset,
boundaries.top + boundaries.topOffset,
boxWidth,
lineHeight);

boundaries.topOffset += lineHeight;
boundaries.topOffset += realLineHeight;
}
},

Expand Down Expand Up @@ -22552,9 +22557,10 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass(/** @lends fabric.Imag
* @param {CanvasRenderingContext2D} ctx Context to render on
*/
_getTextHeight: function(ctx) {
var height = this._getHeightOfLine(ctx, 0) / this.lineHeight;
for (var i = 1, len = this._textLines.length; i < len; i++) {
height += this._getHeightOfLine(ctx, i);
var lineHeight, height = 0;
for (var i = 0, len = this._textLines.length; i < len; i++) {
lineHeight = this._getHeightOfLine(ctx, i);
height += (i === len - 1 ? lineHeight / this.lineHeight : lineHeight);
}
return height;
},
Expand Down
4 changes: 2 additions & 2 deletions fabric.min.js

Large diffs are not rendered by default.

Binary file modified fabric.min.js.gz
Binary file not shown.
15 changes: 10 additions & 5 deletions fabric.require.js
Original file line number Diff line number Diff line change
Expand Up @@ -10298,7 +10298,7 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass({
ctx.fillStyle = this.selectionColor;
var start = this.get2DCursorLocation(this.selectionStart), end = this.get2DCursorLocation(this.selectionEnd), startLine = start.lineIndex, endLine = end.lineIndex;
for (var i = startLine; i <= endLine; i++) {
var lineOffset = this._getLineLeftOffset(this._getLineWidth(ctx, i)) || 0, lineHeight = this._getHeightOfLine(this.ctx, i), boxWidth = 0, line = this._textLines[i];
var lineOffset = this._getLineLeftOffset(this._getLineWidth(ctx, i)) || 0, lineHeight = this._getHeightOfLine(this.ctx, i), realLineHeight = 0, boxWidth = 0, line = this._textLines[i];
if (i === startLine) {
for (var j = 0, len = line.length; j < len; j++) {
if (j >= start.charIndex && (i !== endLine || j < end.charIndex)) {
Expand All @@ -10315,8 +10315,12 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass({
boxWidth += this._getWidthOfChar(ctx, line[j2], i, j2);
}
}
realLineHeight = lineHeight;
if (this.lineHeight < 1 || i === endLine && this.lineHeight > 1) {
lineHeight /= this.lineHeight;
}
ctx.fillRect(boundaries.left + lineOffset, boundaries.top + boundaries.topOffset, boxWidth, lineHeight);
boundaries.topOffset += lineHeight;
boundaries.topOffset += realLineHeight;
}
},
_renderChars: function(method, ctx, line, left, top, lineIndex, charOffset) {
Expand Down Expand Up @@ -10575,9 +10579,10 @@ fabric.Image.filters.BaseFilter = fabric.util.createClass({
return this.__lineHeights[lineIndex];
},
_getTextHeight: function(ctx) {
var height = this._getHeightOfLine(ctx, 0) / this.lineHeight;
for (var i = 1, len = this._textLines.length; i < len; i++) {
height += this._getHeightOfLine(ctx, i);
var lineHeight, height = 0;
for (var i = 0, len = this._textLines.length; i < len; i++) {
lineHeight = this._getHeightOfLine(ctx, i);
height += i === len - 1 ? lineHeight / this.lineHeight : lineHeight;
}
return height;
},
Expand Down

0 comments on commit b101990

Please sign in to comment.